xref: /titanic_51/usr/src/uts/common/inet/ip/ip_if.c (revision ac19272f7eb4a433cfccf2fdccc769cca5528169)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #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 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
135 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
136 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
143     mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
149     int ioccmd, struct linkblk *li, boolean_t doconsist);
150 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
151 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
152 static void	ipsq_flush(ill_t *ill);
153 
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
162 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
163 		    boolean_t isv6);
164 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
165 static void	ipif_delete_cache_ire(ire_t *, char *);
166 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
167 static void	ipif_free(ipif_t *ipif);
168 static void	ipif_free_tail(ipif_t *ipif);
169 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
170 static void	ipif_multicast_down(ipif_t *ipif);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static boolean_t ill_has_usable_ipif(ill_t *);
194 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
195 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
196 static void	ill_phyint_free(ill_t *ill);
197 static void	ill_phyint_reinit(ill_t *ill);
198 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
199 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
200 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
201 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
202 static void	ill_stq_cache_delete(ire_t *, char *);
203 
204 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
211 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     in6_addr_t *);
213 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     ipaddr_t *);
215 
216 static void	ipif_save_ire(ipif_t *, ire_t *);
217 static void	ipif_remove_ire(ipif_t *, ire_t *);
218 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
219 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
220 
221 /*
222  * Per-ill IPsec capabilities management.
223  */
224 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
225 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
226 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
227 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
228 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
229 static void ill_capability_proto(ill_t *, int, mblk_t *);
230 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
231     boolean_t);
232 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
235 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
237 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
239 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
240     dl_capability_sub_t *);
241 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
242 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static void ill_capability_lso_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
245 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
246 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
247 static void	ill_capability_dls_disable(ill_t *);
248 
249 static void	illgrp_cache_delete(ire_t *, char *);
250 static void	illgrp_delete(ill_t *ill);
251 static void	illgrp_reset_schednext(ill_t *ill);
252 
253 static ill_t	*ill_prev_usesrc(ill_t *);
254 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
255 static void	ill_disband_usesrc_group(ill_t *);
256 
257 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
258 
259 #ifdef DEBUG
260 static	void	ill_trace_cleanup(const ill_t *);
261 static	void	ipif_trace_cleanup(const ipif_t *);
262 #endif
263 
264 /*
265  * if we go over the memory footprint limit more than once in this msec
266  * interval, we'll start pruning aggressively.
267  */
268 int ip_min_frag_prune_time = 0;
269 
270 /*
271  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
272  * and the IPsec DOI
273  */
274 #define	MAX_IPSEC_ALGS	256
275 
276 #define	BITSPERBYTE	8
277 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
278 
279 #define	IPSEC_ALG_ENABLE(algs, algid) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
284 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
285 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
286 
287 typedef uint8_t ipsec_capab_elem_t;
288 
289 /*
290  * Per-algorithm parameters.  Note that at present, only encryption
291  * algorithms have variable keysize (IKE does not provide a way to negotiate
292  * auth algorithm keysize).
293  *
294  * All sizes here are in bits.
295  */
296 typedef struct
297 {
298 	uint16_t	minkeylen;
299 	uint16_t	maxkeylen;
300 } ipsec_capab_algparm_t;
301 
302 /*
303  * Per-ill capabilities.
304  */
305 struct ill_ipsec_capab_s {
306 	ipsec_capab_elem_t *encr_hw_algs;
307 	ipsec_capab_elem_t *auth_hw_algs;
308 	uint32_t algs_size;	/* size of _hw_algs in bytes */
309 	/* algorithm key lengths */
310 	ipsec_capab_algparm_t *encr_algparm;
311 	uint32_t encr_algparm_size;
312 	uint32_t encr_algparm_end;
313 };
314 
315 /*
316  * The field values are larger than strictly necessary for simple
317  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
318  */
319 static area_t	ip_area_template = {
320 	AR_ENTRY_ADD,			/* area_cmd */
321 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
322 					/* area_name_offset */
323 	/* area_name_length temporarily holds this structure length */
324 	sizeof (area_t),			/* area_name_length */
325 	IP_ARP_PROTO_TYPE,		/* area_proto */
326 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
327 	IP_ADDR_LEN,			/* area_proto_addr_length */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
329 					/* area_proto_mask_offset */
330 	0,				/* area_flags */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
332 					/* area_hw_addr_offset */
333 	/* Zero length hw_addr_length means 'use your idea of the address' */
334 	0				/* area_hw_addr_length */
335 };
336 
337 /*
338  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
339  * support
340  */
341 static area_t	ip6_area_template = {
342 	AR_ENTRY_ADD,			/* area_cmd */
343 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
344 					/* area_name_offset */
345 	/* area_name_length temporarily holds this structure length */
346 	sizeof (area_t),			/* area_name_length */
347 	IP_ARP_PROTO_TYPE,		/* area_proto */
348 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
349 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
351 					/* area_proto_mask_offset */
352 	0,				/* area_flags */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
354 					/* area_hw_addr_offset */
355 	/* Zero length hw_addr_length means 'use your idea of the address' */
356 	0				/* area_hw_addr_length */
357 };
358 
359 static ared_t	ip_ared_template = {
360 	AR_ENTRY_DELETE,
361 	sizeof (ared_t) + IP_ADDR_LEN,
362 	sizeof (ared_t),
363 	IP_ARP_PROTO_TYPE,
364 	sizeof (ared_t),
365 	IP_ADDR_LEN
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN
375 };
376 
377 /*
378  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
379  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
380  * areq is used).
381  */
382 static areq_t	ip_areq_template = {
383 	AR_ENTRY_QUERY,			/* cmd */
384 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
385 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
386 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
387 	sizeof (areq_t),			/* target addr offset */
388 	IP_ADDR_LEN,			/* target addr_length */
389 	0,				/* flags */
390 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
391 	IP_ADDR_LEN,			/* sender addr length */
392 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
393 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
394 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
395 	/* anything else filled in by the code */
396 };
397 
398 static arc_t	ip_aru_template = {
399 	AR_INTERFACE_UP,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_ard_template = {
405 	AR_INTERFACE_DOWN,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aron_template = {
411 	AR_INTERFACE_ON,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 static arc_t	ip_aroff_template = {
417 	AR_INTERFACE_OFF,
418 	sizeof (arc_t),		/* Name offset */
419 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
420 };
421 
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 
916 	if (ill->ill_frag_ptr != NULL) {
917 		uint_t count;
918 
919 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
920 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
921 		}
922 		mi_free(ill->ill_frag_ptr);
923 		ill->ill_frag_ptr = NULL;
924 		ill->ill_frag_hash_tbl = NULL;
925 	}
926 
927 	freemsg(ill->ill_nd_lla_mp);
928 	/* Free all retained control messages. */
929 	mpp = &ill->ill_first_mp_to_free;
930 	do {
931 		while (mpp[0]) {
932 			mblk_t  *mp;
933 			mblk_t  *mp1;
934 
935 			mp = mpp[0];
936 			mpp[0] = mp->b_next;
937 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
938 				mp1->b_next = NULL;
939 				mp1->b_prev = NULL;
940 			}
941 			freemsg(mp);
942 		}
943 	} while (mpp++ != &ill->ill_last_mp_to_free);
944 
945 	ill_free_mib(ill);
946 
947 #ifdef DEBUG
948 	ill_trace_cleanup(ill);
949 #endif
950 
951 	/* Drop refcnt here */
952 	netstack_rele(ill->ill_ipst->ips_netstack);
953 	ill->ill_ipst = NULL;
954 }
955 
956 static void
957 ill_free_mib(ill_t *ill)
958 {
959 	ip_stack_t *ipst = ill->ill_ipst;
960 
961 	/*
962 	 * MIB statistics must not be lost, so when an interface
963 	 * goes away the counter values will be added to the global
964 	 * MIBs.
965 	 */
966 	if (ill->ill_ip_mib != NULL) {
967 		if (ill->ill_isv6) {
968 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
969 			    ill->ill_ip_mib);
970 		} else {
971 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
972 			    ill->ill_ip_mib);
973 		}
974 
975 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
976 		ill->ill_ip_mib = NULL;
977 	}
978 	if (ill->ill_icmp6_mib != NULL) {
979 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
980 		    ill->ill_icmp6_mib);
981 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
982 		ill->ill_icmp6_mib = NULL;
983 	}
984 }
985 
986 /*
987  * Concatenate together a physical address and a sap.
988  *
989  * Sap_lengths are interpreted as follows:
990  *   sap_length == 0	==>	no sap
991  *   sap_length > 0	==>	sap is at the head of the dlpi address
992  *   sap_length < 0	==>	sap is at the tail of the dlpi address
993  */
994 static void
995 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
996     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
997 {
998 	uint16_t sap_addr = (uint16_t)sap_src;
999 
1000 	if (sap_length == 0) {
1001 		if (phys_src == NULL)
1002 			bzero(dst, phys_length);
1003 		else
1004 			bcopy(phys_src, dst, phys_length);
1005 	} else if (sap_length < 0) {
1006 		if (phys_src == NULL)
1007 			bzero(dst, phys_length);
1008 		else
1009 			bcopy(phys_src, dst, phys_length);
1010 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1011 	} else {
1012 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1013 		if (phys_src == NULL)
1014 			bzero((char *)dst + sap_length, phys_length);
1015 		else
1016 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1017 	}
1018 }
1019 
1020 /*
1021  * Generate a dl_unitdata_req mblk for the device and address given.
1022  * addr_length is the length of the physical portion of the address.
1023  * If addr is NULL include an all zero address of the specified length.
1024  * TRUE? In any case, addr_length is taken to be the entire length of the
1025  * dlpi address, including the absolute value of sap_length.
1026  */
1027 mblk_t *
1028 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1029 		t_scalar_t sap_length)
1030 {
1031 	dl_unitdata_req_t *dlur;
1032 	mblk_t	*mp;
1033 	t_scalar_t	abs_sap_length;		/* absolute value */
1034 
1035 	abs_sap_length = ABS(sap_length);
1036 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1037 	    DL_UNITDATA_REQ);
1038 	if (mp == NULL)
1039 		return (NULL);
1040 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1041 	/* HACK: accomodate incompatible DLPI drivers */
1042 	if (addr_length == 8)
1043 		addr_length = 6;
1044 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1045 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1046 	dlur->dl_priority.dl_min = 0;
1047 	dlur->dl_priority.dl_max = 0;
1048 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1049 	    (uchar_t *)&dlur[1]);
1050 	return (mp);
1051 }
1052 
1053 /*
1054  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1055  * Return an error if we already have 1 or more ioctls in progress.
1056  * This is used only for non-exclusive ioctls. Currently this is used
1057  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1058  * and thus need to use ipsq_pending_mp_add.
1059  */
1060 boolean_t
1061 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1062 {
1063 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1064 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1065 	/*
1066 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1067 	 */
1068 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1069 	    (add_mp->b_datap->db_type == M_IOCTL));
1070 
1071 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1072 	/*
1073 	 * Return error if the conn has started closing. The conn
1074 	 * could have finished cleaning up the pending mp list,
1075 	 * If so we should not add another mp to the list negating
1076 	 * the cleanup.
1077 	 */
1078 	if (connp->conn_state_flags & CONN_CLOSING)
1079 		return (B_FALSE);
1080 	/*
1081 	 * Add the pending mp to the head of the list, chained by b_next.
1082 	 * Note down the conn on which the ioctl request came, in b_prev.
1083 	 * This will be used to later get the conn, when we get a response
1084 	 * on the ill queue, from some other module (typically arp)
1085 	 */
1086 	add_mp->b_next = (void *)ill->ill_pending_mp;
1087 	add_mp->b_queue = CONNP_TO_WQ(connp);
1088 	ill->ill_pending_mp = add_mp;
1089 	if (connp != NULL)
1090 		connp->conn_oper_pending_ill = ill;
1091 	return (B_TRUE);
1092 }
1093 
1094 /*
1095  * Retrieve the ill_pending_mp and return it. We have to walk the list
1096  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1097  */
1098 mblk_t *
1099 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1100 {
1101 	mblk_t	*prev = NULL;
1102 	mblk_t	*curr = NULL;
1103 	uint_t	id;
1104 	conn_t	*connp;
1105 
1106 	/*
1107 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1108 	 * up the pending mp, but it does not know the ioc_id and
1109 	 * passes in a zero for it.
1110 	 */
1111 	mutex_enter(&ill->ill_lock);
1112 	if (ioc_id != 0)
1113 		*connpp = NULL;
1114 
1115 	/* Search the list for the appropriate ioctl based on ioc_id */
1116 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1117 	    prev = curr, curr = curr->b_next) {
1118 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1119 		connp = Q_TO_CONN(curr->b_queue);
1120 		/* Match based on the ioc_id or based on the conn */
1121 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1122 			break;
1123 	}
1124 
1125 	if (curr != NULL) {
1126 		/* Unlink the mblk from the pending mp list */
1127 		if (prev != NULL) {
1128 			prev->b_next = curr->b_next;
1129 		} else {
1130 			ASSERT(ill->ill_pending_mp == curr);
1131 			ill->ill_pending_mp = curr->b_next;
1132 		}
1133 
1134 		/*
1135 		 * conn refcnt must have been bumped up at the start of
1136 		 * the ioctl. So we can safely access the conn.
1137 		 */
1138 		ASSERT(CONN_Q(curr->b_queue));
1139 		*connpp = Q_TO_CONN(curr->b_queue);
1140 		curr->b_next = NULL;
1141 		curr->b_queue = NULL;
1142 	}
1143 
1144 	mutex_exit(&ill->ill_lock);
1145 
1146 	return (curr);
1147 }
1148 
1149 /*
1150  * Add the pending mp to the list. There can be only 1 pending mp
1151  * in the list. Any exclusive ioctl that needs to wait for a response
1152  * from another module or driver needs to use this function to set
1153  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1154  * the other module/driver. This is also used while waiting for the
1155  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1156  */
1157 boolean_t
1158 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1159     int waitfor)
1160 {
1161 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1162 
1163 	ASSERT(IAM_WRITER_IPIF(ipif));
1164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1165 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1166 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1167 	/*
1168 	 * The caller may be using a different ipif than the one passed into
1169 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1170 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1171 	 * that `ipsq_current_ipif == ipif'.
1172 	 */
1173 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1174 
1175 	/*
1176 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1177 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1178 	 */
1179 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1180 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1181 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1182 
1183 	if (connp != NULL) {
1184 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1185 		/*
1186 		 * Return error if the conn has started closing. The conn
1187 		 * could have finished cleaning up the pending mp list,
1188 		 * If so we should not add another mp to the list negating
1189 		 * the cleanup.
1190 		 */
1191 		if (connp->conn_state_flags & CONN_CLOSING)
1192 			return (B_FALSE);
1193 	}
1194 	mutex_enter(&ipsq->ipsq_lock);
1195 	ipsq->ipsq_pending_ipif = ipif;
1196 	/*
1197 	 * Note down the queue in b_queue. This will be returned by
1198 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1199 	 * the processing
1200 	 */
1201 	add_mp->b_next = NULL;
1202 	add_mp->b_queue = q;
1203 	ipsq->ipsq_pending_mp = add_mp;
1204 	ipsq->ipsq_waitfor = waitfor;
1205 
1206 	if (connp != NULL)
1207 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1208 	mutex_exit(&ipsq->ipsq_lock);
1209 	return (B_TRUE);
1210 }
1211 
1212 /*
1213  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1214  * queued in the list.
1215  */
1216 mblk_t *
1217 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1218 {
1219 	mblk_t	*curr = NULL;
1220 
1221 	mutex_enter(&ipsq->ipsq_lock);
1222 	*connpp = NULL;
1223 	if (ipsq->ipsq_pending_mp == NULL) {
1224 		mutex_exit(&ipsq->ipsq_lock);
1225 		return (NULL);
1226 	}
1227 
1228 	/* There can be only 1 such excl message */
1229 	curr = ipsq->ipsq_pending_mp;
1230 	ASSERT(curr != NULL && curr->b_next == NULL);
1231 	ipsq->ipsq_pending_ipif = NULL;
1232 	ipsq->ipsq_pending_mp = NULL;
1233 	ipsq->ipsq_waitfor = 0;
1234 	mutex_exit(&ipsq->ipsq_lock);
1235 
1236 	if (CONN_Q(curr->b_queue)) {
1237 		/*
1238 		 * This mp did a refhold on the conn, at the start of the ioctl.
1239 		 * So we can safely return a pointer to the conn to the caller.
1240 		 */
1241 		*connpp = Q_TO_CONN(curr->b_queue);
1242 	} else {
1243 		*connpp = NULL;
1244 	}
1245 	curr->b_next = NULL;
1246 	curr->b_prev = NULL;
1247 	return (curr);
1248 }
1249 
1250 /*
1251  * Cleanup the ioctl mp queued in ipsq_pending_mp
1252  * - Called in the ill_delete path
1253  * - Called in the M_ERROR or M_HANGUP path on the ill.
1254  * - Called in the conn close path.
1255  */
1256 boolean_t
1257 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1258 {
1259 	mblk_t	*mp;
1260 	ipsq_t	*ipsq;
1261 	queue_t	*q;
1262 	ipif_t	*ipif;
1263 
1264 	ASSERT(IAM_WRITER_ILL(ill));
1265 	ipsq = ill->ill_phyint->phyint_ipsq;
1266 	mutex_enter(&ipsq->ipsq_lock);
1267 	/*
1268 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1269 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1270 	 * even if it is meant for another ill, since we have to enqueue
1271 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1272 	 * If connp is non-null we are called from the conn close path.
1273 	 */
1274 	mp = ipsq->ipsq_pending_mp;
1275 	if (mp == NULL || (connp != NULL &&
1276 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1277 		mutex_exit(&ipsq->ipsq_lock);
1278 		return (B_FALSE);
1279 	}
1280 	/* Now remove from the ipsq_pending_mp */
1281 	ipsq->ipsq_pending_mp = NULL;
1282 	q = mp->b_queue;
1283 	mp->b_next = NULL;
1284 	mp->b_prev = NULL;
1285 	mp->b_queue = NULL;
1286 
1287 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1288 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1289 	if (ill->ill_move_in_progress) {
1290 		ILL_CLEAR_MOVE(ill);
1291 	} else if (ill->ill_up_ipifs) {
1292 		ill_group_cleanup(ill);
1293 	}
1294 
1295 	ipif = ipsq->ipsq_pending_ipif;
1296 	ipsq->ipsq_pending_ipif = NULL;
1297 	ipsq->ipsq_waitfor = 0;
1298 	ipsq->ipsq_current_ipif = NULL;
1299 	ipsq->ipsq_current_ioctl = 0;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_dhcpinit_ill == ill) {
1530 		connp->conn_dhcpinit_ill = NULL;
1531 		ASSERT(ill->ill_dhcpinit != 0);
1532 		atomic_dec_32(&ill->ill_dhcpinit);
1533 	}
1534 	if (connp->conn_ire_cache != NULL) {
1535 		ire = connp->conn_ire_cache;
1536 		/*
1537 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1538 		 * interface X and ipif coming from interface Y, if interface
1539 		 * X and Y are part of the same IPMPgroup. Thus whenever
1540 		 * interface X goes down, remove all references to it by
1541 		 * checking both on ire_ipif and ire_stq.
1542 		 */
1543 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1544 		    (ire->ire_type == IRE_CACHE &&
1545 		    ire->ire_stq == ill->ill_wq)) {
1546 			connp->conn_ire_cache = NULL;
1547 			mutex_exit(&connp->conn_lock);
1548 			ire_refrele_notr(ire);
1549 			return;
1550 		}
1551 	}
1552 	mutex_exit(&connp->conn_lock);
1553 
1554 }
1555 
1556 /* ARGSUSED */
1557 void
1558 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1559 {
1560 	ill_t	*ill = q->q_ptr;
1561 	ipif_t	*ipif;
1562 
1563 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1564 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1565 		ipif_non_duplicate(ipif);
1566 		ipif_down_tail(ipif);
1567 	}
1568 	freemsg(mp);
1569 	ipsq_current_finish(ipsq);
1570 }
1571 
1572 /*
1573  * ill_down_start is called when we want to down this ill and bring it up again
1574  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1575  * all interfaces, but don't tear down any plumbing.
1576  */
1577 boolean_t
1578 ill_down_start(queue_t *q, mblk_t *mp)
1579 {
1580 	ill_t	*ill = q->q_ptr;
1581 	ipif_t	*ipif;
1582 
1583 	ASSERT(IAM_WRITER_ILL(ill));
1584 
1585 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1586 		(void) ipif_down(ipif, NULL, NULL);
1587 
1588 	ill_down(ill);
1589 
1590 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1591 
1592 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1593 
1594 	/*
1595 	 * Atomically test and add the pending mp if references are active.
1596 	 */
1597 	mutex_enter(&ill->ill_lock);
1598 	if (!ill_is_quiescent(ill)) {
1599 		/* call cannot fail since `conn_t *' argument is NULL */
1600 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1601 		    mp, ILL_DOWN);
1602 		mutex_exit(&ill->ill_lock);
1603 		return (B_FALSE);
1604 	}
1605 	mutex_exit(&ill->ill_lock);
1606 	return (B_TRUE);
1607 }
1608 
1609 static void
1610 ill_down(ill_t *ill)
1611 {
1612 	ip_stack_t	*ipst = ill->ill_ipst;
1613 
1614 	/* Blow off any IREs dependent on this ILL. */
1615 	ire_walk(ill_downi, (char *)ill, ipst);
1616 
1617 	/* Remove any conn_*_ill depending on this ill */
1618 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1619 
1620 	if (ill->ill_group != NULL) {
1621 		illgrp_delete(ill);
1622 	}
1623 }
1624 
1625 /*
1626  * ire_walk routine used to delete every IRE that depends on queues
1627  * associated with 'ill'.  (Always called as writer.)
1628  */
1629 static void
1630 ill_downi(ire_t *ire, char *ill_arg)
1631 {
1632 	ill_t	*ill = (ill_t *)ill_arg;
1633 
1634 	/*
1635 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1636 	 * interface X and ipif coming from interface Y, if interface
1637 	 * X and Y are part of the same IPMP group. Thus whenever interface
1638 	 * X goes down, remove all references to it by checking both
1639 	 * on ire_ipif and ire_stq.
1640 	 */
1641 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1642 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1643 		ire_delete(ire);
1644 	}
1645 }
1646 
1647 /*
1648  * Remove ire/nce from the fastpath list.
1649  */
1650 void
1651 ill_fastpath_nack(ill_t *ill)
1652 {
1653 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1654 }
1655 
1656 /* Consume an M_IOCACK of the fastpath probe. */
1657 void
1658 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1659 {
1660 	mblk_t	*mp1 = mp;
1661 
1662 	/*
1663 	 * If this was the first attempt turn on the fastpath probing.
1664 	 */
1665 	mutex_enter(&ill->ill_lock);
1666 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1667 		ill->ill_dlpi_fastpath_state = IDS_OK;
1668 	mutex_exit(&ill->ill_lock);
1669 
1670 	/* Free the M_IOCACK mblk, hold on to the data */
1671 	mp = mp->b_cont;
1672 	freeb(mp1);
1673 	if (mp == NULL)
1674 		return;
1675 	if (mp->b_cont != NULL) {
1676 		/*
1677 		 * Update all IRE's or NCE's that are waiting for
1678 		 * fastpath update.
1679 		 */
1680 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1681 		mp1 = mp->b_cont;
1682 		freeb(mp);
1683 		mp = mp1;
1684 	} else {
1685 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1686 	}
1687 
1688 	freeb(mp);
1689 }
1690 
1691 /*
1692  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1693  * The data portion of the request is a dl_unitdata_req_t template for
1694  * what we would send downstream in the absence of a fastpath confirmation.
1695  */
1696 int
1697 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1698 {
1699 	struct iocblk	*ioc;
1700 	mblk_t	*mp;
1701 
1702 	if (dlur_mp == NULL)
1703 		return (EINVAL);
1704 
1705 	mutex_enter(&ill->ill_lock);
1706 	switch (ill->ill_dlpi_fastpath_state) {
1707 	case IDS_FAILED:
1708 		/*
1709 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1710 		 * support it.
1711 		 */
1712 		mutex_exit(&ill->ill_lock);
1713 		return (ENOTSUP);
1714 	case IDS_UNKNOWN:
1715 		/* This is the first probe */
1716 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1717 		break;
1718 	default:
1719 		break;
1720 	}
1721 	mutex_exit(&ill->ill_lock);
1722 
1723 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1724 		return (EAGAIN);
1725 
1726 	mp->b_cont = copyb(dlur_mp);
1727 	if (mp->b_cont == NULL) {
1728 		freeb(mp);
1729 		return (EAGAIN);
1730 	}
1731 
1732 	ioc = (struct iocblk *)mp->b_rptr;
1733 	ioc->ioc_count = msgdsize(mp->b_cont);
1734 
1735 	putnext(ill->ill_wq, mp);
1736 	return (0);
1737 }
1738 
1739 void
1740 ill_capability_probe(ill_t *ill)
1741 {
1742 	/*
1743 	 * Do so only if capabilities are still unknown.
1744 	 */
1745 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1746 		return;
1747 
1748 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1749 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1750 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1751 }
1752 
1753 void
1754 ill_capability_reset(ill_t *ill)
1755 {
1756 	mblk_t *sc_mp = NULL;
1757 	mblk_t *tmp;
1758 
1759 	/*
1760 	 * Note here that we reset the state to UNKNOWN, and later send
1761 	 * down the DL_CAPABILITY_REQ without first setting the state to
1762 	 * INPROGRESS.  We do this in order to distinguish the
1763 	 * DL_CAPABILITY_ACK response which may come back in response to
1764 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1765 	 * also handle the case where the driver doesn't send us back
1766 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1767 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1768 	 * features are turned off until the state reaches IDS_OK.
1769 	 */
1770 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1771 	ill->ill_capab_reneg = B_FALSE;
1772 
1773 	/*
1774 	 * Disable sub-capabilities and request a list of sub-capability
1775 	 * messages which will be sent down to the driver.  Each handler
1776 	 * allocates the corresponding dl_capability_sub_t inside an
1777 	 * mblk, and links it to the existing sc_mp mblk, or return it
1778 	 * as sc_mp if it's the first sub-capability (the passed in
1779 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1780 	 * sc_mp will be pulled-up, before passing it downstream.
1781 	 */
1782 	ill_capability_mdt_reset(ill, &sc_mp);
1783 	ill_capability_hcksum_reset(ill, &sc_mp);
1784 	ill_capability_zerocopy_reset(ill, &sc_mp);
1785 	ill_capability_ipsec_reset(ill, &sc_mp);
1786 	ill_capability_dls_reset(ill, &sc_mp);
1787 	ill_capability_lso_reset(ill, &sc_mp);
1788 
1789 	/* Nothing to send down in order to disable the capabilities? */
1790 	if (sc_mp == NULL)
1791 		return;
1792 
1793 	tmp = msgpullup(sc_mp, -1);
1794 	freemsg(sc_mp);
1795 	if ((sc_mp = tmp) == NULL) {
1796 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1797 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1798 		return;
1799 	}
1800 
1801 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1802 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1803 }
1804 
1805 /*
1806  * Request or set new-style hardware capabilities supported by DLS provider.
1807  */
1808 static void
1809 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1810 {
1811 	mblk_t *mp;
1812 	dl_capability_req_t *capb;
1813 	size_t size = 0;
1814 	uint8_t *ptr;
1815 
1816 	if (reqp != NULL)
1817 		size = MBLKL(reqp);
1818 
1819 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1820 	if (mp == NULL) {
1821 		freemsg(reqp);
1822 		return;
1823 	}
1824 	ptr = mp->b_rptr;
1825 
1826 	capb = (dl_capability_req_t *)ptr;
1827 	ptr += sizeof (dl_capability_req_t);
1828 
1829 	if (reqp != NULL) {
1830 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1831 		capb->dl_sub_length = size;
1832 		bcopy(reqp->b_rptr, ptr, size);
1833 		ptr += size;
1834 		mp->b_cont = reqp->b_cont;
1835 		freeb(reqp);
1836 	}
1837 	ASSERT(ptr == mp->b_wptr);
1838 
1839 	ill_dlpi_send(ill, mp);
1840 }
1841 
1842 static void
1843 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1844 {
1845 	dl_capab_id_t *id_ic;
1846 	uint_t sub_dl_cap = outers->dl_cap;
1847 	dl_capability_sub_t *inners;
1848 	uint8_t *capend;
1849 
1850 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1851 
1852 	/*
1853 	 * Note: range checks here are not absolutely sufficient to
1854 	 * make us robust against malformed messages sent by drivers;
1855 	 * this is in keeping with the rest of IP's dlpi handling.
1856 	 * (Remember, it's coming from something else in the kernel
1857 	 * address space)
1858 	 */
1859 
1860 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1861 	if (capend > mp->b_wptr) {
1862 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1863 		    "malformed sub-capability too long for mblk");
1864 		return;
1865 	}
1866 
1867 	id_ic = (dl_capab_id_t *)(outers + 1);
1868 
1869 	if (outers->dl_length < sizeof (*id_ic) ||
1870 	    (inners = &id_ic->id_subcap,
1871 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1872 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1873 		    "encapsulated capab type %d too long for mblk",
1874 		    inners->dl_cap);
1875 		return;
1876 	}
1877 
1878 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1879 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1880 		    "isn't as expected; pass-thru module(s) detected, "
1881 		    "discarding capability\n", inners->dl_cap));
1882 		return;
1883 	}
1884 
1885 	/* Process the encapsulated sub-capability */
1886 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1887 }
1888 
1889 /*
1890  * Process Multidata Transmit capability negotiation ack received from a
1891  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1892  * DL_CAPABILITY_ACK message.
1893  */
1894 static void
1895 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1896 {
1897 	mblk_t *nmp = NULL;
1898 	dl_capability_req_t *oc;
1899 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1900 	ill_mdt_capab_t **ill_mdt_capab;
1901 	uint_t sub_dl_cap = isub->dl_cap;
1902 	uint8_t *capend;
1903 
1904 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1905 
1906 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1907 
1908 	/*
1909 	 * Note: range checks here are not absolutely sufficient to
1910 	 * make us robust against malformed messages sent by drivers;
1911 	 * this is in keeping with the rest of IP's dlpi handling.
1912 	 * (Remember, it's coming from something else in the kernel
1913 	 * address space)
1914 	 */
1915 
1916 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1917 	if (capend > mp->b_wptr) {
1918 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1919 		    "malformed sub-capability too long for mblk");
1920 		return;
1921 	}
1922 
1923 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1924 
1925 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1926 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1927 		    "unsupported MDT sub-capability (version %d, expected %d)",
1928 		    mdt_ic->mdt_version, MDT_VERSION_2);
1929 		return;
1930 	}
1931 
1932 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1933 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1934 		    "capability isn't as expected; pass-thru module(s) "
1935 		    "detected, discarding capability\n"));
1936 		return;
1937 	}
1938 
1939 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1940 
1941 		if (*ill_mdt_capab == NULL) {
1942 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1943 			    KM_NOSLEEP);
1944 
1945 			if (*ill_mdt_capab == NULL) {
1946 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1947 				    "could not enable MDT version %d "
1948 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1949 				    ill->ill_name);
1950 				return;
1951 			}
1952 		}
1953 
1954 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1955 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1956 		    "header spaces, %d max pld bufs, %d span limit)\n",
1957 		    ill->ill_name, MDT_VERSION_2,
1958 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1959 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1960 
1961 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1962 		(*ill_mdt_capab)->ill_mdt_on = 1;
1963 		/*
1964 		 * Round the following values to the nearest 32-bit; ULP
1965 		 * may further adjust them to accomodate for additional
1966 		 * protocol headers.  We pass these values to ULP during
1967 		 * bind time.
1968 		 */
1969 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1970 		    roundup(mdt_ic->mdt_hdr_head, 4);
1971 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1972 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1973 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1974 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1975 
1976 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1977 	} else {
1978 		uint_t size;
1979 		uchar_t *rptr;
1980 
1981 		size = sizeof (dl_capability_req_t) +
1982 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1983 
1984 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1985 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1986 			    "could not enable MDT for %s (ENOMEM)\n",
1987 			    ill->ill_name);
1988 			return;
1989 		}
1990 
1991 		rptr = nmp->b_rptr;
1992 		/* initialize dl_capability_req_t */
1993 		oc = (dl_capability_req_t *)nmp->b_rptr;
1994 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1995 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1996 		    sizeof (dl_capab_mdt_t);
1997 		nmp->b_rptr += sizeof (dl_capability_req_t);
1998 
1999 		/* initialize dl_capability_sub_t */
2000 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2001 		nmp->b_rptr += sizeof (*isub);
2002 
2003 		/* initialize dl_capab_mdt_t */
2004 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2005 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2006 
2007 		nmp->b_rptr = rptr;
2008 
2009 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2010 		    "to enable MDT version %d\n", ill->ill_name,
2011 		    MDT_VERSION_2));
2012 
2013 		/* set ENABLE flag */
2014 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2015 
2016 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2017 		ill_dlpi_send(ill, nmp);
2018 	}
2019 }
2020 
2021 static void
2022 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2023 {
2024 	mblk_t *mp;
2025 	dl_capab_mdt_t *mdt_subcap;
2026 	dl_capability_sub_t *dl_subcap;
2027 	int size;
2028 
2029 	if (!ILL_MDT_CAPABLE(ill))
2030 		return;
2031 
2032 	ASSERT(ill->ill_mdt_capab != NULL);
2033 	/*
2034 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2035 	 * structure since it's possible that another thread is still
2036 	 * referring to it.  The structure only gets deallocated when
2037 	 * we destroy the ill.
2038 	 */
2039 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2040 
2041 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2042 
2043 	mp = allocb(size, BPRI_HI);
2044 	if (mp == NULL) {
2045 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2046 		    "request to disable MDT\n"));
2047 		return;
2048 	}
2049 
2050 	mp->b_wptr = mp->b_rptr + size;
2051 
2052 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2053 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2054 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2055 
2056 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2057 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2058 	mdt_subcap->mdt_flags = 0;
2059 	mdt_subcap->mdt_hdr_head = 0;
2060 	mdt_subcap->mdt_hdr_tail = 0;
2061 
2062 	if (*sc_mp != NULL)
2063 		linkb(*sc_mp, mp);
2064 	else
2065 		*sc_mp = mp;
2066 }
2067 
2068 /*
2069  * Send a DL_NOTIFY_REQ to the specified ill to enable
2070  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2071  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2072  * acceleration.
2073  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2074  */
2075 static boolean_t
2076 ill_enable_promisc_notify(ill_t *ill)
2077 {
2078 	mblk_t *mp;
2079 	dl_notify_req_t *req;
2080 
2081 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2082 
2083 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2084 	if (mp == NULL)
2085 		return (B_FALSE);
2086 
2087 	req = (dl_notify_req_t *)mp->b_rptr;
2088 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2089 	    DL_NOTE_PROMISC_OFF_PHYS;
2090 
2091 	ill_dlpi_send(ill, mp);
2092 
2093 	return (B_TRUE);
2094 }
2095 
2096 
2097 /*
2098  * Allocate an IPsec capability request which will be filled by our
2099  * caller to turn on support for one or more algorithms.
2100  */
2101 static mblk_t *
2102 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2103 {
2104 	mblk_t *nmp;
2105 	dl_capability_req_t	*ocap;
2106 	dl_capab_ipsec_t	*ocip;
2107 	dl_capab_ipsec_t	*icip;
2108 	uint8_t			*ptr;
2109 	icip = (dl_capab_ipsec_t *)(isub + 1);
2110 
2111 	/*
2112 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2113 	 * PROMISC_ON/OFF notification from the provider. We need to
2114 	 * do this before enabling the algorithms to avoid leakage of
2115 	 * cleartext packets.
2116 	 */
2117 
2118 	if (!ill_enable_promisc_notify(ill))
2119 		return (NULL);
2120 
2121 	/*
2122 	 * Allocate new mblk which will contain a new capability
2123 	 * request to enable the capabilities.
2124 	 */
2125 
2126 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2127 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2128 	if (nmp == NULL)
2129 		return (NULL);
2130 
2131 	ptr = nmp->b_rptr;
2132 
2133 	/* initialize dl_capability_req_t */
2134 	ocap = (dl_capability_req_t *)ptr;
2135 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2136 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2137 	ptr += sizeof (dl_capability_req_t);
2138 
2139 	/* initialize dl_capability_sub_t */
2140 	bcopy(isub, ptr, sizeof (*isub));
2141 	ptr += sizeof (*isub);
2142 
2143 	/* initialize dl_capab_ipsec_t */
2144 	ocip = (dl_capab_ipsec_t *)ptr;
2145 	bcopy(icip, ocip, sizeof (*icip));
2146 
2147 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2148 	return (nmp);
2149 }
2150 
2151 /*
2152  * Process an IPsec capability negotiation ack received from a DLS Provider.
2153  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2154  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2155  */
2156 static void
2157 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2158 {
2159 	dl_capab_ipsec_t	*icip;
2160 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2161 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2162 	uint_t cipher, nciphers;
2163 	mblk_t *nmp;
2164 	uint_t alg_len;
2165 	boolean_t need_sadb_dump;
2166 	uint_t sub_dl_cap = isub->dl_cap;
2167 	ill_ipsec_capab_t **ill_capab;
2168 	uint64_t ill_capab_flag;
2169 	uint8_t *capend, *ciphend;
2170 	boolean_t sadb_resync;
2171 
2172 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2173 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2174 
2175 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2176 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2177 		ill_capab_flag = ILL_CAPAB_AH;
2178 	} else {
2179 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2180 		ill_capab_flag = ILL_CAPAB_ESP;
2181 	}
2182 
2183 	/*
2184 	 * If the ill capability structure exists, then this incoming
2185 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2186 	 * If this is so, then we'd need to resynchronize the SADB
2187 	 * after re-enabling the offloaded ciphers.
2188 	 */
2189 	sadb_resync = (*ill_capab != NULL);
2190 
2191 	/*
2192 	 * Note: range checks here are not absolutely sufficient to
2193 	 * make us robust against malformed messages sent by drivers;
2194 	 * this is in keeping with the rest of IP's dlpi handling.
2195 	 * (Remember, it's coming from something else in the kernel
2196 	 * address space)
2197 	 */
2198 
2199 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2200 	if (capend > mp->b_wptr) {
2201 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2202 		    "malformed sub-capability too long for mblk");
2203 		return;
2204 	}
2205 
2206 	/*
2207 	 * There are two types of acks we process here:
2208 	 * 1. acks in reply to a (first form) generic capability req
2209 	 *    (no ENABLE flag set)
2210 	 * 2. acks in reply to a ENABLE capability req.
2211 	 *    (ENABLE flag set)
2212 	 *
2213 	 * We process the subcapability passed as argument as follows:
2214 	 * 1 do initializations
2215 	 *   1.1 initialize nmp = NULL
2216 	 *   1.2 set need_sadb_dump to B_FALSE
2217 	 * 2 for each cipher in subcapability:
2218 	 *   2.1 if ENABLE flag is set:
2219 	 *	2.1.1 update per-ill ipsec capabilities info
2220 	 *	2.1.2 set need_sadb_dump to B_TRUE
2221 	 *   2.2 if ENABLE flag is not set:
2222 	 *	2.2.1 if nmp is NULL:
2223 	 *		2.2.1.1 allocate and initialize nmp
2224 	 *		2.2.1.2 init current pos in nmp
2225 	 *	2.2.2 copy current cipher to current pos in nmp
2226 	 *	2.2.3 set ENABLE flag in nmp
2227 	 *	2.2.4 update current pos
2228 	 * 3 if nmp is not equal to NULL, send enable request
2229 	 *   3.1 send capability request
2230 	 * 4 if need_sadb_dump is B_TRUE
2231 	 *   4.1 enable promiscuous on/off notifications
2232 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2233 	 *	AH or ESP SA's to interface.
2234 	 */
2235 
2236 	nmp = NULL;
2237 	oalg = NULL;
2238 	need_sadb_dump = B_FALSE;
2239 	icip = (dl_capab_ipsec_t *)(isub + 1);
2240 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2241 
2242 	nciphers = icip->cip_nciphers;
2243 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2244 
2245 	if (ciphend > capend) {
2246 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2247 		    "too many ciphers for sub-capability len");
2248 		return;
2249 	}
2250 
2251 	for (cipher = 0; cipher < nciphers; cipher++) {
2252 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2253 
2254 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2255 			/*
2256 			 * TBD: when we provide a way to disable capabilities
2257 			 * from above, need to manage the request-pending state
2258 			 * and fail if we were not expecting this ACK.
2259 			 */
2260 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2261 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2262 
2263 			/*
2264 			 * Update IPsec capabilities for this ill
2265 			 */
2266 
2267 			if (*ill_capab == NULL) {
2268 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2269 				    ("ill_capability_ipsec_ack: "
2270 				    "allocating ipsec_capab for ill\n"));
2271 				*ill_capab = ill_ipsec_capab_alloc();
2272 
2273 				if (*ill_capab == NULL) {
2274 					cmn_err(CE_WARN,
2275 					    "ill_capability_ipsec_ack: "
2276 					    "could not enable IPsec Hardware "
2277 					    "acceleration for %s (ENOMEM)\n",
2278 					    ill->ill_name);
2279 					return;
2280 				}
2281 			}
2282 
2283 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2284 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2285 
2286 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2287 				cmn_err(CE_WARN,
2288 				    "ill_capability_ipsec_ack: "
2289 				    "malformed IPsec algorithm id %d",
2290 				    ialg->alg_prim);
2291 				continue;
2292 			}
2293 
2294 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2295 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2296 				    ialg->alg_prim);
2297 			} else {
2298 				ipsec_capab_algparm_t *alp;
2299 
2300 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2301 				    ialg->alg_prim);
2302 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2303 				    ialg->alg_prim)) {
2304 					cmn_err(CE_WARN,
2305 					    "ill_capability_ipsec_ack: "
2306 					    "no space for IPsec alg id %d",
2307 					    ialg->alg_prim);
2308 					continue;
2309 				}
2310 				alp = &((*ill_capab)->encr_algparm[
2311 				    ialg->alg_prim]);
2312 				alp->minkeylen = ialg->alg_minbits;
2313 				alp->maxkeylen = ialg->alg_maxbits;
2314 			}
2315 			ill->ill_capabilities |= ill_capab_flag;
2316 			/*
2317 			 * indicate that a capability was enabled, which
2318 			 * will be used below to kick off a SADB dump
2319 			 * to the ill.
2320 			 */
2321 			need_sadb_dump = B_TRUE;
2322 		} else {
2323 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2324 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2325 			    ialg->alg_prim));
2326 
2327 			if (nmp == NULL) {
2328 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2329 				if (nmp == NULL) {
2330 					/*
2331 					 * Sending the PROMISC_ON/OFF
2332 					 * notification request failed.
2333 					 * We cannot enable the algorithms
2334 					 * since the Provider will not
2335 					 * notify IP of promiscous mode
2336 					 * changes, which could lead
2337 					 * to leakage of packets.
2338 					 */
2339 					cmn_err(CE_WARN,
2340 					    "ill_capability_ipsec_ack: "
2341 					    "could not enable IPsec Hardware "
2342 					    "acceleration for %s (ENOMEM)\n",
2343 					    ill->ill_name);
2344 					return;
2345 				}
2346 				/* ptr to current output alg specifier */
2347 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2348 			}
2349 
2350 			/*
2351 			 * Copy current alg specifier, set ENABLE
2352 			 * flag, and advance to next output alg.
2353 			 * For now we enable all IPsec capabilities.
2354 			 */
2355 			ASSERT(oalg != NULL);
2356 			bcopy(ialg, oalg, alg_len);
2357 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2358 			nmp->b_wptr += alg_len;
2359 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2360 		}
2361 
2362 		/* move to next input algorithm specifier */
2363 		ialg = (dl_capab_ipsec_alg_t *)
2364 		    ((char *)ialg + alg_len);
2365 	}
2366 
2367 	if (nmp != NULL)
2368 		/*
2369 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2370 		 * IPsec hardware acceleration.
2371 		 */
2372 		ill_dlpi_send(ill, nmp);
2373 
2374 	if (need_sadb_dump)
2375 		/*
2376 		 * An acknowledgement corresponding to a request to
2377 		 * enable acceleration was received, notify SADB.
2378 		 */
2379 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2380 }
2381 
2382 /*
2383  * Given an mblk with enough space in it, create sub-capability entries for
2384  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2385  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2386  * in preparation for the reset the DL_CAPABILITY_REQ message.
2387  */
2388 static void
2389 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2390     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2391 {
2392 	dl_capab_ipsec_t *oipsec;
2393 	dl_capab_ipsec_alg_t *oalg;
2394 	dl_capability_sub_t *dl_subcap;
2395 	int i, k;
2396 
2397 	ASSERT(nciphers > 0);
2398 	ASSERT(ill_cap != NULL);
2399 	ASSERT(mp != NULL);
2400 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2401 
2402 	/* dl_capability_sub_t for "stype" */
2403 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2404 	dl_subcap->dl_cap = stype;
2405 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2406 	mp->b_wptr += sizeof (dl_capability_sub_t);
2407 
2408 	/* dl_capab_ipsec_t for "stype" */
2409 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2410 	oipsec->cip_version = 1;
2411 	oipsec->cip_nciphers = nciphers;
2412 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2413 
2414 	/* create entries for "stype" AUTH ciphers */
2415 	for (i = 0; i < ill_cap->algs_size; i++) {
2416 		for (k = 0; k < BITSPERBYTE; k++) {
2417 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2418 				continue;
2419 
2420 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2421 			bzero((void *)oalg, sizeof (*oalg));
2422 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2423 			oalg->alg_prim = k + (BITSPERBYTE * i);
2424 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2425 		}
2426 	}
2427 	/* create entries for "stype" ENCR ciphers */
2428 	for (i = 0; i < ill_cap->algs_size; i++) {
2429 		for (k = 0; k < BITSPERBYTE; k++) {
2430 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2431 				continue;
2432 
2433 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2434 			bzero((void *)oalg, sizeof (*oalg));
2435 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2436 			oalg->alg_prim = k + (BITSPERBYTE * i);
2437 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2438 		}
2439 	}
2440 }
2441 
2442 /*
2443  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2444  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2445  * POPC instruction, but our macro is more flexible for an arbitrary length
2446  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2447  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2448  * stays that way, we can reduce the number of iterations required.
2449  */
2450 #define	COUNT_1S(val, sum) {					\
2451 	uint8_t x = val & 0xff;					\
2452 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2453 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2454 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2455 }
2456 
2457 /* ARGSUSED */
2458 static void
2459 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2460 {
2461 	mblk_t *mp;
2462 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2463 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2464 	uint64_t ill_capabilities = ill->ill_capabilities;
2465 	int ah_cnt = 0, esp_cnt = 0;
2466 	int ah_len = 0, esp_len = 0;
2467 	int i, size = 0;
2468 
2469 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2470 		return;
2471 
2472 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2473 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2474 
2475 	/* Find out the number of ciphers for AH */
2476 	if (cap_ah != NULL) {
2477 		for (i = 0; i < cap_ah->algs_size; i++) {
2478 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2479 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2480 		}
2481 		if (ah_cnt > 0) {
2482 			size += sizeof (dl_capability_sub_t) +
2483 			    sizeof (dl_capab_ipsec_t);
2484 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2485 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2486 			size += ah_len;
2487 		}
2488 	}
2489 
2490 	/* Find out the number of ciphers for ESP */
2491 	if (cap_esp != NULL) {
2492 		for (i = 0; i < cap_esp->algs_size; i++) {
2493 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2494 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2495 		}
2496 		if (esp_cnt > 0) {
2497 			size += sizeof (dl_capability_sub_t) +
2498 			    sizeof (dl_capab_ipsec_t);
2499 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2500 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2501 			size += esp_len;
2502 		}
2503 	}
2504 
2505 	if (size == 0) {
2506 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2507 		    "there's nothing to reset\n"));
2508 		return;
2509 	}
2510 
2511 	mp = allocb(size, BPRI_HI);
2512 	if (mp == NULL) {
2513 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2514 		    "request to disable IPSEC Hardware Acceleration\n"));
2515 		return;
2516 	}
2517 
2518 	/*
2519 	 * Clear the capability flags for IPsec HA but retain the ill
2520 	 * capability structures since it's possible that another thread
2521 	 * is still referring to them.  The structures only get deallocated
2522 	 * when we destroy the ill.
2523 	 *
2524 	 * Various places check the flags to see if the ill is capable of
2525 	 * hardware acceleration, and by clearing them we ensure that new
2526 	 * outbound IPsec packets are sent down encrypted.
2527 	 */
2528 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2529 
2530 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2531 	if (ah_cnt > 0) {
2532 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2533 		    cap_ah, mp);
2534 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2535 	}
2536 
2537 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2538 	if (esp_cnt > 0) {
2539 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2540 		    cap_esp, mp);
2541 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2542 	}
2543 
2544 	/*
2545 	 * At this point we've composed a bunch of sub-capabilities to be
2546 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2547 	 * by the caller.  Upon receiving this reset message, the driver
2548 	 * must stop inbound decryption (by destroying all inbound SAs)
2549 	 * and let the corresponding packets come in encrypted.
2550 	 */
2551 
2552 	if (*sc_mp != NULL)
2553 		linkb(*sc_mp, mp);
2554 	else
2555 		*sc_mp = mp;
2556 }
2557 
2558 static void
2559 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2560     boolean_t encapsulated)
2561 {
2562 	boolean_t legacy = B_FALSE;
2563 
2564 	/*
2565 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2566 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2567 	 * instructed the driver to disable its advertised capabilities,
2568 	 * so there's no point in accepting any response at this moment.
2569 	 */
2570 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2571 		return;
2572 
2573 	/*
2574 	 * Note that only the following two sub-capabilities may be
2575 	 * considered as "legacy", since their original definitions
2576 	 * do not incorporate the dl_mid_t module ID token, and hence
2577 	 * may require the use of the wrapper sub-capability.
2578 	 */
2579 	switch (subp->dl_cap) {
2580 	case DL_CAPAB_IPSEC_AH:
2581 	case DL_CAPAB_IPSEC_ESP:
2582 		legacy = B_TRUE;
2583 		break;
2584 	}
2585 
2586 	/*
2587 	 * For legacy sub-capabilities which don't incorporate a queue_t
2588 	 * pointer in their structures, discard them if we detect that
2589 	 * there are intermediate modules in between IP and the driver.
2590 	 */
2591 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2592 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2593 		    "%d discarded; %d module(s) present below IP\n",
2594 		    subp->dl_cap, ill->ill_lmod_cnt));
2595 		return;
2596 	}
2597 
2598 	switch (subp->dl_cap) {
2599 	case DL_CAPAB_IPSEC_AH:
2600 	case DL_CAPAB_IPSEC_ESP:
2601 		ill_capability_ipsec_ack(ill, mp, subp);
2602 		break;
2603 	case DL_CAPAB_MDT:
2604 		ill_capability_mdt_ack(ill, mp, subp);
2605 		break;
2606 	case DL_CAPAB_HCKSUM:
2607 		ill_capability_hcksum_ack(ill, mp, subp);
2608 		break;
2609 	case DL_CAPAB_ZEROCOPY:
2610 		ill_capability_zerocopy_ack(ill, mp, subp);
2611 		break;
2612 	case DL_CAPAB_POLL:
2613 		if (!SOFT_RINGS_ENABLED())
2614 			ill_capability_dls_ack(ill, mp, subp);
2615 		break;
2616 	case DL_CAPAB_SOFT_RING:
2617 		if (SOFT_RINGS_ENABLED())
2618 			ill_capability_dls_ack(ill, mp, subp);
2619 		break;
2620 	case DL_CAPAB_LSO:
2621 		ill_capability_lso_ack(ill, mp, subp);
2622 		break;
2623 	default:
2624 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2625 		    subp->dl_cap));
2626 	}
2627 }
2628 
2629 /*
2630  * As part of negotiating polling capability, the driver tells us
2631  * the default (or normal) blanking interval and packet threshold
2632  * (the receive timer fires if blanking interval is reached or
2633  * the packet threshold is reached).
2634  *
2635  * As part of manipulating the polling interval, we always use our
2636  * estimated interval (avg service time * number of packets queued
2637  * on the squeue) but we try to blank for a minimum of
2638  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2639  * packet threshold during this time. When we are not in polling mode
2640  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2641  * rr_min_blank_ratio but up the packet cnt by a ratio of
2642  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2643  * possible although for a shorter interval.
2644  */
2645 #define	RR_MAX_BLANK_RATIO	20
2646 #define	RR_MIN_BLANK_RATIO	10
2647 #define	RR_MAX_PKT_CNT_RATIO	3
2648 #define	RR_MIN_PKT_CNT_RATIO	3
2649 
2650 /*
2651  * These can be tuned via /etc/system.
2652  */
2653 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2654 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2655 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2656 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2657 
2658 static mac_resource_handle_t
2659 ill_ring_add(void *arg, mac_resource_t *mrp)
2660 {
2661 	ill_t			*ill = (ill_t *)arg;
2662 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2663 	ill_rx_ring_t		*rx_ring;
2664 	int			ip_rx_index;
2665 
2666 	ASSERT(mrp != NULL);
2667 	if (mrp->mr_type != MAC_RX_FIFO) {
2668 		return (NULL);
2669 	}
2670 	ASSERT(ill != NULL);
2671 	ASSERT(ill->ill_dls_capab != NULL);
2672 
2673 	mutex_enter(&ill->ill_lock);
2674 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2675 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2676 		ASSERT(rx_ring != NULL);
2677 
2678 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2679 			time_t normal_blank_time =
2680 			    mrfp->mrf_normal_blank_time;
2681 			uint_t normal_pkt_cnt =
2682 			    mrfp->mrf_normal_pkt_count;
2683 
2684 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2685 
2686 	rx_ring->rr_blank = mrfp->mrf_blank;
2687 	rx_ring->rr_handle = mrfp->mrf_arg;
2688 	rx_ring->rr_ill = ill;
2689 	rx_ring->rr_normal_blank_time = normal_blank_time;
2690 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2691 
2692 			rx_ring->rr_max_blank_time =
2693 			    normal_blank_time * rr_max_blank_ratio;
2694 			rx_ring->rr_min_blank_time =
2695 			    normal_blank_time * rr_min_blank_ratio;
2696 			rx_ring->rr_max_pkt_cnt =
2697 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2698 			rx_ring->rr_min_pkt_cnt =
2699 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2700 
2701 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2702 			mutex_exit(&ill->ill_lock);
2703 
2704 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2705 			    (int), ip_rx_index);
2706 			return ((mac_resource_handle_t)rx_ring);
2707 		}
2708 	}
2709 
2710 	/*
2711 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2712 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2713 	 * should be made configurable. Meanwhile it cause no panic because
2714 	 * driver will pass ip_input a NULL handle which will make
2715 	 * IP allocate the default squeue and Polling mode will not
2716 	 * be used for this ring.
2717 	 */
2718 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2719 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2720 
2721 	mutex_exit(&ill->ill_lock);
2722 	return (NULL);
2723 }
2724 
2725 static boolean_t
2726 ill_capability_dls_init(ill_t *ill)
2727 {
2728 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2729 	conn_t 			*connp;
2730 	size_t			sz;
2731 	ip_stack_t *ipst = ill->ill_ipst;
2732 
2733 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2734 		if (ill_dls == NULL) {
2735 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2736 			    "soft_ring enabled for ill=%s (%p) but data "
2737 			    "structs uninitialized\n", ill->ill_name,
2738 			    (void *)ill);
2739 		}
2740 		return (B_TRUE);
2741 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2742 		if (ill_dls == NULL) {
2743 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2744 			    "polling enabled for ill=%s (%p) but data "
2745 			    "structs uninitialized\n", ill->ill_name,
2746 			    (void *)ill);
2747 		}
2748 		return (B_TRUE);
2749 	}
2750 
2751 	if (ill_dls != NULL) {
2752 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2753 		/* Soft_Ring or polling is being re-enabled */
2754 
2755 		connp = ill_dls->ill_unbind_conn;
2756 		ASSERT(rx_ring != NULL);
2757 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2758 		bzero((void *)rx_ring,
2759 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2760 		ill_dls->ill_ring_tbl = rx_ring;
2761 		ill_dls->ill_unbind_conn = connp;
2762 		return (B_TRUE);
2763 	}
2764 
2765 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2766 	    ipst->ips_netstack)) == NULL)
2767 		return (B_FALSE);
2768 
2769 	sz = sizeof (ill_dls_capab_t);
2770 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2771 
2772 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2773 	if (ill_dls == NULL) {
2774 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2775 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2776 		    (void *)ill);
2777 		CONN_DEC_REF(connp);
2778 		return (B_FALSE);
2779 	}
2780 
2781 	/* Allocate space to hold ring table */
2782 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2783 	ill->ill_dls_capab = ill_dls;
2784 	ill_dls->ill_unbind_conn = connp;
2785 	return (B_TRUE);
2786 }
2787 
2788 /*
2789  * ill_capability_dls_disable: disable soft_ring and/or polling
2790  * capability. Since any of the rings might already be in use, need
2791  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2792  * direct calls if necessary.
2793  */
2794 static void
2795 ill_capability_dls_disable(ill_t *ill)
2796 {
2797 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2798 
2799 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2800 		ip_squeue_clean_all(ill);
2801 		ill_dls->ill_tx = NULL;
2802 		ill_dls->ill_tx_handle = NULL;
2803 		ill_dls->ill_dls_change_status = NULL;
2804 		ill_dls->ill_dls_bind = NULL;
2805 		ill_dls->ill_dls_unbind = NULL;
2806 	}
2807 
2808 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2809 }
2810 
2811 static void
2812 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2813     dl_capability_sub_t *isub)
2814 {
2815 	uint_t			size;
2816 	uchar_t			*rptr;
2817 	dl_capab_dls_t	dls, *odls;
2818 	ill_dls_capab_t	*ill_dls;
2819 	mblk_t			*nmp = NULL;
2820 	dl_capability_req_t	*ocap;
2821 	uint_t			sub_dl_cap = isub->dl_cap;
2822 
2823 	if (!ill_capability_dls_init(ill))
2824 		return;
2825 	ill_dls = ill->ill_dls_capab;
2826 
2827 	/* Copy locally to get the members aligned */
2828 	bcopy((void *)idls, (void *)&dls,
2829 	    sizeof (dl_capab_dls_t));
2830 
2831 	/* Get the tx function and handle from dld */
2832 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2833 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2834 
2835 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2836 		ill_dls->ill_dls_change_status =
2837 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2838 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2839 		ill_dls->ill_dls_unbind =
2840 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2841 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2842 	}
2843 
2844 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2845 	    isub->dl_length;
2846 
2847 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2848 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2849 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2850 		    ill->ill_name, (void *)ill);
2851 		return;
2852 	}
2853 
2854 	/* initialize dl_capability_req_t */
2855 	rptr = nmp->b_rptr;
2856 	ocap = (dl_capability_req_t *)rptr;
2857 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2858 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2859 	rptr += sizeof (dl_capability_req_t);
2860 
2861 	/* initialize dl_capability_sub_t */
2862 	bcopy(isub, rptr, sizeof (*isub));
2863 	rptr += sizeof (*isub);
2864 
2865 	odls = (dl_capab_dls_t *)rptr;
2866 	rptr += sizeof (dl_capab_dls_t);
2867 
2868 	/* initialize dl_capab_dls_t to be sent down */
2869 	dls.dls_rx_handle = (uintptr_t)ill;
2870 	dls.dls_rx = (uintptr_t)ip_input;
2871 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2872 
2873 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2874 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2875 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2876 		dls.dls_flags = SOFT_RING_ENABLE;
2877 	} else {
2878 		dls.dls_flags = POLL_ENABLE;
2879 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2880 		    "to enable polling\n", ill->ill_name));
2881 	}
2882 	bcopy((void *)&dls, (void *)odls,
2883 	    sizeof (dl_capab_dls_t));
2884 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2885 	/*
2886 	 * nmp points to a DL_CAPABILITY_REQ message to
2887 	 * enable either soft_ring or polling
2888 	 */
2889 	ill_dlpi_send(ill, nmp);
2890 }
2891 
2892 static void
2893 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2894 {
2895 	mblk_t *mp;
2896 	dl_capab_dls_t *idls;
2897 	dl_capability_sub_t *dl_subcap;
2898 	int size;
2899 
2900 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2901 		return;
2902 
2903 	ASSERT(ill->ill_dls_capab != NULL);
2904 
2905 	size = sizeof (*dl_subcap) + sizeof (*idls);
2906 
2907 	mp = allocb(size, BPRI_HI);
2908 	if (mp == NULL) {
2909 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2910 		    "request to disable soft_ring\n"));
2911 		return;
2912 	}
2913 
2914 	mp->b_wptr = mp->b_rptr + size;
2915 
2916 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2917 	dl_subcap->dl_length = sizeof (*idls);
2918 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2919 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2920 	else
2921 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2922 
2923 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2924 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2925 		idls->dls_flags = SOFT_RING_DISABLE;
2926 	else
2927 		idls->dls_flags = POLL_DISABLE;
2928 
2929 	if (*sc_mp != NULL)
2930 		linkb(*sc_mp, mp);
2931 	else
2932 		*sc_mp = mp;
2933 }
2934 
2935 /*
2936  * Process a soft_ring/poll capability negotiation ack received
2937  * from a DLS Provider.isub must point to the sub-capability
2938  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2939  */
2940 static void
2941 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2942 {
2943 	dl_capab_dls_t		*idls;
2944 	uint_t			sub_dl_cap = isub->dl_cap;
2945 	uint8_t			*capend;
2946 
2947 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2948 	    sub_dl_cap == DL_CAPAB_POLL);
2949 
2950 	if (ill->ill_isv6)
2951 		return;
2952 
2953 	/*
2954 	 * Note: range checks here are not absolutely sufficient to
2955 	 * make us robust against malformed messages sent by drivers;
2956 	 * this is in keeping with the rest of IP's dlpi handling.
2957 	 * (Remember, it's coming from something else in the kernel
2958 	 * address space)
2959 	 */
2960 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2961 	if (capend > mp->b_wptr) {
2962 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2963 		    "malformed sub-capability too long for mblk");
2964 		return;
2965 	}
2966 
2967 	/*
2968 	 * There are two types of acks we process here:
2969 	 * 1. acks in reply to a (first form) generic capability req
2970 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2971 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2972 	 *    capability req.
2973 	 */
2974 	idls = (dl_capab_dls_t *)(isub + 1);
2975 
2976 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2977 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2978 		    "capability isn't as expected; pass-thru "
2979 		    "module(s) detected, discarding capability\n"));
2980 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2981 			/*
2982 			 * This is a capability renegotitation case.
2983 			 * The interface better be unusable at this
2984 			 * point other wise bad things will happen
2985 			 * if we disable direct calls on a running
2986 			 * and up interface.
2987 			 */
2988 			ill_capability_dls_disable(ill);
2989 		}
2990 		return;
2991 	}
2992 
2993 	switch (idls->dls_flags) {
2994 	default:
2995 		/* Disable if unknown flag */
2996 	case SOFT_RING_DISABLE:
2997 	case POLL_DISABLE:
2998 		ill_capability_dls_disable(ill);
2999 		break;
3000 	case SOFT_RING_CAPABLE:
3001 	case POLL_CAPABLE:
3002 		/*
3003 		 * If the capability was already enabled, its safe
3004 		 * to disable it first to get rid of stale information
3005 		 * and then start enabling it again.
3006 		 */
3007 		ill_capability_dls_disable(ill);
3008 		ill_capability_dls_capable(ill, idls, isub);
3009 		break;
3010 	case SOFT_RING_ENABLE:
3011 	case POLL_ENABLE:
3012 		mutex_enter(&ill->ill_lock);
3013 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3014 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3015 			ASSERT(ill->ill_dls_capab != NULL);
3016 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3017 		}
3018 		if (sub_dl_cap == DL_CAPAB_POLL &&
3019 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3020 			ASSERT(ill->ill_dls_capab != NULL);
3021 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3022 			ip1dbg(("ill_capability_dls_ack: interface %s "
3023 			    "has enabled polling\n", ill->ill_name));
3024 		}
3025 		mutex_exit(&ill->ill_lock);
3026 		break;
3027 	}
3028 }
3029 
3030 /*
3031  * Process a hardware checksum offload capability negotiation ack received
3032  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3033  * of a DL_CAPABILITY_ACK message.
3034  */
3035 static void
3036 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3037 {
3038 	dl_capability_req_t	*ocap;
3039 	dl_capab_hcksum_t	*ihck, *ohck;
3040 	ill_hcksum_capab_t	**ill_hcksum;
3041 	mblk_t			*nmp = NULL;
3042 	uint_t			sub_dl_cap = isub->dl_cap;
3043 	uint8_t			*capend;
3044 
3045 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3046 
3047 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3048 
3049 	/*
3050 	 * Note: range checks here are not absolutely sufficient to
3051 	 * make us robust against malformed messages sent by drivers;
3052 	 * this is in keeping with the rest of IP's dlpi handling.
3053 	 * (Remember, it's coming from something else in the kernel
3054 	 * address space)
3055 	 */
3056 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3057 	if (capend > mp->b_wptr) {
3058 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3059 		    "malformed sub-capability too long for mblk");
3060 		return;
3061 	}
3062 
3063 	/*
3064 	 * There are two types of acks we process here:
3065 	 * 1. acks in reply to a (first form) generic capability req
3066 	 *    (no ENABLE flag set)
3067 	 * 2. acks in reply to a ENABLE capability req.
3068 	 *    (ENABLE flag set)
3069 	 */
3070 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3071 
3072 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3073 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3074 		    "unsupported hardware checksum "
3075 		    "sub-capability (version %d, expected %d)",
3076 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3077 		return;
3078 	}
3079 
3080 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3081 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3082 		    "checksum capability isn't as expected; pass-thru "
3083 		    "module(s) detected, discarding capability\n"));
3084 		return;
3085 	}
3086 
3087 #define	CURR_HCKSUM_CAPAB				\
3088 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3089 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3090 
3091 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3092 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3093 		/* do ENABLE processing */
3094 		if (*ill_hcksum == NULL) {
3095 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3096 			    KM_NOSLEEP);
3097 
3098 			if (*ill_hcksum == NULL) {
3099 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3100 				    "could not enable hcksum version %d "
3101 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3102 				    ill->ill_name);
3103 				return;
3104 			}
3105 		}
3106 
3107 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3108 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3109 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3110 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3111 		    "has enabled hardware checksumming\n ",
3112 		    ill->ill_name));
3113 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3114 		/*
3115 		 * Enabling hardware checksum offload
3116 		 * Currently IP supports {TCP,UDP}/IPv4
3117 		 * partial and full cksum offload and
3118 		 * IPv4 header checksum offload.
3119 		 * Allocate new mblk which will
3120 		 * contain a new capability request
3121 		 * to enable hardware checksum offload.
3122 		 */
3123 		uint_t	size;
3124 		uchar_t	*rptr;
3125 
3126 		size = sizeof (dl_capability_req_t) +
3127 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3128 
3129 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3130 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3131 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3132 			    ill->ill_name);
3133 			return;
3134 		}
3135 
3136 		rptr = nmp->b_rptr;
3137 		/* initialize dl_capability_req_t */
3138 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3139 		ocap->dl_sub_offset =
3140 		    sizeof (dl_capability_req_t);
3141 		ocap->dl_sub_length =
3142 		    sizeof (dl_capability_sub_t) +
3143 		    isub->dl_length;
3144 		nmp->b_rptr += sizeof (dl_capability_req_t);
3145 
3146 		/* initialize dl_capability_sub_t */
3147 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3148 		nmp->b_rptr += sizeof (*isub);
3149 
3150 		/* initialize dl_capab_hcksum_t */
3151 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3152 		bcopy(ihck, ohck, sizeof (*ihck));
3153 
3154 		nmp->b_rptr = rptr;
3155 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3156 
3157 		/* Set ENABLE flag */
3158 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3159 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3160 
3161 		/*
3162 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3163 		 * hardware checksum acceleration.
3164 		 */
3165 		ill_dlpi_send(ill, nmp);
3166 	} else {
3167 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3168 		    "advertised %x hardware checksum capability flags\n",
3169 		    ill->ill_name, ihck->hcksum_txflags));
3170 	}
3171 }
3172 
3173 static void
3174 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3175 {
3176 	mblk_t *mp;
3177 	dl_capab_hcksum_t *hck_subcap;
3178 	dl_capability_sub_t *dl_subcap;
3179 	int size;
3180 
3181 	if (!ILL_HCKSUM_CAPABLE(ill))
3182 		return;
3183 
3184 	ASSERT(ill->ill_hcksum_capab != NULL);
3185 	/*
3186 	 * Clear the capability flag for hardware checksum offload but
3187 	 * retain the ill_hcksum_capab structure since it's possible that
3188 	 * another thread is still referring to it.  The structure only
3189 	 * gets deallocated when we destroy the ill.
3190 	 */
3191 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3192 
3193 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3194 
3195 	mp = allocb(size, BPRI_HI);
3196 	if (mp == NULL) {
3197 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3198 		    "request to disable hardware checksum offload\n"));
3199 		return;
3200 	}
3201 
3202 	mp->b_wptr = mp->b_rptr + size;
3203 
3204 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3205 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3206 	dl_subcap->dl_length = sizeof (*hck_subcap);
3207 
3208 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3209 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3210 	hck_subcap->hcksum_txflags = 0;
3211 
3212 	if (*sc_mp != NULL)
3213 		linkb(*sc_mp, mp);
3214 	else
3215 		*sc_mp = mp;
3216 }
3217 
3218 static void
3219 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3220 {
3221 	mblk_t *nmp = NULL;
3222 	dl_capability_req_t *oc;
3223 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3224 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3225 	uint_t sub_dl_cap = isub->dl_cap;
3226 	uint8_t *capend;
3227 
3228 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3229 
3230 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3231 
3232 	/*
3233 	 * Note: range checks here are not absolutely sufficient to
3234 	 * make us robust against malformed messages sent by drivers;
3235 	 * this is in keeping with the rest of IP's dlpi handling.
3236 	 * (Remember, it's coming from something else in the kernel
3237 	 * address space)
3238 	 */
3239 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3240 	if (capend > mp->b_wptr) {
3241 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3242 		    "malformed sub-capability too long for mblk");
3243 		return;
3244 	}
3245 
3246 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3247 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3248 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3249 		    "unsupported ZEROCOPY sub-capability (version %d, "
3250 		    "expected %d)", zc_ic->zerocopy_version,
3251 		    ZEROCOPY_VERSION_1);
3252 		return;
3253 	}
3254 
3255 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3256 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3257 		    "capability isn't as expected; pass-thru module(s) "
3258 		    "detected, discarding capability\n"));
3259 		return;
3260 	}
3261 
3262 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3263 		if (*ill_zerocopy_capab == NULL) {
3264 			*ill_zerocopy_capab =
3265 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3266 			    KM_NOSLEEP);
3267 
3268 			if (*ill_zerocopy_capab == NULL) {
3269 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3270 				    "could not enable Zero-copy version %d "
3271 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3272 				    ill->ill_name);
3273 				return;
3274 			}
3275 		}
3276 
3277 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3278 		    "supports Zero-copy version %d\n", ill->ill_name,
3279 		    ZEROCOPY_VERSION_1));
3280 
3281 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3282 		    zc_ic->zerocopy_version;
3283 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3284 		    zc_ic->zerocopy_flags;
3285 
3286 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3287 	} else {
3288 		uint_t size;
3289 		uchar_t *rptr;
3290 
3291 		size = sizeof (dl_capability_req_t) +
3292 		    sizeof (dl_capability_sub_t) +
3293 		    sizeof (dl_capab_zerocopy_t);
3294 
3295 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3296 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3297 			    "could not enable zerocopy for %s (ENOMEM)\n",
3298 			    ill->ill_name);
3299 			return;
3300 		}
3301 
3302 		rptr = nmp->b_rptr;
3303 		/* initialize dl_capability_req_t */
3304 		oc = (dl_capability_req_t *)rptr;
3305 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3306 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3307 		    sizeof (dl_capab_zerocopy_t);
3308 		rptr += sizeof (dl_capability_req_t);
3309 
3310 		/* initialize dl_capability_sub_t */
3311 		bcopy(isub, rptr, sizeof (*isub));
3312 		rptr += sizeof (*isub);
3313 
3314 		/* initialize dl_capab_zerocopy_t */
3315 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3316 		*zc_oc = *zc_ic;
3317 
3318 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3319 		    "to enable zero-copy version %d\n", ill->ill_name,
3320 		    ZEROCOPY_VERSION_1));
3321 
3322 		/* set VMSAFE_MEM flag */
3323 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3324 
3325 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3326 		ill_dlpi_send(ill, nmp);
3327 	}
3328 }
3329 
3330 static void
3331 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3332 {
3333 	mblk_t *mp;
3334 	dl_capab_zerocopy_t *zerocopy_subcap;
3335 	dl_capability_sub_t *dl_subcap;
3336 	int size;
3337 
3338 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3339 		return;
3340 
3341 	ASSERT(ill->ill_zerocopy_capab != NULL);
3342 	/*
3343 	 * Clear the capability flag for Zero-copy but retain the
3344 	 * ill_zerocopy_capab structure since it's possible that another
3345 	 * thread is still referring to it.  The structure only gets
3346 	 * deallocated when we destroy the ill.
3347 	 */
3348 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3349 
3350 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3351 
3352 	mp = allocb(size, BPRI_HI);
3353 	if (mp == NULL) {
3354 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3355 		    "request to disable Zero-copy\n"));
3356 		return;
3357 	}
3358 
3359 	mp->b_wptr = mp->b_rptr + size;
3360 
3361 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3362 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3363 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3364 
3365 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3366 	zerocopy_subcap->zerocopy_version =
3367 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3368 	zerocopy_subcap->zerocopy_flags = 0;
3369 
3370 	if (*sc_mp != NULL)
3371 		linkb(*sc_mp, mp);
3372 	else
3373 		*sc_mp = mp;
3374 }
3375 
3376 /*
3377  * Process Large Segment Offload capability negotiation ack received from a
3378  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3379  * DL_CAPABILITY_ACK message.
3380  */
3381 static void
3382 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3383 {
3384 	mblk_t *nmp = NULL;
3385 	dl_capability_req_t *oc;
3386 	dl_capab_lso_t *lso_ic, *lso_oc;
3387 	ill_lso_capab_t **ill_lso_capab;
3388 	uint_t sub_dl_cap = isub->dl_cap;
3389 	uint8_t *capend;
3390 
3391 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3392 
3393 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3394 
3395 	/*
3396 	 * Note: range checks here are not absolutely sufficient to
3397 	 * make us robust against malformed messages sent by drivers;
3398 	 * this is in keeping with the rest of IP's dlpi handling.
3399 	 * (Remember, it's coming from something else in the kernel
3400 	 * address space)
3401 	 */
3402 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3403 	if (capend > mp->b_wptr) {
3404 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3405 		    "malformed sub-capability too long for mblk");
3406 		return;
3407 	}
3408 
3409 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3410 
3411 	if (lso_ic->lso_version != LSO_VERSION_1) {
3412 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3413 		    "unsupported LSO sub-capability (version %d, expected %d)",
3414 		    lso_ic->lso_version, LSO_VERSION_1);
3415 		return;
3416 	}
3417 
3418 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3419 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3420 		    "capability isn't as expected; pass-thru module(s) "
3421 		    "detected, discarding capability\n"));
3422 		return;
3423 	}
3424 
3425 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3426 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3427 		if (*ill_lso_capab == NULL) {
3428 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3429 			    KM_NOSLEEP);
3430 
3431 			if (*ill_lso_capab == NULL) {
3432 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3433 				    "could not enable LSO version %d "
3434 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3435 				    ill->ill_name);
3436 				return;
3437 			}
3438 		}
3439 
3440 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3441 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3442 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3443 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3444 
3445 		ip1dbg(("ill_capability_lso_ack: interface %s "
3446 		    "has enabled LSO\n ", ill->ill_name));
3447 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3448 		uint_t size;
3449 		uchar_t *rptr;
3450 
3451 		size = sizeof (dl_capability_req_t) +
3452 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3453 
3454 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3455 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3456 			    "could not enable LSO for %s (ENOMEM)\n",
3457 			    ill->ill_name);
3458 			return;
3459 		}
3460 
3461 		rptr = nmp->b_rptr;
3462 		/* initialize dl_capability_req_t */
3463 		oc = (dl_capability_req_t *)nmp->b_rptr;
3464 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3465 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3466 		    sizeof (dl_capab_lso_t);
3467 		nmp->b_rptr += sizeof (dl_capability_req_t);
3468 
3469 		/* initialize dl_capability_sub_t */
3470 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3471 		nmp->b_rptr += sizeof (*isub);
3472 
3473 		/* initialize dl_capab_lso_t */
3474 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3475 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3476 
3477 		nmp->b_rptr = rptr;
3478 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3479 
3480 		/* set ENABLE flag */
3481 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3482 
3483 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3484 		ill_dlpi_send(ill, nmp);
3485 	} else {
3486 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3487 		    "advertised %x LSO capability flags\n",
3488 		    ill->ill_name, lso_ic->lso_flags));
3489 	}
3490 }
3491 
3492 
3493 static void
3494 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3495 {
3496 	mblk_t *mp;
3497 	dl_capab_lso_t *lso_subcap;
3498 	dl_capability_sub_t *dl_subcap;
3499 	int size;
3500 
3501 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3502 		return;
3503 
3504 	ASSERT(ill->ill_lso_capab != NULL);
3505 	/*
3506 	 * Clear the capability flag for LSO but retain the
3507 	 * ill_lso_capab structure since it's possible that another
3508 	 * thread is still referring to it.  The structure only gets
3509 	 * deallocated when we destroy the ill.
3510 	 */
3511 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3512 
3513 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3514 
3515 	mp = allocb(size, BPRI_HI);
3516 	if (mp == NULL) {
3517 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3518 		    "request to disable LSO\n"));
3519 		return;
3520 	}
3521 
3522 	mp->b_wptr = mp->b_rptr + size;
3523 
3524 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3525 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3526 	dl_subcap->dl_length = sizeof (*lso_subcap);
3527 
3528 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3529 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3530 	lso_subcap->lso_flags = 0;
3531 
3532 	if (*sc_mp != NULL)
3533 		linkb(*sc_mp, mp);
3534 	else
3535 		*sc_mp = mp;
3536 }
3537 
3538 /*
3539  * Consume a new-style hardware capabilities negotiation ack.
3540  * Called from ip_rput_dlpi_writer().
3541  */
3542 void
3543 ill_capability_ack(ill_t *ill, mblk_t *mp)
3544 {
3545 	dl_capability_ack_t *capp;
3546 	dl_capability_sub_t *subp, *endp;
3547 
3548 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3549 		ill->ill_dlpi_capab_state = IDS_OK;
3550 
3551 	capp = (dl_capability_ack_t *)mp->b_rptr;
3552 
3553 	if (capp->dl_sub_length == 0)
3554 		/* no new-style capabilities */
3555 		return;
3556 
3557 	/* make sure the driver supplied correct dl_sub_length */
3558 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3559 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3560 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3561 		return;
3562 	}
3563 
3564 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3565 	/*
3566 	 * There are sub-capabilities. Process the ones we know about.
3567 	 * Loop until we don't have room for another sub-cap header..
3568 	 */
3569 	for (subp = SC(capp, capp->dl_sub_offset),
3570 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3571 	    subp <= endp;
3572 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3573 
3574 		switch (subp->dl_cap) {
3575 		case DL_CAPAB_ID_WRAPPER:
3576 			ill_capability_id_ack(ill, mp, subp);
3577 			break;
3578 		default:
3579 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3580 			break;
3581 		}
3582 	}
3583 #undef SC
3584 }
3585 
3586 /*
3587  * This routine is called to scan the fragmentation reassembly table for
3588  * the specified ILL for any packets that are starting to smell.
3589  * dead_interval is the maximum time in seconds that will be tolerated.  It
3590  * will either be the value specified in ip_g_frag_timeout, or zero if the
3591  * ILL is shutting down and it is time to blow everything off.
3592  *
3593  * It returns the number of seconds (as a time_t) that the next frag timer
3594  * should be scheduled for, 0 meaning that the timer doesn't need to be
3595  * re-started.  Note that the method of calculating next_timeout isn't
3596  * entirely accurate since time will flow between the time we grab
3597  * current_time and the time we schedule the next timeout.  This isn't a
3598  * big problem since this is the timer for sending an ICMP reassembly time
3599  * exceeded messages, and it doesn't have to be exactly accurate.
3600  *
3601  * This function is
3602  * sometimes called as writer, although this is not required.
3603  */
3604 time_t
3605 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3606 {
3607 	ipfb_t	*ipfb;
3608 	ipfb_t	*endp;
3609 	ipf_t	*ipf;
3610 	ipf_t	*ipfnext;
3611 	mblk_t	*mp;
3612 	time_t	current_time = gethrestime_sec();
3613 	time_t	next_timeout = 0;
3614 	uint32_t	hdr_length;
3615 	mblk_t	*send_icmp_head;
3616 	mblk_t	*send_icmp_head_v6;
3617 	zoneid_t zoneid;
3618 	ip_stack_t *ipst = ill->ill_ipst;
3619 
3620 	ipfb = ill->ill_frag_hash_tbl;
3621 	if (ipfb == NULL)
3622 		return (B_FALSE);
3623 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3624 	/* Walk the frag hash table. */
3625 	for (; ipfb < endp; ipfb++) {
3626 		send_icmp_head = NULL;
3627 		send_icmp_head_v6 = NULL;
3628 		mutex_enter(&ipfb->ipfb_lock);
3629 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3630 			time_t frag_time = current_time - ipf->ipf_timestamp;
3631 			time_t frag_timeout;
3632 
3633 			if (frag_time < dead_interval) {
3634 				/*
3635 				 * There are some outstanding fragments
3636 				 * that will timeout later.  Make note of
3637 				 * the time so that we can reschedule the
3638 				 * next timeout appropriately.
3639 				 */
3640 				frag_timeout = dead_interval - frag_time;
3641 				if (next_timeout == 0 ||
3642 				    frag_timeout < next_timeout) {
3643 					next_timeout = frag_timeout;
3644 				}
3645 				break;
3646 			}
3647 			/* Time's up.  Get it out of here. */
3648 			hdr_length = ipf->ipf_nf_hdr_len;
3649 			ipfnext = ipf->ipf_hash_next;
3650 			if (ipfnext)
3651 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3652 			*ipf->ipf_ptphn = ipfnext;
3653 			mp = ipf->ipf_mp->b_cont;
3654 			for (; mp; mp = mp->b_cont) {
3655 				/* Extra points for neatness. */
3656 				IP_REASS_SET_START(mp, 0);
3657 				IP_REASS_SET_END(mp, 0);
3658 			}
3659 			mp = ipf->ipf_mp->b_cont;
3660 			ill->ill_frag_count -= ipf->ipf_count;
3661 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3662 			ipfb->ipfb_count -= ipf->ipf_count;
3663 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3664 			ipfb->ipfb_frag_pkts--;
3665 			/*
3666 			 * We do not send any icmp message from here because
3667 			 * we currently are holding the ipfb_lock for this
3668 			 * hash chain. If we try and send any icmp messages
3669 			 * from here we may end up via a put back into ip
3670 			 * trying to get the same lock, causing a recursive
3671 			 * mutex panic. Instead we build a list and send all
3672 			 * the icmp messages after we have dropped the lock.
3673 			 */
3674 			if (ill->ill_isv6) {
3675 				if (hdr_length != 0) {
3676 					mp->b_next = send_icmp_head_v6;
3677 					send_icmp_head_v6 = mp;
3678 				} else {
3679 					freemsg(mp);
3680 				}
3681 			} else {
3682 				if (hdr_length != 0) {
3683 					mp->b_next = send_icmp_head;
3684 					send_icmp_head = mp;
3685 				} else {
3686 					freemsg(mp);
3687 				}
3688 			}
3689 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3690 			freeb(ipf->ipf_mp);
3691 		}
3692 		mutex_exit(&ipfb->ipfb_lock);
3693 		/*
3694 		 * Now need to send any icmp messages that we delayed from
3695 		 * above.
3696 		 */
3697 		while (send_icmp_head_v6 != NULL) {
3698 			ip6_t *ip6h;
3699 
3700 			mp = send_icmp_head_v6;
3701 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3702 			mp->b_next = NULL;
3703 			if (mp->b_datap->db_type == M_CTL)
3704 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3705 			else
3706 				ip6h = (ip6_t *)mp->b_rptr;
3707 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3708 			    ill, ipst);
3709 			if (zoneid == ALL_ZONES) {
3710 				freemsg(mp);
3711 			} else {
3712 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3713 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3714 				    B_FALSE, zoneid, ipst);
3715 			}
3716 		}
3717 		while (send_icmp_head != NULL) {
3718 			ipaddr_t dst;
3719 
3720 			mp = send_icmp_head;
3721 			send_icmp_head = send_icmp_head->b_next;
3722 			mp->b_next = NULL;
3723 
3724 			if (mp->b_datap->db_type == M_CTL)
3725 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3726 			else
3727 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3728 
3729 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3730 			if (zoneid == ALL_ZONES) {
3731 				freemsg(mp);
3732 			} else {
3733 				icmp_time_exceeded(ill->ill_wq, mp,
3734 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3735 				    ipst);
3736 			}
3737 		}
3738 	}
3739 	/*
3740 	 * A non-dying ILL will use the return value to decide whether to
3741 	 * restart the frag timer, and for how long.
3742 	 */
3743 	return (next_timeout);
3744 }
3745 
3746 /*
3747  * This routine is called when the approximate count of mblk memory used
3748  * for the specified ILL has exceeded max_count.
3749  */
3750 void
3751 ill_frag_prune(ill_t *ill, uint_t max_count)
3752 {
3753 	ipfb_t	*ipfb;
3754 	ipf_t	*ipf;
3755 	size_t	count;
3756 
3757 	/*
3758 	 * If we are here within ip_min_frag_prune_time msecs remove
3759 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3760 	 * ill_frag_free_num_pkts.
3761 	 */
3762 	mutex_enter(&ill->ill_lock);
3763 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3764 	    (ip_min_frag_prune_time != 0 ?
3765 	    ip_min_frag_prune_time : msec_per_tick)) {
3766 
3767 		ill->ill_frag_free_num_pkts++;
3768 
3769 	} else {
3770 		ill->ill_frag_free_num_pkts = 0;
3771 	}
3772 	ill->ill_last_frag_clean_time = lbolt;
3773 	mutex_exit(&ill->ill_lock);
3774 
3775 	/*
3776 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3777 	 */
3778 	if (ill->ill_frag_free_num_pkts != 0) {
3779 		int ix;
3780 
3781 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3782 			ipfb = &ill->ill_frag_hash_tbl[ix];
3783 			mutex_enter(&ipfb->ipfb_lock);
3784 			if (ipfb->ipfb_ipf != NULL) {
3785 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3786 				    ill->ill_frag_free_num_pkts);
3787 			}
3788 			mutex_exit(&ipfb->ipfb_lock);
3789 		}
3790 	}
3791 	/*
3792 	 * While the reassembly list for this ILL is too big, prune a fragment
3793 	 * queue by age, oldest first.  Note that the per ILL count is
3794 	 * approximate, while the per frag hash bucket counts are accurate.
3795 	 */
3796 	while (ill->ill_frag_count > max_count) {
3797 		int	ix;
3798 		ipfb_t	*oipfb = NULL;
3799 		uint_t	oldest = UINT_MAX;
3800 
3801 		count = 0;
3802 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3803 			ipfb = &ill->ill_frag_hash_tbl[ix];
3804 			mutex_enter(&ipfb->ipfb_lock);
3805 			ipf = ipfb->ipfb_ipf;
3806 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3807 				oldest = ipf->ipf_gen;
3808 				oipfb = ipfb;
3809 			}
3810 			count += ipfb->ipfb_count;
3811 			mutex_exit(&ipfb->ipfb_lock);
3812 		}
3813 		/* Refresh the per ILL count */
3814 		ill->ill_frag_count = count;
3815 		if (oipfb == NULL) {
3816 			ill->ill_frag_count = 0;
3817 			break;
3818 		}
3819 		if (count <= max_count)
3820 			return;	/* Somebody beat us to it, nothing to do */
3821 		mutex_enter(&oipfb->ipfb_lock);
3822 		ipf = oipfb->ipfb_ipf;
3823 		if (ipf != NULL) {
3824 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3825 		}
3826 		mutex_exit(&oipfb->ipfb_lock);
3827 	}
3828 }
3829 
3830 /*
3831  * free 'free_cnt' fragmented packets starting at ipf.
3832  */
3833 void
3834 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3835 {
3836 	size_t	count;
3837 	mblk_t	*mp;
3838 	mblk_t	*tmp;
3839 	ipf_t **ipfp = ipf->ipf_ptphn;
3840 
3841 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3842 	ASSERT(ipfp != NULL);
3843 	ASSERT(ipf != NULL);
3844 
3845 	while (ipf != NULL && free_cnt-- > 0) {
3846 		count = ipf->ipf_count;
3847 		mp = ipf->ipf_mp;
3848 		ipf = ipf->ipf_hash_next;
3849 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3850 			IP_REASS_SET_START(tmp, 0);
3851 			IP_REASS_SET_END(tmp, 0);
3852 		}
3853 		ill->ill_frag_count -= count;
3854 		ASSERT(ipfb->ipfb_count >= count);
3855 		ipfb->ipfb_count -= count;
3856 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3857 		ipfb->ipfb_frag_pkts--;
3858 		freemsg(mp);
3859 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3860 	}
3861 
3862 	if (ipf)
3863 		ipf->ipf_ptphn = ipfp;
3864 	ipfp[0] = ipf;
3865 }
3866 
3867 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3868 	"obsolete and may be removed in a future release of Solaris.  Use " \
3869 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3870 
3871 /*
3872  * For obsolete per-interface forwarding configuration;
3873  * called in response to ND_GET.
3874  */
3875 /* ARGSUSED */
3876 static int
3877 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3878 {
3879 	ill_t *ill = (ill_t *)cp;
3880 
3881 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3882 
3883 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3884 	return (0);
3885 }
3886 
3887 /*
3888  * For obsolete per-interface forwarding configuration;
3889  * called in response to ND_SET.
3890  */
3891 /* ARGSUSED */
3892 static int
3893 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3894     cred_t *ioc_cr)
3895 {
3896 	long value;
3897 	int retval;
3898 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3899 
3900 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3901 
3902 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3903 	    value < 0 || value > 1) {
3904 		return (EINVAL);
3905 	}
3906 
3907 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3908 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3909 	rw_exit(&ipst->ips_ill_g_lock);
3910 	return (retval);
3911 }
3912 
3913 /*
3914  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3915  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3916  * up RTS_IFINFO routing socket messages for each interface whose flags we
3917  * change.
3918  */
3919 int
3920 ill_forward_set(ill_t *ill, boolean_t enable)
3921 {
3922 	ill_group_t *illgrp;
3923 	ip_stack_t	*ipst = ill->ill_ipst;
3924 
3925 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3926 
3927 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3928 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3929 		return (0);
3930 
3931 	if (IS_LOOPBACK(ill))
3932 		return (EINVAL);
3933 
3934 	/*
3935 	 * If the ill is in an IPMP group, set the forwarding policy on all
3936 	 * members of the group to the same value.
3937 	 */
3938 	illgrp = ill->ill_group;
3939 	if (illgrp != NULL) {
3940 		ill_t *tmp_ill;
3941 
3942 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3943 		    tmp_ill = tmp_ill->ill_group_next) {
3944 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3945 			    (enable ? "Enabling" : "Disabling"),
3946 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3947 			    tmp_ill->ill_name));
3948 			mutex_enter(&tmp_ill->ill_lock);
3949 			if (enable)
3950 				tmp_ill->ill_flags |= ILLF_ROUTER;
3951 			else
3952 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3953 			mutex_exit(&tmp_ill->ill_lock);
3954 			if (tmp_ill->ill_isv6)
3955 				ill_set_nce_router_flags(tmp_ill, enable);
3956 			/* Notify routing socket listeners of this change. */
3957 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3958 		}
3959 	} else {
3960 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3961 		    (enable ? "Enabling" : "Disabling"),
3962 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3963 		mutex_enter(&ill->ill_lock);
3964 		if (enable)
3965 			ill->ill_flags |= ILLF_ROUTER;
3966 		else
3967 			ill->ill_flags &= ~ILLF_ROUTER;
3968 		mutex_exit(&ill->ill_lock);
3969 		if (ill->ill_isv6)
3970 			ill_set_nce_router_flags(ill, enable);
3971 		/* Notify routing socket listeners of this change. */
3972 		ip_rts_ifmsg(ill->ill_ipif);
3973 	}
3974 
3975 	return (0);
3976 }
3977 
3978 /*
3979  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3980  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3981  * set or clear.
3982  */
3983 static void
3984 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3985 {
3986 	ipif_t *ipif;
3987 	nce_t *nce;
3988 
3989 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3990 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3991 		if (nce != NULL) {
3992 			mutex_enter(&nce->nce_lock);
3993 			if (enable)
3994 				nce->nce_flags |= NCE_F_ISROUTER;
3995 			else
3996 				nce->nce_flags &= ~NCE_F_ISROUTER;
3997 			mutex_exit(&nce->nce_lock);
3998 			NCE_REFRELE(nce);
3999 		}
4000 	}
4001 }
4002 
4003 /*
4004  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4005  * for this ill.  Make sure the v6/v4 question has been answered about this
4006  * ill.  The creation of this ndd variable is only for backwards compatibility.
4007  * The preferred way to control per-interface IP forwarding is through the
4008  * ILLF_ROUTER interface flag.
4009  */
4010 static int
4011 ill_set_ndd_name(ill_t *ill)
4012 {
4013 	char *suffix;
4014 	ip_stack_t	*ipst = ill->ill_ipst;
4015 
4016 	ASSERT(IAM_WRITER_ILL(ill));
4017 
4018 	if (ill->ill_isv6)
4019 		suffix = ipv6_forward_suffix;
4020 	else
4021 		suffix = ipv4_forward_suffix;
4022 
4023 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4024 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4025 	/*
4026 	 * Copies over the '\0'.
4027 	 * Note that strlen(suffix) is always bounded.
4028 	 */
4029 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4030 	    strlen(suffix) + 1);
4031 
4032 	/*
4033 	 * Use of the nd table requires holding the reader lock.
4034 	 * Modifying the nd table thru nd_load/nd_unload requires
4035 	 * the writer lock.
4036 	 */
4037 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4038 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4039 	    nd_ill_forward_set, (caddr_t)ill)) {
4040 		/*
4041 		 * If the nd_load failed, it only meant that it could not
4042 		 * allocate a new bunch of room for further NDD expansion.
4043 		 * Because of that, the ill_ndd_name will be set to 0, and
4044 		 * this interface is at the mercy of the global ip_forwarding
4045 		 * variable.
4046 		 */
4047 		rw_exit(&ipst->ips_ip_g_nd_lock);
4048 		ill->ill_ndd_name = NULL;
4049 		return (ENOMEM);
4050 	}
4051 	rw_exit(&ipst->ips_ip_g_nd_lock);
4052 	return (0);
4053 }
4054 
4055 /*
4056  * Intializes the context structure and returns the first ill in the list
4057  * cuurently start_list and end_list can have values:
4058  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4059  * IP_V4_G_HEAD		Traverse IPV4 list only.
4060  * IP_V6_G_HEAD		Traverse IPV6 list only.
4061  */
4062 
4063 /*
4064  * We don't check for CONDEMNED ills here. Caller must do that if
4065  * necessary under the ill lock.
4066  */
4067 ill_t *
4068 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4069     ip_stack_t *ipst)
4070 {
4071 	ill_if_t *ifp;
4072 	ill_t *ill;
4073 	avl_tree_t *avl_tree;
4074 
4075 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4076 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4077 
4078 	/*
4079 	 * setup the lists to search
4080 	 */
4081 	if (end_list != MAX_G_HEADS) {
4082 		ctx->ctx_current_list = start_list;
4083 		ctx->ctx_last_list = end_list;
4084 	} else {
4085 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4086 		ctx->ctx_current_list = 0;
4087 	}
4088 
4089 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4090 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4091 		if (ifp != (ill_if_t *)
4092 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4093 			avl_tree = &ifp->illif_avl_by_ppa;
4094 			ill = avl_first(avl_tree);
4095 			/*
4096 			 * ill is guaranteed to be non NULL or ifp should have
4097 			 * not existed.
4098 			 */
4099 			ASSERT(ill != NULL);
4100 			return (ill);
4101 		}
4102 		ctx->ctx_current_list++;
4103 	}
4104 
4105 	return (NULL);
4106 }
4107 
4108 /*
4109  * returns the next ill in the list. ill_first() must have been called
4110  * before calling ill_next() or bad things will happen.
4111  */
4112 
4113 /*
4114  * We don't check for CONDEMNED ills here. Caller must do that if
4115  * necessary under the ill lock.
4116  */
4117 ill_t *
4118 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4119 {
4120 	ill_if_t *ifp;
4121 	ill_t *ill;
4122 	ip_stack_t	*ipst = lastill->ill_ipst;
4123 
4124 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4125 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4126 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4127 	    AVL_AFTER)) != NULL) {
4128 		return (ill);
4129 	}
4130 
4131 	/* goto next ill_ifp in the list. */
4132 	ifp = lastill->ill_ifptr->illif_next;
4133 
4134 	/* make sure not at end of circular list */
4135 	while (ifp ==
4136 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4137 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4138 			return (NULL);
4139 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4140 	}
4141 
4142 	return (avl_first(&ifp->illif_avl_by_ppa));
4143 }
4144 
4145 /*
4146  * Check interface name for correct format which is name+ppa.
4147  * name can contain characters and digits, the right most digits
4148  * make up the ppa number. use of octal is not allowed, name must contain
4149  * a ppa, return pointer to the start of ppa.
4150  * In case of error return NULL.
4151  */
4152 static char *
4153 ill_get_ppa_ptr(char *name)
4154 {
4155 	int namelen = mi_strlen(name);
4156 
4157 	int len = namelen;
4158 
4159 	name += len;
4160 	while (len > 0) {
4161 		name--;
4162 		if (*name < '0' || *name > '9')
4163 			break;
4164 		len--;
4165 	}
4166 
4167 	/* empty string, all digits, or no trailing digits */
4168 	if (len == 0 || len == (int)namelen)
4169 		return (NULL);
4170 
4171 	name++;
4172 	/* check for attempted use of octal */
4173 	if (*name == '0' && len != (int)namelen - 1)
4174 		return (NULL);
4175 	return (name);
4176 }
4177 
4178 /*
4179  * use avl tree to locate the ill.
4180  */
4181 static ill_t *
4182 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4183     ipsq_func_t func, int *error, ip_stack_t *ipst)
4184 {
4185 	char *ppa_ptr = NULL;
4186 	int len;
4187 	uint_t ppa;
4188 	ill_t *ill = NULL;
4189 	ill_if_t *ifp;
4190 	int list;
4191 	ipsq_t *ipsq;
4192 
4193 	if (error != NULL)
4194 		*error = 0;
4195 
4196 	/*
4197 	 * get ppa ptr
4198 	 */
4199 	if (isv6)
4200 		list = IP_V6_G_HEAD;
4201 	else
4202 		list = IP_V4_G_HEAD;
4203 
4204 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4205 		if (error != NULL)
4206 			*error = ENXIO;
4207 		return (NULL);
4208 	}
4209 
4210 	len = ppa_ptr - name + 1;
4211 
4212 	ppa = stoi(&ppa_ptr);
4213 
4214 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4215 
4216 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4217 		/*
4218 		 * match is done on len - 1 as the name is not null
4219 		 * terminated it contains ppa in addition to the interface
4220 		 * name.
4221 		 */
4222 		if ((ifp->illif_name_len == len) &&
4223 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4224 			break;
4225 		} else {
4226 			ifp = ifp->illif_next;
4227 		}
4228 	}
4229 
4230 
4231 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4232 		/*
4233 		 * Even the interface type does not exist.
4234 		 */
4235 		if (error != NULL)
4236 			*error = ENXIO;
4237 		return (NULL);
4238 	}
4239 
4240 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4241 	if (ill != NULL) {
4242 		/*
4243 		 * The block comment at the start of ipif_down
4244 		 * explains the use of the macros used below
4245 		 */
4246 		GRAB_CONN_LOCK(q);
4247 		mutex_enter(&ill->ill_lock);
4248 		if (ILL_CAN_LOOKUP(ill)) {
4249 			ill_refhold_locked(ill);
4250 			mutex_exit(&ill->ill_lock);
4251 			RELEASE_CONN_LOCK(q);
4252 			return (ill);
4253 		} else if (ILL_CAN_WAIT(ill, q)) {
4254 			ipsq = ill->ill_phyint->phyint_ipsq;
4255 			mutex_enter(&ipsq->ipsq_lock);
4256 			mutex_exit(&ill->ill_lock);
4257 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4258 			mutex_exit(&ipsq->ipsq_lock);
4259 			RELEASE_CONN_LOCK(q);
4260 			if (error != NULL)
4261 				*error = EINPROGRESS;
4262 			return (NULL);
4263 		}
4264 		mutex_exit(&ill->ill_lock);
4265 		RELEASE_CONN_LOCK(q);
4266 	}
4267 	if (error != NULL)
4268 		*error = ENXIO;
4269 	return (NULL);
4270 }
4271 
4272 /*
4273  * comparison function for use with avl.
4274  */
4275 static int
4276 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4277 {
4278 	uint_t ppa;
4279 	uint_t ill_ppa;
4280 
4281 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4282 
4283 	ppa = *((uint_t *)ppa_ptr);
4284 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4285 	/*
4286 	 * We want the ill with the lowest ppa to be on the
4287 	 * top.
4288 	 */
4289 	if (ill_ppa < ppa)
4290 		return (1);
4291 	if (ill_ppa > ppa)
4292 		return (-1);
4293 	return (0);
4294 }
4295 
4296 /*
4297  * remove an interface type from the global list.
4298  */
4299 static void
4300 ill_delete_interface_type(ill_if_t *interface)
4301 {
4302 	ASSERT(interface != NULL);
4303 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4304 
4305 	avl_destroy(&interface->illif_avl_by_ppa);
4306 	if (interface->illif_ppa_arena != NULL)
4307 		vmem_destroy(interface->illif_ppa_arena);
4308 
4309 	remque(interface);
4310 
4311 	mi_free(interface);
4312 }
4313 
4314 /* Defined in ip_netinfo.c */
4315 extern ddi_taskq_t	*eventq_queue_nic;
4316 
4317 /*
4318  * remove ill from the global list.
4319  */
4320 static void
4321 ill_glist_delete(ill_t *ill)
4322 {
4323 	char *nicname;
4324 	size_t nicnamelen;
4325 	hook_nic_event_t *info;
4326 	ip_stack_t	*ipst;
4327 
4328 	if (ill == NULL)
4329 		return;
4330 	ipst = ill->ill_ipst;
4331 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4332 
4333 	if (ill->ill_name != NULL) {
4334 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4335 		if (nicname != NULL) {
4336 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4337 			nicnamelen = ill->ill_name_length;
4338 		}
4339 	} else {
4340 		nicname = NULL;
4341 		nicnamelen = 0;
4342 	}
4343 
4344 	/*
4345 	 * If the ill was never inserted into the AVL tree
4346 	 * we skip the if branch.
4347 	 */
4348 	if (ill->ill_ifptr != NULL) {
4349 		/*
4350 		 * remove from AVL tree and free ppa number
4351 		 */
4352 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4353 
4354 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4355 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4356 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4357 		}
4358 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4359 			ill_delete_interface_type(ill->ill_ifptr);
4360 		}
4361 
4362 		/*
4363 		 * Indicate ill is no longer in the list.
4364 		 */
4365 		ill->ill_ifptr = NULL;
4366 		ill->ill_name_length = 0;
4367 		ill->ill_name[0] = '\0';
4368 		ill->ill_ppa = UINT_MAX;
4369 	}
4370 
4371 	/*
4372 	 * Run the unplumb hook after the NIC has disappeared from being
4373 	 * visible so that attempts to revalidate its existance will fail.
4374 	 *
4375 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4376 	 * that the ordering of delivered events to listeners matches the
4377 	 * order of them in the kernel.
4378 	 */
4379 	if ((info = ill->ill_nic_event_info) != NULL) {
4380 		if (info->hne_event != NE_DOWN) {
4381 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4382 			    "attached for %s\n", info->hne_event,
4383 			    ill->ill_name));
4384 			if (info->hne_data != NULL)
4385 				kmem_free(info->hne_data, info->hne_datalen);
4386 			kmem_free(info, sizeof (hook_nic_event_t));
4387 		} else {
4388 			if (ddi_taskq_dispatch(eventq_queue_nic,
4389 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4390 			    == DDI_FAILURE) {
4391 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4392 				    "failed\n"));
4393 				if (info->hne_data != NULL)
4394 					kmem_free(info->hne_data,
4395 					    info->hne_datalen);
4396 				kmem_free(info, sizeof (hook_nic_event_t));
4397 			}
4398 		}
4399 	}
4400 
4401 	/* Generate NE_UNPLUMB event for ill_name. */
4402 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4403 	if (info != NULL) {
4404 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4405 		info->hne_lif = 0;
4406 		info->hne_event = NE_UNPLUMB;
4407 		info->hne_data = nicname;
4408 		info->hne_datalen = nicnamelen;
4409 		info->hne_family = ill->ill_isv6 ?
4410 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4411 	} else {
4412 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4413 		    "information for %s (ENOMEM)\n", ill->ill_name));
4414 		if (nicname != NULL)
4415 			kmem_free(nicname, nicnamelen);
4416 	}
4417 
4418 	ill->ill_nic_event_info = info;
4419 
4420 	ill_phyint_free(ill);
4421 	rw_exit(&ipst->ips_ill_g_lock);
4422 }
4423 
4424 /*
4425  * allocate a ppa, if the number of plumbed interfaces of this type are
4426  * less than ill_no_arena do a linear search to find a unused ppa.
4427  * When the number goes beyond ill_no_arena switch to using an arena.
4428  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4429  * is the return value for an error condition, so allocation starts at one
4430  * and is decremented by one.
4431  */
4432 static int
4433 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4434 {
4435 	ill_t *tmp_ill;
4436 	uint_t start, end;
4437 	int ppa;
4438 
4439 	if (ifp->illif_ppa_arena == NULL &&
4440 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4441 		/*
4442 		 * Create an arena.
4443 		 */
4444 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4445 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4446 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4447 			/* allocate what has already been assigned */
4448 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4449 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4450 		    tmp_ill, AVL_AFTER)) {
4451 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4452 			    1,		/* size */
4453 			    1,		/* align/quantum */
4454 			    0,		/* phase */
4455 			    0,		/* nocross */
4456 			    /* minaddr */
4457 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4458 			    /* maxaddr */
4459 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4460 			    VM_NOSLEEP|VM_FIRSTFIT);
4461 			if (ppa == 0) {
4462 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4463 				    " failed while switching"));
4464 				vmem_destroy(ifp->illif_ppa_arena);
4465 				ifp->illif_ppa_arena = NULL;
4466 				break;
4467 			}
4468 		}
4469 	}
4470 
4471 	if (ifp->illif_ppa_arena != NULL) {
4472 		if (ill->ill_ppa == UINT_MAX) {
4473 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4474 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4475 			if (ppa == 0)
4476 				return (EAGAIN);
4477 			ill->ill_ppa = --ppa;
4478 		} else {
4479 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4480 			    1, 		/* size */
4481 			    1, 		/* align/quantum */
4482 			    0, 		/* phase */
4483 			    0, 		/* nocross */
4484 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4485 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4486 			    VM_NOSLEEP|VM_FIRSTFIT);
4487 			/*
4488 			 * Most likely the allocation failed because
4489 			 * the requested ppa was in use.
4490 			 */
4491 			if (ppa == 0)
4492 				return (EEXIST);
4493 		}
4494 		return (0);
4495 	}
4496 
4497 	/*
4498 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4499 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4500 	 */
4501 	if (ill->ill_ppa == UINT_MAX) {
4502 		end = UINT_MAX - 1;
4503 		start = 0;
4504 	} else {
4505 		end = start = ill->ill_ppa;
4506 	}
4507 
4508 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4509 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4510 		if (start++ >= end) {
4511 			if (ill->ill_ppa == UINT_MAX)
4512 				return (EAGAIN);
4513 			else
4514 				return (EEXIST);
4515 		}
4516 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4517 	}
4518 	ill->ill_ppa = start;
4519 	return (0);
4520 }
4521 
4522 /*
4523  * Insert ill into the list of configured ill's. Once this function completes,
4524  * the ill is globally visible and is available through lookups. More precisely
4525  * this happens after the caller drops the ill_g_lock.
4526  */
4527 static int
4528 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4529 {
4530 	ill_if_t *ill_interface;
4531 	avl_index_t where = 0;
4532 	int error;
4533 	int name_length;
4534 	int index;
4535 	boolean_t check_length = B_FALSE;
4536 	ip_stack_t	*ipst = ill->ill_ipst;
4537 
4538 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4539 
4540 	name_length = mi_strlen(name) + 1;
4541 
4542 	if (isv6)
4543 		index = IP_V6_G_HEAD;
4544 	else
4545 		index = IP_V4_G_HEAD;
4546 
4547 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4548 	/*
4549 	 * Search for interface type based on name
4550 	 */
4551 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4552 		if ((ill_interface->illif_name_len == name_length) &&
4553 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4554 			break;
4555 		}
4556 		ill_interface = ill_interface->illif_next;
4557 	}
4558 
4559 	/*
4560 	 * Interface type not found, create one.
4561 	 */
4562 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4563 
4564 		ill_g_head_t ghead;
4565 
4566 		/*
4567 		 * allocate ill_if_t structure
4568 		 */
4569 
4570 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4571 		if (ill_interface == NULL) {
4572 			return (ENOMEM);
4573 		}
4574 
4575 
4576 
4577 		(void) strcpy(ill_interface->illif_name, name);
4578 		ill_interface->illif_name_len = name_length;
4579 
4580 		avl_create(&ill_interface->illif_avl_by_ppa,
4581 		    ill_compare_ppa, sizeof (ill_t),
4582 		    offsetof(struct ill_s, ill_avl_byppa));
4583 
4584 		/*
4585 		 * link the structure in the back to maintain order
4586 		 * of configuration for ifconfig output.
4587 		 */
4588 		ghead = ipst->ips_ill_g_heads[index];
4589 		insque(ill_interface, ghead.ill_g_list_tail);
4590 
4591 	}
4592 
4593 	if (ill->ill_ppa == UINT_MAX)
4594 		check_length = B_TRUE;
4595 
4596 	error = ill_alloc_ppa(ill_interface, ill);
4597 	if (error != 0) {
4598 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4599 			ill_delete_interface_type(ill->ill_ifptr);
4600 		return (error);
4601 	}
4602 
4603 	/*
4604 	 * When the ppa is choosen by the system, check that there is
4605 	 * enough space to insert ppa. if a specific ppa was passed in this
4606 	 * check is not required as the interface name passed in will have
4607 	 * the right ppa in it.
4608 	 */
4609 	if (check_length) {
4610 		/*
4611 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4612 		 */
4613 		char buf[sizeof (uint_t) * 3];
4614 
4615 		/*
4616 		 * convert ppa to string to calculate the amount of space
4617 		 * required for it in the name.
4618 		 */
4619 		numtos(ill->ill_ppa, buf);
4620 
4621 		/* Do we have enough space to insert ppa ? */
4622 
4623 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4624 			/* Free ppa and interface type struct */
4625 			if (ill_interface->illif_ppa_arena != NULL) {
4626 				vmem_free(ill_interface->illif_ppa_arena,
4627 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4628 			}
4629 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4630 			    0) {
4631 				ill_delete_interface_type(ill->ill_ifptr);
4632 			}
4633 
4634 			return (EINVAL);
4635 		}
4636 	}
4637 
4638 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4639 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4640 
4641 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4642 	    &where);
4643 	ill->ill_ifptr = ill_interface;
4644 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4645 
4646 	ill_phyint_reinit(ill);
4647 	return (0);
4648 }
4649 
4650 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4651 static boolean_t
4652 ipsq_init(ill_t *ill)
4653 {
4654 	ipsq_t  *ipsq;
4655 
4656 	/* Init the ipsq and impicitly enter as writer */
4657 	ill->ill_phyint->phyint_ipsq =
4658 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4659 	if (ill->ill_phyint->phyint_ipsq == NULL)
4660 		return (B_FALSE);
4661 	ipsq = ill->ill_phyint->phyint_ipsq;
4662 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4663 	ill->ill_phyint->phyint_ipsq_next = NULL;
4664 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4665 	ipsq->ipsq_refs = 1;
4666 	ipsq->ipsq_writer = curthread;
4667 	ipsq->ipsq_reentry_cnt = 1;
4668 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4669 #ifdef DEBUG
4670 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4671 	    IPSQ_STACK_DEPTH);
4672 #endif
4673 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4674 	return (B_TRUE);
4675 }
4676 
4677 /*
4678  * ill_init is called by ip_open when a device control stream is opened.
4679  * It does a few initializations, and shoots a DL_INFO_REQ message down
4680  * to the driver.  The response is later picked up in ip_rput_dlpi and
4681  * used to set up default mechanisms for talking to the driver.  (Always
4682  * called as writer.)
4683  *
4684  * If this function returns error, ip_open will call ip_close which in
4685  * turn will call ill_delete to clean up any memory allocated here that
4686  * is not yet freed.
4687  */
4688 int
4689 ill_init(queue_t *q, ill_t *ill)
4690 {
4691 	int	count;
4692 	dl_info_req_t	*dlir;
4693 	mblk_t	*info_mp;
4694 	uchar_t *frag_ptr;
4695 
4696 	/*
4697 	 * The ill is initialized to zero by mi_alloc*(). In addition
4698 	 * some fields already contain valid values, initialized in
4699 	 * ip_open(), before we reach here.
4700 	 */
4701 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4702 
4703 	ill->ill_rq = q;
4704 	ill->ill_wq = WR(q);
4705 
4706 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4707 	    BPRI_HI);
4708 	if (info_mp == NULL)
4709 		return (ENOMEM);
4710 
4711 	/*
4712 	 * Allocate sufficient space to contain our fragment hash table and
4713 	 * the device name.
4714 	 */
4715 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4716 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4717 	if (frag_ptr == NULL) {
4718 		freemsg(info_mp);
4719 		return (ENOMEM);
4720 	}
4721 	ill->ill_frag_ptr = frag_ptr;
4722 	ill->ill_frag_free_num_pkts = 0;
4723 	ill->ill_last_frag_clean_time = 0;
4724 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4725 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4726 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4727 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4728 		    NULL, MUTEX_DEFAULT, NULL);
4729 	}
4730 
4731 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4732 	if (ill->ill_phyint == NULL) {
4733 		freemsg(info_mp);
4734 		mi_free(frag_ptr);
4735 		return (ENOMEM);
4736 	}
4737 
4738 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4739 	/*
4740 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4741 	 * at this point because of the following reason. If we can't
4742 	 * enter the ipsq at some point and cv_wait, the writer that
4743 	 * wakes us up tries to locate us using the list of all phyints
4744 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4745 	 * If we don't set it now, we risk a missed wakeup.
4746 	 */
4747 	ill->ill_phyint->phyint_illv4 = ill;
4748 	ill->ill_ppa = UINT_MAX;
4749 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4750 
4751 	if (!ipsq_init(ill)) {
4752 		freemsg(info_mp);
4753 		mi_free(frag_ptr);
4754 		mi_free(ill->ill_phyint);
4755 		return (ENOMEM);
4756 	}
4757 
4758 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4759 
4760 
4761 	/* Frag queue limit stuff */
4762 	ill->ill_frag_count = 0;
4763 	ill->ill_ipf_gen = 0;
4764 
4765 	ill->ill_global_timer = INFINITY;
4766 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4767 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4768 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4769 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4770 
4771 	/*
4772 	 * Initialize IPv6 configuration variables.  The IP module is always
4773 	 * opened as an IPv4 module.  Instead tracking down the cases where
4774 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4775 	 * here for convenience, this has no effect until the ill is set to do
4776 	 * IPv6.
4777 	 */
4778 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4779 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4780 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4781 	ill->ill_max_buf = ND_MAX_Q;
4782 	ill->ill_refcnt = 0;
4783 
4784 	/* Send down the Info Request to the driver. */
4785 	info_mp->b_datap->db_type = M_PCPROTO;
4786 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4787 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4788 	dlir->dl_primitive = DL_INFO_REQ;
4789 
4790 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4791 
4792 	qprocson(q);
4793 	ill_dlpi_send(ill, info_mp);
4794 
4795 	return (0);
4796 }
4797 
4798 /*
4799  * ill_dls_info
4800  * creates datalink socket info from the device.
4801  */
4802 int
4803 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4804 {
4805 	size_t	len;
4806 	ill_t	*ill = ipif->ipif_ill;
4807 
4808 	sdl->sdl_family = AF_LINK;
4809 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4810 	sdl->sdl_type = ill->ill_type;
4811 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4812 	len = strlen(sdl->sdl_data);
4813 	ASSERT(len < 256);
4814 	sdl->sdl_nlen = (uchar_t)len;
4815 	sdl->sdl_alen = ill->ill_phys_addr_length;
4816 	sdl->sdl_slen = 0;
4817 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4818 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4819 
4820 	return (sizeof (struct sockaddr_dl));
4821 }
4822 
4823 /*
4824  * ill_xarp_info
4825  * creates xarp info from the device.
4826  */
4827 static int
4828 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4829 {
4830 	sdl->sdl_family = AF_LINK;
4831 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4832 	sdl->sdl_type = ill->ill_type;
4833 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4834 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4835 	sdl->sdl_alen = ill->ill_phys_addr_length;
4836 	sdl->sdl_slen = 0;
4837 	return (sdl->sdl_nlen);
4838 }
4839 
4840 static int
4841 loopback_kstat_update(kstat_t *ksp, int rw)
4842 {
4843 	kstat_named_t *kn;
4844 	netstackid_t	stackid;
4845 	netstack_t	*ns;
4846 	ip_stack_t	*ipst;
4847 
4848 	if (ksp == NULL || ksp->ks_data == NULL)
4849 		return (EIO);
4850 
4851 	if (rw == KSTAT_WRITE)
4852 		return (EACCES);
4853 
4854 	kn = KSTAT_NAMED_PTR(ksp);
4855 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4856 
4857 	ns = netstack_find_by_stackid(stackid);
4858 	if (ns == NULL)
4859 		return (-1);
4860 
4861 	ipst = ns->netstack_ip;
4862 	if (ipst == NULL) {
4863 		netstack_rele(ns);
4864 		return (-1);
4865 	}
4866 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4867 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4868 	netstack_rele(ns);
4869 	return (0);
4870 }
4871 
4872 
4873 /*
4874  * Has ifindex been plumbed already.
4875  * Compares both phyint_ifindex and phyint_group_ifindex.
4876  */
4877 static boolean_t
4878 phyint_exists(uint_t index, ip_stack_t *ipst)
4879 {
4880 	phyint_t *phyi;
4881 
4882 	ASSERT(index != 0);
4883 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4884 	/*
4885 	 * Indexes are stored in the phyint - a common structure
4886 	 * to both IPv4 and IPv6.
4887 	 */
4888 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4889 	for (; phyi != NULL;
4890 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4891 	    phyi, AVL_AFTER)) {
4892 		if (phyi->phyint_ifindex == index ||
4893 		    phyi->phyint_group_ifindex == index)
4894 			return (B_TRUE);
4895 	}
4896 	return (B_FALSE);
4897 }
4898 
4899 /* Pick a unique ifindex */
4900 boolean_t
4901 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4902 {
4903 	uint_t starting_index;
4904 
4905 	if (!ipst->ips_ill_index_wrap) {
4906 		*indexp = ipst->ips_ill_index++;
4907 		if (ipst->ips_ill_index == 0) {
4908 			/* Reached the uint_t limit Next time wrap  */
4909 			ipst->ips_ill_index_wrap = B_TRUE;
4910 		}
4911 		return (B_TRUE);
4912 	}
4913 
4914 	/*
4915 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4916 	 * at this point and don't want to call any function that attempts
4917 	 * to get the lock again.
4918 	 */
4919 	starting_index = ipst->ips_ill_index++;
4920 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4921 		if (ipst->ips_ill_index != 0 &&
4922 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4923 			/* found unused index - use it */
4924 			*indexp = ipst->ips_ill_index;
4925 			return (B_TRUE);
4926 		}
4927 	}
4928 
4929 	/*
4930 	 * all interface indicies are inuse.
4931 	 */
4932 	return (B_FALSE);
4933 }
4934 
4935 /*
4936  * Assign a unique interface index for the phyint.
4937  */
4938 static boolean_t
4939 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4940 {
4941 	ASSERT(phyi->phyint_ifindex == 0);
4942 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4943 }
4944 
4945 /*
4946  * Return a pointer to the ill which matches the supplied name.  Note that
4947  * the ill name length includes the null termination character.  (May be
4948  * called as writer.)
4949  * If do_alloc and the interface is "lo0" it will be automatically created.
4950  * Cannot bump up reference on condemned ills. So dup detect can't be done
4951  * using this func.
4952  */
4953 ill_t *
4954 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4955     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4956     ip_stack_t *ipst)
4957 {
4958 	ill_t	*ill;
4959 	ipif_t	*ipif;
4960 	kstat_named_t	*kn;
4961 	boolean_t isloopback;
4962 	ipsq_t *old_ipsq;
4963 	in6_addr_t ov6addr;
4964 
4965 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4966 
4967 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4968 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4969 	rw_exit(&ipst->ips_ill_g_lock);
4970 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4971 		return (ill);
4972 
4973 	/*
4974 	 * Couldn't find it.  Does this happen to be a lookup for the
4975 	 * loopback device and are we allowed to allocate it?
4976 	 */
4977 	if (!isloopback || !do_alloc)
4978 		return (NULL);
4979 
4980 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4981 
4982 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4983 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4984 		rw_exit(&ipst->ips_ill_g_lock);
4985 		return (ill);
4986 	}
4987 
4988 	/* Create the loopback device on demand */
4989 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4990 	    sizeof (ipif_loopback_name), BPRI_MED));
4991 	if (ill == NULL)
4992 		goto done;
4993 
4994 	*ill = ill_null;
4995 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4996 	ill->ill_ipst = ipst;
4997 	netstack_hold(ipst->ips_netstack);
4998 	/*
4999 	 * For exclusive stacks we set the zoneid to zero
5000 	 * to make IP operate as if in the global zone.
5001 	 */
5002 	ill->ill_zoneid = GLOBAL_ZONEID;
5003 
5004 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5005 	if (ill->ill_phyint == NULL)
5006 		goto done;
5007 
5008 	if (isv6)
5009 		ill->ill_phyint->phyint_illv6 = ill;
5010 	else
5011 		ill->ill_phyint->phyint_illv4 = ill;
5012 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5013 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5014 	/* Add room for tcp+ip headers */
5015 	if (isv6) {
5016 		ill->ill_isv6 = B_TRUE;
5017 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5018 	} else {
5019 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5020 	}
5021 	if (!ill_allocate_mibs(ill))
5022 		goto done;
5023 	ill->ill_max_mtu = ill->ill_max_frag;
5024 	/*
5025 	 * ipif_loopback_name can't be pointed at directly because its used
5026 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5027 	 * from the glist, ill_glist_delete() sets the first character of
5028 	 * ill_name to '\0'.
5029 	 */
5030 	ill->ill_name = (char *)ill + sizeof (*ill);
5031 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5032 	ill->ill_name_length = sizeof (ipif_loopback_name);
5033 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5034 
5035 	ill->ill_global_timer = INFINITY;
5036 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5037 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5038 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5039 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5040 
5041 	/* No resolver here. */
5042 	ill->ill_net_type = IRE_LOOPBACK;
5043 
5044 	/* Initialize the ipsq */
5045 	if (!ipsq_init(ill))
5046 		goto done;
5047 
5048 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5049 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5050 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5051 #ifdef DEBUG
5052 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5053 #endif
5054 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5055 	if (ipif == NULL)
5056 		goto done;
5057 
5058 	ill->ill_flags = ILLF_MULTICAST;
5059 
5060 	ov6addr = ipif->ipif_v6lcl_addr;
5061 	/* Set up default loopback address and mask. */
5062 	if (!isv6) {
5063 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5064 
5065 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5066 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5067 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5068 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5069 		    ipif->ipif_v6subnet);
5070 		ill->ill_flags |= ILLF_IPV4;
5071 	} else {
5072 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5073 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5074 		ipif->ipif_v6net_mask = ipv6_all_ones;
5075 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5076 		    ipif->ipif_v6subnet);
5077 		ill->ill_flags |= ILLF_IPV6;
5078 	}
5079 
5080 	/*
5081 	 * Chain us in at the end of the ill list. hold the ill
5082 	 * before we make it globally visible. 1 for the lookup.
5083 	 */
5084 	ill->ill_refcnt = 0;
5085 	ill_refhold(ill);
5086 
5087 	ill->ill_frag_count = 0;
5088 	ill->ill_frag_free_num_pkts = 0;
5089 	ill->ill_last_frag_clean_time = 0;
5090 
5091 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5092 
5093 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5094 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5095 
5096 	/* Let SCTP know so that it can add this to its list */
5097 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5098 
5099 	/*
5100 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5101 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5102 	 * requires to be after ill_glist_insert() since we need the
5103 	 * ill_index set. Pass on ipv6_loopback as the old address.
5104 	 */
5105 	sctp_update_ipif_addr(ipif, ov6addr);
5106 
5107 	/*
5108 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5109 	 */
5110 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5111 		/* Loopback ills aren't in any IPMP group */
5112 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5113 		ipsq_delete(old_ipsq);
5114 	}
5115 
5116 	/*
5117 	 * Delay this till the ipif is allocated as ipif_allocate
5118 	 * de-references ill_phyint for getting the ifindex. We
5119 	 * can't do this before ipif_allocate because ill_phyint_reinit
5120 	 * -> phyint_assign_ifindex expects ipif to be present.
5121 	 */
5122 	mutex_enter(&ill->ill_phyint->phyint_lock);
5123 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5124 	mutex_exit(&ill->ill_phyint->phyint_lock);
5125 
5126 	if (ipst->ips_loopback_ksp == NULL) {
5127 		/* Export loopback interface statistics */
5128 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5129 		    ipif_loopback_name, "net",
5130 		    KSTAT_TYPE_NAMED, 2, 0,
5131 		    ipst->ips_netstack->netstack_stackid);
5132 		if (ipst->ips_loopback_ksp != NULL) {
5133 			ipst->ips_loopback_ksp->ks_update =
5134 			    loopback_kstat_update;
5135 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5136 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5137 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5138 			ipst->ips_loopback_ksp->ks_private =
5139 			    (void *)(uintptr_t)ipst->ips_netstack->
5140 			    netstack_stackid;
5141 			kstat_install(ipst->ips_loopback_ksp);
5142 		}
5143 	}
5144 
5145 	if (error != NULL)
5146 		*error = 0;
5147 	*did_alloc = B_TRUE;
5148 	rw_exit(&ipst->ips_ill_g_lock);
5149 	return (ill);
5150 done:
5151 	if (ill != NULL) {
5152 		if (ill->ill_phyint != NULL) {
5153 			ipsq_t	*ipsq;
5154 
5155 			ipsq = ill->ill_phyint->phyint_ipsq;
5156 			if (ipsq != NULL) {
5157 				ipsq->ipsq_ipst = NULL;
5158 				kmem_free(ipsq, sizeof (ipsq_t));
5159 			}
5160 			mi_free(ill->ill_phyint);
5161 		}
5162 		ill_free_mib(ill);
5163 		if (ill->ill_ipst != NULL)
5164 			netstack_rele(ill->ill_ipst->ips_netstack);
5165 		mi_free(ill);
5166 	}
5167 	rw_exit(&ipst->ips_ill_g_lock);
5168 	if (error != NULL)
5169 		*error = ENOMEM;
5170 	return (NULL);
5171 }
5172 
5173 /*
5174  * For IPP calls - use the ip_stack_t for global stack.
5175  */
5176 ill_t *
5177 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5178     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5179 {
5180 	ip_stack_t	*ipst;
5181 	ill_t		*ill;
5182 
5183 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5184 	if (ipst == NULL) {
5185 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5186 		return (NULL);
5187 	}
5188 
5189 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5190 	netstack_rele(ipst->ips_netstack);
5191 	return (ill);
5192 }
5193 
5194 /*
5195  * Return a pointer to the ill which matches the index and IP version type.
5196  */
5197 ill_t *
5198 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5199     ipsq_func_t func, int *err, ip_stack_t *ipst)
5200 {
5201 	ill_t	*ill;
5202 	ipsq_t  *ipsq;
5203 	phyint_t *phyi;
5204 
5205 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5206 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5207 
5208 	if (err != NULL)
5209 		*err = 0;
5210 
5211 	/*
5212 	 * Indexes are stored in the phyint - a common structure
5213 	 * to both IPv4 and IPv6.
5214 	 */
5215 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5216 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5217 	    (void *) &index, NULL);
5218 	if (phyi != NULL) {
5219 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5220 		if (ill != NULL) {
5221 			/*
5222 			 * The block comment at the start of ipif_down
5223 			 * explains the use of the macros used below
5224 			 */
5225 			GRAB_CONN_LOCK(q);
5226 			mutex_enter(&ill->ill_lock);
5227 			if (ILL_CAN_LOOKUP(ill)) {
5228 				ill_refhold_locked(ill);
5229 				mutex_exit(&ill->ill_lock);
5230 				RELEASE_CONN_LOCK(q);
5231 				rw_exit(&ipst->ips_ill_g_lock);
5232 				return (ill);
5233 			} else if (ILL_CAN_WAIT(ill, q)) {
5234 				ipsq = ill->ill_phyint->phyint_ipsq;
5235 				mutex_enter(&ipsq->ipsq_lock);
5236 				rw_exit(&ipst->ips_ill_g_lock);
5237 				mutex_exit(&ill->ill_lock);
5238 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5239 				mutex_exit(&ipsq->ipsq_lock);
5240 				RELEASE_CONN_LOCK(q);
5241 				if (err != NULL)
5242 					*err = EINPROGRESS;
5243 				return (NULL);
5244 			}
5245 			RELEASE_CONN_LOCK(q);
5246 			mutex_exit(&ill->ill_lock);
5247 		}
5248 	}
5249 	rw_exit(&ipst->ips_ill_g_lock);
5250 	if (err != NULL)
5251 		*err = ENXIO;
5252 	return (NULL);
5253 }
5254 
5255 /*
5256  * Return the ifindex next in sequence after the passed in ifindex.
5257  * If there is no next ifindex for the given protocol, return 0.
5258  */
5259 uint_t
5260 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5261 {
5262 	phyint_t *phyi;
5263 	phyint_t *phyi_initial;
5264 	uint_t   ifindex;
5265 
5266 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5267 
5268 	if (index == 0) {
5269 		phyi = avl_first(
5270 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5271 	} else {
5272 		phyi = phyi_initial = avl_find(
5273 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5274 		    (void *) &index, NULL);
5275 	}
5276 
5277 	for (; phyi != NULL;
5278 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5279 	    phyi, AVL_AFTER)) {
5280 		/*
5281 		 * If we're not returning the first interface in the tree
5282 		 * and we still haven't moved past the phyint_t that
5283 		 * corresponds to index, avl_walk needs to be called again
5284 		 */
5285 		if (!((index != 0) && (phyi == phyi_initial))) {
5286 			if (isv6) {
5287 				if ((phyi->phyint_illv6) &&
5288 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5289 				    (phyi->phyint_illv6->ill_isv6 == 1))
5290 					break;
5291 			} else {
5292 				if ((phyi->phyint_illv4) &&
5293 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5294 				    (phyi->phyint_illv4->ill_isv6 == 0))
5295 					break;
5296 			}
5297 		}
5298 	}
5299 
5300 	rw_exit(&ipst->ips_ill_g_lock);
5301 
5302 	if (phyi != NULL)
5303 		ifindex = phyi->phyint_ifindex;
5304 	else
5305 		ifindex = 0;
5306 
5307 	return (ifindex);
5308 }
5309 
5310 
5311 /*
5312  * Return the ifindex for the named interface.
5313  * If there is no next ifindex for the interface, return 0.
5314  */
5315 uint_t
5316 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5317 {
5318 	phyint_t	*phyi;
5319 	avl_index_t	where = 0;
5320 	uint_t		ifindex;
5321 
5322 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5323 
5324 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5325 	    name, &where)) == NULL) {
5326 		rw_exit(&ipst->ips_ill_g_lock);
5327 		return (0);
5328 	}
5329 
5330 	ifindex = phyi->phyint_ifindex;
5331 
5332 	rw_exit(&ipst->ips_ill_g_lock);
5333 
5334 	return (ifindex);
5335 }
5336 
5337 
5338 /*
5339  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5340  * that gives a running thread a reference to the ill. This reference must be
5341  * released by the thread when it is done accessing the ill and related
5342  * objects. ill_refcnt can not be used to account for static references
5343  * such as other structures pointing to an ill. Callers must generally
5344  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5345  * or be sure that the ill is not being deleted or changing state before
5346  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5347  * ill won't change any of its critical state such as address, netmask etc.
5348  */
5349 void
5350 ill_refhold(ill_t *ill)
5351 {
5352 	mutex_enter(&ill->ill_lock);
5353 	ill->ill_refcnt++;
5354 	ILL_TRACE_REF(ill);
5355 	mutex_exit(&ill->ill_lock);
5356 }
5357 
5358 void
5359 ill_refhold_locked(ill_t *ill)
5360 {
5361 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5362 	ill->ill_refcnt++;
5363 	ILL_TRACE_REF(ill);
5364 }
5365 
5366 int
5367 ill_check_and_refhold(ill_t *ill)
5368 {
5369 	mutex_enter(&ill->ill_lock);
5370 	if (ILL_CAN_LOOKUP(ill)) {
5371 		ill_refhold_locked(ill);
5372 		mutex_exit(&ill->ill_lock);
5373 		return (0);
5374 	}
5375 	mutex_exit(&ill->ill_lock);
5376 	return (ILL_LOOKUP_FAILED);
5377 }
5378 
5379 /*
5380  * Must not be called while holding any locks. Otherwise if this is
5381  * the last reference to be released, there is a chance of recursive mutex
5382  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5383  * to restart an ioctl.
5384  */
5385 void
5386 ill_refrele(ill_t *ill)
5387 {
5388 	mutex_enter(&ill->ill_lock);
5389 	ASSERT(ill->ill_refcnt != 0);
5390 	ill->ill_refcnt--;
5391 	ILL_UNTRACE_REF(ill);
5392 	if (ill->ill_refcnt != 0) {
5393 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5394 		mutex_exit(&ill->ill_lock);
5395 		return;
5396 	}
5397 
5398 	/* Drops the ill_lock */
5399 	ipif_ill_refrele_tail(ill);
5400 }
5401 
5402 /*
5403  * Obtain a weak reference count on the ill. This reference ensures the
5404  * ill won't be freed, but the ill may change any of its critical state
5405  * such as netmask, address etc. Returns an error if the ill has started
5406  * closing.
5407  */
5408 boolean_t
5409 ill_waiter_inc(ill_t *ill)
5410 {
5411 	mutex_enter(&ill->ill_lock);
5412 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5413 		mutex_exit(&ill->ill_lock);
5414 		return (B_FALSE);
5415 	}
5416 	ill->ill_waiters++;
5417 	mutex_exit(&ill->ill_lock);
5418 	return (B_TRUE);
5419 }
5420 
5421 void
5422 ill_waiter_dcr(ill_t *ill)
5423 {
5424 	mutex_enter(&ill->ill_lock);
5425 	ill->ill_waiters--;
5426 	if (ill->ill_waiters == 0)
5427 		cv_broadcast(&ill->ill_cv);
5428 	mutex_exit(&ill->ill_lock);
5429 }
5430 
5431 /*
5432  * Named Dispatch routine to produce a formatted report on all ILLs.
5433  * This report is accessed by using the ndd utility to "get" ND variable
5434  * "ip_ill_status".
5435  */
5436 /* ARGSUSED */
5437 int
5438 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5439 {
5440 	ill_t		*ill;
5441 	ill_walk_context_t ctx;
5442 	ip_stack_t	*ipst;
5443 
5444 	ipst = CONNQ_TO_IPST(q);
5445 
5446 	(void) mi_mpprintf(mp,
5447 	    "ILL      " MI_COL_HDRPAD_STR
5448 	/*   01234567[89ABCDEF] */
5449 	    "rq       " MI_COL_HDRPAD_STR
5450 	/*   01234567[89ABCDEF] */
5451 	    "wq       " MI_COL_HDRPAD_STR
5452 	/*   01234567[89ABCDEF] */
5453 	    "upcnt mxfrg err name");
5454 	/*   12345 12345 123 xxxxxxxx  */
5455 
5456 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5457 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5458 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5459 		(void) mi_mpprintf(mp,
5460 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5461 		    "%05u %05u %03d %s",
5462 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5463 		    ill->ill_ipif_up_count,
5464 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5465 	}
5466 	rw_exit(&ipst->ips_ill_g_lock);
5467 
5468 	return (0);
5469 }
5470 
5471 /*
5472  * Named Dispatch routine to produce a formatted report on all IPIFs.
5473  * This report is accessed by using the ndd utility to "get" ND variable
5474  * "ip_ipif_status".
5475  */
5476 /* ARGSUSED */
5477 int
5478 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5479 {
5480 	char	buf1[INET6_ADDRSTRLEN];
5481 	char	buf2[INET6_ADDRSTRLEN];
5482 	char	buf3[INET6_ADDRSTRLEN];
5483 	char	buf4[INET6_ADDRSTRLEN];
5484 	char	buf5[INET6_ADDRSTRLEN];
5485 	char	buf6[INET6_ADDRSTRLEN];
5486 	char	buf[LIFNAMSIZ];
5487 	ill_t	*ill;
5488 	ipif_t	*ipif;
5489 	nv_t	*nvp;
5490 	uint64_t flags;
5491 	zoneid_t zoneid;
5492 	ill_walk_context_t ctx;
5493 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5494 
5495 	(void) mi_mpprintf(mp,
5496 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5497 	    "\tlocal address\n"
5498 	    "\tsrc address\n"
5499 	    "\tsubnet\n"
5500 	    "\tmask\n"
5501 	    "\tbroadcast\n"
5502 	    "\tp-p-dst");
5503 
5504 	ASSERT(q->q_next == NULL);
5505 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5506 
5507 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5508 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5509 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5510 		for (ipif = ill->ill_ipif; ipif != NULL;
5511 		    ipif = ipif->ipif_next) {
5512 			if (zoneid != GLOBAL_ZONEID &&
5513 			    zoneid != ipif->ipif_zoneid &&
5514 			    ipif->ipif_zoneid != ALL_ZONES)
5515 				continue;
5516 
5517 			ipif_get_name(ipif, buf, sizeof (buf));
5518 			(void) mi_mpprintf(mp,
5519 			    MI_COL_PTRFMT_STR
5520 			    "%04u %05u %u/%u/%u %s %d",
5521 			    (void *)ipif,
5522 			    ipif->ipif_metric, ipif->ipif_mtu,
5523 			    ipif->ipif_ib_pkt_count,
5524 			    ipif->ipif_ob_pkt_count,
5525 			    ipif->ipif_fo_pkt_count,
5526 			    buf,
5527 			    ipif->ipif_zoneid);
5528 
5529 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5530 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5531 
5532 		/* Tack on text strings for any flags. */
5533 		nvp = ipif_nv_tbl;
5534 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5535 			if (nvp->nv_value & flags)
5536 				(void) mi_mpprintf_nr(mp, " %s",
5537 				    nvp->nv_name);
5538 		}
5539 		(void) mi_mpprintf(mp,
5540 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5541 		    inet_ntop(AF_INET6,
5542 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5543 		    inet_ntop(AF_INET6,
5544 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5545 		    inet_ntop(AF_INET6,
5546 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5547 		    inet_ntop(AF_INET6,
5548 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5549 		    inet_ntop(AF_INET6,
5550 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5551 		    inet_ntop(AF_INET6,
5552 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5553 		}
5554 	}
5555 	rw_exit(&ipst->ips_ill_g_lock);
5556 	return (0);
5557 }
5558 
5559 /*
5560  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5561  * driver.  We construct best guess defaults for lower level information that
5562  * we need.  If an interface is brought up without injection of any overriding
5563  * information from outside, we have to be ready to go with these defaults.
5564  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5565  * we primarely want the dl_provider_style.
5566  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5567  * at which point we assume the other part of the information is valid.
5568  */
5569 void
5570 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5571 {
5572 	uchar_t		*brdcst_addr;
5573 	uint_t		brdcst_addr_length, phys_addr_length;
5574 	t_scalar_t	sap_length;
5575 	dl_info_ack_t	*dlia;
5576 	ip_m_t		*ipm;
5577 	dl_qos_cl_sel1_t *sel1;
5578 
5579 	ASSERT(IAM_WRITER_ILL(ill));
5580 
5581 	/*
5582 	 * Till the ill is fully up ILL_CHANGING will be set and
5583 	 * the ill is not globally visible. So no need for a lock.
5584 	 */
5585 	dlia = (dl_info_ack_t *)mp->b_rptr;
5586 	ill->ill_mactype = dlia->dl_mac_type;
5587 
5588 	ipm = ip_m_lookup(dlia->dl_mac_type);
5589 	if (ipm == NULL) {
5590 		ipm = ip_m_lookup(DL_OTHER);
5591 		ASSERT(ipm != NULL);
5592 	}
5593 	ill->ill_media = ipm;
5594 
5595 	/*
5596 	 * When the new DLPI stuff is ready we'll pull lengths
5597 	 * from dlia.
5598 	 */
5599 	if (dlia->dl_version == DL_VERSION_2) {
5600 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5601 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5602 		    brdcst_addr_length);
5603 		if (brdcst_addr == NULL) {
5604 			brdcst_addr_length = 0;
5605 		}
5606 		sap_length = dlia->dl_sap_length;
5607 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5608 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5609 		    brdcst_addr_length, sap_length, phys_addr_length));
5610 	} else {
5611 		brdcst_addr_length = 6;
5612 		brdcst_addr = ip_six_byte_all_ones;
5613 		sap_length = -2;
5614 		phys_addr_length = brdcst_addr_length;
5615 	}
5616 
5617 	ill->ill_bcast_addr_length = brdcst_addr_length;
5618 	ill->ill_phys_addr_length = phys_addr_length;
5619 	ill->ill_sap_length = sap_length;
5620 	ill->ill_max_frag = dlia->dl_max_sdu;
5621 	ill->ill_max_mtu = ill->ill_max_frag;
5622 
5623 	ill->ill_type = ipm->ip_m_type;
5624 
5625 	if (!ill->ill_dlpi_style_set) {
5626 		if (dlia->dl_provider_style == DL_STYLE2)
5627 			ill->ill_needs_attach = 1;
5628 
5629 		/*
5630 		 * Allocate the first ipif on this ill. We don't delay it
5631 		 * further as ioctl handling assumes atleast one ipif to
5632 		 * be present.
5633 		 *
5634 		 * At this point we don't know whether the ill is v4 or v6.
5635 		 * We will know this whan the SIOCSLIFNAME happens and
5636 		 * the correct value for ill_isv6 will be assigned in
5637 		 * ipif_set_values(). We need to hold the ill lock and
5638 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5639 		 * the wakeup.
5640 		 */
5641 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5642 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5643 		mutex_enter(&ill->ill_lock);
5644 		ASSERT(ill->ill_dlpi_style_set == 0);
5645 		ill->ill_dlpi_style_set = 1;
5646 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5647 		cv_broadcast(&ill->ill_cv);
5648 		mutex_exit(&ill->ill_lock);
5649 		freemsg(mp);
5650 		return;
5651 	}
5652 	ASSERT(ill->ill_ipif != NULL);
5653 	/*
5654 	 * We know whether it is IPv4 or IPv6 now, as this is the
5655 	 * second DL_INFO_ACK we are recieving in response to the
5656 	 * DL_INFO_REQ sent in ipif_set_values.
5657 	 */
5658 	if (ill->ill_isv6)
5659 		ill->ill_sap = IP6_DL_SAP;
5660 	else
5661 		ill->ill_sap = IP_DL_SAP;
5662 	/*
5663 	 * Set ipif_mtu which is used to set the IRE's
5664 	 * ire_max_frag value. The driver could have sent
5665 	 * a different mtu from what it sent last time. No
5666 	 * need to call ipif_mtu_change because IREs have
5667 	 * not yet been created.
5668 	 */
5669 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5670 	/*
5671 	 * Clear all the flags that were set based on ill_bcast_addr_length
5672 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5673 	 * changed now and we need to re-evaluate.
5674 	 */
5675 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5676 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5677 
5678 	/*
5679 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5680 	 * changed now.
5681 	 */
5682 	if (ill->ill_bcast_addr_length == 0) {
5683 		if (ill->ill_resolver_mp != NULL)
5684 			freemsg(ill->ill_resolver_mp);
5685 		if (ill->ill_bcast_mp != NULL)
5686 			freemsg(ill->ill_bcast_mp);
5687 		if (ill->ill_flags & ILLF_XRESOLV)
5688 			ill->ill_net_type = IRE_IF_RESOLVER;
5689 		else
5690 			ill->ill_net_type = IRE_IF_NORESOLVER;
5691 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5692 		    ill->ill_phys_addr_length,
5693 		    ill->ill_sap,
5694 		    ill->ill_sap_length);
5695 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5696 
5697 		if (ill->ill_isv6)
5698 			/*
5699 			 * Note: xresolv interfaces will eventually need NOARP
5700 			 * set here as well, but that will require those
5701 			 * external resolvers to have some knowledge of
5702 			 * that flag and act appropriately. Not to be changed
5703 			 * at present.
5704 			 */
5705 			ill->ill_flags |= ILLF_NONUD;
5706 		else
5707 			ill->ill_flags |= ILLF_NOARP;
5708 
5709 		if (ill->ill_phys_addr_length == 0) {
5710 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5711 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5712 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5713 			} else {
5714 				/* pt-pt supports multicast. */
5715 				ill->ill_flags |= ILLF_MULTICAST;
5716 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5717 			}
5718 		}
5719 	} else {
5720 		ill->ill_net_type = IRE_IF_RESOLVER;
5721 		if (ill->ill_bcast_mp != NULL)
5722 			freemsg(ill->ill_bcast_mp);
5723 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5724 		    ill->ill_bcast_addr_length, ill->ill_sap,
5725 		    ill->ill_sap_length);
5726 		/*
5727 		 * Later detect lack of DLPI driver multicast
5728 		 * capability by catching DL_ENABMULTI errors in
5729 		 * ip_rput_dlpi.
5730 		 */
5731 		ill->ill_flags |= ILLF_MULTICAST;
5732 		if (!ill->ill_isv6)
5733 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5734 	}
5735 	/* By default an interface does not support any CoS marking */
5736 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5737 
5738 	/*
5739 	 * If we get QoS information in DL_INFO_ACK, the device supports
5740 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5741 	 */
5742 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5743 	    dlia->dl_qos_length);
5744 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5745 		ill->ill_flags |= ILLF_COS_ENABLED;
5746 	}
5747 
5748 	/* Clear any previous error indication. */
5749 	ill->ill_error = 0;
5750 	freemsg(mp);
5751 }
5752 
5753 /*
5754  * Perform various checks to verify that an address would make sense as a
5755  * local, remote, or subnet interface address.
5756  */
5757 static boolean_t
5758 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5759 {
5760 	ipaddr_t	net_mask;
5761 
5762 	/*
5763 	 * Don't allow all zeroes, all ones or experimental address, but allow
5764 	 * all ones netmask.
5765 	 */
5766 	if ((net_mask = ip_net_mask(addr)) == 0)
5767 		return (B_FALSE);
5768 	/* A given netmask overrides the "guess" netmask */
5769 	if (subnet_mask != 0)
5770 		net_mask = subnet_mask;
5771 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5772 	    (addr == (addr | ~net_mask)))) {
5773 		return (B_FALSE);
5774 	}
5775 	if (CLASSD(addr))
5776 		return (B_FALSE);
5777 
5778 	return (B_TRUE);
5779 }
5780 
5781 #define	V6_IPIF_LINKLOCAL(p)	\
5782 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5783 
5784 /*
5785  * Compare two given ipifs and check if the second one is better than
5786  * the first one using the order of preference (not taking deprecated
5787  * into acount) specified in ipif_lookup_multicast().
5788  */
5789 static boolean_t
5790 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5791 {
5792 	/* Check the least preferred first. */
5793 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5794 		/* If both ipifs are the same, use the first one. */
5795 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5796 			return (B_FALSE);
5797 		else
5798 			return (B_TRUE);
5799 	}
5800 
5801 	/* For IPv6, check for link local address. */
5802 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5803 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5804 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5805 			/* The second one is equal or less preferred. */
5806 			return (B_FALSE);
5807 		} else {
5808 			return (B_TRUE);
5809 		}
5810 	}
5811 
5812 	/* Then check for point to point interface. */
5813 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5814 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5815 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5816 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5817 			return (B_FALSE);
5818 		} else {
5819 			return (B_TRUE);
5820 		}
5821 	}
5822 
5823 	/* old_ipif is a normal interface, so no need to use the new one. */
5824 	return (B_FALSE);
5825 }
5826 
5827 /*
5828  * Find any non-virtual, not condemned, and up multicast capable interface
5829  * given an IP instance and zoneid.  Order of preference is:
5830  *
5831  * 1. normal
5832  * 1.1 normal, but deprecated
5833  * 2. point to point
5834  * 2.1 point to point, but deprecated
5835  * 3. link local
5836  * 3.1 link local, but deprecated
5837  * 4. loopback.
5838  */
5839 ipif_t *
5840 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5841 {
5842 	ill_t			*ill;
5843 	ill_walk_context_t	ctx;
5844 	ipif_t			*ipif;
5845 	ipif_t			*saved_ipif = NULL;
5846 	ipif_t			*dep_ipif = NULL;
5847 
5848 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5849 	if (isv6)
5850 		ill = ILL_START_WALK_V6(&ctx, ipst);
5851 	else
5852 		ill = ILL_START_WALK_V4(&ctx, ipst);
5853 
5854 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5855 		mutex_enter(&ill->ill_lock);
5856 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5857 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5858 			mutex_exit(&ill->ill_lock);
5859 			continue;
5860 		}
5861 		for (ipif = ill->ill_ipif; ipif != NULL;
5862 		    ipif = ipif->ipif_next) {
5863 			if (zoneid != ipif->ipif_zoneid &&
5864 			    zoneid != ALL_ZONES &&
5865 			    ipif->ipif_zoneid != ALL_ZONES) {
5866 				continue;
5867 			}
5868 			if (!(ipif->ipif_flags & IPIF_UP) ||
5869 			    !IPIF_CAN_LOOKUP(ipif)) {
5870 				continue;
5871 			}
5872 
5873 			/*
5874 			 * Found one candidate.  If it is deprecated,
5875 			 * remember it in dep_ipif.  If it is not deprecated,
5876 			 * remember it in saved_ipif.
5877 			 */
5878 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5879 				if (dep_ipif == NULL) {
5880 					dep_ipif = ipif;
5881 				} else if (ipif_comp_multi(dep_ipif, ipif,
5882 				    isv6)) {
5883 					/*
5884 					 * If the previous dep_ipif does not
5885 					 * belong to the same ill, we've done
5886 					 * a ipif_refhold() on it.  So we need
5887 					 * to release it.
5888 					 */
5889 					if (dep_ipif->ipif_ill != ill)
5890 						ipif_refrele(dep_ipif);
5891 					dep_ipif = ipif;
5892 				}
5893 				continue;
5894 			}
5895 			if (saved_ipif == NULL) {
5896 				saved_ipif = ipif;
5897 			} else {
5898 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5899 					if (saved_ipif->ipif_ill != ill)
5900 						ipif_refrele(saved_ipif);
5901 					saved_ipif = ipif;
5902 				}
5903 			}
5904 		}
5905 		/*
5906 		 * Before going to the next ill, do a ipif_refhold() on the
5907 		 * saved ones.
5908 		 */
5909 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5910 			ipif_refhold_locked(saved_ipif);
5911 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5912 			ipif_refhold_locked(dep_ipif);
5913 		mutex_exit(&ill->ill_lock);
5914 	}
5915 	rw_exit(&ipst->ips_ill_g_lock);
5916 
5917 	/*
5918 	 * If we have only the saved_ipif, return it.  But if we have both
5919 	 * saved_ipif and dep_ipif, check to see which one is better.
5920 	 */
5921 	if (saved_ipif != NULL) {
5922 		if (dep_ipif != NULL) {
5923 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5924 				ipif_refrele(saved_ipif);
5925 				return (dep_ipif);
5926 			} else {
5927 				ipif_refrele(dep_ipif);
5928 				return (saved_ipif);
5929 			}
5930 		}
5931 		return (saved_ipif);
5932 	} else {
5933 		return (dep_ipif);
5934 	}
5935 }
5936 
5937 /*
5938  * This function is called when an application does not specify an interface
5939  * to be used for multicast traffic (joining a group/sending data).  It
5940  * calls ire_lookup_multi() to look for an interface route for the
5941  * specified multicast group.  Doing this allows the administrator to add
5942  * prefix routes for multicast to indicate which interface to be used for
5943  * multicast traffic in the above scenario.  The route could be for all
5944  * multicast (224.0/4), for a single multicast group (a /32 route) or
5945  * anything in between.  If there is no such multicast route, we just find
5946  * any multicast capable interface and return it.  The returned ipif
5947  * is refhold'ed.
5948  */
5949 ipif_t *
5950 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5951 {
5952 	ire_t			*ire;
5953 	ipif_t			*ipif;
5954 
5955 	ire = ire_lookup_multi(group, zoneid, ipst);
5956 	if (ire != NULL) {
5957 		ipif = ire->ire_ipif;
5958 		ipif_refhold(ipif);
5959 		ire_refrele(ire);
5960 		return (ipif);
5961 	}
5962 
5963 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5964 }
5965 
5966 /*
5967  * Look for an ipif with the specified interface address and destination.
5968  * The destination address is used only for matching point-to-point interfaces.
5969  */
5970 ipif_t *
5971 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5972     ipsq_func_t func, int *error, ip_stack_t *ipst)
5973 {
5974 	ipif_t	*ipif;
5975 	ill_t	*ill;
5976 	ill_walk_context_t ctx;
5977 	ipsq_t	*ipsq;
5978 
5979 	if (error != NULL)
5980 		*error = 0;
5981 
5982 	/*
5983 	 * First match all the point-to-point interfaces
5984 	 * before looking at non-point-to-point interfaces.
5985 	 * This is done to avoid returning non-point-to-point
5986 	 * ipif instead of unnumbered point-to-point ipif.
5987 	 */
5988 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5989 	ill = ILL_START_WALK_V4(&ctx, ipst);
5990 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5991 		GRAB_CONN_LOCK(q);
5992 		mutex_enter(&ill->ill_lock);
5993 		for (ipif = ill->ill_ipif; ipif != NULL;
5994 		    ipif = ipif->ipif_next) {
5995 			/* Allow the ipif to be down */
5996 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5997 			    (ipif->ipif_lcl_addr == if_addr) &&
5998 			    (ipif->ipif_pp_dst_addr == dst)) {
5999 				/*
6000 				 * The block comment at the start of ipif_down
6001 				 * explains the use of the macros used below
6002 				 */
6003 				if (IPIF_CAN_LOOKUP(ipif)) {
6004 					ipif_refhold_locked(ipif);
6005 					mutex_exit(&ill->ill_lock);
6006 					RELEASE_CONN_LOCK(q);
6007 					rw_exit(&ipst->ips_ill_g_lock);
6008 					return (ipif);
6009 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6010 					ipsq = ill->ill_phyint->phyint_ipsq;
6011 					mutex_enter(&ipsq->ipsq_lock);
6012 					mutex_exit(&ill->ill_lock);
6013 					rw_exit(&ipst->ips_ill_g_lock);
6014 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6015 					    ill);
6016 					mutex_exit(&ipsq->ipsq_lock);
6017 					RELEASE_CONN_LOCK(q);
6018 					if (error != NULL)
6019 						*error = EINPROGRESS;
6020 					return (NULL);
6021 				}
6022 			}
6023 		}
6024 		mutex_exit(&ill->ill_lock);
6025 		RELEASE_CONN_LOCK(q);
6026 	}
6027 	rw_exit(&ipst->ips_ill_g_lock);
6028 
6029 	/* lookup the ipif based on interface address */
6030 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6031 	    ipst);
6032 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6033 	return (ipif);
6034 }
6035 
6036 /*
6037  * Look for an ipif with the specified address. For point-point links
6038  * we look for matches on either the destination address and the local
6039  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6040  * is set.
6041  * Matches on a specific ill if match_ill is set.
6042  */
6043 ipif_t *
6044 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6045     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6046 {
6047 	ipif_t  *ipif;
6048 	ill_t   *ill;
6049 	boolean_t ptp = B_FALSE;
6050 	ipsq_t	*ipsq;
6051 	ill_walk_context_t	ctx;
6052 
6053 	if (error != NULL)
6054 		*error = 0;
6055 
6056 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6057 	/*
6058 	 * Repeat twice, first based on local addresses and
6059 	 * next time for pointopoint.
6060 	 */
6061 repeat:
6062 	ill = ILL_START_WALK_V4(&ctx, ipst);
6063 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6064 		if (match_ill != NULL && ill != match_ill) {
6065 			continue;
6066 		}
6067 		GRAB_CONN_LOCK(q);
6068 		mutex_enter(&ill->ill_lock);
6069 		for (ipif = ill->ill_ipif; ipif != NULL;
6070 		    ipif = ipif->ipif_next) {
6071 			if (zoneid != ALL_ZONES &&
6072 			    zoneid != ipif->ipif_zoneid &&
6073 			    ipif->ipif_zoneid != ALL_ZONES)
6074 				continue;
6075 			/* Allow the ipif to be down */
6076 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6077 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6078 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6079 			    (ipif->ipif_pp_dst_addr == addr))) {
6080 				/*
6081 				 * The block comment at the start of ipif_down
6082 				 * explains the use of the macros used below
6083 				 */
6084 				if (IPIF_CAN_LOOKUP(ipif)) {
6085 					ipif_refhold_locked(ipif);
6086 					mutex_exit(&ill->ill_lock);
6087 					RELEASE_CONN_LOCK(q);
6088 					rw_exit(&ipst->ips_ill_g_lock);
6089 					return (ipif);
6090 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6091 					ipsq = ill->ill_phyint->phyint_ipsq;
6092 					mutex_enter(&ipsq->ipsq_lock);
6093 					mutex_exit(&ill->ill_lock);
6094 					rw_exit(&ipst->ips_ill_g_lock);
6095 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6096 					    ill);
6097 					mutex_exit(&ipsq->ipsq_lock);
6098 					RELEASE_CONN_LOCK(q);
6099 					if (error != NULL)
6100 						*error = EINPROGRESS;
6101 					return (NULL);
6102 				}
6103 			}
6104 		}
6105 		mutex_exit(&ill->ill_lock);
6106 		RELEASE_CONN_LOCK(q);
6107 	}
6108 
6109 	/* If we already did the ptp case, then we are done */
6110 	if (ptp) {
6111 		rw_exit(&ipst->ips_ill_g_lock);
6112 		if (error != NULL)
6113 			*error = ENXIO;
6114 		return (NULL);
6115 	}
6116 	ptp = B_TRUE;
6117 	goto repeat;
6118 }
6119 
6120 /*
6121  * Look for an ipif with the specified address. For point-point links
6122  * we look for matches on either the destination address and the local
6123  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6124  * is set.
6125  * Matches on a specific ill if match_ill is set.
6126  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6127  */
6128 zoneid_t
6129 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6130 {
6131 	zoneid_t zoneid;
6132 	ipif_t  *ipif;
6133 	ill_t   *ill;
6134 	boolean_t ptp = B_FALSE;
6135 	ill_walk_context_t	ctx;
6136 
6137 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6138 	/*
6139 	 * Repeat twice, first based on local addresses and
6140 	 * next time for pointopoint.
6141 	 */
6142 repeat:
6143 	ill = ILL_START_WALK_V4(&ctx, ipst);
6144 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6145 		if (match_ill != NULL && ill != match_ill) {
6146 			continue;
6147 		}
6148 		mutex_enter(&ill->ill_lock);
6149 		for (ipif = ill->ill_ipif; ipif != NULL;
6150 		    ipif = ipif->ipif_next) {
6151 			/* Allow the ipif to be down */
6152 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6153 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6154 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6155 			    (ipif->ipif_pp_dst_addr == addr)) &&
6156 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6157 				zoneid = ipif->ipif_zoneid;
6158 				mutex_exit(&ill->ill_lock);
6159 				rw_exit(&ipst->ips_ill_g_lock);
6160 				/*
6161 				 * If ipif_zoneid was ALL_ZONES then we have
6162 				 * a trusted extensions shared IP address.
6163 				 * In that case GLOBAL_ZONEID works to send.
6164 				 */
6165 				if (zoneid == ALL_ZONES)
6166 					zoneid = GLOBAL_ZONEID;
6167 				return (zoneid);
6168 			}
6169 		}
6170 		mutex_exit(&ill->ill_lock);
6171 	}
6172 
6173 	/* If we already did the ptp case, then we are done */
6174 	if (ptp) {
6175 		rw_exit(&ipst->ips_ill_g_lock);
6176 		return (ALL_ZONES);
6177 	}
6178 	ptp = B_TRUE;
6179 	goto repeat;
6180 }
6181 
6182 /*
6183  * Look for an ipif that matches the specified remote address i.e. the
6184  * ipif that would receive the specified packet.
6185  * First look for directly connected interfaces and then do a recursive
6186  * IRE lookup and pick the first ipif corresponding to the source address in the
6187  * ire.
6188  * Returns: held ipif
6189  */
6190 ipif_t *
6191 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6192 {
6193 	ipif_t	*ipif;
6194 	ire_t	*ire;
6195 	ip_stack_t	*ipst = ill->ill_ipst;
6196 
6197 	ASSERT(!ill->ill_isv6);
6198 
6199 	/*
6200 	 * Someone could be changing this ipif currently or change it
6201 	 * after we return this. Thus  a few packets could use the old
6202 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6203 	 * will atomically be updated or cleaned up with the new value
6204 	 * Thus we don't need a lock to check the flags or other attrs below.
6205 	 */
6206 	mutex_enter(&ill->ill_lock);
6207 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6208 		if (!IPIF_CAN_LOOKUP(ipif))
6209 			continue;
6210 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6211 		    ipif->ipif_zoneid != ALL_ZONES)
6212 			continue;
6213 		/* Allow the ipif to be down */
6214 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6215 			if ((ipif->ipif_pp_dst_addr == addr) ||
6216 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6217 			    ipif->ipif_lcl_addr == addr)) {
6218 				ipif_refhold_locked(ipif);
6219 				mutex_exit(&ill->ill_lock);
6220 				return (ipif);
6221 			}
6222 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6223 			ipif_refhold_locked(ipif);
6224 			mutex_exit(&ill->ill_lock);
6225 			return (ipif);
6226 		}
6227 	}
6228 	mutex_exit(&ill->ill_lock);
6229 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6230 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6231 	if (ire != NULL) {
6232 		/*
6233 		 * The callers of this function wants to know the
6234 		 * interface on which they have to send the replies
6235 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6236 		 * derived from different ills, we really don't care
6237 		 * what we return here.
6238 		 */
6239 		ipif = ire->ire_ipif;
6240 		if (ipif != NULL) {
6241 			ipif_refhold(ipif);
6242 			ire_refrele(ire);
6243 			return (ipif);
6244 		}
6245 		ire_refrele(ire);
6246 	}
6247 	/* Pick the first interface */
6248 	ipif = ipif_get_next_ipif(NULL, ill);
6249 	return (ipif);
6250 }
6251 
6252 /*
6253  * This func does not prevent refcnt from increasing. But if
6254  * the caller has taken steps to that effect, then this func
6255  * can be used to determine whether the ill has become quiescent
6256  */
6257 boolean_t
6258 ill_is_quiescent(ill_t *ill)
6259 {
6260 	ipif_t	*ipif;
6261 
6262 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6263 
6264 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6265 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6266 			return (B_FALSE);
6267 		}
6268 	}
6269 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6270 	    ill->ill_nce_cnt != 0) {
6271 		return (B_FALSE);
6272 	}
6273 	return (B_TRUE);
6274 }
6275 
6276 /*
6277  * This func does not prevent refcnt from increasing. But if
6278  * the caller has taken steps to that effect, then this func
6279  * can be used to determine whether the ipif has become quiescent
6280  */
6281 static boolean_t
6282 ipif_is_quiescent(ipif_t *ipif)
6283 {
6284 	ill_t *ill;
6285 
6286 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6287 
6288 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6289 		return (B_FALSE);
6290 	}
6291 
6292 	ill = ipif->ipif_ill;
6293 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6294 	    ill->ill_logical_down) {
6295 		return (B_TRUE);
6296 	}
6297 
6298 	/* This is the last ipif going down or being deleted on this ill */
6299 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6300 		return (B_FALSE);
6301 	}
6302 
6303 	return (B_TRUE);
6304 }
6305 
6306 /*
6307  * This func does not prevent refcnt from increasing. But if
6308  * the caller has taken steps to that effect, then this func
6309  * can be used to determine whether the ipifs marked with IPIF_MOVING
6310  * have become quiescent and can be moved in a failover/failback.
6311  */
6312 static ipif_t *
6313 ill_quiescent_to_move(ill_t *ill)
6314 {
6315 	ipif_t  *ipif;
6316 
6317 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6318 
6319 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6320 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6321 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6322 				return (ipif);
6323 			}
6324 		}
6325 	}
6326 	return (NULL);
6327 }
6328 
6329 /*
6330  * The ipif/ill/ire has been refreled. Do the tail processing.
6331  * Determine if the ipif or ill in question has become quiescent and if so
6332  * wakeup close and/or restart any queued pending ioctl that is waiting
6333  * for the ipif_down (or ill_down)
6334  */
6335 void
6336 ipif_ill_refrele_tail(ill_t *ill)
6337 {
6338 	mblk_t	*mp;
6339 	conn_t	*connp;
6340 	ipsq_t	*ipsq;
6341 	ipif_t	*ipif;
6342 	dl_notify_ind_t *dlindp;
6343 
6344 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6345 
6346 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6347 	    ill_is_quiescent(ill)) {
6348 		/* ill_close may be waiting */
6349 		cv_broadcast(&ill->ill_cv);
6350 	}
6351 
6352 	/* ipsq can't change because ill_lock  is held */
6353 	ipsq = ill->ill_phyint->phyint_ipsq;
6354 	if (ipsq->ipsq_waitfor == 0) {
6355 		/* Not waiting for anything, just return. */
6356 		mutex_exit(&ill->ill_lock);
6357 		return;
6358 	}
6359 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6360 	    ipsq->ipsq_pending_ipif != NULL);
6361 	/*
6362 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6363 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6364 	 * be zero for restarting an ioctl that ends up downing the ill.
6365 	 */
6366 	ipif = ipsq->ipsq_pending_ipif;
6367 	if (ipif->ipif_ill != ill) {
6368 		/* The ioctl is pending on some other ill. */
6369 		mutex_exit(&ill->ill_lock);
6370 		return;
6371 	}
6372 
6373 	switch (ipsq->ipsq_waitfor) {
6374 	case IPIF_DOWN:
6375 	case IPIF_FREE:
6376 		if (!ipif_is_quiescent(ipif)) {
6377 			mutex_exit(&ill->ill_lock);
6378 			return;
6379 		}
6380 		break;
6381 
6382 	case ILL_DOWN:
6383 	case ILL_FREE:
6384 		/*
6385 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6386 		 * waits synchronously in ip_close, and no message is queued in
6387 		 * ipsq_pending_mp at all in this case
6388 		 */
6389 		if (!ill_is_quiescent(ill)) {
6390 			mutex_exit(&ill->ill_lock);
6391 			return;
6392 		}
6393 
6394 		break;
6395 
6396 	case ILL_MOVE_OK:
6397 		if (ill_quiescent_to_move(ill) != NULL) {
6398 			mutex_exit(&ill->ill_lock);
6399 			return;
6400 		}
6401 
6402 		break;
6403 	default:
6404 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6405 		    (void *)ipsq, ipsq->ipsq_waitfor);
6406 	}
6407 
6408 	/*
6409 	 * Incr refcnt for the qwriter_ip call below which
6410 	 * does a refrele
6411 	 */
6412 	ill_refhold_locked(ill);
6413 	mutex_exit(&ill->ill_lock);
6414 
6415 	mp = ipsq_pending_mp_get(ipsq, &connp);
6416 	ASSERT(mp != NULL);
6417 
6418 	/*
6419 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6420 	 * we can only get here when the current operation decides it
6421 	 * it needs to quiesce via ipsq_pending_mp_add().
6422 	 */
6423 	switch (mp->b_datap->db_type) {
6424 	case M_PCPROTO:
6425 	case M_PROTO:
6426 		/*
6427 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6428 		 */
6429 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6430 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6431 
6432 		switch (dlindp->dl_notification) {
6433 		case DL_NOTE_PHYS_ADDR:
6434 			qwriter_ip(ill, ill->ill_rq, mp,
6435 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6436 			return;
6437 		default:
6438 			ASSERT(0);
6439 		}
6440 		break;
6441 
6442 	case M_ERROR:
6443 	case M_HANGUP:
6444 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6445 		    B_TRUE);
6446 		return;
6447 
6448 	case M_IOCTL:
6449 	case M_IOCDATA:
6450 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6451 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6452 		return;
6453 
6454 	default:
6455 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6456 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6457 	}
6458 }
6459 
6460 #ifdef DEBUG
6461 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6462 static void
6463 th_trace_rrecord(th_trace_t *th_trace)
6464 {
6465 	tr_buf_t *tr_buf;
6466 	uint_t lastref;
6467 
6468 	lastref = th_trace->th_trace_lastref;
6469 	lastref++;
6470 	if (lastref == TR_BUF_MAX)
6471 		lastref = 0;
6472 	th_trace->th_trace_lastref = lastref;
6473 	tr_buf = &th_trace->th_trbuf[lastref];
6474 	tr_buf->tr_time = lbolt;
6475 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6476 }
6477 
6478 static void
6479 th_trace_free(void *value)
6480 {
6481 	th_trace_t *th_trace = value;
6482 
6483 	ASSERT(th_trace->th_refcnt == 0);
6484 	kmem_free(th_trace, sizeof (*th_trace));
6485 }
6486 
6487 /*
6488  * Find or create the per-thread hash table used to track object references.
6489  * The ipst argument is NULL if we shouldn't allocate.
6490  *
6491  * Accesses per-thread data, so there's no need to lock here.
6492  */
6493 static mod_hash_t *
6494 th_trace_gethash(ip_stack_t *ipst)
6495 {
6496 	th_hash_t *thh;
6497 
6498 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6499 		mod_hash_t *mh;
6500 		char name[256];
6501 		size_t objsize, rshift;
6502 		int retv;
6503 
6504 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6505 			return (NULL);
6506 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6507 
6508 		/*
6509 		 * We use mod_hash_create_extended here rather than the more
6510 		 * obvious mod_hash_create_ptrhash because the latter has a
6511 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6512 		 * block.
6513 		 */
6514 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6515 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6516 		rshift = highbit(objsize);
6517 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6518 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6519 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6520 		if (mh == NULL) {
6521 			kmem_free(thh, sizeof (*thh));
6522 			return (NULL);
6523 		}
6524 		thh->thh_hash = mh;
6525 		thh->thh_ipst = ipst;
6526 		/*
6527 		 * We trace ills, ipifs, ires, and nces.  All of these are
6528 		 * per-IP-stack, so the lock on the thread list is as well.
6529 		 */
6530 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6531 		list_insert_tail(&ip_thread_list, thh);
6532 		rw_exit(&ip_thread_rwlock);
6533 		retv = tsd_set(ip_thread_data, thh);
6534 		ASSERT(retv == 0);
6535 	}
6536 	return (thh != NULL ? thh->thh_hash : NULL);
6537 }
6538 
6539 boolean_t
6540 th_trace_ref(const void *obj, ip_stack_t *ipst)
6541 {
6542 	th_trace_t *th_trace;
6543 	mod_hash_t *mh;
6544 	mod_hash_val_t val;
6545 
6546 	if ((mh = th_trace_gethash(ipst)) == NULL)
6547 		return (B_FALSE);
6548 
6549 	/*
6550 	 * Attempt to locate the trace buffer for this obj and thread.
6551 	 * If it does not exist, then allocate a new trace buffer and
6552 	 * insert into the hash.
6553 	 */
6554 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6555 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6556 		if (th_trace == NULL)
6557 			return (B_FALSE);
6558 
6559 		th_trace->th_id = curthread;
6560 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6561 		    (mod_hash_val_t)th_trace) != 0) {
6562 			kmem_free(th_trace, sizeof (th_trace_t));
6563 			return (B_FALSE);
6564 		}
6565 	} else {
6566 		th_trace = (th_trace_t *)val;
6567 	}
6568 
6569 	ASSERT(th_trace->th_refcnt >= 0 &&
6570 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6571 
6572 	th_trace->th_refcnt++;
6573 	th_trace_rrecord(th_trace);
6574 	return (B_TRUE);
6575 }
6576 
6577 /*
6578  * For the purpose of tracing a reference release, we assume that global
6579  * tracing is always on and that the same thread initiated the reference hold
6580  * is releasing.
6581  */
6582 void
6583 th_trace_unref(const void *obj)
6584 {
6585 	int retv;
6586 	mod_hash_t *mh;
6587 	th_trace_t *th_trace;
6588 	mod_hash_val_t val;
6589 
6590 	mh = th_trace_gethash(NULL);
6591 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6592 	ASSERT(retv == 0);
6593 	th_trace = (th_trace_t *)val;
6594 
6595 	ASSERT(th_trace->th_refcnt > 0);
6596 	th_trace->th_refcnt--;
6597 	th_trace_rrecord(th_trace);
6598 }
6599 
6600 /*
6601  * If tracing has been disabled, then we assume that the reference counts are
6602  * now useless, and we clear them out before destroying the entries.
6603  */
6604 void
6605 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6606 {
6607 	th_hash_t	*thh;
6608 	mod_hash_t	*mh;
6609 	mod_hash_val_t	val;
6610 	th_trace_t	*th_trace;
6611 	int		retv;
6612 
6613 	rw_enter(&ip_thread_rwlock, RW_READER);
6614 	for (thh = list_head(&ip_thread_list); thh != NULL;
6615 	    thh = list_next(&ip_thread_list, thh)) {
6616 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6617 		    &val) == 0) {
6618 			th_trace = (th_trace_t *)val;
6619 			if (trace_disable)
6620 				th_trace->th_refcnt = 0;
6621 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6622 			ASSERT(retv == 0);
6623 		}
6624 	}
6625 	rw_exit(&ip_thread_rwlock);
6626 }
6627 
6628 void
6629 ipif_trace_ref(ipif_t *ipif)
6630 {
6631 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6632 
6633 	if (ipif->ipif_trace_disable)
6634 		return;
6635 
6636 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6637 		ipif->ipif_trace_disable = B_TRUE;
6638 		ipif_trace_cleanup(ipif);
6639 	}
6640 }
6641 
6642 void
6643 ipif_untrace_ref(ipif_t *ipif)
6644 {
6645 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6646 
6647 	if (!ipif->ipif_trace_disable)
6648 		th_trace_unref(ipif);
6649 }
6650 
6651 void
6652 ill_trace_ref(ill_t *ill)
6653 {
6654 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6655 
6656 	if (ill->ill_trace_disable)
6657 		return;
6658 
6659 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6660 		ill->ill_trace_disable = B_TRUE;
6661 		ill_trace_cleanup(ill);
6662 	}
6663 }
6664 
6665 void
6666 ill_untrace_ref(ill_t *ill)
6667 {
6668 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6669 
6670 	if (!ill->ill_trace_disable)
6671 		th_trace_unref(ill);
6672 }
6673 
6674 /*
6675  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6676  * failure, ipif_trace_disable is set.
6677  */
6678 static void
6679 ipif_trace_cleanup(const ipif_t *ipif)
6680 {
6681 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6682 }
6683 
6684 /*
6685  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6686  * failure, ill_trace_disable is set.
6687  */
6688 static void
6689 ill_trace_cleanup(const ill_t *ill)
6690 {
6691 	th_trace_cleanup(ill, ill->ill_trace_disable);
6692 }
6693 #endif /* DEBUG */
6694 
6695 void
6696 ipif_refhold_locked(ipif_t *ipif)
6697 {
6698 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6699 	ipif->ipif_refcnt++;
6700 	IPIF_TRACE_REF(ipif);
6701 }
6702 
6703 void
6704 ipif_refhold(ipif_t *ipif)
6705 {
6706 	ill_t	*ill;
6707 
6708 	ill = ipif->ipif_ill;
6709 	mutex_enter(&ill->ill_lock);
6710 	ipif->ipif_refcnt++;
6711 	IPIF_TRACE_REF(ipif);
6712 	mutex_exit(&ill->ill_lock);
6713 }
6714 
6715 /*
6716  * Must not be called while holding any locks. Otherwise if this is
6717  * the last reference to be released there is a chance of recursive mutex
6718  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6719  * to restart an ioctl.
6720  */
6721 void
6722 ipif_refrele(ipif_t *ipif)
6723 {
6724 	ill_t	*ill;
6725 
6726 	ill = ipif->ipif_ill;
6727 
6728 	mutex_enter(&ill->ill_lock);
6729 	ASSERT(ipif->ipif_refcnt != 0);
6730 	ipif->ipif_refcnt--;
6731 	IPIF_UNTRACE_REF(ipif);
6732 	if (ipif->ipif_refcnt != 0) {
6733 		mutex_exit(&ill->ill_lock);
6734 		return;
6735 	}
6736 
6737 	/* Drops the ill_lock */
6738 	ipif_ill_refrele_tail(ill);
6739 }
6740 
6741 ipif_t *
6742 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6743 {
6744 	ipif_t	*ipif;
6745 
6746 	mutex_enter(&ill->ill_lock);
6747 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6748 	    ipif != NULL; ipif = ipif->ipif_next) {
6749 		if (!IPIF_CAN_LOOKUP(ipif))
6750 			continue;
6751 		ipif_refhold_locked(ipif);
6752 		mutex_exit(&ill->ill_lock);
6753 		return (ipif);
6754 	}
6755 	mutex_exit(&ill->ill_lock);
6756 	return (NULL);
6757 }
6758 
6759 /*
6760  * TODO: make this table extendible at run time
6761  * Return a pointer to the mac type info for 'mac_type'
6762  */
6763 static ip_m_t *
6764 ip_m_lookup(t_uscalar_t mac_type)
6765 {
6766 	ip_m_t	*ipm;
6767 
6768 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6769 		if (ipm->ip_m_mac_type == mac_type)
6770 			return (ipm);
6771 	return (NULL);
6772 }
6773 
6774 /*
6775  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6776  * ipif_arg is passed in to associate it with the correct interface.
6777  * We may need to restart this operation if the ipif cannot be looked up
6778  * due to an exclusive operation that is currently in progress. The restart
6779  * entry point is specified by 'func'
6780  */
6781 int
6782 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6783     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6784     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6785     struct rtsa_s *sp, ip_stack_t *ipst)
6786 {
6787 	ire_t	*ire;
6788 	ire_t	*gw_ire = NULL;
6789 	ipif_t	*ipif = NULL;
6790 	boolean_t ipif_refheld = B_FALSE;
6791 	uint_t	type;
6792 	int	match_flags = MATCH_IRE_TYPE;
6793 	int	error;
6794 	tsol_gc_t *gc = NULL;
6795 	tsol_gcgrp_t *gcgrp = NULL;
6796 	boolean_t gcgrp_xtraref = B_FALSE;
6797 
6798 	ip1dbg(("ip_rt_add:"));
6799 
6800 	if (ire_arg != NULL)
6801 		*ire_arg = NULL;
6802 
6803 	/*
6804 	 * If this is the case of RTF_HOST being set, then we set the netmask
6805 	 * to all ones (regardless if one was supplied).
6806 	 */
6807 	if (flags & RTF_HOST)
6808 		mask = IP_HOST_MASK;
6809 
6810 	/*
6811 	 * Prevent routes with a zero gateway from being created (since
6812 	 * interfaces can currently be plumbed and brought up no assigned
6813 	 * address).
6814 	 */
6815 	if (gw_addr == 0)
6816 		return (ENETUNREACH);
6817 	/*
6818 	 * Get the ipif, if any, corresponding to the gw_addr
6819 	 */
6820 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6821 	    ipst);
6822 	if (ipif != NULL) {
6823 		if (IS_VNI(ipif->ipif_ill)) {
6824 			ipif_refrele(ipif);
6825 			return (EINVAL);
6826 		}
6827 		ipif_refheld = B_TRUE;
6828 	} else if (error == EINPROGRESS) {
6829 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6830 		return (EINPROGRESS);
6831 	} else {
6832 		error = 0;
6833 	}
6834 
6835 	if (ipif != NULL) {
6836 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6837 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6838 	} else {
6839 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6840 	}
6841 
6842 	/*
6843 	 * GateD will attempt to create routes with a loopback interface
6844 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6845 	 * these routes to be added, but create them as interface routes
6846 	 * since the gateway is an interface address.
6847 	 */
6848 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6849 		flags &= ~RTF_GATEWAY;
6850 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6851 		    mask == IP_HOST_MASK) {
6852 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6853 			    ALL_ZONES, NULL, match_flags, ipst);
6854 			if (ire != NULL) {
6855 				ire_refrele(ire);
6856 				if (ipif_refheld)
6857 					ipif_refrele(ipif);
6858 				return (EEXIST);
6859 			}
6860 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6861 			    "for 0x%x\n", (void *)ipif,
6862 			    ipif->ipif_ire_type,
6863 			    ntohl(ipif->ipif_lcl_addr)));
6864 			ire = ire_create(
6865 			    (uchar_t *)&dst_addr,	/* dest address */
6866 			    (uchar_t *)&mask,		/* mask */
6867 			    (uchar_t *)&ipif->ipif_src_addr,
6868 			    NULL,			/* no gateway */
6869 			    &ipif->ipif_mtu,
6870 			    NULL,
6871 			    ipif->ipif_rq,		/* recv-from queue */
6872 			    NULL,			/* no send-to queue */
6873 			    ipif->ipif_ire_type,	/* LOOPBACK */
6874 			    ipif,
6875 			    0,
6876 			    0,
6877 			    0,
6878 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6879 			    RTF_PRIVATE : 0,
6880 			    &ire_uinfo_null,
6881 			    NULL,
6882 			    NULL,
6883 			    ipst);
6884 
6885 			if (ire == NULL) {
6886 				if (ipif_refheld)
6887 					ipif_refrele(ipif);
6888 				return (ENOMEM);
6889 			}
6890 			error = ire_add(&ire, q, mp, func, B_FALSE);
6891 			if (error == 0)
6892 				goto save_ire;
6893 			if (ipif_refheld)
6894 				ipif_refrele(ipif);
6895 			return (error);
6896 
6897 		}
6898 	}
6899 
6900 	/*
6901 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6902 	 * and the gateway address provided is one of the system's interface
6903 	 * addresses.  By using the routing socket interface and supplying an
6904 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6905 	 * specifying an interface route to be created is available which uses
6906 	 * the interface index that specifies the outgoing interface rather than
6907 	 * the address of an outgoing interface (which may not be able to
6908 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6909 	 * flag, routes can be specified which not only specify the next-hop to
6910 	 * be used when routing to a certain prefix, but also which outgoing
6911 	 * interface should be used.
6912 	 *
6913 	 * Previously, interfaces would have unique addresses assigned to them
6914 	 * and so the address assigned to a particular interface could be used
6915 	 * to identify a particular interface.  One exception to this was the
6916 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6917 	 *
6918 	 * With the advent of IPv6 and its link-local addresses, this
6919 	 * restriction was relaxed and interfaces could share addresses between
6920 	 * themselves.  In fact, typically all of the link-local interfaces on
6921 	 * an IPv6 node or router will have the same link-local address.  In
6922 	 * order to differentiate between these interfaces, the use of an
6923 	 * interface index is necessary and this index can be carried inside a
6924 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6925 	 * of using the interface index, however, is that all of the ipif's that
6926 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6927 	 * cannot be used to differentiate between ipif's (or logical
6928 	 * interfaces) that belong to the same ill (physical interface).
6929 	 *
6930 	 * For example, in the following case involving IPv4 interfaces and
6931 	 * logical interfaces
6932 	 *
6933 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6934 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6935 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6936 	 *
6937 	 * the ipif's corresponding to each of these interface routes can be
6938 	 * uniquely identified by the "gateway" (actually interface address).
6939 	 *
6940 	 * In this case involving multiple IPv6 default routes to a particular
6941 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6942 	 * default route is of interest:
6943 	 *
6944 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6945 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6946 	 */
6947 
6948 	/* RTF_GATEWAY not set */
6949 	if (!(flags & RTF_GATEWAY)) {
6950 		queue_t	*stq;
6951 
6952 		if (sp != NULL) {
6953 			ip2dbg(("ip_rt_add: gateway security attributes "
6954 			    "cannot be set with interface route\n"));
6955 			if (ipif_refheld)
6956 				ipif_refrele(ipif);
6957 			return (EINVAL);
6958 		}
6959 
6960 		/*
6961 		 * As the interface index specified with the RTA_IFP sockaddr is
6962 		 * the same for all ipif's off of an ill, the matching logic
6963 		 * below uses MATCH_IRE_ILL if such an index was specified.
6964 		 * This means that routes sharing the same prefix when added
6965 		 * using a RTA_IFP sockaddr must have distinct interface
6966 		 * indices (namely, they must be on distinct ill's).
6967 		 *
6968 		 * On the other hand, since the gateway address will usually be
6969 		 * different for each ipif on the system, the matching logic
6970 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6971 		 * route.  This means that interface routes for the same prefix
6972 		 * can be created if they belong to distinct ipif's and if a
6973 		 * RTA_IFP sockaddr is not present.
6974 		 */
6975 		if (ipif_arg != NULL) {
6976 			if (ipif_refheld)  {
6977 				ipif_refrele(ipif);
6978 				ipif_refheld = B_FALSE;
6979 			}
6980 			ipif = ipif_arg;
6981 			match_flags |= MATCH_IRE_ILL;
6982 		} else {
6983 			/*
6984 			 * Check the ipif corresponding to the gw_addr
6985 			 */
6986 			if (ipif == NULL)
6987 				return (ENETUNREACH);
6988 			match_flags |= MATCH_IRE_IPIF;
6989 		}
6990 		ASSERT(ipif != NULL);
6991 
6992 		/*
6993 		 * We check for an existing entry at this point.
6994 		 *
6995 		 * Since a netmask isn't passed in via the ioctl interface
6996 		 * (SIOCADDRT), we don't check for a matching netmask in that
6997 		 * case.
6998 		 */
6999 		if (!ioctl_msg)
7000 			match_flags |= MATCH_IRE_MASK;
7001 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7002 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7003 		if (ire != NULL) {
7004 			ire_refrele(ire);
7005 			if (ipif_refheld)
7006 				ipif_refrele(ipif);
7007 			return (EEXIST);
7008 		}
7009 
7010 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7011 		    ? ipif->ipif_rq : ipif->ipif_wq;
7012 
7013 		/*
7014 		 * Create a copy of the IRE_LOOPBACK,
7015 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7016 		 * the modified address and netmask.
7017 		 */
7018 		ire = ire_create(
7019 		    (uchar_t *)&dst_addr,
7020 		    (uint8_t *)&mask,
7021 		    (uint8_t *)&ipif->ipif_src_addr,
7022 		    NULL,
7023 		    &ipif->ipif_mtu,
7024 		    NULL,
7025 		    NULL,
7026 		    stq,
7027 		    ipif->ipif_net_type,
7028 		    ipif,
7029 		    0,
7030 		    0,
7031 		    0,
7032 		    flags,
7033 		    &ire_uinfo_null,
7034 		    NULL,
7035 		    NULL,
7036 		    ipst);
7037 		if (ire == NULL) {
7038 			if (ipif_refheld)
7039 				ipif_refrele(ipif);
7040 			return (ENOMEM);
7041 		}
7042 
7043 		/*
7044 		 * Some software (for example, GateD and Sun Cluster) attempts
7045 		 * to create (what amount to) IRE_PREFIX routes with the
7046 		 * loopback address as the gateway.  This is primarily done to
7047 		 * set up prefixes with the RTF_REJECT flag set (for example,
7048 		 * when generating aggregate routes.)
7049 		 *
7050 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7051 		 * IRE_LOOPBACK, then we map the request into a
7052 		 * IRE_IF_NORESOLVER.
7053 		 *
7054 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7055 		 * routine, but rather using ire_create() directly.
7056 		 *
7057 		 */
7058 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7059 			ire->ire_type = IRE_IF_NORESOLVER;
7060 
7061 		error = ire_add(&ire, q, mp, func, B_FALSE);
7062 		if (error == 0)
7063 			goto save_ire;
7064 
7065 		/*
7066 		 * In the result of failure, ire_add() will have already
7067 		 * deleted the ire in question, so there is no need to
7068 		 * do that here.
7069 		 */
7070 		if (ipif_refheld)
7071 			ipif_refrele(ipif);
7072 		return (error);
7073 	}
7074 	if (ipif_refheld) {
7075 		ipif_refrele(ipif);
7076 		ipif_refheld = B_FALSE;
7077 	}
7078 
7079 	/*
7080 	 * Get an interface IRE for the specified gateway.
7081 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7082 	 * gateway, it is currently unreachable and we fail the request
7083 	 * accordingly.
7084 	 */
7085 	ipif = ipif_arg;
7086 	if (ipif_arg != NULL)
7087 		match_flags |= MATCH_IRE_ILL;
7088 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7089 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7090 	if (gw_ire == NULL)
7091 		return (ENETUNREACH);
7092 
7093 	/*
7094 	 * We create one of three types of IREs as a result of this request
7095 	 * based on the netmask.  A netmask of all ones (which is automatically
7096 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7097 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7098 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7099 	 * destination prefix.
7100 	 */
7101 	if (mask == IP_HOST_MASK)
7102 		type = IRE_HOST;
7103 	else if (mask == 0)
7104 		type = IRE_DEFAULT;
7105 	else
7106 		type = IRE_PREFIX;
7107 
7108 	/* check for a duplicate entry */
7109 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7110 	    NULL, ALL_ZONES, 0, NULL,
7111 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7112 	if (ire != NULL) {
7113 		ire_refrele(gw_ire);
7114 		ire_refrele(ire);
7115 		return (EEXIST);
7116 	}
7117 
7118 	/* Security attribute exists */
7119 	if (sp != NULL) {
7120 		tsol_gcgrp_addr_t ga;
7121 
7122 		/* find or create the gateway credentials group */
7123 		ga.ga_af = AF_INET;
7124 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7125 
7126 		/* we hold reference to it upon success */
7127 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7128 		if (gcgrp == NULL) {
7129 			ire_refrele(gw_ire);
7130 			return (ENOMEM);
7131 		}
7132 
7133 		/*
7134 		 * Create and add the security attribute to the group; a
7135 		 * reference to the group is made upon allocating a new
7136 		 * entry successfully.  If it finds an already-existing
7137 		 * entry for the security attribute in the group, it simply
7138 		 * returns it and no new reference is made to the group.
7139 		 */
7140 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7141 		if (gc == NULL) {
7142 			/* release reference held by gcgrp_lookup */
7143 			GCGRP_REFRELE(gcgrp);
7144 			ire_refrele(gw_ire);
7145 			return (ENOMEM);
7146 		}
7147 	}
7148 
7149 	/* Create the IRE. */
7150 	ire = ire_create(
7151 	    (uchar_t *)&dst_addr,		/* dest address */
7152 	    (uchar_t *)&mask,			/* mask */
7153 	    /* src address assigned by the caller? */
7154 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7155 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7156 	    (uchar_t *)&gw_addr,		/* gateway address */
7157 	    &gw_ire->ire_max_frag,
7158 	    NULL,				/* no src nce */
7159 	    NULL,				/* no recv-from queue */
7160 	    NULL,				/* no send-to queue */
7161 	    (ushort_t)type,			/* IRE type */
7162 	    ipif_arg,
7163 	    0,
7164 	    0,
7165 	    0,
7166 	    flags,
7167 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7168 	    gc,					/* security attribute */
7169 	    NULL,
7170 	    ipst);
7171 
7172 	/*
7173 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7174 	 * reference to the 'gcgrp'. We can now release the extra reference
7175 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7176 	 */
7177 	if (gcgrp_xtraref)
7178 		GCGRP_REFRELE(gcgrp);
7179 	if (ire == NULL) {
7180 		if (gc != NULL)
7181 			GC_REFRELE(gc);
7182 		ire_refrele(gw_ire);
7183 		return (ENOMEM);
7184 	}
7185 
7186 	/*
7187 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7188 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7189 	 */
7190 
7191 	/* Add the new IRE. */
7192 	error = ire_add(&ire, q, mp, func, B_FALSE);
7193 	if (error != 0) {
7194 		/*
7195 		 * In the result of failure, ire_add() will have already
7196 		 * deleted the ire in question, so there is no need to
7197 		 * do that here.
7198 		 */
7199 		ire_refrele(gw_ire);
7200 		return (error);
7201 	}
7202 
7203 	if (flags & RTF_MULTIRT) {
7204 		/*
7205 		 * Invoke the CGTP (multirouting) filtering module
7206 		 * to add the dst address in the filtering database.
7207 		 * Replicated inbound packets coming from that address
7208 		 * will be filtered to discard the duplicates.
7209 		 * It is not necessary to call the CGTP filter hook
7210 		 * when the dst address is a broadcast or multicast,
7211 		 * because an IP source address cannot be a broadcast
7212 		 * or a multicast.
7213 		 */
7214 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7215 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7216 		if (ire_dst != NULL) {
7217 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7218 			ire_refrele(ire_dst);
7219 			goto save_ire;
7220 		}
7221 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7222 		    !CLASSD(ire->ire_addr)) {
7223 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7224 			    ipst->ips_netstack->netstack_stackid,
7225 			    ire->ire_addr,
7226 			    ire->ire_gateway_addr,
7227 			    ire->ire_src_addr,
7228 			    gw_ire->ire_src_addr);
7229 			if (res != 0) {
7230 				ire_refrele(gw_ire);
7231 				ire_delete(ire);
7232 				return (res);
7233 			}
7234 		}
7235 	}
7236 
7237 	/*
7238 	 * Now that the prefix IRE entry has been created, delete any
7239 	 * existing gateway IRE cache entries as well as any IRE caches
7240 	 * using the gateway, and force them to be created through
7241 	 * ip_newroute.
7242 	 */
7243 	if (gc != NULL) {
7244 		ASSERT(gcgrp != NULL);
7245 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7246 	}
7247 
7248 save_ire:
7249 	if (gw_ire != NULL) {
7250 		ire_refrele(gw_ire);
7251 	}
7252 	if (ipif != NULL) {
7253 		/*
7254 		 * Save enough information so that we can recreate the IRE if
7255 		 * the interface goes down and then up.  The metrics associated
7256 		 * with the route will be saved as well when rts_setmetrics() is
7257 		 * called after the IRE has been created.  In the case where
7258 		 * memory cannot be allocated, none of this information will be
7259 		 * saved.
7260 		 */
7261 		ipif_save_ire(ipif, ire);
7262 	}
7263 	if (ioctl_msg)
7264 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7265 	if (ire_arg != NULL) {
7266 		/*
7267 		 * Store the ire that was successfully added into where ire_arg
7268 		 * points to so that callers don't have to look it up
7269 		 * themselves (but they are responsible for ire_refrele()ing
7270 		 * the ire when they are finished with it).
7271 		 */
7272 		*ire_arg = ire;
7273 	} else {
7274 		ire_refrele(ire);		/* Held in ire_add */
7275 	}
7276 	if (ipif_refheld)
7277 		ipif_refrele(ipif);
7278 	return (0);
7279 }
7280 
7281 /*
7282  * ip_rt_delete is called to delete an IPv4 route.
7283  * ipif_arg is passed in to associate it with the correct interface.
7284  * We may need to restart this operation if the ipif cannot be looked up
7285  * due to an exclusive operation that is currently in progress. The restart
7286  * entry point is specified by 'func'
7287  */
7288 /* ARGSUSED4 */
7289 int
7290 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7291     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7292     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7293 {
7294 	ire_t	*ire = NULL;
7295 	ipif_t	*ipif;
7296 	boolean_t ipif_refheld = B_FALSE;
7297 	uint_t	type;
7298 	uint_t	match_flags = MATCH_IRE_TYPE;
7299 	int	err = 0;
7300 
7301 	ip1dbg(("ip_rt_delete:"));
7302 	/*
7303 	 * If this is the case of RTF_HOST being set, then we set the netmask
7304 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7305 	 */
7306 	if (flags & RTF_HOST) {
7307 		mask = IP_HOST_MASK;
7308 		match_flags |= MATCH_IRE_MASK;
7309 	} else if (rtm_addrs & RTA_NETMASK) {
7310 		match_flags |= MATCH_IRE_MASK;
7311 	}
7312 
7313 	/*
7314 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7315 	 * we check if the gateway address is one of our interfaces first,
7316 	 * and fall back on RTF_GATEWAY routes.
7317 	 *
7318 	 * This makes it possible to delete an original
7319 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7320 	 *
7321 	 * As the interface index specified with the RTA_IFP sockaddr is the
7322 	 * same for all ipif's off of an ill, the matching logic below uses
7323 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7324 	 * sharing the same prefix and interface index as the the route
7325 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7326 	 * is specified in the request.
7327 	 *
7328 	 * On the other hand, since the gateway address will usually be
7329 	 * different for each ipif on the system, the matching logic
7330 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7331 	 * route.  This means that interface routes for the same prefix can be
7332 	 * uniquely identified if they belong to distinct ipif's and if a
7333 	 * RTA_IFP sockaddr is not present.
7334 	 *
7335 	 * For more detail on specifying routes by gateway address and by
7336 	 * interface index, see the comments in ip_rt_add().
7337 	 */
7338 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7339 	    ipst);
7340 	if (ipif != NULL)
7341 		ipif_refheld = B_TRUE;
7342 	else if (err == EINPROGRESS)
7343 		return (err);
7344 	else
7345 		err = 0;
7346 	if (ipif != NULL) {
7347 		if (ipif_arg != NULL) {
7348 			if (ipif_refheld) {
7349 				ipif_refrele(ipif);
7350 				ipif_refheld = B_FALSE;
7351 			}
7352 			ipif = ipif_arg;
7353 			match_flags |= MATCH_IRE_ILL;
7354 		} else {
7355 			match_flags |= MATCH_IRE_IPIF;
7356 		}
7357 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7358 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7359 			    ALL_ZONES, NULL, match_flags, ipst);
7360 		}
7361 		if (ire == NULL) {
7362 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7363 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7364 			    match_flags, ipst);
7365 		}
7366 	}
7367 
7368 	if (ire == NULL) {
7369 		/*
7370 		 * At this point, the gateway address is not one of our own
7371 		 * addresses or a matching interface route was not found.  We
7372 		 * set the IRE type to lookup based on whether
7373 		 * this is a host route, a default route or just a prefix.
7374 		 *
7375 		 * If an ipif_arg was passed in, then the lookup is based on an
7376 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7377 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7378 		 * set as the route being looked up is not a traditional
7379 		 * interface route.
7380 		 */
7381 		match_flags &= ~MATCH_IRE_IPIF;
7382 		match_flags |= MATCH_IRE_GW;
7383 		if (ipif_arg != NULL)
7384 			match_flags |= MATCH_IRE_ILL;
7385 		if (mask == IP_HOST_MASK)
7386 			type = IRE_HOST;
7387 		else if (mask == 0)
7388 			type = IRE_DEFAULT;
7389 		else
7390 			type = IRE_PREFIX;
7391 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7392 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7393 	}
7394 
7395 	if (ipif_refheld)
7396 		ipif_refrele(ipif);
7397 
7398 	/* ipif is not refheld anymore */
7399 	if (ire == NULL)
7400 		return (ESRCH);
7401 
7402 	if (ire->ire_flags & RTF_MULTIRT) {
7403 		/*
7404 		 * Invoke the CGTP (multirouting) filtering module
7405 		 * to remove the dst address from the filtering database.
7406 		 * Packets coming from that address will no longer be
7407 		 * filtered to remove duplicates.
7408 		 */
7409 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7410 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7411 			    ipst->ips_netstack->netstack_stackid,
7412 			    ire->ire_addr, ire->ire_gateway_addr);
7413 		}
7414 		ip_cgtp_bcast_delete(ire, ipst);
7415 	}
7416 
7417 	ipif = ire->ire_ipif;
7418 	if (ipif != NULL)
7419 		ipif_remove_ire(ipif, ire);
7420 	if (ioctl_msg)
7421 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7422 	ire_delete(ire);
7423 	ire_refrele(ire);
7424 	return (err);
7425 }
7426 
7427 /*
7428  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7429  */
7430 /* ARGSUSED */
7431 int
7432 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7433     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7434 {
7435 	ipaddr_t dst_addr;
7436 	ipaddr_t gw_addr;
7437 	ipaddr_t mask;
7438 	int error = 0;
7439 	mblk_t *mp1;
7440 	struct rtentry *rt;
7441 	ipif_t *ipif = NULL;
7442 	ip_stack_t	*ipst;
7443 
7444 	ASSERT(q->q_next == NULL);
7445 	ipst = CONNQ_TO_IPST(q);
7446 
7447 	ip1dbg(("ip_siocaddrt:"));
7448 	/* Existence of mp1 verified in ip_wput_nondata */
7449 	mp1 = mp->b_cont->b_cont;
7450 	rt = (struct rtentry *)mp1->b_rptr;
7451 
7452 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7453 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7454 
7455 	/*
7456 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7457 	 * to a particular host address.  In this case, we set the netmask to
7458 	 * all ones for the particular destination address.  Otherwise,
7459 	 * determine the netmask to be used based on dst_addr and the interfaces
7460 	 * in use.
7461 	 */
7462 	if (rt->rt_flags & RTF_HOST) {
7463 		mask = IP_HOST_MASK;
7464 	} else {
7465 		/*
7466 		 * Note that ip_subnet_mask returns a zero mask in the case of
7467 		 * default (an all-zeroes address).
7468 		 */
7469 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7470 	}
7471 
7472 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7473 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7474 	if (ipif != NULL)
7475 		ipif_refrele(ipif);
7476 	return (error);
7477 }
7478 
7479 /*
7480  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7481  */
7482 /* ARGSUSED */
7483 int
7484 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7485     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7486 {
7487 	ipaddr_t dst_addr;
7488 	ipaddr_t gw_addr;
7489 	ipaddr_t mask;
7490 	int error;
7491 	mblk_t *mp1;
7492 	struct rtentry *rt;
7493 	ipif_t *ipif = NULL;
7494 	ip_stack_t	*ipst;
7495 
7496 	ASSERT(q->q_next == NULL);
7497 	ipst = CONNQ_TO_IPST(q);
7498 
7499 	ip1dbg(("ip_siocdelrt:"));
7500 	/* Existence of mp1 verified in ip_wput_nondata */
7501 	mp1 = mp->b_cont->b_cont;
7502 	rt = (struct rtentry *)mp1->b_rptr;
7503 
7504 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7505 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7506 
7507 	/*
7508 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7509 	 * to a particular host address.  In this case, we set the netmask to
7510 	 * all ones for the particular destination address.  Otherwise,
7511 	 * determine the netmask to be used based on dst_addr and the interfaces
7512 	 * in use.
7513 	 */
7514 	if (rt->rt_flags & RTF_HOST) {
7515 		mask = IP_HOST_MASK;
7516 	} else {
7517 		/*
7518 		 * Note that ip_subnet_mask returns a zero mask in the case of
7519 		 * default (an all-zeroes address).
7520 		 */
7521 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7522 	}
7523 
7524 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7525 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7526 	    mp, ip_process_ioctl, ipst);
7527 	if (ipif != NULL)
7528 		ipif_refrele(ipif);
7529 	return (error);
7530 }
7531 
7532 /*
7533  * Enqueue the mp onto the ipsq, chained by b_next.
7534  * b_prev stores the function to be executed later, and b_queue the queue
7535  * where this mp originated.
7536  */
7537 void
7538 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7539     ill_t *pending_ill)
7540 {
7541 	conn_t	*connp = NULL;
7542 
7543 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7544 	ASSERT(func != NULL);
7545 
7546 	mp->b_queue = q;
7547 	mp->b_prev = (void *)func;
7548 	mp->b_next = NULL;
7549 
7550 	switch (type) {
7551 	case CUR_OP:
7552 		if (ipsq->ipsq_mptail != NULL) {
7553 			ASSERT(ipsq->ipsq_mphead != NULL);
7554 			ipsq->ipsq_mptail->b_next = mp;
7555 		} else {
7556 			ASSERT(ipsq->ipsq_mphead == NULL);
7557 			ipsq->ipsq_mphead = mp;
7558 		}
7559 		ipsq->ipsq_mptail = mp;
7560 		break;
7561 
7562 	case NEW_OP:
7563 		if (ipsq->ipsq_xopq_mptail != NULL) {
7564 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7565 			ipsq->ipsq_xopq_mptail->b_next = mp;
7566 		} else {
7567 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7568 			ipsq->ipsq_xopq_mphead = mp;
7569 		}
7570 		ipsq->ipsq_xopq_mptail = mp;
7571 		break;
7572 	default:
7573 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7574 	}
7575 
7576 	if (CONN_Q(q) && pending_ill != NULL) {
7577 		connp = Q_TO_CONN(q);
7578 
7579 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7580 		connp->conn_oper_pending_ill = pending_ill;
7581 	}
7582 }
7583 
7584 /*
7585  * Return the mp at the head of the ipsq. After emptying the ipsq
7586  * look at the next ioctl, if this ioctl is complete. Otherwise
7587  * return, we will resume when we complete the current ioctl.
7588  * The current ioctl will wait till it gets a response from the
7589  * driver below.
7590  */
7591 static mblk_t *
7592 ipsq_dq(ipsq_t *ipsq)
7593 {
7594 	mblk_t	*mp;
7595 
7596 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7597 
7598 	mp = ipsq->ipsq_mphead;
7599 	if (mp != NULL) {
7600 		ipsq->ipsq_mphead = mp->b_next;
7601 		if (ipsq->ipsq_mphead == NULL)
7602 			ipsq->ipsq_mptail = NULL;
7603 		mp->b_next = NULL;
7604 		return (mp);
7605 	}
7606 	if (ipsq->ipsq_current_ipif != NULL)
7607 		return (NULL);
7608 	mp = ipsq->ipsq_xopq_mphead;
7609 	if (mp != NULL) {
7610 		ipsq->ipsq_xopq_mphead = mp->b_next;
7611 		if (ipsq->ipsq_xopq_mphead == NULL)
7612 			ipsq->ipsq_xopq_mptail = NULL;
7613 		mp->b_next = NULL;
7614 		return (mp);
7615 	}
7616 	return (NULL);
7617 }
7618 
7619 /*
7620  * Enter the ipsq corresponding to ill, by waiting synchronously till
7621  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7622  * will have to drain completely before ipsq_enter returns success.
7623  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7624  * and the ipsq_exit logic will start the next enqueued ioctl after
7625  * completion of the current ioctl. If 'force' is used, we don't wait
7626  * for the enqueued ioctls. This is needed when a conn_close wants to
7627  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7628  * of an ill can also use this option. But we dont' use it currently.
7629  */
7630 #define	ENTER_SQ_WAIT_TICKS 100
7631 boolean_t
7632 ipsq_enter(ill_t *ill, boolean_t force)
7633 {
7634 	ipsq_t	*ipsq;
7635 	boolean_t waited_enough = B_FALSE;
7636 
7637 	/*
7638 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7639 	 * Since the <ill-ipsq> assocs could change while we wait for the
7640 	 * writer, it is easier to wait on a fixed global rather than try to
7641 	 * cv_wait on a changing ipsq.
7642 	 */
7643 	mutex_enter(&ill->ill_lock);
7644 	for (;;) {
7645 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7646 			mutex_exit(&ill->ill_lock);
7647 			return (B_FALSE);
7648 		}
7649 
7650 		ipsq = ill->ill_phyint->phyint_ipsq;
7651 		mutex_enter(&ipsq->ipsq_lock);
7652 		if (ipsq->ipsq_writer == NULL &&
7653 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7654 			break;
7655 		} else if (ipsq->ipsq_writer != NULL) {
7656 			mutex_exit(&ipsq->ipsq_lock);
7657 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7658 		} else {
7659 			mutex_exit(&ipsq->ipsq_lock);
7660 			if (force) {
7661 				(void) cv_timedwait(&ill->ill_cv,
7662 				    &ill->ill_lock,
7663 				    lbolt + ENTER_SQ_WAIT_TICKS);
7664 				waited_enough = B_TRUE;
7665 				continue;
7666 			} else {
7667 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7668 			}
7669 		}
7670 	}
7671 
7672 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7673 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7674 	ipsq->ipsq_writer = curthread;
7675 	ipsq->ipsq_reentry_cnt++;
7676 #ifdef DEBUG
7677 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7678 #endif
7679 	mutex_exit(&ipsq->ipsq_lock);
7680 	mutex_exit(&ill->ill_lock);
7681 	return (B_TRUE);
7682 }
7683 
7684 /*
7685  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7686  * certain critical operations like plumbing (i.e. most set ioctls),
7687  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7688  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7689  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7690  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7691  * threads executing in the ipsq. Responses from the driver pertain to the
7692  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7693  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7694  *
7695  * If a thread does not want to reenter the ipsq when it is already writer,
7696  * it must make sure that the specified reentry point to be called later
7697  * when the ipsq is empty, nor any code path starting from the specified reentry
7698  * point must never ever try to enter the ipsq again. Otherwise it can lead
7699  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7700  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7701  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7702  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7703  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7704  * ioctl if the current ioctl has completed. If the current ioctl is still
7705  * in progress it simply returns. The current ioctl could be waiting for
7706  * a response from another module (arp_ or the driver or could be waiting for
7707  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7708  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7709  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7710  * ipsq_current_ipif is clear which happens only on ioctl completion.
7711  */
7712 
7713 /*
7714  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7715  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7716  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7717  * completion.
7718  */
7719 ipsq_t *
7720 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7721     ipsq_func_t func, int type, boolean_t reentry_ok)
7722 {
7723 	ipsq_t	*ipsq;
7724 
7725 	/* Only 1 of ipif or ill can be specified */
7726 	ASSERT((ipif != NULL) ^ (ill != NULL));
7727 	if (ipif != NULL)
7728 		ill = ipif->ipif_ill;
7729 
7730 	/*
7731 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7732 	 * ipsq of an ill can't change when ill_lock is held.
7733 	 */
7734 	GRAB_CONN_LOCK(q);
7735 	mutex_enter(&ill->ill_lock);
7736 	ipsq = ill->ill_phyint->phyint_ipsq;
7737 	mutex_enter(&ipsq->ipsq_lock);
7738 
7739 	/*
7740 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7741 	 *    (Note: If the caller does not specify reentry_ok then neither
7742 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7743 	 *    again. Otherwise it can lead to an infinite loop
7744 	 * 2. Enter the ipsq if there is no current writer and this attempted
7745 	 *    entry is part of the current ioctl or operation
7746 	 * 3. Enter the ipsq if there is no current writer and this is a new
7747 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7748 	 *    empty and there is no ioctl (or operation) currently in progress
7749 	 */
7750 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7751 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7752 	    ipsq->ipsq_current_ipif == NULL))) ||
7753 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7754 		/* Success. */
7755 		ipsq->ipsq_reentry_cnt++;
7756 		ipsq->ipsq_writer = curthread;
7757 		mutex_exit(&ipsq->ipsq_lock);
7758 		mutex_exit(&ill->ill_lock);
7759 		RELEASE_CONN_LOCK(q);
7760 #ifdef DEBUG
7761 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7762 		    IPSQ_STACK_DEPTH);
7763 #endif
7764 		return (ipsq);
7765 	}
7766 
7767 	ipsq_enq(ipsq, q, mp, func, type, ill);
7768 
7769 	mutex_exit(&ipsq->ipsq_lock);
7770 	mutex_exit(&ill->ill_lock);
7771 	RELEASE_CONN_LOCK(q);
7772 	return (NULL);
7773 }
7774 
7775 /*
7776  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7777  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7778  * cannot be entered, the mp is queued for completion.
7779  */
7780 void
7781 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7782     boolean_t reentry_ok)
7783 {
7784 	ipsq_t	*ipsq;
7785 
7786 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7787 
7788 	/*
7789 	 * Drop the caller's refhold on the ill.  This is safe since we either
7790 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7791 	 * IPSQ, in which case we return without accessing ill anymore.  This
7792 	 * is needed because func needs to see the correct refcount.
7793 	 * e.g. removeif can work only then.
7794 	 */
7795 	ill_refrele(ill);
7796 	if (ipsq != NULL) {
7797 		(*func)(ipsq, q, mp, NULL);
7798 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7799 	}
7800 }
7801 
7802 /*
7803  * If there are more than ILL_GRP_CNT ills in a group,
7804  * we use kmem alloc'd buffers, else use the stack
7805  */
7806 #define	ILL_GRP_CNT	14
7807 /*
7808  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7809  * Called by a thread that is currently exclusive on this ipsq.
7810  */
7811 void
7812 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7813 {
7814 	queue_t	*q;
7815 	mblk_t	*mp;
7816 	ipsq_func_t	func;
7817 	int	next;
7818 	ill_t	**ill_list = NULL;
7819 	size_t	ill_list_size = 0;
7820 	int	cnt = 0;
7821 	boolean_t need_ipsq_free = B_FALSE;
7822 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7823 
7824 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7825 	mutex_enter(&ipsq->ipsq_lock);
7826 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7827 	if (ipsq->ipsq_reentry_cnt != 1) {
7828 		ipsq->ipsq_reentry_cnt--;
7829 		mutex_exit(&ipsq->ipsq_lock);
7830 		return;
7831 	}
7832 
7833 	mp = ipsq_dq(ipsq);
7834 	while (mp != NULL) {
7835 again:
7836 		mutex_exit(&ipsq->ipsq_lock);
7837 		func = (ipsq_func_t)mp->b_prev;
7838 		q = (queue_t *)mp->b_queue;
7839 		mp->b_prev = NULL;
7840 		mp->b_queue = NULL;
7841 
7842 		/*
7843 		 * If 'q' is an conn queue, it is valid, since we did a
7844 		 * a refhold on the connp, at the start of the ioctl.
7845 		 * If 'q' is an ill queue, it is valid, since close of an
7846 		 * ill will clean up the 'ipsq'.
7847 		 */
7848 		(*func)(ipsq, q, mp, NULL);
7849 
7850 		mutex_enter(&ipsq->ipsq_lock);
7851 		mp = ipsq_dq(ipsq);
7852 	}
7853 
7854 	mutex_exit(&ipsq->ipsq_lock);
7855 
7856 	/*
7857 	 * Need to grab the locks in the right order. Need to
7858 	 * atomically check (under ipsq_lock) that there are no
7859 	 * messages before relinquishing the ipsq. Also need to
7860 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7861 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7862 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7863 	 * to grab ill_g_lock as writer.
7864 	 */
7865 	rw_enter(&ipst->ips_ill_g_lock,
7866 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7867 
7868 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7869 	if (ipsq->ipsq_refs != 0) {
7870 		/* At most 2 ills v4/v6 per phyint */
7871 		cnt = ipsq->ipsq_refs << 1;
7872 		ill_list_size = cnt * sizeof (ill_t *);
7873 		/*
7874 		 * If memory allocation fails, we will do the split
7875 		 * the next time ipsq_exit is called for whatever reason.
7876 		 * As long as the ipsq_split flag is set the need to
7877 		 * split is remembered.
7878 		 */
7879 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7880 		if (ill_list != NULL)
7881 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7882 	}
7883 	mutex_enter(&ipsq->ipsq_lock);
7884 	mp = ipsq_dq(ipsq);
7885 	if (mp != NULL) {
7886 		/* oops, some message has landed up, we can't get out */
7887 		if (ill_list != NULL)
7888 			ill_unlock_ills(ill_list, cnt);
7889 		rw_exit(&ipst->ips_ill_g_lock);
7890 		if (ill_list != NULL)
7891 			kmem_free(ill_list, ill_list_size);
7892 		ill_list = NULL;
7893 		ill_list_size = 0;
7894 		cnt = 0;
7895 		goto again;
7896 	}
7897 
7898 	/*
7899 	 * Split only if no ioctl is pending and if memory alloc succeeded
7900 	 * above.
7901 	 */
7902 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7903 	    ill_list != NULL) {
7904 		/*
7905 		 * No new ill can join this ipsq since we are holding the
7906 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7907 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7908 		 * If so we will retry on the next ipsq_exit.
7909 		 */
7910 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7911 	}
7912 
7913 	/*
7914 	 * We are holding the ipsq lock, hence no new messages can
7915 	 * land up on the ipsq, and there are no messages currently.
7916 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7917 	 * atomically while holding ill locks.
7918 	 */
7919 	ipsq->ipsq_writer = NULL;
7920 	ipsq->ipsq_reentry_cnt--;
7921 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7922 #ifdef DEBUG
7923 	ipsq->ipsq_depth = 0;
7924 #endif
7925 	mutex_exit(&ipsq->ipsq_lock);
7926 	/*
7927 	 * For IPMP this should wake up all ills in this ipsq.
7928 	 * We need to hold the ill_lock while waking up waiters to
7929 	 * avoid missed wakeups. But there is no need to acquire all
7930 	 * the ill locks and then wakeup. If we have not acquired all
7931 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7932 	 * wakes up ills one at a time after getting the right ill_lock
7933 	 */
7934 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7935 	if (ill_list != NULL)
7936 		ill_unlock_ills(ill_list, cnt);
7937 	if (ipsq->ipsq_refs == 0)
7938 		need_ipsq_free = B_TRUE;
7939 	rw_exit(&ipst->ips_ill_g_lock);
7940 	if (ill_list != 0)
7941 		kmem_free(ill_list, ill_list_size);
7942 
7943 	if (need_ipsq_free) {
7944 		/*
7945 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7946 		 * looked up. ipsq can be looked up only thru ill or phyint
7947 		 * and there are no ills/phyint on this ipsq.
7948 		 */
7949 		ipsq_delete(ipsq);
7950 	}
7951 	/*
7952 	 * Now start any igmp or mld timers that could not be started
7953 	 * while inside the ipsq. The timers can't be started while inside
7954 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7955 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7956 	 * there could be a deadlock since the timeout handlers
7957 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7958 	 * wait in ipsq_enter() trying to get the ipsq.
7959 	 *
7960 	 * However there is one exception to the above. If this thread is
7961 	 * itself the igmp/mld timeout handler thread, then we don't want
7962 	 * to start any new timer until the current handler is done. The
7963 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7964 	 * all others pass B_TRUE.
7965 	 */
7966 	if (start_igmp_timer) {
7967 		mutex_enter(&ipst->ips_igmp_timer_lock);
7968 		next = ipst->ips_igmp_deferred_next;
7969 		ipst->ips_igmp_deferred_next = INFINITY;
7970 		mutex_exit(&ipst->ips_igmp_timer_lock);
7971 
7972 		if (next != INFINITY)
7973 			igmp_start_timers(next, ipst);
7974 	}
7975 
7976 	if (start_mld_timer) {
7977 		mutex_enter(&ipst->ips_mld_timer_lock);
7978 		next = ipst->ips_mld_deferred_next;
7979 		ipst->ips_mld_deferred_next = INFINITY;
7980 		mutex_exit(&ipst->ips_mld_timer_lock);
7981 
7982 		if (next != INFINITY)
7983 			mld_start_timers(next, ipst);
7984 	}
7985 }
7986 
7987 /*
7988  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7989  * and `ioccmd'.
7990  */
7991 void
7992 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7993 {
7994 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7995 
7996 	mutex_enter(&ipsq->ipsq_lock);
7997 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7998 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7999 	ipsq->ipsq_current_ipif = ipif;
8000 	ipsq->ipsq_current_ioctl = ioccmd;
8001 	mutex_exit(&ipsq->ipsq_lock);
8002 }
8003 
8004 /*
8005  * Finish the current exclusive operation on `ipsq'.  Note that other
8006  * operations will not be able to proceed until an ipsq_exit() is done.
8007  */
8008 void
8009 ipsq_current_finish(ipsq_t *ipsq)
8010 {
8011 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8012 
8013 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8014 
8015 	/*
8016 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8017 	 * (but we're careful to never set IPIF_CHANGING in that case).
8018 	 */
8019 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8020 		mutex_enter(&ipif->ipif_ill->ill_lock);
8021 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8022 
8023 		/* Send any queued event */
8024 		ill_nic_info_dispatch(ipif->ipif_ill);
8025 		mutex_exit(&ipif->ipif_ill->ill_lock);
8026 	}
8027 
8028 	mutex_enter(&ipsq->ipsq_lock);
8029 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8030 	ipsq->ipsq_current_ipif = NULL;
8031 	ipsq->ipsq_current_ioctl = 0;
8032 	mutex_exit(&ipsq->ipsq_lock);
8033 }
8034 
8035 /*
8036  * The ill is closing. Flush all messages on the ipsq that originated
8037  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8038  * for this ill since ipsq_enter could not have entered until then.
8039  * New messages can't be queued since the CONDEMNED flag is set.
8040  */
8041 static void
8042 ipsq_flush(ill_t *ill)
8043 {
8044 	queue_t	*q;
8045 	mblk_t	*prev;
8046 	mblk_t	*mp;
8047 	mblk_t	*mp_next;
8048 	ipsq_t	*ipsq;
8049 
8050 	ASSERT(IAM_WRITER_ILL(ill));
8051 	ipsq = ill->ill_phyint->phyint_ipsq;
8052 	/*
8053 	 * Flush any messages sent up by the driver.
8054 	 */
8055 	mutex_enter(&ipsq->ipsq_lock);
8056 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8057 		mp_next = mp->b_next;
8058 		q = mp->b_queue;
8059 		if (q == ill->ill_rq || q == ill->ill_wq) {
8060 			/* Remove the mp from the ipsq */
8061 			if (prev == NULL)
8062 				ipsq->ipsq_mphead = mp->b_next;
8063 			else
8064 				prev->b_next = mp->b_next;
8065 			if (ipsq->ipsq_mptail == mp) {
8066 				ASSERT(mp_next == NULL);
8067 				ipsq->ipsq_mptail = prev;
8068 			}
8069 			inet_freemsg(mp);
8070 		} else {
8071 			prev = mp;
8072 		}
8073 	}
8074 	mutex_exit(&ipsq->ipsq_lock);
8075 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8076 	ipsq_xopq_mp_cleanup(ill, NULL);
8077 	ill_pending_mp_cleanup(ill);
8078 }
8079 
8080 /* ARGSUSED */
8081 int
8082 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8083     ip_ioctl_cmd_t *ipip, void *ifreq)
8084 {
8085 	ill_t	*ill;
8086 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8087 	boolean_t isv6;
8088 	conn_t	*connp;
8089 	ip_stack_t	*ipst;
8090 
8091 	connp = Q_TO_CONN(q);
8092 	ipst = connp->conn_netstack->netstack_ip;
8093 	isv6 = connp->conn_af_isv6;
8094 	/*
8095 	 * Set original index.
8096 	 * Failover and failback move logical interfaces
8097 	 * from one physical interface to another.  The
8098 	 * original index indicates the parent of a logical
8099 	 * interface, in other words, the physical interface
8100 	 * the logical interface will be moved back to on
8101 	 * failback.
8102 	 */
8103 
8104 	/*
8105 	 * Don't allow the original index to be changed
8106 	 * for non-failover addresses, autoconfigured
8107 	 * addresses, or IPv6 link local addresses.
8108 	 */
8109 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8110 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8111 		return (EINVAL);
8112 	}
8113 	/*
8114 	 * The new original index must be in use by some
8115 	 * physical interface.
8116 	 */
8117 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8118 	    NULL, NULL, ipst);
8119 	if (ill == NULL)
8120 		return (ENXIO);
8121 	ill_refrele(ill);
8122 
8123 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8124 	/*
8125 	 * When this ipif gets failed back, don't
8126 	 * preserve the original id, as it is no
8127 	 * longer applicable.
8128 	 */
8129 	ipif->ipif_orig_ipifid = 0;
8130 	/*
8131 	 * For IPv4, change the original index of any
8132 	 * multicast addresses associated with the
8133 	 * ipif to the new value.
8134 	 */
8135 	if (!isv6) {
8136 		ilm_t *ilm;
8137 
8138 		mutex_enter(&ipif->ipif_ill->ill_lock);
8139 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8140 		    ilm = ilm->ilm_next) {
8141 			if (ilm->ilm_ipif == ipif) {
8142 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8143 			}
8144 		}
8145 		mutex_exit(&ipif->ipif_ill->ill_lock);
8146 	}
8147 	return (0);
8148 }
8149 
8150 /* ARGSUSED */
8151 int
8152 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8153     ip_ioctl_cmd_t *ipip, void *ifreq)
8154 {
8155 	struct lifreq *lifr = (struct lifreq *)ifreq;
8156 
8157 	/*
8158 	 * Get the original interface index i.e the one
8159 	 * before FAILOVER if it ever happened.
8160 	 */
8161 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8162 	return (0);
8163 }
8164 
8165 /*
8166  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8167  * refhold and return the associated ipif
8168  */
8169 /* ARGSUSED */
8170 int
8171 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8172     cmd_info_t *ci, ipsq_func_t func)
8173 {
8174 	boolean_t exists;
8175 	struct iftun_req *ta;
8176 	ipif_t	*ipif;
8177 	ill_t	*ill;
8178 	boolean_t isv6;
8179 	mblk_t	*mp1;
8180 	int	error;
8181 	conn_t	*connp;
8182 	ip_stack_t	*ipst;
8183 
8184 	/* Existence verified in ip_wput_nondata */
8185 	mp1 = mp->b_cont->b_cont;
8186 	ta = (struct iftun_req *)mp1->b_rptr;
8187 	/*
8188 	 * Null terminate the string to protect against buffer
8189 	 * overrun. String was generated by user code and may not
8190 	 * be trusted.
8191 	 */
8192 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8193 
8194 	connp = Q_TO_CONN(q);
8195 	isv6 = connp->conn_af_isv6;
8196 	ipst = connp->conn_netstack->netstack_ip;
8197 
8198 	/* Disallows implicit create */
8199 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8200 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8201 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8202 	if (ipif == NULL)
8203 		return (error);
8204 
8205 	if (ipif->ipif_id != 0) {
8206 		/*
8207 		 * We really don't want to set/get tunnel parameters
8208 		 * on virtual tunnel interfaces.  Only allow the
8209 		 * base tunnel to do these.
8210 		 */
8211 		ipif_refrele(ipif);
8212 		return (EINVAL);
8213 	}
8214 
8215 	/*
8216 	 * Send down to tunnel mod for ioctl processing.
8217 	 * Will finish ioctl in ip_rput_other().
8218 	 */
8219 	ill = ipif->ipif_ill;
8220 	if (ill->ill_net_type == IRE_LOOPBACK) {
8221 		ipif_refrele(ipif);
8222 		return (EOPNOTSUPP);
8223 	}
8224 
8225 	if (ill->ill_wq == NULL) {
8226 		ipif_refrele(ipif);
8227 		return (ENXIO);
8228 	}
8229 	/*
8230 	 * Mark the ioctl as coming from an IPv6 interface for
8231 	 * tun's convenience.
8232 	 */
8233 	if (ill->ill_isv6)
8234 		ta->ifta_flags |= 0x80000000;
8235 	ci->ci_ipif = ipif;
8236 	return (0);
8237 }
8238 
8239 /*
8240  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8241  * and return the associated ipif.
8242  * Return value:
8243  *	Non zero: An error has occurred. ci may not be filled out.
8244  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8245  *	a held ipif in ci.ci_ipif.
8246  */
8247 int
8248 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8249     cmd_info_t *ci, ipsq_func_t func)
8250 {
8251 	sin_t		*sin;
8252 	sin6_t		*sin6;
8253 	char		*name;
8254 	struct ifreq    *ifr;
8255 	struct lifreq    *lifr;
8256 	ipif_t		*ipif = NULL;
8257 	ill_t		*ill;
8258 	conn_t		*connp;
8259 	boolean_t	isv6;
8260 	boolean_t	exists;
8261 	int		err;
8262 	mblk_t		*mp1;
8263 	zoneid_t	zoneid;
8264 	ip_stack_t	*ipst;
8265 
8266 	if (q->q_next != NULL) {
8267 		ill = (ill_t *)q->q_ptr;
8268 		isv6 = ill->ill_isv6;
8269 		connp = NULL;
8270 		zoneid = ALL_ZONES;
8271 		ipst = ill->ill_ipst;
8272 	} else {
8273 		ill = NULL;
8274 		connp = Q_TO_CONN(q);
8275 		isv6 = connp->conn_af_isv6;
8276 		zoneid = connp->conn_zoneid;
8277 		if (zoneid == GLOBAL_ZONEID) {
8278 			/* global zone can access ipifs in all zones */
8279 			zoneid = ALL_ZONES;
8280 		}
8281 		ipst = connp->conn_netstack->netstack_ip;
8282 	}
8283 
8284 	/* Has been checked in ip_wput_nondata */
8285 	mp1 = mp->b_cont->b_cont;
8286 
8287 	if (ipip->ipi_cmd_type == IF_CMD) {
8288 		/* This a old style SIOC[GS]IF* command */
8289 		ifr = (struct ifreq *)mp1->b_rptr;
8290 		/*
8291 		 * Null terminate the string to protect against buffer
8292 		 * overrun. String was generated by user code and may not
8293 		 * be trusted.
8294 		 */
8295 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8296 		sin = (sin_t *)&ifr->ifr_addr;
8297 		name = ifr->ifr_name;
8298 		ci->ci_sin = sin;
8299 		ci->ci_sin6 = NULL;
8300 		ci->ci_lifr = (struct lifreq *)ifr;
8301 	} else {
8302 		/* This a new style SIOC[GS]LIF* command */
8303 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8304 		lifr = (struct lifreq *)mp1->b_rptr;
8305 		/*
8306 		 * Null terminate the string to protect against buffer
8307 		 * overrun. String was generated by user code and may not
8308 		 * be trusted.
8309 		 */
8310 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8311 		name = lifr->lifr_name;
8312 		sin = (sin_t *)&lifr->lifr_addr;
8313 		sin6 = (sin6_t *)&lifr->lifr_addr;
8314 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8315 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8316 			    LIFNAMSIZ);
8317 		}
8318 		ci->ci_sin = sin;
8319 		ci->ci_sin6 = sin6;
8320 		ci->ci_lifr = lifr;
8321 	}
8322 
8323 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8324 		/*
8325 		 * The ioctl will be failed if the ioctl comes down
8326 		 * an conn stream
8327 		 */
8328 		if (ill == NULL) {
8329 			/*
8330 			 * Not an ill queue, return EINVAL same as the
8331 			 * old error code.
8332 			 */
8333 			return (ENXIO);
8334 		}
8335 		ipif = ill->ill_ipif;
8336 		ipif_refhold(ipif);
8337 	} else {
8338 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8339 		    &exists, isv6, zoneid,
8340 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8341 		    ipst);
8342 		if (ipif == NULL) {
8343 			if (err == EINPROGRESS)
8344 				return (err);
8345 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8346 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8347 				/*
8348 				 * Need to try both v4 and v6 since this
8349 				 * ioctl can come down either v4 or v6
8350 				 * socket. The lifreq.lifr_family passed
8351 				 * down by this ioctl is AF_UNSPEC.
8352 				 */
8353 				ipif = ipif_lookup_on_name(name,
8354 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8355 				    zoneid, (connp == NULL) ? q :
8356 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8357 				if (err == EINPROGRESS)
8358 					return (err);
8359 			}
8360 			err = 0;	/* Ensure we don't use it below */
8361 		}
8362 	}
8363 
8364 	/*
8365 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8366 	 */
8367 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8368 		ipif_refrele(ipif);
8369 		return (ENXIO);
8370 	}
8371 
8372 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8373 	    name[0] == '\0') {
8374 		/*
8375 		 * Handle a or a SIOC?IF* with a null name
8376 		 * during plumb (on the ill queue before the I_PLINK).
8377 		 */
8378 		ipif = ill->ill_ipif;
8379 		ipif_refhold(ipif);
8380 	}
8381 
8382 	if (ipif == NULL)
8383 		return (ENXIO);
8384 
8385 	/*
8386 	 * Allow only GET operations if this ipif has been created
8387 	 * temporarily due to a MOVE operation.
8388 	 */
8389 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8390 		ipif_refrele(ipif);
8391 		return (EINVAL);
8392 	}
8393 
8394 	ci->ci_ipif = ipif;
8395 	return (0);
8396 }
8397 
8398 /*
8399  * Return the total number of ipifs.
8400  */
8401 static uint_t
8402 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8403 {
8404 	uint_t numifs = 0;
8405 	ill_t	*ill;
8406 	ill_walk_context_t	ctx;
8407 	ipif_t	*ipif;
8408 
8409 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8410 	ill = ILL_START_WALK_V4(&ctx, ipst);
8411 
8412 	while (ill != NULL) {
8413 		for (ipif = ill->ill_ipif; ipif != NULL;
8414 		    ipif = ipif->ipif_next) {
8415 			if (ipif->ipif_zoneid == zoneid ||
8416 			    ipif->ipif_zoneid == ALL_ZONES)
8417 				numifs++;
8418 		}
8419 		ill = ill_next(&ctx, ill);
8420 	}
8421 	rw_exit(&ipst->ips_ill_g_lock);
8422 	return (numifs);
8423 }
8424 
8425 /*
8426  * Return the total number of ipifs.
8427  */
8428 static uint_t
8429 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8430 {
8431 	uint_t numifs = 0;
8432 	ill_t	*ill;
8433 	ipif_t	*ipif;
8434 	ill_walk_context_t	ctx;
8435 
8436 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8437 
8438 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8439 	if (family == AF_INET)
8440 		ill = ILL_START_WALK_V4(&ctx, ipst);
8441 	else if (family == AF_INET6)
8442 		ill = ILL_START_WALK_V6(&ctx, ipst);
8443 	else
8444 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8445 
8446 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8447 		for (ipif = ill->ill_ipif; ipif != NULL;
8448 		    ipif = ipif->ipif_next) {
8449 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8450 			    !(lifn_flags & LIFC_NOXMIT))
8451 				continue;
8452 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8453 			    !(lifn_flags & LIFC_TEMPORARY))
8454 				continue;
8455 			if (((ipif->ipif_flags &
8456 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8457 			    IPIF_DEPRECATED)) ||
8458 			    IS_LOOPBACK(ill) ||
8459 			    !(ipif->ipif_flags & IPIF_UP)) &&
8460 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8461 				continue;
8462 
8463 			if (zoneid != ipif->ipif_zoneid &&
8464 			    ipif->ipif_zoneid != ALL_ZONES &&
8465 			    (zoneid != GLOBAL_ZONEID ||
8466 			    !(lifn_flags & LIFC_ALLZONES)))
8467 				continue;
8468 
8469 			numifs++;
8470 		}
8471 	}
8472 	rw_exit(&ipst->ips_ill_g_lock);
8473 	return (numifs);
8474 }
8475 
8476 uint_t
8477 ip_get_lifsrcofnum(ill_t *ill)
8478 {
8479 	uint_t numifs = 0;
8480 	ill_t	*ill_head = ill;
8481 	ip_stack_t	*ipst = ill->ill_ipst;
8482 
8483 	/*
8484 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8485 	 * other thread may be trying to relink the ILLs in this usesrc group
8486 	 * and adjusting the ill_usesrc_grp_next pointers
8487 	 */
8488 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8489 	if ((ill->ill_usesrc_ifindex == 0) &&
8490 	    (ill->ill_usesrc_grp_next != NULL)) {
8491 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8492 		    ill = ill->ill_usesrc_grp_next)
8493 			numifs++;
8494 	}
8495 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8496 
8497 	return (numifs);
8498 }
8499 
8500 /* Null values are passed in for ipif, sin, and ifreq */
8501 /* ARGSUSED */
8502 int
8503 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8504     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8505 {
8506 	int *nump;
8507 	conn_t *connp = Q_TO_CONN(q);
8508 
8509 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8510 
8511 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8512 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8513 
8514 	*nump = ip_get_numifs(connp->conn_zoneid,
8515 	    connp->conn_netstack->netstack_ip);
8516 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8517 	return (0);
8518 }
8519 
8520 /* Null values are passed in for ipif, sin, and ifreq */
8521 /* ARGSUSED */
8522 int
8523 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8524     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8525 {
8526 	struct lifnum *lifn;
8527 	mblk_t	*mp1;
8528 	conn_t *connp = Q_TO_CONN(q);
8529 
8530 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8531 
8532 	/* Existence checked in ip_wput_nondata */
8533 	mp1 = mp->b_cont->b_cont;
8534 
8535 	lifn = (struct lifnum *)mp1->b_rptr;
8536 	switch (lifn->lifn_family) {
8537 	case AF_UNSPEC:
8538 	case AF_INET:
8539 	case AF_INET6:
8540 		break;
8541 	default:
8542 		return (EAFNOSUPPORT);
8543 	}
8544 
8545 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8546 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8547 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8548 	return (0);
8549 }
8550 
8551 /* ARGSUSED */
8552 int
8553 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8554     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8555 {
8556 	STRUCT_HANDLE(ifconf, ifc);
8557 	mblk_t *mp1;
8558 	struct iocblk *iocp;
8559 	struct ifreq *ifr;
8560 	ill_walk_context_t	ctx;
8561 	ill_t	*ill;
8562 	ipif_t	*ipif;
8563 	struct sockaddr_in *sin;
8564 	int32_t	ifclen;
8565 	zoneid_t zoneid;
8566 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8567 
8568 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8569 
8570 	ip1dbg(("ip_sioctl_get_ifconf"));
8571 	/* Existence verified in ip_wput_nondata */
8572 	mp1 = mp->b_cont->b_cont;
8573 	iocp = (struct iocblk *)mp->b_rptr;
8574 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8575 
8576 	/*
8577 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8578 	 * the user buffer address and length into which the list of struct
8579 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8580 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8581 	 * the SIOCGIFCONF operation was redefined to simply provide
8582 	 * a large output buffer into which we are supposed to jam the ifreq
8583 	 * array.  The same ioctl command code was used, despite the fact that
8584 	 * both the applications and the kernel code had to change, thus making
8585 	 * it impossible to support both interfaces.
8586 	 *
8587 	 * For reasons not good enough to try to explain, the following
8588 	 * algorithm is used for deciding what to do with one of these:
8589 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8590 	 * form with the output buffer coming down as the continuation message.
8591 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8592 	 * and we have to copy in the ifconf structure to find out how big the
8593 	 * output buffer is and where to copy out to.  Sure no problem...
8594 	 *
8595 	 */
8596 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8597 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8598 		int numifs = 0;
8599 		size_t ifc_bufsize;
8600 
8601 		/*
8602 		 * Must be (better be!) continuation of a TRANSPARENT
8603 		 * IOCTL.  We just copied in the ifconf structure.
8604 		 */
8605 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8606 		    (struct ifconf *)mp1->b_rptr);
8607 
8608 		/*
8609 		 * Allocate a buffer to hold requested information.
8610 		 *
8611 		 * If ifc_len is larger than what is needed, we only
8612 		 * allocate what we will use.
8613 		 *
8614 		 * If ifc_len is smaller than what is needed, return
8615 		 * EINVAL.
8616 		 *
8617 		 * XXX: the ill_t structure can hava 2 counters, for
8618 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8619 		 * number of interfaces for a device, so we don't need
8620 		 * to count them here...
8621 		 */
8622 		numifs = ip_get_numifs(zoneid, ipst);
8623 
8624 		ifclen = STRUCT_FGET(ifc, ifc_len);
8625 		ifc_bufsize = numifs * sizeof (struct ifreq);
8626 		if (ifc_bufsize > ifclen) {
8627 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8628 				/* old behaviour */
8629 				return (EINVAL);
8630 			} else {
8631 				ifc_bufsize = ifclen;
8632 			}
8633 		}
8634 
8635 		mp1 = mi_copyout_alloc(q, mp,
8636 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8637 		if (mp1 == NULL)
8638 			return (ENOMEM);
8639 
8640 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8641 	}
8642 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8643 	/*
8644 	 * the SIOCGIFCONF ioctl only knows about
8645 	 * IPv4 addresses, so don't try to tell
8646 	 * it about interfaces with IPv6-only
8647 	 * addresses. (Last parm 'isv6' is B_FALSE)
8648 	 */
8649 
8650 	ifr = (struct ifreq *)mp1->b_rptr;
8651 
8652 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8653 	ill = ILL_START_WALK_V4(&ctx, ipst);
8654 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8655 		for (ipif = ill->ill_ipif; ipif != NULL;
8656 		    ipif = ipif->ipif_next) {
8657 			if (zoneid != ipif->ipif_zoneid &&
8658 			    ipif->ipif_zoneid != ALL_ZONES)
8659 				continue;
8660 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8661 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8662 					/* old behaviour */
8663 					rw_exit(&ipst->ips_ill_g_lock);
8664 					return (EINVAL);
8665 				} else {
8666 					goto if_copydone;
8667 				}
8668 			}
8669 			ipif_get_name(ipif, ifr->ifr_name,
8670 			    sizeof (ifr->ifr_name));
8671 			sin = (sin_t *)&ifr->ifr_addr;
8672 			*sin = sin_null;
8673 			sin->sin_family = AF_INET;
8674 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8675 			ifr++;
8676 		}
8677 	}
8678 if_copydone:
8679 	rw_exit(&ipst->ips_ill_g_lock);
8680 	mp1->b_wptr = (uchar_t *)ifr;
8681 
8682 	if (STRUCT_BUF(ifc) != NULL) {
8683 		STRUCT_FSET(ifc, ifc_len,
8684 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8685 	}
8686 	return (0);
8687 }
8688 
8689 /*
8690  * Get the interfaces using the address hosted on the interface passed in,
8691  * as a source adddress
8692  */
8693 /* ARGSUSED */
8694 int
8695 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8696     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8697 {
8698 	mblk_t *mp1;
8699 	ill_t	*ill, *ill_head;
8700 	ipif_t	*ipif, *orig_ipif;
8701 	int	numlifs = 0;
8702 	size_t	lifs_bufsize, lifsmaxlen;
8703 	struct	lifreq *lifr;
8704 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8705 	uint_t	ifindex;
8706 	zoneid_t zoneid;
8707 	int err = 0;
8708 	boolean_t isv6 = B_FALSE;
8709 	struct	sockaddr_in	*sin;
8710 	struct	sockaddr_in6	*sin6;
8711 	STRUCT_HANDLE(lifsrcof, lifs);
8712 	ip_stack_t		*ipst;
8713 
8714 	ipst = CONNQ_TO_IPST(q);
8715 
8716 	ASSERT(q->q_next == NULL);
8717 
8718 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8719 
8720 	/* Existence verified in ip_wput_nondata */
8721 	mp1 = mp->b_cont->b_cont;
8722 
8723 	/*
8724 	 * Must be (better be!) continuation of a TRANSPARENT
8725 	 * IOCTL.  We just copied in the lifsrcof structure.
8726 	 */
8727 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8728 	    (struct lifsrcof *)mp1->b_rptr);
8729 
8730 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8731 		return (EINVAL);
8732 
8733 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8734 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8735 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8736 	    ip_process_ioctl, &err, ipst);
8737 	if (ipif == NULL) {
8738 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8739 		    ifindex));
8740 		return (err);
8741 	}
8742 
8743 
8744 	/* Allocate a buffer to hold requested information */
8745 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8746 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8747 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8748 	/* The actual size needed is always returned in lifs_len */
8749 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8750 
8751 	/* If the amount we need is more than what is passed in, abort */
8752 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8753 		ipif_refrele(ipif);
8754 		return (0);
8755 	}
8756 
8757 	mp1 = mi_copyout_alloc(q, mp,
8758 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8759 	if (mp1 == NULL) {
8760 		ipif_refrele(ipif);
8761 		return (ENOMEM);
8762 	}
8763 
8764 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8765 	bzero(mp1->b_rptr, lifs_bufsize);
8766 
8767 	lifr = (struct lifreq *)mp1->b_rptr;
8768 
8769 	ill = ill_head = ipif->ipif_ill;
8770 	orig_ipif = ipif;
8771 
8772 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8773 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8774 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8775 
8776 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8777 	for (; (ill != NULL) && (ill != ill_head);
8778 	    ill = ill->ill_usesrc_grp_next) {
8779 
8780 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8781 			break;
8782 
8783 		ipif = ill->ill_ipif;
8784 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8785 		if (ipif->ipif_isv6) {
8786 			sin6 = (sin6_t *)&lifr->lifr_addr;
8787 			*sin6 = sin6_null;
8788 			sin6->sin6_family = AF_INET6;
8789 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8790 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8791 			    &ipif->ipif_v6net_mask);
8792 		} else {
8793 			sin = (sin_t *)&lifr->lifr_addr;
8794 			*sin = sin_null;
8795 			sin->sin_family = AF_INET;
8796 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8797 			lifr->lifr_addrlen = ip_mask_to_plen(
8798 			    ipif->ipif_net_mask);
8799 		}
8800 		lifr++;
8801 	}
8802 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8803 	rw_exit(&ipst->ips_ill_g_lock);
8804 	ipif_refrele(orig_ipif);
8805 	mp1->b_wptr = (uchar_t *)lifr;
8806 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8807 
8808 	return (0);
8809 }
8810 
8811 /* ARGSUSED */
8812 int
8813 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8814     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8815 {
8816 	mblk_t *mp1;
8817 	int	list;
8818 	ill_t	*ill;
8819 	ipif_t	*ipif;
8820 	int	flags;
8821 	int	numlifs = 0;
8822 	size_t	lifc_bufsize;
8823 	struct	lifreq *lifr;
8824 	sa_family_t	family;
8825 	struct	sockaddr_in	*sin;
8826 	struct	sockaddr_in6	*sin6;
8827 	ill_walk_context_t	ctx;
8828 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8829 	int32_t	lifclen;
8830 	zoneid_t zoneid;
8831 	STRUCT_HANDLE(lifconf, lifc);
8832 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8833 
8834 	ip1dbg(("ip_sioctl_get_lifconf"));
8835 
8836 	ASSERT(q->q_next == NULL);
8837 
8838 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8839 
8840 	/* Existence verified in ip_wput_nondata */
8841 	mp1 = mp->b_cont->b_cont;
8842 
8843 	/*
8844 	 * An extended version of SIOCGIFCONF that takes an
8845 	 * additional address family and flags field.
8846 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8847 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8848 	 * interfaces are omitted.
8849 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8850 	 * unless LIFC_TEMPORARY is specified.
8851 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8852 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8853 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8854 	 * has priority over LIFC_NOXMIT.
8855 	 */
8856 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8857 
8858 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8859 		return (EINVAL);
8860 
8861 	/*
8862 	 * Must be (better be!) continuation of a TRANSPARENT
8863 	 * IOCTL.  We just copied in the lifconf structure.
8864 	 */
8865 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8866 
8867 	family = STRUCT_FGET(lifc, lifc_family);
8868 	flags = STRUCT_FGET(lifc, lifc_flags);
8869 
8870 	switch (family) {
8871 	case AF_UNSPEC:
8872 		/*
8873 		 * walk all ILL's.
8874 		 */
8875 		list = MAX_G_HEADS;
8876 		break;
8877 	case AF_INET:
8878 		/*
8879 		 * walk only IPV4 ILL's.
8880 		 */
8881 		list = IP_V4_G_HEAD;
8882 		break;
8883 	case AF_INET6:
8884 		/*
8885 		 * walk only IPV6 ILL's.
8886 		 */
8887 		list = IP_V6_G_HEAD;
8888 		break;
8889 	default:
8890 		return (EAFNOSUPPORT);
8891 	}
8892 
8893 	/*
8894 	 * Allocate a buffer to hold requested information.
8895 	 *
8896 	 * If lifc_len is larger than what is needed, we only
8897 	 * allocate what we will use.
8898 	 *
8899 	 * If lifc_len is smaller than what is needed, return
8900 	 * EINVAL.
8901 	 */
8902 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8903 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8904 	lifclen = STRUCT_FGET(lifc, lifc_len);
8905 	if (lifc_bufsize > lifclen) {
8906 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8907 			return (EINVAL);
8908 		else
8909 			lifc_bufsize = lifclen;
8910 	}
8911 
8912 	mp1 = mi_copyout_alloc(q, mp,
8913 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8914 	if (mp1 == NULL)
8915 		return (ENOMEM);
8916 
8917 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8918 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8919 
8920 	lifr = (struct lifreq *)mp1->b_rptr;
8921 
8922 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8923 	ill = ill_first(list, list, &ctx, ipst);
8924 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8925 		for (ipif = ill->ill_ipif; ipif != NULL;
8926 		    ipif = ipif->ipif_next) {
8927 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8928 			    !(flags & LIFC_NOXMIT))
8929 				continue;
8930 
8931 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8932 			    !(flags & LIFC_TEMPORARY))
8933 				continue;
8934 
8935 			if (((ipif->ipif_flags &
8936 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8937 			    IPIF_DEPRECATED)) ||
8938 			    IS_LOOPBACK(ill) ||
8939 			    !(ipif->ipif_flags & IPIF_UP)) &&
8940 			    (flags & LIFC_EXTERNAL_SOURCE))
8941 				continue;
8942 
8943 			if (zoneid != ipif->ipif_zoneid &&
8944 			    ipif->ipif_zoneid != ALL_ZONES &&
8945 			    (zoneid != GLOBAL_ZONEID ||
8946 			    !(flags & LIFC_ALLZONES)))
8947 				continue;
8948 
8949 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8950 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8951 					rw_exit(&ipst->ips_ill_g_lock);
8952 					return (EINVAL);
8953 				} else {
8954 					goto lif_copydone;
8955 				}
8956 			}
8957 
8958 			ipif_get_name(ipif, lifr->lifr_name,
8959 			    sizeof (lifr->lifr_name));
8960 			if (ipif->ipif_isv6) {
8961 				sin6 = (sin6_t *)&lifr->lifr_addr;
8962 				*sin6 = sin6_null;
8963 				sin6->sin6_family = AF_INET6;
8964 				sin6->sin6_addr =
8965 				    ipif->ipif_v6lcl_addr;
8966 				lifr->lifr_addrlen =
8967 				    ip_mask_to_plen_v6(
8968 				    &ipif->ipif_v6net_mask);
8969 			} else {
8970 				sin = (sin_t *)&lifr->lifr_addr;
8971 				*sin = sin_null;
8972 				sin->sin_family = AF_INET;
8973 				sin->sin_addr.s_addr =
8974 				    ipif->ipif_lcl_addr;
8975 				lifr->lifr_addrlen =
8976 				    ip_mask_to_plen(
8977 				    ipif->ipif_net_mask);
8978 			}
8979 			lifr++;
8980 		}
8981 	}
8982 lif_copydone:
8983 	rw_exit(&ipst->ips_ill_g_lock);
8984 
8985 	mp1->b_wptr = (uchar_t *)lifr;
8986 	if (STRUCT_BUF(lifc) != NULL) {
8987 		STRUCT_FSET(lifc, lifc_len,
8988 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8989 	}
8990 	return (0);
8991 }
8992 
8993 /* ARGSUSED */
8994 int
8995 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8996     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8997 {
8998 	ip_stack_t	*ipst;
8999 
9000 	if (q->q_next == NULL)
9001 		ipst = CONNQ_TO_IPST(q);
9002 	else
9003 		ipst = ILLQ_TO_IPST(q);
9004 
9005 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9006 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9007 	return (0);
9008 }
9009 
9010 static void
9011 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9012 {
9013 	ip6_asp_t *table;
9014 	size_t table_size;
9015 	mblk_t *data_mp;
9016 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9017 	ip_stack_t	*ipst;
9018 
9019 	if (q->q_next == NULL)
9020 		ipst = CONNQ_TO_IPST(q);
9021 	else
9022 		ipst = ILLQ_TO_IPST(q);
9023 
9024 	/* These two ioctls are I_STR only */
9025 	if (iocp->ioc_count == TRANSPARENT) {
9026 		miocnak(q, mp, 0, EINVAL);
9027 		return;
9028 	}
9029 
9030 	data_mp = mp->b_cont;
9031 	if (data_mp == NULL) {
9032 		/* The user passed us a NULL argument */
9033 		table = NULL;
9034 		table_size = iocp->ioc_count;
9035 	} else {
9036 		/*
9037 		 * The user provided a table.  The stream head
9038 		 * may have copied in the user data in chunks,
9039 		 * so make sure everything is pulled up
9040 		 * properly.
9041 		 */
9042 		if (MBLKL(data_mp) < iocp->ioc_count) {
9043 			mblk_t *new_data_mp;
9044 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9045 			    NULL) {
9046 				miocnak(q, mp, 0, ENOMEM);
9047 				return;
9048 			}
9049 			freemsg(data_mp);
9050 			data_mp = new_data_mp;
9051 			mp->b_cont = data_mp;
9052 		}
9053 		table = (ip6_asp_t *)data_mp->b_rptr;
9054 		table_size = iocp->ioc_count;
9055 	}
9056 
9057 	switch (iocp->ioc_cmd) {
9058 	case SIOCGIP6ADDRPOLICY:
9059 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9060 		if (iocp->ioc_rval == -1)
9061 			iocp->ioc_error = EINVAL;
9062 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9063 		else if (table != NULL &&
9064 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9065 			ip6_asp_t *src = table;
9066 			ip6_asp32_t *dst = (void *)table;
9067 			int count = table_size / sizeof (ip6_asp_t);
9068 			int i;
9069 
9070 			/*
9071 			 * We need to do an in-place shrink of the array
9072 			 * to match the alignment attributes of the
9073 			 * 32-bit ABI looking at it.
9074 			 */
9075 			/* LINTED: logical expression always true: op "||" */
9076 			ASSERT(sizeof (*src) > sizeof (*dst));
9077 			for (i = 1; i < count; i++)
9078 				bcopy(src + i, dst + i, sizeof (*dst));
9079 		}
9080 #endif
9081 		break;
9082 
9083 	case SIOCSIP6ADDRPOLICY:
9084 		ASSERT(mp->b_prev == NULL);
9085 		mp->b_prev = (void *)q;
9086 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9087 		/*
9088 		 * We pass in the datamodel here so that the ip6_asp_replace()
9089 		 * routine can handle converting from 32-bit to native formats
9090 		 * where necessary.
9091 		 *
9092 		 * A better way to handle this might be to convert the inbound
9093 		 * data structure here, and hang it off a new 'mp'; thus the
9094 		 * ip6_asp_replace() logic would always be dealing with native
9095 		 * format data structures..
9096 		 *
9097 		 * (An even simpler way to handle these ioctls is to just
9098 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9099 		 * and just recompile everything that depends on it.)
9100 		 */
9101 #endif
9102 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9103 		    iocp->ioc_flag & IOC_MODELS);
9104 		return;
9105 	}
9106 
9107 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9108 	qreply(q, mp);
9109 }
9110 
9111 static void
9112 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9113 {
9114 	mblk_t 		*data_mp;
9115 	struct dstinforeq	*dir;
9116 	uint8_t		*end, *cur;
9117 	in6_addr_t	*daddr, *saddr;
9118 	ipaddr_t	v4daddr;
9119 	ire_t		*ire;
9120 	char		*slabel, *dlabel;
9121 	boolean_t	isipv4;
9122 	int		match_ire;
9123 	ill_t		*dst_ill;
9124 	ipif_t		*src_ipif, *ire_ipif;
9125 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9126 	zoneid_t	zoneid;
9127 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9128 
9129 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9130 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9131 
9132 	/*
9133 	 * This ioctl is I_STR only, and must have a
9134 	 * data mblk following the M_IOCTL mblk.
9135 	 */
9136 	data_mp = mp->b_cont;
9137 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9138 		miocnak(q, mp, 0, EINVAL);
9139 		return;
9140 	}
9141 
9142 	if (MBLKL(data_mp) < iocp->ioc_count) {
9143 		mblk_t *new_data_mp;
9144 
9145 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9146 			miocnak(q, mp, 0, ENOMEM);
9147 			return;
9148 		}
9149 		freemsg(data_mp);
9150 		data_mp = new_data_mp;
9151 		mp->b_cont = data_mp;
9152 	}
9153 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9154 
9155 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9156 	    end - cur >= sizeof (struct dstinforeq);
9157 	    cur += sizeof (struct dstinforeq)) {
9158 		dir = (struct dstinforeq *)cur;
9159 		daddr = &dir->dir_daddr;
9160 		saddr = &dir->dir_saddr;
9161 
9162 		/*
9163 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9164 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9165 		 * and ipif_select_source[_v6]() do not.
9166 		 */
9167 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9168 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9169 
9170 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9171 		if (isipv4) {
9172 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9173 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9174 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9175 		} else {
9176 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9177 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9178 		}
9179 		if (ire == NULL) {
9180 			dir->dir_dreachable = 0;
9181 
9182 			/* move on to next dst addr */
9183 			continue;
9184 		}
9185 		dir->dir_dreachable = 1;
9186 
9187 		ire_ipif = ire->ire_ipif;
9188 		if (ire_ipif == NULL)
9189 			goto next_dst;
9190 
9191 		/*
9192 		 * We expect to get back an interface ire or a
9193 		 * gateway ire cache entry.  For both types, the
9194 		 * output interface is ire_ipif->ipif_ill.
9195 		 */
9196 		dst_ill = ire_ipif->ipif_ill;
9197 		dir->dir_dmactype = dst_ill->ill_mactype;
9198 
9199 		if (isipv4) {
9200 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9201 		} else {
9202 			src_ipif = ipif_select_source_v6(dst_ill,
9203 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9204 			    zoneid);
9205 		}
9206 		if (src_ipif == NULL)
9207 			goto next_dst;
9208 
9209 		*saddr = src_ipif->ipif_v6lcl_addr;
9210 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9211 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9212 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9213 		dir->dir_sdeprecated =
9214 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9215 		ipif_refrele(src_ipif);
9216 next_dst:
9217 		ire_refrele(ire);
9218 	}
9219 	miocack(q, mp, iocp->ioc_count, 0);
9220 }
9221 
9222 
9223 /*
9224  * Check if this is an address assigned to this machine.
9225  * Skips interfaces that are down by using ire checks.
9226  * Translates mapped addresses to v4 addresses and then
9227  * treats them as such, returning true if the v4 address
9228  * associated with this mapped address is configured.
9229  * Note: Applications will have to be careful what they do
9230  * with the response; use of mapped addresses limits
9231  * what can be done with the socket, especially with
9232  * respect to socket options and ioctls - neither IPv4
9233  * options nor IPv6 sticky options/ancillary data options
9234  * may be used.
9235  */
9236 /* ARGSUSED */
9237 int
9238 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9239     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9240 {
9241 	struct sioc_addrreq *sia;
9242 	sin_t *sin;
9243 	ire_t *ire;
9244 	mblk_t *mp1;
9245 	zoneid_t zoneid;
9246 	ip_stack_t	*ipst;
9247 
9248 	ip1dbg(("ip_sioctl_tmyaddr"));
9249 
9250 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9251 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9252 	ipst = CONNQ_TO_IPST(q);
9253 
9254 	/* Existence verified in ip_wput_nondata */
9255 	mp1 = mp->b_cont->b_cont;
9256 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9257 	sin = (sin_t *)&sia->sa_addr;
9258 	switch (sin->sin_family) {
9259 	case AF_INET6: {
9260 		sin6_t *sin6 = (sin6_t *)sin;
9261 
9262 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9263 			ipaddr_t v4_addr;
9264 
9265 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9266 			    v4_addr);
9267 			ire = ire_ctable_lookup(v4_addr, 0,
9268 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9269 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9270 		} else {
9271 			in6_addr_t v6addr;
9272 
9273 			v6addr = sin6->sin6_addr;
9274 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9275 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9276 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9277 		}
9278 		break;
9279 	}
9280 	case AF_INET: {
9281 		ipaddr_t v4addr;
9282 
9283 		v4addr = sin->sin_addr.s_addr;
9284 		ire = ire_ctable_lookup(v4addr, 0,
9285 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9286 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9287 		break;
9288 	}
9289 	default:
9290 		return (EAFNOSUPPORT);
9291 	}
9292 	if (ire != NULL) {
9293 		sia->sa_res = 1;
9294 		ire_refrele(ire);
9295 	} else {
9296 		sia->sa_res = 0;
9297 	}
9298 	return (0);
9299 }
9300 
9301 /*
9302  * Check if this is an address assigned on-link i.e. neighbor,
9303  * and makes sure it's reachable from the current zone.
9304  * Returns true for my addresses as well.
9305  * Translates mapped addresses to v4 addresses and then
9306  * treats them as such, returning true if the v4 address
9307  * associated with this mapped address is configured.
9308  * Note: Applications will have to be careful what they do
9309  * with the response; use of mapped addresses limits
9310  * what can be done with the socket, especially with
9311  * respect to socket options and ioctls - neither IPv4
9312  * options nor IPv6 sticky options/ancillary data options
9313  * may be used.
9314  */
9315 /* ARGSUSED */
9316 int
9317 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9318     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9319 {
9320 	struct sioc_addrreq *sia;
9321 	sin_t *sin;
9322 	mblk_t	*mp1;
9323 	ire_t *ire = NULL;
9324 	zoneid_t zoneid;
9325 	ip_stack_t	*ipst;
9326 
9327 	ip1dbg(("ip_sioctl_tonlink"));
9328 
9329 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9330 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9331 	ipst = CONNQ_TO_IPST(q);
9332 
9333 	/* Existence verified in ip_wput_nondata */
9334 	mp1 = mp->b_cont->b_cont;
9335 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9336 	sin = (sin_t *)&sia->sa_addr;
9337 
9338 	/*
9339 	 * Match addresses with a zero gateway field to avoid
9340 	 * routes going through a router.
9341 	 * Exclude broadcast and multicast addresses.
9342 	 */
9343 	switch (sin->sin_family) {
9344 	case AF_INET6: {
9345 		sin6_t *sin6 = (sin6_t *)sin;
9346 
9347 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9348 			ipaddr_t v4_addr;
9349 
9350 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9351 			    v4_addr);
9352 			if (!CLASSD(v4_addr)) {
9353 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9354 				    NULL, NULL, zoneid, NULL,
9355 				    MATCH_IRE_GW, ipst);
9356 			}
9357 		} else {
9358 			in6_addr_t v6addr;
9359 			in6_addr_t v6gw;
9360 
9361 			v6addr = sin6->sin6_addr;
9362 			v6gw = ipv6_all_zeros;
9363 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9364 				ire = ire_route_lookup_v6(&v6addr, 0,
9365 				    &v6gw, 0, NULL, NULL, zoneid,
9366 				    NULL, MATCH_IRE_GW, ipst);
9367 			}
9368 		}
9369 		break;
9370 	}
9371 	case AF_INET: {
9372 		ipaddr_t v4addr;
9373 
9374 		v4addr = sin->sin_addr.s_addr;
9375 		if (!CLASSD(v4addr)) {
9376 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9377 			    NULL, NULL, zoneid, NULL,
9378 			    MATCH_IRE_GW, ipst);
9379 		}
9380 		break;
9381 	}
9382 	default:
9383 		return (EAFNOSUPPORT);
9384 	}
9385 	sia->sa_res = 0;
9386 	if (ire != NULL) {
9387 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9388 		    IRE_LOCAL|IRE_LOOPBACK)) {
9389 			sia->sa_res = 1;
9390 		}
9391 		ire_refrele(ire);
9392 	}
9393 	return (0);
9394 }
9395 
9396 /*
9397  * TBD: implement when kernel maintaines a list of site prefixes.
9398  */
9399 /* ARGSUSED */
9400 int
9401 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9402     ip_ioctl_cmd_t *ipip, void *ifreq)
9403 {
9404 	return (ENXIO);
9405 }
9406 
9407 /* ARGSUSED */
9408 int
9409 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9410     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9411 {
9412 	ill_t  		*ill;
9413 	mblk_t		*mp1;
9414 	conn_t		*connp;
9415 	boolean_t	success;
9416 
9417 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9418 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9419 	/* ioctl comes down on an conn */
9420 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9421 	connp = Q_TO_CONN(q);
9422 
9423 	mp->b_datap->db_type = M_IOCTL;
9424 
9425 	/*
9426 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9427 	 * The original mp contains contaminated b_next values due to 'mi',
9428 	 * which is needed to do the mi_copy_done. Unfortunately if we
9429 	 * send down the original mblk itself and if we are popped due to an
9430 	 * an unplumb before the response comes back from tunnel,
9431 	 * the streamhead (which does a freemsg) will see this contaminated
9432 	 * message and the assertion in freemsg about non-null b_next/b_prev
9433 	 * will panic a DEBUG kernel.
9434 	 */
9435 	mp1 = copymsg(mp);
9436 	if (mp1 == NULL)
9437 		return (ENOMEM);
9438 
9439 	ill = ipif->ipif_ill;
9440 	mutex_enter(&connp->conn_lock);
9441 	mutex_enter(&ill->ill_lock);
9442 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9443 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9444 		    mp, 0);
9445 	} else {
9446 		success = ill_pending_mp_add(ill, connp, mp);
9447 	}
9448 	mutex_exit(&ill->ill_lock);
9449 	mutex_exit(&connp->conn_lock);
9450 
9451 	if (success) {
9452 		ip1dbg(("sending down tunparam request "));
9453 		putnext(ill->ill_wq, mp1);
9454 		return (EINPROGRESS);
9455 	} else {
9456 		/* The conn has started closing */
9457 		freemsg(mp1);
9458 		return (EINTR);
9459 	}
9460 }
9461 
9462 /*
9463  * ARP IOCTLs.
9464  * How does IP get in the business of fronting ARP configuration/queries?
9465  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9466  * are by tradition passed in through a datagram socket.  That lands in IP.
9467  * As it happens, this is just as well since the interface is quite crude in
9468  * that it passes in no information about protocol or hardware types, or
9469  * interface association.  After making the protocol assumption, IP is in
9470  * the position to look up the name of the ILL, which ARP will need, and
9471  * format a request that can be handled by ARP.  The request is passed up
9472  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9473  * back a response.  ARP supports its own set of more general IOCTLs, in
9474  * case anyone is interested.
9475  */
9476 /* ARGSUSED */
9477 int
9478 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9479     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9480 {
9481 	mblk_t *mp1;
9482 	mblk_t *mp2;
9483 	mblk_t *pending_mp;
9484 	ipaddr_t ipaddr;
9485 	area_t *area;
9486 	struct iocblk *iocp;
9487 	conn_t *connp;
9488 	struct arpreq *ar;
9489 	struct xarpreq *xar;
9490 	int flags, alength;
9491 	char *lladdr;
9492 	ip_stack_t	*ipst;
9493 	ill_t *ill = ipif->ipif_ill;
9494 	boolean_t if_arp_ioctl = B_FALSE;
9495 
9496 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9497 	connp = Q_TO_CONN(q);
9498 	ipst = connp->conn_netstack->netstack_ip;
9499 
9500 	if (ipip->ipi_cmd_type == XARP_CMD) {
9501 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9502 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9503 		ar = NULL;
9504 
9505 		flags = xar->xarp_flags;
9506 		lladdr = LLADDR(&xar->xarp_ha);
9507 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9508 		/*
9509 		 * Validate against user's link layer address length
9510 		 * input and name and addr length limits.
9511 		 */
9512 		alength = ill->ill_phys_addr_length;
9513 		if (ipip->ipi_cmd == SIOCSXARP) {
9514 			if (alength != xar->xarp_ha.sdl_alen ||
9515 			    (alength + xar->xarp_ha.sdl_nlen >
9516 			    sizeof (xar->xarp_ha.sdl_data)))
9517 				return (EINVAL);
9518 		}
9519 	} else {
9520 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9521 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9522 		xar = NULL;
9523 
9524 		flags = ar->arp_flags;
9525 		lladdr = ar->arp_ha.sa_data;
9526 		/*
9527 		 * Theoretically, the sa_family could tell us what link
9528 		 * layer type this operation is trying to deal with. By
9529 		 * common usage AF_UNSPEC means ethernet. We'll assume
9530 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9531 		 * for now. Our new SIOC*XARP ioctls can be used more
9532 		 * generally.
9533 		 *
9534 		 * If the underlying media happens to have a non 6 byte
9535 		 * address, arp module will fail set/get, but the del
9536 		 * operation will succeed.
9537 		 */
9538 		alength = 6;
9539 		if ((ipip->ipi_cmd != SIOCDARP) &&
9540 		    (alength != ill->ill_phys_addr_length)) {
9541 			return (EINVAL);
9542 		}
9543 	}
9544 
9545 	/*
9546 	 * We are going to pass up to ARP a packet chain that looks
9547 	 * like:
9548 	 *
9549 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9550 	 *
9551 	 * Get a copy of the original IOCTL mblk to head the chain,
9552 	 * to be sent up (in mp1). Also get another copy to store
9553 	 * in the ill_pending_mp list, for matching the response
9554 	 * when it comes back from ARP.
9555 	 */
9556 	mp1 = copyb(mp);
9557 	pending_mp = copymsg(mp);
9558 	if (mp1 == NULL || pending_mp == NULL) {
9559 		if (mp1 != NULL)
9560 			freeb(mp1);
9561 		if (pending_mp != NULL)
9562 			inet_freemsg(pending_mp);
9563 		return (ENOMEM);
9564 	}
9565 
9566 	ipaddr = sin->sin_addr.s_addr;
9567 
9568 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9569 	    (caddr_t)&ipaddr);
9570 	if (mp2 == NULL) {
9571 		freeb(mp1);
9572 		inet_freemsg(pending_mp);
9573 		return (ENOMEM);
9574 	}
9575 	/* Put together the chain. */
9576 	mp1->b_cont = mp2;
9577 	mp1->b_datap->db_type = M_IOCTL;
9578 	mp2->b_cont = mp;
9579 	mp2->b_datap->db_type = M_DATA;
9580 
9581 	iocp = (struct iocblk *)mp1->b_rptr;
9582 
9583 	/*
9584 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9585 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9586 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9587 	 * ioc_count field; set ioc_count to be correct.
9588 	 */
9589 	iocp->ioc_count = MBLKL(mp1->b_cont);
9590 
9591 	/*
9592 	 * Set the proper command in the ARP message.
9593 	 * Convert the SIOC{G|S|D}ARP calls into our
9594 	 * AR_ENTRY_xxx calls.
9595 	 */
9596 	area = (area_t *)mp2->b_rptr;
9597 	switch (iocp->ioc_cmd) {
9598 	case SIOCDARP:
9599 	case SIOCDXARP:
9600 		/*
9601 		 * We defer deleting the corresponding IRE until
9602 		 * we return from arp.
9603 		 */
9604 		area->area_cmd = AR_ENTRY_DELETE;
9605 		area->area_proto_mask_offset = 0;
9606 		break;
9607 	case SIOCGARP:
9608 	case SIOCGXARP:
9609 		area->area_cmd = AR_ENTRY_SQUERY;
9610 		area->area_proto_mask_offset = 0;
9611 		break;
9612 	case SIOCSARP:
9613 	case SIOCSXARP:
9614 		/*
9615 		 * Delete the corresponding ire to make sure IP will
9616 		 * pick up any change from arp.
9617 		 */
9618 		if (!if_arp_ioctl) {
9619 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9620 		} else {
9621 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9622 			if (ipif != NULL) {
9623 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9624 				    ipst);
9625 				ipif_refrele(ipif);
9626 			}
9627 		}
9628 		break;
9629 	}
9630 	iocp->ioc_cmd = area->area_cmd;
9631 
9632 	/*
9633 	 * Fill in the rest of the ARP operation fields.
9634 	 */
9635 	area->area_hw_addr_length = alength;
9636 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9637 
9638 	/* Translate the flags. */
9639 	if (flags & ATF_PERM)
9640 		area->area_flags |= ACE_F_PERMANENT;
9641 	if (flags & ATF_PUBL)
9642 		area->area_flags |= ACE_F_PUBLISH;
9643 	if (flags & ATF_AUTHORITY)
9644 		area->area_flags |= ACE_F_AUTHORITY;
9645 
9646 	/*
9647 	 * Before sending 'mp' to ARP, we have to clear the b_next
9648 	 * and b_prev. Otherwise if STREAMS encounters such a message
9649 	 * in freemsg(), (because ARP can close any time) it can cause
9650 	 * a panic. But mi code needs the b_next and b_prev values of
9651 	 * mp->b_cont, to complete the ioctl. So we store it here
9652 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9653 	 * when the response comes down from ARP.
9654 	 */
9655 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9656 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9657 	mp->b_cont->b_next = NULL;
9658 	mp->b_cont->b_prev = NULL;
9659 
9660 	mutex_enter(&connp->conn_lock);
9661 	mutex_enter(&ill->ill_lock);
9662 	/* conn has not yet started closing, hence this can't fail */
9663 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9664 	mutex_exit(&ill->ill_lock);
9665 	mutex_exit(&connp->conn_lock);
9666 
9667 	/*
9668 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9669 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9670 	 */
9671 	putnext(ill->ill_rq, mp1);
9672 	return (EINPROGRESS);
9673 }
9674 
9675 /*
9676  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9677  * the associated sin and refhold and return the associated ipif via `ci'.
9678  */
9679 int
9680 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9681     cmd_info_t *ci, ipsq_func_t func)
9682 {
9683 	mblk_t	*mp1;
9684 	int	err;
9685 	sin_t	*sin;
9686 	conn_t	*connp;
9687 	ipif_t	*ipif;
9688 	ire_t	*ire = NULL;
9689 	ill_t	*ill = NULL;
9690 	boolean_t exists;
9691 	ip_stack_t *ipst;
9692 	struct arpreq *ar;
9693 	struct xarpreq *xar;
9694 	struct sockaddr_dl *sdl;
9695 
9696 	/* ioctl comes down on a conn */
9697 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9698 	connp = Q_TO_CONN(q);
9699 	if (connp->conn_af_isv6)
9700 		return (ENXIO);
9701 
9702 	ipst = connp->conn_netstack->netstack_ip;
9703 
9704 	/* Verified in ip_wput_nondata */
9705 	mp1 = mp->b_cont->b_cont;
9706 
9707 	if (ipip->ipi_cmd_type == XARP_CMD) {
9708 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9709 		xar = (struct xarpreq *)mp1->b_rptr;
9710 		sin = (sin_t *)&xar->xarp_pa;
9711 		sdl = &xar->xarp_ha;
9712 
9713 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9714 			return (ENXIO);
9715 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9716 			return (EINVAL);
9717 	} else {
9718 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9719 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9720 		ar = (struct arpreq *)mp1->b_rptr;
9721 		sin = (sin_t *)&ar->arp_pa;
9722 	}
9723 
9724 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9725 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9726 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9727 		    mp, func, &err, ipst);
9728 		if (ipif == NULL)
9729 			return (err);
9730 		if (ipif->ipif_id != 0 ||
9731 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9732 			ipif_refrele(ipif);
9733 			return (ENXIO);
9734 		}
9735 	} else {
9736 		/*
9737 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9738 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9739 		 * case, a simple forwarding table lookup will return the
9740 		 * IRE_IF_RESOLVER for the first interface in the group, which
9741 		 * might not be the interface on which the requested IP
9742 		 * address was resolved due to the ill selection algorithm
9743 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9744 		 * lookup first: if the IRE cache entry for the IP address is
9745 		 * still there, it will contain the ill pointer for the right
9746 		 * interface, so we use that. If the cache entry has been
9747 		 * flushed, we fall back to the forwarding table lookup. This
9748 		 * should be rare enough since IRE cache entries have a longer
9749 		 * life expectancy than ARP cache entries.
9750 		 */
9751 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9752 		    ipst);
9753 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9754 		    ((ill = ire_to_ill(ire)) == NULL) ||
9755 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9756 			if (ire != NULL)
9757 				ire_refrele(ire);
9758 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9759 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9760 			    NULL, MATCH_IRE_TYPE, ipst);
9761 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9762 
9763 				if (ire != NULL)
9764 					ire_refrele(ire);
9765 				return (ENXIO);
9766 			}
9767 		}
9768 		ASSERT(ire != NULL && ill != NULL);
9769 		ipif = ill->ill_ipif;
9770 		ipif_refhold(ipif);
9771 		ire_refrele(ire);
9772 	}
9773 	ci->ci_sin = sin;
9774 	ci->ci_ipif = ipif;
9775 	return (0);
9776 }
9777 
9778 /*
9779  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9780  * atomically set/clear the muxids. Also complete the ioctl by acking or
9781  * naking it.  Note that the code is structured such that the link type,
9782  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9783  * its clones use the persistent link, while pppd(1M) and perhaps many
9784  * other daemons may use non-persistent link.  When combined with some
9785  * ill_t states, linking and unlinking lower streams may be used as
9786  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9787  */
9788 /* ARGSUSED */
9789 void
9790 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9791 {
9792 	mblk_t		*mp1, *mp2;
9793 	struct linkblk	*li;
9794 	struct ipmx_s	*ipmxp;
9795 	ill_t		*ill;
9796 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9797 	int		err = 0;
9798 	boolean_t	entered_ipsq = B_FALSE;
9799 	boolean_t	islink;
9800 	ip_stack_t	*ipst;
9801 
9802 	if (CONN_Q(q))
9803 		ipst = CONNQ_TO_IPST(q);
9804 	else
9805 		ipst = ILLQ_TO_IPST(q);
9806 
9807 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9808 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9809 
9810 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9811 
9812 	mp1 = mp->b_cont;	/* This is the linkblk info */
9813 	li = (struct linkblk *)mp1->b_rptr;
9814 
9815 	/*
9816 	 * ARP has added this special mblk, and the utility is asking us
9817 	 * to perform consistency checks, and also atomically set the
9818 	 * muxid. Ifconfig is an example.  It achieves this by using
9819 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9820 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9821 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9822 	 * and other comments in this routine for more details.
9823 	 */
9824 	mp2 = mp1->b_cont;	/* This is added by ARP */
9825 
9826 	/*
9827 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9828 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9829 	 * get the special mblk above.  For backward compatibility, we
9830 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9831 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9832 	 * not atomic, and can leave the streams unplumbable if the utility
9833 	 * is interrupted before it does the SIOCSLIFMUXID.
9834 	 */
9835 	if (mp2 == NULL) {
9836 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9837 		if (err == EINPROGRESS)
9838 			return;
9839 		goto done;
9840 	}
9841 
9842 	/*
9843 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9844 	 * ARP has appended this last mblk to tell us whether the lower stream
9845 	 * is an arp-dev stream or an IP module stream.
9846 	 */
9847 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9848 	if (ipmxp->ipmx_arpdev_stream) {
9849 		/*
9850 		 * The lower stream is the arp-dev stream.
9851 		 */
9852 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9853 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9854 		if (ill == NULL) {
9855 			if (err == EINPROGRESS)
9856 				return;
9857 			err = EINVAL;
9858 			goto done;
9859 		}
9860 
9861 		if (ipsq == NULL) {
9862 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9863 			    NEW_OP, B_TRUE);
9864 			if (ipsq == NULL) {
9865 				ill_refrele(ill);
9866 				return;
9867 			}
9868 			entered_ipsq = B_TRUE;
9869 		}
9870 		ASSERT(IAM_WRITER_ILL(ill));
9871 		ill_refrele(ill);
9872 
9873 		/*
9874 		 * To ensure consistency between IP and ARP, the following
9875 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9876 		 * This is because the muxid's are stored in the IP stream on
9877 		 * the ill.
9878 		 *
9879 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9880 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9881 		 * not yet plinked, and it also checks that the corresponding
9882 		 * IP stream is already plinked.
9883 		 *
9884 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9885 		 * punlinking the IP stream. IP does not allow punlink of the
9886 		 * IP stream unless the arp stream has been punlinked.
9887 		 */
9888 		if ((islink &&
9889 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9890 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9891 			err = EINVAL;
9892 			goto done;
9893 		}
9894 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9895 	} else {
9896 		/*
9897 		 * The lower stream is probably an IP module stream.  Do
9898 		 * consistency checking.
9899 		 */
9900 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9901 		if (err == EINPROGRESS)
9902 			return;
9903 	}
9904 done:
9905 	if (err == 0)
9906 		miocack(q, mp, 0, 0);
9907 	else
9908 		miocnak(q, mp, 0, err);
9909 
9910 	/* Conn was refheld in ip_sioctl_copyin_setup */
9911 	if (CONN_Q(q))
9912 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9913 	if (entered_ipsq)
9914 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9915 }
9916 
9917 /*
9918  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9919  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9920  * module stream).  If `doconsist' is set, then do the extended consistency
9921  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9922  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9923  * an error code on failure.
9924  */
9925 static int
9926 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9927     struct linkblk *li, boolean_t doconsist)
9928 {
9929 	ill_t  		*ill;
9930 	queue_t		*ipwq, *dwq;
9931 	const char	*name;
9932 	struct qinit	*qinfo;
9933 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9934 	boolean_t	entered_ipsq = B_FALSE;
9935 
9936 	/*
9937 	 * Walk the lower stream to verify it's the IP module stream.
9938 	 * The IP module is identified by its name, wput function,
9939 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9940 	 * (li->l_qbot) will not vanish until this ioctl completes.
9941 	 */
9942 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9943 		qinfo = ipwq->q_qinfo;
9944 		name = qinfo->qi_minfo->mi_idname;
9945 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9946 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9947 			break;
9948 		}
9949 	}
9950 
9951 	/*
9952 	 * If this isn't an IP module stream, bail.
9953 	 */
9954 	if (ipwq == NULL)
9955 		return (0);
9956 
9957 	ill = ipwq->q_ptr;
9958 	ASSERT(ill != NULL);
9959 
9960 	if (ipsq == NULL) {
9961 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9962 		    NEW_OP, B_TRUE);
9963 		if (ipsq == NULL)
9964 			return (EINPROGRESS);
9965 		entered_ipsq = B_TRUE;
9966 	}
9967 	ASSERT(IAM_WRITER_ILL(ill));
9968 
9969 	if (doconsist) {
9970 		/*
9971 		 * Consistency checking requires that I_{P}LINK occurs
9972 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9973 		 * occurs prior to clearing ill_arp_muxid.
9974 		 */
9975 		if ((islink && ill->ill_ip_muxid != 0) ||
9976 		    (!islink && ill->ill_arp_muxid != 0)) {
9977 			if (entered_ipsq)
9978 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
9979 			return (EINVAL);
9980 		}
9981 	}
9982 
9983 	/*
9984 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9985 	 * the read queue of the module immediately below IP in the ill.
9986 	 * These are used during the capability negotiation below.
9987 	 */
9988 	ill->ill_lmod_rq = NULL;
9989 	ill->ill_lmod_cnt = 0;
9990 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9991 		ill->ill_lmod_rq = RD(dwq);
9992 		for (; dwq != NULL; dwq = dwq->q_next)
9993 			ill->ill_lmod_cnt++;
9994 	}
9995 
9996 	if (doconsist)
9997 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9998 
9999 	/*
10000 	 * If there's at least one up ipif on this ill, then we're bound to
10001 	 * the underlying driver via DLPI.  In that case, renegotiate
10002 	 * capabilities to account for any possible change in modules
10003 	 * interposed between IP and the driver.
10004 	 */
10005 	if (ill->ill_ipif_up_count > 0) {
10006 		if (islink)
10007 			ill_capability_probe(ill);
10008 		else
10009 			ill_capability_reset(ill);
10010 	}
10011 
10012 	if (entered_ipsq)
10013 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10014 
10015 	return (0);
10016 }
10017 
10018 /*
10019  * Search the ioctl command in the ioctl tables and return a pointer
10020  * to the ioctl command information. The ioctl command tables are
10021  * static and fully populated at compile time.
10022  */
10023 ip_ioctl_cmd_t *
10024 ip_sioctl_lookup(int ioc_cmd)
10025 {
10026 	int index;
10027 	ip_ioctl_cmd_t *ipip;
10028 	ip_ioctl_cmd_t *ipip_end;
10029 
10030 	if (ioc_cmd == IPI_DONTCARE)
10031 		return (NULL);
10032 
10033 	/*
10034 	 * Do a 2 step search. First search the indexed table
10035 	 * based on the least significant byte of the ioctl cmd.
10036 	 * If we don't find a match, then search the misc table
10037 	 * serially.
10038 	 */
10039 	index = ioc_cmd & 0xFF;
10040 	if (index < ip_ndx_ioctl_count) {
10041 		ipip = &ip_ndx_ioctl_table[index];
10042 		if (ipip->ipi_cmd == ioc_cmd) {
10043 			/* Found a match in the ndx table */
10044 			return (ipip);
10045 		}
10046 	}
10047 
10048 	/* Search the misc table */
10049 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10050 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10051 		if (ipip->ipi_cmd == ioc_cmd)
10052 			/* Found a match in the misc table */
10053 			return (ipip);
10054 	}
10055 
10056 	return (NULL);
10057 }
10058 
10059 /*
10060  * Wrapper function for resuming deferred ioctl processing
10061  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10062  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10063  */
10064 /* ARGSUSED */
10065 void
10066 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10067     void *dummy_arg)
10068 {
10069 	ip_sioctl_copyin_setup(q, mp);
10070 }
10071 
10072 /*
10073  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10074  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10075  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10076  * We establish here the size of the block to be copied in.  mi_copyin
10077  * arranges for this to happen, an processing continues in ip_wput with
10078  * an M_IOCDATA message.
10079  */
10080 void
10081 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10082 {
10083 	int	copyin_size;
10084 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10085 	ip_ioctl_cmd_t *ipip;
10086 	cred_t *cr;
10087 	ip_stack_t	*ipst;
10088 
10089 	if (CONN_Q(q))
10090 		ipst = CONNQ_TO_IPST(q);
10091 	else
10092 		ipst = ILLQ_TO_IPST(q);
10093 
10094 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10095 	if (ipip == NULL) {
10096 		/*
10097 		 * The ioctl is not one we understand or own.
10098 		 * Pass it along to be processed down stream,
10099 		 * if this is a module instance of IP, else nak
10100 		 * the ioctl.
10101 		 */
10102 		if (q->q_next == NULL) {
10103 			goto nak;
10104 		} else {
10105 			putnext(q, mp);
10106 			return;
10107 		}
10108 	}
10109 
10110 	/*
10111 	 * If this is deferred, then we will do all the checks when we
10112 	 * come back.
10113 	 */
10114 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10115 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10116 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10117 		return;
10118 	}
10119 
10120 	/*
10121 	 * Only allow a very small subset of IP ioctls on this stream if
10122 	 * IP is a module and not a driver. Allowing ioctls to be processed
10123 	 * in this case may cause assert failures or data corruption.
10124 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10125 	 * ioctls allowed on an IP module stream, after which this stream
10126 	 * normally becomes a multiplexor (at which time the stream head
10127 	 * will fail all ioctls).
10128 	 */
10129 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10130 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10131 			/*
10132 			 * Pass common Streams ioctls which the IP
10133 			 * module does not own or consume along to
10134 			 * be processed down stream.
10135 			 */
10136 			putnext(q, mp);
10137 			return;
10138 		} else {
10139 			goto nak;
10140 		}
10141 	}
10142 
10143 	/* Make sure we have ioctl data to process. */
10144 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10145 		goto nak;
10146 
10147 	/*
10148 	 * Prefer dblk credential over ioctl credential; some synthesized
10149 	 * ioctls have kcred set because there's no way to crhold()
10150 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10151 	 * the framework; the caller of ioctl needs to hold the reference
10152 	 * for the duration of the call).
10153 	 */
10154 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10155 
10156 	/* Make sure normal users don't send down privileged ioctls */
10157 	if ((ipip->ipi_flags & IPI_PRIV) &&
10158 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10159 		/* We checked the privilege earlier but log it here */
10160 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10161 		return;
10162 	}
10163 
10164 	/*
10165 	 * The ioctl command tables can only encode fixed length
10166 	 * ioctl data. If the length is variable, the table will
10167 	 * encode the length as zero. Such special cases are handled
10168 	 * below in the switch.
10169 	 */
10170 	if (ipip->ipi_copyin_size != 0) {
10171 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10172 		return;
10173 	}
10174 
10175 	switch (iocp->ioc_cmd) {
10176 	case O_SIOCGIFCONF:
10177 	case SIOCGIFCONF:
10178 		/*
10179 		 * This IOCTL is hilarious.  See comments in
10180 		 * ip_sioctl_get_ifconf for the story.
10181 		 */
10182 		if (iocp->ioc_count == TRANSPARENT)
10183 			copyin_size = SIZEOF_STRUCT(ifconf,
10184 			    iocp->ioc_flag);
10185 		else
10186 			copyin_size = iocp->ioc_count;
10187 		mi_copyin(q, mp, NULL, copyin_size);
10188 		return;
10189 
10190 	case O_SIOCGLIFCONF:
10191 	case SIOCGLIFCONF:
10192 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10193 		mi_copyin(q, mp, NULL, copyin_size);
10194 		return;
10195 
10196 	case SIOCGLIFSRCOF:
10197 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10198 		mi_copyin(q, mp, NULL, copyin_size);
10199 		return;
10200 	case SIOCGIP6ADDRPOLICY:
10201 		ip_sioctl_ip6addrpolicy(q, mp);
10202 		ip6_asp_table_refrele(ipst);
10203 		return;
10204 
10205 	case SIOCSIP6ADDRPOLICY:
10206 		ip_sioctl_ip6addrpolicy(q, mp);
10207 		return;
10208 
10209 	case SIOCGDSTINFO:
10210 		ip_sioctl_dstinfo(q, mp);
10211 		ip6_asp_table_refrele(ipst);
10212 		return;
10213 
10214 	case I_PLINK:
10215 	case I_PUNLINK:
10216 	case I_LINK:
10217 	case I_UNLINK:
10218 		/*
10219 		 * We treat non-persistent link similarly as the persistent
10220 		 * link case, in terms of plumbing/unplumbing, as well as
10221 		 * dynamic re-plumbing events indicator.  See comments
10222 		 * in ip_sioctl_plink() for more.
10223 		 *
10224 		 * Request can be enqueued in the 'ipsq' while waiting
10225 		 * to become exclusive. So bump up the conn ref.
10226 		 */
10227 		if (CONN_Q(q))
10228 			CONN_INC_REF(Q_TO_CONN(q));
10229 		ip_sioctl_plink(NULL, q, mp, NULL);
10230 		return;
10231 
10232 	case ND_GET:
10233 	case ND_SET:
10234 		/*
10235 		 * Use of the nd table requires holding the reader lock.
10236 		 * Modifying the nd table thru nd_load/nd_unload requires
10237 		 * the writer lock.
10238 		 */
10239 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10240 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10241 			rw_exit(&ipst->ips_ip_g_nd_lock);
10242 
10243 			if (iocp->ioc_error)
10244 				iocp->ioc_count = 0;
10245 			mp->b_datap->db_type = M_IOCACK;
10246 			qreply(q, mp);
10247 			return;
10248 		}
10249 		rw_exit(&ipst->ips_ip_g_nd_lock);
10250 		/*
10251 		 * We don't understand this subioctl of ND_GET / ND_SET.
10252 		 * Maybe intended for some driver / module below us
10253 		 */
10254 		if (q->q_next) {
10255 			putnext(q, mp);
10256 		} else {
10257 			iocp->ioc_error = ENOENT;
10258 			mp->b_datap->db_type = M_IOCNAK;
10259 			iocp->ioc_count = 0;
10260 			qreply(q, mp);
10261 		}
10262 		return;
10263 
10264 	case IP_IOCTL:
10265 		ip_wput_ioctl(q, mp);
10266 		return;
10267 	default:
10268 		cmn_err(CE_PANIC, "should not happen ");
10269 	}
10270 nak:
10271 	if (mp->b_cont != NULL) {
10272 		freemsg(mp->b_cont);
10273 		mp->b_cont = NULL;
10274 	}
10275 	iocp->ioc_error = EINVAL;
10276 	mp->b_datap->db_type = M_IOCNAK;
10277 	iocp->ioc_count = 0;
10278 	qreply(q, mp);
10279 }
10280 
10281 /* ip_wput hands off ARP IOCTL responses to us */
10282 void
10283 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10284 {
10285 	struct arpreq *ar;
10286 	struct xarpreq *xar;
10287 	area_t	*area;
10288 	mblk_t	*area_mp;
10289 	struct iocblk *iocp;
10290 	mblk_t	*orig_ioc_mp, *tmp;
10291 	struct iocblk	*orig_iocp;
10292 	ill_t *ill;
10293 	conn_t *connp = NULL;
10294 	uint_t ioc_id;
10295 	mblk_t *pending_mp;
10296 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10297 	int *flagsp;
10298 	char *storage = NULL;
10299 	sin_t *sin;
10300 	ipaddr_t addr;
10301 	int err;
10302 	ip_stack_t *ipst;
10303 
10304 	ill = q->q_ptr;
10305 	ASSERT(ill != NULL);
10306 	ipst = ill->ill_ipst;
10307 
10308 	/*
10309 	 * We should get back from ARP a packet chain that looks like:
10310 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10311 	 */
10312 	if (!(area_mp = mp->b_cont) ||
10313 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10314 	    !(orig_ioc_mp = area_mp->b_cont) ||
10315 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10316 		freemsg(mp);
10317 		return;
10318 	}
10319 
10320 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10321 
10322 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10323 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10324 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10325 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10326 		x_arp_ioctl = B_TRUE;
10327 		xar = (struct xarpreq *)tmp->b_rptr;
10328 		sin = (sin_t *)&xar->xarp_pa;
10329 		flagsp = &xar->xarp_flags;
10330 		storage = xar->xarp_ha.sdl_data;
10331 		if (xar->xarp_ha.sdl_nlen != 0)
10332 			ifx_arp_ioctl = B_TRUE;
10333 	} else {
10334 		ar = (struct arpreq *)tmp->b_rptr;
10335 		sin = (sin_t *)&ar->arp_pa;
10336 		flagsp = &ar->arp_flags;
10337 		storage = ar->arp_ha.sa_data;
10338 	}
10339 
10340 	iocp = (struct iocblk *)mp->b_rptr;
10341 
10342 	/*
10343 	 * Pick out the originating queue based on the ioc_id.
10344 	 */
10345 	ioc_id = iocp->ioc_id;
10346 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10347 	if (pending_mp == NULL) {
10348 		ASSERT(connp == NULL);
10349 		inet_freemsg(mp);
10350 		return;
10351 	}
10352 	ASSERT(connp != NULL);
10353 	q = CONNP_TO_WQ(connp);
10354 
10355 	/* Uncouple the internally generated IOCTL from the original one */
10356 	area = (area_t *)area_mp->b_rptr;
10357 	area_mp->b_cont = NULL;
10358 
10359 	/*
10360 	 * Restore the b_next and b_prev used by mi code. This is needed
10361 	 * to complete the ioctl using mi* functions. We stored them in
10362 	 * the pending mp prior to sending the request to ARP.
10363 	 */
10364 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10365 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10366 	inet_freemsg(pending_mp);
10367 
10368 	/*
10369 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10370 	 * Catch the case where there is an IRE_CACHE by no entry in the
10371 	 * arp table.
10372 	 */
10373 	addr = sin->sin_addr.s_addr;
10374 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10375 		ire_t			*ire;
10376 		dl_unitdata_req_t	*dlup;
10377 		mblk_t			*llmp;
10378 		int			addr_len;
10379 		ill_t			*ipsqill = NULL;
10380 
10381 		if (ifx_arp_ioctl) {
10382 			/*
10383 			 * There's no need to lookup the ill, since
10384 			 * we've already done that when we started
10385 			 * processing the ioctl and sent the message
10386 			 * to ARP on that ill.  So use the ill that
10387 			 * is stored in q->q_ptr.
10388 			 */
10389 			ipsqill = ill;
10390 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10391 			    ipsqill->ill_ipif, ALL_ZONES,
10392 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10393 		} else {
10394 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10395 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10396 			if (ire != NULL)
10397 				ipsqill = ire_to_ill(ire);
10398 		}
10399 
10400 		if ((x_arp_ioctl) && (ipsqill != NULL))
10401 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10402 
10403 		if (ire != NULL) {
10404 			/*
10405 			 * Since the ire obtained from cachetable is used for
10406 			 * mac addr copying below, treat an incomplete ire as if
10407 			 * as if we never found it.
10408 			 */
10409 			if (ire->ire_nce != NULL &&
10410 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10411 				ire_refrele(ire);
10412 				ire = NULL;
10413 				ipsqill = NULL;
10414 				goto errack;
10415 			}
10416 			*flagsp = ATF_INUSE;
10417 			llmp = (ire->ire_nce != NULL ?
10418 			    ire->ire_nce->nce_res_mp : NULL);
10419 			if (llmp != NULL && ipsqill != NULL) {
10420 				uchar_t *macaddr;
10421 
10422 				addr_len = ipsqill->ill_phys_addr_length;
10423 				if (x_arp_ioctl && ((addr_len +
10424 				    ipsqill->ill_name_length) >
10425 				    sizeof (xar->xarp_ha.sdl_data))) {
10426 					ire_refrele(ire);
10427 					freemsg(mp);
10428 					ip_ioctl_finish(q, orig_ioc_mp,
10429 					    EINVAL, NO_COPYOUT, NULL);
10430 					return;
10431 				}
10432 				*flagsp |= ATF_COM;
10433 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10434 				if (ipsqill->ill_sap_length < 0)
10435 					macaddr = llmp->b_rptr +
10436 					    dlup->dl_dest_addr_offset;
10437 				else
10438 					macaddr = llmp->b_rptr +
10439 					    dlup->dl_dest_addr_offset +
10440 					    ipsqill->ill_sap_length;
10441 				/*
10442 				 * For SIOCGARP, MAC address length
10443 				 * validation has already been done
10444 				 * before the ioctl was issued to ARP to
10445 				 * allow it to progress only on 6 byte
10446 				 * addressable (ethernet like) media. Thus
10447 				 * the mac address copying can not overwrite
10448 				 * the sa_data area below.
10449 				 */
10450 				bcopy(macaddr, storage, addr_len);
10451 			}
10452 			/* Ditch the internal IOCTL. */
10453 			freemsg(mp);
10454 			ire_refrele(ire);
10455 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10456 			return;
10457 		}
10458 	}
10459 
10460 	/*
10461 	 * Delete the coresponding IRE_CACHE if any.
10462 	 * Reset the error if there was one (in case there was no entry
10463 	 * in arp.)
10464 	 */
10465 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10466 		ipif_t *ipintf = NULL;
10467 
10468 		if (ifx_arp_ioctl) {
10469 			/*
10470 			 * There's no need to lookup the ill, since
10471 			 * we've already done that when we started
10472 			 * processing the ioctl and sent the message
10473 			 * to ARP on that ill.  So use the ill that
10474 			 * is stored in q->q_ptr.
10475 			 */
10476 			ipintf = ill->ill_ipif;
10477 		}
10478 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10479 			/*
10480 			 * The address in "addr" may be an entry for a
10481 			 * router. If that's true, then any off-net
10482 			 * IRE_CACHE entries that go through the router
10483 			 * with address "addr" must be clobbered. Use
10484 			 * ire_walk to achieve this goal.
10485 			 */
10486 			if (ifx_arp_ioctl)
10487 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10488 				    ire_delete_cache_gw, (char *)&addr, ill);
10489 			else
10490 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10491 				    ALL_ZONES, ipst);
10492 			iocp->ioc_error = 0;
10493 		}
10494 	}
10495 errack:
10496 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10497 		err = iocp->ioc_error;
10498 		freemsg(mp);
10499 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10500 		return;
10501 	}
10502 
10503 	/*
10504 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10505 	 * the area_t into the struct {x}arpreq.
10506 	 */
10507 	if (x_arp_ioctl) {
10508 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10509 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10510 		    sizeof (xar->xarp_ha.sdl_data)) {
10511 			freemsg(mp);
10512 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10513 			    NULL);
10514 			return;
10515 		}
10516 	}
10517 	*flagsp = ATF_INUSE;
10518 	if (area->area_flags & ACE_F_PERMANENT)
10519 		*flagsp |= ATF_PERM;
10520 	if (area->area_flags & ACE_F_PUBLISH)
10521 		*flagsp |= ATF_PUBL;
10522 	if (area->area_flags & ACE_F_AUTHORITY)
10523 		*flagsp |= ATF_AUTHORITY;
10524 	if (area->area_hw_addr_length != 0) {
10525 		*flagsp |= ATF_COM;
10526 		/*
10527 		 * For SIOCGARP, MAC address length validation has
10528 		 * already been done before the ioctl was issued to ARP
10529 		 * to allow it to progress only on 6 byte addressable
10530 		 * (ethernet like) media. Thus the mac address copying
10531 		 * can not overwrite the sa_data area below.
10532 		 */
10533 		bcopy((char *)area + area->area_hw_addr_offset,
10534 		    storage, area->area_hw_addr_length);
10535 	}
10536 
10537 	/* Ditch the internal IOCTL. */
10538 	freemsg(mp);
10539 	/* Complete the original. */
10540 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10541 }
10542 
10543 /*
10544  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10545  * interface) create the next available logical interface for this
10546  * physical interface.
10547  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10548  * ipif with the specified name.
10549  *
10550  * If the address family is not AF_UNSPEC then set the address as well.
10551  *
10552  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10553  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10554  *
10555  * Executed as a writer on the ill or ill group.
10556  * So no lock is needed to traverse the ipif chain, or examine the
10557  * phyint flags.
10558  */
10559 /* ARGSUSED */
10560 int
10561 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10562     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10563 {
10564 	mblk_t	*mp1;
10565 	struct lifreq *lifr;
10566 	boolean_t	isv6;
10567 	boolean_t	exists;
10568 	char 	*name;
10569 	char	*endp;
10570 	char	*cp;
10571 	int	namelen;
10572 	ipif_t	*ipif;
10573 	long	id;
10574 	ipsq_t	*ipsq;
10575 	ill_t	*ill;
10576 	sin_t	*sin;
10577 	int	err = 0;
10578 	boolean_t found_sep = B_FALSE;
10579 	conn_t	*connp;
10580 	zoneid_t zoneid;
10581 	int	orig_ifindex = 0;
10582 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10583 
10584 	ASSERT(q->q_next == NULL);
10585 	ip1dbg(("ip_sioctl_addif\n"));
10586 	/* Existence of mp1 has been checked in ip_wput_nondata */
10587 	mp1 = mp->b_cont->b_cont;
10588 	/*
10589 	 * Null terminate the string to protect against buffer
10590 	 * overrun. String was generated by user code and may not
10591 	 * be trusted.
10592 	 */
10593 	lifr = (struct lifreq *)mp1->b_rptr;
10594 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10595 	name = lifr->lifr_name;
10596 	ASSERT(CONN_Q(q));
10597 	connp = Q_TO_CONN(q);
10598 	isv6 = connp->conn_af_isv6;
10599 	zoneid = connp->conn_zoneid;
10600 	namelen = mi_strlen(name);
10601 	if (namelen == 0)
10602 		return (EINVAL);
10603 
10604 	exists = B_FALSE;
10605 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10606 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10607 		/*
10608 		 * Allow creating lo0 using SIOCLIFADDIF.
10609 		 * can't be any other writer thread. So can pass null below
10610 		 * for the last 4 args to ipif_lookup_name.
10611 		 */
10612 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10613 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10614 		/* Prevent any further action */
10615 		if (ipif == NULL) {
10616 			return (ENOBUFS);
10617 		} else if (!exists) {
10618 			/* We created the ipif now and as writer */
10619 			ipif_refrele(ipif);
10620 			return (0);
10621 		} else {
10622 			ill = ipif->ipif_ill;
10623 			ill_refhold(ill);
10624 			ipif_refrele(ipif);
10625 		}
10626 	} else {
10627 		/* Look for a colon in the name. */
10628 		endp = &name[namelen];
10629 		for (cp = endp; --cp > name; ) {
10630 			if (*cp == IPIF_SEPARATOR_CHAR) {
10631 				found_sep = B_TRUE;
10632 				/*
10633 				 * Reject any non-decimal aliases for plumbing
10634 				 * of logical interfaces. Aliases with leading
10635 				 * zeroes are also rejected as they introduce
10636 				 * ambiguity in the naming of the interfaces.
10637 				 * Comparing with "0" takes care of all such
10638 				 * cases.
10639 				 */
10640 				if ((strncmp("0", cp+1, 1)) == 0)
10641 					return (EINVAL);
10642 
10643 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10644 				    id <= 0 || *endp != '\0') {
10645 					return (EINVAL);
10646 				}
10647 				*cp = '\0';
10648 				break;
10649 			}
10650 		}
10651 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10652 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10653 		if (found_sep)
10654 			*cp = IPIF_SEPARATOR_CHAR;
10655 		if (ill == NULL)
10656 			return (err);
10657 	}
10658 
10659 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10660 	    B_TRUE);
10661 
10662 	/*
10663 	 * Release the refhold due to the lookup, now that we are excl
10664 	 * or we are just returning
10665 	 */
10666 	ill_refrele(ill);
10667 
10668 	if (ipsq == NULL)
10669 		return (EINPROGRESS);
10670 
10671 	/*
10672 	 * If the interface is failed, inactive or offlined, look for a working
10673 	 * interface in the ill group and create the ipif there. If we can't
10674 	 * find a good interface, create the ipif anyway so that in.mpathd can
10675 	 * move it to the first repaired interface.
10676 	 */
10677 	if ((ill->ill_phyint->phyint_flags &
10678 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10679 	    ill->ill_phyint->phyint_groupname_len != 0) {
10680 		phyint_t *phyi;
10681 		char *groupname = ill->ill_phyint->phyint_groupname;
10682 
10683 		/*
10684 		 * We're looking for a working interface, but it doesn't matter
10685 		 * if it's up or down; so instead of following the group lists,
10686 		 * we look at each physical interface and compare the groupname.
10687 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10688 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10689 		 * Otherwise we create the ipif on the failed interface.
10690 		 */
10691 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10692 		phyi = avl_first(&ipst->ips_phyint_g_list->
10693 		    phyint_list_avl_by_index);
10694 		for (; phyi != NULL;
10695 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10696 		    phyint_list_avl_by_index,
10697 		    phyi, AVL_AFTER)) {
10698 			if (phyi->phyint_groupname_len == 0)
10699 				continue;
10700 			ASSERT(phyi->phyint_groupname != NULL);
10701 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10702 			    !(phyi->phyint_flags &
10703 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10704 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10705 			    (phyi->phyint_illv4 != NULL))) {
10706 				break;
10707 			}
10708 		}
10709 		rw_exit(&ipst->ips_ill_g_lock);
10710 
10711 		if (phyi != NULL) {
10712 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10713 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10714 			    phyi->phyint_illv4);
10715 		}
10716 	}
10717 
10718 	/*
10719 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10720 	 * before or after us.
10721 	 */
10722 	ASSERT(IAM_WRITER_ILL(ill));
10723 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10724 
10725 	if (found_sep && orig_ifindex == 0) {
10726 		/* Now see if there is an IPIF with this unit number. */
10727 		for (ipif = ill->ill_ipif; ipif != NULL;
10728 		    ipif = ipif->ipif_next) {
10729 			if (ipif->ipif_id == id) {
10730 				err = EEXIST;
10731 				goto done;
10732 			}
10733 		}
10734 	}
10735 
10736 	/*
10737 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10738 	 * of lo0. We never come here when we plumb lo0:0. It
10739 	 * happens in ipif_lookup_on_name.
10740 	 * The specified unit number is ignored when we create the ipif on a
10741 	 * different interface. However, we save it in ipif_orig_ipifid below so
10742 	 * that the ipif fails back to the right position.
10743 	 */
10744 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10745 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10746 		err = ENOBUFS;
10747 		goto done;
10748 	}
10749 
10750 	/* Return created name with ioctl */
10751 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10752 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10753 	ip1dbg(("created %s\n", lifr->lifr_name));
10754 
10755 	/* Set address */
10756 	sin = (sin_t *)&lifr->lifr_addr;
10757 	if (sin->sin_family != AF_UNSPEC) {
10758 		err = ip_sioctl_addr(ipif, sin, q, mp,
10759 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10760 	}
10761 
10762 	/* Set ifindex and unit number for failback */
10763 	if (err == 0 && orig_ifindex != 0) {
10764 		ipif->ipif_orig_ifindex = orig_ifindex;
10765 		if (found_sep) {
10766 			ipif->ipif_orig_ipifid = id;
10767 		}
10768 	}
10769 
10770 done:
10771 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10772 	return (err);
10773 }
10774 
10775 /*
10776  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10777  * interface) delete it based on the IP address (on this physical interface).
10778  * Otherwise delete it based on the ipif_id.
10779  * Also, special handling to allow a removeif of lo0.
10780  */
10781 /* ARGSUSED */
10782 int
10783 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10784     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10785 {
10786 	conn_t		*connp;
10787 	ill_t		*ill = ipif->ipif_ill;
10788 	boolean_t	 success;
10789 	ip_stack_t	*ipst;
10790 
10791 	ipst = CONNQ_TO_IPST(q);
10792 
10793 	ASSERT(q->q_next == NULL);
10794 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10795 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10796 	ASSERT(IAM_WRITER_IPIF(ipif));
10797 
10798 	connp = Q_TO_CONN(q);
10799 	/*
10800 	 * Special case for unplumbing lo0 (the loopback physical interface).
10801 	 * If unplumbing lo0, the incoming address structure has been
10802 	 * initialized to all zeros. When unplumbing lo0, all its logical
10803 	 * interfaces must be removed too.
10804 	 *
10805 	 * Note that this interface may be called to remove a specific
10806 	 * loopback logical interface (eg, lo0:1). But in that case
10807 	 * ipif->ipif_id != 0 so that the code path for that case is the
10808 	 * same as any other interface (meaning it skips the code directly
10809 	 * below).
10810 	 */
10811 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10812 		if (sin->sin_family == AF_UNSPEC &&
10813 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10814 			/*
10815 			 * Mark it condemned. No new ref. will be made to ill.
10816 			 */
10817 			mutex_enter(&ill->ill_lock);
10818 			ill->ill_state_flags |= ILL_CONDEMNED;
10819 			for (ipif = ill->ill_ipif; ipif != NULL;
10820 			    ipif = ipif->ipif_next) {
10821 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10822 			}
10823 			mutex_exit(&ill->ill_lock);
10824 
10825 			ipif = ill->ill_ipif;
10826 			/* unplumb the loopback interface */
10827 			ill_delete(ill);
10828 			mutex_enter(&connp->conn_lock);
10829 			mutex_enter(&ill->ill_lock);
10830 			ASSERT(ill->ill_group == NULL);
10831 
10832 			/* Are any references to this ill active */
10833 			if (ill_is_quiescent(ill)) {
10834 				mutex_exit(&ill->ill_lock);
10835 				mutex_exit(&connp->conn_lock);
10836 				ill_delete_tail(ill);
10837 				mi_free(ill);
10838 				return (0);
10839 			}
10840 			success = ipsq_pending_mp_add(connp, ipif,
10841 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10842 			mutex_exit(&connp->conn_lock);
10843 			mutex_exit(&ill->ill_lock);
10844 			if (success)
10845 				return (EINPROGRESS);
10846 			else
10847 				return (EINTR);
10848 		}
10849 	}
10850 
10851 	/*
10852 	 * We are exclusive on the ipsq, so an ill move will be serialized
10853 	 * before or after us.
10854 	 */
10855 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10856 
10857 	if (ipif->ipif_id == 0) {
10858 		/* Find based on address */
10859 		if (ipif->ipif_isv6) {
10860 			sin6_t *sin6;
10861 
10862 			if (sin->sin_family != AF_INET6)
10863 				return (EAFNOSUPPORT);
10864 
10865 			sin6 = (sin6_t *)sin;
10866 			/* We are a writer, so we should be able to lookup */
10867 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10868 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10869 			if (ipif == NULL) {
10870 				/*
10871 				 * Maybe the address in on another interface in
10872 				 * the same IPMP group? We check this below.
10873 				 */
10874 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10875 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10876 				    ipst);
10877 			}
10878 		} else {
10879 			ipaddr_t addr;
10880 
10881 			if (sin->sin_family != AF_INET)
10882 				return (EAFNOSUPPORT);
10883 
10884 			addr = sin->sin_addr.s_addr;
10885 			/* We are a writer, so we should be able to lookup */
10886 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10887 			    NULL, NULL, NULL, ipst);
10888 			if (ipif == NULL) {
10889 				/*
10890 				 * Maybe the address in on another interface in
10891 				 * the same IPMP group? We check this below.
10892 				 */
10893 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10894 				    NULL, NULL, NULL, NULL, ipst);
10895 			}
10896 		}
10897 		if (ipif == NULL) {
10898 			return (EADDRNOTAVAIL);
10899 		}
10900 		/*
10901 		 * When the address to be removed is hosted on a different
10902 		 * interface, we check if the interface is in the same IPMP
10903 		 * group as the specified one; if so we proceed with the
10904 		 * removal.
10905 		 * ill->ill_group is NULL when the ill is down, so we have to
10906 		 * compare the group names instead.
10907 		 */
10908 		if (ipif->ipif_ill != ill &&
10909 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10910 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10911 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10912 		    ill->ill_phyint->phyint_groupname) != 0)) {
10913 			ipif_refrele(ipif);
10914 			return (EADDRNOTAVAIL);
10915 		}
10916 
10917 		/* This is a writer */
10918 		ipif_refrele(ipif);
10919 	}
10920 
10921 	/*
10922 	 * Can not delete instance zero since it is tied to the ill.
10923 	 */
10924 	if (ipif->ipif_id == 0)
10925 		return (EBUSY);
10926 
10927 	mutex_enter(&ill->ill_lock);
10928 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10929 	mutex_exit(&ill->ill_lock);
10930 
10931 	ipif_free(ipif);
10932 
10933 	mutex_enter(&connp->conn_lock);
10934 	mutex_enter(&ill->ill_lock);
10935 
10936 	/* Are any references to this ipif active */
10937 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10938 		mutex_exit(&ill->ill_lock);
10939 		mutex_exit(&connp->conn_lock);
10940 		ipif_non_duplicate(ipif);
10941 		ipif_down_tail(ipif);
10942 		ipif_free_tail(ipif);
10943 		return (0);
10944 	}
10945 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10946 	    IPIF_FREE);
10947 	mutex_exit(&ill->ill_lock);
10948 	mutex_exit(&connp->conn_lock);
10949 	if (success)
10950 		return (EINPROGRESS);
10951 	else
10952 		return (EINTR);
10953 }
10954 
10955 /*
10956  * Restart the removeif ioctl. The refcnt has gone down to 0.
10957  * The ipif is already condemned. So can't find it thru lookups.
10958  */
10959 /* ARGSUSED */
10960 int
10961 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10962     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10963 {
10964 	ill_t *ill = ipif->ipif_ill;
10965 
10966 	ASSERT(IAM_WRITER_IPIF(ipif));
10967 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10968 
10969 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10970 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10971 
10972 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10973 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10974 		ill_delete_tail(ill);
10975 		mi_free(ill);
10976 		return (0);
10977 	}
10978 
10979 	ipif_non_duplicate(ipif);
10980 	ipif_down_tail(ipif);
10981 	ipif_free_tail(ipif);
10982 
10983 	ILL_UNMARK_CHANGING(ill);
10984 	return (0);
10985 }
10986 
10987 /*
10988  * Set the local interface address.
10989  * Allow an address of all zero when the interface is down.
10990  */
10991 /* ARGSUSED */
10992 int
10993 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10994     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10995 {
10996 	int err = 0;
10997 	in6_addr_t v6addr;
10998 	boolean_t need_up = B_FALSE;
10999 
11000 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11001 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11002 
11003 	ASSERT(IAM_WRITER_IPIF(ipif));
11004 
11005 	if (ipif->ipif_isv6) {
11006 		sin6_t *sin6;
11007 		ill_t *ill;
11008 		phyint_t *phyi;
11009 
11010 		if (sin->sin_family != AF_INET6)
11011 			return (EAFNOSUPPORT);
11012 
11013 		sin6 = (sin6_t *)sin;
11014 		v6addr = sin6->sin6_addr;
11015 		ill = ipif->ipif_ill;
11016 		phyi = ill->ill_phyint;
11017 
11018 		/*
11019 		 * Enforce that true multicast interfaces have a link-local
11020 		 * address for logical unit 0.
11021 		 */
11022 		if (ipif->ipif_id == 0 &&
11023 		    (ill->ill_flags & ILLF_MULTICAST) &&
11024 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11025 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11026 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11027 			return (EADDRNOTAVAIL);
11028 		}
11029 
11030 		/*
11031 		 * up interfaces shouldn't have the unspecified address
11032 		 * unless they also have the IPIF_NOLOCAL flags set and
11033 		 * have a subnet assigned.
11034 		 */
11035 		if ((ipif->ipif_flags & IPIF_UP) &&
11036 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11037 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11038 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11039 			return (EADDRNOTAVAIL);
11040 		}
11041 
11042 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11043 			return (EADDRNOTAVAIL);
11044 	} else {
11045 		ipaddr_t addr;
11046 
11047 		if (sin->sin_family != AF_INET)
11048 			return (EAFNOSUPPORT);
11049 
11050 		addr = sin->sin_addr.s_addr;
11051 
11052 		/* Allow 0 as the local address. */
11053 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11054 			return (EADDRNOTAVAIL);
11055 
11056 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11057 	}
11058 
11059 
11060 	/*
11061 	 * Even if there is no change we redo things just to rerun
11062 	 * ipif_set_default.
11063 	 */
11064 	if (ipif->ipif_flags & IPIF_UP) {
11065 		/*
11066 		 * Setting a new local address, make sure
11067 		 * we have net and subnet bcast ire's for
11068 		 * the old address if we need them.
11069 		 */
11070 		if (!ipif->ipif_isv6)
11071 			ipif_check_bcast_ires(ipif);
11072 		/*
11073 		 * If the interface is already marked up,
11074 		 * we call ipif_down which will take care
11075 		 * of ditching any IREs that have been set
11076 		 * up based on the old interface address.
11077 		 */
11078 		err = ipif_logical_down(ipif, q, mp);
11079 		if (err == EINPROGRESS)
11080 			return (err);
11081 		ipif_down_tail(ipif);
11082 		need_up = 1;
11083 	}
11084 
11085 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11086 	return (err);
11087 }
11088 
11089 int
11090 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11091     boolean_t need_up)
11092 {
11093 	in6_addr_t v6addr;
11094 	in6_addr_t ov6addr;
11095 	ipaddr_t addr;
11096 	sin6_t	*sin6;
11097 	int	sinlen;
11098 	int	err = 0;
11099 	ill_t	*ill = ipif->ipif_ill;
11100 	boolean_t need_dl_down;
11101 	boolean_t need_arp_down;
11102 	struct iocblk *iocp;
11103 
11104 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11105 
11106 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11107 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11108 	ASSERT(IAM_WRITER_IPIF(ipif));
11109 
11110 	/* Must cancel any pending timer before taking the ill_lock */
11111 	if (ipif->ipif_recovery_id != 0)
11112 		(void) untimeout(ipif->ipif_recovery_id);
11113 	ipif->ipif_recovery_id = 0;
11114 
11115 	if (ipif->ipif_isv6) {
11116 		sin6 = (sin6_t *)sin;
11117 		v6addr = sin6->sin6_addr;
11118 		sinlen = sizeof (struct sockaddr_in6);
11119 	} else {
11120 		addr = sin->sin_addr.s_addr;
11121 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11122 		sinlen = sizeof (struct sockaddr_in);
11123 	}
11124 	mutex_enter(&ill->ill_lock);
11125 	ov6addr = ipif->ipif_v6lcl_addr;
11126 	ipif->ipif_v6lcl_addr = v6addr;
11127 	sctp_update_ipif_addr(ipif, ov6addr);
11128 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11129 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11130 	} else {
11131 		ipif->ipif_v6src_addr = v6addr;
11132 	}
11133 	ipif->ipif_addr_ready = 0;
11134 
11135 	/*
11136 	 * If the interface was previously marked as a duplicate, then since
11137 	 * we've now got a "new" address, it should no longer be considered a
11138 	 * duplicate -- even if the "new" address is the same as the old one.
11139 	 * Note that if all ipifs are down, we may have a pending ARP down
11140 	 * event to handle.  This is because we want to recover from duplicates
11141 	 * and thus delay tearing down ARP until the duplicates have been
11142 	 * removed or disabled.
11143 	 */
11144 	need_dl_down = need_arp_down = B_FALSE;
11145 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11146 		need_arp_down = !need_up;
11147 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11148 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11149 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11150 			need_dl_down = B_TRUE;
11151 		}
11152 	}
11153 
11154 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11155 	    !ill->ill_is_6to4tun) {
11156 		queue_t *wqp = ill->ill_wq;
11157 
11158 		/*
11159 		 * The local address of this interface is a 6to4 address,
11160 		 * check if this interface is in fact a 6to4 tunnel or just
11161 		 * an interface configured with a 6to4 address.  We are only
11162 		 * interested in the former.
11163 		 */
11164 		if (wqp != NULL) {
11165 			while ((wqp->q_next != NULL) &&
11166 			    (wqp->q_next->q_qinfo != NULL) &&
11167 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11168 
11169 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11170 				    == TUN6TO4_MODID) {
11171 					/* set for use in IP */
11172 					ill->ill_is_6to4tun = 1;
11173 					break;
11174 				}
11175 				wqp = wqp->q_next;
11176 			}
11177 		}
11178 	}
11179 
11180 	ipif_set_default(ipif);
11181 
11182 	/*
11183 	 * When publishing an interface address change event, we only notify
11184 	 * the event listeners of the new address.  It is assumed that if they
11185 	 * actively care about the addresses assigned that they will have
11186 	 * already discovered the previous address assigned (if there was one.)
11187 	 *
11188 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11189 	 */
11190 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11191 		hook_nic_event_t *info;
11192 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11193 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11194 			    "attached for %s\n", info->hne_event,
11195 			    ill->ill_name));
11196 			if (info->hne_data != NULL)
11197 				kmem_free(info->hne_data, info->hne_datalen);
11198 			kmem_free(info, sizeof (hook_nic_event_t));
11199 		}
11200 
11201 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11202 		if (info != NULL) {
11203 			ip_stack_t	*ipst = ill->ill_ipst;
11204 
11205 			info->hne_nic =
11206 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11207 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11208 			info->hne_event = NE_ADDRESS_CHANGE;
11209 			info->hne_family = ipif->ipif_isv6 ?
11210 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11211 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11212 			if (info->hne_data != NULL) {
11213 				info->hne_datalen = sinlen;
11214 				bcopy(sin, info->hne_data, sinlen);
11215 			} else {
11216 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11217 				    "address information for ADDRESS_CHANGE nic"
11218 				    " event of %s (ENOMEM)\n",
11219 				    ipif->ipif_ill->ill_name));
11220 				kmem_free(info, sizeof (hook_nic_event_t));
11221 			}
11222 		} else
11223 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11224 			    "ADDRESS_CHANGE nic event information for %s "
11225 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11226 
11227 		ipif->ipif_ill->ill_nic_event_info = info;
11228 	}
11229 
11230 	mutex_exit(&ill->ill_lock);
11231 
11232 	if (need_up) {
11233 		/*
11234 		 * Now bring the interface back up.  If this
11235 		 * is the only IPIF for the ILL, ipif_up
11236 		 * will have to re-bind to the device, so
11237 		 * we may get back EINPROGRESS, in which
11238 		 * case, this IOCTL will get completed in
11239 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11240 		 */
11241 		err = ipif_up(ipif, q, mp);
11242 	}
11243 
11244 	if (need_dl_down)
11245 		ill_dl_down(ill);
11246 	if (need_arp_down)
11247 		ipif_arp_down(ipif);
11248 
11249 	return (err);
11250 }
11251 
11252 
11253 /*
11254  * Restart entry point to restart the address set operation after the
11255  * refcounts have dropped to zero.
11256  */
11257 /* ARGSUSED */
11258 int
11259 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11260     ip_ioctl_cmd_t *ipip, void *ifreq)
11261 {
11262 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11263 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11264 	ASSERT(IAM_WRITER_IPIF(ipif));
11265 	ipif_down_tail(ipif);
11266 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11267 }
11268 
11269 /* ARGSUSED */
11270 int
11271 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11272     ip_ioctl_cmd_t *ipip, void *if_req)
11273 {
11274 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11275 	struct lifreq *lifr = (struct lifreq *)if_req;
11276 
11277 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11278 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11279 	/*
11280 	 * The net mask and address can't change since we have a
11281 	 * reference to the ipif. So no lock is necessary.
11282 	 */
11283 	if (ipif->ipif_isv6) {
11284 		*sin6 = sin6_null;
11285 		sin6->sin6_family = AF_INET6;
11286 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11287 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11288 		lifr->lifr_addrlen =
11289 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11290 	} else {
11291 		*sin = sin_null;
11292 		sin->sin_family = AF_INET;
11293 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11294 		if (ipip->ipi_cmd_type == LIF_CMD) {
11295 			lifr->lifr_addrlen =
11296 			    ip_mask_to_plen(ipif->ipif_net_mask);
11297 		}
11298 	}
11299 	return (0);
11300 }
11301 
11302 /*
11303  * Set the destination address for a pt-pt interface.
11304  */
11305 /* ARGSUSED */
11306 int
11307 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11308     ip_ioctl_cmd_t *ipip, void *if_req)
11309 {
11310 	int err = 0;
11311 	in6_addr_t v6addr;
11312 	boolean_t need_up = B_FALSE;
11313 
11314 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11315 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11316 	ASSERT(IAM_WRITER_IPIF(ipif));
11317 
11318 	if (ipif->ipif_isv6) {
11319 		sin6_t *sin6;
11320 
11321 		if (sin->sin_family != AF_INET6)
11322 			return (EAFNOSUPPORT);
11323 
11324 		sin6 = (sin6_t *)sin;
11325 		v6addr = sin6->sin6_addr;
11326 
11327 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11328 			return (EADDRNOTAVAIL);
11329 	} else {
11330 		ipaddr_t addr;
11331 
11332 		if (sin->sin_family != AF_INET)
11333 			return (EAFNOSUPPORT);
11334 
11335 		addr = sin->sin_addr.s_addr;
11336 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11337 			return (EADDRNOTAVAIL);
11338 
11339 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11340 	}
11341 
11342 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11343 		return (0);	/* No change */
11344 
11345 	if (ipif->ipif_flags & IPIF_UP) {
11346 		/*
11347 		 * If the interface is already marked up,
11348 		 * we call ipif_down which will take care
11349 		 * of ditching any IREs that have been set
11350 		 * up based on the old pp dst address.
11351 		 */
11352 		err = ipif_logical_down(ipif, q, mp);
11353 		if (err == EINPROGRESS)
11354 			return (err);
11355 		ipif_down_tail(ipif);
11356 		need_up = B_TRUE;
11357 	}
11358 	/*
11359 	 * could return EINPROGRESS. If so ioctl will complete in
11360 	 * ip_rput_dlpi_writer
11361 	 */
11362 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11363 	return (err);
11364 }
11365 
11366 static int
11367 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11368     boolean_t need_up)
11369 {
11370 	in6_addr_t v6addr;
11371 	ill_t	*ill = ipif->ipif_ill;
11372 	int	err = 0;
11373 	boolean_t need_dl_down;
11374 	boolean_t need_arp_down;
11375 
11376 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11377 	    ipif->ipif_id, (void *)ipif));
11378 
11379 	/* Must cancel any pending timer before taking the ill_lock */
11380 	if (ipif->ipif_recovery_id != 0)
11381 		(void) untimeout(ipif->ipif_recovery_id);
11382 	ipif->ipif_recovery_id = 0;
11383 
11384 	if (ipif->ipif_isv6) {
11385 		sin6_t *sin6;
11386 
11387 		sin6 = (sin6_t *)sin;
11388 		v6addr = sin6->sin6_addr;
11389 	} else {
11390 		ipaddr_t addr;
11391 
11392 		addr = sin->sin_addr.s_addr;
11393 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11394 	}
11395 	mutex_enter(&ill->ill_lock);
11396 	/* Set point to point destination address. */
11397 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11398 		/*
11399 		 * Allow this as a means of creating logical
11400 		 * pt-pt interfaces on top of e.g. an Ethernet.
11401 		 * XXX Undocumented HACK for testing.
11402 		 * pt-pt interfaces are created with NUD disabled.
11403 		 */
11404 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11405 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11406 		if (ipif->ipif_isv6)
11407 			ill->ill_flags |= ILLF_NONUD;
11408 	}
11409 
11410 	/*
11411 	 * If the interface was previously marked as a duplicate, then since
11412 	 * we've now got a "new" address, it should no longer be considered a
11413 	 * duplicate -- even if the "new" address is the same as the old one.
11414 	 * Note that if all ipifs are down, we may have a pending ARP down
11415 	 * event to handle.
11416 	 */
11417 	need_dl_down = need_arp_down = B_FALSE;
11418 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11419 		need_arp_down = !need_up;
11420 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11421 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11422 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11423 			need_dl_down = B_TRUE;
11424 		}
11425 	}
11426 
11427 	/* Set the new address. */
11428 	ipif->ipif_v6pp_dst_addr = v6addr;
11429 	/* Make sure subnet tracks pp_dst */
11430 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11431 	mutex_exit(&ill->ill_lock);
11432 
11433 	if (need_up) {
11434 		/*
11435 		 * Now bring the interface back up.  If this
11436 		 * is the only IPIF for the ILL, ipif_up
11437 		 * will have to re-bind to the device, so
11438 		 * we may get back EINPROGRESS, in which
11439 		 * case, this IOCTL will get completed in
11440 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11441 		 */
11442 		err = ipif_up(ipif, q, mp);
11443 	}
11444 
11445 	if (need_dl_down)
11446 		ill_dl_down(ill);
11447 
11448 	if (need_arp_down)
11449 		ipif_arp_down(ipif);
11450 	return (err);
11451 }
11452 
11453 /*
11454  * Restart entry point to restart the dstaddress set operation after the
11455  * refcounts have dropped to zero.
11456  */
11457 /* ARGSUSED */
11458 int
11459 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11460     ip_ioctl_cmd_t *ipip, void *ifreq)
11461 {
11462 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11463 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11464 	ipif_down_tail(ipif);
11465 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11466 }
11467 
11468 /* ARGSUSED */
11469 int
11470 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11471     ip_ioctl_cmd_t *ipip, void *if_req)
11472 {
11473 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11474 
11475 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11476 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11477 	/*
11478 	 * Get point to point destination address. The addresses can't
11479 	 * change since we hold a reference to the ipif.
11480 	 */
11481 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11482 		return (EADDRNOTAVAIL);
11483 
11484 	if (ipif->ipif_isv6) {
11485 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11486 		*sin6 = sin6_null;
11487 		sin6->sin6_family = AF_INET6;
11488 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11489 	} else {
11490 		*sin = sin_null;
11491 		sin->sin_family = AF_INET;
11492 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11493 	}
11494 	return (0);
11495 }
11496 
11497 /*
11498  * part of ipmp, make this func return the active/inactive state and
11499  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11500  */
11501 /*
11502  * This function either sets or clears the IFF_INACTIVE flag.
11503  *
11504  * As long as there are some addresses or multicast memberships on the
11505  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11506  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11507  * will be used for outbound packets.
11508  *
11509  * Caller needs to verify the validity of setting IFF_INACTIVE.
11510  */
11511 static void
11512 phyint_inactive(phyint_t *phyi)
11513 {
11514 	ill_t *ill_v4;
11515 	ill_t *ill_v6;
11516 	ipif_t *ipif;
11517 	ilm_t *ilm;
11518 
11519 	ill_v4 = phyi->phyint_illv4;
11520 	ill_v6 = phyi->phyint_illv6;
11521 
11522 	/*
11523 	 * No need for a lock while traversing the list since iam
11524 	 * a writer
11525 	 */
11526 	if (ill_v4 != NULL) {
11527 		ASSERT(IAM_WRITER_ILL(ill_v4));
11528 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11529 		    ipif = ipif->ipif_next) {
11530 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11531 				mutex_enter(&phyi->phyint_lock);
11532 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11533 				mutex_exit(&phyi->phyint_lock);
11534 				return;
11535 			}
11536 		}
11537 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11538 		    ilm = ilm->ilm_next) {
11539 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11540 				mutex_enter(&phyi->phyint_lock);
11541 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11542 				mutex_exit(&phyi->phyint_lock);
11543 				return;
11544 			}
11545 		}
11546 	}
11547 	if (ill_v6 != NULL) {
11548 		ill_v6 = phyi->phyint_illv6;
11549 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11550 		    ipif = ipif->ipif_next) {
11551 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11552 				mutex_enter(&phyi->phyint_lock);
11553 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11554 				mutex_exit(&phyi->phyint_lock);
11555 				return;
11556 			}
11557 		}
11558 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11559 		    ilm = ilm->ilm_next) {
11560 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11561 				mutex_enter(&phyi->phyint_lock);
11562 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11563 				mutex_exit(&phyi->phyint_lock);
11564 				return;
11565 			}
11566 		}
11567 	}
11568 	mutex_enter(&phyi->phyint_lock);
11569 	phyi->phyint_flags |= PHYI_INACTIVE;
11570 	mutex_exit(&phyi->phyint_lock);
11571 }
11572 
11573 /*
11574  * This function is called only when the phyint flags change. Currently
11575  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11576  * that we can select a good ill.
11577  */
11578 static void
11579 ip_redo_nomination(phyint_t *phyi)
11580 {
11581 	ill_t *ill_v4;
11582 
11583 	ill_v4 = phyi->phyint_illv4;
11584 
11585 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11586 		ASSERT(IAM_WRITER_ILL(ill_v4));
11587 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11588 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11589 	}
11590 }
11591 
11592 /*
11593  * Heuristic to check if ill is INACTIVE.
11594  * Checks if ill has an ipif with an usable ip address.
11595  *
11596  * Return values:
11597  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11598  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11599  */
11600 static boolean_t
11601 ill_is_inactive(ill_t *ill)
11602 {
11603 	ipif_t *ipif;
11604 
11605 	/* Check whether it is in an IPMP group */
11606 	if (ill->ill_phyint->phyint_groupname == NULL)
11607 		return (B_FALSE);
11608 
11609 	if (ill->ill_ipif_up_count == 0)
11610 		return (B_TRUE);
11611 
11612 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11613 		uint64_t flags = ipif->ipif_flags;
11614 
11615 		/*
11616 		 * This ipif is usable if it is IPIF_UP and not a
11617 		 * dedicated test address.  A dedicated test address
11618 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11619 		 * (note in particular that V6 test addresses are
11620 		 * link-local data addresses and thus are marked
11621 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11622 		 */
11623 		if ((flags & IPIF_UP) &&
11624 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11625 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11626 			return (B_FALSE);
11627 	}
11628 	return (B_TRUE);
11629 }
11630 
11631 /*
11632  * Set interface flags.
11633  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11634  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11635  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11636  *
11637  * NOTE : We really don't enforce that ipif_id zero should be used
11638  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11639  *	  is because applications generally does SICGLIFFLAGS and
11640  *	  ORs in the new flags (that affects the logical) and does a
11641  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11642  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11643  *	  flags that will be turned on is correct with respect to
11644  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11645  */
11646 /* ARGSUSED */
11647 int
11648 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11649     ip_ioctl_cmd_t *ipip, void *if_req)
11650 {
11651 	uint64_t turn_on;
11652 	uint64_t turn_off;
11653 	int	err;
11654 	boolean_t need_up = B_FALSE;
11655 	phyint_t *phyi;
11656 	ill_t *ill;
11657 	uint64_t intf_flags;
11658 	boolean_t phyint_flags_modified = B_FALSE;
11659 	uint64_t flags;
11660 	struct ifreq *ifr;
11661 	struct lifreq *lifr;
11662 	boolean_t set_linklocal = B_FALSE;
11663 	boolean_t zero_source = B_FALSE;
11664 	ip_stack_t *ipst;
11665 
11666 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11667 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11668 
11669 	ASSERT(IAM_WRITER_IPIF(ipif));
11670 
11671 	ill = ipif->ipif_ill;
11672 	phyi = ill->ill_phyint;
11673 	ipst = ill->ill_ipst;
11674 
11675 	if (ipip->ipi_cmd_type == IF_CMD) {
11676 		ifr = (struct ifreq *)if_req;
11677 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11678 	} else {
11679 		lifr = (struct lifreq *)if_req;
11680 		flags = lifr->lifr_flags;
11681 	}
11682 
11683 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11684 
11685 	/*
11686 	 * Has the flags been set correctly till now ?
11687 	 */
11688 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11689 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11690 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11691 	/*
11692 	 * Compare the new flags to the old, and partition
11693 	 * into those coming on and those going off.
11694 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11695 	 */
11696 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11697 		flags |= intf_flags & ~0xFFFF;
11698 
11699 	/*
11700 	 * First check which bits will change and then which will
11701 	 * go on and off
11702 	 */
11703 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11704 	if (!turn_on)
11705 		return (0);	/* No change */
11706 
11707 	turn_off = intf_flags & turn_on;
11708 	turn_on ^= turn_off;
11709 	err = 0;
11710 
11711 	/*
11712 	 * Don't allow any bits belonging to the logical interface
11713 	 * to be set or cleared on the replacement ipif that was
11714 	 * created temporarily during a MOVE.
11715 	 */
11716 	if (ipif->ipif_replace_zero &&
11717 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11718 		return (EINVAL);
11719 	}
11720 
11721 	/*
11722 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11723 	 * IPv6 interfaces.
11724 	 */
11725 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11726 		return (EINVAL);
11727 
11728 	/*
11729 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11730 	 */
11731 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11732 		return (EINVAL);
11733 
11734 	/*
11735 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11736 	 * interfaces.  It makes no sense in that context.
11737 	 */
11738 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11739 		return (EINVAL);
11740 
11741 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11742 		zero_source = B_TRUE;
11743 
11744 	/*
11745 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11746 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11747 	 * If the link local address isn't set, and can be set, it will get
11748 	 * set later on in this function.
11749 	 */
11750 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11751 	    (flags & IFF_UP) && !zero_source &&
11752 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11753 		if (ipif_cant_setlinklocal(ipif))
11754 			return (EINVAL);
11755 		set_linklocal = B_TRUE;
11756 	}
11757 
11758 	/*
11759 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11760 	 * same time. No need to grab ill_g_usesrc_lock here, see
11761 	 * synchronization notes in ip.c
11762 	 */
11763 	if (turn_on & PHYI_STANDBY &&
11764 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11765 		return (EINVAL);
11766 	}
11767 
11768 	/*
11769 	 * If we modify physical interface flags, we'll potentially need to
11770 	 * send up two routing socket messages for the changes (one for the
11771 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11772 	 */
11773 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11774 		phyint_flags_modified = B_TRUE;
11775 
11776 	/*
11777 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11778 	 * we need to flush the IRE_CACHES belonging to this ill.
11779 	 * We handle this case here without doing the DOWN/UP dance
11780 	 * like it is done for other flags. If some other flags are
11781 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11782 	 * below will handle it by bringing it down and then
11783 	 * bringing it UP.
11784 	 */
11785 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11786 		ill_t *ill_v4, *ill_v6;
11787 
11788 		ill_v4 = phyi->phyint_illv4;
11789 		ill_v6 = phyi->phyint_illv6;
11790 
11791 		/*
11792 		 * First set the INACTIVE flag if needed. Then delete the ires.
11793 		 * ire_add will atomically prevent creating new IRE_CACHEs
11794 		 * unless hidden flag is set.
11795 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11796 		 */
11797 		if ((turn_on & PHYI_FAILED) &&
11798 		    ((intf_flags & PHYI_STANDBY) ||
11799 		    !ipst->ips_ipmp_enable_failback)) {
11800 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11801 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11802 		}
11803 		if ((turn_off & PHYI_FAILED) &&
11804 		    ((intf_flags & PHYI_STANDBY) ||
11805 		    (!ipst->ips_ipmp_enable_failback &&
11806 		    ill_is_inactive(ill)))) {
11807 			phyint_inactive(phyi);
11808 		}
11809 
11810 		if (turn_on & PHYI_STANDBY) {
11811 			/*
11812 			 * We implicitly set INACTIVE only when STANDBY is set.
11813 			 * INACTIVE is also set on non-STANDBY phyint when user
11814 			 * disables FAILBACK using configuration file.
11815 			 * Do not allow STANDBY to be set on such INACTIVE
11816 			 * phyint
11817 			 */
11818 			if (phyi->phyint_flags & PHYI_INACTIVE)
11819 				return (EINVAL);
11820 			if (!(phyi->phyint_flags & PHYI_FAILED))
11821 				phyint_inactive(phyi);
11822 		}
11823 		if (turn_off & PHYI_STANDBY) {
11824 			if (ipst->ips_ipmp_enable_failback) {
11825 				/*
11826 				 * Reset PHYI_INACTIVE.
11827 				 */
11828 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11829 			} else if (ill_is_inactive(ill) &&
11830 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11831 				/*
11832 				 * Need to set INACTIVE, when user sets
11833 				 * STANDBY on a non-STANDBY phyint and
11834 				 * later resets STANDBY
11835 				 */
11836 				phyint_inactive(phyi);
11837 			}
11838 		}
11839 		/*
11840 		 * We should always send up a message so that the
11841 		 * daemons come to know of it. Note that the zeroth
11842 		 * interface can be down and the check below for IPIF_UP
11843 		 * will not make sense as we are actually setting
11844 		 * a phyint flag here. We assume that the ipif used
11845 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11846 		 * send up any message for non-zero ipifs).
11847 		 */
11848 		phyint_flags_modified = B_TRUE;
11849 
11850 		if (ill_v4 != NULL) {
11851 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11852 			    IRE_CACHE, ill_stq_cache_delete,
11853 			    (char *)ill_v4, ill_v4);
11854 			illgrp_reset_schednext(ill_v4);
11855 		}
11856 		if (ill_v6 != NULL) {
11857 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11858 			    IRE_CACHE, ill_stq_cache_delete,
11859 			    (char *)ill_v6, ill_v6);
11860 			illgrp_reset_schednext(ill_v6);
11861 		}
11862 	}
11863 
11864 	/*
11865 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11866 	 * status of the interface and, if the interface is part of an IPMP
11867 	 * group, all other interfaces that are part of the same IPMP
11868 	 * group.
11869 	 */
11870 	if ((turn_on | turn_off) & ILLF_ROUTER)
11871 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11872 
11873 	/*
11874 	 * If the interface is not UP and we are not going to
11875 	 * bring it UP, record the flags and return. When the
11876 	 * interface comes UP later, the right actions will be
11877 	 * taken.
11878 	 */
11879 	if (!(ipif->ipif_flags & IPIF_UP) &&
11880 	    !(turn_on & IPIF_UP)) {
11881 		/* Record new flags in their respective places. */
11882 		mutex_enter(&ill->ill_lock);
11883 		mutex_enter(&ill->ill_phyint->phyint_lock);
11884 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11885 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11886 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11887 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11888 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11889 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11890 		mutex_exit(&ill->ill_lock);
11891 		mutex_exit(&ill->ill_phyint->phyint_lock);
11892 
11893 		/*
11894 		 * We do the broadcast and nomination here rather
11895 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11896 		 * the case of FAILBACK from INACTIVE standby to the
11897 		 * interface that has been repaired, PHYI_FAILED has not
11898 		 * been cleared yet. If there are only two interfaces in
11899 		 * that group, all we have is a FAILED and INACTIVE
11900 		 * interface. If we do the nomination soon after a failback,
11901 		 * the broadcast nomination code would select the
11902 		 * INACTIVE interface for receiving broadcasts as FAILED is
11903 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11904 		 * receive broadcast packets, we need to redo nomination
11905 		 * when the FAILED is cleared here. Thus, in general we
11906 		 * always do the nomination here for FAILED, STANDBY
11907 		 * and OFFLINE.
11908 		 */
11909 		if (((turn_on | turn_off) &
11910 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11911 			ip_redo_nomination(phyi);
11912 		}
11913 		if (phyint_flags_modified) {
11914 			if (phyi->phyint_illv4 != NULL) {
11915 				ip_rts_ifmsg(phyi->phyint_illv4->
11916 				    ill_ipif);
11917 			}
11918 			if (phyi->phyint_illv6 != NULL) {
11919 				ip_rts_ifmsg(phyi->phyint_illv6->
11920 				    ill_ipif);
11921 			}
11922 		}
11923 		return (0);
11924 	} else if (set_linklocal || zero_source) {
11925 		mutex_enter(&ill->ill_lock);
11926 		if (set_linklocal)
11927 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11928 		if (zero_source)
11929 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11930 		mutex_exit(&ill->ill_lock);
11931 	}
11932 
11933 	/*
11934 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11935 	 * or point-to-point interfaces with an unspecified destination. We do
11936 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11937 	 * have a subnet assigned, which is how in.ndpd currently manages its
11938 	 * onlink prefix list when no addresses are configured with those
11939 	 * prefixes.
11940 	 */
11941 	if (ipif->ipif_isv6 &&
11942 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11943 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11944 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11945 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11946 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11947 		return (EINVAL);
11948 	}
11949 
11950 	/*
11951 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11952 	 * from being brought up.
11953 	 */
11954 	if (!ipif->ipif_isv6 &&
11955 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11956 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11957 		return (EINVAL);
11958 	}
11959 
11960 	/*
11961 	 * The only flag changes that we currently take specific action on
11962 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11963 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11964 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11965 	 * the flags and bringing it back up again.
11966 	 */
11967 	if ((turn_on|turn_off) &
11968 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11969 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11970 		/*
11971 		 * Taking this ipif down, make sure we have
11972 		 * valid net and subnet bcast ire's for other
11973 		 * logical interfaces, if we need them.
11974 		 */
11975 		if (!ipif->ipif_isv6)
11976 			ipif_check_bcast_ires(ipif);
11977 
11978 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11979 		    !(turn_off & IPIF_UP)) {
11980 			need_up = B_TRUE;
11981 			if (ipif->ipif_flags & IPIF_UP)
11982 				ill->ill_logical_down = 1;
11983 			turn_on &= ~IPIF_UP;
11984 		}
11985 		err = ipif_down(ipif, q, mp);
11986 		ip1dbg(("ipif_down returns %d err ", err));
11987 		if (err == EINPROGRESS)
11988 			return (err);
11989 		ipif_down_tail(ipif);
11990 	}
11991 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11992 }
11993 
11994 static int
11995 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11996     boolean_t need_up)
11997 {
11998 	ill_t	*ill;
11999 	phyint_t *phyi;
12000 	uint64_t turn_on;
12001 	uint64_t turn_off;
12002 	uint64_t intf_flags;
12003 	boolean_t phyint_flags_modified = B_FALSE;
12004 	int	err = 0;
12005 	boolean_t set_linklocal = B_FALSE;
12006 	boolean_t zero_source = B_FALSE;
12007 
12008 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12009 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12010 
12011 	ASSERT(IAM_WRITER_IPIF(ipif));
12012 
12013 	ill = ipif->ipif_ill;
12014 	phyi = ill->ill_phyint;
12015 
12016 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12017 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12018 
12019 	turn_off = intf_flags & turn_on;
12020 	turn_on ^= turn_off;
12021 
12022 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12023 		phyint_flags_modified = B_TRUE;
12024 
12025 	/*
12026 	 * Now we change the flags. Track current value of
12027 	 * other flags in their respective places.
12028 	 */
12029 	mutex_enter(&ill->ill_lock);
12030 	mutex_enter(&phyi->phyint_lock);
12031 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12032 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12033 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12034 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12035 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12036 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12037 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12038 		set_linklocal = B_TRUE;
12039 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12040 	}
12041 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12042 		zero_source = B_TRUE;
12043 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12044 	}
12045 	mutex_exit(&ill->ill_lock);
12046 	mutex_exit(&phyi->phyint_lock);
12047 
12048 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12049 		ip_redo_nomination(phyi);
12050 
12051 	if (set_linklocal)
12052 		(void) ipif_setlinklocal(ipif);
12053 
12054 	if (zero_source)
12055 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12056 	else
12057 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12058 
12059 	if (need_up) {
12060 		/*
12061 		 * XXX ipif_up really does not know whether a phyint flags
12062 		 * was modified or not. So, it sends up information on
12063 		 * only one routing sockets message. As we don't bring up
12064 		 * the interface and also set STANDBY/FAILED simultaneously
12065 		 * it should be okay.
12066 		 */
12067 		err = ipif_up(ipif, q, mp);
12068 	} else {
12069 		/*
12070 		 * Make sure routing socket sees all changes to the flags.
12071 		 * ipif_up_done* handles this when we use ipif_up.
12072 		 */
12073 		if (phyint_flags_modified) {
12074 			if (phyi->phyint_illv4 != NULL) {
12075 				ip_rts_ifmsg(phyi->phyint_illv4->
12076 				    ill_ipif);
12077 			}
12078 			if (phyi->phyint_illv6 != NULL) {
12079 				ip_rts_ifmsg(phyi->phyint_illv6->
12080 				    ill_ipif);
12081 			}
12082 		} else {
12083 			ip_rts_ifmsg(ipif);
12084 		}
12085 		/*
12086 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12087 		 * this in need_up case.
12088 		 */
12089 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12090 	}
12091 	return (err);
12092 }
12093 
12094 /*
12095  * Restart entry point to restart the flags restart operation after the
12096  * refcounts have dropped to zero.
12097  */
12098 /* ARGSUSED */
12099 int
12100 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12101     ip_ioctl_cmd_t *ipip, void *if_req)
12102 {
12103 	int	err;
12104 	struct ifreq *ifr = (struct ifreq *)if_req;
12105 	struct lifreq *lifr = (struct lifreq *)if_req;
12106 
12107 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12108 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12109 
12110 	ipif_down_tail(ipif);
12111 	if (ipip->ipi_cmd_type == IF_CMD) {
12112 		/*
12113 		 * Since ip_sioctl_flags expects an int and ifr_flags
12114 		 * is a short we need to cast ifr_flags into an int
12115 		 * to avoid having sign extension cause bits to get
12116 		 * set that should not be.
12117 		 */
12118 		err = ip_sioctl_flags_tail(ipif,
12119 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12120 		    q, mp, B_TRUE);
12121 	} else {
12122 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12123 		    q, mp, B_TRUE);
12124 	}
12125 	return (err);
12126 }
12127 
12128 /*
12129  * Can operate on either a module or a driver queue.
12130  */
12131 /* ARGSUSED */
12132 int
12133 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12134     ip_ioctl_cmd_t *ipip, void *if_req)
12135 {
12136 	/*
12137 	 * Has the flags been set correctly till now ?
12138 	 */
12139 	ill_t *ill = ipif->ipif_ill;
12140 	phyint_t *phyi = ill->ill_phyint;
12141 
12142 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12143 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12144 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12145 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12146 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12147 
12148 	/*
12149 	 * Need a lock since some flags can be set even when there are
12150 	 * references to the ipif.
12151 	 */
12152 	mutex_enter(&ill->ill_lock);
12153 	if (ipip->ipi_cmd_type == IF_CMD) {
12154 		struct ifreq *ifr = (struct ifreq *)if_req;
12155 
12156 		/* Get interface flags (low 16 only). */
12157 		ifr->ifr_flags = ((ipif->ipif_flags |
12158 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12159 	} else {
12160 		struct lifreq *lifr = (struct lifreq *)if_req;
12161 
12162 		/* Get interface flags. */
12163 		lifr->lifr_flags = ipif->ipif_flags |
12164 		    ill->ill_flags | phyi->phyint_flags;
12165 	}
12166 	mutex_exit(&ill->ill_lock);
12167 	return (0);
12168 }
12169 
12170 /* ARGSUSED */
12171 int
12172 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12173     ip_ioctl_cmd_t *ipip, void *if_req)
12174 {
12175 	int mtu;
12176 	int ip_min_mtu;
12177 	struct ifreq	*ifr;
12178 	struct lifreq *lifr;
12179 	ire_t	*ire;
12180 	ip_stack_t *ipst;
12181 
12182 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12183 	    ipif->ipif_id, (void *)ipif));
12184 	if (ipip->ipi_cmd_type == IF_CMD) {
12185 		ifr = (struct ifreq *)if_req;
12186 		mtu = ifr->ifr_metric;
12187 	} else {
12188 		lifr = (struct lifreq *)if_req;
12189 		mtu = lifr->lifr_mtu;
12190 	}
12191 
12192 	if (ipif->ipif_isv6)
12193 		ip_min_mtu = IPV6_MIN_MTU;
12194 	else
12195 		ip_min_mtu = IP_MIN_MTU;
12196 
12197 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12198 		return (EINVAL);
12199 
12200 	/*
12201 	 * Change the MTU size in all relevant ire's.
12202 	 * Mtu change Vs. new ire creation - protocol below.
12203 	 * First change ipif_mtu and the ire_max_frag of the
12204 	 * interface ire. Then do an ire walk and change the
12205 	 * ire_max_frag of all affected ires. During ire_add
12206 	 * under the bucket lock, set the ire_max_frag of the
12207 	 * new ire being created from the ipif/ire from which
12208 	 * it is being derived. If an mtu change happens after
12209 	 * the ire is added, the new ire will be cleaned up.
12210 	 * Conversely if the mtu change happens before the ire
12211 	 * is added, ire_add will see the new value of the mtu.
12212 	 */
12213 	ipif->ipif_mtu = mtu;
12214 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12215 
12216 	if (ipif->ipif_isv6)
12217 		ire = ipif_to_ire_v6(ipif);
12218 	else
12219 		ire = ipif_to_ire(ipif);
12220 	if (ire != NULL) {
12221 		ire->ire_max_frag = ipif->ipif_mtu;
12222 		ire_refrele(ire);
12223 	}
12224 	ipst = ipif->ipif_ill->ill_ipst;
12225 	if (ipif->ipif_flags & IPIF_UP) {
12226 		if (ipif->ipif_isv6)
12227 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12228 			    ipst);
12229 		else
12230 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12231 			    ipst);
12232 	}
12233 	/* Update the MTU in SCTP's list */
12234 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12235 	return (0);
12236 }
12237 
12238 /* Get interface MTU. */
12239 /* ARGSUSED */
12240 int
12241 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12242 	ip_ioctl_cmd_t *ipip, void *if_req)
12243 {
12244 	struct ifreq	*ifr;
12245 	struct lifreq	*lifr;
12246 
12247 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12248 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12249 	if (ipip->ipi_cmd_type == IF_CMD) {
12250 		ifr = (struct ifreq *)if_req;
12251 		ifr->ifr_metric = ipif->ipif_mtu;
12252 	} else {
12253 		lifr = (struct lifreq *)if_req;
12254 		lifr->lifr_mtu = ipif->ipif_mtu;
12255 	}
12256 	return (0);
12257 }
12258 
12259 /* Set interface broadcast address. */
12260 /* ARGSUSED2 */
12261 int
12262 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12263 	ip_ioctl_cmd_t *ipip, void *if_req)
12264 {
12265 	ipaddr_t addr;
12266 	ire_t	*ire;
12267 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12268 
12269 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12270 	    ipif->ipif_id));
12271 
12272 	ASSERT(IAM_WRITER_IPIF(ipif));
12273 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12274 		return (EADDRNOTAVAIL);
12275 
12276 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12277 
12278 	if (sin->sin_family != AF_INET)
12279 		return (EAFNOSUPPORT);
12280 
12281 	addr = sin->sin_addr.s_addr;
12282 	if (ipif->ipif_flags & IPIF_UP) {
12283 		/*
12284 		 * If we are already up, make sure the new
12285 		 * broadcast address makes sense.  If it does,
12286 		 * there should be an IRE for it already.
12287 		 * Don't match on ipif, only on the ill
12288 		 * since we are sharing these now. Don't use
12289 		 * MATCH_IRE_ILL_GROUP as we are looking for
12290 		 * the broadcast ire on this ill and each ill
12291 		 * in the group has its own broadcast ire.
12292 		 */
12293 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12294 		    ipif, ALL_ZONES, NULL,
12295 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12296 		if (ire == NULL) {
12297 			return (EINVAL);
12298 		} else {
12299 			ire_refrele(ire);
12300 		}
12301 	}
12302 	/*
12303 	 * Changing the broadcast addr for this ipif.
12304 	 * Make sure we have valid net and subnet bcast
12305 	 * ire's for other logical interfaces, if needed.
12306 	 */
12307 	if (addr != ipif->ipif_brd_addr)
12308 		ipif_check_bcast_ires(ipif);
12309 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12310 	return (0);
12311 }
12312 
12313 /* Get interface broadcast address. */
12314 /* ARGSUSED */
12315 int
12316 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12317     ip_ioctl_cmd_t *ipip, void *if_req)
12318 {
12319 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12320 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12321 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12322 		return (EADDRNOTAVAIL);
12323 
12324 	/* IPIF_BROADCAST not possible with IPv6 */
12325 	ASSERT(!ipif->ipif_isv6);
12326 	*sin = sin_null;
12327 	sin->sin_family = AF_INET;
12328 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12329 	return (0);
12330 }
12331 
12332 /*
12333  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12334  */
12335 /* ARGSUSED */
12336 int
12337 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12338     ip_ioctl_cmd_t *ipip, void *if_req)
12339 {
12340 	int err = 0;
12341 	in6_addr_t v6mask;
12342 
12343 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12344 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12345 
12346 	ASSERT(IAM_WRITER_IPIF(ipif));
12347 
12348 	if (ipif->ipif_isv6) {
12349 		sin6_t *sin6;
12350 
12351 		if (sin->sin_family != AF_INET6)
12352 			return (EAFNOSUPPORT);
12353 
12354 		sin6 = (sin6_t *)sin;
12355 		v6mask = sin6->sin6_addr;
12356 	} else {
12357 		ipaddr_t mask;
12358 
12359 		if (sin->sin_family != AF_INET)
12360 			return (EAFNOSUPPORT);
12361 
12362 		mask = sin->sin_addr.s_addr;
12363 		V4MASK_TO_V6(mask, v6mask);
12364 	}
12365 
12366 	/*
12367 	 * No big deal if the interface isn't already up, or the mask
12368 	 * isn't really changing, or this is pt-pt.
12369 	 */
12370 	if (!(ipif->ipif_flags & IPIF_UP) ||
12371 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12372 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12373 		ipif->ipif_v6net_mask = v6mask;
12374 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12375 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12376 			    ipif->ipif_v6net_mask,
12377 			    ipif->ipif_v6subnet);
12378 		}
12379 		return (0);
12380 	}
12381 	/*
12382 	 * Make sure we have valid net and subnet broadcast ire's
12383 	 * for the old netmask, if needed by other logical interfaces.
12384 	 */
12385 	if (!ipif->ipif_isv6)
12386 		ipif_check_bcast_ires(ipif);
12387 
12388 	err = ipif_logical_down(ipif, q, mp);
12389 	if (err == EINPROGRESS)
12390 		return (err);
12391 	ipif_down_tail(ipif);
12392 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12393 	return (err);
12394 }
12395 
12396 static int
12397 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12398 {
12399 	in6_addr_t v6mask;
12400 	int err = 0;
12401 
12402 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12403 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12404 
12405 	if (ipif->ipif_isv6) {
12406 		sin6_t *sin6;
12407 
12408 		sin6 = (sin6_t *)sin;
12409 		v6mask = sin6->sin6_addr;
12410 	} else {
12411 		ipaddr_t mask;
12412 
12413 		mask = sin->sin_addr.s_addr;
12414 		V4MASK_TO_V6(mask, v6mask);
12415 	}
12416 
12417 	ipif->ipif_v6net_mask = v6mask;
12418 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12419 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12420 		    ipif->ipif_v6subnet);
12421 	}
12422 	err = ipif_up(ipif, q, mp);
12423 
12424 	if (err == 0 || err == EINPROGRESS) {
12425 		/*
12426 		 * The interface must be DL_BOUND if this packet has to
12427 		 * go out on the wire. Since we only go through a logical
12428 		 * down and are bound with the driver during an internal
12429 		 * down/up that is satisfied.
12430 		 */
12431 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12432 			/* Potentially broadcast an address mask reply. */
12433 			ipif_mask_reply(ipif);
12434 		}
12435 	}
12436 	return (err);
12437 }
12438 
12439 /* ARGSUSED */
12440 int
12441 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12442     ip_ioctl_cmd_t *ipip, void *if_req)
12443 {
12444 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12445 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12446 	ipif_down_tail(ipif);
12447 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12448 }
12449 
12450 /* Get interface net mask. */
12451 /* ARGSUSED */
12452 int
12453 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12454     ip_ioctl_cmd_t *ipip, void *if_req)
12455 {
12456 	struct lifreq *lifr = (struct lifreq *)if_req;
12457 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12458 
12459 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12460 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12461 
12462 	/*
12463 	 * net mask can't change since we have a reference to the ipif.
12464 	 */
12465 	if (ipif->ipif_isv6) {
12466 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12467 		*sin6 = sin6_null;
12468 		sin6->sin6_family = AF_INET6;
12469 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12470 		lifr->lifr_addrlen =
12471 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12472 	} else {
12473 		*sin = sin_null;
12474 		sin->sin_family = AF_INET;
12475 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12476 		if (ipip->ipi_cmd_type == LIF_CMD) {
12477 			lifr->lifr_addrlen =
12478 			    ip_mask_to_plen(ipif->ipif_net_mask);
12479 		}
12480 	}
12481 	return (0);
12482 }
12483 
12484 /* ARGSUSED */
12485 int
12486 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12487     ip_ioctl_cmd_t *ipip, void *if_req)
12488 {
12489 
12490 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12491 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12492 	/*
12493 	 * Set interface metric.  We don't use this for
12494 	 * anything but we keep track of it in case it is
12495 	 * important to routing applications or such.
12496 	 */
12497 	if (ipip->ipi_cmd_type == IF_CMD) {
12498 		struct ifreq    *ifr;
12499 
12500 		ifr = (struct ifreq *)if_req;
12501 		ipif->ipif_metric = ifr->ifr_metric;
12502 	} else {
12503 		struct lifreq   *lifr;
12504 
12505 		lifr = (struct lifreq *)if_req;
12506 		ipif->ipif_metric = lifr->lifr_metric;
12507 	}
12508 	return (0);
12509 }
12510 
12511 
12512 /* ARGSUSED */
12513 int
12514 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12515     ip_ioctl_cmd_t *ipip, void *if_req)
12516 {
12517 
12518 	/* Get interface metric. */
12519 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12520 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12521 	if (ipip->ipi_cmd_type == IF_CMD) {
12522 		struct ifreq    *ifr;
12523 
12524 		ifr = (struct ifreq *)if_req;
12525 		ifr->ifr_metric = ipif->ipif_metric;
12526 	} else {
12527 		struct lifreq   *lifr;
12528 
12529 		lifr = (struct lifreq *)if_req;
12530 		lifr->lifr_metric = ipif->ipif_metric;
12531 	}
12532 
12533 	return (0);
12534 }
12535 
12536 /* ARGSUSED */
12537 int
12538 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12539     ip_ioctl_cmd_t *ipip, void *if_req)
12540 {
12541 
12542 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12543 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12544 	/*
12545 	 * Set the muxid returned from I_PLINK.
12546 	 */
12547 	if (ipip->ipi_cmd_type == IF_CMD) {
12548 		struct ifreq *ifr = (struct ifreq *)if_req;
12549 
12550 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12551 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12552 	} else {
12553 		struct lifreq *lifr = (struct lifreq *)if_req;
12554 
12555 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12556 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12557 	}
12558 	return (0);
12559 }
12560 
12561 /* ARGSUSED */
12562 int
12563 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12564     ip_ioctl_cmd_t *ipip, void *if_req)
12565 {
12566 
12567 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12568 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12569 	/*
12570 	 * Get the muxid saved in ill for I_PUNLINK.
12571 	 */
12572 	if (ipip->ipi_cmd_type == IF_CMD) {
12573 		struct ifreq *ifr = (struct ifreq *)if_req;
12574 
12575 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12576 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12577 	} else {
12578 		struct lifreq *lifr = (struct lifreq *)if_req;
12579 
12580 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12581 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12582 	}
12583 	return (0);
12584 }
12585 
12586 /*
12587  * Set the subnet prefix. Does not modify the broadcast address.
12588  */
12589 /* ARGSUSED */
12590 int
12591 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12592     ip_ioctl_cmd_t *ipip, void *if_req)
12593 {
12594 	int err = 0;
12595 	in6_addr_t v6addr;
12596 	in6_addr_t v6mask;
12597 	boolean_t need_up = B_FALSE;
12598 	int addrlen;
12599 
12600 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12601 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12602 
12603 	ASSERT(IAM_WRITER_IPIF(ipif));
12604 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12605 
12606 	if (ipif->ipif_isv6) {
12607 		sin6_t *sin6;
12608 
12609 		if (sin->sin_family != AF_INET6)
12610 			return (EAFNOSUPPORT);
12611 
12612 		sin6 = (sin6_t *)sin;
12613 		v6addr = sin6->sin6_addr;
12614 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12615 			return (EADDRNOTAVAIL);
12616 	} else {
12617 		ipaddr_t addr;
12618 
12619 		if (sin->sin_family != AF_INET)
12620 			return (EAFNOSUPPORT);
12621 
12622 		addr = sin->sin_addr.s_addr;
12623 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12624 			return (EADDRNOTAVAIL);
12625 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12626 		/* Add 96 bits */
12627 		addrlen += IPV6_ABITS - IP_ABITS;
12628 	}
12629 
12630 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12631 		return (EINVAL);
12632 
12633 	/* Check if bits in the address is set past the mask */
12634 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12635 		return (EINVAL);
12636 
12637 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12638 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12639 		return (0);	/* No change */
12640 
12641 	if (ipif->ipif_flags & IPIF_UP) {
12642 		/*
12643 		 * If the interface is already marked up,
12644 		 * we call ipif_down which will take care
12645 		 * of ditching any IREs that have been set
12646 		 * up based on the old interface address.
12647 		 */
12648 		err = ipif_logical_down(ipif, q, mp);
12649 		if (err == EINPROGRESS)
12650 			return (err);
12651 		ipif_down_tail(ipif);
12652 		need_up = B_TRUE;
12653 	}
12654 
12655 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12656 	return (err);
12657 }
12658 
12659 static int
12660 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12661     queue_t *q, mblk_t *mp, boolean_t need_up)
12662 {
12663 	ill_t	*ill = ipif->ipif_ill;
12664 	int	err = 0;
12665 
12666 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12667 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12668 
12669 	/* Set the new address. */
12670 	mutex_enter(&ill->ill_lock);
12671 	ipif->ipif_v6net_mask = v6mask;
12672 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12673 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12674 		    ipif->ipif_v6subnet);
12675 	}
12676 	mutex_exit(&ill->ill_lock);
12677 
12678 	if (need_up) {
12679 		/*
12680 		 * Now bring the interface back up.  If this
12681 		 * is the only IPIF for the ILL, ipif_up
12682 		 * will have to re-bind to the device, so
12683 		 * we may get back EINPROGRESS, in which
12684 		 * case, this IOCTL will get completed in
12685 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12686 		 */
12687 		err = ipif_up(ipif, q, mp);
12688 		if (err == EINPROGRESS)
12689 			return (err);
12690 	}
12691 	return (err);
12692 }
12693 
12694 /* ARGSUSED */
12695 int
12696 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12697     ip_ioctl_cmd_t *ipip, void *if_req)
12698 {
12699 	int	addrlen;
12700 	in6_addr_t v6addr;
12701 	in6_addr_t v6mask;
12702 	struct lifreq *lifr = (struct lifreq *)if_req;
12703 
12704 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12705 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12706 	ipif_down_tail(ipif);
12707 
12708 	addrlen = lifr->lifr_addrlen;
12709 	if (ipif->ipif_isv6) {
12710 		sin6_t *sin6;
12711 
12712 		sin6 = (sin6_t *)sin;
12713 		v6addr = sin6->sin6_addr;
12714 	} else {
12715 		ipaddr_t addr;
12716 
12717 		addr = sin->sin_addr.s_addr;
12718 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12719 		addrlen += IPV6_ABITS - IP_ABITS;
12720 	}
12721 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12722 
12723 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12724 }
12725 
12726 /* ARGSUSED */
12727 int
12728 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12729     ip_ioctl_cmd_t *ipip, void *if_req)
12730 {
12731 	struct lifreq *lifr = (struct lifreq *)if_req;
12732 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12733 
12734 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12735 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12736 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12737 
12738 	if (ipif->ipif_isv6) {
12739 		*sin6 = sin6_null;
12740 		sin6->sin6_family = AF_INET6;
12741 		sin6->sin6_addr = ipif->ipif_v6subnet;
12742 		lifr->lifr_addrlen =
12743 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12744 	} else {
12745 		*sin = sin_null;
12746 		sin->sin_family = AF_INET;
12747 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12748 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12749 	}
12750 	return (0);
12751 }
12752 
12753 /*
12754  * Set the IPv6 address token.
12755  */
12756 /* ARGSUSED */
12757 int
12758 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12759     ip_ioctl_cmd_t *ipi, void *if_req)
12760 {
12761 	ill_t *ill = ipif->ipif_ill;
12762 	int err;
12763 	in6_addr_t v6addr;
12764 	in6_addr_t v6mask;
12765 	boolean_t need_up = B_FALSE;
12766 	int i;
12767 	sin6_t *sin6 = (sin6_t *)sin;
12768 	struct lifreq *lifr = (struct lifreq *)if_req;
12769 	int addrlen;
12770 
12771 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12772 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12773 	ASSERT(IAM_WRITER_IPIF(ipif));
12774 
12775 	addrlen = lifr->lifr_addrlen;
12776 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12777 	if (ipif->ipif_id != 0)
12778 		return (EINVAL);
12779 
12780 	if (!ipif->ipif_isv6)
12781 		return (EINVAL);
12782 
12783 	if (addrlen > IPV6_ABITS)
12784 		return (EINVAL);
12785 
12786 	v6addr = sin6->sin6_addr;
12787 
12788 	/*
12789 	 * The length of the token is the length from the end.  To get
12790 	 * the proper mask for this, compute the mask of the bits not
12791 	 * in the token; ie. the prefix, and then xor to get the mask.
12792 	 */
12793 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12794 		return (EINVAL);
12795 	for (i = 0; i < 4; i++) {
12796 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12797 	}
12798 
12799 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12800 	    ill->ill_token_length == addrlen)
12801 		return (0);	/* No change */
12802 
12803 	if (ipif->ipif_flags & IPIF_UP) {
12804 		err = ipif_logical_down(ipif, q, mp);
12805 		if (err == EINPROGRESS)
12806 			return (err);
12807 		ipif_down_tail(ipif);
12808 		need_up = B_TRUE;
12809 	}
12810 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12811 	return (err);
12812 }
12813 
12814 static int
12815 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12816     mblk_t *mp, boolean_t need_up)
12817 {
12818 	in6_addr_t v6addr;
12819 	in6_addr_t v6mask;
12820 	ill_t	*ill = ipif->ipif_ill;
12821 	int	i;
12822 	int	err = 0;
12823 
12824 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12825 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12826 	v6addr = sin6->sin6_addr;
12827 	/*
12828 	 * The length of the token is the length from the end.  To get
12829 	 * the proper mask for this, compute the mask of the bits not
12830 	 * in the token; ie. the prefix, and then xor to get the mask.
12831 	 */
12832 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12833 	for (i = 0; i < 4; i++)
12834 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12835 
12836 	mutex_enter(&ill->ill_lock);
12837 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12838 	ill->ill_token_length = addrlen;
12839 	mutex_exit(&ill->ill_lock);
12840 
12841 	if (need_up) {
12842 		/*
12843 		 * Now bring the interface back up.  If this
12844 		 * is the only IPIF for the ILL, ipif_up
12845 		 * will have to re-bind to the device, so
12846 		 * we may get back EINPROGRESS, in which
12847 		 * case, this IOCTL will get completed in
12848 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12849 		 */
12850 		err = ipif_up(ipif, q, mp);
12851 		if (err == EINPROGRESS)
12852 			return (err);
12853 	}
12854 	return (err);
12855 }
12856 
12857 /* ARGSUSED */
12858 int
12859 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12860     ip_ioctl_cmd_t *ipi, void *if_req)
12861 {
12862 	ill_t *ill;
12863 	sin6_t *sin6 = (sin6_t *)sin;
12864 	struct lifreq *lifr = (struct lifreq *)if_req;
12865 
12866 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12867 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12868 	if (ipif->ipif_id != 0)
12869 		return (EINVAL);
12870 
12871 	ill = ipif->ipif_ill;
12872 	if (!ill->ill_isv6)
12873 		return (ENXIO);
12874 
12875 	*sin6 = sin6_null;
12876 	sin6->sin6_family = AF_INET6;
12877 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12878 	sin6->sin6_addr = ill->ill_token;
12879 	lifr->lifr_addrlen = ill->ill_token_length;
12880 	return (0);
12881 }
12882 
12883 /*
12884  * Set (hardware) link specific information that might override
12885  * what was acquired through the DL_INFO_ACK.
12886  * The logic is as follows.
12887  *
12888  * become exclusive
12889  * set CHANGING flag
12890  * change mtu on affected IREs
12891  * clear CHANGING flag
12892  *
12893  * An ire add that occurs before the CHANGING flag is set will have its mtu
12894  * changed by the ip_sioctl_lnkinfo.
12895  *
12896  * During the time the CHANGING flag is set, no new ires will be added to the
12897  * bucket, and ire add will fail (due the CHANGING flag).
12898  *
12899  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12900  * before it is added to the bucket.
12901  *
12902  * Obviously only 1 thread can set the CHANGING flag and we need to become
12903  * exclusive to set the flag.
12904  */
12905 /* ARGSUSED */
12906 int
12907 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12908     ip_ioctl_cmd_t *ipi, void *if_req)
12909 {
12910 	ill_t		*ill = ipif->ipif_ill;
12911 	ipif_t		*nipif;
12912 	int		ip_min_mtu;
12913 	boolean_t	mtu_walk = B_FALSE;
12914 	struct lifreq	*lifr = (struct lifreq *)if_req;
12915 	lif_ifinfo_req_t *lir;
12916 	ire_t		*ire;
12917 
12918 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12919 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12920 	lir = &lifr->lifr_ifinfo;
12921 	ASSERT(IAM_WRITER_IPIF(ipif));
12922 
12923 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12924 	if (ipif->ipif_id != 0)
12925 		return (EINVAL);
12926 
12927 	/* Set interface MTU. */
12928 	if (ipif->ipif_isv6)
12929 		ip_min_mtu = IPV6_MIN_MTU;
12930 	else
12931 		ip_min_mtu = IP_MIN_MTU;
12932 
12933 	/*
12934 	 * Verify values before we set anything. Allow zero to
12935 	 * mean unspecified.
12936 	 */
12937 	if (lir->lir_maxmtu != 0 &&
12938 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12939 	    lir->lir_maxmtu < ip_min_mtu))
12940 		return (EINVAL);
12941 	if (lir->lir_reachtime != 0 &&
12942 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12943 		return (EINVAL);
12944 	if (lir->lir_reachretrans != 0 &&
12945 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12946 		return (EINVAL);
12947 
12948 	mutex_enter(&ill->ill_lock);
12949 	ill->ill_state_flags |= ILL_CHANGING;
12950 	for (nipif = ill->ill_ipif; nipif != NULL;
12951 	    nipif = nipif->ipif_next) {
12952 		nipif->ipif_state_flags |= IPIF_CHANGING;
12953 	}
12954 
12955 	mutex_exit(&ill->ill_lock);
12956 
12957 	if (lir->lir_maxmtu != 0) {
12958 		ill->ill_max_mtu = lir->lir_maxmtu;
12959 		ill->ill_mtu_userspecified = 1;
12960 		mtu_walk = B_TRUE;
12961 	}
12962 
12963 	if (lir->lir_reachtime != 0)
12964 		ill->ill_reachable_time = lir->lir_reachtime;
12965 
12966 	if (lir->lir_reachretrans != 0)
12967 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12968 
12969 	ill->ill_max_hops = lir->lir_maxhops;
12970 
12971 	ill->ill_max_buf = ND_MAX_Q;
12972 
12973 	if (mtu_walk) {
12974 		/*
12975 		 * Set the MTU on all ipifs associated with this ill except
12976 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12977 		 */
12978 		for (nipif = ill->ill_ipif; nipif != NULL;
12979 		    nipif = nipif->ipif_next) {
12980 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12981 				continue;
12982 
12983 			nipif->ipif_mtu = ill->ill_max_mtu;
12984 
12985 			if (!(nipif->ipif_flags & IPIF_UP))
12986 				continue;
12987 
12988 			if (nipif->ipif_isv6)
12989 				ire = ipif_to_ire_v6(nipif);
12990 			else
12991 				ire = ipif_to_ire(nipif);
12992 			if (ire != NULL) {
12993 				ire->ire_max_frag = ipif->ipif_mtu;
12994 				ire_refrele(ire);
12995 			}
12996 			if (ill->ill_isv6) {
12997 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12998 				    ipif_mtu_change, (char *)nipif,
12999 				    ill);
13000 			} else {
13001 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13002 				    ipif_mtu_change, (char *)nipif,
13003 				    ill);
13004 			}
13005 		}
13006 	}
13007 
13008 	mutex_enter(&ill->ill_lock);
13009 	for (nipif = ill->ill_ipif; nipif != NULL;
13010 	    nipif = nipif->ipif_next) {
13011 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13012 	}
13013 	ILL_UNMARK_CHANGING(ill);
13014 	mutex_exit(&ill->ill_lock);
13015 
13016 	return (0);
13017 }
13018 
13019 /* ARGSUSED */
13020 int
13021 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13022     ip_ioctl_cmd_t *ipi, void *if_req)
13023 {
13024 	struct lif_ifinfo_req *lir;
13025 	ill_t *ill = ipif->ipif_ill;
13026 
13027 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13028 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13029 	if (ipif->ipif_id != 0)
13030 		return (EINVAL);
13031 
13032 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13033 	lir->lir_maxhops = ill->ill_max_hops;
13034 	lir->lir_reachtime = ill->ill_reachable_time;
13035 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13036 	lir->lir_maxmtu = ill->ill_max_mtu;
13037 
13038 	return (0);
13039 }
13040 
13041 /*
13042  * Return best guess as to the subnet mask for the specified address.
13043  * Based on the subnet masks for all the configured interfaces.
13044  *
13045  * We end up returning a zero mask in the case of default, multicast or
13046  * experimental.
13047  */
13048 static ipaddr_t
13049 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13050 {
13051 	ipaddr_t net_mask;
13052 	ill_t	*ill;
13053 	ipif_t	*ipif;
13054 	ill_walk_context_t ctx;
13055 	ipif_t	*fallback_ipif = NULL;
13056 
13057 	net_mask = ip_net_mask(addr);
13058 	if (net_mask == 0) {
13059 		*ipifp = NULL;
13060 		return (0);
13061 	}
13062 
13063 	/* Let's check to see if this is maybe a local subnet route. */
13064 	/* this function only applies to IPv4 interfaces */
13065 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13066 	ill = ILL_START_WALK_V4(&ctx, ipst);
13067 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13068 		mutex_enter(&ill->ill_lock);
13069 		for (ipif = ill->ill_ipif; ipif != NULL;
13070 		    ipif = ipif->ipif_next) {
13071 			if (!IPIF_CAN_LOOKUP(ipif))
13072 				continue;
13073 			if (!(ipif->ipif_flags & IPIF_UP))
13074 				continue;
13075 			if ((ipif->ipif_subnet & net_mask) ==
13076 			    (addr & net_mask)) {
13077 				/*
13078 				 * Don't trust pt-pt interfaces if there are
13079 				 * other interfaces.
13080 				 */
13081 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13082 					if (fallback_ipif == NULL) {
13083 						ipif_refhold_locked(ipif);
13084 						fallback_ipif = ipif;
13085 					}
13086 					continue;
13087 				}
13088 
13089 				/*
13090 				 * Fine. Just assume the same net mask as the
13091 				 * directly attached subnet interface is using.
13092 				 */
13093 				ipif_refhold_locked(ipif);
13094 				mutex_exit(&ill->ill_lock);
13095 				rw_exit(&ipst->ips_ill_g_lock);
13096 				if (fallback_ipif != NULL)
13097 					ipif_refrele(fallback_ipif);
13098 				*ipifp = ipif;
13099 				return (ipif->ipif_net_mask);
13100 			}
13101 		}
13102 		mutex_exit(&ill->ill_lock);
13103 	}
13104 	rw_exit(&ipst->ips_ill_g_lock);
13105 
13106 	*ipifp = fallback_ipif;
13107 	return ((fallback_ipif != NULL) ?
13108 	    fallback_ipif->ipif_net_mask : net_mask);
13109 }
13110 
13111 /*
13112  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13113  */
13114 static void
13115 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13116 {
13117 	IOCP	iocp;
13118 	ipft_t	*ipft;
13119 	ipllc_t	*ipllc;
13120 	mblk_t	*mp1;
13121 	cred_t	*cr;
13122 	int	error = 0;
13123 	conn_t	*connp;
13124 
13125 	ip1dbg(("ip_wput_ioctl"));
13126 	iocp = (IOCP)mp->b_rptr;
13127 	mp1 = mp->b_cont;
13128 	if (mp1 == NULL) {
13129 		iocp->ioc_error = EINVAL;
13130 		mp->b_datap->db_type = M_IOCNAK;
13131 		iocp->ioc_count = 0;
13132 		qreply(q, mp);
13133 		return;
13134 	}
13135 
13136 	/*
13137 	 * These IOCTLs provide various control capabilities to
13138 	 * upstream agents such as ULPs and processes.	There
13139 	 * are currently two such IOCTLs implemented.  They
13140 	 * are used by TCP to provide update information for
13141 	 * existing IREs and to forcibly delete an IRE for a
13142 	 * host that is not responding, thereby forcing an
13143 	 * attempt at a new route.
13144 	 */
13145 	iocp->ioc_error = EINVAL;
13146 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13147 		goto done;
13148 
13149 	ipllc = (ipllc_t *)mp1->b_rptr;
13150 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13151 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13152 			break;
13153 	}
13154 	/*
13155 	 * prefer credential from mblk over ioctl;
13156 	 * see ip_sioctl_copyin_setup
13157 	 */
13158 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13159 
13160 	/*
13161 	 * Refhold the conn in case the request gets queued up in some lookup
13162 	 */
13163 	ASSERT(CONN_Q(q));
13164 	connp = Q_TO_CONN(q);
13165 	CONN_INC_REF(connp);
13166 	if (ipft->ipft_pfi &&
13167 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13168 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13169 		error = (*ipft->ipft_pfi)(q,
13170 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13171 	}
13172 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13173 		/*
13174 		 * CONN_OPER_PENDING_DONE happens in the function called
13175 		 * through ipft_pfi above.
13176 		 */
13177 		return;
13178 	}
13179 
13180 	CONN_OPER_PENDING_DONE(connp);
13181 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13182 		freemsg(mp);
13183 		return;
13184 	}
13185 	iocp->ioc_error = error;
13186 
13187 done:
13188 	mp->b_datap->db_type = M_IOCACK;
13189 	if (iocp->ioc_error)
13190 		iocp->ioc_count = 0;
13191 	qreply(q, mp);
13192 }
13193 
13194 /*
13195  * Lookup an ipif using the sequence id (ipif_seqid)
13196  */
13197 ipif_t *
13198 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13199 {
13200 	ipif_t *ipif;
13201 
13202 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13203 
13204 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13205 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13206 			return (ipif);
13207 	}
13208 	return (NULL);
13209 }
13210 
13211 /*
13212  * Assign a unique id for the ipif. This is used later when we send
13213  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13214  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13215  * IRE is added, we verify that ipif has not disappeared.
13216  */
13217 
13218 static void
13219 ipif_assign_seqid(ipif_t *ipif)
13220 {
13221 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13222 
13223 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13224 }
13225 
13226 /*
13227  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13228  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13229  * be inserted into the first space available in the list. The value of
13230  * ipif_id will then be set to the appropriate value for its position.
13231  */
13232 static int
13233 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13234 {
13235 	ill_t *ill;
13236 	ipif_t *tipif;
13237 	ipif_t **tipifp;
13238 	int id;
13239 	ip_stack_t	*ipst;
13240 
13241 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13242 	    IAM_WRITER_IPIF(ipif));
13243 
13244 	ill = ipif->ipif_ill;
13245 	ASSERT(ill != NULL);
13246 	ipst = ill->ill_ipst;
13247 
13248 	/*
13249 	 * In the case of lo0:0 we already hold the ill_g_lock.
13250 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13251 	 * ipif_insert. Another such caller is ipif_move.
13252 	 */
13253 	if (acquire_g_lock)
13254 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13255 	if (acquire_ill_lock)
13256 		mutex_enter(&ill->ill_lock);
13257 	id = ipif->ipif_id;
13258 	tipifp = &(ill->ill_ipif);
13259 	if (id == -1) {	/* need to find a real id */
13260 		id = 0;
13261 		while ((tipif = *tipifp) != NULL) {
13262 			ASSERT(tipif->ipif_id >= id);
13263 			if (tipif->ipif_id != id)
13264 				break; /* non-consecutive id */
13265 			id++;
13266 			tipifp = &(tipif->ipif_next);
13267 		}
13268 		/* limit number of logical interfaces */
13269 		if (id >= ipst->ips_ip_addrs_per_if) {
13270 			if (acquire_ill_lock)
13271 				mutex_exit(&ill->ill_lock);
13272 			if (acquire_g_lock)
13273 				rw_exit(&ipst->ips_ill_g_lock);
13274 			return (-1);
13275 		}
13276 		ipif->ipif_id = id; /* assign new id */
13277 	} else if (id < ipst->ips_ip_addrs_per_if) {
13278 		/* we have a real id; insert ipif in the right place */
13279 		while ((tipif = *tipifp) != NULL) {
13280 			ASSERT(tipif->ipif_id != id);
13281 			if (tipif->ipif_id > id)
13282 				break; /* found correct location */
13283 			tipifp = &(tipif->ipif_next);
13284 		}
13285 	} else {
13286 		if (acquire_ill_lock)
13287 			mutex_exit(&ill->ill_lock);
13288 		if (acquire_g_lock)
13289 			rw_exit(&ipst->ips_ill_g_lock);
13290 		return (-1);
13291 	}
13292 
13293 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13294 
13295 	ipif->ipif_next = tipif;
13296 	*tipifp = ipif;
13297 	if (acquire_ill_lock)
13298 		mutex_exit(&ill->ill_lock);
13299 	if (acquire_g_lock)
13300 		rw_exit(&ipst->ips_ill_g_lock);
13301 	return (0);
13302 }
13303 
13304 static void
13305 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13306 {
13307 	ipif_t	**ipifp;
13308 	ill_t	*ill = ipif->ipif_ill;
13309 
13310 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13311 	if (acquire_ill_lock)
13312 		mutex_enter(&ill->ill_lock);
13313 	else
13314 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13315 
13316 	ipifp = &ill->ill_ipif;
13317 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13318 		if (*ipifp == ipif) {
13319 			*ipifp = ipif->ipif_next;
13320 			break;
13321 		}
13322 	}
13323 
13324 	if (acquire_ill_lock)
13325 		mutex_exit(&ill->ill_lock);
13326 }
13327 
13328 /*
13329  * Allocate and initialize a new interface control structure.  (Always
13330  * called as writer.)
13331  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13332  * is not part of the global linked list of ills. ipif_seqid is unique
13333  * in the system and to preserve the uniqueness, it is assigned only
13334  * when ill becomes part of the global list. At that point ill will
13335  * have a name. If it doesn't get assigned here, it will get assigned
13336  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13337  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13338  * the interface flags or any other information from the DL_INFO_ACK for
13339  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13340  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13341  * second DL_INFO_ACK comes in from the driver.
13342  */
13343 static ipif_t *
13344 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13345 {
13346 	ipif_t	*ipif;
13347 	phyint_t *phyi;
13348 
13349 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13350 	    ill->ill_name, id, (void *)ill));
13351 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13352 
13353 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13354 		return (NULL);
13355 	*ipif = ipif_zero;	/* start clean */
13356 
13357 	ipif->ipif_ill = ill;
13358 	ipif->ipif_id = id;	/* could be -1 */
13359 	/*
13360 	 * Inherit the zoneid from the ill; for the shared stack instance
13361 	 * this is always the global zone
13362 	 */
13363 	ipif->ipif_zoneid = ill->ill_zoneid;
13364 
13365 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13366 
13367 	ipif->ipif_refcnt = 0;
13368 	ipif->ipif_saved_ire_cnt = 0;
13369 
13370 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13371 		mi_free(ipif);
13372 		return (NULL);
13373 	}
13374 	/* -1 id should have been replaced by real id */
13375 	id = ipif->ipif_id;
13376 	ASSERT(id >= 0);
13377 
13378 	if (ill->ill_name[0] != '\0')
13379 		ipif_assign_seqid(ipif);
13380 
13381 	/*
13382 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13383 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13384 	 * ioctl sets ipif_orig_ipifid to zero.
13385 	 */
13386 	ipif->ipif_orig_ipifid = id;
13387 
13388 	/*
13389 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13390 	 * The ipif is still not up and can't be looked up until the
13391 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13392 	 */
13393 	mutex_enter(&ill->ill_lock);
13394 	mutex_enter(&ill->ill_phyint->phyint_lock);
13395 	/*
13396 	 * Set the running flag when logical interface zero is created.
13397 	 * For subsequent logical interfaces, a DLPI link down
13398 	 * notification message may have cleared the running flag to
13399 	 * indicate the link is down, so we shouldn't just blindly set it.
13400 	 */
13401 	if (id == 0)
13402 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13403 	ipif->ipif_ire_type = ire_type;
13404 	phyi = ill->ill_phyint;
13405 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13406 
13407 	if (ipif->ipif_isv6) {
13408 		ill->ill_flags |= ILLF_IPV6;
13409 	} else {
13410 		ipaddr_t inaddr_any = INADDR_ANY;
13411 
13412 		ill->ill_flags |= ILLF_IPV4;
13413 
13414 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13415 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13416 		    &ipif->ipif_v6lcl_addr);
13417 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13418 		    &ipif->ipif_v6src_addr);
13419 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13420 		    &ipif->ipif_v6subnet);
13421 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13422 		    &ipif->ipif_v6net_mask);
13423 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13424 		    &ipif->ipif_v6brd_addr);
13425 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13426 		    &ipif->ipif_v6pp_dst_addr);
13427 	}
13428 
13429 	/*
13430 	 * Don't set the interface flags etc. now, will do it in
13431 	 * ip_ll_subnet_defaults.
13432 	 */
13433 	if (!initialize) {
13434 		mutex_exit(&ill->ill_lock);
13435 		mutex_exit(&ill->ill_phyint->phyint_lock);
13436 		return (ipif);
13437 	}
13438 	ipif->ipif_mtu = ill->ill_max_mtu;
13439 
13440 	if (ill->ill_bcast_addr_length != 0) {
13441 		/*
13442 		 * Later detect lack of DLPI driver multicast
13443 		 * capability by catching DL_ENABMULTI errors in
13444 		 * ip_rput_dlpi.
13445 		 */
13446 		ill->ill_flags |= ILLF_MULTICAST;
13447 		if (!ipif->ipif_isv6)
13448 			ipif->ipif_flags |= IPIF_BROADCAST;
13449 	} else {
13450 		if (ill->ill_net_type != IRE_LOOPBACK) {
13451 			if (ipif->ipif_isv6)
13452 				/*
13453 				 * Note: xresolv interfaces will eventually need
13454 				 * NOARP set here as well, but that will require
13455 				 * those external resolvers to have some
13456 				 * knowledge of that flag and act appropriately.
13457 				 * Not to be changed at present.
13458 				 */
13459 				ill->ill_flags |= ILLF_NONUD;
13460 			else
13461 				ill->ill_flags |= ILLF_NOARP;
13462 		}
13463 		if (ill->ill_phys_addr_length == 0) {
13464 			if (ill->ill_media &&
13465 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13466 				ipif->ipif_flags |= IPIF_NOXMIT;
13467 				phyi->phyint_flags |= PHYI_VIRTUAL;
13468 			} else {
13469 				/* pt-pt supports multicast. */
13470 				ill->ill_flags |= ILLF_MULTICAST;
13471 				if (ill->ill_net_type == IRE_LOOPBACK) {
13472 					phyi->phyint_flags |=
13473 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13474 				} else {
13475 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13476 				}
13477 			}
13478 		}
13479 	}
13480 	mutex_exit(&ill->ill_lock);
13481 	mutex_exit(&ill->ill_phyint->phyint_lock);
13482 	return (ipif);
13483 }
13484 
13485 /*
13486  * If appropriate, send a message up to the resolver delete the entry
13487  * for the address of this interface which is going out of business.
13488  * (Always called as writer).
13489  *
13490  * NOTE : We need to check for NULL mps as some of the fields are
13491  *	  initialized only for some interface types. See ipif_resolver_up()
13492  *	  for details.
13493  */
13494 void
13495 ipif_arp_down(ipif_t *ipif)
13496 {
13497 	mblk_t	*mp;
13498 	ill_t	*ill = ipif->ipif_ill;
13499 
13500 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13501 	ASSERT(IAM_WRITER_IPIF(ipif));
13502 
13503 	/* Delete the mapping for the local address */
13504 	mp = ipif->ipif_arp_del_mp;
13505 	if (mp != NULL) {
13506 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13507 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13508 		putnext(ill->ill_rq, mp);
13509 		ipif->ipif_arp_del_mp = NULL;
13510 	}
13511 
13512 	/*
13513 	 * If this is the last ipif that is going down and there are no
13514 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13515 	 * clean up ARP completely.
13516 	 */
13517 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13518 
13519 		/* Send up AR_INTERFACE_DOWN message */
13520 		mp = ill->ill_arp_down_mp;
13521 		if (mp != NULL) {
13522 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13523 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13524 			    ipif->ipif_id));
13525 			putnext(ill->ill_rq, mp);
13526 			ill->ill_arp_down_mp = NULL;
13527 		}
13528 
13529 		/* Tell ARP to delete the multicast mappings */
13530 		mp = ill->ill_arp_del_mapping_mp;
13531 		if (mp != NULL) {
13532 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13533 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13534 			    ipif->ipif_id));
13535 			putnext(ill->ill_rq, mp);
13536 			ill->ill_arp_del_mapping_mp = NULL;
13537 		}
13538 	}
13539 }
13540 
13541 /*
13542  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13543  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13544  * that it wants the add_mp allocated in this function to be returned
13545  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13546  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13547  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13548  * as it does a ipif_arp_down after calling this function - which will
13549  * remove what we add here.
13550  *
13551  * Returns -1 on failures and 0 on success.
13552  */
13553 int
13554 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13555 {
13556 	mblk_t	*del_mp = NULL;
13557 	mblk_t *add_mp = NULL;
13558 	mblk_t *mp;
13559 	ill_t	*ill = ipif->ipif_ill;
13560 	phyint_t *phyi = ill->ill_phyint;
13561 	ipaddr_t addr, mask, extract_mask = 0;
13562 	arma_t	*arma;
13563 	uint8_t *maddr, *bphys_addr;
13564 	uint32_t hw_start;
13565 	dl_unitdata_req_t *dlur;
13566 
13567 	ASSERT(IAM_WRITER_IPIF(ipif));
13568 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13569 		return (0);
13570 
13571 	/*
13572 	 * Delete the existing mapping from ARP. Normally ipif_down
13573 	 * -> ipif_arp_down should send this up to ARP. The only
13574 	 * reason we would find this when we are switching from
13575 	 * Multicast to Broadcast where we did not do a down.
13576 	 */
13577 	mp = ill->ill_arp_del_mapping_mp;
13578 	if (mp != NULL) {
13579 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13580 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13581 		putnext(ill->ill_rq, mp);
13582 		ill->ill_arp_del_mapping_mp = NULL;
13583 	}
13584 
13585 	if (arp_add_mapping_mp != NULL)
13586 		*arp_add_mapping_mp = NULL;
13587 
13588 	/*
13589 	 * Check that the address is not to long for the constant
13590 	 * length reserved in the template arma_t.
13591 	 */
13592 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13593 		return (-1);
13594 
13595 	/* Add mapping mblk */
13596 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13597 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13598 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13599 	    (caddr_t)&addr);
13600 	if (add_mp == NULL)
13601 		return (-1);
13602 	arma = (arma_t *)add_mp->b_rptr;
13603 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13604 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13605 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13606 
13607 	/*
13608 	 * Determine the broadcast address.
13609 	 */
13610 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13611 	if (ill->ill_sap_length < 0)
13612 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13613 	else
13614 		bphys_addr = (uchar_t *)dlur +
13615 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13616 	/*
13617 	 * Check PHYI_MULTI_BCAST and length of physical
13618 	 * address to determine if we use the mapping or the
13619 	 * broadcast address.
13620 	 */
13621 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13622 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13623 		    bphys_addr, maddr, &hw_start, &extract_mask))
13624 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13625 
13626 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13627 	    (ill->ill_flags & ILLF_MULTICAST)) {
13628 		/* Make sure this will not match the "exact" entry. */
13629 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13630 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13631 		    (caddr_t)&addr);
13632 		if (del_mp == NULL) {
13633 			freemsg(add_mp);
13634 			return (-1);
13635 		}
13636 		bcopy(&extract_mask, (char *)arma +
13637 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13638 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13639 			/* Use link-layer broadcast address for MULTI_BCAST */
13640 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13641 			ip2dbg(("ipif_arp_setup_multicast: adding"
13642 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13643 		} else {
13644 			arma->arma_hw_mapping_start = hw_start;
13645 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13646 			    " ARP setup for %s\n", ill->ill_name));
13647 		}
13648 	} else {
13649 		freemsg(add_mp);
13650 		ASSERT(del_mp == NULL);
13651 		/* It is neither MULTICAST nor MULTI_BCAST */
13652 		return (0);
13653 	}
13654 	ASSERT(add_mp != NULL && del_mp != NULL);
13655 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13656 	ill->ill_arp_del_mapping_mp = del_mp;
13657 	if (arp_add_mapping_mp != NULL) {
13658 		/* The caller just wants the mblks allocated */
13659 		*arp_add_mapping_mp = add_mp;
13660 	} else {
13661 		/* The caller wants us to send it to arp */
13662 		putnext(ill->ill_rq, add_mp);
13663 	}
13664 	return (0);
13665 }
13666 
13667 /*
13668  * Get the resolver set up for a new interface address.
13669  * (Always called as writer.)
13670  * Called both for IPv4 and IPv6 interfaces,
13671  * though it only sets up the resolver for v6
13672  * if it's an xresolv interface (one using an external resolver).
13673  * Honors ILLF_NOARP.
13674  * The enumerated value res_act is used to tune the behavior.
13675  * If set to Res_act_initial, then we set up all the resolver
13676  * structures for a new interface.  If set to Res_act_move, then
13677  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13678  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13679  * asynchronous hardware address change notification.  If set to
13680  * Res_act_defend, then we tell ARP that it needs to send a single
13681  * gratuitous message in defense of the address.
13682  * Returns error on failure.
13683  */
13684 int
13685 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13686 {
13687 	caddr_t	addr;
13688 	mblk_t	*arp_up_mp = NULL;
13689 	mblk_t	*arp_down_mp = NULL;
13690 	mblk_t	*arp_add_mp = NULL;
13691 	mblk_t	*arp_del_mp = NULL;
13692 	mblk_t	*arp_add_mapping_mp = NULL;
13693 	mblk_t	*arp_del_mapping_mp = NULL;
13694 	ill_t	*ill = ipif->ipif_ill;
13695 	uchar_t	*area_p = NULL;
13696 	uchar_t	*ared_p = NULL;
13697 	int	err = ENOMEM;
13698 	boolean_t was_dup;
13699 
13700 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13701 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13702 	ASSERT(IAM_WRITER_IPIF(ipif));
13703 
13704 	was_dup = B_FALSE;
13705 	if (res_act == Res_act_initial) {
13706 		ipif->ipif_addr_ready = 0;
13707 		/*
13708 		 * We're bringing an interface up here.  There's no way that we
13709 		 * should need to shut down ARP now.
13710 		 */
13711 		mutex_enter(&ill->ill_lock);
13712 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13713 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13714 			ill->ill_ipif_dup_count--;
13715 			was_dup = B_TRUE;
13716 		}
13717 		mutex_exit(&ill->ill_lock);
13718 	}
13719 	if (ipif->ipif_recovery_id != 0)
13720 		(void) untimeout(ipif->ipif_recovery_id);
13721 	ipif->ipif_recovery_id = 0;
13722 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13723 		ipif->ipif_addr_ready = 1;
13724 		return (0);
13725 	}
13726 	/* NDP will set the ipif_addr_ready flag when it's ready */
13727 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13728 		return (0);
13729 
13730 	if (ill->ill_isv6) {
13731 		/*
13732 		 * External resolver for IPv6
13733 		 */
13734 		ASSERT(res_act == Res_act_initial);
13735 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13736 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13737 			area_p = (uchar_t *)&ip6_area_template;
13738 			ared_p = (uchar_t *)&ip6_ared_template;
13739 		}
13740 	} else {
13741 		/*
13742 		 * IPv4 arp case. If the ARP stream has already started
13743 		 * closing, fail this request for ARP bringup. Else
13744 		 * record the fact that an ARP bringup is pending.
13745 		 */
13746 		mutex_enter(&ill->ill_lock);
13747 		if (ill->ill_arp_closing) {
13748 			mutex_exit(&ill->ill_lock);
13749 			err = EINVAL;
13750 			goto failed;
13751 		} else {
13752 			if (ill->ill_ipif_up_count == 0 &&
13753 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13754 				ill->ill_arp_bringup_pending = 1;
13755 			mutex_exit(&ill->ill_lock);
13756 		}
13757 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13758 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13759 			area_p = (uchar_t *)&ip_area_template;
13760 			ared_p = (uchar_t *)&ip_ared_template;
13761 		}
13762 	}
13763 
13764 	/*
13765 	 * Add an entry for the local address in ARP only if it
13766 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13767 	 */
13768 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13769 		area_t *area;
13770 
13771 		/* Now ask ARP to publish our address. */
13772 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13773 		if (arp_add_mp == NULL)
13774 			goto failed;
13775 		area = (area_t *)arp_add_mp->b_rptr;
13776 		if (res_act != Res_act_initial) {
13777 			/*
13778 			 * Copy the new hardware address and length into
13779 			 * arp_add_mp to be sent to ARP.
13780 			 */
13781 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13782 			bcopy(ill->ill_phys_addr,
13783 			    ((char *)area + area->area_hw_addr_offset),
13784 			    area->area_hw_addr_length);
13785 		}
13786 
13787 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13788 		    ACE_F_MYADDR;
13789 
13790 		if (res_act == Res_act_defend) {
13791 			area->area_flags |= ACE_F_DEFEND;
13792 			/*
13793 			 * If we're just defending our address now, then
13794 			 * there's no need to set up ARP multicast mappings.
13795 			 * The publish command is enough.
13796 			 */
13797 			goto done;
13798 		}
13799 
13800 		if (res_act != Res_act_initial)
13801 			goto arp_setup_multicast;
13802 
13803 		/*
13804 		 * Allocate an ARP deletion message so we know we can tell ARP
13805 		 * when the interface goes down.
13806 		 */
13807 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13808 		if (arp_del_mp == NULL)
13809 			goto failed;
13810 
13811 	} else {
13812 		if (res_act != Res_act_initial)
13813 			goto done;
13814 	}
13815 	/*
13816 	 * Need to bring up ARP or setup multicast mapping only
13817 	 * when the first interface is coming UP.
13818 	 */
13819 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13820 	    was_dup) {
13821 		goto done;
13822 	}
13823 
13824 	/*
13825 	 * Allocate an ARP down message (to be saved) and an ARP up
13826 	 * message.
13827 	 */
13828 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13829 	if (arp_down_mp == NULL)
13830 		goto failed;
13831 
13832 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13833 	if (arp_up_mp == NULL)
13834 		goto failed;
13835 
13836 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13837 		goto done;
13838 
13839 arp_setup_multicast:
13840 	/*
13841 	 * Setup the multicast mappings. This function initializes
13842 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13843 	 * IPv6.
13844 	 */
13845 	if (!ill->ill_isv6) {
13846 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13847 		if (err != 0)
13848 			goto failed;
13849 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13850 		ASSERT(arp_add_mapping_mp != NULL);
13851 	}
13852 
13853 done:
13854 	if (arp_del_mp != NULL) {
13855 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13856 		ipif->ipif_arp_del_mp = arp_del_mp;
13857 	}
13858 	if (arp_down_mp != NULL) {
13859 		ASSERT(ill->ill_arp_down_mp == NULL);
13860 		ill->ill_arp_down_mp = arp_down_mp;
13861 	}
13862 	if (arp_del_mapping_mp != NULL) {
13863 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13864 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13865 	}
13866 	if (arp_up_mp != NULL) {
13867 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13868 		    ill->ill_name, ipif->ipif_id));
13869 		putnext(ill->ill_rq, arp_up_mp);
13870 	}
13871 	if (arp_add_mp != NULL) {
13872 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13873 		    ill->ill_name, ipif->ipif_id));
13874 		/*
13875 		 * If it's an extended ARP implementation, then we'll wait to
13876 		 * hear that DAD has finished before using the interface.
13877 		 */
13878 		if (!ill->ill_arp_extend)
13879 			ipif->ipif_addr_ready = 1;
13880 		putnext(ill->ill_rq, arp_add_mp);
13881 	} else {
13882 		ipif->ipif_addr_ready = 1;
13883 	}
13884 	if (arp_add_mapping_mp != NULL) {
13885 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13886 		    ill->ill_name, ipif->ipif_id));
13887 		putnext(ill->ill_rq, arp_add_mapping_mp);
13888 	}
13889 	if (res_act != Res_act_initial)
13890 		return (0);
13891 
13892 	if (ill->ill_flags & ILLF_NOARP)
13893 		err = ill_arp_off(ill);
13894 	else
13895 		err = ill_arp_on(ill);
13896 	if (err != 0) {
13897 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13898 		freemsg(ipif->ipif_arp_del_mp);
13899 		freemsg(ill->ill_arp_down_mp);
13900 		freemsg(ill->ill_arp_del_mapping_mp);
13901 		ipif->ipif_arp_del_mp = NULL;
13902 		ill->ill_arp_down_mp = NULL;
13903 		ill->ill_arp_del_mapping_mp = NULL;
13904 		return (err);
13905 	}
13906 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13907 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13908 
13909 failed:
13910 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13911 	freemsg(arp_add_mp);
13912 	freemsg(arp_del_mp);
13913 	freemsg(arp_add_mapping_mp);
13914 	freemsg(arp_up_mp);
13915 	freemsg(arp_down_mp);
13916 	ill->ill_arp_bringup_pending = 0;
13917 	return (err);
13918 }
13919 
13920 /*
13921  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13922  * just gone back up.
13923  */
13924 static void
13925 ipif_arp_start_dad(ipif_t *ipif)
13926 {
13927 	ill_t *ill = ipif->ipif_ill;
13928 	mblk_t *arp_add_mp;
13929 	area_t *area;
13930 
13931 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13932 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13933 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13934 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13935 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13936 		/*
13937 		 * If we can't contact ARP for some reason, that's not really a
13938 		 * problem.  Just send out the routing socket notification that
13939 		 * DAD completion would have done, and continue.
13940 		 */
13941 		ipif_mask_reply(ipif);
13942 		ip_rts_ifmsg(ipif);
13943 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13944 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13945 		ipif->ipif_addr_ready = 1;
13946 		return;
13947 	}
13948 
13949 	/* Setting the 'unverified' flag restarts DAD */
13950 	area = (area_t *)arp_add_mp->b_rptr;
13951 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13952 	    ACE_F_UNVERIFIED;
13953 	putnext(ill->ill_rq, arp_add_mp);
13954 }
13955 
13956 static void
13957 ipif_ndp_start_dad(ipif_t *ipif)
13958 {
13959 	nce_t *nce;
13960 
13961 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13962 	if (nce == NULL)
13963 		return;
13964 
13965 	if (!ndp_restart_dad(nce)) {
13966 		/*
13967 		 * If we can't restart DAD for some reason, that's not really a
13968 		 * problem.  Just send out the routing socket notification that
13969 		 * DAD completion would have done, and continue.
13970 		 */
13971 		ip_rts_ifmsg(ipif);
13972 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13973 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13974 		ipif->ipif_addr_ready = 1;
13975 	}
13976 	NCE_REFRELE(nce);
13977 }
13978 
13979 /*
13980  * Restart duplicate address detection on all interfaces on the given ill.
13981  *
13982  * This is called when an interface transitions from down to up
13983  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13984  *
13985  * Note that since the underlying physical link has transitioned, we must cause
13986  * at least one routing socket message to be sent here, either via DAD
13987  * completion or just by default on the first ipif.  (If we don't do this, then
13988  * in.mpathd will see long delays when doing link-based failure recovery.)
13989  */
13990 void
13991 ill_restart_dad(ill_t *ill, boolean_t went_up)
13992 {
13993 	ipif_t *ipif;
13994 
13995 	if (ill == NULL)
13996 		return;
13997 
13998 	/*
13999 	 * If layer two doesn't support duplicate address detection, then just
14000 	 * send the routing socket message now and be done with it.
14001 	 */
14002 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14003 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14004 		ip_rts_ifmsg(ill->ill_ipif);
14005 		return;
14006 	}
14007 
14008 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14009 		if (went_up) {
14010 			if (ipif->ipif_flags & IPIF_UP) {
14011 				if (ill->ill_isv6)
14012 					ipif_ndp_start_dad(ipif);
14013 				else
14014 					ipif_arp_start_dad(ipif);
14015 			} else if (ill->ill_isv6 &&
14016 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14017 				/*
14018 				 * For IPv4, the ARP module itself will
14019 				 * automatically start the DAD process when it
14020 				 * sees DL_NOTE_LINK_UP.  We respond to the
14021 				 * AR_CN_READY at the completion of that task.
14022 				 * For IPv6, we must kick off the bring-up
14023 				 * process now.
14024 				 */
14025 				ndp_do_recovery(ipif);
14026 			} else {
14027 				/*
14028 				 * Unfortunately, the first ipif is "special"
14029 				 * and represents the underlying ill in the
14030 				 * routing socket messages.  Thus, when this
14031 				 * one ipif is down, we must still notify so
14032 				 * that the user knows the IFF_RUNNING status
14033 				 * change.  (If the first ipif is up, then
14034 				 * we'll handle eventual routing socket
14035 				 * notification via DAD completion.)
14036 				 */
14037 				if (ipif == ill->ill_ipif)
14038 					ip_rts_ifmsg(ill->ill_ipif);
14039 			}
14040 		} else {
14041 			/*
14042 			 * After link down, we'll need to send a new routing
14043 			 * message when the link comes back, so clear
14044 			 * ipif_addr_ready.
14045 			 */
14046 			ipif->ipif_addr_ready = 0;
14047 		}
14048 	}
14049 
14050 	/*
14051 	 * If we've torn down links, then notify the user right away.
14052 	 */
14053 	if (!went_up)
14054 		ip_rts_ifmsg(ill->ill_ipif);
14055 }
14056 
14057 /*
14058  * Wakeup all threads waiting to enter the ipsq, and sleeping
14059  * on any of the ills in this ipsq. The ill_lock of the ill
14060  * must be held so that waiters don't miss wakeups
14061  */
14062 static void
14063 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14064 {
14065 	phyint_t *phyint;
14066 
14067 	phyint = ipsq->ipsq_phyint_list;
14068 	while (phyint != NULL) {
14069 		if (phyint->phyint_illv4) {
14070 			if (!caller_holds_lock)
14071 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14072 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14073 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14074 			if (!caller_holds_lock)
14075 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14076 		}
14077 		if (phyint->phyint_illv6) {
14078 			if (!caller_holds_lock)
14079 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14080 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14081 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14082 			if (!caller_holds_lock)
14083 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14084 		}
14085 		phyint = phyint->phyint_ipsq_next;
14086 	}
14087 }
14088 
14089 static ipsq_t *
14090 ipsq_create(char *groupname, ip_stack_t *ipst)
14091 {
14092 	ipsq_t	*ipsq;
14093 
14094 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14095 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14096 	if (ipsq == NULL) {
14097 		return (NULL);
14098 	}
14099 
14100 	if (groupname != NULL)
14101 		(void) strcpy(ipsq->ipsq_name, groupname);
14102 	else
14103 		ipsq->ipsq_name[0] = '\0';
14104 
14105 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14106 	ipsq->ipsq_flags |= IPSQ_GROUP;
14107 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14108 	ipst->ips_ipsq_g_head = ipsq;
14109 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14110 	return (ipsq);
14111 }
14112 
14113 /*
14114  * Return an ipsq correspoding to the groupname. If 'create' is true
14115  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14116  * uniquely with an IPMP group. However during IPMP groupname operations,
14117  * multiple IPMP groups may be associated with a single ipsq. But no
14118  * IPMP group can be associated with more than 1 ipsq at any time.
14119  * For example
14120  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14121  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14122  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14123  *
14124  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14125  * status shown below during the execution of the above command.
14126  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14127  *
14128  * After the completion of the above groupname command we return to the stable
14129  * state shown below.
14130  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14131  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14132  *
14133  * Because of the above, we don't search based on the ipsq_name since that
14134  * would miss the correct ipsq during certain windows as shown above.
14135  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14136  * natural state.
14137  */
14138 static ipsq_t *
14139 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14140     ip_stack_t *ipst)
14141 {
14142 	ipsq_t	*ipsq;
14143 	int	group_len;
14144 	phyint_t *phyint;
14145 
14146 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14147 
14148 	group_len = strlen(groupname);
14149 	ASSERT(group_len != 0);
14150 	group_len++;
14151 
14152 	for (ipsq = ipst->ips_ipsq_g_head;
14153 	    ipsq != NULL;
14154 	    ipsq = ipsq->ipsq_next) {
14155 		/*
14156 		 * When an ipsq is being split, and ill_split_ipsq
14157 		 * calls this function, we exclude it from being considered.
14158 		 */
14159 		if (ipsq == exclude_ipsq)
14160 			continue;
14161 
14162 		/*
14163 		 * Compare against the ipsq_name. The groupname change happens
14164 		 * in 2 phases. The 1st phase merges the from group into
14165 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14166 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14167 		 * ipsq_name. At this point the phyint_groupname has not been
14168 		 * updated.
14169 		 */
14170 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14171 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14172 			/*
14173 			 * Verify that an ipmp groupname is exactly
14174 			 * part of 1 ipsq and is not found in any other
14175 			 * ipsq.
14176 			 */
14177 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14178 			    NULL);
14179 			return (ipsq);
14180 		}
14181 
14182 		/*
14183 		 * Comparison against ipsq_name alone is not sufficient.
14184 		 * In the case when groups are currently being
14185 		 * merged, the ipsq could hold other IPMP groups temporarily.
14186 		 * so we walk the phyint list and compare against the
14187 		 * phyint_groupname as well.
14188 		 */
14189 		phyint = ipsq->ipsq_phyint_list;
14190 		while (phyint != NULL) {
14191 			if ((group_len == phyint->phyint_groupname_len) &&
14192 			    (bcmp(phyint->phyint_groupname, groupname,
14193 			    group_len) == 0)) {
14194 				/*
14195 				 * Verify that an ipmp groupname is exactly
14196 				 * part of 1 ipsq and is not found in any other
14197 				 * ipsq.
14198 				 */
14199 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14200 				    ipst) == NULL);
14201 				return (ipsq);
14202 			}
14203 			phyint = phyint->phyint_ipsq_next;
14204 		}
14205 	}
14206 	if (create)
14207 		ipsq = ipsq_create(groupname, ipst);
14208 	return (ipsq);
14209 }
14210 
14211 static void
14212 ipsq_delete(ipsq_t *ipsq)
14213 {
14214 	ipsq_t *nipsq;
14215 	ipsq_t *pipsq = NULL;
14216 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14217 
14218 	/*
14219 	 * We don't hold the ipsq lock, but we are sure no new
14220 	 * messages can land up, since the ipsq_refs is zero.
14221 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14222 	 * is associated with this ipsq. (Lookups are based on ill_name
14223 	 * or phyint_groupname)
14224 	 */
14225 	ASSERT(ipsq->ipsq_refs == 0);
14226 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14227 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14228 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14229 		/*
14230 		 * This is not the ipsq of an IPMP group.
14231 		 */
14232 		ipsq->ipsq_ipst = NULL;
14233 		kmem_free(ipsq, sizeof (ipsq_t));
14234 		return;
14235 	}
14236 
14237 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14238 
14239 	/*
14240 	 * Locate the ipsq  before we can remove it from
14241 	 * the singly linked list of ipsq's.
14242 	 */
14243 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14244 	    nipsq = nipsq->ipsq_next) {
14245 		if (nipsq == ipsq) {
14246 			break;
14247 		}
14248 		pipsq = nipsq;
14249 	}
14250 
14251 	ASSERT(nipsq == ipsq);
14252 
14253 	/* unlink ipsq from the list */
14254 	if (pipsq != NULL)
14255 		pipsq->ipsq_next = ipsq->ipsq_next;
14256 	else
14257 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14258 	ipsq->ipsq_ipst = NULL;
14259 	kmem_free(ipsq, sizeof (ipsq_t));
14260 	rw_exit(&ipst->ips_ill_g_lock);
14261 }
14262 
14263 static void
14264 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14265     queue_t *q)
14266 {
14267 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14268 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14269 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14270 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14271 	ASSERT(current_mp != NULL);
14272 
14273 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14274 	    NEW_OP, NULL);
14275 
14276 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14277 	    new_ipsq->ipsq_xopq_mphead != NULL);
14278 
14279 	/*
14280 	 * move from old ipsq to the new ipsq.
14281 	 */
14282 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14283 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14284 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14285 
14286 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14287 }
14288 
14289 void
14290 ill_group_cleanup(ill_t *ill)
14291 {
14292 	ill_t *ill_v4;
14293 	ill_t *ill_v6;
14294 	ipif_t *ipif;
14295 
14296 	ill_v4 = ill->ill_phyint->phyint_illv4;
14297 	ill_v6 = ill->ill_phyint->phyint_illv6;
14298 
14299 	if (ill_v4 != NULL) {
14300 		mutex_enter(&ill_v4->ill_lock);
14301 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14302 		    ipif = ipif->ipif_next) {
14303 			IPIF_UNMARK_MOVING(ipif);
14304 		}
14305 		ill_v4->ill_up_ipifs = B_FALSE;
14306 		mutex_exit(&ill_v4->ill_lock);
14307 	}
14308 
14309 	if (ill_v6 != NULL) {
14310 		mutex_enter(&ill_v6->ill_lock);
14311 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14312 		    ipif = ipif->ipif_next) {
14313 			IPIF_UNMARK_MOVING(ipif);
14314 		}
14315 		ill_v6->ill_up_ipifs = B_FALSE;
14316 		mutex_exit(&ill_v6->ill_lock);
14317 	}
14318 }
14319 /*
14320  * This function is called when an ill has had a change in its group status
14321  * to bring up all the ipifs that were up before the change.
14322  */
14323 int
14324 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14325 {
14326 	ipif_t *ipif;
14327 	ill_t *ill_v4;
14328 	ill_t *ill_v6;
14329 	ill_t *from_ill;
14330 	int err = 0;
14331 
14332 
14333 	ASSERT(IAM_WRITER_ILL(ill));
14334 
14335 	/*
14336 	 * Except for ipif_state_flags and ill_state_flags the other
14337 	 * fields of the ipif/ill that are modified below are protected
14338 	 * implicitly since we are a writer. We would have tried to down
14339 	 * even an ipif that was already down, in ill_down_ipifs. So we
14340 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14341 	 */
14342 	ill_v4 = ill->ill_phyint->phyint_illv4;
14343 	ill_v6 = ill->ill_phyint->phyint_illv6;
14344 	if (ill_v4 != NULL) {
14345 		ill_v4->ill_up_ipifs = B_TRUE;
14346 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14347 		    ipif = ipif->ipif_next) {
14348 			mutex_enter(&ill_v4->ill_lock);
14349 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14350 			IPIF_UNMARK_MOVING(ipif);
14351 			mutex_exit(&ill_v4->ill_lock);
14352 			if (ipif->ipif_was_up) {
14353 				if (!(ipif->ipif_flags & IPIF_UP))
14354 					err = ipif_up(ipif, q, mp);
14355 				ipif->ipif_was_up = B_FALSE;
14356 				if (err != 0) {
14357 					/*
14358 					 * Can there be any other error ?
14359 					 */
14360 					ASSERT(err == EINPROGRESS);
14361 					return (err);
14362 				}
14363 			}
14364 		}
14365 		mutex_enter(&ill_v4->ill_lock);
14366 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14367 		mutex_exit(&ill_v4->ill_lock);
14368 		ill_v4->ill_up_ipifs = B_FALSE;
14369 		if (ill_v4->ill_move_in_progress) {
14370 			ASSERT(ill_v4->ill_move_peer != NULL);
14371 			ill_v4->ill_move_in_progress = B_FALSE;
14372 			from_ill = ill_v4->ill_move_peer;
14373 			from_ill->ill_move_in_progress = B_FALSE;
14374 			from_ill->ill_move_peer = NULL;
14375 			mutex_enter(&from_ill->ill_lock);
14376 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14377 			mutex_exit(&from_ill->ill_lock);
14378 			if (ill_v6 == NULL) {
14379 				if (from_ill->ill_phyint->phyint_flags &
14380 				    PHYI_STANDBY) {
14381 					phyint_inactive(from_ill->ill_phyint);
14382 				}
14383 				if (ill_v4->ill_phyint->phyint_flags &
14384 				    PHYI_STANDBY) {
14385 					phyint_inactive(ill_v4->ill_phyint);
14386 				}
14387 			}
14388 			ill_v4->ill_move_peer = NULL;
14389 		}
14390 	}
14391 
14392 	if (ill_v6 != NULL) {
14393 		ill_v6->ill_up_ipifs = B_TRUE;
14394 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14395 		    ipif = ipif->ipif_next) {
14396 			mutex_enter(&ill_v6->ill_lock);
14397 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14398 			IPIF_UNMARK_MOVING(ipif);
14399 			mutex_exit(&ill_v6->ill_lock);
14400 			if (ipif->ipif_was_up) {
14401 				if (!(ipif->ipif_flags & IPIF_UP))
14402 					err = ipif_up(ipif, q, mp);
14403 				ipif->ipif_was_up = B_FALSE;
14404 				if (err != 0) {
14405 					/*
14406 					 * Can there be any other error ?
14407 					 */
14408 					ASSERT(err == EINPROGRESS);
14409 					return (err);
14410 				}
14411 			}
14412 		}
14413 		mutex_enter(&ill_v6->ill_lock);
14414 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14415 		mutex_exit(&ill_v6->ill_lock);
14416 		ill_v6->ill_up_ipifs = B_FALSE;
14417 		if (ill_v6->ill_move_in_progress) {
14418 			ASSERT(ill_v6->ill_move_peer != NULL);
14419 			ill_v6->ill_move_in_progress = B_FALSE;
14420 			from_ill = ill_v6->ill_move_peer;
14421 			from_ill->ill_move_in_progress = B_FALSE;
14422 			from_ill->ill_move_peer = NULL;
14423 			mutex_enter(&from_ill->ill_lock);
14424 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14425 			mutex_exit(&from_ill->ill_lock);
14426 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14427 				phyint_inactive(from_ill->ill_phyint);
14428 			}
14429 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14430 				phyint_inactive(ill_v6->ill_phyint);
14431 			}
14432 			ill_v6->ill_move_peer = NULL;
14433 		}
14434 	}
14435 	return (0);
14436 }
14437 
14438 /*
14439  * bring down all the approriate ipifs.
14440  */
14441 /* ARGSUSED */
14442 static void
14443 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14444 {
14445 	ipif_t *ipif;
14446 
14447 	ASSERT(IAM_WRITER_ILL(ill));
14448 
14449 	/*
14450 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14451 	 * are modified below are protected implicitly since we are a writer
14452 	 */
14453 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14454 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14455 			continue;
14456 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14457 			/*
14458 			 * We go through the ipif_down logic even if the ipif
14459 			 * is already down, since routes can be added based
14460 			 * on down ipifs. Going through ipif_down once again
14461 			 * will delete any IREs created based on these routes.
14462 			 */
14463 			if (ipif->ipif_flags & IPIF_UP)
14464 				ipif->ipif_was_up = B_TRUE;
14465 			/*
14466 			 * If called with chk_nofailover true ipif is moving.
14467 			 */
14468 			mutex_enter(&ill->ill_lock);
14469 			if (chk_nofailover) {
14470 				ipif->ipif_state_flags |=
14471 				    IPIF_MOVING | IPIF_CHANGING;
14472 			} else {
14473 				ipif->ipif_state_flags |= IPIF_CHANGING;
14474 			}
14475 			mutex_exit(&ill->ill_lock);
14476 			/*
14477 			 * Need to re-create net/subnet bcast ires if
14478 			 * they are dependent on ipif.
14479 			 */
14480 			if (!ipif->ipif_isv6)
14481 				ipif_check_bcast_ires(ipif);
14482 			(void) ipif_logical_down(ipif, NULL, NULL);
14483 			ipif_non_duplicate(ipif);
14484 			ipif_down_tail(ipif);
14485 		}
14486 	}
14487 }
14488 
14489 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14490 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14491 	(ipsq)->ipsq_refs++;				\
14492 }
14493 
14494 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14495 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14496 	(ipsq)->ipsq_refs--;				\
14497 	if ((ipsq)->ipsq_refs == 0)				\
14498 		(ipsq)->ipsq_name[0] = '\0'; 		\
14499 }
14500 
14501 /*
14502  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14503  * new_ipsq.
14504  */
14505 static void
14506 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14507 {
14508 	phyint_t *phyint;
14509 	phyint_t *next_phyint;
14510 
14511 	/*
14512 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14513 	 * writer and the ill_lock of the ill in question. Also the dest
14514 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14515 	 */
14516 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14517 
14518 	phyint = cur_ipsq->ipsq_phyint_list;
14519 	cur_ipsq->ipsq_phyint_list = NULL;
14520 	while (phyint != NULL) {
14521 		next_phyint = phyint->phyint_ipsq_next;
14522 		IPSQ_DEC_REF(cur_ipsq, ipst);
14523 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14524 		new_ipsq->ipsq_phyint_list = phyint;
14525 		IPSQ_INC_REF(new_ipsq, ipst);
14526 		phyint->phyint_ipsq = new_ipsq;
14527 		phyint = next_phyint;
14528 	}
14529 }
14530 
14531 #define	SPLIT_SUCCESS		0
14532 #define	SPLIT_NOT_NEEDED	1
14533 #define	SPLIT_FAILED		2
14534 
14535 int
14536 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14537     ip_stack_t *ipst)
14538 {
14539 	ipsq_t *newipsq = NULL;
14540 
14541 	/*
14542 	 * Assertions denote pre-requisites for changing the ipsq of
14543 	 * a phyint
14544 	 */
14545 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14546 	/*
14547 	 * <ill-phyint> assocs can't change while ill_g_lock
14548 	 * is held as writer. See ill_phyint_reinit()
14549 	 */
14550 	ASSERT(phyint->phyint_illv4 == NULL ||
14551 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14552 	ASSERT(phyint->phyint_illv6 == NULL ||
14553 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14554 
14555 	if ((phyint->phyint_groupname_len !=
14556 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14557 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14558 	    phyint->phyint_groupname_len) != 0)) {
14559 		/*
14560 		 * Once we fail in creating a new ipsq due to memory shortage,
14561 		 * don't attempt to create new ipsq again, based on another
14562 		 * phyint, since we want all phyints belonging to an IPMP group
14563 		 * to be in the same ipsq even in the event of mem alloc fails.
14564 		 */
14565 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14566 		    cur_ipsq, ipst);
14567 		if (newipsq == NULL) {
14568 			/* Memory allocation failure */
14569 			return (SPLIT_FAILED);
14570 		} else {
14571 			/* ipsq_refs protected by ill_g_lock (writer) */
14572 			IPSQ_DEC_REF(cur_ipsq, ipst);
14573 			phyint->phyint_ipsq = newipsq;
14574 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14575 			newipsq->ipsq_phyint_list = phyint;
14576 			IPSQ_INC_REF(newipsq, ipst);
14577 			return (SPLIT_SUCCESS);
14578 		}
14579 	}
14580 	return (SPLIT_NOT_NEEDED);
14581 }
14582 
14583 /*
14584  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14585  * to do this split
14586  */
14587 static int
14588 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14589 {
14590 	ipsq_t *newipsq;
14591 
14592 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14593 	/*
14594 	 * <ill-phyint> assocs can't change while ill_g_lock
14595 	 * is held as writer. See ill_phyint_reinit()
14596 	 */
14597 
14598 	ASSERT(phyint->phyint_illv4 == NULL ||
14599 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14600 	ASSERT(phyint->phyint_illv6 == NULL ||
14601 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14602 
14603 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14604 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14605 		/*
14606 		 * ipsq_init failed due to no memory
14607 		 * caller will use the same ipsq
14608 		 */
14609 		return (SPLIT_FAILED);
14610 	}
14611 
14612 	/* ipsq_ref is protected by ill_g_lock (writer) */
14613 	IPSQ_DEC_REF(cur_ipsq, ipst);
14614 
14615 	/*
14616 	 * This is a new ipsq that is unknown to the world.
14617 	 * So we don't need to hold ipsq_lock,
14618 	 */
14619 	newipsq = phyint->phyint_ipsq;
14620 	newipsq->ipsq_writer = NULL;
14621 	newipsq->ipsq_reentry_cnt--;
14622 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14623 #ifdef DEBUG
14624 	newipsq->ipsq_depth = 0;
14625 #endif
14626 
14627 	return (SPLIT_SUCCESS);
14628 }
14629 
14630 /*
14631  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14632  * ipsq's representing their individual groups or themselves. Return
14633  * whether split needs to be retried again later.
14634  */
14635 static boolean_t
14636 ill_split_ipsq(ipsq_t *cur_ipsq)
14637 {
14638 	phyint_t *phyint;
14639 	phyint_t *next_phyint;
14640 	int	error;
14641 	boolean_t need_retry = B_FALSE;
14642 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14643 
14644 	phyint = cur_ipsq->ipsq_phyint_list;
14645 	cur_ipsq->ipsq_phyint_list = NULL;
14646 	while (phyint != NULL) {
14647 		next_phyint = phyint->phyint_ipsq_next;
14648 		/*
14649 		 * 'created' will tell us whether the callee actually
14650 		 * created an ipsq. Lack of memory may force the callee
14651 		 * to return without creating an ipsq.
14652 		 */
14653 		if (phyint->phyint_groupname == NULL) {
14654 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14655 		} else {
14656 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14657 			    need_retry, ipst);
14658 		}
14659 
14660 		switch (error) {
14661 		case SPLIT_FAILED:
14662 			need_retry = B_TRUE;
14663 			/* FALLTHRU */
14664 		case SPLIT_NOT_NEEDED:
14665 			/*
14666 			 * Keep it on the list.
14667 			 */
14668 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14669 			cur_ipsq->ipsq_phyint_list = phyint;
14670 			break;
14671 		case SPLIT_SUCCESS:
14672 			break;
14673 		default:
14674 			ASSERT(0);
14675 		}
14676 
14677 		phyint = next_phyint;
14678 	}
14679 	return (need_retry);
14680 }
14681 
14682 /*
14683  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14684  * and return the ills in the list. This list will be
14685  * needed to unlock all the ills later on by the caller.
14686  * The <ill-ipsq> associations could change between the
14687  * lock and unlock. Hence the unlock can't traverse the
14688  * ipsq to get the list of ills.
14689  */
14690 static int
14691 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14692 {
14693 	int	cnt = 0;
14694 	phyint_t	*phyint;
14695 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14696 
14697 	/*
14698 	 * The caller holds ill_g_lock to ensure that the ill memberships
14699 	 * of the ipsq don't change
14700 	 */
14701 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14702 
14703 	phyint = ipsq->ipsq_phyint_list;
14704 	while (phyint != NULL) {
14705 		if (phyint->phyint_illv4 != NULL) {
14706 			ASSERT(cnt < list_max);
14707 			list[cnt++] = phyint->phyint_illv4;
14708 		}
14709 		if (phyint->phyint_illv6 != NULL) {
14710 			ASSERT(cnt < list_max);
14711 			list[cnt++] = phyint->phyint_illv6;
14712 		}
14713 		phyint = phyint->phyint_ipsq_next;
14714 	}
14715 	ill_lock_ills(list, cnt);
14716 	return (cnt);
14717 }
14718 
14719 void
14720 ill_lock_ills(ill_t **list, int cnt)
14721 {
14722 	int	i;
14723 
14724 	if (cnt > 1) {
14725 		boolean_t try_again;
14726 		do {
14727 			try_again = B_FALSE;
14728 			for (i = 0; i < cnt - 1; i++) {
14729 				if (list[i] < list[i + 1]) {
14730 					ill_t	*tmp;
14731 
14732 					/* swap the elements */
14733 					tmp = list[i];
14734 					list[i] = list[i + 1];
14735 					list[i + 1] = tmp;
14736 					try_again = B_TRUE;
14737 				}
14738 			}
14739 		} while (try_again);
14740 	}
14741 
14742 	for (i = 0; i < cnt; i++) {
14743 		if (i == 0) {
14744 			if (list[i] != NULL)
14745 				mutex_enter(&list[i]->ill_lock);
14746 			else
14747 				return;
14748 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14749 			mutex_enter(&list[i]->ill_lock);
14750 		}
14751 	}
14752 }
14753 
14754 void
14755 ill_unlock_ills(ill_t **list, int cnt)
14756 {
14757 	int	i;
14758 
14759 	for (i = 0; i < cnt; i++) {
14760 		if ((i == 0) && (list[i] != NULL)) {
14761 			mutex_exit(&list[i]->ill_lock);
14762 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14763 			mutex_exit(&list[i]->ill_lock);
14764 		}
14765 	}
14766 }
14767 
14768 /*
14769  * Merge all the ills from 1 ipsq group into another ipsq group.
14770  * The source ipsq group is specified by the ipsq associated with
14771  * 'from_ill'. The destination ipsq group is specified by the ipsq
14772  * associated with 'to_ill' or 'groupname' respectively.
14773  * Note that ipsq itself does not have a reference count mechanism
14774  * and functions don't look up an ipsq and pass it around. Instead
14775  * functions pass around an ill or groupname, and the ipsq is looked
14776  * up from the ill or groupname and the required operation performed
14777  * atomically with the lookup on the ipsq.
14778  */
14779 static int
14780 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14781     queue_t *q)
14782 {
14783 	ipsq_t *old_ipsq;
14784 	ipsq_t *new_ipsq;
14785 	ill_t	**ill_list;
14786 	int	cnt;
14787 	size_t	ill_list_size;
14788 	boolean_t became_writer_on_new_sq = B_FALSE;
14789 	ip_stack_t	*ipst = from_ill->ill_ipst;
14790 
14791 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14792 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14793 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14794 
14795 	/*
14796 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14797 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14798 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14799 	 */
14800 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14801 
14802 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14803 	if (groupname != NULL)
14804 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14805 	else {
14806 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14807 	}
14808 
14809 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14810 
14811 	/*
14812 	 * both groups are on the same ipsq.
14813 	 */
14814 	if (old_ipsq == new_ipsq) {
14815 		rw_exit(&ipst->ips_ill_g_lock);
14816 		return (0);
14817 	}
14818 
14819 	cnt = old_ipsq->ipsq_refs << 1;
14820 	ill_list_size = cnt * sizeof (ill_t *);
14821 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14822 	if (ill_list == NULL) {
14823 		rw_exit(&ipst->ips_ill_g_lock);
14824 		return (ENOMEM);
14825 	}
14826 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14827 
14828 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14829 	mutex_enter(&new_ipsq->ipsq_lock);
14830 	if ((new_ipsq->ipsq_writer == NULL &&
14831 	    new_ipsq->ipsq_current_ipif == NULL) ||
14832 	    (new_ipsq->ipsq_writer == curthread)) {
14833 		new_ipsq->ipsq_writer = curthread;
14834 		new_ipsq->ipsq_reentry_cnt++;
14835 		became_writer_on_new_sq = B_TRUE;
14836 	}
14837 
14838 	/*
14839 	 * We are holding ill_g_lock as writer and all the ill locks of
14840 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14841 	 * message can land up on the old ipsq even though we don't hold the
14842 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14843 	 */
14844 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14845 
14846 	/*
14847 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14848 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14849 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14850 	 */
14851 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14852 
14853 	/*
14854 	 * Mark the new ipsq as needing a split since it is currently
14855 	 * being shared by more than 1 IPMP group. The split will
14856 	 * occur at the end of ipsq_exit
14857 	 */
14858 	new_ipsq->ipsq_split = B_TRUE;
14859 
14860 	/* Now release all the locks */
14861 	mutex_exit(&new_ipsq->ipsq_lock);
14862 	ill_unlock_ills(ill_list, cnt);
14863 	rw_exit(&ipst->ips_ill_g_lock);
14864 
14865 	kmem_free(ill_list, ill_list_size);
14866 
14867 	/*
14868 	 * If we succeeded in becoming writer on the new ipsq, then
14869 	 * drain the new ipsq and start processing  all enqueued messages
14870 	 * including the current ioctl we are processing which is either
14871 	 * a set groupname or failover/failback.
14872 	 */
14873 	if (became_writer_on_new_sq)
14874 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14875 
14876 	/*
14877 	 * syncq has been changed and all the messages have been moved.
14878 	 */
14879 	mutex_enter(&old_ipsq->ipsq_lock);
14880 	old_ipsq->ipsq_current_ipif = NULL;
14881 	old_ipsq->ipsq_current_ioctl = 0;
14882 	mutex_exit(&old_ipsq->ipsq_lock);
14883 	return (EINPROGRESS);
14884 }
14885 
14886 /*
14887  * Delete and add the loopback copy and non-loopback copy of
14888  * the BROADCAST ire corresponding to ill and addr. Used to
14889  * group broadcast ires together when ill becomes part of
14890  * a group.
14891  *
14892  * This function is also called when ill is leaving the group
14893  * so that the ires belonging to the group gets re-grouped.
14894  */
14895 static void
14896 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14897 {
14898 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14899 	ire_t **ire_ptpn = &ire_head;
14900 	ip_stack_t	*ipst = ill->ill_ipst;
14901 
14902 	/*
14903 	 * The loopback and non-loopback IREs are inserted in the order in which
14904 	 * they're found, on the basis that they are correctly ordered (loopback
14905 	 * first).
14906 	 */
14907 	for (;;) {
14908 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14909 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14910 		if (ire == NULL)
14911 			break;
14912 
14913 		/*
14914 		 * we are passing in KM_SLEEP because it is not easy to
14915 		 * go back to a sane state in case of memory failure.
14916 		 */
14917 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14918 		ASSERT(nire != NULL);
14919 		bzero(nire, sizeof (ire_t));
14920 		/*
14921 		 * Don't use ire_max_frag directly since we don't
14922 		 * hold on to 'ire' until we add the new ire 'nire' and
14923 		 * we don't want the new ire to have a dangling reference
14924 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14925 		 * be in sync with the ipif_mtu of the associate ipif.
14926 		 * For eg. this happens as a result of SIOCSLIFNAME,
14927 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14928 		 * the driver. A change in ire_max_frag triggered as
14929 		 * as a result of path mtu discovery, or due to an
14930 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14931 		 * route change -mtu command does not apply to broadcast ires.
14932 		 *
14933 		 * XXX We need a recovery strategy here if ire_init fails
14934 		 */
14935 		if (ire_init(nire,
14936 		    (uchar_t *)&ire->ire_addr,
14937 		    (uchar_t *)&ire->ire_mask,
14938 		    (uchar_t *)&ire->ire_src_addr,
14939 		    (uchar_t *)&ire->ire_gateway_addr,
14940 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14941 		    &ire->ire_ipif->ipif_mtu,
14942 		    ire->ire_nce,
14943 		    ire->ire_rfq,
14944 		    ire->ire_stq,
14945 		    ire->ire_type,
14946 		    ire->ire_ipif,
14947 		    ire->ire_cmask,
14948 		    ire->ire_phandle,
14949 		    ire->ire_ihandle,
14950 		    ire->ire_flags,
14951 		    &ire->ire_uinfo,
14952 		    NULL,
14953 		    NULL,
14954 		    ipst) == NULL) {
14955 			cmn_err(CE_PANIC, "ire_init() failed");
14956 		}
14957 		ire_delete(ire);
14958 		ire_refrele(ire);
14959 
14960 		/*
14961 		 * The newly created IREs are inserted at the tail of the list
14962 		 * starting with ire_head. As we've just allocated them no one
14963 		 * knows about them so it's safe.
14964 		 */
14965 		*ire_ptpn = nire;
14966 		ire_ptpn = &nire->ire_next;
14967 	}
14968 
14969 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14970 		int error;
14971 		ire_t *oire;
14972 		/* unlink the IRE from our list before calling ire_add() */
14973 		nire_next = nire->ire_next;
14974 		nire->ire_next = NULL;
14975 
14976 		/* ire_add adds the ire at the right place in the list */
14977 		oire = nire;
14978 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14979 		ASSERT(error == 0);
14980 		ASSERT(oire == nire);
14981 		ire_refrele(nire);	/* Held in ire_add */
14982 	}
14983 }
14984 
14985 /*
14986  * This function is usually called when an ill is inserted in
14987  * a group and all the ipifs are already UP. As all the ipifs
14988  * are already UP, the broadcast ires have already been created
14989  * and been inserted. But, ire_add_v4 would not have grouped properly.
14990  * We need to re-group for the benefit of ip_wput_ire which
14991  * expects BROADCAST ires to be grouped properly to avoid sending
14992  * more than one copy of the broadcast packet per group.
14993  *
14994  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14995  *	  because when ipif_up_done ends up calling this, ires have
14996  *        already been added before illgrp_insert i.e before ill_group
14997  *	  has been initialized.
14998  */
14999 static void
15000 ill_group_bcast_for_xmit(ill_t *ill)
15001 {
15002 	ill_group_t *illgrp;
15003 	ipif_t *ipif;
15004 	ipaddr_t addr;
15005 	ipaddr_t net_mask;
15006 	ipaddr_t subnet_netmask;
15007 
15008 	illgrp = ill->ill_group;
15009 
15010 	/*
15011 	 * This function is called even when an ill is deleted from
15012 	 * the group. Hence, illgrp could be null.
15013 	 */
15014 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15015 		return;
15016 
15017 	/*
15018 	 * Delete all the BROADCAST ires matching this ill and add
15019 	 * them back. This time, ire_add_v4 should take care of
15020 	 * grouping them with others because ill is part of the
15021 	 * group.
15022 	 */
15023 	ill_bcast_delete_and_add(ill, 0);
15024 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15025 
15026 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15027 
15028 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15029 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15030 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15031 		} else {
15032 			net_mask = htonl(IN_CLASSA_NET);
15033 		}
15034 		addr = net_mask & ipif->ipif_subnet;
15035 		ill_bcast_delete_and_add(ill, addr);
15036 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15037 
15038 		subnet_netmask = ipif->ipif_net_mask;
15039 		addr = ipif->ipif_subnet;
15040 		ill_bcast_delete_and_add(ill, addr);
15041 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15042 	}
15043 }
15044 
15045 /*
15046  * This function is called from illgrp_delete when ill is being deleted
15047  * from the group.
15048  *
15049  * As ill is not there in the group anymore, any address belonging
15050  * to this ill should be cleared of IRE_MARK_NORECV.
15051  */
15052 static void
15053 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15054 {
15055 	ire_t *ire;
15056 	irb_t *irb;
15057 	ip_stack_t	*ipst = ill->ill_ipst;
15058 
15059 	ASSERT(ill->ill_group == NULL);
15060 
15061 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15062 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15063 
15064 	if (ire != NULL) {
15065 		/*
15066 		 * IPMP and plumbing operations are serialized on the ipsq, so
15067 		 * no one will insert or delete a broadcast ire under our feet.
15068 		 */
15069 		irb = ire->ire_bucket;
15070 		rw_enter(&irb->irb_lock, RW_READER);
15071 		ire_refrele(ire);
15072 
15073 		for (; ire != NULL; ire = ire->ire_next) {
15074 			if (ire->ire_addr != addr)
15075 				break;
15076 			if (ire_to_ill(ire) != ill)
15077 				continue;
15078 
15079 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15080 			ire->ire_marks &= ~IRE_MARK_NORECV;
15081 		}
15082 		rw_exit(&irb->irb_lock);
15083 	}
15084 }
15085 
15086 /*
15087  * This function must be called only after the broadcast ires
15088  * have been grouped together. For a given address addr, nominate
15089  * only one of the ires whose interface is not FAILED or OFFLINE.
15090  *
15091  * This is also called when an ipif goes down, so that we can nominate
15092  * a different ire with the same address for receiving.
15093  */
15094 static void
15095 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15096 {
15097 	irb_t *irb;
15098 	ire_t *ire;
15099 	ire_t *ire1;
15100 	ire_t *save_ire;
15101 	ire_t **irep = NULL;
15102 	boolean_t first = B_TRUE;
15103 	ire_t *clear_ire = NULL;
15104 	ire_t *start_ire = NULL;
15105 	ire_t	*new_lb_ire;
15106 	ire_t	*new_nlb_ire;
15107 	boolean_t new_lb_ire_used = B_FALSE;
15108 	boolean_t new_nlb_ire_used = B_FALSE;
15109 	uint64_t match_flags;
15110 	uint64_t phyi_flags;
15111 	boolean_t fallback = B_FALSE;
15112 	uint_t	max_frag;
15113 
15114 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15115 	    NULL, MATCH_IRE_TYPE, ipst);
15116 	/*
15117 	 * We may not be able to find some ires if a previous
15118 	 * ire_create failed. This happens when an ipif goes
15119 	 * down and we are unable to create BROADCAST ires due
15120 	 * to memory failure. Thus, we have to check for NULL
15121 	 * below. This should handle the case for LOOPBACK,
15122 	 * POINTOPOINT and interfaces with some POINTOPOINT
15123 	 * logicals for which there are no BROADCAST ires.
15124 	 */
15125 	if (ire == NULL)
15126 		return;
15127 	/*
15128 	 * Currently IRE_BROADCASTS are deleted when an ipif
15129 	 * goes down which runs exclusively. Thus, setting
15130 	 * IRE_MARK_RCVD should not race with ire_delete marking
15131 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15132 	 * be consistent with other parts of the code that walks
15133 	 * a given bucket.
15134 	 */
15135 	save_ire = ire;
15136 	irb = ire->ire_bucket;
15137 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15138 	if (new_lb_ire == NULL) {
15139 		ire_refrele(ire);
15140 		return;
15141 	}
15142 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15143 	if (new_nlb_ire == NULL) {
15144 		ire_refrele(ire);
15145 		kmem_cache_free(ire_cache, new_lb_ire);
15146 		return;
15147 	}
15148 	IRB_REFHOLD(irb);
15149 	rw_enter(&irb->irb_lock, RW_WRITER);
15150 	/*
15151 	 * Get to the first ire matching the address and the
15152 	 * group. If the address does not match we are done
15153 	 * as we could not find the IRE. If the address matches
15154 	 * we should get to the first one matching the group.
15155 	 */
15156 	while (ire != NULL) {
15157 		if (ire->ire_addr != addr ||
15158 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15159 			break;
15160 		}
15161 		ire = ire->ire_next;
15162 	}
15163 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15164 	start_ire = ire;
15165 redo:
15166 	while (ire != NULL && ire->ire_addr == addr &&
15167 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15168 		/*
15169 		 * The first ire for any address within a group
15170 		 * should always be the one with IRE_MARK_NORECV cleared
15171 		 * so that ip_wput_ire can avoid searching for one.
15172 		 * Note down the insertion point which will be used
15173 		 * later.
15174 		 */
15175 		if (first && (irep == NULL))
15176 			irep = ire->ire_ptpn;
15177 		/*
15178 		 * PHYI_FAILED is set when the interface fails.
15179 		 * This interface might have become good, but the
15180 		 * daemon has not yet detected. We should still
15181 		 * not receive on this. PHYI_OFFLINE should never
15182 		 * be picked as this has been offlined and soon
15183 		 * be removed.
15184 		 */
15185 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15186 		if (phyi_flags & PHYI_OFFLINE) {
15187 			ire->ire_marks |= IRE_MARK_NORECV;
15188 			ire = ire->ire_next;
15189 			continue;
15190 		}
15191 		if (phyi_flags & match_flags) {
15192 			ire->ire_marks |= IRE_MARK_NORECV;
15193 			ire = ire->ire_next;
15194 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15195 			    PHYI_INACTIVE) {
15196 				fallback = B_TRUE;
15197 			}
15198 			continue;
15199 		}
15200 		if (first) {
15201 			/*
15202 			 * We will move this to the front of the list later
15203 			 * on.
15204 			 */
15205 			clear_ire = ire;
15206 			ire->ire_marks &= ~IRE_MARK_NORECV;
15207 		} else {
15208 			ire->ire_marks |= IRE_MARK_NORECV;
15209 		}
15210 		first = B_FALSE;
15211 		ire = ire->ire_next;
15212 	}
15213 	/*
15214 	 * If we never nominated anybody, try nominating at least
15215 	 * an INACTIVE, if we found one. Do it only once though.
15216 	 */
15217 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15218 	    fallback) {
15219 		match_flags = PHYI_FAILED;
15220 		ire = start_ire;
15221 		irep = NULL;
15222 		goto redo;
15223 	}
15224 	ire_refrele(save_ire);
15225 
15226 	/*
15227 	 * irep non-NULL indicates that we entered the while loop
15228 	 * above. If clear_ire is at the insertion point, we don't
15229 	 * have to do anything. clear_ire will be NULL if all the
15230 	 * interfaces are failed.
15231 	 *
15232 	 * We cannot unlink and reinsert the ire at the right place
15233 	 * in the list since there can be other walkers of this bucket.
15234 	 * Instead we delete and recreate the ire
15235 	 */
15236 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15237 		ire_t *clear_ire_stq = NULL;
15238 
15239 		bzero(new_lb_ire, sizeof (ire_t));
15240 		/* XXX We need a recovery strategy here. */
15241 		if (ire_init(new_lb_ire,
15242 		    (uchar_t *)&clear_ire->ire_addr,
15243 		    (uchar_t *)&clear_ire->ire_mask,
15244 		    (uchar_t *)&clear_ire->ire_src_addr,
15245 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15246 		    &clear_ire->ire_max_frag,
15247 		    NULL, /* let ire_nce_init derive the resolver info */
15248 		    clear_ire->ire_rfq,
15249 		    clear_ire->ire_stq,
15250 		    clear_ire->ire_type,
15251 		    clear_ire->ire_ipif,
15252 		    clear_ire->ire_cmask,
15253 		    clear_ire->ire_phandle,
15254 		    clear_ire->ire_ihandle,
15255 		    clear_ire->ire_flags,
15256 		    &clear_ire->ire_uinfo,
15257 		    NULL,
15258 		    NULL,
15259 		    ipst) == NULL)
15260 			cmn_err(CE_PANIC, "ire_init() failed");
15261 		if (clear_ire->ire_stq == NULL) {
15262 			ire_t *ire_next = clear_ire->ire_next;
15263 			if (ire_next != NULL &&
15264 			    ire_next->ire_stq != NULL &&
15265 			    ire_next->ire_addr == clear_ire->ire_addr &&
15266 			    ire_next->ire_ipif->ipif_ill ==
15267 			    clear_ire->ire_ipif->ipif_ill) {
15268 				clear_ire_stq = ire_next;
15269 
15270 				bzero(new_nlb_ire, sizeof (ire_t));
15271 				/* XXX We need a recovery strategy here. */
15272 				if (ire_init(new_nlb_ire,
15273 				    (uchar_t *)&clear_ire_stq->ire_addr,
15274 				    (uchar_t *)&clear_ire_stq->ire_mask,
15275 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15276 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15277 				    &clear_ire_stq->ire_max_frag,
15278 				    NULL,
15279 				    clear_ire_stq->ire_rfq,
15280 				    clear_ire_stq->ire_stq,
15281 				    clear_ire_stq->ire_type,
15282 				    clear_ire_stq->ire_ipif,
15283 				    clear_ire_stq->ire_cmask,
15284 				    clear_ire_stq->ire_phandle,
15285 				    clear_ire_stq->ire_ihandle,
15286 				    clear_ire_stq->ire_flags,
15287 				    &clear_ire_stq->ire_uinfo,
15288 				    NULL,
15289 				    NULL,
15290 				    ipst) == NULL)
15291 					cmn_err(CE_PANIC, "ire_init() failed");
15292 			}
15293 		}
15294 
15295 		/*
15296 		 * Delete the ire. We can't call ire_delete() since
15297 		 * we are holding the bucket lock. We can't release the
15298 		 * bucket lock since we can't allow irep to change. So just
15299 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15300 		 * ire from the list and do the refrele.
15301 		 */
15302 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15303 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15304 
15305 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15306 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15307 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15308 		}
15309 
15310 		/*
15311 		 * Also take care of otherfields like ib/ob pkt count
15312 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15313 		 */
15314 
15315 		/* Set the max_frag before adding the ire */
15316 		max_frag = *new_lb_ire->ire_max_fragp;
15317 		new_lb_ire->ire_max_fragp = NULL;
15318 		new_lb_ire->ire_max_frag = max_frag;
15319 
15320 		/* Add the new ire's. Insert at *irep */
15321 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15322 		ire1 = *irep;
15323 		if (ire1 != NULL)
15324 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15325 		new_lb_ire->ire_next = ire1;
15326 		/* Link the new one in. */
15327 		new_lb_ire->ire_ptpn = irep;
15328 		membar_producer();
15329 		*irep = new_lb_ire;
15330 		new_lb_ire_used = B_TRUE;
15331 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15332 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15333 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15334 
15335 		if (clear_ire_stq != NULL) {
15336 			/* Set the max_frag before adding the ire */
15337 			max_frag = *new_nlb_ire->ire_max_fragp;
15338 			new_nlb_ire->ire_max_fragp = NULL;
15339 			new_nlb_ire->ire_max_frag = max_frag;
15340 
15341 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15342 			irep = &new_lb_ire->ire_next;
15343 			/* Add the new ire. Insert at *irep */
15344 			ire1 = *irep;
15345 			if (ire1 != NULL)
15346 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15347 			new_nlb_ire->ire_next = ire1;
15348 			/* Link the new one in. */
15349 			new_nlb_ire->ire_ptpn = irep;
15350 			membar_producer();
15351 			*irep = new_nlb_ire;
15352 			new_nlb_ire_used = B_TRUE;
15353 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15354 			    ire_stats_inserted);
15355 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15356 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15357 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15358 		}
15359 	}
15360 	rw_exit(&irb->irb_lock);
15361 	if (!new_lb_ire_used)
15362 		kmem_cache_free(ire_cache, new_lb_ire);
15363 	if (!new_nlb_ire_used)
15364 		kmem_cache_free(ire_cache, new_nlb_ire);
15365 	IRB_REFRELE(irb);
15366 }
15367 
15368 /*
15369  * Whenever an ipif goes down we have to renominate a different
15370  * broadcast ire to receive. Whenever an ipif comes up, we need
15371  * to make sure that we have only one nominated to receive.
15372  */
15373 static void
15374 ipif_renominate_bcast(ipif_t *ipif)
15375 {
15376 	ill_t *ill = ipif->ipif_ill;
15377 	ipaddr_t subnet_addr;
15378 	ipaddr_t net_addr;
15379 	ipaddr_t net_mask = 0;
15380 	ipaddr_t subnet_netmask;
15381 	ipaddr_t addr;
15382 	ill_group_t *illgrp;
15383 	ip_stack_t	*ipst = ill->ill_ipst;
15384 
15385 	illgrp = ill->ill_group;
15386 	/*
15387 	 * If this is the last ipif going down, it might take
15388 	 * the ill out of the group. In that case ipif_down ->
15389 	 * illgrp_delete takes care of doing the nomination.
15390 	 * ipif_down does not call for this case.
15391 	 */
15392 	ASSERT(illgrp != NULL);
15393 
15394 	/* There could not have been any ires associated with this */
15395 	if (ipif->ipif_subnet == 0)
15396 		return;
15397 
15398 	ill_mark_bcast(illgrp, 0, ipst);
15399 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15400 
15401 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15402 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15403 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15404 	} else {
15405 		net_mask = htonl(IN_CLASSA_NET);
15406 	}
15407 	addr = net_mask & ipif->ipif_subnet;
15408 	ill_mark_bcast(illgrp, addr, ipst);
15409 
15410 	net_addr = ~net_mask | addr;
15411 	ill_mark_bcast(illgrp, net_addr, ipst);
15412 
15413 	subnet_netmask = ipif->ipif_net_mask;
15414 	addr = ipif->ipif_subnet;
15415 	ill_mark_bcast(illgrp, addr, ipst);
15416 
15417 	subnet_addr = ~subnet_netmask | addr;
15418 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15419 }
15420 
15421 /*
15422  * Whenever we form or delete ill groups, we need to nominate one set of
15423  * BROADCAST ires for receiving in the group.
15424  *
15425  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15426  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15427  *    for ill_ipif_up_count to be non-zero. This is the only case where
15428  *    ill_ipif_up_count is zero and we would still find the ires.
15429  *
15430  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15431  *    ipif is UP and we just have to do the nomination.
15432  *
15433  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15434  *    from the group. So, we have to do the nomination.
15435  *
15436  * Because of (3), there could be just one ill in the group. But we have
15437  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15438  * Thus, this function does not optimize when there is only one ill as
15439  * it is not correct for (3).
15440  */
15441 static void
15442 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15443 {
15444 	ill_t *ill;
15445 	ipif_t *ipif;
15446 	ipaddr_t subnet_addr;
15447 	ipaddr_t prev_subnet_addr = 0;
15448 	ipaddr_t net_addr;
15449 	ipaddr_t prev_net_addr = 0;
15450 	ipaddr_t net_mask = 0;
15451 	ipaddr_t subnet_netmask;
15452 	ipaddr_t addr;
15453 	ip_stack_t	*ipst;
15454 
15455 	/*
15456 	 * When the last memeber is leaving, there is nothing to
15457 	 * nominate.
15458 	 */
15459 	if (illgrp->illgrp_ill_count == 0) {
15460 		ASSERT(illgrp->illgrp_ill == NULL);
15461 		return;
15462 	}
15463 
15464 	ill = illgrp->illgrp_ill;
15465 	ASSERT(!ill->ill_isv6);
15466 	ipst = ill->ill_ipst;
15467 	/*
15468 	 * We assume that ires with same address and belonging to the
15469 	 * same group, has been grouped together. Nominating a *single*
15470 	 * ill in the group for sending and receiving broadcast is done
15471 	 * by making sure that the first BROADCAST ire (which will be
15472 	 * the one returned by ire_ctable_lookup for ip_rput and the
15473 	 * one that will be used in ip_wput_ire) will be the one that
15474 	 * will not have IRE_MARK_NORECV set.
15475 	 *
15476 	 * 1) ip_rput checks and discards packets received on ires marked
15477 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15478 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15479 	 *    first ire in the group for every broadcast address in the group.
15480 	 *    ip_rput will accept packets only on the first ire i.e only
15481 	 *    one copy of the ill.
15482 	 *
15483 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15484 	 *    packet for the whole group. It needs to send out on the ill
15485 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15486 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15487 	 *    the copy echoed back on other port where the ire is not marked
15488 	 *    with IRE_MARK_NORECV.
15489 	 *
15490 	 * Note that we just need to have the first IRE either loopback or
15491 	 * non-loopback (either of them may not exist if ire_create failed
15492 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15493 	 * always hit the first one and hence will always accept one copy.
15494 	 *
15495 	 * We have a broadcast ire per ill for all the unique prefixes
15496 	 * hosted on that ill. As we don't have a way of knowing the
15497 	 * unique prefixes on a given ill and hence in the whole group,
15498 	 * we just call ill_mark_bcast on all the prefixes that exist
15499 	 * in the group. For the common case of one prefix, the code
15500 	 * below optimizes by remebering the last address used for
15501 	 * markng. In the case of multiple prefixes, this will still
15502 	 * optimize depending the order of prefixes.
15503 	 *
15504 	 * The only unique address across the whole group is 0.0.0.0 and
15505 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15506 	 * the first ire in the bucket for receiving and disables the
15507 	 * others.
15508 	 */
15509 	ill_mark_bcast(illgrp, 0, ipst);
15510 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15511 	for (; ill != NULL; ill = ill->ill_group_next) {
15512 
15513 		for (ipif = ill->ill_ipif; ipif != NULL;
15514 		    ipif = ipif->ipif_next) {
15515 
15516 			if (!(ipif->ipif_flags & IPIF_UP) ||
15517 			    ipif->ipif_subnet == 0) {
15518 				continue;
15519 			}
15520 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15521 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15522 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15523 			} else {
15524 				net_mask = htonl(IN_CLASSA_NET);
15525 			}
15526 			addr = net_mask & ipif->ipif_subnet;
15527 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15528 				ill_mark_bcast(illgrp, addr, ipst);
15529 				net_addr = ~net_mask | addr;
15530 				ill_mark_bcast(illgrp, net_addr, ipst);
15531 			}
15532 			prev_net_addr = addr;
15533 
15534 			subnet_netmask = ipif->ipif_net_mask;
15535 			addr = ipif->ipif_subnet;
15536 			if (prev_subnet_addr == 0 ||
15537 			    prev_subnet_addr != addr) {
15538 				ill_mark_bcast(illgrp, addr, ipst);
15539 				subnet_addr = ~subnet_netmask | addr;
15540 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15541 			}
15542 			prev_subnet_addr = addr;
15543 		}
15544 	}
15545 }
15546 
15547 /*
15548  * This function is called while forming ill groups.
15549  *
15550  * Currently, we handle only allmulti groups. We want to join
15551  * allmulti on only one of the ills in the groups. In future,
15552  * when we have link aggregation, we may have to join normal
15553  * multicast groups on multiple ills as switch does inbound load
15554  * balancing. Following are the functions that calls this
15555  * function :
15556  *
15557  * 1) ill_recover_multicast : Interface is coming back UP.
15558  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15559  *    will call ill_recover_multicast to recover all the multicast
15560  *    groups. We need to make sure that only one member is joined
15561  *    in the ill group.
15562  *
15563  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15564  *    Somebody is joining allmulti. We need to make sure that only one
15565  *    member is joined in the group.
15566  *
15567  * 3) illgrp_insert : If allmulti has already joined, we need to make
15568  *    sure that only one member is joined in the group.
15569  *
15570  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15571  *    allmulti who we have nominated. We need to pick someother ill.
15572  *
15573  * 5) illgrp_delete : The ill we nominated is leaving the group,
15574  *    we need to pick a new ill to join the group.
15575  *
15576  * For (1), (2), (5) - we just have to check whether there is
15577  * a good ill joined in the group. If we could not find any ills
15578  * joined the group, we should join.
15579  *
15580  * For (4), the one that was nominated to receive, left the group.
15581  * There could be nobody joined in the group when this function is
15582  * called.
15583  *
15584  * For (3) - we need to explicitly check whether there are multiple
15585  * ills joined in the group.
15586  *
15587  * For simplicity, we don't differentiate any of the above cases. We
15588  * just leave the group if it is joined on any of them and join on
15589  * the first good ill.
15590  */
15591 int
15592 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15593 {
15594 	ilm_t *ilm;
15595 	ill_t *ill;
15596 	ill_t *fallback_inactive_ill = NULL;
15597 	ill_t *fallback_failed_ill = NULL;
15598 	int ret = 0;
15599 
15600 	/*
15601 	 * Leave the allmulti on all the ills and start fresh.
15602 	 */
15603 	for (ill = illgrp->illgrp_ill; ill != NULL;
15604 	    ill = ill->ill_group_next) {
15605 		if (ill->ill_join_allmulti)
15606 			(void) ip_leave_allmulti(ill->ill_ipif);
15607 	}
15608 
15609 	/*
15610 	 * Choose a good ill. Fallback to inactive or failed if
15611 	 * none available. We need to fallback to FAILED in the
15612 	 * case where we have 2 interfaces in a group - where
15613 	 * one of them is failed and another is a good one and
15614 	 * the good one (not marked inactive) is leaving the group.
15615 	 */
15616 	ret = 0;
15617 	for (ill = illgrp->illgrp_ill; ill != NULL;
15618 	    ill = ill->ill_group_next) {
15619 		/* Never pick an offline interface */
15620 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15621 			continue;
15622 
15623 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15624 			fallback_failed_ill = ill;
15625 			continue;
15626 		}
15627 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15628 			fallback_inactive_ill = ill;
15629 			continue;
15630 		}
15631 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15632 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15633 				ret = ip_join_allmulti(ill->ill_ipif);
15634 				/*
15635 				 * ip_join_allmulti can fail because of memory
15636 				 * failures. So, make sure we join at least
15637 				 * on one ill.
15638 				 */
15639 				if (ill->ill_join_allmulti)
15640 					return (0);
15641 			}
15642 		}
15643 	}
15644 	if (ret != 0) {
15645 		/*
15646 		 * If we tried nominating above and failed to do so,
15647 		 * return error. We might have tried multiple times.
15648 		 * But, return the latest error.
15649 		 */
15650 		return (ret);
15651 	}
15652 	if ((ill = fallback_inactive_ill) != NULL) {
15653 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15654 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15655 				ret = ip_join_allmulti(ill->ill_ipif);
15656 				return (ret);
15657 			}
15658 		}
15659 	} else if ((ill = fallback_failed_ill) != NULL) {
15660 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15661 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15662 				ret = ip_join_allmulti(ill->ill_ipif);
15663 				return (ret);
15664 			}
15665 		}
15666 	}
15667 	return (0);
15668 }
15669 
15670 /*
15671  * This function is called from illgrp_delete after it is
15672  * deleted from the group to reschedule responsibilities
15673  * to a different ill.
15674  */
15675 static void
15676 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15677 {
15678 	ilm_t	*ilm;
15679 	ipif_t	*ipif;
15680 	ipaddr_t subnet_addr;
15681 	ipaddr_t net_addr;
15682 	ipaddr_t net_mask = 0;
15683 	ipaddr_t subnet_netmask;
15684 	ipaddr_t addr;
15685 	ip_stack_t *ipst = ill->ill_ipst;
15686 
15687 	ASSERT(ill->ill_group == NULL);
15688 	/*
15689 	 * Broadcast Responsibility:
15690 	 *
15691 	 * 1. If this ill has been nominated for receiving broadcast
15692 	 * packets, we need to find a new one. Before we find a new
15693 	 * one, we need to re-group the ires that are part of this new
15694 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15695 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15696 	 * thing for us.
15697 	 *
15698 	 * 2. If this ill was not nominated for receiving broadcast
15699 	 * packets, we need to clear the IRE_MARK_NORECV flag
15700 	 * so that we continue to send up broadcast packets.
15701 	 */
15702 	if (!ill->ill_isv6) {
15703 		/*
15704 		 * Case 1 above : No optimization here. Just redo the
15705 		 * nomination.
15706 		 */
15707 		ill_group_bcast_for_xmit(ill);
15708 		ill_nominate_bcast_rcv(illgrp);
15709 
15710 		/*
15711 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15712 		 */
15713 		ill_clear_bcast_mark(ill, 0);
15714 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15715 
15716 		for (ipif = ill->ill_ipif; ipif != NULL;
15717 		    ipif = ipif->ipif_next) {
15718 
15719 			if (!(ipif->ipif_flags & IPIF_UP) ||
15720 			    ipif->ipif_subnet == 0) {
15721 				continue;
15722 			}
15723 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15724 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15725 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15726 			} else {
15727 				net_mask = htonl(IN_CLASSA_NET);
15728 			}
15729 			addr = net_mask & ipif->ipif_subnet;
15730 			ill_clear_bcast_mark(ill, addr);
15731 
15732 			net_addr = ~net_mask | addr;
15733 			ill_clear_bcast_mark(ill, net_addr);
15734 
15735 			subnet_netmask = ipif->ipif_net_mask;
15736 			addr = ipif->ipif_subnet;
15737 			ill_clear_bcast_mark(ill, addr);
15738 
15739 			subnet_addr = ~subnet_netmask | addr;
15740 			ill_clear_bcast_mark(ill, subnet_addr);
15741 		}
15742 	}
15743 
15744 	/*
15745 	 * Multicast Responsibility.
15746 	 *
15747 	 * If we have joined allmulti on this one, find a new member
15748 	 * in the group to join allmulti. As this ill is already part
15749 	 * of allmulti, we don't have to join on this one.
15750 	 *
15751 	 * If we have not joined allmulti on this one, there is no
15752 	 * responsibility to handoff. But we need to take new
15753 	 * responsibility i.e, join allmulti on this one if we need
15754 	 * to.
15755 	 */
15756 	if (ill->ill_join_allmulti) {
15757 		(void) ill_nominate_mcast_rcv(illgrp);
15758 	} else {
15759 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15760 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15761 				(void) ip_join_allmulti(ill->ill_ipif);
15762 				break;
15763 			}
15764 		}
15765 	}
15766 
15767 	/*
15768 	 * We intentionally do the flushing of IRE_CACHES only matching
15769 	 * on the ill and not on groups. Note that we are already deleted
15770 	 * from the group.
15771 	 *
15772 	 * This will make sure that all IRE_CACHES whose stq is pointing
15773 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15774 	 * deleted and IRE_CACHES that are not pointing at this ill will
15775 	 * be left alone.
15776 	 */
15777 	if (ill->ill_isv6) {
15778 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15779 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15780 	} else {
15781 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15782 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15783 	}
15784 
15785 	/*
15786 	 * Some conn may have cached one of the IREs deleted above. By removing
15787 	 * the ire reference, we clean up the extra reference to the ill held in
15788 	 * ire->ire_stq.
15789 	 */
15790 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15791 
15792 	/*
15793 	 * Re-do source address selection for all the members in the
15794 	 * group, if they borrowed source address from one of the ipifs
15795 	 * in this ill.
15796 	 */
15797 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15798 		if (ill->ill_isv6) {
15799 			ipif_update_other_ipifs_v6(ipif, illgrp);
15800 		} else {
15801 			ipif_update_other_ipifs(ipif, illgrp);
15802 		}
15803 	}
15804 }
15805 
15806 /*
15807  * Delete the ill from the group. The caller makes sure that it is
15808  * in a group and it okay to delete from the group. So, we always
15809  * delete here.
15810  */
15811 static void
15812 illgrp_delete(ill_t *ill)
15813 {
15814 	ill_group_t *illgrp;
15815 	ill_group_t *tmpg;
15816 	ill_t *tmp_ill;
15817 	ip_stack_t	*ipst = ill->ill_ipst;
15818 
15819 	/*
15820 	 * Reset illgrp_ill_schednext if it was pointing at us.
15821 	 * We need to do this before we set ill_group to NULL.
15822 	 */
15823 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15824 	mutex_enter(&ill->ill_lock);
15825 
15826 	illgrp_reset_schednext(ill);
15827 
15828 	illgrp = ill->ill_group;
15829 
15830 	/* Delete the ill from illgrp. */
15831 	if (illgrp->illgrp_ill == ill) {
15832 		illgrp->illgrp_ill = ill->ill_group_next;
15833 	} else {
15834 		tmp_ill = illgrp->illgrp_ill;
15835 		while (tmp_ill->ill_group_next != ill) {
15836 			tmp_ill = tmp_ill->ill_group_next;
15837 			ASSERT(tmp_ill != NULL);
15838 		}
15839 		tmp_ill->ill_group_next = ill->ill_group_next;
15840 	}
15841 	ill->ill_group = NULL;
15842 	ill->ill_group_next = NULL;
15843 
15844 	illgrp->illgrp_ill_count--;
15845 	mutex_exit(&ill->ill_lock);
15846 	rw_exit(&ipst->ips_ill_g_lock);
15847 
15848 	/*
15849 	 * As this ill is leaving the group, we need to hand off
15850 	 * the responsibilities to the other ills in the group, if
15851 	 * this ill had some responsibilities.
15852 	 */
15853 
15854 	ill_handoff_responsibility(ill, illgrp);
15855 
15856 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15857 
15858 	if (illgrp->illgrp_ill_count == 0) {
15859 
15860 		ASSERT(illgrp->illgrp_ill == NULL);
15861 		if (ill->ill_isv6) {
15862 			if (illgrp == ipst->ips_illgrp_head_v6) {
15863 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15864 			} else {
15865 				tmpg = ipst->ips_illgrp_head_v6;
15866 				while (tmpg->illgrp_next != illgrp) {
15867 					tmpg = tmpg->illgrp_next;
15868 					ASSERT(tmpg != NULL);
15869 				}
15870 				tmpg->illgrp_next = illgrp->illgrp_next;
15871 			}
15872 		} else {
15873 			if (illgrp == ipst->ips_illgrp_head_v4) {
15874 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15875 			} else {
15876 				tmpg = ipst->ips_illgrp_head_v4;
15877 				while (tmpg->illgrp_next != illgrp) {
15878 					tmpg = tmpg->illgrp_next;
15879 					ASSERT(tmpg != NULL);
15880 				}
15881 				tmpg->illgrp_next = illgrp->illgrp_next;
15882 			}
15883 		}
15884 		mutex_destroy(&illgrp->illgrp_lock);
15885 		mi_free(illgrp);
15886 	}
15887 	rw_exit(&ipst->ips_ill_g_lock);
15888 
15889 	/*
15890 	 * Even though the ill is out of the group its not necessary
15891 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15892 	 * We will split the ipsq when phyint_groupname is set to NULL.
15893 	 */
15894 
15895 	/*
15896 	 * Send a routing sockets message if we are deleting from
15897 	 * groups with names.
15898 	 */
15899 	if (ill->ill_phyint->phyint_groupname_len != 0)
15900 		ip_rts_ifmsg(ill->ill_ipif);
15901 }
15902 
15903 /*
15904  * Re-do source address selection. This is normally called when
15905  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15906  * ipif comes up.
15907  */
15908 void
15909 ill_update_source_selection(ill_t *ill)
15910 {
15911 	ipif_t *ipif;
15912 
15913 	ASSERT(IAM_WRITER_ILL(ill));
15914 
15915 	if (ill->ill_group != NULL)
15916 		ill = ill->ill_group->illgrp_ill;
15917 
15918 	for (; ill != NULL; ill = ill->ill_group_next) {
15919 		for (ipif = ill->ill_ipif; ipif != NULL;
15920 		    ipif = ipif->ipif_next) {
15921 			if (ill->ill_isv6)
15922 				ipif_recreate_interface_routes_v6(NULL, ipif);
15923 			else
15924 				ipif_recreate_interface_routes(NULL, ipif);
15925 		}
15926 	}
15927 }
15928 
15929 /*
15930  * Insert ill in a group headed by illgrp_head. The caller can either
15931  * pass a groupname in which case we search for a group with the
15932  * same name to insert in or pass a group to insert in. This function
15933  * would only search groups with names.
15934  *
15935  * NOTE : The caller should make sure that there is at least one ipif
15936  *	  UP on this ill so that illgrp_scheduler can pick this ill
15937  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15938  *	  already sent a DL_UNBIND to the driver and we don't want to
15939  *	  send anymore packets. We don't assert for ipif_up_count
15940  *	  to be greater than zero, because ipif_up_done wants to call
15941  *	  this function before bumping up the ipif_up_count. See
15942  *	  ipif_up_done() for details.
15943  */
15944 int
15945 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15946     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15947 {
15948 	ill_group_t *illgrp;
15949 	ill_t *prev_ill;
15950 	phyint_t *phyi;
15951 	ip_stack_t	*ipst = ill->ill_ipst;
15952 
15953 	ASSERT(ill->ill_group == NULL);
15954 
15955 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15956 	mutex_enter(&ill->ill_lock);
15957 
15958 	if (groupname != NULL) {
15959 		/*
15960 		 * Look for a group with a matching groupname to insert.
15961 		 */
15962 		for (illgrp = *illgrp_head; illgrp != NULL;
15963 		    illgrp = illgrp->illgrp_next) {
15964 
15965 			ill_t *tmp_ill;
15966 
15967 			/*
15968 			 * If we have an ill_group_t in the list which has
15969 			 * no ill_t assigned then we must be in the process of
15970 			 * removing this group. We skip this as illgrp_delete()
15971 			 * will remove it from the list.
15972 			 */
15973 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15974 				ASSERT(illgrp->illgrp_ill_count == 0);
15975 				continue;
15976 			}
15977 
15978 			ASSERT(tmp_ill->ill_phyint != NULL);
15979 			phyi = tmp_ill->ill_phyint;
15980 			/*
15981 			 * Look at groups which has names only.
15982 			 */
15983 			if (phyi->phyint_groupname_len == 0)
15984 				continue;
15985 			/*
15986 			 * Names are stored in the phyint common to both
15987 			 * IPv4 and IPv6.
15988 			 */
15989 			if (mi_strcmp(phyi->phyint_groupname,
15990 			    groupname) == 0) {
15991 				break;
15992 			}
15993 		}
15994 	} else {
15995 		/*
15996 		 * If the caller passes in a NULL "grp_to_insert", we
15997 		 * allocate one below and insert this singleton.
15998 		 */
15999 		illgrp = grp_to_insert;
16000 	}
16001 
16002 	ill->ill_group_next = NULL;
16003 
16004 	if (illgrp == NULL) {
16005 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16006 		if (illgrp == NULL) {
16007 			return (ENOMEM);
16008 		}
16009 		illgrp->illgrp_next = *illgrp_head;
16010 		*illgrp_head = illgrp;
16011 		illgrp->illgrp_ill = ill;
16012 		illgrp->illgrp_ill_count = 1;
16013 		ill->ill_group = illgrp;
16014 		/*
16015 		 * Used in illgrp_scheduler to protect multiple threads
16016 		 * from traversing the list.
16017 		 */
16018 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16019 	} else {
16020 		ASSERT(ill->ill_net_type ==
16021 		    illgrp->illgrp_ill->ill_net_type);
16022 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16023 
16024 		/* Insert ill at tail of this group */
16025 		prev_ill = illgrp->illgrp_ill;
16026 		while (prev_ill->ill_group_next != NULL)
16027 			prev_ill = prev_ill->ill_group_next;
16028 		prev_ill->ill_group_next = ill;
16029 		ill->ill_group = illgrp;
16030 		illgrp->illgrp_ill_count++;
16031 		/*
16032 		 * Inherit group properties. Currently only forwarding
16033 		 * is the property we try to keep the same with all the
16034 		 * ills. When there are more, we will abstract this into
16035 		 * a function.
16036 		 */
16037 		ill->ill_flags &= ~ILLF_ROUTER;
16038 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16039 	}
16040 	mutex_exit(&ill->ill_lock);
16041 	rw_exit(&ipst->ips_ill_g_lock);
16042 
16043 	/*
16044 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16045 	 *    may be zero as it has not yet been bumped. But the ires
16046 	 *    have already been added. So, we do the nomination here
16047 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16048 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16049 	 *    ill_ipif_up_count here while nominating broadcast ires for
16050 	 *    receive.
16051 	 *
16052 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16053 	 *    to group them properly as ire_add() has already happened
16054 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16055 	 *    case, we need to do it here anyway.
16056 	 */
16057 	if (!ill->ill_isv6) {
16058 		ill_group_bcast_for_xmit(ill);
16059 		ill_nominate_bcast_rcv(illgrp);
16060 	}
16061 
16062 	if (!ipif_is_coming_up) {
16063 		/*
16064 		 * When ipif_up_done() calls this function, the multicast
16065 		 * groups have not been joined yet. So, there is no point in
16066 		 * nomination. ip_join_allmulti will handle groups when
16067 		 * ill_recover_multicast is called from ipif_up_done() later.
16068 		 */
16069 		(void) ill_nominate_mcast_rcv(illgrp);
16070 		/*
16071 		 * ipif_up_done calls ill_update_source_selection
16072 		 * anyway. Moreover, we don't want to re-create
16073 		 * interface routes while ipif_up_done() still has reference
16074 		 * to them. Refer to ipif_up_done() for more details.
16075 		 */
16076 		ill_update_source_selection(ill);
16077 	}
16078 
16079 	/*
16080 	 * Send a routing sockets message if we are inserting into
16081 	 * groups with names.
16082 	 */
16083 	if (groupname != NULL)
16084 		ip_rts_ifmsg(ill->ill_ipif);
16085 	return (0);
16086 }
16087 
16088 /*
16089  * Return the first phyint matching the groupname. There could
16090  * be more than one when there are ill groups.
16091  *
16092  * If 'usable' is set, then we exclude ones that are marked with any of
16093  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16094  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16095  * emulation of ipmp.
16096  */
16097 phyint_t *
16098 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16099 {
16100 	phyint_t *phyi;
16101 
16102 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16103 	/*
16104 	 * Group names are stored in the phyint - a common structure
16105 	 * to both IPv4 and IPv6.
16106 	 */
16107 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16108 	for (; phyi != NULL;
16109 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16110 	    phyi, AVL_AFTER)) {
16111 		if (phyi->phyint_groupname_len == 0)
16112 			continue;
16113 		/*
16114 		 * Skip the ones that should not be used since the callers
16115 		 * sometime use this for sending packets.
16116 		 */
16117 		if (usable && (phyi->phyint_flags &
16118 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16119 			continue;
16120 
16121 		ASSERT(phyi->phyint_groupname != NULL);
16122 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16123 			return (phyi);
16124 	}
16125 	return (NULL);
16126 }
16127 
16128 
16129 /*
16130  * Return the first usable phyint matching the group index. By 'usable'
16131  * we exclude ones that are marked ununsable with any of
16132  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16133  *
16134  * Used only for the ipmp/netinfo emulation of ipmp.
16135  */
16136 phyint_t *
16137 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16138 {
16139 	phyint_t *phyi;
16140 
16141 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16142 
16143 	if (!ipst->ips_ipmp_hook_emulation)
16144 		return (NULL);
16145 
16146 	/*
16147 	 * Group indicies are stored in the phyint - a common structure
16148 	 * to both IPv4 and IPv6.
16149 	 */
16150 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16151 	for (; phyi != NULL;
16152 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16153 	    phyi, AVL_AFTER)) {
16154 		/* Ignore the ones that do not have a group */
16155 		if (phyi->phyint_groupname_len == 0)
16156 			continue;
16157 
16158 		ASSERT(phyi->phyint_group_ifindex != 0);
16159 		/*
16160 		 * Skip the ones that should not be used since the callers
16161 		 * sometime use this for sending packets.
16162 		 */
16163 		if (phyi->phyint_flags &
16164 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16165 			continue;
16166 		if (phyi->phyint_group_ifindex == group_ifindex)
16167 			return (phyi);
16168 	}
16169 	return (NULL);
16170 }
16171 
16172 
16173 /*
16174  * MT notes on creation and deletion of IPMP groups
16175  *
16176  * Creation and deletion of IPMP groups introduce the need to merge or
16177  * split the associated serialization objects i.e the ipsq's. Normally all
16178  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16179  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16180  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16181  * is a need to change the <ill-ipsq> association and we have to operate on both
16182  * the source and destination IPMP groups. For eg. attempting to set the
16183  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16184  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16185  * source or destination IPMP group are mapped to a single ipsq for executing
16186  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16187  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16188  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16189  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16190  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16191  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16192  *
16193  * In the above example the ioctl handling code locates the current ipsq of hme0
16194  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16195  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16196  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16197  * the destination ipsq. If the destination ipsq is not busy, it also enters
16198  * the destination ipsq exclusively. Now the actual groupname setting operation
16199  * can proceed. If the destination ipsq is busy, the operation is enqueued
16200  * on the destination (merged) ipsq and will be handled in the unwind from
16201  * ipsq_exit.
16202  *
16203  * To prevent other threads accessing the ill while the group name change is
16204  * in progres, we bring down the ipifs which also removes the ill from the
16205  * group. The group is changed in phyint and when the first ipif on the ill
16206  * is brought up, the ill is inserted into the right IPMP group by
16207  * illgrp_insert.
16208  */
16209 /* ARGSUSED */
16210 int
16211 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16212     ip_ioctl_cmd_t *ipip, void *ifreq)
16213 {
16214 	int i;
16215 	char *tmp;
16216 	int namelen;
16217 	ill_t *ill = ipif->ipif_ill;
16218 	ill_t *ill_v4, *ill_v6;
16219 	int err = 0;
16220 	phyint_t *phyi;
16221 	phyint_t *phyi_tmp;
16222 	struct lifreq *lifr;
16223 	mblk_t	*mp1;
16224 	char *groupname;
16225 	ipsq_t *ipsq;
16226 	ip_stack_t	*ipst = ill->ill_ipst;
16227 
16228 	ASSERT(IAM_WRITER_IPIF(ipif));
16229 
16230 	/* Existance verified in ip_wput_nondata */
16231 	mp1 = mp->b_cont->b_cont;
16232 	lifr = (struct lifreq *)mp1->b_rptr;
16233 	groupname = lifr->lifr_groupname;
16234 
16235 	if (ipif->ipif_id != 0)
16236 		return (EINVAL);
16237 
16238 	phyi = ill->ill_phyint;
16239 	ASSERT(phyi != NULL);
16240 
16241 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16242 		return (EINVAL);
16243 
16244 	tmp = groupname;
16245 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16246 		;
16247 
16248 	if (i == LIFNAMSIZ) {
16249 		/* no null termination */
16250 		return (EINVAL);
16251 	}
16252 
16253 	/*
16254 	 * Calculate the namelen exclusive of the null
16255 	 * termination character.
16256 	 */
16257 	namelen = tmp - groupname;
16258 
16259 	ill_v4 = phyi->phyint_illv4;
16260 	ill_v6 = phyi->phyint_illv6;
16261 
16262 	/*
16263 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16264 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16265 	 * synchronization notes in ip.c
16266 	 */
16267 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16268 		return (EINVAL);
16269 	}
16270 
16271 	/*
16272 	 * mark the ill as changing.
16273 	 * this should queue all new requests on the syncq.
16274 	 */
16275 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16276 
16277 	if (ill_v4 != NULL)
16278 		ill_v4->ill_state_flags |= ILL_CHANGING;
16279 	if (ill_v6 != NULL)
16280 		ill_v6->ill_state_flags |= ILL_CHANGING;
16281 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16282 
16283 	if (namelen == 0) {
16284 		/*
16285 		 * Null string means remove this interface from the
16286 		 * existing group.
16287 		 */
16288 		if (phyi->phyint_groupname_len == 0) {
16289 			/*
16290 			 * Never was in a group.
16291 			 */
16292 			err = 0;
16293 			goto done;
16294 		}
16295 
16296 		/*
16297 		 * IPv4 or IPv6 may be temporarily out of the group when all
16298 		 * the ipifs are down. Thus, we need to check for ill_group to
16299 		 * be non-NULL.
16300 		 */
16301 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16302 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16303 			mutex_enter(&ill_v4->ill_lock);
16304 			if (!ill_is_quiescent(ill_v4)) {
16305 				/*
16306 				 * ipsq_pending_mp_add will not fail since
16307 				 * connp is NULL
16308 				 */
16309 				(void) ipsq_pending_mp_add(NULL,
16310 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16311 				mutex_exit(&ill_v4->ill_lock);
16312 				err = EINPROGRESS;
16313 				goto done;
16314 			}
16315 			mutex_exit(&ill_v4->ill_lock);
16316 		}
16317 
16318 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16319 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16320 			mutex_enter(&ill_v6->ill_lock);
16321 			if (!ill_is_quiescent(ill_v6)) {
16322 				(void) ipsq_pending_mp_add(NULL,
16323 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16324 				mutex_exit(&ill_v6->ill_lock);
16325 				err = EINPROGRESS;
16326 				goto done;
16327 			}
16328 			mutex_exit(&ill_v6->ill_lock);
16329 		}
16330 
16331 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16332 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16333 		mutex_enter(&phyi->phyint_lock);
16334 		ASSERT(phyi->phyint_groupname != NULL);
16335 		mi_free(phyi->phyint_groupname);
16336 		phyi->phyint_groupname = NULL;
16337 		phyi->phyint_groupname_len = 0;
16338 
16339 		/* Restore the ifindex used to be the per interface one */
16340 		phyi->phyint_group_ifindex = 0;
16341 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16342 		mutex_exit(&phyi->phyint_lock);
16343 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16344 		rw_exit(&ipst->ips_ill_g_lock);
16345 		err = ill_up_ipifs(ill, q, mp);
16346 
16347 		/*
16348 		 * set the split flag so that the ipsq can be split
16349 		 */
16350 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16351 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16352 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16353 
16354 	} else {
16355 		if (phyi->phyint_groupname_len != 0) {
16356 			ASSERT(phyi->phyint_groupname != NULL);
16357 			/* Are we inserting in the same group ? */
16358 			if (mi_strcmp(groupname,
16359 			    phyi->phyint_groupname) == 0) {
16360 				err = 0;
16361 				goto done;
16362 			}
16363 		}
16364 
16365 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16366 		/*
16367 		 * Merge ipsq for the group's.
16368 		 * This check is here as multiple groups/ills might be
16369 		 * sharing the same ipsq.
16370 		 * If we have to merege than the operation is restarted
16371 		 * on the new ipsq.
16372 		 */
16373 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16374 		if (phyi->phyint_ipsq != ipsq) {
16375 			rw_exit(&ipst->ips_ill_g_lock);
16376 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16377 			goto done;
16378 		}
16379 		/*
16380 		 * Running exclusive on new ipsq.
16381 		 */
16382 
16383 		ASSERT(ipsq != NULL);
16384 		ASSERT(ipsq->ipsq_writer == curthread);
16385 
16386 		/*
16387 		 * Check whether the ill_type and ill_net_type matches before
16388 		 * we allocate any memory so that the cleanup is easier.
16389 		 *
16390 		 * We can't group dissimilar ones as we can't load spread
16391 		 * packets across the group because of potential link-level
16392 		 * header differences.
16393 		 */
16394 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16395 		if (phyi_tmp != NULL) {
16396 			if ((ill_v4 != NULL &&
16397 			    phyi_tmp->phyint_illv4 != NULL) &&
16398 			    ((ill_v4->ill_net_type !=
16399 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16400 			    (ill_v4->ill_type !=
16401 			    phyi_tmp->phyint_illv4->ill_type))) {
16402 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16403 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16404 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16405 				rw_exit(&ipst->ips_ill_g_lock);
16406 				return (EINVAL);
16407 			}
16408 			if ((ill_v6 != NULL &&
16409 			    phyi_tmp->phyint_illv6 != NULL) &&
16410 			    ((ill_v6->ill_net_type !=
16411 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16412 			    (ill_v6->ill_type !=
16413 			    phyi_tmp->phyint_illv6->ill_type))) {
16414 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16415 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16416 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16417 				rw_exit(&ipst->ips_ill_g_lock);
16418 				return (EINVAL);
16419 			}
16420 		}
16421 
16422 		rw_exit(&ipst->ips_ill_g_lock);
16423 
16424 		/*
16425 		 * bring down all v4 ipifs.
16426 		 */
16427 		if (ill_v4 != NULL) {
16428 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16429 		}
16430 
16431 		/*
16432 		 * bring down all v6 ipifs.
16433 		 */
16434 		if (ill_v6 != NULL) {
16435 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16436 		}
16437 
16438 		/*
16439 		 * make sure all ipifs are down and there are no active
16440 		 * references. Call to ipsq_pending_mp_add will not fail
16441 		 * since connp is NULL.
16442 		 */
16443 		if (ill_v4 != NULL) {
16444 			mutex_enter(&ill_v4->ill_lock);
16445 			if (!ill_is_quiescent(ill_v4)) {
16446 				(void) ipsq_pending_mp_add(NULL,
16447 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16448 				mutex_exit(&ill_v4->ill_lock);
16449 				err = EINPROGRESS;
16450 				goto done;
16451 			}
16452 			mutex_exit(&ill_v4->ill_lock);
16453 		}
16454 
16455 		if (ill_v6 != NULL) {
16456 			mutex_enter(&ill_v6->ill_lock);
16457 			if (!ill_is_quiescent(ill_v6)) {
16458 				(void) ipsq_pending_mp_add(NULL,
16459 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16460 				mutex_exit(&ill_v6->ill_lock);
16461 				err = EINPROGRESS;
16462 				goto done;
16463 			}
16464 			mutex_exit(&ill_v6->ill_lock);
16465 		}
16466 
16467 		/*
16468 		 * allocate including space for null terminator
16469 		 * before we insert.
16470 		 */
16471 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16472 		if (tmp == NULL)
16473 			return (ENOMEM);
16474 
16475 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16476 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16477 		mutex_enter(&phyi->phyint_lock);
16478 		if (phyi->phyint_groupname_len != 0) {
16479 			ASSERT(phyi->phyint_groupname != NULL);
16480 			mi_free(phyi->phyint_groupname);
16481 		}
16482 
16483 		/*
16484 		 * setup the new group name.
16485 		 */
16486 		phyi->phyint_groupname = tmp;
16487 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16488 		phyi->phyint_groupname_len = namelen + 1;
16489 
16490 		if (ipst->ips_ipmp_hook_emulation) {
16491 			/*
16492 			 * If the group already exists we use the existing
16493 			 * group_ifindex, otherwise we pick a new index here.
16494 			 */
16495 			if (phyi_tmp != NULL) {
16496 				phyi->phyint_group_ifindex =
16497 				    phyi_tmp->phyint_group_ifindex;
16498 			} else {
16499 				/* XXX We need a recovery strategy here. */
16500 				if (!ip_assign_ifindex(
16501 				    &phyi->phyint_group_ifindex, ipst))
16502 					cmn_err(CE_PANIC,
16503 					    "ip_assign_ifindex() failed");
16504 			}
16505 		}
16506 		/*
16507 		 * Select whether the netinfo and hook use the per-interface
16508 		 * or per-group ifindex.
16509 		 */
16510 		if (ipst->ips_ipmp_hook_emulation)
16511 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16512 		else
16513 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16514 
16515 		if (ipst->ips_ipmp_hook_emulation &&
16516 		    phyi_tmp != NULL) {
16517 			/* First phyint in group - group PLUMB event */
16518 			ill_nic_info_plumb(ill, B_TRUE);
16519 		}
16520 		mutex_exit(&phyi->phyint_lock);
16521 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16522 		rw_exit(&ipst->ips_ill_g_lock);
16523 
16524 		err = ill_up_ipifs(ill, q, mp);
16525 	}
16526 
16527 done:
16528 	/*
16529 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16530 	 */
16531 	if (err != EINPROGRESS) {
16532 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16533 		if (ill_v4 != NULL)
16534 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16535 		if (ill_v6 != NULL)
16536 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16537 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16538 	}
16539 	return (err);
16540 }
16541 
16542 /* ARGSUSED */
16543 int
16544 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16545     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16546 {
16547 	ill_t *ill;
16548 	phyint_t *phyi;
16549 	struct lifreq *lifr;
16550 	mblk_t	*mp1;
16551 
16552 	/* Existence verified in ip_wput_nondata */
16553 	mp1 = mp->b_cont->b_cont;
16554 	lifr = (struct lifreq *)mp1->b_rptr;
16555 	ill = ipif->ipif_ill;
16556 	phyi = ill->ill_phyint;
16557 
16558 	lifr->lifr_groupname[0] = '\0';
16559 	/*
16560 	 * ill_group may be null if all the interfaces
16561 	 * are down. But still, the phyint should always
16562 	 * hold the name.
16563 	 */
16564 	if (phyi->phyint_groupname_len != 0) {
16565 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16566 		    phyi->phyint_groupname_len);
16567 	}
16568 
16569 	return (0);
16570 }
16571 
16572 
16573 typedef struct conn_move_s {
16574 	ill_t	*cm_from_ill;
16575 	ill_t	*cm_to_ill;
16576 	int	cm_ifindex;
16577 } conn_move_t;
16578 
16579 /*
16580  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16581  */
16582 static void
16583 conn_move(conn_t *connp, caddr_t arg)
16584 {
16585 	conn_move_t *connm;
16586 	int ifindex;
16587 	int i;
16588 	ill_t *from_ill;
16589 	ill_t *to_ill;
16590 	ilg_t *ilg;
16591 	ilm_t *ret_ilm;
16592 
16593 	connm = (conn_move_t *)arg;
16594 	ifindex = connm->cm_ifindex;
16595 	from_ill = connm->cm_from_ill;
16596 	to_ill = connm->cm_to_ill;
16597 
16598 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16599 
16600 	/* All multicast fields protected by conn_lock */
16601 	mutex_enter(&connp->conn_lock);
16602 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16603 	if ((connp->conn_outgoing_ill == from_ill) &&
16604 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16605 		connp->conn_outgoing_ill = to_ill;
16606 		connp->conn_incoming_ill = to_ill;
16607 	}
16608 
16609 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16610 
16611 	if ((connp->conn_multicast_ill == from_ill) &&
16612 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16613 		connp->conn_multicast_ill = connm->cm_to_ill;
16614 	}
16615 
16616 	/*
16617 	 * Change the ilg_ill to point to the new one. This assumes
16618 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16619 	 * has been told to receive packets on this interface.
16620 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16621 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16622 	 * some ilms may not have moved. We check to see whether
16623 	 * the ilms have moved to to_ill. We can't check on from_ill
16624 	 * as in the process of moving, we could have split an ilm
16625 	 * in to two - which has the same orig_ifindex and v6group.
16626 	 *
16627 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16628 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16629 	 */
16630 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16631 		ilg = &connp->conn_ilg[i];
16632 		if ((ilg->ilg_ill == from_ill) &&
16633 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16634 			/* ifindex != 0 indicates failback */
16635 			if (ifindex != 0) {
16636 				connp->conn_ilg[i].ilg_ill = to_ill;
16637 				continue;
16638 			}
16639 
16640 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16641 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16642 			    connp->conn_zoneid);
16643 
16644 			if (ret_ilm != NULL)
16645 				connp->conn_ilg[i].ilg_ill = to_ill;
16646 		}
16647 	}
16648 	mutex_exit(&connp->conn_lock);
16649 }
16650 
16651 static void
16652 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16653 {
16654 	conn_move_t connm;
16655 	ip_stack_t	*ipst = from_ill->ill_ipst;
16656 
16657 	connm.cm_from_ill = from_ill;
16658 	connm.cm_to_ill = to_ill;
16659 	connm.cm_ifindex = ifindex;
16660 
16661 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16662 }
16663 
16664 /*
16665  * ilm has been moved from from_ill to to_ill.
16666  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16667  * appropriately.
16668  *
16669  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16670  *	  the code there de-references ipif_ill to get the ill to
16671  *	  send multicast requests. It does not work as ipif is on its
16672  *	  move and already moved when this function is called.
16673  *	  Thus, we need to use from_ill and to_ill send down multicast
16674  *	  requests.
16675  */
16676 static void
16677 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16678 {
16679 	ipif_t *ipif;
16680 	ilm_t *ilm;
16681 
16682 	/*
16683 	 * See whether we need to send down DL_ENABMULTI_REQ on
16684 	 * to_ill as ilm has just been added.
16685 	 */
16686 	ASSERT(IAM_WRITER_ILL(to_ill));
16687 	ASSERT(IAM_WRITER_ILL(from_ill));
16688 
16689 	ILM_WALKER_HOLD(to_ill);
16690 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16691 
16692 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16693 			continue;
16694 		/*
16695 		 * no locks held, ill/ipif cannot dissappear as long
16696 		 * as we are writer.
16697 		 */
16698 		ipif = to_ill->ill_ipif;
16699 		/*
16700 		 * No need to hold any lock as we are the writer and this
16701 		 * can only be changed by a writer.
16702 		 */
16703 		ilm->ilm_is_new = B_FALSE;
16704 
16705 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16706 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16707 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16708 			    "resolver\n"));
16709 			continue;		/* Must be IRE_IF_NORESOLVER */
16710 		}
16711 
16712 
16713 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16714 			ip1dbg(("ilm_send_multicast_reqs: "
16715 			    "to_ill MULTI_BCAST\n"));
16716 			goto from;
16717 		}
16718 
16719 		if (to_ill->ill_isv6)
16720 			mld_joingroup(ilm);
16721 		else
16722 			igmp_joingroup(ilm);
16723 
16724 		if (to_ill->ill_ipif_up_count == 0) {
16725 			/*
16726 			 * Nobody there. All multicast addresses will be
16727 			 * re-joined when we get the DL_BIND_ACK bringing the
16728 			 * interface up.
16729 			 */
16730 			ilm->ilm_notify_driver = B_FALSE;
16731 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16732 			goto from;
16733 		}
16734 
16735 		/*
16736 		 * For allmulti address, we want to join on only one interface.
16737 		 * Checking for ilm_numentries_v6 is not correct as you may
16738 		 * find an ilm with zero address on to_ill, but we may not
16739 		 * have nominated to_ill for receiving. Thus, if we have
16740 		 * nominated from_ill (ill_join_allmulti is set), nominate
16741 		 * only if to_ill is not already nominated (to_ill normally
16742 		 * should not have been nominated if "from_ill" has already
16743 		 * been nominated. As we don't prevent failovers from happening
16744 		 * across groups, we don't assert).
16745 		 */
16746 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16747 			/*
16748 			 * There is no need to hold ill locks as we are
16749 			 * writer on both ills and when ill_join_allmulti
16750 			 * is changed the thread is always a writer.
16751 			 */
16752 			if (from_ill->ill_join_allmulti &&
16753 			    !to_ill->ill_join_allmulti) {
16754 				(void) ip_join_allmulti(to_ill->ill_ipif);
16755 			}
16756 		} else if (ilm->ilm_notify_driver) {
16757 
16758 			/*
16759 			 * This is a newly moved ilm so we need to tell the
16760 			 * driver about the new group. There can be more than
16761 			 * one ilm's for the same group in the list each with a
16762 			 * different orig_ifindex. We have to inform the driver
16763 			 * once. In ilm_move_v[4,6] we only set the flag
16764 			 * ilm_notify_driver for the first ilm.
16765 			 */
16766 
16767 			(void) ip_ll_send_enabmulti_req(to_ill,
16768 			    &ilm->ilm_v6addr);
16769 		}
16770 
16771 		ilm->ilm_notify_driver = B_FALSE;
16772 
16773 		/*
16774 		 * See whether we need to send down DL_DISABMULTI_REQ on
16775 		 * from_ill as ilm has just been removed.
16776 		 */
16777 from:
16778 		ipif = from_ill->ill_ipif;
16779 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16780 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16781 			ip1dbg(("ilm_send_multicast_reqs: "
16782 			    "from_ill not resolver\n"));
16783 			continue;		/* Must be IRE_IF_NORESOLVER */
16784 		}
16785 
16786 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16787 			ip1dbg(("ilm_send_multicast_reqs: "
16788 			    "from_ill MULTI_BCAST\n"));
16789 			continue;
16790 		}
16791 
16792 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16793 			if (from_ill->ill_join_allmulti)
16794 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16795 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16796 			(void) ip_ll_send_disabmulti_req(from_ill,
16797 			    &ilm->ilm_v6addr);
16798 		}
16799 	}
16800 	ILM_WALKER_RELE(to_ill);
16801 }
16802 
16803 /*
16804  * This function is called when all multicast memberships needs
16805  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16806  * called only once unlike the IPv4 counterpart where it is called after
16807  * every logical interface is moved. The reason is due to multicast
16808  * memberships are joined using an interface address in IPv4 while in
16809  * IPv6, interface index is used.
16810  */
16811 static void
16812 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16813 {
16814 	ilm_t	*ilm;
16815 	ilm_t	*ilm_next;
16816 	ilm_t	*new_ilm;
16817 	ilm_t	**ilmp;
16818 	int	count;
16819 	char buf[INET6_ADDRSTRLEN];
16820 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16821 	ip_stack_t	*ipst = from_ill->ill_ipst;
16822 
16823 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16824 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16825 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16826 
16827 	if (ifindex == 0) {
16828 		/*
16829 		 * Form the solicited node mcast address which is used later.
16830 		 */
16831 		ipif_t *ipif;
16832 
16833 		ipif = from_ill->ill_ipif;
16834 		ASSERT(ipif->ipif_id == 0);
16835 
16836 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16837 	}
16838 
16839 	ilmp = &from_ill->ill_ilm;
16840 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16841 		ilm_next = ilm->ilm_next;
16842 
16843 		if (ilm->ilm_flags & ILM_DELETED) {
16844 			ilmp = &ilm->ilm_next;
16845 			continue;
16846 		}
16847 
16848 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16849 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16850 		ASSERT(ilm->ilm_orig_ifindex != 0);
16851 		if (ilm->ilm_orig_ifindex == ifindex) {
16852 			/*
16853 			 * We are failing back multicast memberships.
16854 			 * If the same ilm exists in to_ill, it means somebody
16855 			 * has joined the same group there e.g. ff02::1
16856 			 * is joined within the kernel when the interfaces
16857 			 * came UP.
16858 			 */
16859 			ASSERT(ilm->ilm_ipif == NULL);
16860 			if (new_ilm != NULL) {
16861 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16862 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16863 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16864 					new_ilm->ilm_is_new = B_TRUE;
16865 				}
16866 			} else {
16867 				/*
16868 				 * check if we can just move the ilm
16869 				 */
16870 				if (from_ill->ill_ilm_walker_cnt != 0) {
16871 					/*
16872 					 * We have walkers we cannot move
16873 					 * the ilm, so allocate a new ilm,
16874 					 * this (old) ilm will be marked
16875 					 * ILM_DELETED at the end of the loop
16876 					 * and will be freed when the
16877 					 * last walker exits.
16878 					 */
16879 					new_ilm = (ilm_t *)mi_zalloc
16880 					    (sizeof (ilm_t));
16881 					if (new_ilm == NULL) {
16882 						ip0dbg(("ilm_move_v6: "
16883 						    "FAILBACK of IPv6"
16884 						    " multicast address %s : "
16885 						    "from %s to"
16886 						    " %s failed : ENOMEM \n",
16887 						    inet_ntop(AF_INET6,
16888 						    &ilm->ilm_v6addr, buf,
16889 						    sizeof (buf)),
16890 						    from_ill->ill_name,
16891 						    to_ill->ill_name));
16892 
16893 							ilmp = &ilm->ilm_next;
16894 							continue;
16895 					}
16896 					*new_ilm = *ilm;
16897 					/*
16898 					 * we don't want new_ilm linked to
16899 					 * ilm's filter list.
16900 					 */
16901 					new_ilm->ilm_filter = NULL;
16902 				} else {
16903 					/*
16904 					 * No walkers we can move the ilm.
16905 					 * lets take it out of the list.
16906 					 */
16907 					*ilmp = ilm->ilm_next;
16908 					ilm->ilm_next = NULL;
16909 					new_ilm = ilm;
16910 				}
16911 
16912 				/*
16913 				 * if this is the first ilm for the group
16914 				 * set ilm_notify_driver so that we notify the
16915 				 * driver in ilm_send_multicast_reqs.
16916 				 */
16917 				if (ilm_lookup_ill_v6(to_ill,
16918 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16919 					new_ilm->ilm_notify_driver = B_TRUE;
16920 
16921 				new_ilm->ilm_ill = to_ill;
16922 				/* Add to the to_ill's list */
16923 				new_ilm->ilm_next = to_ill->ill_ilm;
16924 				to_ill->ill_ilm = new_ilm;
16925 				/*
16926 				 * set the flag so that mld_joingroup is
16927 				 * called in ilm_send_multicast_reqs().
16928 				 */
16929 				new_ilm->ilm_is_new = B_TRUE;
16930 			}
16931 			goto bottom;
16932 		} else if (ifindex != 0) {
16933 			/*
16934 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16935 			 * has not matched above, look at the next ilm.
16936 			 */
16937 			ilmp = &ilm->ilm_next;
16938 			continue;
16939 		}
16940 		/*
16941 		 * If we are here, it means ifindex is 0. Failover
16942 		 * everything.
16943 		 *
16944 		 * We need to handle solicited node mcast address
16945 		 * and all_nodes mcast address differently as they
16946 		 * are joined witin the kenrel (ipif_multicast_up)
16947 		 * and potentially from the userland. We are called
16948 		 * after the ipifs of from_ill has been moved.
16949 		 * If we still find ilms on ill with solicited node
16950 		 * mcast address or all_nodes mcast address, it must
16951 		 * belong to the UP interface that has not moved e.g.
16952 		 * ipif_id 0 with the link local prefix does not move.
16953 		 * We join this on the new ill accounting for all the
16954 		 * userland memberships so that applications don't
16955 		 * see any failure.
16956 		 *
16957 		 * We need to make sure that we account only for the
16958 		 * solicited node and all node multicast addresses
16959 		 * that was brought UP on these. In the case of
16960 		 * a failover from A to B, we might have ilms belonging
16961 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16962 		 * for the membership from the userland. If we are failing
16963 		 * over from B to C now, we will find the ones belonging
16964 		 * to A on B. These don't account for the ill_ipif_up_count.
16965 		 * They just move from B to C. The check below on
16966 		 * ilm_orig_ifindex ensures that.
16967 		 */
16968 		if ((ilm->ilm_orig_ifindex ==
16969 		    from_ill->ill_phyint->phyint_ifindex) &&
16970 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16971 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16972 		    &ilm->ilm_v6addr))) {
16973 			ASSERT(ilm->ilm_refcnt > 0);
16974 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16975 			/*
16976 			 * For indentation reasons, we are not using a
16977 			 * "else" here.
16978 			 */
16979 			if (count == 0) {
16980 				ilmp = &ilm->ilm_next;
16981 				continue;
16982 			}
16983 			ilm->ilm_refcnt -= count;
16984 			if (new_ilm != NULL) {
16985 				/*
16986 				 * Can find one with the same
16987 				 * ilm_orig_ifindex, if we are failing
16988 				 * over to a STANDBY. This happens
16989 				 * when somebody wants to join a group
16990 				 * on a STANDBY interface and we
16991 				 * internally join on a different one.
16992 				 * If we had joined on from_ill then, a
16993 				 * failover now will find a new ilm
16994 				 * with this index.
16995 				 */
16996 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16997 				    " new ilm on %s, group address %s\n",
16998 				    to_ill->ill_name,
16999 				    inet_ntop(AF_INET6,
17000 				    &ilm->ilm_v6addr, buf,
17001 				    sizeof (buf))));
17002 				new_ilm->ilm_refcnt += count;
17003 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17004 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17005 					new_ilm->ilm_is_new = B_TRUE;
17006 				}
17007 			} else {
17008 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17009 				if (new_ilm == NULL) {
17010 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17011 					    " multicast address %s : from %s to"
17012 					    " %s failed : ENOMEM \n",
17013 					    inet_ntop(AF_INET6,
17014 					    &ilm->ilm_v6addr, buf,
17015 					    sizeof (buf)), from_ill->ill_name,
17016 					    to_ill->ill_name));
17017 					ilmp = &ilm->ilm_next;
17018 					continue;
17019 				}
17020 				*new_ilm = *ilm;
17021 				new_ilm->ilm_filter = NULL;
17022 				new_ilm->ilm_refcnt = count;
17023 				new_ilm->ilm_timer = INFINITY;
17024 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17025 				new_ilm->ilm_is_new = B_TRUE;
17026 				/*
17027 				 * If the to_ill has not joined this
17028 				 * group we need to tell the driver in
17029 				 * ill_send_multicast_reqs.
17030 				 */
17031 				if (ilm_lookup_ill_v6(to_ill,
17032 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17033 					new_ilm->ilm_notify_driver = B_TRUE;
17034 
17035 				new_ilm->ilm_ill = to_ill;
17036 				/* Add to the to_ill's list */
17037 				new_ilm->ilm_next = to_ill->ill_ilm;
17038 				to_ill->ill_ilm = new_ilm;
17039 				ASSERT(new_ilm->ilm_ipif == NULL);
17040 			}
17041 			if (ilm->ilm_refcnt == 0) {
17042 				goto bottom;
17043 			} else {
17044 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17045 				CLEAR_SLIST(new_ilm->ilm_filter);
17046 				ilmp = &ilm->ilm_next;
17047 			}
17048 			continue;
17049 		} else {
17050 			/*
17051 			 * ifindex = 0 means, move everything pointing at
17052 			 * from_ill. We are doing this becuase ill has
17053 			 * either FAILED or became INACTIVE.
17054 			 *
17055 			 * As we would like to move things later back to
17056 			 * from_ill, we want to retain the identity of this
17057 			 * ilm. Thus, we don't blindly increment the reference
17058 			 * count on the ilms matching the address alone. We
17059 			 * need to match on the ilm_orig_index also. new_ilm
17060 			 * was obtained by matching ilm_orig_index also.
17061 			 */
17062 			if (new_ilm != NULL) {
17063 				/*
17064 				 * This is possible only if a previous restore
17065 				 * was incomplete i.e restore to
17066 				 * ilm_orig_ifindex left some ilms because
17067 				 * of some failures. Thus when we are failing
17068 				 * again, we might find our old friends there.
17069 				 */
17070 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17071 				    " on %s, group address %s\n",
17072 				    to_ill->ill_name,
17073 				    inet_ntop(AF_INET6,
17074 				    &ilm->ilm_v6addr, buf,
17075 				    sizeof (buf))));
17076 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17077 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17078 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17079 					new_ilm->ilm_is_new = B_TRUE;
17080 				}
17081 			} else {
17082 				if (from_ill->ill_ilm_walker_cnt != 0) {
17083 					new_ilm = (ilm_t *)
17084 					    mi_zalloc(sizeof (ilm_t));
17085 					if (new_ilm == NULL) {
17086 						ip0dbg(("ilm_move_v6: "
17087 						    "FAILOVER of IPv6"
17088 						    " multicast address %s : "
17089 						    "from %s to"
17090 						    " %s failed : ENOMEM \n",
17091 						    inet_ntop(AF_INET6,
17092 						    &ilm->ilm_v6addr, buf,
17093 						    sizeof (buf)),
17094 						    from_ill->ill_name,
17095 						    to_ill->ill_name));
17096 
17097 							ilmp = &ilm->ilm_next;
17098 							continue;
17099 					}
17100 					*new_ilm = *ilm;
17101 					new_ilm->ilm_filter = NULL;
17102 				} else {
17103 					*ilmp = ilm->ilm_next;
17104 					new_ilm = ilm;
17105 				}
17106 				/*
17107 				 * If the to_ill has not joined this
17108 				 * group we need to tell the driver in
17109 				 * ill_send_multicast_reqs.
17110 				 */
17111 				if (ilm_lookup_ill_v6(to_ill,
17112 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17113 					new_ilm->ilm_notify_driver = B_TRUE;
17114 
17115 				/* Add to the to_ill's list */
17116 				new_ilm->ilm_next = to_ill->ill_ilm;
17117 				to_ill->ill_ilm = new_ilm;
17118 				ASSERT(ilm->ilm_ipif == NULL);
17119 				new_ilm->ilm_ill = to_ill;
17120 				new_ilm->ilm_is_new = B_TRUE;
17121 			}
17122 
17123 		}
17124 
17125 bottom:
17126 		/*
17127 		 * Revert multicast filter state to (EXCLUDE, NULL).
17128 		 * new_ilm->ilm_is_new should already be set if needed.
17129 		 */
17130 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17131 		CLEAR_SLIST(new_ilm->ilm_filter);
17132 		/*
17133 		 * We allocated/got a new ilm, free the old one.
17134 		 */
17135 		if (new_ilm != ilm) {
17136 			if (from_ill->ill_ilm_walker_cnt == 0) {
17137 				*ilmp = ilm->ilm_next;
17138 				ilm->ilm_next = NULL;
17139 				FREE_SLIST(ilm->ilm_filter);
17140 				FREE_SLIST(ilm->ilm_pendsrcs);
17141 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17142 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17143 				mi_free((char *)ilm);
17144 			} else {
17145 				ilm->ilm_flags |= ILM_DELETED;
17146 				from_ill->ill_ilm_cleanup_reqd = 1;
17147 				ilmp = &ilm->ilm_next;
17148 			}
17149 		}
17150 	}
17151 }
17152 
17153 /*
17154  * Move all the multicast memberships to to_ill. Called when
17155  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17156  * different from IPv6 counterpart as multicast memberships are associated
17157  * with ills in IPv6. This function is called after every ipif is moved
17158  * unlike IPv6, where it is moved only once.
17159  */
17160 static void
17161 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17162 {
17163 	ilm_t	*ilm;
17164 	ilm_t	*ilm_next;
17165 	ilm_t	*new_ilm;
17166 	ilm_t	**ilmp;
17167 	ip_stack_t	*ipst = from_ill->ill_ipst;
17168 
17169 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17170 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17171 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17172 
17173 	ilmp = &from_ill->ill_ilm;
17174 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17175 		ilm_next = ilm->ilm_next;
17176 
17177 		if (ilm->ilm_flags & ILM_DELETED) {
17178 			ilmp = &ilm->ilm_next;
17179 			continue;
17180 		}
17181 
17182 		ASSERT(ilm->ilm_ipif != NULL);
17183 
17184 		if (ilm->ilm_ipif != ipif) {
17185 			ilmp = &ilm->ilm_next;
17186 			continue;
17187 		}
17188 
17189 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17190 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17191 			new_ilm = ilm_lookup_ipif(ipif,
17192 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17193 			if (new_ilm != NULL) {
17194 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17195 				/*
17196 				 * We still need to deal with the from_ill.
17197 				 */
17198 				new_ilm->ilm_is_new = B_TRUE;
17199 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17200 				CLEAR_SLIST(new_ilm->ilm_filter);
17201 				goto delete_ilm;
17202 			}
17203 			/*
17204 			 * If we could not find one e.g. ipif is
17205 			 * still down on to_ill, we add this ilm
17206 			 * on ill_new to preserve the reference
17207 			 * count.
17208 			 */
17209 		}
17210 		/*
17211 		 * When ipifs move, ilms always move with it
17212 		 * to the NEW ill. Thus we should never be
17213 		 * able to find ilm till we really move it here.
17214 		 */
17215 		ASSERT(ilm_lookup_ipif(ipif,
17216 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17217 
17218 		if (from_ill->ill_ilm_walker_cnt != 0) {
17219 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17220 			if (new_ilm == NULL) {
17221 				char buf[INET6_ADDRSTRLEN];
17222 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17223 				    " multicast address %s : "
17224 				    "from %s to"
17225 				    " %s failed : ENOMEM \n",
17226 				    inet_ntop(AF_INET,
17227 				    &ilm->ilm_v6addr, buf,
17228 				    sizeof (buf)),
17229 				    from_ill->ill_name,
17230 				    to_ill->ill_name));
17231 
17232 				ilmp = &ilm->ilm_next;
17233 				continue;
17234 			}
17235 			*new_ilm = *ilm;
17236 			/* We don't want new_ilm linked to ilm's filter list */
17237 			new_ilm->ilm_filter = NULL;
17238 		} else {
17239 			/* Remove from the list */
17240 			*ilmp = ilm->ilm_next;
17241 			new_ilm = ilm;
17242 		}
17243 
17244 		/*
17245 		 * If we have never joined this group on the to_ill
17246 		 * make sure we tell the driver.
17247 		 */
17248 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17249 		    ALL_ZONES) == NULL)
17250 			new_ilm->ilm_notify_driver = B_TRUE;
17251 
17252 		/* Add to the to_ill's list */
17253 		new_ilm->ilm_next = to_ill->ill_ilm;
17254 		to_ill->ill_ilm = new_ilm;
17255 		new_ilm->ilm_is_new = B_TRUE;
17256 
17257 		/*
17258 		 * Revert multicast filter state to (EXCLUDE, NULL)
17259 		 */
17260 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17261 		CLEAR_SLIST(new_ilm->ilm_filter);
17262 
17263 		/*
17264 		 * Delete only if we have allocated a new ilm.
17265 		 */
17266 		if (new_ilm != ilm) {
17267 delete_ilm:
17268 			if (from_ill->ill_ilm_walker_cnt == 0) {
17269 				/* Remove from the list */
17270 				*ilmp = ilm->ilm_next;
17271 				ilm->ilm_next = NULL;
17272 				FREE_SLIST(ilm->ilm_filter);
17273 				FREE_SLIST(ilm->ilm_pendsrcs);
17274 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17275 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17276 				mi_free((char *)ilm);
17277 			} else {
17278 				ilm->ilm_flags |= ILM_DELETED;
17279 				from_ill->ill_ilm_cleanup_reqd = 1;
17280 				ilmp = &ilm->ilm_next;
17281 			}
17282 		}
17283 	}
17284 }
17285 
17286 static uint_t
17287 ipif_get_id(ill_t *ill, uint_t id)
17288 {
17289 	uint_t	unit;
17290 	ipif_t	*tipif;
17291 	boolean_t found = B_FALSE;
17292 	ip_stack_t	*ipst = ill->ill_ipst;
17293 
17294 	/*
17295 	 * During failback, we want to go back to the same id
17296 	 * instead of the smallest id so that the original
17297 	 * configuration is maintained. id is non-zero in that
17298 	 * case.
17299 	 */
17300 	if (id != 0) {
17301 		/*
17302 		 * While failing back, if we still have an ipif with
17303 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17304 		 * as soon as we return from this function. It was
17305 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17306 		 * we can choose the smallest id. Thus we return zero
17307 		 * in that case ignoring the hint.
17308 		 */
17309 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17310 			return (0);
17311 		for (tipif = ill->ill_ipif; tipif != NULL;
17312 		    tipif = tipif->ipif_next) {
17313 			if (tipif->ipif_id == id) {
17314 				found = B_TRUE;
17315 				break;
17316 			}
17317 		}
17318 		/*
17319 		 * If somebody already plumbed another logical
17320 		 * with the same id, we won't be able to find it.
17321 		 */
17322 		if (!found)
17323 			return (id);
17324 	}
17325 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17326 		found = B_FALSE;
17327 		for (tipif = ill->ill_ipif; tipif != NULL;
17328 		    tipif = tipif->ipif_next) {
17329 			if (tipif->ipif_id == unit) {
17330 				found = B_TRUE;
17331 				break;
17332 			}
17333 		}
17334 		if (!found)
17335 			break;
17336 	}
17337 	return (unit);
17338 }
17339 
17340 /* ARGSUSED */
17341 static int
17342 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17343     ipif_t **rep_ipif_ptr)
17344 {
17345 	ill_t	*from_ill;
17346 	ipif_t	*rep_ipif;
17347 	uint_t	unit;
17348 	int err = 0;
17349 	ipif_t	*to_ipif;
17350 	struct iocblk	*iocp;
17351 	boolean_t failback_cmd;
17352 	boolean_t remove_ipif;
17353 	int	rc;
17354 	ip_stack_t	*ipst;
17355 
17356 	ASSERT(IAM_WRITER_ILL(to_ill));
17357 	ASSERT(IAM_WRITER_IPIF(ipif));
17358 
17359 	iocp = (struct iocblk *)mp->b_rptr;
17360 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17361 	remove_ipif = B_FALSE;
17362 
17363 	from_ill = ipif->ipif_ill;
17364 	ipst = from_ill->ill_ipst;
17365 
17366 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17367 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17368 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17369 
17370 	/*
17371 	 * Don't move LINK LOCAL addresses as they are tied to
17372 	 * physical interface.
17373 	 */
17374 	if (from_ill->ill_isv6 &&
17375 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17376 		ipif->ipif_was_up = B_FALSE;
17377 		IPIF_UNMARK_MOVING(ipif);
17378 		return (0);
17379 	}
17380 
17381 	/*
17382 	 * We set the ipif_id to maximum so that the search for
17383 	 * ipif_id will pick the lowest number i.e 0 in the
17384 	 * following 2 cases :
17385 	 *
17386 	 * 1) We have a replacement ipif at the head of to_ill.
17387 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17388 	 *    on to_ill and hence the MOVE might fail. We want to
17389 	 *    remove it only if we could move the ipif. Thus, by
17390 	 *    setting it to the MAX value, we make the search in
17391 	 *    ipif_get_id return the zeroth id.
17392 	 *
17393 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17394 	 *    we might just have a zero address plumbed on the ipif
17395 	 *    with zero id in the case of IPv4. We remove that while
17396 	 *    doing the failback. We want to remove it only if we
17397 	 *    could move the ipif. Thus, by setting it to the MAX
17398 	 *    value, we make the search in ipif_get_id return the
17399 	 *    zeroth id.
17400 	 *
17401 	 * Both (1) and (2) are done only when when we are moving
17402 	 * an ipif (either due to failover/failback) which originally
17403 	 * belonged to this interface i.e the ipif_orig_ifindex is
17404 	 * the same as to_ill's ifindex. This is needed so that
17405 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17406 	 * from B -> A (B is being removed from the group) and
17407 	 * FAILBACK from A -> B restores the original configuration.
17408 	 * Without the check for orig_ifindex, the second FAILOVER
17409 	 * could make the ipif belonging to B replace the A's zeroth
17410 	 * ipif and the subsequent failback re-creating the replacement
17411 	 * ipif again.
17412 	 *
17413 	 * NOTE : We created the replacement ipif when we did a
17414 	 * FAILOVER (See below). We could check for FAILBACK and
17415 	 * then look for replacement ipif to be removed. But we don't
17416 	 * want to do that because we wan't to allow the possibility
17417 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17418 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17419 	 * from B -> A.
17420 	 */
17421 	to_ipif = to_ill->ill_ipif;
17422 	if ((to_ill->ill_phyint->phyint_ifindex ==
17423 	    ipif->ipif_orig_ifindex) &&
17424 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17425 		ASSERT(to_ipif->ipif_id == 0);
17426 		remove_ipif = B_TRUE;
17427 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17428 	}
17429 	/*
17430 	 * Find the lowest logical unit number on the to_ill.
17431 	 * If we are failing back, try to get the original id
17432 	 * rather than the lowest one so that the original
17433 	 * configuration is maintained.
17434 	 *
17435 	 * XXX need a better scheme for this.
17436 	 */
17437 	if (failback_cmd) {
17438 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17439 	} else {
17440 		unit = ipif_get_id(to_ill, 0);
17441 	}
17442 
17443 	/* Reset back to zero in case we fail below */
17444 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17445 		to_ipif->ipif_id = 0;
17446 
17447 	if (unit == ipst->ips_ip_addrs_per_if) {
17448 		ipif->ipif_was_up = B_FALSE;
17449 		IPIF_UNMARK_MOVING(ipif);
17450 		return (EINVAL);
17451 	}
17452 
17453 	/*
17454 	 * ipif is ready to move from "from_ill" to "to_ill".
17455 	 *
17456 	 * 1) If we are moving ipif with id zero, create a
17457 	 *    replacement ipif for this ipif on from_ill. If this fails
17458 	 *    fail the MOVE operation.
17459 	 *
17460 	 * 2) Remove the replacement ipif on to_ill if any.
17461 	 *    We could remove the replacement ipif when we are moving
17462 	 *    the ipif with id zero. But what if somebody already
17463 	 *    unplumbed it ? Thus we always remove it if it is present.
17464 	 *    We want to do it only if we are sure we are going to
17465 	 *    move the ipif to to_ill which is why there are no
17466 	 *    returns due to error till ipif is linked to to_ill.
17467 	 *    Note that the first ipif that we failback will always
17468 	 *    be zero if it is present.
17469 	 */
17470 	if (ipif->ipif_id == 0) {
17471 		ipaddr_t inaddr_any = INADDR_ANY;
17472 
17473 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17474 		if (rep_ipif == NULL) {
17475 			ipif->ipif_was_up = B_FALSE;
17476 			IPIF_UNMARK_MOVING(ipif);
17477 			return (ENOMEM);
17478 		}
17479 		*rep_ipif = ipif_zero;
17480 		/*
17481 		 * Before we put the ipif on the list, store the addresses
17482 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17483 		 * assumes so. This logic is not any different from what
17484 		 * ipif_allocate does.
17485 		 */
17486 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17487 		    &rep_ipif->ipif_v6lcl_addr);
17488 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17489 		    &rep_ipif->ipif_v6src_addr);
17490 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17491 		    &rep_ipif->ipif_v6subnet);
17492 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17493 		    &rep_ipif->ipif_v6net_mask);
17494 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17495 		    &rep_ipif->ipif_v6brd_addr);
17496 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17497 		    &rep_ipif->ipif_v6pp_dst_addr);
17498 		/*
17499 		 * We mark IPIF_NOFAILOVER so that this can never
17500 		 * move.
17501 		 */
17502 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17503 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17504 		rep_ipif->ipif_replace_zero = B_TRUE;
17505 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17506 		    MUTEX_DEFAULT, NULL);
17507 		rep_ipif->ipif_id = 0;
17508 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17509 		rep_ipif->ipif_ill = from_ill;
17510 		rep_ipif->ipif_orig_ifindex =
17511 		    from_ill->ill_phyint->phyint_ifindex;
17512 		/* Insert at head */
17513 		rep_ipif->ipif_next = from_ill->ill_ipif;
17514 		from_ill->ill_ipif = rep_ipif;
17515 		/*
17516 		 * We don't really care to let apps know about
17517 		 * this interface.
17518 		 */
17519 	}
17520 
17521 	if (remove_ipif) {
17522 		/*
17523 		 * We set to a max value above for this case to get
17524 		 * id zero. ASSERT that we did get one.
17525 		 */
17526 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17527 		rep_ipif = to_ipif;
17528 		to_ill->ill_ipif = rep_ipif->ipif_next;
17529 		rep_ipif->ipif_next = NULL;
17530 		/*
17531 		 * If some apps scanned and find this interface,
17532 		 * it is time to let them know, so that they can
17533 		 * delete it.
17534 		 */
17535 
17536 		*rep_ipif_ptr = rep_ipif;
17537 	}
17538 
17539 	/* Get it out of the ILL interface list. */
17540 	ipif_remove(ipif, B_FALSE);
17541 
17542 	/* Assign the new ill */
17543 	ipif->ipif_ill = to_ill;
17544 	ipif->ipif_id = unit;
17545 	/* id has already been checked */
17546 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17547 	ASSERT(rc == 0);
17548 	/* Let SCTP update its list */
17549 	sctp_move_ipif(ipif, from_ill, to_ill);
17550 	/*
17551 	 * Handle the failover and failback of ipif_t between
17552 	 * ill_t that have differing maximum mtu values.
17553 	 */
17554 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17555 		if (ipif->ipif_saved_mtu == 0) {
17556 			/*
17557 			 * As this ipif_t is moving to an ill_t
17558 			 * that has a lower ill_max_mtu, its
17559 			 * ipif_mtu needs to be saved so it can
17560 			 * be restored during failback or during
17561 			 * failover to an ill_t which has a
17562 			 * higher ill_max_mtu.
17563 			 */
17564 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17565 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17566 		} else {
17567 			/*
17568 			 * The ipif_t is, once again, moving to
17569 			 * an ill_t that has a lower maximum mtu
17570 			 * value.
17571 			 */
17572 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17573 		}
17574 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17575 	    ipif->ipif_saved_mtu != 0) {
17576 		/*
17577 		 * The mtu of this ipif_t had to be reduced
17578 		 * during an earlier failover; this is an
17579 		 * opportunity for it to be increased (either as
17580 		 * part of another failover or a failback).
17581 		 */
17582 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17583 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17584 			ipif->ipif_saved_mtu = 0;
17585 		} else {
17586 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17587 		}
17588 	}
17589 
17590 	/*
17591 	 * We preserve all the other fields of the ipif including
17592 	 * ipif_saved_ire_mp. The routes that are saved here will
17593 	 * be recreated on the new interface and back on the old
17594 	 * interface when we move back.
17595 	 */
17596 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17597 
17598 	return (err);
17599 }
17600 
17601 static int
17602 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17603     int ifindex, ipif_t **rep_ipif_ptr)
17604 {
17605 	ipif_t *mipif;
17606 	ipif_t *ipif_next;
17607 	int err;
17608 
17609 	/*
17610 	 * We don't really try to MOVE back things if some of the
17611 	 * operations fail. The daemon will take care of moving again
17612 	 * later on.
17613 	 */
17614 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17615 		ipif_next = mipif->ipif_next;
17616 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17617 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17618 
17619 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17620 
17621 			/*
17622 			 * When the MOVE fails, it is the job of the
17623 			 * application to take care of this properly
17624 			 * i.e try again if it is ENOMEM.
17625 			 */
17626 			if (mipif->ipif_ill != from_ill) {
17627 				/*
17628 				 * ipif has moved.
17629 				 *
17630 				 * Move the multicast memberships associated
17631 				 * with this ipif to the new ill. For IPv6, we
17632 				 * do it once after all the ipifs are moved
17633 				 * (in ill_move) as they are not associated
17634 				 * with ipifs.
17635 				 *
17636 				 * We need to move the ilms as the ipif has
17637 				 * already been moved to a new ill even
17638 				 * in the case of errors. Neither
17639 				 * ilm_free(ipif) will find the ilm
17640 				 * when somebody unplumbs this ipif nor
17641 				 * ilm_delete(ilm) will be able to find the
17642 				 * ilm, if we don't move now.
17643 				 */
17644 				if (!from_ill->ill_isv6)
17645 					ilm_move_v4(from_ill, to_ill, mipif);
17646 			}
17647 
17648 			if (err != 0)
17649 				return (err);
17650 		}
17651 	}
17652 	return (0);
17653 }
17654 
17655 static int
17656 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17657 {
17658 	int ifindex;
17659 	int err;
17660 	struct iocblk	*iocp;
17661 	ipif_t	*ipif;
17662 	ipif_t *rep_ipif_ptr = NULL;
17663 	ipif_t	*from_ipif = NULL;
17664 	boolean_t check_rep_if = B_FALSE;
17665 	ip_stack_t	*ipst = from_ill->ill_ipst;
17666 
17667 	iocp = (struct iocblk *)mp->b_rptr;
17668 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17669 		/*
17670 		 * Move everything pointing at from_ill to to_ill.
17671 		 * We acheive this by passing in 0 as ifindex.
17672 		 */
17673 		ifindex = 0;
17674 	} else {
17675 		/*
17676 		 * Move everything pointing at from_ill whose original
17677 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17678 		 * We acheive this by passing in ifindex rather than 0.
17679 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17680 		 */
17681 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17682 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17683 	}
17684 
17685 	/*
17686 	 * Determine if there is at least one ipif that would move from
17687 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17688 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17689 	 * the move, in which case we need to quiesce the replacement ipif also.
17690 	 */
17691 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17692 	    from_ipif = from_ipif->ipif_next) {
17693 		if (((ifindex == 0) ||
17694 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17695 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17696 			check_rep_if = B_TRUE;
17697 			break;
17698 		}
17699 	}
17700 
17701 
17702 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17703 
17704 	GRAB_ILL_LOCKS(from_ill, to_ill);
17705 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17706 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17707 		    mp, ILL_MOVE_OK);
17708 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17709 		return (EINPROGRESS);
17710 	}
17711 
17712 	/* Check if the replacement ipif is quiescent to delete */
17713 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17714 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17715 		to_ill->ill_ipif->ipif_state_flags |=
17716 		    IPIF_MOVING | IPIF_CHANGING;
17717 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17718 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17719 			    mp, ILL_MOVE_OK);
17720 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17721 			return (EINPROGRESS);
17722 		}
17723 	}
17724 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17725 
17726 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17727 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17728 	GRAB_ILL_LOCKS(from_ill, to_ill);
17729 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17730 
17731 	/* ilm_move is done inside ipif_move for IPv4 */
17732 	if (err == 0 && from_ill->ill_isv6)
17733 		ilm_move_v6(from_ill, to_ill, ifindex);
17734 
17735 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17736 	rw_exit(&ipst->ips_ill_g_lock);
17737 
17738 	/*
17739 	 * send rts messages and multicast messages.
17740 	 */
17741 	if (rep_ipif_ptr != NULL) {
17742 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17743 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17744 			rep_ipif_ptr->ipif_recovery_id = 0;
17745 		}
17746 		ip_rts_ifmsg(rep_ipif_ptr);
17747 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17748 #ifdef DEBUG
17749 		ipif_trace_cleanup(rep_ipif_ptr);
17750 #endif
17751 		mi_free(rep_ipif_ptr);
17752 	}
17753 
17754 	conn_move_ill(from_ill, to_ill, ifindex);
17755 
17756 	return (err);
17757 }
17758 
17759 /*
17760  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17761  * Also checks for the validity of the arguments.
17762  * Note: We are already exclusive inside the from group.
17763  * It is upto the caller to release refcnt on the to_ill's.
17764  */
17765 static int
17766 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17767     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17768 {
17769 	int dst_index;
17770 	ipif_t *ipif_v4, *ipif_v6;
17771 	struct lifreq *lifr;
17772 	mblk_t *mp1;
17773 	boolean_t exists;
17774 	sin_t	*sin;
17775 	int	err = 0;
17776 	ip_stack_t	*ipst;
17777 
17778 	if (CONN_Q(q))
17779 		ipst = CONNQ_TO_IPST(q);
17780 	else
17781 		ipst = ILLQ_TO_IPST(q);
17782 
17783 
17784 	if ((mp1 = mp->b_cont) == NULL)
17785 		return (EPROTO);
17786 
17787 	if ((mp1 = mp1->b_cont) == NULL)
17788 		return (EPROTO);
17789 
17790 	lifr = (struct lifreq *)mp1->b_rptr;
17791 	sin = (sin_t *)&lifr->lifr_addr;
17792 
17793 	/*
17794 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17795 	 * specific operations.
17796 	 */
17797 	if (sin->sin_family != AF_UNSPEC)
17798 		return (EINVAL);
17799 
17800 	/*
17801 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17802 	 * NULLs for the last 4 args and we know the lookup won't fail
17803 	 * with EINPROGRESS.
17804 	 */
17805 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17806 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17807 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17808 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17809 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17810 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17811 
17812 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17813 		return (ENXIO);
17814 
17815 	if (ipif_v4 != NULL) {
17816 		ASSERT(ipif_v4->ipif_refcnt != 0);
17817 		if (ipif_v4->ipif_id != 0) {
17818 			err = EINVAL;
17819 			goto done;
17820 		}
17821 
17822 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17823 		*ill_from_v4 = ipif_v4->ipif_ill;
17824 	}
17825 
17826 	if (ipif_v6 != NULL) {
17827 		ASSERT(ipif_v6->ipif_refcnt != 0);
17828 		if (ipif_v6->ipif_id != 0) {
17829 			err = EINVAL;
17830 			goto done;
17831 		}
17832 
17833 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17834 		*ill_from_v6 = ipif_v6->ipif_ill;
17835 	}
17836 
17837 	err = 0;
17838 	dst_index = lifr->lifr_movetoindex;
17839 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17840 	    q, mp, ip_process_ioctl, &err, ipst);
17841 	if (err != 0) {
17842 		/*
17843 		 * There could be only v6.
17844 		 */
17845 		if (err != ENXIO)
17846 			goto done;
17847 		err = 0;
17848 	}
17849 
17850 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17851 	    q, mp, ip_process_ioctl, &err, ipst);
17852 	if (err != 0) {
17853 		if (err != ENXIO)
17854 			goto done;
17855 		if (*ill_to_v4 == NULL) {
17856 			err = ENXIO;
17857 			goto done;
17858 		}
17859 		err = 0;
17860 	}
17861 
17862 	/*
17863 	 * If we have something to MOVE i.e "from" not NULL,
17864 	 * "to" should be non-NULL.
17865 	 */
17866 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17867 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17868 		err = EINVAL;
17869 	}
17870 
17871 done:
17872 	if (ipif_v4 != NULL)
17873 		ipif_refrele(ipif_v4);
17874 	if (ipif_v6 != NULL)
17875 		ipif_refrele(ipif_v6);
17876 	return (err);
17877 }
17878 
17879 /*
17880  * FAILOVER and FAILBACK are modelled as MOVE operations.
17881  *
17882  * We don't check whether the MOVE is within the same group or
17883  * not, because this ioctl can be used as a generic mechanism
17884  * to failover from interface A to B, though things will function
17885  * only if they are really part of the same group. Moreover,
17886  * all ipifs may be down and hence temporarily out of the group.
17887  *
17888  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17889  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17890  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17891  * have been deleted and there are no active references. Once quiescent the
17892  * ipif's are moved and brought up on the new ill.
17893  *
17894  * Normally the source ill and destination ill belong to the same IPMP group
17895  * and hence the same ipsq_t. In the event they don't belong to the same
17896  * same group the two ipsq's are first merged into one ipsq - that of the
17897  * to_ill. The multicast memberships on the source and destination ill cannot
17898  * change during the move operation since multicast joins/leaves also have to
17899  * execute on the same ipsq and are hence serialized.
17900  */
17901 /* ARGSUSED */
17902 int
17903 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17904     ip_ioctl_cmd_t *ipip, void *ifreq)
17905 {
17906 	ill_t *ill_to_v4 = NULL;
17907 	ill_t *ill_to_v6 = NULL;
17908 	ill_t *ill_from_v4 = NULL;
17909 	ill_t *ill_from_v6 = NULL;
17910 	int err = 0;
17911 
17912 	/*
17913 	 * setup from and to ill's, we can get EINPROGRESS only for
17914 	 * to_ill's.
17915 	 */
17916 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17917 	    &ill_to_v4, &ill_to_v6);
17918 
17919 	if (err != 0) {
17920 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17921 		goto done;
17922 	}
17923 
17924 	/*
17925 	 * nothing to do.
17926 	 */
17927 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17928 		goto done;
17929 	}
17930 
17931 	/*
17932 	 * nothing to do.
17933 	 */
17934 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17935 		goto done;
17936 	}
17937 
17938 	/*
17939 	 * Mark the ill as changing.
17940 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17941 	 * in ill_up_ipifs in case of error they are cleared below.
17942 	 */
17943 
17944 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17945 	if (ill_from_v4 != NULL)
17946 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17947 	if (ill_from_v6 != NULL)
17948 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17949 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17950 
17951 	/*
17952 	 * Make sure that both src and dst are
17953 	 * in the same syncq group. If not make it happen.
17954 	 * We are not holding any locks because we are the writer
17955 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17956 	 * to protect to_ipsq against changing.
17957 	 */
17958 	if (ill_from_v4 != NULL) {
17959 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17960 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17961 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17962 			    NULL, mp, q);
17963 			goto err_ret;
17964 
17965 		}
17966 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17967 	} else {
17968 
17969 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17970 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17971 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17972 			    NULL, mp, q);
17973 			goto err_ret;
17974 
17975 		}
17976 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17977 	}
17978 
17979 	/*
17980 	 * Now that the ipsq's have been merged and we are the writer
17981 	 * lets mark to_ill as changing as well.
17982 	 */
17983 
17984 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17985 	if (ill_to_v4 != NULL)
17986 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17987 	if (ill_to_v6 != NULL)
17988 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17989 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17990 
17991 	/*
17992 	 * Its ok for us to proceed with the move even if
17993 	 * ill_pending_mp is non null on one of the from ill's as the reply
17994 	 * should not be looking at the ipif, it should only care about the
17995 	 * ill itself.
17996 	 */
17997 
17998 	/*
17999 	 * lets move ipv4 first.
18000 	 */
18001 	if (ill_from_v4 != NULL) {
18002 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18003 		ill_from_v4->ill_move_in_progress = B_TRUE;
18004 		ill_to_v4->ill_move_in_progress = B_TRUE;
18005 		ill_to_v4->ill_move_peer = ill_from_v4;
18006 		ill_from_v4->ill_move_peer = ill_to_v4;
18007 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18008 	}
18009 
18010 	/*
18011 	 * Now lets move ipv6.
18012 	 */
18013 	if (err == 0 && ill_from_v6 != NULL) {
18014 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18015 		ill_from_v6->ill_move_in_progress = B_TRUE;
18016 		ill_to_v6->ill_move_in_progress = B_TRUE;
18017 		ill_to_v6->ill_move_peer = ill_from_v6;
18018 		ill_from_v6->ill_move_peer = ill_to_v6;
18019 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18020 	}
18021 
18022 err_ret:
18023 	/*
18024 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18025 	 * moved to become quiescent.
18026 	 */
18027 	if (err == EINPROGRESS) {
18028 		goto done;
18029 	}
18030 
18031 	/*
18032 	 * if err is set ill_up_ipifs will not be called
18033 	 * lets clear the flags.
18034 	 */
18035 
18036 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18037 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18038 	/*
18039 	 * Some of the clearing may be redundant. But it is simple
18040 	 * not making any extra checks.
18041 	 */
18042 	if (ill_from_v6 != NULL) {
18043 		ill_from_v6->ill_move_in_progress = B_FALSE;
18044 		ill_from_v6->ill_move_peer = NULL;
18045 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18046 	}
18047 	if (ill_from_v4 != NULL) {
18048 		ill_from_v4->ill_move_in_progress = B_FALSE;
18049 		ill_from_v4->ill_move_peer = NULL;
18050 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18051 	}
18052 	if (ill_to_v6 != NULL) {
18053 		ill_to_v6->ill_move_in_progress = B_FALSE;
18054 		ill_to_v6->ill_move_peer = NULL;
18055 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18056 	}
18057 	if (ill_to_v4 != NULL) {
18058 		ill_to_v4->ill_move_in_progress = B_FALSE;
18059 		ill_to_v4->ill_move_peer = NULL;
18060 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18061 	}
18062 
18063 	/*
18064 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18065 	 * Do this always to maintain proper state i.e even in case of errors.
18066 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18067 	 * we need not call on both v4 and v6 interfaces.
18068 	 */
18069 	if (ill_from_v4 != NULL) {
18070 		if ((ill_from_v4->ill_phyint->phyint_flags &
18071 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18072 			phyint_inactive(ill_from_v4->ill_phyint);
18073 		}
18074 	} else if (ill_from_v6 != NULL) {
18075 		if ((ill_from_v6->ill_phyint->phyint_flags &
18076 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18077 			phyint_inactive(ill_from_v6->ill_phyint);
18078 		}
18079 	}
18080 
18081 	if (ill_to_v4 != NULL) {
18082 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18083 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18084 		}
18085 	} else if (ill_to_v6 != NULL) {
18086 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18087 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18088 		}
18089 	}
18090 
18091 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18092 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18093 
18094 no_err:
18095 	/*
18096 	 * lets bring the interfaces up on the to_ill.
18097 	 */
18098 	if (err == 0) {
18099 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18100 		    q, mp);
18101 	}
18102 
18103 	if (err == 0) {
18104 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18105 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18106 
18107 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18108 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18109 	}
18110 done:
18111 
18112 	if (ill_to_v4 != NULL) {
18113 		ill_refrele(ill_to_v4);
18114 	}
18115 	if (ill_to_v6 != NULL) {
18116 		ill_refrele(ill_to_v6);
18117 	}
18118 
18119 	return (err);
18120 }
18121 
18122 static void
18123 ill_dl_down(ill_t *ill)
18124 {
18125 	/*
18126 	 * The ill is down; unbind but stay attached since we're still
18127 	 * associated with a PPA. If we have negotiated DLPI capabilites
18128 	 * with the data link service provider (IDS_OK) then reset them.
18129 	 * The interval between unbinding and rebinding is potentially
18130 	 * unbounded hence we cannot assume things will be the same.
18131 	 * The DLPI capabilities will be probed again when the data link
18132 	 * is brought up.
18133 	 */
18134 	mblk_t	*mp = ill->ill_unbind_mp;
18135 	hook_nic_event_t *info;
18136 
18137 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18138 
18139 	ill->ill_unbind_mp = NULL;
18140 	if (mp != NULL) {
18141 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18142 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18143 		    ill->ill_name));
18144 		mutex_enter(&ill->ill_lock);
18145 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18146 		mutex_exit(&ill->ill_lock);
18147 		/*
18148 		 * Reset the capabilities if the negotiation is done or is
18149 		 * still in progress. Note that ill_capability_reset() will
18150 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18151 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18152 		 *
18153 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18154 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18155 		 * the capabilities renegotiation from happening.
18156 		 */
18157 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18158 			ill_capability_reset(ill);
18159 		ill->ill_capab_reneg = B_FALSE;
18160 
18161 		ill_dlpi_send(ill, mp);
18162 	}
18163 
18164 	/*
18165 	 * Toss all of our multicast memberships.  We could keep them, but
18166 	 * then we'd have to do bookkeeping of any joins and leaves performed
18167 	 * by the application while the the interface is down (we can't just
18168 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18169 	 * on a downed interface).
18170 	 */
18171 	ill_leave_multicast(ill);
18172 
18173 	mutex_enter(&ill->ill_lock);
18174 
18175 	ill->ill_dl_up = 0;
18176 
18177 	if ((info = ill->ill_nic_event_info) != NULL) {
18178 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18179 		    info->hne_event, ill->ill_name));
18180 		if (info->hne_data != NULL)
18181 			kmem_free(info->hne_data, info->hne_datalen);
18182 		kmem_free(info, sizeof (hook_nic_event_t));
18183 	}
18184 
18185 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18186 	if (info != NULL) {
18187 		ip_stack_t	*ipst = ill->ill_ipst;
18188 
18189 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18190 		info->hne_lif = 0;
18191 		info->hne_event = NE_DOWN;
18192 		info->hne_data = NULL;
18193 		info->hne_datalen = 0;
18194 		info->hne_family = ill->ill_isv6 ?
18195 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18196 	} else
18197 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18198 		    "information for %s (ENOMEM)\n", ill->ill_name));
18199 
18200 	ill->ill_nic_event_info = info;
18201 
18202 	mutex_exit(&ill->ill_lock);
18203 }
18204 
18205 static void
18206 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18207 {
18208 	union DL_primitives *dlp;
18209 	t_uscalar_t prim;
18210 
18211 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18212 
18213 	dlp = (union DL_primitives *)mp->b_rptr;
18214 	prim = dlp->dl_primitive;
18215 
18216 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18217 	    dlpi_prim_str(prim), prim, ill->ill_name));
18218 
18219 	switch (prim) {
18220 	case DL_PHYS_ADDR_REQ:
18221 	{
18222 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18223 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18224 		break;
18225 	}
18226 	case DL_BIND_REQ:
18227 		mutex_enter(&ill->ill_lock);
18228 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18229 		mutex_exit(&ill->ill_lock);
18230 		break;
18231 	}
18232 
18233 	/*
18234 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18235 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18236 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18237 	 */
18238 	mutex_enter(&ill->ill_lock);
18239 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18240 	    (prim == DL_UNBIND_REQ)) {
18241 		ill->ill_dlpi_pending = prim;
18242 	}
18243 	mutex_exit(&ill->ill_lock);
18244 
18245 	putnext(ill->ill_wq, mp);
18246 }
18247 
18248 /*
18249  * Helper function for ill_dlpi_send().
18250  */
18251 /* ARGSUSED */
18252 static void
18253 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18254 {
18255 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18256 }
18257 
18258 /*
18259  * Send a DLPI control message to the driver but make sure there
18260  * is only one outstanding message. Uses ill_dlpi_pending to tell
18261  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18262  * when an ACK or a NAK is received to process the next queued message.
18263  */
18264 void
18265 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18266 {
18267 	mblk_t **mpp;
18268 
18269 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18270 
18271 	/*
18272 	 * To ensure that any DLPI requests for current exclusive operation
18273 	 * are always completely sent before any DLPI messages for other
18274 	 * operations, require writer access before enqueuing.
18275 	 */
18276 	if (!IAM_WRITER_ILL(ill)) {
18277 		ill_refhold(ill);
18278 		/* qwriter_ip() does the ill_refrele() */
18279 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18280 		    NEW_OP, B_TRUE);
18281 		return;
18282 	}
18283 
18284 	mutex_enter(&ill->ill_lock);
18285 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18286 		/* Must queue message. Tail insertion */
18287 		mpp = &ill->ill_dlpi_deferred;
18288 		while (*mpp != NULL)
18289 			mpp = &((*mpp)->b_next);
18290 
18291 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18292 		    ill->ill_name));
18293 
18294 		*mpp = mp;
18295 		mutex_exit(&ill->ill_lock);
18296 		return;
18297 	}
18298 	mutex_exit(&ill->ill_lock);
18299 	ill_dlpi_dispatch(ill, mp);
18300 }
18301 
18302 /*
18303  * Send all deferred DLPI messages without waiting for their ACKs.
18304  */
18305 void
18306 ill_dlpi_send_deferred(ill_t *ill)
18307 {
18308 	mblk_t *mp, *nextmp;
18309 
18310 	/*
18311 	 * Clear ill_dlpi_pending so that the message is not queued in
18312 	 * ill_dlpi_send().
18313 	 */
18314 	mutex_enter(&ill->ill_lock);
18315 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18316 	mp = ill->ill_dlpi_deferred;
18317 	ill->ill_dlpi_deferred = NULL;
18318 	mutex_exit(&ill->ill_lock);
18319 
18320 	for (; mp != NULL; mp = nextmp) {
18321 		nextmp = mp->b_next;
18322 		mp->b_next = NULL;
18323 		ill_dlpi_send(ill, mp);
18324 	}
18325 }
18326 
18327 /*
18328  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18329  */
18330 boolean_t
18331 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18332 {
18333 	t_uscalar_t pending;
18334 
18335 	mutex_enter(&ill->ill_lock);
18336 	if (ill->ill_dlpi_pending == prim) {
18337 		mutex_exit(&ill->ill_lock);
18338 		return (B_TRUE);
18339 	}
18340 
18341 	/*
18342 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18343 	 * without waiting, so don't print any warnings in that case.
18344 	 */
18345 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18346 		mutex_exit(&ill->ill_lock);
18347 		return (B_FALSE);
18348 	}
18349 	pending = ill->ill_dlpi_pending;
18350 	mutex_exit(&ill->ill_lock);
18351 
18352 	if (pending == DL_PRIM_INVAL) {
18353 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18354 		    "received unsolicited ack for %s on %s\n",
18355 		    dlpi_prim_str(prim), ill->ill_name);
18356 	} else {
18357 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18358 		    "received unexpected ack for %s on %s (expecting %s)\n",
18359 		    dlpi_prim_str(prim), ill->ill_name, dlpi_prim_str(pending));
18360 	}
18361 	return (B_FALSE);
18362 }
18363 
18364 /*
18365  * Called when an DLPI control message has been acked or nacked to
18366  * send down the next queued message (if any).
18367  */
18368 void
18369 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18370 {
18371 	mblk_t *mp;
18372 
18373 	ASSERT(IAM_WRITER_ILL(ill));
18374 	mutex_enter(&ill->ill_lock);
18375 
18376 	ASSERT(prim != DL_PRIM_INVAL);
18377 	ASSERT(ill->ill_dlpi_pending == prim);
18378 
18379 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18380 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18381 
18382 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18383 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18384 		cv_signal(&ill->ill_cv);
18385 		mutex_exit(&ill->ill_lock);
18386 		return;
18387 	}
18388 
18389 	ill->ill_dlpi_deferred = mp->b_next;
18390 	mp->b_next = NULL;
18391 	mutex_exit(&ill->ill_lock);
18392 
18393 	ill_dlpi_dispatch(ill, mp);
18394 }
18395 
18396 void
18397 conn_delete_ire(conn_t *connp, caddr_t arg)
18398 {
18399 	ipif_t	*ipif = (ipif_t *)arg;
18400 	ire_t	*ire;
18401 
18402 	/*
18403 	 * Look at the cached ires on conns which has pointers to ipifs.
18404 	 * We just call ire_refrele which clears up the reference
18405 	 * to ire. Called when a conn closes. Also called from ipif_free
18406 	 * to cleanup indirect references to the stale ipif via the cached ire.
18407 	 */
18408 	mutex_enter(&connp->conn_lock);
18409 	ire = connp->conn_ire_cache;
18410 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18411 		connp->conn_ire_cache = NULL;
18412 		mutex_exit(&connp->conn_lock);
18413 		IRE_REFRELE_NOTR(ire);
18414 		return;
18415 	}
18416 	mutex_exit(&connp->conn_lock);
18417 
18418 }
18419 
18420 /*
18421  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18422  * of IREs. Those IREs may have been previously cached in the conn structure.
18423  * This ipcl_walk() walker function releases all references to such IREs based
18424  * on the condemned flag.
18425  */
18426 /* ARGSUSED */
18427 void
18428 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18429 {
18430 	ire_t	*ire;
18431 
18432 	mutex_enter(&connp->conn_lock);
18433 	ire = connp->conn_ire_cache;
18434 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18435 		connp->conn_ire_cache = NULL;
18436 		mutex_exit(&connp->conn_lock);
18437 		IRE_REFRELE_NOTR(ire);
18438 		return;
18439 	}
18440 	mutex_exit(&connp->conn_lock);
18441 }
18442 
18443 /*
18444  * Take down a specific interface, but don't lose any information about it.
18445  * Also delete interface from its interface group (ifgrp).
18446  * (Always called as writer.)
18447  * This function goes through the down sequence even if the interface is
18448  * already down. There are 2 reasons.
18449  * a. Currently we permit interface routes that depend on down interfaces
18450  *    to be added. This behaviour itself is questionable. However it appears
18451  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18452  *    time. We go thru the cleanup in order to remove these routes.
18453  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18454  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18455  *    down, but we need to cleanup i.e. do ill_dl_down and
18456  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18457  *
18458  * IP-MT notes:
18459  *
18460  * Model of reference to interfaces.
18461  *
18462  * The following members in ipif_t track references to the ipif.
18463  *	int     ipif_refcnt;    Active reference count
18464  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18465  * The following members in ill_t track references to the ill.
18466  *	int             ill_refcnt;     active refcnt
18467  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18468  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18469  *
18470  * Reference to an ipif or ill can be obtained in any of the following ways.
18471  *
18472  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18473  * Pointers to ipif / ill from other data structures viz ire and conn.
18474  * Implicit reference to the ipif / ill by holding a reference to the ire.
18475  *
18476  * The ipif/ill lookup functions return a reference held ipif / ill.
18477  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18478  * This is a purely dynamic reference count associated with threads holding
18479  * references to the ipif / ill. Pointers from other structures do not
18480  * count towards this reference count.
18481  *
18482  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18483  * ipif/ill. This is incremented whenever a new ire is created referencing the
18484  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18485  * actually added to the ire hash table. The count is decremented in
18486  * ire_inactive where the ire is destroyed.
18487  *
18488  * nce's reference ill's thru nce_ill and the count of nce's associated with
18489  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18490  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18491  * table. Similarly it is decremented in ndp_inactive() where the nce
18492  * is destroyed.
18493  *
18494  * Flow of ioctls involving interface down/up
18495  *
18496  * The following is the sequence of an attempt to set some critical flags on an
18497  * up interface.
18498  * ip_sioctl_flags
18499  * ipif_down
18500  * wait for ipif to be quiescent
18501  * ipif_down_tail
18502  * ip_sioctl_flags_tail
18503  *
18504  * All set ioctls that involve down/up sequence would have a skeleton similar
18505  * to the above. All the *tail functions are called after the refcounts have
18506  * dropped to the appropriate values.
18507  *
18508  * The mechanism to quiesce an ipif is as follows.
18509  *
18510  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18511  * on the ipif. Callers either pass a flag requesting wait or the lookup
18512  *  functions will return NULL.
18513  *
18514  * Delete all ires referencing this ipif
18515  *
18516  * Any thread attempting to do an ipif_refhold on an ipif that has been
18517  * obtained thru a cached pointer will first make sure that
18518  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18519  * increment the refcount.
18520  *
18521  * The above guarantees that the ipif refcount will eventually come down to
18522  * zero and the ipif will quiesce, once all threads that currently hold a
18523  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18524  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18525  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18526  * drop to zero.
18527  *
18528  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18529  *
18530  * Threads trying to lookup an ipif or ill can pass a flag requesting
18531  * wait and restart if the ipif / ill cannot be looked up currently.
18532  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18533  * failure if the ipif is currently undergoing an exclusive operation, and
18534  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18535  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18536  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18537  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18538  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18539  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18540  * until we release the ipsq_lock, even though the the ill/ipif state flags
18541  * can change after we drop the ill_lock.
18542  *
18543  * An attempt to send out a packet using an ipif that is currently
18544  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18545  * operation and restart it later when the exclusive condition on the ipif ends.
18546  * This is an example of not passing the wait flag to the lookup functions. For
18547  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18548  * out a multicast packet on that ipif will fail while the ipif is
18549  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18550  * currently IPIF_CHANGING will also fail.
18551  */
18552 int
18553 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18554 {
18555 	ill_t		*ill = ipif->ipif_ill;
18556 	phyint_t	*phyi;
18557 	conn_t		*connp;
18558 	boolean_t	success;
18559 	boolean_t	ipif_was_up = B_FALSE;
18560 	ip_stack_t	*ipst = ill->ill_ipst;
18561 
18562 	ASSERT(IAM_WRITER_IPIF(ipif));
18563 
18564 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18565 
18566 	if (ipif->ipif_flags & IPIF_UP) {
18567 		mutex_enter(&ill->ill_lock);
18568 		ipif->ipif_flags &= ~IPIF_UP;
18569 		ASSERT(ill->ill_ipif_up_count > 0);
18570 		--ill->ill_ipif_up_count;
18571 		mutex_exit(&ill->ill_lock);
18572 		ipif_was_up = B_TRUE;
18573 		/* Update status in SCTP's list */
18574 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18575 	}
18576 
18577 	/*
18578 	 * Blow away memberships we established in ipif_multicast_up().
18579 	 */
18580 	ipif_multicast_down(ipif);
18581 
18582 	/*
18583 	 * Remove from the mapping for __sin6_src_id. We insert only
18584 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18585 	 * stored as mapped addresses, we need to check for mapped
18586 	 * INADDR_ANY also.
18587 	 */
18588 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18589 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18590 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18591 		int err;
18592 
18593 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18594 		    ipif->ipif_zoneid, ipst);
18595 		if (err != 0) {
18596 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18597 		}
18598 	}
18599 
18600 	/*
18601 	 * Before we delete the ill from the group (if any), we need
18602 	 * to make sure that we delete all the routes dependent on
18603 	 * this and also any ipifs dependent on this ipif for
18604 	 * source address. We need to do before we delete from
18605 	 * the group because
18606 	 *
18607 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18608 	 *
18609 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18610 	 *    for re-doing source address selection. Note that
18611 	 *    ipif_select_source[_v6] called from
18612 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18613 	 *    because we have already marked down here i.e cleared
18614 	 *    IPIF_UP.
18615 	 */
18616 	if (ipif->ipif_isv6) {
18617 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18618 		    ipst);
18619 	} else {
18620 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18621 		    ipst);
18622 	}
18623 
18624 	/*
18625 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18626 	 * ires have been deleted above. Otherwise a thread could end up
18627 	 * caching an ire in a conn after we have finished the cleanup of the
18628 	 * conn. The caching is done after making sure that the ire is not yet
18629 	 * condemned. Also documented in the block comment above ip_output
18630 	 */
18631 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18632 	/* Also, delete the ires cached in SCTP */
18633 	sctp_ire_cache_flush(ipif);
18634 
18635 	/*
18636 	 * Update any other ipifs which have used "our" local address as
18637 	 * a source address. This entails removing and recreating IRE_INTERFACE
18638 	 * entries for such ipifs.
18639 	 */
18640 	if (ipif->ipif_isv6)
18641 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18642 	else
18643 		ipif_update_other_ipifs(ipif, ill->ill_group);
18644 
18645 	if (ipif_was_up) {
18646 		/*
18647 		 * Check whether it is last ipif to leave this group.
18648 		 * If this is the last ipif to leave, we should remove
18649 		 * this ill from the group as ipif_select_source will not
18650 		 * be able to find any useful ipifs if this ill is selected
18651 		 * for load balancing.
18652 		 *
18653 		 * For nameless groups, we should call ifgrp_delete if this
18654 		 * belongs to some group. As this ipif is going down, we may
18655 		 * need to reconstruct groups.
18656 		 */
18657 		phyi = ill->ill_phyint;
18658 		/*
18659 		 * If the phyint_groupname_len is 0, it may or may not
18660 		 * be in the nameless group. If the phyint_groupname_len is
18661 		 * not 0, then this ill should be part of some group.
18662 		 * As we always insert this ill in the group if
18663 		 * phyint_groupname_len is not zero when the first ipif
18664 		 * comes up (in ipif_up_done), it should be in a group
18665 		 * when the namelen is not 0.
18666 		 *
18667 		 * NOTE : When we delete the ill from the group,it will
18668 		 * blow away all the IRE_CACHES pointing either at this ipif or
18669 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18670 		 * should be pointing at this ill.
18671 		 */
18672 		ASSERT(phyi->phyint_groupname_len == 0 ||
18673 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18674 
18675 		if (phyi->phyint_groupname_len != 0) {
18676 			if (ill->ill_ipif_up_count == 0)
18677 				illgrp_delete(ill);
18678 		}
18679 
18680 		/*
18681 		 * If we have deleted some of the broadcast ires associated
18682 		 * with this ipif, we need to re-nominate somebody else if
18683 		 * the ires that we deleted were the nominated ones.
18684 		 */
18685 		if (ill->ill_group != NULL && !ill->ill_isv6)
18686 			ipif_renominate_bcast(ipif);
18687 	}
18688 
18689 	/*
18690 	 * neighbor-discovery or arp entries for this interface.
18691 	 */
18692 	ipif_ndp_down(ipif);
18693 
18694 	/*
18695 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18696 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18697 	 * and ill_delete -> ipif_free -> ipif_down
18698 	 */
18699 	if (mp == NULL) {
18700 		ASSERT(q == NULL);
18701 		return (0);
18702 	}
18703 
18704 	if (CONN_Q(q)) {
18705 		connp = Q_TO_CONN(q);
18706 		mutex_enter(&connp->conn_lock);
18707 	} else {
18708 		connp = NULL;
18709 	}
18710 	mutex_enter(&ill->ill_lock);
18711 	/*
18712 	 * Are there any ire's pointing to this ipif that are still active ?
18713 	 * If this is the last ipif going down, are there any ire's pointing
18714 	 * to this ill that are still active ?
18715 	 */
18716 	if (ipif_is_quiescent(ipif)) {
18717 		mutex_exit(&ill->ill_lock);
18718 		if (connp != NULL)
18719 			mutex_exit(&connp->conn_lock);
18720 		return (0);
18721 	}
18722 
18723 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18724 	    ill->ill_name, (void *)ill));
18725 	/*
18726 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18727 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18728 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18729 	 */
18730 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18731 	if (!success) {
18732 		/* The conn is closing. So just return */
18733 		ASSERT(connp != NULL);
18734 		mutex_exit(&ill->ill_lock);
18735 		mutex_exit(&connp->conn_lock);
18736 		return (EINTR);
18737 	}
18738 
18739 	mutex_exit(&ill->ill_lock);
18740 	if (connp != NULL)
18741 		mutex_exit(&connp->conn_lock);
18742 	return (EINPROGRESS);
18743 }
18744 
18745 void
18746 ipif_down_tail(ipif_t *ipif)
18747 {
18748 	ill_t	*ill = ipif->ipif_ill;
18749 
18750 	/*
18751 	 * Skip any loopback interface (null wq).
18752 	 * If this is the last logical interface on the ill
18753 	 * have ill_dl_down tell the driver we are gone (unbind)
18754 	 * Note that lun 0 can ipif_down even though
18755 	 * there are other logical units that are up.
18756 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18757 	 */
18758 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18759 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18760 	    ill->ill_dl_up) {
18761 		ill_dl_down(ill);
18762 	}
18763 	ill->ill_logical_down = 0;
18764 
18765 	/*
18766 	 * Have to be after removing the routes in ipif_down_delete_ire.
18767 	 */
18768 	if (ipif->ipif_isv6) {
18769 		if (ill->ill_flags & ILLF_XRESOLV)
18770 			ipif_arp_down(ipif);
18771 	} else {
18772 		ipif_arp_down(ipif);
18773 	}
18774 
18775 	ip_rts_ifmsg(ipif);
18776 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18777 }
18778 
18779 /*
18780  * Bring interface logically down without bringing the physical interface
18781  * down e.g. when the netmask is changed. This avoids long lasting link
18782  * negotiations between an ethernet interface and a certain switches.
18783  */
18784 static int
18785 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18786 {
18787 	/*
18788 	 * The ill_logical_down flag is a transient flag. It is set here
18789 	 * and is cleared once the down has completed in ipif_down_tail.
18790 	 * This flag does not indicate whether the ill stream is in the
18791 	 * DL_BOUND state with the driver. Instead this flag is used by
18792 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18793 	 * the driver. The state of the ill stream i.e. whether it is
18794 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18795 	 */
18796 	ipif->ipif_ill->ill_logical_down = 1;
18797 	return (ipif_down(ipif, q, mp));
18798 }
18799 
18800 /*
18801  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18802  * If the usesrc client ILL is already part of a usesrc group or not,
18803  * in either case a ire_stq with the matching usesrc client ILL will
18804  * locate the IRE's that need to be deleted. We want IREs to be created
18805  * with the new source address.
18806  */
18807 static void
18808 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18809 {
18810 	ill_t	*ucill = (ill_t *)ill_arg;
18811 
18812 	ASSERT(IAM_WRITER_ILL(ucill));
18813 
18814 	if (ire->ire_stq == NULL)
18815 		return;
18816 
18817 	if ((ire->ire_type == IRE_CACHE) &&
18818 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18819 		ire_delete(ire);
18820 }
18821 
18822 /*
18823  * ire_walk routine to delete every IRE dependent on the interface
18824  * address that is going down.	(Always called as writer.)
18825  * Works for both v4 and v6.
18826  * In addition for checking for ire_ipif matches it also checks for
18827  * IRE_CACHE entries which have the same source address as the
18828  * disappearing ipif since ipif_select_source might have picked
18829  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18830  * care of any IRE_INTERFACE with the disappearing source address.
18831  */
18832 static void
18833 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18834 {
18835 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18836 	ill_t *ire_ill;
18837 	ill_t *ipif_ill;
18838 
18839 	ASSERT(IAM_WRITER_IPIF(ipif));
18840 	if (ire->ire_ipif == NULL)
18841 		return;
18842 
18843 	/*
18844 	 * For IPv4, we derive source addresses for an IRE from ipif's
18845 	 * belonging to the same IPMP group as the IRE's outgoing
18846 	 * interface.  If an IRE's outgoing interface isn't in the
18847 	 * same IPMP group as a particular ipif, then that ipif
18848 	 * couldn't have been used as a source address for this IRE.
18849 	 *
18850 	 * For IPv6, source addresses are only restricted to the IPMP group
18851 	 * if the IRE is for a link-local address or a multicast address.
18852 	 * Otherwise, source addresses for an IRE can be chosen from
18853 	 * interfaces other than the the outgoing interface for that IRE.
18854 	 *
18855 	 * For source address selection details, see ipif_select_source()
18856 	 * and ipif_select_source_v6().
18857 	 */
18858 	if (ire->ire_ipversion == IPV4_VERSION ||
18859 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18860 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18861 		ire_ill = ire->ire_ipif->ipif_ill;
18862 		ipif_ill = ipif->ipif_ill;
18863 
18864 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18865 			return;
18866 		}
18867 	}
18868 
18869 
18870 	if (ire->ire_ipif != ipif) {
18871 		/*
18872 		 * Look for a matching source address.
18873 		 */
18874 		if (ire->ire_type != IRE_CACHE)
18875 			return;
18876 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18877 			return;
18878 
18879 		if (ire->ire_ipversion == IPV4_VERSION) {
18880 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18881 				return;
18882 		} else {
18883 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18884 			    &ipif->ipif_v6lcl_addr))
18885 				return;
18886 		}
18887 		ire_delete(ire);
18888 		return;
18889 	}
18890 	/*
18891 	 * ire_delete() will do an ire_flush_cache which will delete
18892 	 * all ire_ipif matches
18893 	 */
18894 	ire_delete(ire);
18895 }
18896 
18897 /*
18898  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18899  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18900  * 2) when an interface is brought up or down (on that ill).
18901  * This ensures that the IRE_CACHE entries don't retain stale source
18902  * address selection results.
18903  */
18904 void
18905 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18906 {
18907 	ill_t	*ill = (ill_t *)ill_arg;
18908 	ill_t	*ipif_ill;
18909 
18910 	ASSERT(IAM_WRITER_ILL(ill));
18911 	/*
18912 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18913 	 * Hence this should be IRE_CACHE.
18914 	 */
18915 	ASSERT(ire->ire_type == IRE_CACHE);
18916 
18917 	/*
18918 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18919 	 * We are only interested in IRE_CACHES that has borrowed
18920 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18921 	 * for which we need to look at ire_ipif->ipif_ill match
18922 	 * with ill.
18923 	 */
18924 	ASSERT(ire->ire_ipif != NULL);
18925 	ipif_ill = ire->ire_ipif->ipif_ill;
18926 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18927 	    ipif_ill->ill_group == ill->ill_group)) {
18928 		ire_delete(ire);
18929 	}
18930 }
18931 
18932 /*
18933  * Delete all the ire whose stq references ill_arg.
18934  */
18935 static void
18936 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18937 {
18938 	ill_t	*ill = (ill_t *)ill_arg;
18939 	ill_t	*ire_ill;
18940 
18941 	ASSERT(IAM_WRITER_ILL(ill));
18942 	/*
18943 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18944 	 * Hence this should be IRE_CACHE.
18945 	 */
18946 	ASSERT(ire->ire_type == IRE_CACHE);
18947 
18948 	/*
18949 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18950 	 * matches ill. We are only interested in IRE_CACHES that
18951 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18952 	 * filtering here.
18953 	 */
18954 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18955 
18956 	if (ire_ill == ill)
18957 		ire_delete(ire);
18958 }
18959 
18960 /*
18961  * This is called when an ill leaves the group. We want to delete
18962  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18963  * pointing at ill.
18964  */
18965 static void
18966 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18967 {
18968 	ill_t	*ill = (ill_t *)ill_arg;
18969 
18970 	ASSERT(IAM_WRITER_ILL(ill));
18971 	ASSERT(ill->ill_group == NULL);
18972 	/*
18973 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18974 	 * Hence this should be IRE_CACHE.
18975 	 */
18976 	ASSERT(ire->ire_type == IRE_CACHE);
18977 	/*
18978 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18979 	 * matches ill. We are interested in both.
18980 	 */
18981 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18982 	    (ire->ire_ipif->ipif_ill == ill));
18983 
18984 	ire_delete(ire);
18985 }
18986 
18987 /*
18988  * Initiate deallocate of an IPIF. Always called as writer. Called by
18989  * ill_delete or ip_sioctl_removeif.
18990  */
18991 static void
18992 ipif_free(ipif_t *ipif)
18993 {
18994 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18995 
18996 	ASSERT(IAM_WRITER_IPIF(ipif));
18997 
18998 	if (ipif->ipif_recovery_id != 0)
18999 		(void) untimeout(ipif->ipif_recovery_id);
19000 	ipif->ipif_recovery_id = 0;
19001 
19002 	/* Remove conn references */
19003 	reset_conn_ipif(ipif);
19004 
19005 	/*
19006 	 * Make sure we have valid net and subnet broadcast ire's for the
19007 	 * other ipif's which share them with this ipif.
19008 	 */
19009 	if (!ipif->ipif_isv6)
19010 		ipif_check_bcast_ires(ipif);
19011 
19012 	/*
19013 	 * Take down the interface. We can be called either from ill_delete
19014 	 * or from ip_sioctl_removeif.
19015 	 */
19016 	(void) ipif_down(ipif, NULL, NULL);
19017 
19018 	/*
19019 	 * Now that the interface is down, there's no chance it can still
19020 	 * become a duplicate.  Cancel any timer that may have been set while
19021 	 * tearing down.
19022 	 */
19023 	if (ipif->ipif_recovery_id != 0)
19024 		(void) untimeout(ipif->ipif_recovery_id);
19025 	ipif->ipif_recovery_id = 0;
19026 
19027 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19028 	/* Remove pointers to this ill in the multicast routing tables */
19029 	reset_mrt_vif_ipif(ipif);
19030 	rw_exit(&ipst->ips_ill_g_lock);
19031 }
19032 
19033 /*
19034  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19035  * also ill_move().
19036  */
19037 static void
19038 ipif_free_tail(ipif_t *ipif)
19039 {
19040 	mblk_t	*mp;
19041 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19042 
19043 	/*
19044 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19045 	 */
19046 	mutex_enter(&ipif->ipif_saved_ire_lock);
19047 	mp = ipif->ipif_saved_ire_mp;
19048 	ipif->ipif_saved_ire_mp = NULL;
19049 	mutex_exit(&ipif->ipif_saved_ire_lock);
19050 	freemsg(mp);
19051 
19052 	/*
19053 	 * Need to hold both ill_g_lock and ill_lock while
19054 	 * inserting or removing an ipif from the linked list
19055 	 * of ipifs hanging off the ill.
19056 	 */
19057 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19058 	/*
19059 	 * Remove all IPv4 multicast memberships on the interface now.
19060 	 * IPv6 is not handled here as the multicast memberships are
19061 	 * tied to the ill rather than the ipif.
19062 	 */
19063 	ilm_free(ipif);
19064 
19065 	/*
19066 	 * Since we held the ill_g_lock while doing the ilm_free above,
19067 	 * we can assert the ilms were really deleted and not just marked
19068 	 * ILM_DELETED.
19069 	 */
19070 	ASSERT(ilm_walk_ipif(ipif) == 0);
19071 
19072 #ifdef DEBUG
19073 	ipif_trace_cleanup(ipif);
19074 #endif
19075 
19076 	/* Ask SCTP to take it out of it list */
19077 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19078 
19079 	/* Get it out of the ILL interface list. */
19080 	ipif_remove(ipif, B_TRUE);
19081 	rw_exit(&ipst->ips_ill_g_lock);
19082 
19083 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19084 
19085 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19086 	ASSERT(ipif->ipif_recovery_id == 0);
19087 
19088 	/* Free the memory. */
19089 	mi_free(ipif);
19090 }
19091 
19092 /*
19093  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19094  * is zero.
19095  */
19096 void
19097 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19098 {
19099 	char	lbuf[LIFNAMSIZ];
19100 	char	*name;
19101 	size_t	name_len;
19102 
19103 	buf[0] = '\0';
19104 	name = ipif->ipif_ill->ill_name;
19105 	name_len = ipif->ipif_ill->ill_name_length;
19106 	if (ipif->ipif_id != 0) {
19107 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19108 		    ipif->ipif_id);
19109 		name = lbuf;
19110 		name_len = mi_strlen(name) + 1;
19111 	}
19112 	len -= 1;
19113 	buf[len] = '\0';
19114 	len = MIN(len, name_len);
19115 	bcopy(name, buf, len);
19116 }
19117 
19118 /*
19119  * Find an IPIF based on the name passed in.  Names can be of the
19120  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19121  * The <phys> string can have forms like <dev><#> (e.g., le0),
19122  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19123  * When there is no colon, the implied unit id is zero. <phys> must
19124  * correspond to the name of an ILL.  (May be called as writer.)
19125  */
19126 static ipif_t *
19127 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19128     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19129     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19130 {
19131 	char	*cp;
19132 	char	*endp;
19133 	long	id;
19134 	ill_t	*ill;
19135 	ipif_t	*ipif;
19136 	uint_t	ire_type;
19137 	boolean_t did_alloc = B_FALSE;
19138 	ipsq_t	*ipsq;
19139 
19140 	if (error != NULL)
19141 		*error = 0;
19142 
19143 	/*
19144 	 * If the caller wants to us to create the ipif, make sure we have a
19145 	 * valid zoneid
19146 	 */
19147 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19148 
19149 	if (namelen == 0) {
19150 		if (error != NULL)
19151 			*error = ENXIO;
19152 		return (NULL);
19153 	}
19154 
19155 	*exists = B_FALSE;
19156 	/* Look for a colon in the name. */
19157 	endp = &name[namelen];
19158 	for (cp = endp; --cp > name; ) {
19159 		if (*cp == IPIF_SEPARATOR_CHAR)
19160 			break;
19161 	}
19162 
19163 	if (*cp == IPIF_SEPARATOR_CHAR) {
19164 		/*
19165 		 * Reject any non-decimal aliases for logical
19166 		 * interfaces. Aliases with leading zeroes
19167 		 * are also rejected as they introduce ambiguity
19168 		 * in the naming of the interfaces.
19169 		 * In order to confirm with existing semantics,
19170 		 * and to not break any programs/script relying
19171 		 * on that behaviour, if<0>:0 is considered to be
19172 		 * a valid interface.
19173 		 *
19174 		 * If alias has two or more digits and the first
19175 		 * is zero, fail.
19176 		 */
19177 		if (&cp[2] < endp && cp[1] == '0') {
19178 			if (error != NULL)
19179 				*error = EINVAL;
19180 			return (NULL);
19181 		}
19182 	}
19183 
19184 	if (cp <= name) {
19185 		cp = endp;
19186 	} else {
19187 		*cp = '\0';
19188 	}
19189 
19190 	/*
19191 	 * Look up the ILL, based on the portion of the name
19192 	 * before the slash. ill_lookup_on_name returns a held ill.
19193 	 * Temporary to check whether ill exists already. If so
19194 	 * ill_lookup_on_name will clear it.
19195 	 */
19196 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19197 	    q, mp, func, error, &did_alloc, ipst);
19198 	if (cp != endp)
19199 		*cp = IPIF_SEPARATOR_CHAR;
19200 	if (ill == NULL)
19201 		return (NULL);
19202 
19203 	/* Establish the unit number in the name. */
19204 	id = 0;
19205 	if (cp < endp && *endp == '\0') {
19206 		/* If there was a colon, the unit number follows. */
19207 		cp++;
19208 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19209 			ill_refrele(ill);
19210 			if (error != NULL)
19211 				*error = ENXIO;
19212 			return (NULL);
19213 		}
19214 	}
19215 
19216 	GRAB_CONN_LOCK(q);
19217 	mutex_enter(&ill->ill_lock);
19218 	/* Now see if there is an IPIF with this unit number. */
19219 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19220 		if (ipif->ipif_id == id) {
19221 			if (zoneid != ALL_ZONES &&
19222 			    zoneid != ipif->ipif_zoneid &&
19223 			    ipif->ipif_zoneid != ALL_ZONES) {
19224 				mutex_exit(&ill->ill_lock);
19225 				RELEASE_CONN_LOCK(q);
19226 				ill_refrele(ill);
19227 				if (error != NULL)
19228 					*error = ENXIO;
19229 				return (NULL);
19230 			}
19231 			/*
19232 			 * The block comment at the start of ipif_down
19233 			 * explains the use of the macros used below
19234 			 */
19235 			if (IPIF_CAN_LOOKUP(ipif)) {
19236 				ipif_refhold_locked(ipif);
19237 				mutex_exit(&ill->ill_lock);
19238 				if (!did_alloc)
19239 					*exists = B_TRUE;
19240 				/*
19241 				 * Drop locks before calling ill_refrele
19242 				 * since it can potentially call into
19243 				 * ipif_ill_refrele_tail which can end up
19244 				 * in trying to acquire any lock.
19245 				 */
19246 				RELEASE_CONN_LOCK(q);
19247 				ill_refrele(ill);
19248 				return (ipif);
19249 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19250 				ipsq = ill->ill_phyint->phyint_ipsq;
19251 				mutex_enter(&ipsq->ipsq_lock);
19252 				mutex_exit(&ill->ill_lock);
19253 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19254 				mutex_exit(&ipsq->ipsq_lock);
19255 				RELEASE_CONN_LOCK(q);
19256 				ill_refrele(ill);
19257 				if (error != NULL)
19258 					*error = EINPROGRESS;
19259 				return (NULL);
19260 			}
19261 		}
19262 	}
19263 	RELEASE_CONN_LOCK(q);
19264 
19265 	if (!do_alloc) {
19266 		mutex_exit(&ill->ill_lock);
19267 		ill_refrele(ill);
19268 		if (error != NULL)
19269 			*error = ENXIO;
19270 		return (NULL);
19271 	}
19272 
19273 	/*
19274 	 * If none found, atomically allocate and return a new one.
19275 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19276 	 * to support "receive only" use of lo0:1 etc. as is still done
19277 	 * below as an initial guess.
19278 	 * However, this is now likely to be overriden later in ipif_up_done()
19279 	 * when we know for sure what address has been configured on the
19280 	 * interface, since we might have more than one loopback interface
19281 	 * with a loopback address, e.g. in the case of zones, and all the
19282 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19283 	 */
19284 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19285 		ire_type = IRE_LOOPBACK;
19286 	else
19287 		ire_type = IRE_LOCAL;
19288 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19289 	if (ipif != NULL)
19290 		ipif_refhold_locked(ipif);
19291 	else if (error != NULL)
19292 		*error = ENOMEM;
19293 	mutex_exit(&ill->ill_lock);
19294 	ill_refrele(ill);
19295 	return (ipif);
19296 }
19297 
19298 /*
19299  * This routine is called whenever a new address comes up on an ipif.  If
19300  * we are configured to respond to address mask requests, then we are supposed
19301  * to broadcast an address mask reply at this time.  This routine is also
19302  * called if we are already up, but a netmask change is made.  This is legal
19303  * but might not make the system manager very popular.	(May be called
19304  * as writer.)
19305  */
19306 void
19307 ipif_mask_reply(ipif_t *ipif)
19308 {
19309 	icmph_t	*icmph;
19310 	ipha_t	*ipha;
19311 	mblk_t	*mp;
19312 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19313 
19314 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19315 
19316 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19317 		return;
19318 
19319 	/* ICMP mask reply is IPv4 only */
19320 	ASSERT(!ipif->ipif_isv6);
19321 	/* ICMP mask reply is not for a loopback interface */
19322 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19323 
19324 	mp = allocb(REPLY_LEN, BPRI_HI);
19325 	if (mp == NULL)
19326 		return;
19327 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19328 
19329 	ipha = (ipha_t *)mp->b_rptr;
19330 	bzero(ipha, REPLY_LEN);
19331 	*ipha = icmp_ipha;
19332 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19333 	ipha->ipha_src = ipif->ipif_src_addr;
19334 	ipha->ipha_dst = ipif->ipif_brd_addr;
19335 	ipha->ipha_length = htons(REPLY_LEN);
19336 	ipha->ipha_ident = 0;
19337 
19338 	icmph = (icmph_t *)&ipha[1];
19339 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19340 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19341 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19342 
19343 	put(ipif->ipif_wq, mp);
19344 
19345 #undef	REPLY_LEN
19346 }
19347 
19348 /*
19349  * When the mtu in the ipif changes, we call this routine through ire_walk
19350  * to update all the relevant IREs.
19351  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19352  */
19353 static void
19354 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19355 {
19356 	ipif_t *ipif = (ipif_t *)ipif_arg;
19357 
19358 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19359 		return;
19360 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19361 }
19362 
19363 /*
19364  * When the mtu in the ill 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 void
19369 ill_mtu_change(ire_t *ire, char *ill_arg)
19370 {
19371 	ill_t	*ill = (ill_t *)ill_arg;
19372 
19373 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19374 		return;
19375 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19376 }
19377 
19378 /*
19379  * Join the ipif specific multicast groups.
19380  * Must be called after a mapping has been set up in the resolver.  (Always
19381  * called as writer.)
19382  */
19383 void
19384 ipif_multicast_up(ipif_t *ipif)
19385 {
19386 	int err, index;
19387 	ill_t *ill;
19388 
19389 	ASSERT(IAM_WRITER_IPIF(ipif));
19390 
19391 	ill = ipif->ipif_ill;
19392 	index = ill->ill_phyint->phyint_ifindex;
19393 
19394 	ip1dbg(("ipif_multicast_up\n"));
19395 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19396 		return;
19397 
19398 	if (ipif->ipif_isv6) {
19399 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19400 			return;
19401 
19402 		/* Join the all hosts multicast address */
19403 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19404 		/*
19405 		 * Passing B_TRUE means we have to join the multicast
19406 		 * membership on this interface even though this is
19407 		 * FAILED. If we join on a different one in the group,
19408 		 * we will not be able to delete the membership later
19409 		 * as we currently don't track where we join when we
19410 		 * join within the kernel unlike applications where
19411 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19412 		 * for more on this.
19413 		 */
19414 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19415 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19416 		if (err != 0) {
19417 			ip0dbg(("ipif_multicast_up: "
19418 			    "all_hosts_mcast failed %d\n",
19419 			    err));
19420 			return;
19421 		}
19422 		/*
19423 		 * Enable multicast for the solicited node multicast address
19424 		 */
19425 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19426 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19427 
19428 			ipv6_multi.s6_addr32[3] |=
19429 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19430 
19431 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19432 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19433 			    NULL);
19434 			if (err != 0) {
19435 				ip0dbg(("ipif_multicast_up: solicited MC"
19436 				    " failed %d\n", err));
19437 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19438 				    ill, ill->ill_phyint->phyint_ifindex,
19439 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19440 				return;
19441 			}
19442 		}
19443 	} else {
19444 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19445 			return;
19446 
19447 		/* Join the all hosts multicast address */
19448 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19449 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19450 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19451 		if (err) {
19452 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19453 			return;
19454 		}
19455 	}
19456 	ipif->ipif_multicast_up = 1;
19457 }
19458 
19459 /*
19460  * Blow away any multicast groups that we joined in ipif_multicast_up().
19461  * (Explicit memberships are blown away in ill_leave_multicast() when the
19462  * ill is brought down.)
19463  */
19464 static void
19465 ipif_multicast_down(ipif_t *ipif)
19466 {
19467 	int err;
19468 
19469 	ASSERT(IAM_WRITER_IPIF(ipif));
19470 
19471 	ip1dbg(("ipif_multicast_down\n"));
19472 	if (!ipif->ipif_multicast_up)
19473 		return;
19474 
19475 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19476 
19477 	if (!ipif->ipif_isv6) {
19478 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19479 		    B_TRUE);
19480 		if (err != 0)
19481 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19482 
19483 		ipif->ipif_multicast_up = 0;
19484 		return;
19485 	}
19486 
19487 	/*
19488 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19489 	 * we should look for ilms on this ill rather than the ones that have
19490 	 * been failed over here.  They are here temporarily. As
19491 	 * ipif_multicast_up has joined on this ill, we should delete only
19492 	 * from this ill.
19493 	 */
19494 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19495 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19496 	    B_TRUE, B_TRUE);
19497 	if (err != 0) {
19498 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19499 		    err));
19500 	}
19501 	/*
19502 	 * Disable multicast for the solicited node multicast address
19503 	 */
19504 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19505 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19506 
19507 		ipv6_multi.s6_addr32[3] |=
19508 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19509 
19510 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19511 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19512 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19513 
19514 		if (err != 0) {
19515 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19516 			    err));
19517 		}
19518 	}
19519 
19520 	ipif->ipif_multicast_up = 0;
19521 }
19522 
19523 /*
19524  * Used when an interface comes up to recreate any extra routes on this
19525  * interface.
19526  */
19527 static ire_t **
19528 ipif_recover_ire(ipif_t *ipif)
19529 {
19530 	mblk_t	*mp;
19531 	ire_t	**ipif_saved_irep;
19532 	ire_t	**irep;
19533 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19534 
19535 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19536 	    ipif->ipif_id));
19537 
19538 	mutex_enter(&ipif->ipif_saved_ire_lock);
19539 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19540 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19541 	if (ipif_saved_irep == NULL) {
19542 		mutex_exit(&ipif->ipif_saved_ire_lock);
19543 		return (NULL);
19544 	}
19545 
19546 	irep = ipif_saved_irep;
19547 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19548 		ire_t		*ire;
19549 		queue_t		*rfq;
19550 		queue_t		*stq;
19551 		ifrt_t		*ifrt;
19552 		uchar_t		*src_addr;
19553 		uchar_t		*gateway_addr;
19554 		ushort_t	type;
19555 
19556 		/*
19557 		 * When the ire was initially created and then added in
19558 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19559 		 * in the case of a traditional interface route, or as one of
19560 		 * the IRE_OFFSUBNET types (with the exception of
19561 		 * IRE_HOST types ire which is created by icmp_redirect() and
19562 		 * which we don't need to save or recover).  In the case where
19563 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19564 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19565 		 * to satisfy software like GateD and Sun Cluster which creates
19566 		 * routes using the the loopback interface's address as a
19567 		 * gateway.
19568 		 *
19569 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19570 		 * ire_create() will be called in the same way here as
19571 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19572 		 * the route looks like a traditional interface route (where
19573 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19574 		 * the saved ifrt->ifrt_type.  This means that in the case where
19575 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19576 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19577 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19578 		 */
19579 		ifrt = (ifrt_t *)mp->b_rptr;
19580 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19581 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19582 			rfq = NULL;
19583 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19584 			    ? ipif->ipif_rq : ipif->ipif_wq;
19585 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19586 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19587 			    : (uint8_t *)&ipif->ipif_src_addr;
19588 			gateway_addr = NULL;
19589 			type = ipif->ipif_net_type;
19590 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19591 			/* Recover multiroute broadcast IRE. */
19592 			rfq = ipif->ipif_rq;
19593 			stq = ipif->ipif_wq;
19594 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19595 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19596 			    : (uint8_t *)&ipif->ipif_src_addr;
19597 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19598 			type = ifrt->ifrt_type;
19599 		} else {
19600 			rfq = NULL;
19601 			stq = NULL;
19602 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19603 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19604 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19605 			type = ifrt->ifrt_type;
19606 		}
19607 
19608 		/*
19609 		 * Create a copy of the IRE with the saved address and netmask.
19610 		 */
19611 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19612 		    "0x%x/0x%x\n",
19613 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19614 		    ntohl(ifrt->ifrt_addr),
19615 		    ntohl(ifrt->ifrt_mask)));
19616 		ire = ire_create(
19617 		    (uint8_t *)&ifrt->ifrt_addr,
19618 		    (uint8_t *)&ifrt->ifrt_mask,
19619 		    src_addr,
19620 		    gateway_addr,
19621 		    &ifrt->ifrt_max_frag,
19622 		    NULL,
19623 		    rfq,
19624 		    stq,
19625 		    type,
19626 		    ipif,
19627 		    0,
19628 		    0,
19629 		    0,
19630 		    ifrt->ifrt_flags,
19631 		    &ifrt->ifrt_iulp_info,
19632 		    NULL,
19633 		    NULL,
19634 		    ipst);
19635 
19636 		if (ire == NULL) {
19637 			mutex_exit(&ipif->ipif_saved_ire_lock);
19638 			kmem_free(ipif_saved_irep,
19639 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19640 			return (NULL);
19641 		}
19642 
19643 		/*
19644 		 * Some software (for example, GateD and Sun Cluster) attempts
19645 		 * to create (what amount to) IRE_PREFIX routes with the
19646 		 * loopback address as the gateway.  This is primarily done to
19647 		 * set up prefixes with the RTF_REJECT flag set (for example,
19648 		 * when generating aggregate routes.)
19649 		 *
19650 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19651 		 * IRE_LOOPBACK, then we map the request into a
19652 		 * IRE_IF_NORESOLVER.
19653 		 */
19654 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19655 			ire->ire_type = IRE_IF_NORESOLVER;
19656 		/*
19657 		 * ire held by ire_add, will be refreled' towards the
19658 		 * the end of ipif_up_done
19659 		 */
19660 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19661 		*irep = ire;
19662 		irep++;
19663 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19664 	}
19665 	mutex_exit(&ipif->ipif_saved_ire_lock);
19666 	return (ipif_saved_irep);
19667 }
19668 
19669 /*
19670  * Used to set the netmask and broadcast address to default values when the
19671  * interface is brought up.  (Always called as writer.)
19672  */
19673 static void
19674 ipif_set_default(ipif_t *ipif)
19675 {
19676 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19677 
19678 	if (!ipif->ipif_isv6) {
19679 		/*
19680 		 * Interface holds an IPv4 address. Default
19681 		 * mask is the natural netmask.
19682 		 */
19683 		if (!ipif->ipif_net_mask) {
19684 			ipaddr_t	v4mask;
19685 
19686 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19687 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19688 		}
19689 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19690 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19691 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19692 		} else {
19693 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19694 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19695 		}
19696 		/*
19697 		 * NOTE: SunOS 4.X does this even if the broadcast address
19698 		 * has been already set thus we do the same here.
19699 		 */
19700 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19701 			ipaddr_t	v4addr;
19702 
19703 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19704 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19705 		}
19706 	} else {
19707 		/*
19708 		 * Interface holds an IPv6-only address.  Default
19709 		 * mask is all-ones.
19710 		 */
19711 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19712 			ipif->ipif_v6net_mask = ipv6_all_ones;
19713 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19714 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19715 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19716 		} else {
19717 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19718 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19719 		}
19720 	}
19721 }
19722 
19723 /*
19724  * Return 0 if this address can be used as local address without causing
19725  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19726  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19727  * Special checks are needed to allow the same IPv6 link-local address
19728  * on different ills.
19729  * TODO: allowing the same site-local address on different ill's.
19730  */
19731 int
19732 ip_addr_availability_check(ipif_t *new_ipif)
19733 {
19734 	in6_addr_t our_v6addr;
19735 	ill_t *ill;
19736 	ipif_t *ipif;
19737 	ill_walk_context_t ctx;
19738 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19739 
19740 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19741 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19742 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19743 
19744 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19745 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19746 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19747 		return (0);
19748 
19749 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19750 
19751 	if (new_ipif->ipif_isv6)
19752 		ill = ILL_START_WALK_V6(&ctx, ipst);
19753 	else
19754 		ill = ILL_START_WALK_V4(&ctx, ipst);
19755 
19756 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19757 		for (ipif = ill->ill_ipif; ipif != NULL;
19758 		    ipif = ipif->ipif_next) {
19759 			if ((ipif == new_ipif) ||
19760 			    !(ipif->ipif_flags & IPIF_UP) ||
19761 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19762 				continue;
19763 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19764 			    &our_v6addr)) {
19765 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19766 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19767 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19768 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19769 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19770 				    new_ipif->ipif_ill != ill)
19771 					continue;
19772 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19773 				    new_ipif->ipif_ill != ill)
19774 					continue;
19775 				else if (new_ipif->ipif_zoneid !=
19776 				    ipif->ipif_zoneid &&
19777 				    ipif->ipif_zoneid != ALL_ZONES &&
19778 				    IS_LOOPBACK(ill))
19779 					continue;
19780 				else if (new_ipif->ipif_ill == ill)
19781 					return (EADDRINUSE);
19782 				else
19783 					return (EADDRNOTAVAIL);
19784 			}
19785 		}
19786 	}
19787 
19788 	return (0);
19789 }
19790 
19791 /*
19792  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19793  * IREs for the ipif.
19794  * When the routine returns EINPROGRESS then mp has been consumed and
19795  * the ioctl will be acked from ip_rput_dlpi.
19796  */
19797 static int
19798 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19799 {
19800 	ill_t	*ill = ipif->ipif_ill;
19801 	boolean_t isv6 = ipif->ipif_isv6;
19802 	int	err = 0;
19803 	boolean_t success;
19804 
19805 	ASSERT(IAM_WRITER_IPIF(ipif));
19806 
19807 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19808 
19809 	/* Shouldn't get here if it is already up. */
19810 	if (ipif->ipif_flags & IPIF_UP)
19811 		return (EALREADY);
19812 
19813 	/* Skip arp/ndp for any loopback interface. */
19814 	if (ill->ill_wq != NULL) {
19815 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19816 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19817 
19818 		if (!ill->ill_dl_up) {
19819 			/*
19820 			 * ill_dl_up is not yet set. i.e. we are yet to
19821 			 * DL_BIND with the driver and this is the first
19822 			 * logical interface on the ill to become "up".
19823 			 * Tell the driver to get going (via DL_BIND_REQ).
19824 			 * Note that changing "significant" IFF_ flags
19825 			 * address/netmask etc cause a down/up dance, but
19826 			 * does not cause an unbind (DL_UNBIND) with the driver
19827 			 */
19828 			return (ill_dl_up(ill, ipif, mp, q));
19829 		}
19830 
19831 		/*
19832 		 * ipif_resolver_up may end up sending an
19833 		 * AR_INTERFACE_UP message to ARP, which would, in
19834 		 * turn send a DLPI message to the driver. ioctls are
19835 		 * serialized and so we cannot send more than one
19836 		 * interface up message at a time. If ipif_resolver_up
19837 		 * does send an interface up message to ARP, we get
19838 		 * EINPROGRESS and we will complete in ip_arp_done.
19839 		 */
19840 
19841 		ASSERT(connp != NULL || !CONN_Q(q));
19842 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19843 		if (connp != NULL)
19844 			mutex_enter(&connp->conn_lock);
19845 		mutex_enter(&ill->ill_lock);
19846 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19847 		mutex_exit(&ill->ill_lock);
19848 		if (connp != NULL)
19849 			mutex_exit(&connp->conn_lock);
19850 		if (!success)
19851 			return (EINTR);
19852 
19853 		/*
19854 		 * Crank up IPv6 neighbor discovery
19855 		 * Unlike ARP, this should complete when
19856 		 * ipif_ndp_up returns. However, for
19857 		 * ILLF_XRESOLV interfaces we also send a
19858 		 * AR_INTERFACE_UP to the external resolver.
19859 		 * That ioctl will complete in ip_rput.
19860 		 */
19861 		if (isv6) {
19862 			err = ipif_ndp_up(ipif);
19863 			if (err != 0) {
19864 				if (err != EINPROGRESS)
19865 					mp = ipsq_pending_mp_get(ipsq, &connp);
19866 				return (err);
19867 			}
19868 		}
19869 		/* Now, ARP */
19870 		err = ipif_resolver_up(ipif, Res_act_initial);
19871 		if (err == EINPROGRESS) {
19872 			/* We will complete it in ip_arp_done */
19873 			return (err);
19874 		}
19875 		mp = ipsq_pending_mp_get(ipsq, &connp);
19876 		ASSERT(mp != NULL);
19877 		if (err != 0)
19878 			return (err);
19879 	} else {
19880 		/*
19881 		 * Interfaces without underlying hardware don't do duplicate
19882 		 * address detection.
19883 		 */
19884 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19885 		ipif->ipif_addr_ready = 1;
19886 	}
19887 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19888 }
19889 
19890 /*
19891  * Perform a bind for the physical device.
19892  * When the routine returns EINPROGRESS then mp has been consumed and
19893  * the ioctl will be acked from ip_rput_dlpi.
19894  * Allocate an unbind message and save it until ipif_down.
19895  */
19896 static int
19897 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19898 {
19899 	areq_t	*areq;
19900 	mblk_t	*areq_mp = NULL;
19901 	mblk_t	*bind_mp = NULL;
19902 	mblk_t	*unbind_mp = NULL;
19903 	conn_t	*connp;
19904 	boolean_t success;
19905 	uint16_t sap_addr;
19906 
19907 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19908 	ASSERT(IAM_WRITER_ILL(ill));
19909 	ASSERT(mp != NULL);
19910 
19911 	/* Create a resolver cookie for ARP */
19912 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19913 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19914 		if (areq_mp == NULL)
19915 			return (ENOMEM);
19916 
19917 		freemsg(ill->ill_resolver_mp);
19918 		ill->ill_resolver_mp = areq_mp;
19919 		areq = (areq_t *)areq_mp->b_rptr;
19920 		sap_addr = ill->ill_sap;
19921 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19922 	}
19923 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19924 	    DL_BIND_REQ);
19925 	if (bind_mp == NULL)
19926 		goto bad;
19927 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19928 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19929 
19930 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19931 	if (unbind_mp == NULL)
19932 		goto bad;
19933 
19934 	/*
19935 	 * Record state needed to complete this operation when the
19936 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19937 	 */
19938 	ASSERT(WR(q)->q_next == NULL);
19939 	connp = Q_TO_CONN(q);
19940 
19941 	mutex_enter(&connp->conn_lock);
19942 	mutex_enter(&ipif->ipif_ill->ill_lock);
19943 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19944 	mutex_exit(&ipif->ipif_ill->ill_lock);
19945 	mutex_exit(&connp->conn_lock);
19946 	if (!success)
19947 		goto bad;
19948 
19949 	/*
19950 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19951 	 * the interface goes down.
19952 	 */
19953 	ASSERT(ill->ill_unbind_mp == NULL);
19954 	ill->ill_unbind_mp = unbind_mp;
19955 
19956 	ill_dlpi_send(ill, bind_mp);
19957 	/* Send down link-layer capabilities probe if not already done. */
19958 	ill_capability_probe(ill);
19959 
19960 	/*
19961 	 * Sysid used to rely on the fact that netboots set domainname
19962 	 * and the like. Now that miniroot boots aren't strictly netboots
19963 	 * and miniroot network configuration is driven from userland
19964 	 * these things still need to be set. This situation can be detected
19965 	 * by comparing the interface being configured here to the one
19966 	 * dhcack was set to reference by the boot loader. Once sysid is
19967 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19968 	 */
19969 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19970 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19971 	    (strlen(srpc_domain) == 0)) {
19972 		if (dhcpinit() != 0)
19973 			cmn_err(CE_WARN, "no cached dhcp response");
19974 	}
19975 
19976 	/*
19977 	 * This operation will complete in ip_rput_dlpi with either
19978 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19979 	 */
19980 	return (EINPROGRESS);
19981 bad:
19982 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19983 	/*
19984 	 * We don't have to check for possible removal from illgrp
19985 	 * as we have not yet inserted in illgrp. For groups
19986 	 * without names, this ipif is still not UP and hence
19987 	 * this could not have possibly had any influence in forming
19988 	 * groups.
19989 	 */
19990 
19991 	freemsg(bind_mp);
19992 	freemsg(unbind_mp);
19993 	return (ENOMEM);
19994 }
19995 
19996 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19997 
19998 /*
19999  * DLPI and ARP is up.
20000  * Create all the IREs associated with an interface bring up multicast.
20001  * Set the interface flag and finish other initialization
20002  * that potentially had to be differed to after DL_BIND_ACK.
20003  */
20004 int
20005 ipif_up_done(ipif_t *ipif)
20006 {
20007 	ire_t	*ire_array[20];
20008 	ire_t	**irep = ire_array;
20009 	ire_t	**irep1;
20010 	ipaddr_t net_mask = 0;
20011 	ipaddr_t subnet_mask, route_mask;
20012 	ill_t	*ill = ipif->ipif_ill;
20013 	queue_t	*stq;
20014 	ipif_t	 *src_ipif;
20015 	ipif_t   *tmp_ipif;
20016 	boolean_t	flush_ire_cache = B_TRUE;
20017 	int	err = 0;
20018 	phyint_t *phyi;
20019 	ire_t	**ipif_saved_irep = NULL;
20020 	int ipif_saved_ire_cnt;
20021 	int	cnt;
20022 	boolean_t	src_ipif_held = B_FALSE;
20023 	boolean_t	ire_added = B_FALSE;
20024 	boolean_t	loopback = B_FALSE;
20025 	ip_stack_t	*ipst = ill->ill_ipst;
20026 
20027 	ip1dbg(("ipif_up_done(%s:%u)\n",
20028 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20029 	/* Check if this is a loopback interface */
20030 	if (ipif->ipif_ill->ill_wq == NULL)
20031 		loopback = B_TRUE;
20032 
20033 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20034 	/*
20035 	 * If all other interfaces for this ill are down or DEPRECATED,
20036 	 * or otherwise unsuitable for source address selection, remove
20037 	 * any IRE_CACHE entries for this ill to make sure source
20038 	 * address selection gets to take this new ipif into account.
20039 	 * No need to hold ill_lock while traversing the ipif list since
20040 	 * we are writer
20041 	 */
20042 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20043 	    tmp_ipif = tmp_ipif->ipif_next) {
20044 		if (((tmp_ipif->ipif_flags &
20045 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20046 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20047 		    (tmp_ipif == ipif))
20048 			continue;
20049 		/* first useable pre-existing interface */
20050 		flush_ire_cache = B_FALSE;
20051 		break;
20052 	}
20053 	if (flush_ire_cache)
20054 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20055 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20056 
20057 	/*
20058 	 * Figure out which way the send-to queue should go.  Only
20059 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20060 	 * should show up here.
20061 	 */
20062 	switch (ill->ill_net_type) {
20063 	case IRE_IF_RESOLVER:
20064 		stq = ill->ill_rq;
20065 		break;
20066 	case IRE_IF_NORESOLVER:
20067 	case IRE_LOOPBACK:
20068 		stq = ill->ill_wq;
20069 		break;
20070 	default:
20071 		return (EINVAL);
20072 	}
20073 
20074 	if (IS_LOOPBACK(ill)) {
20075 		/*
20076 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20077 		 * ipif_lookup_on_name(), but in the case of zones we can have
20078 		 * several loopback addresses on lo0. So all the interfaces with
20079 		 * loopback addresses need to be marked IRE_LOOPBACK.
20080 		 */
20081 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20082 		    htonl(INADDR_LOOPBACK))
20083 			ipif->ipif_ire_type = IRE_LOOPBACK;
20084 		else
20085 			ipif->ipif_ire_type = IRE_LOCAL;
20086 	}
20087 
20088 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20089 		/*
20090 		 * Can't use our source address. Select a different
20091 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20092 		 */
20093 		src_ipif = ipif_select_source(ipif->ipif_ill,
20094 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20095 		if (src_ipif == NULL)
20096 			src_ipif = ipif;	/* Last resort */
20097 		else
20098 			src_ipif_held = B_TRUE;
20099 	} else {
20100 		src_ipif = ipif;
20101 	}
20102 
20103 	/* Create all the IREs associated with this interface */
20104 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20105 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20106 
20107 		/*
20108 		 * If we're on a labeled system then make sure that zone-
20109 		 * private addresses have proper remote host database entries.
20110 		 */
20111 		if (is_system_labeled() &&
20112 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20113 		    !tsol_check_interface_address(ipif))
20114 			return (EINVAL);
20115 
20116 		/* Register the source address for __sin6_src_id */
20117 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20118 		    ipif->ipif_zoneid, ipst);
20119 		if (err != 0) {
20120 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20121 			return (err);
20122 		}
20123 
20124 		/* If the interface address is set, create the local IRE. */
20125 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20126 		    (void *)ipif,
20127 		    ipif->ipif_ire_type,
20128 		    ntohl(ipif->ipif_lcl_addr)));
20129 		*irep++ = ire_create(
20130 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20131 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20132 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20133 		    NULL,				/* no gateway */
20134 		    &ip_loopback_mtuplus,		/* max frag size */
20135 		    NULL,
20136 		    ipif->ipif_rq,			/* recv-from queue */
20137 		    NULL,				/* no send-to queue */
20138 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20139 		    ipif,
20140 		    0,
20141 		    0,
20142 		    0,
20143 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20144 		    RTF_PRIVATE : 0,
20145 		    &ire_uinfo_null,
20146 		    NULL,
20147 		    NULL,
20148 		    ipst);
20149 	} else {
20150 		ip1dbg((
20151 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20152 		    ipif->ipif_ire_type,
20153 		    ntohl(ipif->ipif_lcl_addr),
20154 		    (uint_t)ipif->ipif_flags));
20155 	}
20156 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20157 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20158 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20159 	} else {
20160 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20161 	}
20162 
20163 	subnet_mask = ipif->ipif_net_mask;
20164 
20165 	/*
20166 	 * If mask was not specified, use natural netmask of
20167 	 * interface address. Also, store this mask back into the
20168 	 * ipif struct.
20169 	 */
20170 	if (subnet_mask == 0) {
20171 		subnet_mask = net_mask;
20172 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20173 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20174 		    ipif->ipif_v6subnet);
20175 	}
20176 
20177 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20178 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20179 	    ipif->ipif_subnet != INADDR_ANY) {
20180 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20181 
20182 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20183 			route_mask = IP_HOST_MASK;
20184 		} else {
20185 			route_mask = subnet_mask;
20186 		}
20187 
20188 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20189 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20190 		    (void *)ipif, (void *)ill,
20191 		    ill->ill_net_type,
20192 		    ntohl(ipif->ipif_subnet)));
20193 		*irep++ = ire_create(
20194 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20195 		    (uchar_t *)&route_mask,		/* mask */
20196 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20197 		    NULL,				/* no gateway */
20198 		    &ipif->ipif_mtu,			/* max frag */
20199 		    NULL,
20200 		    NULL,				/* no recv queue */
20201 		    stq,				/* send-to queue */
20202 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20203 		    ipif,
20204 		    0,
20205 		    0,
20206 		    0,
20207 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20208 		    &ire_uinfo_null,
20209 		    NULL,
20210 		    NULL,
20211 		    ipst);
20212 	}
20213 
20214 	/*
20215 	 * Create any necessary broadcast IREs.
20216 	 */
20217 	if (ipif->ipif_flags & IPIF_BROADCAST)
20218 		irep = ipif_create_bcast_ires(ipif, irep);
20219 
20220 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20221 
20222 	/* If an earlier ire_create failed, get out now */
20223 	for (irep1 = irep; irep1 > ire_array; ) {
20224 		irep1--;
20225 		if (*irep1 == NULL) {
20226 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20227 			err = ENOMEM;
20228 			goto bad;
20229 		}
20230 	}
20231 
20232 	/*
20233 	 * Need to atomically check for ip_addr_availablity_check
20234 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20235 	 * from group also.The ill_g_lock is grabbed as reader
20236 	 * just to make sure no new ills or new ipifs are being added
20237 	 * to the system while we are checking the uniqueness of addresses.
20238 	 */
20239 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20240 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20241 	/* Mark it up, and increment counters. */
20242 	ipif->ipif_flags |= IPIF_UP;
20243 	ill->ill_ipif_up_count++;
20244 	err = ip_addr_availability_check(ipif);
20245 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20246 	rw_exit(&ipst->ips_ill_g_lock);
20247 
20248 	if (err != 0) {
20249 		/*
20250 		 * Our address may already be up on the same ill. In this case,
20251 		 * the ARP entry for our ipif replaced the one for the other
20252 		 * ipif. So we don't want to delete it (otherwise the other ipif
20253 		 * would be unable to send packets).
20254 		 * ip_addr_availability_check() identifies this case for us and
20255 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20256 		 * which is the expected error code.
20257 		 */
20258 		if (err == EADDRINUSE) {
20259 			freemsg(ipif->ipif_arp_del_mp);
20260 			ipif->ipif_arp_del_mp = NULL;
20261 			err = EADDRNOTAVAIL;
20262 		}
20263 		ill->ill_ipif_up_count--;
20264 		ipif->ipif_flags &= ~IPIF_UP;
20265 		goto bad;
20266 	}
20267 
20268 	/*
20269 	 * Add in all newly created IREs.  ire_create_bcast() has
20270 	 * already checked for duplicates of the IRE_BROADCAST type.
20271 	 * We want to add before we call ifgrp_insert which wants
20272 	 * to know whether IRE_IF_RESOLVER exists or not.
20273 	 *
20274 	 * NOTE : We refrele the ire though we may branch to "bad"
20275 	 *	  later on where we do ire_delete. This is okay
20276 	 *	  because nobody can delete it as we are running
20277 	 *	  exclusively.
20278 	 */
20279 	for (irep1 = irep; irep1 > ire_array; ) {
20280 		irep1--;
20281 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20282 		/*
20283 		 * refheld by ire_add. refele towards the end of the func
20284 		 */
20285 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20286 	}
20287 	ire_added = B_TRUE;
20288 	/*
20289 	 * Form groups if possible.
20290 	 *
20291 	 * If we are supposed to be in a ill_group with a name, insert it
20292 	 * now as we know that at least one ipif is UP. Otherwise form
20293 	 * nameless groups.
20294 	 *
20295 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20296 	 * this ipif into the appropriate interface group, or create a
20297 	 * new one. If this is already in a nameless group, we try to form
20298 	 * a bigger group looking at other ills potentially sharing this
20299 	 * ipif's prefix.
20300 	 */
20301 	phyi = ill->ill_phyint;
20302 	if (phyi->phyint_groupname_len != 0) {
20303 		ASSERT(phyi->phyint_groupname != NULL);
20304 		if (ill->ill_ipif_up_count == 1) {
20305 			ASSERT(ill->ill_group == NULL);
20306 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20307 			    phyi->phyint_groupname, NULL, B_TRUE);
20308 			if (err != 0) {
20309 				ip1dbg(("ipif_up_done: illgrp allocation "
20310 				    "failed, error %d\n", err));
20311 				goto bad;
20312 			}
20313 		}
20314 		ASSERT(ill->ill_group != NULL);
20315 	}
20316 
20317 	/*
20318 	 * When this is part of group, we need to make sure that
20319 	 * any broadcast ires created because of this ipif coming
20320 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20321 	 * so that we don't receive duplicate broadcast packets.
20322 	 */
20323 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20324 		ipif_renominate_bcast(ipif);
20325 
20326 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20327 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20328 	ipif_saved_irep = ipif_recover_ire(ipif);
20329 
20330 	if (!loopback) {
20331 		/*
20332 		 * If the broadcast address has been set, make sure it makes
20333 		 * sense based on the interface address.
20334 		 * Only match on ill since we are sharing broadcast addresses.
20335 		 */
20336 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20337 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20338 			ire_t	*ire;
20339 
20340 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20341 			    IRE_BROADCAST, ipif, ALL_ZONES,
20342 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20343 
20344 			if (ire == NULL) {
20345 				/*
20346 				 * If there isn't a matching broadcast IRE,
20347 				 * revert to the default for this netmask.
20348 				 */
20349 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20350 				mutex_enter(&ipif->ipif_ill->ill_lock);
20351 				ipif_set_default(ipif);
20352 				mutex_exit(&ipif->ipif_ill->ill_lock);
20353 			} else {
20354 				ire_refrele(ire);
20355 			}
20356 		}
20357 
20358 	}
20359 
20360 	/* This is the first interface on this ill */
20361 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20362 		/*
20363 		 * Need to recover all multicast memberships in the driver.
20364 		 * This had to be deferred until we had attached.
20365 		 */
20366 		ill_recover_multicast(ill);
20367 	}
20368 	/* Join the allhosts multicast address */
20369 	ipif_multicast_up(ipif);
20370 
20371 	if (!loopback) {
20372 		/*
20373 		 * See whether anybody else would benefit from the
20374 		 * new ipif that we added. We call this always rather
20375 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20376 		 * ipif is for the benefit of illgrp_insert (done above)
20377 		 * which does not do source address selection as it does
20378 		 * not want to re-create interface routes that we are
20379 		 * having reference to it here.
20380 		 */
20381 		ill_update_source_selection(ill);
20382 	}
20383 
20384 	for (irep1 = irep; irep1 > ire_array; ) {
20385 		irep1--;
20386 		if (*irep1 != NULL) {
20387 			/* was held in ire_add */
20388 			ire_refrele(*irep1);
20389 		}
20390 	}
20391 
20392 	cnt = ipif_saved_ire_cnt;
20393 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20394 		if (*irep1 != NULL) {
20395 			/* was held in ire_add */
20396 			ire_refrele(*irep1);
20397 		}
20398 	}
20399 
20400 	if (!loopback && ipif->ipif_addr_ready) {
20401 		/* Broadcast an address mask reply. */
20402 		ipif_mask_reply(ipif);
20403 	}
20404 	if (ipif_saved_irep != NULL) {
20405 		kmem_free(ipif_saved_irep,
20406 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20407 	}
20408 	if (src_ipif_held)
20409 		ipif_refrele(src_ipif);
20410 
20411 	/*
20412 	 * This had to be deferred until we had bound.  Tell routing sockets and
20413 	 * others that this interface is up if it looks like the address has
20414 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20415 	 * duplicate address detection to do its thing.
20416 	 */
20417 	if (ipif->ipif_addr_ready) {
20418 		ip_rts_ifmsg(ipif);
20419 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20420 		/* Let SCTP update the status for this ipif */
20421 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20422 	}
20423 	return (0);
20424 
20425 bad:
20426 	ip1dbg(("ipif_up_done: FAILED \n"));
20427 	/*
20428 	 * We don't have to bother removing from ill groups because
20429 	 *
20430 	 * 1) For groups with names, we insert only when the first ipif
20431 	 *    comes up. In that case if it fails, it will not be in any
20432 	 *    group. So, we need not try to remove for that case.
20433 	 *
20434 	 * 2) For groups without names, either we tried to insert ipif_ill
20435 	 *    in a group as singleton or found some other group to become
20436 	 *    a bigger group. For the former, if it fails we don't have
20437 	 *    anything to do as ipif_ill is not in the group and for the
20438 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20439 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20440 	 */
20441 	while (irep > ire_array) {
20442 		irep--;
20443 		if (*irep != NULL) {
20444 			ire_delete(*irep);
20445 			if (ire_added)
20446 				ire_refrele(*irep);
20447 		}
20448 	}
20449 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20450 
20451 	if (ipif_saved_irep != NULL) {
20452 		kmem_free(ipif_saved_irep,
20453 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20454 	}
20455 	if (src_ipif_held)
20456 		ipif_refrele(src_ipif);
20457 
20458 	ipif_arp_down(ipif);
20459 	return (err);
20460 }
20461 
20462 /*
20463  * Turn off the ARP with the ILLF_NOARP flag.
20464  */
20465 static int
20466 ill_arp_off(ill_t *ill)
20467 {
20468 	mblk_t	*arp_off_mp = NULL;
20469 	mblk_t	*arp_on_mp = NULL;
20470 
20471 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20472 
20473 	ASSERT(IAM_WRITER_ILL(ill));
20474 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20475 
20476 	/*
20477 	 * If the on message is still around we've already done
20478 	 * an arp_off without doing an arp_on thus there is no
20479 	 * work needed.
20480 	 */
20481 	if (ill->ill_arp_on_mp != NULL)
20482 		return (0);
20483 
20484 	/*
20485 	 * Allocate an ARP on message (to be saved) and an ARP off message
20486 	 */
20487 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20488 	if (!arp_off_mp)
20489 		return (ENOMEM);
20490 
20491 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20492 	if (!arp_on_mp)
20493 		goto failed;
20494 
20495 	ASSERT(ill->ill_arp_on_mp == NULL);
20496 	ill->ill_arp_on_mp = arp_on_mp;
20497 
20498 	/* Send an AR_INTERFACE_OFF request */
20499 	putnext(ill->ill_rq, arp_off_mp);
20500 	return (0);
20501 failed:
20502 
20503 	if (arp_off_mp)
20504 		freemsg(arp_off_mp);
20505 	return (ENOMEM);
20506 }
20507 
20508 /*
20509  * Turn on ARP by turning off the ILLF_NOARP flag.
20510  */
20511 static int
20512 ill_arp_on(ill_t *ill)
20513 {
20514 	mblk_t	*mp;
20515 
20516 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20517 
20518 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20519 
20520 	ASSERT(IAM_WRITER_ILL(ill));
20521 	/*
20522 	 * Send an AR_INTERFACE_ON request if we have already done
20523 	 * an arp_off (which allocated the message).
20524 	 */
20525 	if (ill->ill_arp_on_mp != NULL) {
20526 		mp = ill->ill_arp_on_mp;
20527 		ill->ill_arp_on_mp = NULL;
20528 		putnext(ill->ill_rq, mp);
20529 	}
20530 	return (0);
20531 }
20532 
20533 /*
20534  * Called after either deleting ill from the group or when setting
20535  * FAILED or STANDBY on the interface.
20536  */
20537 static void
20538 illgrp_reset_schednext(ill_t *ill)
20539 {
20540 	ill_group_t *illgrp;
20541 	ill_t *save_ill;
20542 
20543 	ASSERT(IAM_WRITER_ILL(ill));
20544 	/*
20545 	 * When called from illgrp_delete, ill_group will be non-NULL.
20546 	 * But when called from ip_sioctl_flags, it could be NULL if
20547 	 * somebody is setting FAILED/INACTIVE on some interface which
20548 	 * is not part of a group.
20549 	 */
20550 	illgrp = ill->ill_group;
20551 	if (illgrp == NULL)
20552 		return;
20553 	if (illgrp->illgrp_ill_schednext != ill)
20554 		return;
20555 
20556 	illgrp->illgrp_ill_schednext = NULL;
20557 	save_ill = ill;
20558 	/*
20559 	 * Choose a good ill to be the next one for
20560 	 * outbound traffic. As the flags FAILED/STANDBY is
20561 	 * not yet marked when called from ip_sioctl_flags,
20562 	 * we check for ill separately.
20563 	 */
20564 	for (ill = illgrp->illgrp_ill; ill != NULL;
20565 	    ill = ill->ill_group_next) {
20566 		if ((ill != save_ill) &&
20567 		    !(ill->ill_phyint->phyint_flags &
20568 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20569 			illgrp->illgrp_ill_schednext = ill;
20570 			return;
20571 		}
20572 	}
20573 }
20574 
20575 /*
20576  * Given an ill, find the next ill in the group to be scheduled.
20577  * (This should be called by ip_newroute() before ire_create().)
20578  * The passed in ill may be pulled out of the group, after we have picked
20579  * up a different outgoing ill from the same group. However ire add will
20580  * atomically check this.
20581  */
20582 ill_t *
20583 illgrp_scheduler(ill_t *ill)
20584 {
20585 	ill_t *retill;
20586 	ill_group_t *illgrp;
20587 	int illcnt;
20588 	int i;
20589 	uint64_t flags;
20590 	ip_stack_t	*ipst = ill->ill_ipst;
20591 
20592 	/*
20593 	 * We don't use a lock to check for the ill_group. If this ill
20594 	 * is currently being inserted we may end up just returning this
20595 	 * ill itself. That is ok.
20596 	 */
20597 	if (ill->ill_group == NULL) {
20598 		ill_refhold(ill);
20599 		return (ill);
20600 	}
20601 
20602 	/*
20603 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20604 	 * a set of stable ills. No ill can be added or deleted or change
20605 	 * group while we hold the reader lock.
20606 	 */
20607 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20608 	if ((illgrp = ill->ill_group) == NULL) {
20609 		rw_exit(&ipst->ips_ill_g_lock);
20610 		ill_refhold(ill);
20611 		return (ill);
20612 	}
20613 
20614 	illcnt = illgrp->illgrp_ill_count;
20615 	mutex_enter(&illgrp->illgrp_lock);
20616 	retill = illgrp->illgrp_ill_schednext;
20617 
20618 	if (retill == NULL)
20619 		retill = illgrp->illgrp_ill;
20620 
20621 	/*
20622 	 * We do a circular search beginning at illgrp_ill_schednext
20623 	 * or illgrp_ill. We don't check the flags against the ill lock
20624 	 * since it can change anytime. The ire creation will be atomic
20625 	 * and will fail if the ill is FAILED or OFFLINE.
20626 	 */
20627 	for (i = 0; i < illcnt; i++) {
20628 		flags = retill->ill_phyint->phyint_flags;
20629 
20630 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20631 		    ILL_CAN_LOOKUP(retill)) {
20632 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20633 			ill_refhold(retill);
20634 			break;
20635 		}
20636 		retill = retill->ill_group_next;
20637 		if (retill == NULL)
20638 			retill = illgrp->illgrp_ill;
20639 	}
20640 	mutex_exit(&illgrp->illgrp_lock);
20641 	rw_exit(&ipst->ips_ill_g_lock);
20642 
20643 	return (i == illcnt ? NULL : retill);
20644 }
20645 
20646 /*
20647  * Checks for availbility of a usable source address (if there is one) when the
20648  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20649  * this selection is done regardless of the destination.
20650  */
20651 boolean_t
20652 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20653 {
20654 	uint_t	ifindex;
20655 	ipif_t	*ipif = NULL;
20656 	ill_t	*uill;
20657 	boolean_t isv6;
20658 	ip_stack_t	*ipst = ill->ill_ipst;
20659 
20660 	ASSERT(ill != NULL);
20661 
20662 	isv6 = ill->ill_isv6;
20663 	ifindex = ill->ill_usesrc_ifindex;
20664 	if (ifindex != 0) {
20665 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20666 		    NULL, ipst);
20667 		if (uill == NULL)
20668 			return (NULL);
20669 		mutex_enter(&uill->ill_lock);
20670 		for (ipif = uill->ill_ipif; ipif != NULL;
20671 		    ipif = ipif->ipif_next) {
20672 			if (!IPIF_CAN_LOOKUP(ipif))
20673 				continue;
20674 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20675 				continue;
20676 			if (!(ipif->ipif_flags & IPIF_UP))
20677 				continue;
20678 			if (ipif->ipif_zoneid != zoneid)
20679 				continue;
20680 			if ((isv6 &&
20681 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20682 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20683 				continue;
20684 			mutex_exit(&uill->ill_lock);
20685 			ill_refrele(uill);
20686 			return (B_TRUE);
20687 		}
20688 		mutex_exit(&uill->ill_lock);
20689 		ill_refrele(uill);
20690 	}
20691 	return (B_FALSE);
20692 }
20693 
20694 /*
20695  * Determine the best source address given a destination address and an ill.
20696  * Prefers non-deprecated over deprecated but will return a deprecated
20697  * address if there is no other choice. If there is a usable source address
20698  * on the interface pointed to by ill_usesrc_ifindex then that is given
20699  * first preference.
20700  *
20701  * Returns NULL if there is no suitable source address for the ill.
20702  * This only occurs when there is no valid source address for the ill.
20703  */
20704 ipif_t *
20705 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20706 {
20707 	ipif_t *ipif;
20708 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20709 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20710 	int index = 0;
20711 	boolean_t wrapped = B_FALSE;
20712 	boolean_t same_subnet_only = B_FALSE;
20713 	boolean_t ipif_same_found, ipif_other_found;
20714 	boolean_t specific_found;
20715 	ill_t	*till, *usill = NULL;
20716 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20717 	ip_stack_t	*ipst = ill->ill_ipst;
20718 
20719 	if (ill->ill_usesrc_ifindex != 0) {
20720 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20721 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20722 		if (usill != NULL)
20723 			ill = usill;	/* Select source from usesrc ILL */
20724 		else
20725 			return (NULL);
20726 	}
20727 
20728 	/*
20729 	 * If we're dealing with an unlabeled destination on a labeled system,
20730 	 * make sure that we ignore source addresses that are incompatible with
20731 	 * the destination's default label.  That destination's default label
20732 	 * must dominate the minimum label on the source address.
20733 	 */
20734 	dst_rhtp = NULL;
20735 	if (is_system_labeled()) {
20736 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20737 		if (dst_rhtp == NULL)
20738 			return (NULL);
20739 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20740 			TPC_RELE(dst_rhtp);
20741 			dst_rhtp = NULL;
20742 		}
20743 	}
20744 
20745 	/*
20746 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20747 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20748 	 * After selecting the right ipif, under ill_lock make sure ipif is
20749 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20750 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20751 	 * but not under a lock.
20752 	 */
20753 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20754 
20755 retry:
20756 	till = ill;
20757 	ipif_arr[0] = NULL;
20758 
20759 	if (till->ill_group != NULL)
20760 		till = till->ill_group->illgrp_ill;
20761 
20762 	/*
20763 	 * Choose one good source address from each ill across the group.
20764 	 * If possible choose a source address in the same subnet as
20765 	 * the destination address.
20766 	 *
20767 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20768 	 * This is okay because of the following.
20769 	 *
20770 	 *    If PHYI_FAILED is set and we still have non-deprecated
20771 	 *    addresses, it means the addresses have not yet been
20772 	 *    failed over to a different interface. We potentially
20773 	 *    select them to create IRE_CACHES, which will be later
20774 	 *    flushed when the addresses move over.
20775 	 *
20776 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20777 	 *    addresses, it means either the user has configured them
20778 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20779 	 *    been moved over. For the former, in.mpathd does a failover
20780 	 *    when the interface becomes INACTIVE and hence we should
20781 	 *    not find them. Once INACTIVE is set, we don't allow them
20782 	 *    to create logical interfaces anymore. For the latter, a
20783 	 *    flush will happen when INACTIVE is cleared which will
20784 	 *    flush the IRE_CACHES.
20785 	 *
20786 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20787 	 *    over soon. We potentially select them to create IRE_CACHEs,
20788 	 *    which will be later flushed when the addresses move over.
20789 	 *
20790 	 * NOTE : As ipif_select_source is called to borrow source address
20791 	 * for an ipif that is part of a group, source address selection
20792 	 * will be re-done whenever the group changes i.e either an
20793 	 * insertion/deletion in the group.
20794 	 *
20795 	 * Fill ipif_arr[] with source addresses, using these rules:
20796 	 *
20797 	 *	1. At most one source address from a given ill ends up
20798 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20799 	 *	   associated with a given ill ends up in ipif_arr[].
20800 	 *
20801 	 *	2. If there is at least one non-deprecated ipif in the
20802 	 *	   IPMP group with a source address on the same subnet as
20803 	 *	   our destination, then fill ipif_arr[] only with
20804 	 *	   source addresses on the same subnet as our destination.
20805 	 *	   Note that because of (1), only the first
20806 	 *	   non-deprecated ipif found with a source address
20807 	 *	   matching the destination ends up in ipif_arr[].
20808 	 *
20809 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20810 	 *	   addresses not in the same subnet as our destination.
20811 	 *	   Again, because of (1), only the first off-subnet source
20812 	 *	   address will be chosen.
20813 	 *
20814 	 *	4. If there are no non-deprecated ipifs, then just use
20815 	 *	   the source address associated with the last deprecated
20816 	 *	   one we find that happens to be on the same subnet,
20817 	 *	   otherwise the first one not in the same subnet.
20818 	 */
20819 	specific_found = B_FALSE;
20820 	for (; till != NULL; till = till->ill_group_next) {
20821 		ipif_same_found = B_FALSE;
20822 		ipif_other_found = B_FALSE;
20823 		for (ipif = till->ill_ipif; ipif != NULL;
20824 		    ipif = ipif->ipif_next) {
20825 			if (!IPIF_CAN_LOOKUP(ipif))
20826 				continue;
20827 			/* Always skip NOLOCAL and ANYCAST interfaces */
20828 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20829 				continue;
20830 			if (!(ipif->ipif_flags & IPIF_UP) ||
20831 			    !ipif->ipif_addr_ready)
20832 				continue;
20833 			if (ipif->ipif_zoneid != zoneid &&
20834 			    ipif->ipif_zoneid != ALL_ZONES)
20835 				continue;
20836 			/*
20837 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20838 			 * but are not valid as source addresses.
20839 			 */
20840 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20841 				continue;
20842 
20843 			/*
20844 			 * Check compatibility of local address for
20845 			 * destination's default label if we're on a labeled
20846 			 * system.  Incompatible addresses can't be used at
20847 			 * all.
20848 			 */
20849 			if (dst_rhtp != NULL) {
20850 				boolean_t incompat;
20851 
20852 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20853 				    IPV4_VERSION, B_FALSE);
20854 				if (src_rhtp == NULL)
20855 					continue;
20856 				incompat =
20857 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20858 				    src_rhtp->tpc_tp.tp_doi !=
20859 				    dst_rhtp->tpc_tp.tp_doi ||
20860 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20861 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20862 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20863 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20864 				TPC_RELE(src_rhtp);
20865 				if (incompat)
20866 					continue;
20867 			}
20868 
20869 			/*
20870 			 * We prefer not to use all all-zones addresses, if we
20871 			 * can avoid it, as they pose problems with unlabeled
20872 			 * destinations.
20873 			 */
20874 			if (ipif->ipif_zoneid != ALL_ZONES) {
20875 				if (!specific_found &&
20876 				    (!same_subnet_only ||
20877 				    (ipif->ipif_net_mask & dst) ==
20878 				    ipif->ipif_subnet)) {
20879 					index = 0;
20880 					specific_found = B_TRUE;
20881 					ipif_other_found = B_FALSE;
20882 				}
20883 			} else {
20884 				if (specific_found)
20885 					continue;
20886 			}
20887 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20888 				if (ipif_dep == NULL ||
20889 				    (ipif->ipif_net_mask & dst) ==
20890 				    ipif->ipif_subnet)
20891 					ipif_dep = ipif;
20892 				continue;
20893 			}
20894 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20895 				/* found a source address in the same subnet */
20896 				if (!same_subnet_only) {
20897 					same_subnet_only = B_TRUE;
20898 					index = 0;
20899 				}
20900 				ipif_same_found = B_TRUE;
20901 			} else {
20902 				if (same_subnet_only || ipif_other_found)
20903 					continue;
20904 				ipif_other_found = B_TRUE;
20905 			}
20906 			ipif_arr[index++] = ipif;
20907 			if (index == MAX_IPIF_SELECT_SOURCE) {
20908 				wrapped = B_TRUE;
20909 				index = 0;
20910 			}
20911 			if (ipif_same_found)
20912 				break;
20913 		}
20914 	}
20915 
20916 	if (ipif_arr[0] == NULL) {
20917 		ipif = ipif_dep;
20918 	} else {
20919 		if (wrapped)
20920 			index = MAX_IPIF_SELECT_SOURCE;
20921 		ipif = ipif_arr[ipif_rand(ipst) % index];
20922 		ASSERT(ipif != NULL);
20923 	}
20924 
20925 	if (ipif != NULL) {
20926 		mutex_enter(&ipif->ipif_ill->ill_lock);
20927 		if (!IPIF_CAN_LOOKUP(ipif)) {
20928 			mutex_exit(&ipif->ipif_ill->ill_lock);
20929 			goto retry;
20930 		}
20931 		ipif_refhold_locked(ipif);
20932 		mutex_exit(&ipif->ipif_ill->ill_lock);
20933 	}
20934 
20935 	rw_exit(&ipst->ips_ill_g_lock);
20936 	if (usill != NULL)
20937 		ill_refrele(usill);
20938 	if (dst_rhtp != NULL)
20939 		TPC_RELE(dst_rhtp);
20940 
20941 #ifdef DEBUG
20942 	if (ipif == NULL) {
20943 		char buf1[INET6_ADDRSTRLEN];
20944 
20945 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20946 		    ill->ill_name,
20947 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20948 	} else {
20949 		char buf1[INET6_ADDRSTRLEN];
20950 		char buf2[INET6_ADDRSTRLEN];
20951 
20952 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20953 		    ipif->ipif_ill->ill_name,
20954 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20955 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20956 		    buf2, sizeof (buf2))));
20957 	}
20958 #endif /* DEBUG */
20959 	return (ipif);
20960 }
20961 
20962 
20963 /*
20964  * If old_ipif is not NULL, see if ipif was derived from old
20965  * ipif and if so, recreate the interface route by re-doing
20966  * source address selection. This happens when ipif_down ->
20967  * ipif_update_other_ipifs calls us.
20968  *
20969  * If old_ipif is NULL, just redo the source address selection
20970  * if needed. This happens when illgrp_insert or ipif_up_done
20971  * calls us.
20972  */
20973 static void
20974 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20975 {
20976 	ire_t *ire;
20977 	ire_t *ipif_ire;
20978 	queue_t *stq;
20979 	ipif_t *nipif;
20980 	ill_t *ill;
20981 	boolean_t need_rele = B_FALSE;
20982 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20983 
20984 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20985 	ASSERT(IAM_WRITER_IPIF(ipif));
20986 
20987 	ill = ipif->ipif_ill;
20988 	if (!(ipif->ipif_flags &
20989 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20990 		/*
20991 		 * Can't possibly have borrowed the source
20992 		 * from old_ipif.
20993 		 */
20994 		return;
20995 	}
20996 
20997 	/*
20998 	 * Is there any work to be done? No work if the address
20999 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21000 	 * ipif_select_source() does not borrow addresses from
21001 	 * NOLOCAL and ANYCAST interfaces).
21002 	 */
21003 	if ((old_ipif != NULL) &&
21004 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21005 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21006 	    (old_ipif->ipif_flags &
21007 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21008 		return;
21009 	}
21010 
21011 	/*
21012 	 * Perform the same checks as when creating the
21013 	 * IRE_INTERFACE in ipif_up_done.
21014 	 */
21015 	if (!(ipif->ipif_flags & IPIF_UP))
21016 		return;
21017 
21018 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21019 	    (ipif->ipif_subnet == INADDR_ANY))
21020 		return;
21021 
21022 	ipif_ire = ipif_to_ire(ipif);
21023 	if (ipif_ire == NULL)
21024 		return;
21025 
21026 	/*
21027 	 * We know that ipif uses some other source for its
21028 	 * IRE_INTERFACE. Is it using the source of this
21029 	 * old_ipif?
21030 	 */
21031 	if (old_ipif != NULL &&
21032 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21033 		ire_refrele(ipif_ire);
21034 		return;
21035 	}
21036 	if (ip_debug > 2) {
21037 		/* ip1dbg */
21038 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21039 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21040 	}
21041 
21042 	stq = ipif_ire->ire_stq;
21043 
21044 	/*
21045 	 * Can't use our source address. Select a different
21046 	 * source address for the IRE_INTERFACE.
21047 	 */
21048 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21049 	if (nipif == NULL) {
21050 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21051 		nipif = ipif;
21052 	} else {
21053 		need_rele = B_TRUE;
21054 	}
21055 
21056 	ire = ire_create(
21057 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21058 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21059 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21060 	    NULL,				/* no gateway */
21061 	    &ipif->ipif_mtu,			/* max frag */
21062 	    NULL,				/* no src nce */
21063 	    NULL,				/* no recv from queue */
21064 	    stq,				/* send-to queue */
21065 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21066 	    ipif,
21067 	    0,
21068 	    0,
21069 	    0,
21070 	    0,
21071 	    &ire_uinfo_null,
21072 	    NULL,
21073 	    NULL,
21074 	    ipst);
21075 
21076 	if (ire != NULL) {
21077 		ire_t *ret_ire;
21078 		int error;
21079 
21080 		/*
21081 		 * We don't need ipif_ire anymore. We need to delete
21082 		 * before we add so that ire_add does not detect
21083 		 * duplicates.
21084 		 */
21085 		ire_delete(ipif_ire);
21086 		ret_ire = ire;
21087 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21088 		ASSERT(error == 0);
21089 		ASSERT(ire == ret_ire);
21090 		/* Held in ire_add */
21091 		ire_refrele(ret_ire);
21092 	}
21093 	/*
21094 	 * Either we are falling through from above or could not
21095 	 * allocate a replacement.
21096 	 */
21097 	ire_refrele(ipif_ire);
21098 	if (need_rele)
21099 		ipif_refrele(nipif);
21100 }
21101 
21102 /*
21103  * This old_ipif is going away.
21104  *
21105  * Determine if any other ipif's is using our address as
21106  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21107  * IPIF_DEPRECATED).
21108  * Find the IRE_INTERFACE for such ipifs and recreate them
21109  * to use an different source address following the rules in
21110  * ipif_up_done.
21111  *
21112  * This function takes an illgrp as an argument so that illgrp_delete
21113  * can call this to update source address even after deleting the
21114  * old_ipif->ipif_ill from the ill group.
21115  */
21116 static void
21117 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21118 {
21119 	ipif_t *ipif;
21120 	ill_t *ill;
21121 	char	buf[INET6_ADDRSTRLEN];
21122 
21123 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21124 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21125 
21126 	ill = old_ipif->ipif_ill;
21127 
21128 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21129 	    ill->ill_name,
21130 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21131 	    buf, sizeof (buf))));
21132 	/*
21133 	 * If this part of a group, look at all ills as ipif_select_source
21134 	 * borrows source address across all the ills in the group.
21135 	 */
21136 	if (illgrp != NULL)
21137 		ill = illgrp->illgrp_ill;
21138 
21139 	for (; ill != NULL; ill = ill->ill_group_next) {
21140 		for (ipif = ill->ill_ipif; ipif != NULL;
21141 		    ipif = ipif->ipif_next) {
21142 
21143 			if (ipif == old_ipif)
21144 				continue;
21145 
21146 			ipif_recreate_interface_routes(old_ipif, ipif);
21147 		}
21148 	}
21149 }
21150 
21151 /* ARGSUSED */
21152 int
21153 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21154 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21155 {
21156 	/*
21157 	 * ill_phyint_reinit merged the v4 and v6 into a single
21158 	 * ipsq. Could also have become part of a ipmp group in the
21159 	 * process, and we might not have been able to complete the
21160 	 * operation in ipif_set_values, if we could not become
21161 	 * exclusive.  If so restart it here.
21162 	 */
21163 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21164 }
21165 
21166 
21167 /*
21168  * Can operate on either a module or a driver queue.
21169  * Returns an error if not a module queue.
21170  */
21171 /* ARGSUSED */
21172 int
21173 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21174     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21175 {
21176 	queue_t		*q1 = q;
21177 	char 		*cp;
21178 	char		interf_name[LIFNAMSIZ];
21179 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21180 
21181 	if (q->q_next == NULL) {
21182 		ip1dbg((
21183 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21184 		return (EINVAL);
21185 	}
21186 
21187 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21188 		return (EALREADY);
21189 
21190 	do {
21191 		q1 = q1->q_next;
21192 	} while (q1->q_next);
21193 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21194 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21195 
21196 	/*
21197 	 * Here we are not going to delay the ioack until after
21198 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21199 	 * original ioctl message before sending the requests.
21200 	 */
21201 	return (ipif_set_values(q, mp, interf_name, &ppa));
21202 }
21203 
21204 /* ARGSUSED */
21205 int
21206 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21207     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21208 {
21209 	return (ENXIO);
21210 }
21211 
21212 /*
21213  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21214  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21215  * ire_check_and_create_bcast()).
21216  */
21217 static ire_t **
21218 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21219 {
21220 	ipaddr_t addr;
21221 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21222 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21223 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21224 
21225 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21226 
21227 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21228 
21229 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21230 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21231 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21232 
21233 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21234 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21235 
21236 	/*
21237 	 * For backward compatibility, we create net broadcast IREs based on
21238 	 * the old "IP address class system", since some old machines only
21239 	 * respond to these class derived net broadcast.  However, we must not
21240 	 * create these net broadcast IREs if the subnetmask is shorter than
21241 	 * the IP address class based derived netmask.  Otherwise, we may
21242 	 * create a net broadcast address which is the same as an IP address
21243 	 * on the subnet -- and then TCP will refuse to talk to that address.
21244 	 */
21245 	if (netmask < subnetmask) {
21246 		addr = netmask & ipif->ipif_subnet;
21247 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21248 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21249 		    flags);
21250 	}
21251 
21252 	/*
21253 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21254 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21255 	 * created.  Creating these broadcast IREs will only create confusion
21256 	 * as `addr' will be the same as the IP address.
21257 	 */
21258 	if (subnetmask != 0xFFFFFFFF) {
21259 		addr = ipif->ipif_subnet;
21260 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21261 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21262 		    irep, flags);
21263 	}
21264 
21265 	return (irep);
21266 }
21267 
21268 /*
21269  * Broadcast IRE info structure used in the functions below.  Since we
21270  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21271  */
21272 typedef struct bcast_ireinfo {
21273 	uchar_t		bi_type;	/* BCAST_* value from below */
21274 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21275 			bi_needrep:1,	/* do we need to replace it? */
21276 			bi_haverep:1,	/* have we replaced it? */
21277 			bi_pad:5;
21278 	ipaddr_t	bi_addr;	/* IRE address */
21279 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21280 } bcast_ireinfo_t;
21281 
21282 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21283 
21284 /*
21285  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21286  * return B_TRUE if it should immediately be used to recreate the IRE.
21287  */
21288 static boolean_t
21289 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21290 {
21291 	ipaddr_t addr;
21292 
21293 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21294 
21295 	switch (bireinfop->bi_type) {
21296 	case BCAST_NET:
21297 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21298 		if (addr != bireinfop->bi_addr)
21299 			return (B_FALSE);
21300 		break;
21301 	case BCAST_SUBNET:
21302 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21303 			return (B_FALSE);
21304 		break;
21305 	}
21306 
21307 	bireinfop->bi_needrep = 1;
21308 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21309 		if (bireinfop->bi_backup == NULL)
21310 			bireinfop->bi_backup = ipif;
21311 		return (B_FALSE);
21312 	}
21313 	return (B_TRUE);
21314 }
21315 
21316 /*
21317  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21318  * them ala ire_check_and_create_bcast().
21319  */
21320 static ire_t **
21321 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21322 {
21323 	ipaddr_t mask, addr;
21324 
21325 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21326 
21327 	addr = bireinfop->bi_addr;
21328 	irep = ire_create_bcast(ipif, addr, irep);
21329 
21330 	switch (bireinfop->bi_type) {
21331 	case BCAST_NET:
21332 		mask = ip_net_mask(ipif->ipif_subnet);
21333 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21334 		break;
21335 	case BCAST_SUBNET:
21336 		mask = ipif->ipif_net_mask;
21337 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21338 		break;
21339 	}
21340 
21341 	bireinfop->bi_haverep = 1;
21342 	return (irep);
21343 }
21344 
21345 /*
21346  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21347  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21348  * that are going away are still needed.  If so, have ipif_create_bcast()
21349  * recreate them (except for the deprecated case, as explained below).
21350  */
21351 static ire_t **
21352 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21353     ire_t **irep)
21354 {
21355 	int i;
21356 	ipif_t *ipif;
21357 
21358 	ASSERT(!ill->ill_isv6);
21359 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21360 		/*
21361 		 * Skip this ipif if it's (a) the one being taken down, (b)
21362 		 * not in the same zone, or (c) has no valid local address.
21363 		 */
21364 		if (ipif == test_ipif ||
21365 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21366 		    ipif->ipif_subnet == 0 ||
21367 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21368 		    (IPIF_UP|IPIF_BROADCAST))
21369 			continue;
21370 
21371 		/*
21372 		 * For each dying IRE that hasn't yet been replaced, see if
21373 		 * `ipif' needs it and whether the IRE should be recreated on
21374 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21375 		 * will return B_FALSE even if `ipif' needs the IRE on the
21376 		 * hopes that we'll later find a needy non-deprecated ipif.
21377 		 * However, the ipif is recorded in bi_backup for possible
21378 		 * subsequent use by ipif_check_bcast_ires().
21379 		 */
21380 		for (i = 0; i < BCAST_COUNT; i++) {
21381 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21382 				continue;
21383 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21384 				continue;
21385 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21386 		}
21387 
21388 		/*
21389 		 * If we've replaced all of the broadcast IREs that are going
21390 		 * to be taken down, we know we're done.
21391 		 */
21392 		for (i = 0; i < BCAST_COUNT; i++) {
21393 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21394 				break;
21395 		}
21396 		if (i == BCAST_COUNT)
21397 			break;
21398 	}
21399 	return (irep);
21400 }
21401 
21402 /*
21403  * Check if `test_ipif' (which is going away) is associated with any existing
21404  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21405  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21406  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21407  *
21408  * This is necessary because broadcast IREs are shared.  In particular, a
21409  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21410  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21411  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21412  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21413  * same zone, they will share the same set of broadcast IREs.
21414  *
21415  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21416  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21417  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21418  */
21419 static void
21420 ipif_check_bcast_ires(ipif_t *test_ipif)
21421 {
21422 	ill_t		*ill = test_ipif->ipif_ill;
21423 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21424 	ire_t		**irep1, **irep = &ire_array[0];
21425 	uint_t 		i, willdie;
21426 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21427 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21428 
21429 	ASSERT(!test_ipif->ipif_isv6);
21430 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21431 
21432 	/*
21433 	 * No broadcast IREs for the LOOPBACK interface
21434 	 * or others such as point to point and IPIF_NOXMIT.
21435 	 */
21436 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21437 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21438 		return;
21439 
21440 	bzero(bireinfo, sizeof (bireinfo));
21441 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21442 	bireinfo[0].bi_addr = 0;
21443 
21444 	bireinfo[1].bi_type = BCAST_ALLONES;
21445 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21446 
21447 	bireinfo[2].bi_type = BCAST_NET;
21448 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21449 
21450 	if (test_ipif->ipif_net_mask != 0)
21451 		mask = test_ipif->ipif_net_mask;
21452 	bireinfo[3].bi_type = BCAST_SUBNET;
21453 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21454 
21455 	/*
21456 	 * Figure out what (if any) broadcast IREs will die as a result of
21457 	 * `test_ipif' going away.  If none will die, we're done.
21458 	 */
21459 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21460 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21461 		    test_ipif, ALL_ZONES, NULL,
21462 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21463 		if (ire != NULL) {
21464 			willdie++;
21465 			bireinfo[i].bi_willdie = 1;
21466 			ire_refrele(ire);
21467 		}
21468 	}
21469 
21470 	if (willdie == 0)
21471 		return;
21472 
21473 	/*
21474 	 * Walk through all the ipifs that will be affected by the dying IREs,
21475 	 * and recreate the IREs as necessary.
21476 	 */
21477 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21478 
21479 	/*
21480 	 * Scan through the set of broadcast IREs and see if there are any
21481 	 * that we need to replace that have not yet been replaced.  If so,
21482 	 * replace them using the appropriate backup ipif.
21483 	 */
21484 	for (i = 0; i < BCAST_COUNT; i++) {
21485 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21486 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21487 			    &bireinfo[i], irep);
21488 	}
21489 
21490 	/*
21491 	 * If we can't create all of them, don't add any of them.  (Code in
21492 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21493 	 * non-loopback copy and loopback copy for a given address.)
21494 	 */
21495 	for (irep1 = irep; irep1 > ire_array; ) {
21496 		irep1--;
21497 		if (*irep1 == NULL) {
21498 			ip0dbg(("ipif_check_bcast_ires: can't create "
21499 			    "IRE_BROADCAST, memory allocation failure\n"));
21500 			while (irep > ire_array) {
21501 				irep--;
21502 				if (*irep != NULL)
21503 					ire_delete(*irep);
21504 			}
21505 			return;
21506 		}
21507 	}
21508 
21509 	for (irep1 = irep; irep1 > ire_array; ) {
21510 		irep1--;
21511 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21512 			ire_refrele(*irep1);		/* Held in ire_add */
21513 	}
21514 }
21515 
21516 /*
21517  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21518  * from lifr_flags and the name from lifr_name.
21519  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21520  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21521  * Returns EINPROGRESS when mp has been consumed by queueing it on
21522  * ill_pending_mp and the ioctl will complete in ip_rput.
21523  *
21524  * Can operate on either a module or a driver queue.
21525  * Returns an error if not a module queue.
21526  */
21527 /* ARGSUSED */
21528 int
21529 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21530     ip_ioctl_cmd_t *ipip, void *if_req)
21531 {
21532 	int	err;
21533 	ill_t	*ill;
21534 	struct lifreq *lifr = (struct lifreq *)if_req;
21535 
21536 	ASSERT(ipif != NULL);
21537 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21538 
21539 	if (q->q_next == NULL) {
21540 		ip1dbg((
21541 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21542 		return (EINVAL);
21543 	}
21544 
21545 	ill = (ill_t *)q->q_ptr;
21546 	/*
21547 	 * If we are not writer on 'q' then this interface exists already
21548 	 * and previous lookups (ipif_extract_lifreq()) found this ipif.
21549 	 * So return EALREADY
21550 	 */
21551 	if (ill != ipif->ipif_ill)
21552 		return (EALREADY);
21553 
21554 	if (ill->ill_name[0] != '\0')
21555 		return (EALREADY);
21556 
21557 	/*
21558 	 * Set all the flags. Allows all kinds of override. Provide some
21559 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21560 	 * unless there is either multicast/broadcast support in the driver
21561 	 * or it is a pt-pt link.
21562 	 */
21563 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21564 		/* Meaningless to IP thus don't allow them to be set. */
21565 		ip1dbg(("ip_setname: EINVAL 1\n"));
21566 		return (EINVAL);
21567 	}
21568 	/*
21569 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21570 	 * ill_bcast_addr_length info.
21571 	 */
21572 	if (!ill->ill_needs_attach &&
21573 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21574 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21575 	    ill->ill_bcast_addr_length == 0)) {
21576 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21577 		ip1dbg(("ip_setname: EINVAL 2\n"));
21578 		return (EINVAL);
21579 	}
21580 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21581 	    ((lifr->lifr_flags & IFF_IPV6) ||
21582 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21583 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21584 		ip1dbg(("ip_setname: EINVAL 3\n"));
21585 		return (EINVAL);
21586 	}
21587 	if (lifr->lifr_flags & IFF_UP) {
21588 		/* Can only be set with SIOCSLIFFLAGS */
21589 		ip1dbg(("ip_setname: EINVAL 4\n"));
21590 		return (EINVAL);
21591 	}
21592 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21593 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21594 		ip1dbg(("ip_setname: EINVAL 5\n"));
21595 		return (EINVAL);
21596 	}
21597 	/*
21598 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21599 	 */
21600 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21601 	    !(lifr->lifr_flags & IFF_IPV6) &&
21602 	    !(ipif->ipif_isv6)) {
21603 		ip1dbg(("ip_setname: EINVAL 6\n"));
21604 		return (EINVAL);
21605 	}
21606 
21607 	/*
21608 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21609 	 * we have all the flags here. So, we assign rather than we OR.
21610 	 * We can't OR the flags here because we don't want to set
21611 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21612 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21613 	 * on lifr_flags value here.
21614 	 */
21615 	/*
21616 	 * This ill has not been inserted into the global list.
21617 	 * So we are still single threaded and don't need any lock
21618 	 */
21619 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21620 	    ~IFF_DUPLICATE;
21621 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21622 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21623 
21624 	/* We started off as V4. */
21625 	if (ill->ill_flags & ILLF_IPV6) {
21626 		ill->ill_phyint->phyint_illv6 = ill;
21627 		ill->ill_phyint->phyint_illv4 = NULL;
21628 	}
21629 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21630 	return (err);
21631 }
21632 
21633 /* ARGSUSED */
21634 int
21635 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21636     ip_ioctl_cmd_t *ipip, void *if_req)
21637 {
21638 	/*
21639 	 * ill_phyint_reinit merged the v4 and v6 into a single
21640 	 * ipsq. Could also have become part of a ipmp group in the
21641 	 * process, and we might not have been able to complete the
21642 	 * slifname in ipif_set_values, if we could not become
21643 	 * exclusive.  If so restart it here
21644 	 */
21645 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21646 }
21647 
21648 /*
21649  * Return a pointer to the ipif which matches the index, IP version type and
21650  * zoneid.
21651  */
21652 ipif_t *
21653 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21654     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21655 {
21656 	ill_t	*ill;
21657 	ipif_t	*ipif = NULL;
21658 
21659 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21660 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21661 
21662 	if (err != NULL)
21663 		*err = 0;
21664 
21665 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21666 	if (ill != NULL) {
21667 		mutex_enter(&ill->ill_lock);
21668 		for (ipif = ill->ill_ipif; ipif != NULL;
21669 		    ipif = ipif->ipif_next) {
21670 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21671 			    zoneid == ipif->ipif_zoneid ||
21672 			    ipif->ipif_zoneid == ALL_ZONES)) {
21673 				ipif_refhold_locked(ipif);
21674 				break;
21675 			}
21676 		}
21677 		mutex_exit(&ill->ill_lock);
21678 		ill_refrele(ill);
21679 		if (ipif == NULL && err != NULL)
21680 			*err = ENXIO;
21681 	}
21682 	return (ipif);
21683 }
21684 
21685 typedef struct conn_change_s {
21686 	uint_t cc_old_ifindex;
21687 	uint_t cc_new_ifindex;
21688 } conn_change_t;
21689 
21690 /*
21691  * ipcl_walk function for changing interface index.
21692  */
21693 static void
21694 conn_change_ifindex(conn_t *connp, caddr_t arg)
21695 {
21696 	conn_change_t *connc;
21697 	uint_t old_ifindex;
21698 	uint_t new_ifindex;
21699 	int i;
21700 	ilg_t *ilg;
21701 
21702 	connc = (conn_change_t *)arg;
21703 	old_ifindex = connc->cc_old_ifindex;
21704 	new_ifindex = connc->cc_new_ifindex;
21705 
21706 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21707 		connp->conn_orig_bound_ifindex = new_ifindex;
21708 
21709 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21710 		connp->conn_orig_multicast_ifindex = new_ifindex;
21711 
21712 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21713 		ilg = &connp->conn_ilg[i];
21714 		if (ilg->ilg_orig_ifindex == old_ifindex)
21715 			ilg->ilg_orig_ifindex = new_ifindex;
21716 	}
21717 }
21718 
21719 /*
21720  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21721  * to new_index if it matches the old_index.
21722  *
21723  * Failovers typically happen within a group of ills. But somebody
21724  * can remove an ill from the group after a failover happened. If
21725  * we are setting the ifindex after this, we potentially need to
21726  * look at all the ills rather than just the ones in the group.
21727  * We cut down the work by looking at matching ill_net_types
21728  * and ill_types as we could not possibly grouped them together.
21729  */
21730 static void
21731 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21732 {
21733 	ill_t *ill;
21734 	ipif_t *ipif;
21735 	uint_t old_ifindex;
21736 	uint_t new_ifindex;
21737 	ilm_t *ilm;
21738 	ill_walk_context_t ctx;
21739 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21740 
21741 	old_ifindex = connc->cc_old_ifindex;
21742 	new_ifindex = connc->cc_new_ifindex;
21743 
21744 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21745 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21746 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21747 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21748 		    (ill_orig->ill_type != ill->ill_type)) {
21749 			continue;
21750 		}
21751 		for (ipif = ill->ill_ipif; ipif != NULL;
21752 		    ipif = ipif->ipif_next) {
21753 			if (ipif->ipif_orig_ifindex == old_ifindex)
21754 				ipif->ipif_orig_ifindex = new_ifindex;
21755 		}
21756 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21757 			if (ilm->ilm_orig_ifindex == old_ifindex)
21758 				ilm->ilm_orig_ifindex = new_ifindex;
21759 		}
21760 	}
21761 	rw_exit(&ipst->ips_ill_g_lock);
21762 }
21763 
21764 /*
21765  * We first need to ensure that the new index is unique, and
21766  * then carry the change across both v4 and v6 ill representation
21767  * of the physical interface.
21768  */
21769 /* ARGSUSED */
21770 int
21771 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21772     ip_ioctl_cmd_t *ipip, void *ifreq)
21773 {
21774 	ill_t		*ill;
21775 	ill_t		*ill_other;
21776 	phyint_t	*phyi;
21777 	int		old_index;
21778 	conn_change_t	connc;
21779 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21780 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21781 	uint_t	index;
21782 	ill_t	*ill_v4;
21783 	ill_t	*ill_v6;
21784 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21785 
21786 	if (ipip->ipi_cmd_type == IF_CMD)
21787 		index = ifr->ifr_index;
21788 	else
21789 		index = lifr->lifr_index;
21790 
21791 	/*
21792 	 * Only allow on physical interface. Also, index zero is illegal.
21793 	 *
21794 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21795 	 *
21796 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21797 	 *    implies a possible failback might have to happen. As failback
21798 	 *    depends on the old index, we should fail setting the index.
21799 	 *
21800 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21801 	 *    any addresses or multicast memberships are failed over to
21802 	 *    a non-STANDBY interface. As failback depends on the old
21803 	 *    index, we should fail setting the index for this case also.
21804 	 *
21805 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21806 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21807 	 */
21808 	ill = ipif->ipif_ill;
21809 	phyi = ill->ill_phyint;
21810 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21811 	    ipif->ipif_id != 0 || index == 0) {
21812 		return (EINVAL);
21813 	}
21814 	old_index = phyi->phyint_ifindex;
21815 
21816 	/* If the index is not changing, no work to do */
21817 	if (old_index == index)
21818 		return (0);
21819 
21820 	/*
21821 	 * Use ill_lookup_on_ifindex to determine if the
21822 	 * new index is unused and if so allow the change.
21823 	 */
21824 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21825 	    ipst);
21826 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21827 	    ipst);
21828 	if (ill_v6 != NULL || ill_v4 != NULL) {
21829 		if (ill_v4 != NULL)
21830 			ill_refrele(ill_v4);
21831 		if (ill_v6 != NULL)
21832 			ill_refrele(ill_v6);
21833 		return (EBUSY);
21834 	}
21835 
21836 	/*
21837 	 * The new index is unused. Set it in the phyint.
21838 	 * Locate the other ill so that we can send a routing
21839 	 * sockets message.
21840 	 */
21841 	if (ill->ill_isv6) {
21842 		ill_other = phyi->phyint_illv4;
21843 	} else {
21844 		ill_other = phyi->phyint_illv6;
21845 	}
21846 
21847 	phyi->phyint_ifindex = index;
21848 
21849 	/* Update SCTP's ILL list */
21850 	sctp_ill_reindex(ill, old_index);
21851 
21852 	connc.cc_old_ifindex = old_index;
21853 	connc.cc_new_ifindex = index;
21854 	ip_change_ifindex(ill, &connc);
21855 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21856 
21857 	/* Send the routing sockets message */
21858 	ip_rts_ifmsg(ipif);
21859 	if (ill_other != NULL)
21860 		ip_rts_ifmsg(ill_other->ill_ipif);
21861 
21862 	return (0);
21863 }
21864 
21865 /* ARGSUSED */
21866 int
21867 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21868     ip_ioctl_cmd_t *ipip, void *ifreq)
21869 {
21870 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21871 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21872 
21873 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21874 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21875 	/* Get the interface index */
21876 	if (ipip->ipi_cmd_type == IF_CMD) {
21877 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21878 	} else {
21879 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21880 	}
21881 	return (0);
21882 }
21883 
21884 /* ARGSUSED */
21885 int
21886 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21887     ip_ioctl_cmd_t *ipip, void *ifreq)
21888 {
21889 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21890 
21891 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21892 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21893 	/* Get the interface zone */
21894 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21895 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21896 	return (0);
21897 }
21898 
21899 /*
21900  * Set the zoneid of an interface.
21901  */
21902 /* ARGSUSED */
21903 int
21904 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21905     ip_ioctl_cmd_t *ipip, void *ifreq)
21906 {
21907 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21908 	int err = 0;
21909 	boolean_t need_up = B_FALSE;
21910 	zone_t *zptr;
21911 	zone_status_t status;
21912 	zoneid_t zoneid;
21913 
21914 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21915 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21916 		if (!is_system_labeled())
21917 			return (ENOTSUP);
21918 		zoneid = GLOBAL_ZONEID;
21919 	}
21920 
21921 	/* cannot assign instance zero to a non-global zone */
21922 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21923 		return (ENOTSUP);
21924 
21925 	/*
21926 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21927 	 * the event of a race with the zone shutdown processing, since IP
21928 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21929 	 * interface will be cleaned up even if the zone is shut down
21930 	 * immediately after the status check. If the interface can't be brought
21931 	 * down right away, and the zone is shut down before the restart
21932 	 * function is called, we resolve the possible races by rechecking the
21933 	 * zone status in the restart function.
21934 	 */
21935 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21936 		return (EINVAL);
21937 	status = zone_status_get(zptr);
21938 	zone_rele(zptr);
21939 
21940 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21941 		return (EINVAL);
21942 
21943 	if (ipif->ipif_flags & IPIF_UP) {
21944 		/*
21945 		 * If the interface is already marked up,
21946 		 * we call ipif_down which will take care
21947 		 * of ditching any IREs that have been set
21948 		 * up based on the old interface address.
21949 		 */
21950 		err = ipif_logical_down(ipif, q, mp);
21951 		if (err == EINPROGRESS)
21952 			return (err);
21953 		ipif_down_tail(ipif);
21954 		need_up = B_TRUE;
21955 	}
21956 
21957 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21958 	return (err);
21959 }
21960 
21961 static int
21962 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21963     queue_t *q, mblk_t *mp, boolean_t need_up)
21964 {
21965 	int	err = 0;
21966 	ip_stack_t	*ipst;
21967 
21968 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21969 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21970 
21971 	if (CONN_Q(q))
21972 		ipst = CONNQ_TO_IPST(q);
21973 	else
21974 		ipst = ILLQ_TO_IPST(q);
21975 
21976 	/*
21977 	 * For exclusive stacks we don't allow a different zoneid than
21978 	 * global.
21979 	 */
21980 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21981 	    zoneid != GLOBAL_ZONEID)
21982 		return (EINVAL);
21983 
21984 	/* Set the new zone id. */
21985 	ipif->ipif_zoneid = zoneid;
21986 
21987 	/* Update sctp list */
21988 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21989 
21990 	if (need_up) {
21991 		/*
21992 		 * Now bring the interface back up.  If this
21993 		 * is the only IPIF for the ILL, ipif_up
21994 		 * will have to re-bind to the device, so
21995 		 * we may get back EINPROGRESS, in which
21996 		 * case, this IOCTL will get completed in
21997 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21998 		 */
21999 		err = ipif_up(ipif, q, mp);
22000 	}
22001 	return (err);
22002 }
22003 
22004 /* ARGSUSED */
22005 int
22006 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22007     ip_ioctl_cmd_t *ipip, void *if_req)
22008 {
22009 	struct lifreq *lifr = (struct lifreq *)if_req;
22010 	zoneid_t zoneid;
22011 	zone_t *zptr;
22012 	zone_status_t status;
22013 
22014 	ASSERT(ipif->ipif_id != 0);
22015 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22016 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22017 		zoneid = GLOBAL_ZONEID;
22018 
22019 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22020 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22021 
22022 	/*
22023 	 * We recheck the zone status to resolve the following race condition:
22024 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22025 	 * 2) hme0:1 is up and can't be brought down right away;
22026 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22027 	 * 3) zone "myzone" is halted; the zone status switches to
22028 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22029 	 * the interfaces to remove - hme0:1 is not returned because it's not
22030 	 * yet in "myzone", so it won't be removed;
22031 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22032 	 * status check here, we would have hme0:1 in "myzone" after it's been
22033 	 * destroyed.
22034 	 * Note that if the status check fails, we need to bring the interface
22035 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22036 	 * ipif_up_done[_v6]().
22037 	 */
22038 	status = ZONE_IS_UNINITIALIZED;
22039 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22040 		status = zone_status_get(zptr);
22041 		zone_rele(zptr);
22042 	}
22043 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22044 		if (ipif->ipif_isv6) {
22045 			(void) ipif_up_done_v6(ipif);
22046 		} else {
22047 			(void) ipif_up_done(ipif);
22048 		}
22049 		return (EINVAL);
22050 	}
22051 
22052 	ipif_down_tail(ipif);
22053 
22054 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22055 	    B_TRUE));
22056 }
22057 
22058 /* ARGSUSED */
22059 int
22060 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22061 	ip_ioctl_cmd_t *ipip, void *ifreq)
22062 {
22063 	struct lifreq	*lifr = ifreq;
22064 
22065 	ASSERT(q->q_next == NULL);
22066 	ASSERT(CONN_Q(q));
22067 
22068 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22069 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22070 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22071 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22072 
22073 	return (0);
22074 }
22075 
22076 
22077 /* Find the previous ILL in this usesrc group */
22078 static ill_t *
22079 ill_prev_usesrc(ill_t *uill)
22080 {
22081 	ill_t *ill;
22082 
22083 	for (ill = uill->ill_usesrc_grp_next;
22084 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22085 	    ill = ill->ill_usesrc_grp_next)
22086 		/* do nothing */;
22087 	return (ill);
22088 }
22089 
22090 /*
22091  * Release all members of the usesrc group. This routine is called
22092  * from ill_delete when the interface being unplumbed is the
22093  * group head.
22094  */
22095 static void
22096 ill_disband_usesrc_group(ill_t *uill)
22097 {
22098 	ill_t *next_ill, *tmp_ill;
22099 	ip_stack_t	*ipst = uill->ill_ipst;
22100 
22101 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22102 	next_ill = uill->ill_usesrc_grp_next;
22103 
22104 	do {
22105 		ASSERT(next_ill != NULL);
22106 		tmp_ill = next_ill->ill_usesrc_grp_next;
22107 		ASSERT(tmp_ill != NULL);
22108 		next_ill->ill_usesrc_grp_next = NULL;
22109 		next_ill->ill_usesrc_ifindex = 0;
22110 		next_ill = tmp_ill;
22111 	} while (next_ill->ill_usesrc_ifindex != 0);
22112 	uill->ill_usesrc_grp_next = NULL;
22113 }
22114 
22115 /*
22116  * Remove the client usesrc ILL from the list and relink to a new list
22117  */
22118 int
22119 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22120 {
22121 	ill_t *ill, *tmp_ill;
22122 	ip_stack_t	*ipst = ucill->ill_ipst;
22123 
22124 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22125 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22126 
22127 	/*
22128 	 * Check if the usesrc client ILL passed in is not already
22129 	 * in use as a usesrc ILL i.e one whose source address is
22130 	 * in use OR a usesrc ILL is not already in use as a usesrc
22131 	 * client ILL
22132 	 */
22133 	if ((ucill->ill_usesrc_ifindex == 0) ||
22134 	    (uill->ill_usesrc_ifindex != 0)) {
22135 		return (-1);
22136 	}
22137 
22138 	ill = ill_prev_usesrc(ucill);
22139 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22140 
22141 	/* Remove from the current list */
22142 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22143 		/* Only two elements in the list */
22144 		ASSERT(ill->ill_usesrc_ifindex == 0);
22145 		ill->ill_usesrc_grp_next = NULL;
22146 	} else {
22147 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22148 	}
22149 
22150 	if (ifindex == 0) {
22151 		ucill->ill_usesrc_ifindex = 0;
22152 		ucill->ill_usesrc_grp_next = NULL;
22153 		return (0);
22154 	}
22155 
22156 	ucill->ill_usesrc_ifindex = ifindex;
22157 	tmp_ill = uill->ill_usesrc_grp_next;
22158 	uill->ill_usesrc_grp_next = ucill;
22159 	ucill->ill_usesrc_grp_next =
22160 	    (tmp_ill != NULL) ? tmp_ill : uill;
22161 	return (0);
22162 }
22163 
22164 /*
22165  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22166  * ip.c for locking details.
22167  */
22168 /* ARGSUSED */
22169 int
22170 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22171     ip_ioctl_cmd_t *ipip, void *ifreq)
22172 {
22173 	struct lifreq *lifr = (struct lifreq *)ifreq;
22174 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22175 	    ill_flag_changed = B_FALSE;
22176 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22177 	int err = 0, ret;
22178 	uint_t ifindex;
22179 	phyint_t *us_phyint, *us_cli_phyint;
22180 	ipsq_t *ipsq = NULL;
22181 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22182 
22183 	ASSERT(IAM_WRITER_IPIF(ipif));
22184 	ASSERT(q->q_next == NULL);
22185 	ASSERT(CONN_Q(q));
22186 
22187 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22188 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22189 
22190 	ASSERT(us_cli_phyint != NULL);
22191 
22192 	/*
22193 	 * If the client ILL is being used for IPMP, abort.
22194 	 * Note, this can be done before ipsq_try_enter since we are already
22195 	 * exclusive on this ILL
22196 	 */
22197 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22198 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22199 		return (EINVAL);
22200 	}
22201 
22202 	ifindex = lifr->lifr_index;
22203 	if (ifindex == 0) {
22204 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22205 			/* non usesrc group interface, nothing to reset */
22206 			return (0);
22207 		}
22208 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22209 		/* valid reset request */
22210 		reset_flg = B_TRUE;
22211 	}
22212 
22213 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22214 	    ip_process_ioctl, &err, ipst);
22215 
22216 	if (usesrc_ill == NULL) {
22217 		return (err);
22218 	}
22219 
22220 	/*
22221 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22222 	 * group nor can either of the interfaces be used for standy. So
22223 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22224 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22225 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22226 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22227 	 * the usesrc_cli_ill
22228 	 */
22229 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22230 	    NEW_OP, B_TRUE);
22231 	if (ipsq == NULL) {
22232 		err = EINPROGRESS;
22233 		/* Operation enqueued on the ipsq of the usesrc ILL */
22234 		goto done;
22235 	}
22236 
22237 	/* Check if the usesrc_ill is used for IPMP */
22238 	us_phyint = usesrc_ill->ill_phyint;
22239 	if ((us_phyint->phyint_groupname != NULL) ||
22240 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22241 		err = EINVAL;
22242 		goto done;
22243 	}
22244 
22245 	/*
22246 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22247 	 * already a client then return EINVAL
22248 	 */
22249 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22250 		err = EINVAL;
22251 		goto done;
22252 	}
22253 
22254 	/*
22255 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22256 	 * be then this is a duplicate operation.
22257 	 */
22258 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22259 		err = 0;
22260 		goto done;
22261 	}
22262 
22263 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22264 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22265 	    usesrc_ill->ill_isv6));
22266 
22267 	/*
22268 	 * The next step ensures that no new ires will be created referencing
22269 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22270 	 * we go through an ire walk deleting all ire caches that reference
22271 	 * the client ill. New ires referencing the client ill that are added
22272 	 * to the ire table before the ILL_CHANGING flag is set, will be
22273 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22274 	 * the client ill while the ILL_CHANGING flag is set will be failed
22275 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22276 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22277 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22278 	 * belong to the same usesrc group.
22279 	 */
22280 	mutex_enter(&usesrc_cli_ill->ill_lock);
22281 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22282 	mutex_exit(&usesrc_cli_ill->ill_lock);
22283 	ill_flag_changed = B_TRUE;
22284 
22285 	if (ipif->ipif_isv6)
22286 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22287 		    ALL_ZONES, ipst);
22288 	else
22289 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22290 		    ALL_ZONES, ipst);
22291 
22292 	/*
22293 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22294 	 * and the ill_usesrc_ifindex fields
22295 	 */
22296 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22297 
22298 	if (reset_flg) {
22299 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22300 		if (ret != 0) {
22301 			err = EINVAL;
22302 		}
22303 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22304 		goto done;
22305 	}
22306 
22307 	/*
22308 	 * Four possibilities to consider:
22309 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22310 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22311 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22312 	 * 4. Both are part of their respective usesrc groups
22313 	 */
22314 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22315 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22316 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22317 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22318 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22319 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22320 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22321 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22322 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22323 		/* Insert at head of list */
22324 		usesrc_cli_ill->ill_usesrc_grp_next =
22325 		    usesrc_ill->ill_usesrc_grp_next;
22326 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22327 	} else {
22328 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22329 		    ifindex);
22330 		if (ret != 0)
22331 			err = EINVAL;
22332 	}
22333 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22334 
22335 done:
22336 	if (ill_flag_changed) {
22337 		mutex_enter(&usesrc_cli_ill->ill_lock);
22338 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22339 		mutex_exit(&usesrc_cli_ill->ill_lock);
22340 	}
22341 	if (ipsq != NULL)
22342 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22343 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22344 	ill_refrele(usesrc_ill);
22345 	return (err);
22346 }
22347 
22348 /*
22349  * comparison function used by avl.
22350  */
22351 static int
22352 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22353 {
22354 
22355 	uint_t index;
22356 
22357 	ASSERT(phyip != NULL && index_ptr != NULL);
22358 
22359 	index = *((uint_t *)index_ptr);
22360 	/*
22361 	 * let the phyint with the lowest index be on top.
22362 	 */
22363 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22364 		return (1);
22365 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22366 		return (-1);
22367 	return (0);
22368 }
22369 
22370 /*
22371  * comparison function used by avl.
22372  */
22373 static int
22374 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22375 {
22376 	ill_t *ill;
22377 	int res = 0;
22378 
22379 	ASSERT(phyip != NULL && name_ptr != NULL);
22380 
22381 	if (((phyint_t *)phyip)->phyint_illv4)
22382 		ill = ((phyint_t *)phyip)->phyint_illv4;
22383 	else
22384 		ill = ((phyint_t *)phyip)->phyint_illv6;
22385 	ASSERT(ill != NULL);
22386 
22387 	res = strcmp(ill->ill_name, (char *)name_ptr);
22388 	if (res > 0)
22389 		return (1);
22390 	else if (res < 0)
22391 		return (-1);
22392 	return (0);
22393 }
22394 /*
22395  * This function is called from ill_delete when the ill is being
22396  * unplumbed. We remove the reference from the phyint and we also
22397  * free the phyint when there are no more references to it.
22398  */
22399 static void
22400 ill_phyint_free(ill_t *ill)
22401 {
22402 	phyint_t *phyi;
22403 	phyint_t *next_phyint;
22404 	ipsq_t *cur_ipsq;
22405 	ip_stack_t	*ipst = ill->ill_ipst;
22406 
22407 	ASSERT(ill->ill_phyint != NULL);
22408 
22409 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22410 	phyi = ill->ill_phyint;
22411 	ill->ill_phyint = NULL;
22412 	/*
22413 	 * ill_init allocates a phyint always to store the copy
22414 	 * of flags relevant to phyint. At that point in time, we could
22415 	 * not assign the name and hence phyint_illv4/v6 could not be
22416 	 * initialized. Later in ipif_set_values, we assign the name to
22417 	 * the ill, at which point in time we assign phyint_illv4/v6.
22418 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22419 	 */
22420 	if (ill->ill_flags & ILLF_IPV6) {
22421 		phyi->phyint_illv6 = NULL;
22422 	} else {
22423 		phyi->phyint_illv4 = NULL;
22424 	}
22425 	/*
22426 	 * ipif_down removes it from the group when the last ipif goes
22427 	 * down.
22428 	 */
22429 	ASSERT(ill->ill_group == NULL);
22430 
22431 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22432 		return;
22433 
22434 	/*
22435 	 * Make sure this phyint was put in the list.
22436 	 */
22437 	if (phyi->phyint_ifindex > 0) {
22438 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22439 		    phyi);
22440 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22441 		    phyi);
22442 	}
22443 	/*
22444 	 * remove phyint from the ipsq list.
22445 	 */
22446 	cur_ipsq = phyi->phyint_ipsq;
22447 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22448 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22449 	} else {
22450 		next_phyint = cur_ipsq->ipsq_phyint_list;
22451 		while (next_phyint != NULL) {
22452 			if (next_phyint->phyint_ipsq_next == phyi) {
22453 				next_phyint->phyint_ipsq_next =
22454 				    phyi->phyint_ipsq_next;
22455 				break;
22456 			}
22457 			next_phyint = next_phyint->phyint_ipsq_next;
22458 		}
22459 		ASSERT(next_phyint != NULL);
22460 	}
22461 	IPSQ_DEC_REF(cur_ipsq, ipst);
22462 
22463 	if (phyi->phyint_groupname_len != 0) {
22464 		ASSERT(phyi->phyint_groupname != NULL);
22465 		mi_free(phyi->phyint_groupname);
22466 	}
22467 	mi_free(phyi);
22468 }
22469 
22470 /*
22471  * Attach the ill to the phyint structure which can be shared by both
22472  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22473  * function is called from ipif_set_values and ill_lookup_on_name (for
22474  * loopback) where we know the name of the ill. We lookup the ill and if
22475  * there is one present already with the name use that phyint. Otherwise
22476  * reuse the one allocated by ill_init.
22477  */
22478 static void
22479 ill_phyint_reinit(ill_t *ill)
22480 {
22481 	boolean_t isv6 = ill->ill_isv6;
22482 	phyint_t *phyi_old;
22483 	phyint_t *phyi;
22484 	avl_index_t where = 0;
22485 	ill_t	*ill_other = NULL;
22486 	ipsq_t	*ipsq;
22487 	ip_stack_t	*ipst = ill->ill_ipst;
22488 
22489 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22490 
22491 	phyi_old = ill->ill_phyint;
22492 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22493 	    phyi_old->phyint_illv6 == NULL));
22494 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22495 	    phyi_old->phyint_illv4 == NULL));
22496 	ASSERT(phyi_old->phyint_ifindex == 0);
22497 
22498 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22499 	    ill->ill_name, &where);
22500 
22501 	/*
22502 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22503 	 *    the global list of ills. So no other thread could have located
22504 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22505 	 * 2. Now locate the other protocol instance of this ill.
22506 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22507 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22508 	 *    of neither ill can change.
22509 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22510 	 *    other ill.
22511 	 * 5. Release all locks.
22512 	 */
22513 
22514 	/*
22515 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22516 	 * we are initializing IPv4.
22517 	 */
22518 	if (phyi != NULL) {
22519 		ill_other = (isv6) ? phyi->phyint_illv4 :
22520 		    phyi->phyint_illv6;
22521 		ASSERT(ill_other->ill_phyint != NULL);
22522 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22523 		    (!isv6 && ill_other->ill_isv6));
22524 		GRAB_ILL_LOCKS(ill, ill_other);
22525 		/*
22526 		 * We are potentially throwing away phyint_flags which
22527 		 * could be different from the one that we obtain from
22528 		 * ill_other->ill_phyint. But it is okay as we are assuming
22529 		 * that the state maintained within IP is correct.
22530 		 */
22531 		mutex_enter(&phyi->phyint_lock);
22532 		if (isv6) {
22533 			ASSERT(phyi->phyint_illv6 == NULL);
22534 			phyi->phyint_illv6 = ill;
22535 		} else {
22536 			ASSERT(phyi->phyint_illv4 == NULL);
22537 			phyi->phyint_illv4 = ill;
22538 		}
22539 		/*
22540 		 * This is a new ill, currently undergoing SLIFNAME
22541 		 * So we could not have joined an IPMP group until now.
22542 		 */
22543 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22544 		    phyi_old->phyint_groupname == NULL);
22545 
22546 		/*
22547 		 * This phyi_old is going away. Decref ipsq_refs and
22548 		 * assert it is zero. The ipsq itself will be freed in
22549 		 * ipsq_exit
22550 		 */
22551 		ipsq = phyi_old->phyint_ipsq;
22552 		IPSQ_DEC_REF(ipsq, ipst);
22553 		ASSERT(ipsq->ipsq_refs == 0);
22554 		/* Get the singleton phyint out of the ipsq list */
22555 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22556 		ipsq->ipsq_phyint_list = NULL;
22557 		phyi_old->phyint_illv4 = NULL;
22558 		phyi_old->phyint_illv6 = NULL;
22559 		mi_free(phyi_old);
22560 	} else {
22561 		mutex_enter(&ill->ill_lock);
22562 		/*
22563 		 * We don't need to acquire any lock, since
22564 		 * the ill is not yet visible globally  and we
22565 		 * have not yet released the ill_g_lock.
22566 		 */
22567 		phyi = phyi_old;
22568 		mutex_enter(&phyi->phyint_lock);
22569 		/* XXX We need a recovery strategy here. */
22570 		if (!phyint_assign_ifindex(phyi, ipst))
22571 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22572 
22573 		/* No IPMP group yet, thus the hook uses the ifindex */
22574 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22575 
22576 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22577 		    (void *)phyi, where);
22578 
22579 		(void) avl_find(&ipst->ips_phyint_g_list->
22580 		    phyint_list_avl_by_index,
22581 		    &phyi->phyint_ifindex, &where);
22582 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22583 		    (void *)phyi, where);
22584 	}
22585 
22586 	/*
22587 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22588 	 * pending mp is not affected because that is per ill basis.
22589 	 */
22590 	ill->ill_phyint = phyi;
22591 
22592 	/*
22593 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22594 	 * We do this here as when the first ipif was allocated,
22595 	 * ipif_allocate does not know the right interface index.
22596 	 */
22597 
22598 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22599 	/*
22600 	 * Now that the phyint's ifindex has been assigned, complete the
22601 	 * remaining
22602 	 */
22603 
22604 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22605 	if (ill->ill_isv6) {
22606 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22607 		    ill->ill_phyint->phyint_ifindex;
22608 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22609 	} else {
22610 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22611 	}
22612 
22613 	/*
22614 	 * Generate an event within the hooks framework to indicate that
22615 	 * a new interface has just been added to IP.  For this event to
22616 	 * be generated, the network interface must, at least, have an
22617 	 * ifindex assigned to it.
22618 	 *
22619 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22620 	 * that the ordering of delivered events to listeners matches the
22621 	 * order of them in the kernel.
22622 	 *
22623 	 * This function could be called from ill_lookup_on_name. In that case
22624 	 * the interface is loopback "lo", which will not generate a NIC event.
22625 	 */
22626 	if (ill->ill_name_length <= 2 ||
22627 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22628 		/*
22629 		 * Generate nic plumb event for ill_name even if
22630 		 * ipmp_hook_emulation is set. That avoids generating events
22631 		 * for the ill_names should ipmp_hook_emulation be turned on
22632 		 * later.
22633 		 */
22634 		ill_nic_info_plumb(ill, B_FALSE);
22635 	}
22636 	RELEASE_ILL_LOCKS(ill, ill_other);
22637 	mutex_exit(&phyi->phyint_lock);
22638 }
22639 
22640 /*
22641  * Allocate a NE_PLUMB nic info event and store in the ill.
22642  * If 'group' is set we do it for the group name, otherwise the ill name.
22643  * It will be sent when we leave the ipsq.
22644  */
22645 void
22646 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22647 {
22648 	phyint_t	*phyi = ill->ill_phyint;
22649 	ip_stack_t	*ipst = ill->ill_ipst;
22650 	hook_nic_event_t *info;
22651 	char		*name;
22652 	int		namelen;
22653 
22654 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22655 
22656 	if ((info = ill->ill_nic_event_info) != NULL) {
22657 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22658 		    "attached for %s\n", info->hne_event,
22659 		    ill->ill_name));
22660 		if (info->hne_data != NULL)
22661 			kmem_free(info->hne_data, info->hne_datalen);
22662 		kmem_free(info, sizeof (hook_nic_event_t));
22663 		ill->ill_nic_event_info = NULL;
22664 	}
22665 
22666 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22667 	if (info == NULL) {
22668 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22669 		    "event information for %s (ENOMEM)\n",
22670 		    ill->ill_name));
22671 		return;
22672 	}
22673 
22674 	if (group) {
22675 		ASSERT(phyi->phyint_groupname_len != 0);
22676 		namelen = phyi->phyint_groupname_len;
22677 		name = phyi->phyint_groupname;
22678 	} else {
22679 		namelen = ill->ill_name_length;
22680 		name = ill->ill_name;
22681 	}
22682 
22683 	info->hne_nic = phyi->phyint_hook_ifindex;
22684 	info->hne_lif = 0;
22685 	info->hne_event = NE_PLUMB;
22686 	info->hne_family = ill->ill_isv6 ?
22687 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22688 
22689 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22690 	if (info->hne_data != NULL) {
22691 		info->hne_datalen = namelen;
22692 		bcopy(name, info->hne_data, info->hne_datalen);
22693 	} else {
22694 		ip2dbg(("ill_nic_info_plumb: could not attach "
22695 		    "name information for PLUMB nic event "
22696 		    "of %s (ENOMEM)\n", name));
22697 		kmem_free(info, sizeof (hook_nic_event_t));
22698 		info = NULL;
22699 	}
22700 	ill->ill_nic_event_info = info;
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