xref: /titanic_41/usr/src/uts/common/inet/ip/ip_if.c (revision df0345f7d6cc87cde9e532e8362f1aca053d98cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strlog.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kstat.h>
42 #include <sys/debug.h>
43 #include <sys/zone.h>
44 #include <sys/sunldi.h>
45 #include <sys/file.h>
46 #include <sys/bitmap.h>
47 #include <sys/cpuvar.h>
48 #include <sys/time.h>
49 #include <sys/kmem.h>
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/isa_defs.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_types.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <sys/sockio.h>
60 #include <netinet/in.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/igmp_var.h>
64 #include <sys/strsun.h>
65 #include <sys/policy.h>
66 #include <sys/ethernet.h>
67 #include <sys/callb.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_client.h>
101 #include <sys/dld.h>
102 
103 #include <sys/systeminfo.h>
104 #include <sys/bootconf.h>
105 
106 #include <sys/tsol/tndb.h>
107 #include <sys/tsol/tnet.h>
108 
109 /* The character which tells where the ill_name ends */
110 #define	IPIF_SEPARATOR_CHAR	':'
111 
112 /* IP ioctl function table entry */
113 typedef struct ipft_s {
114 	int	ipft_cmd;
115 	pfi_t	ipft_pfi;
116 	int	ipft_min_size;
117 	int	ipft_flags;
118 } ipft_t;
119 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
120 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
121 
122 typedef struct ip_sock_ar_s {
123 	union {
124 		area_t	ip_sock_area;
125 		ared_t	ip_sock_ared;
126 		areq_t	ip_sock_areq;
127 	} ip_sock_ar_u;
128 	queue_t	*ip_sock_ar_q;
129 } ip_sock_ar_t;
130 
131 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
132 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
133 		    char *value, caddr_t cp, cred_t *ioc_cr);
134 
135 static boolean_t ill_is_quiescent(ill_t *);
136 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
137 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
138 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
141     mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
147     mblk_t *mp);
148 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
149     queue_t *q, mblk_t *mp, boolean_t need_up);
150 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
151     int ioccmd, struct linkblk *li, boolean_t doconsist);
152 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
153 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
154 static void	ipsq_flush(ill_t *ill);
155 
156 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
157     queue_t *q, mblk_t *mp, boolean_t need_up);
158 static void	ipsq_delete(ipsq_t *);
159 
160 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
161 		    boolean_t initialize);
162 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
163 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
164 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
165 		    boolean_t isv6);
166 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
167 static void	ipif_delete_cache_ire(ire_t *, char *);
168 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
169 static void	ipif_free(ipif_t *ipif);
170 static void	ipif_free_tail(ipif_t *ipif);
171 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
172 static void	ipif_multicast_down(ipif_t *ipif);
173 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
174 static void	ipif_set_default(ipif_t *ipif);
175 static int	ipif_set_values(queue_t *q, mblk_t *mp,
176     char *interf_name, uint_t *ppa);
177 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
178     queue_t *q);
179 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
180     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
181     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
182 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
183 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
184 
185 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
186 static int	ill_arp_off(ill_t *ill);
187 static int	ill_arp_on(ill_t *ill);
188 static void	ill_delete_interface_type(ill_if_t *);
189 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
190 static void	ill_dl_down(ill_t *ill);
191 static void	ill_down(ill_t *ill);
192 static void	ill_downi(ire_t *ire, char *ill_arg);
193 static void	ill_free_mib(ill_t *ill);
194 static void	ill_glist_delete(ill_t *);
195 static boolean_t ill_has_usable_ipif(ill_t *);
196 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
197 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
198 static void	ill_phyint_free(ill_t *ill);
199 static void	ill_phyint_reinit(ill_t *ill);
200 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
201 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
202 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
203 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
204 static void	ill_stq_cache_delete(ire_t *, char *);
205 
206 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
208 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     in6_addr_t *);
210 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
211     ipaddr_t *);
212 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
213 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     in6_addr_t *);
215 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
216     ipaddr_t *);
217 
218 static void	ipif_save_ire(ipif_t *, ire_t *);
219 static void	ipif_remove_ire(ipif_t *, ire_t *);
220 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
221 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
222 
223 /*
224  * Per-ill IPsec capabilities management.
225  */
226 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
227 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
228 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
229 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
230 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset_fill(ill_t *, mblk_t *);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset_fill(ill_t *, mblk_t *);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
243 static int  ill_capability_ipsec_reset_size(ill_t *, int *, int *, int *,
244     int *);
245 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
246 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
247 		    dl_capability_sub_t *);
248 static void	ill_capability_dld_enable(ill_t *);
249 static void	ill_capability_ack_thr(void *);
250 static void	ill_capability_lso_enable(ill_t *);
251 static void	ill_capability_send(ill_t *, mblk_t *);
252 
253 static void	illgrp_cache_delete(ire_t *, char *);
254 static void	illgrp_delete(ill_t *ill);
255 static void	illgrp_reset_schednext(ill_t *ill);
256 
257 static ill_t	*ill_prev_usesrc(ill_t *);
258 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
259 static void	ill_disband_usesrc_group(ill_t *);
260 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
261 
262 #ifdef DEBUG
263 static	void	ill_trace_cleanup(const ill_t *);
264 static	void	ipif_trace_cleanup(const ipif_t *);
265 #endif
266 
267 /*
268  * if we go over the memory footprint limit more than once in this msec
269  * interval, we'll start pruning aggressively.
270  */
271 int ip_min_frag_prune_time = 0;
272 
273 /*
274  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
275  * and the IPsec DOI
276  */
277 #define	MAX_IPSEC_ALGS	256
278 
279 #define	BITSPERBYTE	8
280 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
281 
282 #define	IPSEC_ALG_ENABLE(algs, algid) \
283 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
284 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
285 
286 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
287 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
288 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
289 
290 typedef uint8_t ipsec_capab_elem_t;
291 
292 /*
293  * Per-algorithm parameters.  Note that at present, only encryption
294  * algorithms have variable keysize (IKE does not provide a way to negotiate
295  * auth algorithm keysize).
296  *
297  * All sizes here are in bits.
298  */
299 typedef struct
300 {
301 	uint16_t	minkeylen;
302 	uint16_t	maxkeylen;
303 } ipsec_capab_algparm_t;
304 
305 /*
306  * Per-ill capabilities.
307  */
308 struct ill_ipsec_capab_s {
309 	ipsec_capab_elem_t *encr_hw_algs;
310 	ipsec_capab_elem_t *auth_hw_algs;
311 	uint32_t algs_size;	/* size of _hw_algs in bytes */
312 	/* algorithm key lengths */
313 	ipsec_capab_algparm_t *encr_algparm;
314 	uint32_t encr_algparm_size;
315 	uint32_t encr_algparm_end;
316 };
317 
318 /*
319  * The field values are larger than strictly necessary for simple
320  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
321  */
322 static area_t	ip_area_template = {
323 	AR_ENTRY_ADD,			/* area_cmd */
324 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
325 					/* area_name_offset */
326 	/* area_name_length temporarily holds this structure length */
327 	sizeof (area_t),			/* area_name_length */
328 	IP_ARP_PROTO_TYPE,		/* area_proto */
329 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
330 	IP_ADDR_LEN,			/* area_proto_addr_length */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
332 					/* area_proto_mask_offset */
333 	0,				/* area_flags */
334 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
335 					/* area_hw_addr_offset */
336 	/* Zero length hw_addr_length means 'use your idea of the address' */
337 	0				/* area_hw_addr_length */
338 };
339 
340 /*
341  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
342  * support
343  */
344 static area_t	ip6_area_template = {
345 	AR_ENTRY_ADD,			/* area_cmd */
346 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
347 					/* area_name_offset */
348 	/* area_name_length temporarily holds this structure length */
349 	sizeof (area_t),			/* area_name_length */
350 	IP_ARP_PROTO_TYPE,		/* area_proto */
351 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
352 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
354 					/* area_proto_mask_offset */
355 	0,				/* area_flags */
356 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
357 					/* area_hw_addr_offset */
358 	/* Zero length hw_addr_length means 'use your idea of the address' */
359 	0				/* area_hw_addr_length */
360 };
361 
362 static ared_t	ip_ared_template = {
363 	AR_ENTRY_DELETE,
364 	sizeof (ared_t) + IP_ADDR_LEN,
365 	sizeof (ared_t),
366 	IP_ARP_PROTO_TYPE,
367 	sizeof (ared_t),
368 	IP_ADDR_LEN,
369 	0
370 };
371 
372 static ared_t	ip6_ared_template = {
373 	AR_ENTRY_DELETE,
374 	sizeof (ared_t) + IPV6_ADDR_LEN,
375 	sizeof (ared_t),
376 	IP_ARP_PROTO_TYPE,
377 	sizeof (ared_t),
378 	IPV6_ADDR_LEN,
379 	0
380 };
381 
382 /*
383  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
384  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
385  * areq is used).
386  */
387 static areq_t	ip_areq_template = {
388 	AR_ENTRY_QUERY,			/* cmd */
389 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
390 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
391 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
392 	sizeof (areq_t),			/* target addr offset */
393 	IP_ADDR_LEN,			/* target addr_length */
394 	0,				/* flags */
395 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
396 	IP_ADDR_LEN,			/* sender addr length */
397 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
398 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
399 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
400 	/* anything else filled in by the code */
401 };
402 
403 static arc_t	ip_aru_template = {
404 	AR_INTERFACE_UP,
405 	sizeof (arc_t),		/* Name offset */
406 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
407 };
408 
409 static arc_t	ip_ard_template = {
410 	AR_INTERFACE_DOWN,
411 	sizeof (arc_t),		/* Name offset */
412 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
413 };
414 
415 static arc_t	ip_aron_template = {
416 	AR_INTERFACE_ON,
417 	sizeof (arc_t),		/* Name offset */
418 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
419 };
420 
421 static arc_t	ip_aroff_template = {
422 	AR_INTERFACE_OFF,
423 	sizeof (arc_t),		/* Name offset */
424 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
425 };
426 
427 static arma_t	ip_arma_multi_template = {
428 	AR_MAPPING_ADD,
429 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
430 				/* Name offset */
431 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
432 	IP_ARP_PROTO_TYPE,
433 	sizeof (arma_t),			/* proto_addr_offset */
434 	IP_ADDR_LEN,				/* proto_addr_length */
435 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
436 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
437 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
438 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
439 	IP_MAX_HW_LEN,				/* hw_addr_length */
440 	0,					/* hw_mapping_start */
441 };
442 
443 static ipft_t	ip_ioctl_ftbl[] = {
444 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
445 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
446 		IPFT_F_NO_REPLY },
447 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
448 		IPFT_F_NO_REPLY },
449 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
450 	{ 0 }
451 };
452 
453 /* Simple ICMP IP Header Template */
454 static ipha_t icmp_ipha = {
455 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
456 };
457 
458 /* Flag descriptors for ip_ipif_report */
459 static nv_t	ipif_nv_tbl[] = {
460 	{ IPIF_UP,		"UP" },
461 	{ IPIF_BROADCAST,	"BROADCAST" },
462 	{ ILLF_DEBUG,		"DEBUG" },
463 	{ PHYI_LOOPBACK,	"LOOPBACK" },
464 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
465 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
466 	{ PHYI_RUNNING,		"RUNNING" },
467 	{ ILLF_NOARP,		"NOARP" },
468 	{ PHYI_PROMISC,		"PROMISC" },
469 	{ PHYI_ALLMULTI,	"ALLMULTI" },
470 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
471 	{ ILLF_MULTICAST,	"MULTICAST" },
472 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
473 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
474 	{ IPIF_DHCPRUNNING,	"DHCP" },
475 	{ IPIF_PRIVATE,		"PRIVATE" },
476 	{ IPIF_NOXMIT,		"NOXMIT" },
477 	{ IPIF_NOLOCAL,		"NOLOCAL" },
478 	{ IPIF_DEPRECATED,	"DEPRECATED" },
479 	{ IPIF_PREFERRED,	"PREFERRED" },
480 	{ IPIF_TEMPORARY,	"TEMPORARY" },
481 	{ IPIF_ADDRCONF,	"ADDRCONF" },
482 	{ PHYI_VIRTUAL,		"VIRTUAL" },
483 	{ ILLF_ROUTER,		"ROUTER" },
484 	{ ILLF_NONUD,		"NONUD" },
485 	{ IPIF_ANYCAST,		"ANYCAST" },
486 	{ ILLF_NORTEXCH,	"NORTEXCH" },
487 	{ ILLF_IPV4,		"IPV4" },
488 	{ ILLF_IPV6,		"IPV6" },
489 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
490 	{ PHYI_FAILED,		"FAILED" },
491 	{ PHYI_STANDBY,		"STANDBY" },
492 	{ PHYI_INACTIVE,	"INACTIVE" },
493 	{ PHYI_OFFLINE,		"OFFLINE" },
494 };
495 
496 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
497 
498 static ip_m_t   ip_m_tbl[] = {
499 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_ether_v6intfid },
501 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_nodef_v6intfid },
507 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
508 	    ip_ether_v6intfid },
509 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
510 	    ip_ib_v6intfid },
511 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
512 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
513 	    ip_nodef_v6intfid }
514 };
515 
516 static ill_t	ill_null;		/* Empty ILL for init. */
517 char	ipif_loopback_name[] = "lo0";
518 static char *ipv4_forward_suffix = ":ip_forwarding";
519 static char *ipv6_forward_suffix = ":ip6_forwarding";
520 static	sin6_t	sin6_null;	/* Zero address for quick clears */
521 static	sin_t	sin_null;	/* Zero address for quick clears */
522 
523 /* When set search for unused ipif_seqid */
524 static ipif_t	ipif_zero;
525 
526 /*
527  * ppa arena is created after these many
528  * interfaces have been plumbed.
529  */
530 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
531 
532 static uint_t
533 ipif_rand(ip_stack_t *ipst)
534 {
535 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
536 	    12345;
537 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
538 }
539 
540 /*
541  * Allocate per-interface mibs.
542  * Returns true if ok. False otherwise.
543  *  ipsq  may not yet be allocated (loopback case ).
544  */
545 static boolean_t
546 ill_allocate_mibs(ill_t *ill)
547 {
548 	/* Already allocated? */
549 	if (ill->ill_ip_mib != NULL) {
550 		if (ill->ill_isv6)
551 			ASSERT(ill->ill_icmp6_mib != NULL);
552 		return (B_TRUE);
553 	}
554 
555 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
556 	    KM_NOSLEEP);
557 	if (ill->ill_ip_mib == NULL) {
558 		return (B_FALSE);
559 	}
560 
561 	/* Setup static information */
562 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
563 	    sizeof (mib2_ipIfStatsEntry_t));
564 	if (ill->ill_isv6) {
565 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
566 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
567 		    sizeof (mib2_ipv6AddrEntry_t));
568 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
569 		    sizeof (mib2_ipv6RouteEntry_t));
570 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
571 		    sizeof (mib2_ipv6NetToMediaEntry_t));
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
573 		    sizeof (ipv6_member_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
575 		    sizeof (ipv6_grpsrc_t));
576 	} else {
577 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
579 		    sizeof (mib2_ipAddrEntry_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
581 		    sizeof (mib2_ipRouteEntry_t));
582 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
583 		    sizeof (mib2_ipNetToMediaEntry_t));
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
585 		    sizeof (ip_member_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
587 		    sizeof (ip_grpsrc_t));
588 
589 		/*
590 		 * For a v4 ill, we are done at this point, because per ill
591 		 * icmp mibs are only used for v6.
592 		 */
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_icmp6_mib == NULL) {
599 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
600 		ill->ill_ip_mib = NULL;
601 		return (B_FALSE);
602 	}
603 	/* static icmp info */
604 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
605 	    sizeof (mib2_ipv6IfIcmpEntry_t);
606 	/*
607 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
608 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
609 	 * -> ill_phyint_reinit
610 	 */
611 	return (B_TRUE);
612 }
613 
614 /*
615  * Common code for preparation of ARP commands.  Two points to remember:
616  * 	1) The ill_name is tacked on at the end of the allocated space so
617  *	   the templates name_offset field must contain the total space
618  *	   to allocate less the name length.
619  *
620  *	2) The templates name_length field should contain the *template*
621  *	   length.  We use it as a parameter to bcopy() and then write
622  *	   the real ill_name_length into the name_length field of the copy.
623  * (Always called as writer.)
624  */
625 mblk_t *
626 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
627 {
628 	arc_t	*arc = (arc_t *)template;
629 	char	*cp;
630 	int	len;
631 	mblk_t	*mp;
632 	uint_t	name_length = ill->ill_name_length;
633 	uint_t	template_len = arc->arc_name_length;
634 
635 	len = arc->arc_name_offset + name_length;
636 	mp = allocb(len, BPRI_HI);
637 	if (mp == NULL)
638 		return (NULL);
639 	cp = (char *)mp->b_rptr;
640 	mp->b_wptr = (uchar_t *)&cp[len];
641 	if (template_len)
642 		bcopy(template, cp, template_len);
643 	if (len > template_len)
644 		bzero(&cp[template_len], len - template_len);
645 	mp->b_datap->db_type = M_PROTO;
646 
647 	arc = (arc_t *)cp;
648 	arc->arc_name_length = name_length;
649 	cp = (char *)arc + arc->arc_name_offset;
650 	bcopy(ill->ill_name, cp, name_length);
651 
652 	if (addr) {
653 		area_t	*area = (area_t *)mp->b_rptr;
654 
655 		cp = (char *)area + area->area_proto_addr_offset;
656 		bcopy(addr, cp, area->area_proto_addr_length);
657 		if (area->area_cmd == AR_ENTRY_ADD) {
658 			cp = (char *)area;
659 			len = area->area_proto_addr_length;
660 			if (area->area_proto_mask_offset)
661 				cp += area->area_proto_mask_offset;
662 			else
663 				cp += area->area_proto_addr_offset + len;
664 			while (len-- > 0)
665 				*cp++ = (char)~0;
666 		}
667 	}
668 	return (mp);
669 }
670 
671 mblk_t *
672 ipif_area_alloc(ipif_t *ipif)
673 {
674 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
675 	    (char *)&ipif->ipif_lcl_addr));
676 }
677 
678 mblk_t *
679 ipif_ared_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
687 {
688 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&addr));
690 }
691 
692 /*
693  * Completely vaporize a lower level tap and all associated interfaces.
694  * ill_delete is called only out of ip_close when the device control
695  * stream is being closed.
696  */
697 void
698 ill_delete(ill_t *ill)
699 {
700 	ipif_t	*ipif;
701 	ill_t	*prev_ill;
702 	ip_stack_t	*ipst = ill->ill_ipst;
703 
704 	/*
705 	 * ill_delete may be forcibly entering the ipsq. The previous
706 	 * ioctl may not have completed and may need to be aborted.
707 	 * ipsq_flush takes care of it. If we don't need to enter the
708 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
709 	 * ill_delete_tail is sufficient.
710 	 */
711 	ipsq_flush(ill);
712 
713 	/*
714 	 * Nuke all interfaces.  ipif_free will take down the interface,
715 	 * remove it from the list, and free the data structure.
716 	 * Walk down the ipif list and remove the logical interfaces
717 	 * first before removing the main ipif. We can't unplumb
718 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
719 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
720 	 * POINTOPOINT.
721 	 *
722 	 * If ill_ipif was not properly initialized (i.e low on memory),
723 	 * then no interfaces to clean up. In this case just clean up the
724 	 * ill.
725 	 */
726 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
727 		ipif_free(ipif);
728 
729 	/*
730 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
731 	 * So nobody can be using this mp now. Free the mp allocated for
732 	 * honoring ILLF_NOARP
733 	 */
734 	freemsg(ill->ill_arp_on_mp);
735 	ill->ill_arp_on_mp = NULL;
736 
737 	/* Clean up msgs on pending upcalls for mrouted */
738 	reset_mrt_ill(ill);
739 
740 	/*
741 	 * ipif_free -> reset_conn_ipif will remove all multicast
742 	 * references for IPv4. For IPv6, we need to do it here as
743 	 * it points only at ills.
744 	 */
745 	reset_conn_ill(ill);
746 
747 	/*
748 	 * Remove multicast references added as a result of calls to
749 	 * ip_join_allmulti().
750 	 */
751 	ip_purge_allmulti(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 	ASSERT(!(ill->ill_capabilities &
824 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
825 
826 	if (ill->ill_net_type != IRE_LOOPBACK)
827 		qprocsoff(ill->ill_rq);
828 
829 	/*
830 	 * We do an ipsq_flush once again now. New messages could have
831 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
832 	 * could also have landed up if an ioctl thread had looked up
833 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
834 	 * enqueued the ioctl when we did the ipsq_flush last time.
835 	 */
836 	ipsq_flush(ill);
837 
838 	/*
839 	 * Free capabilities.
840 	 */
841 	if (ill->ill_ipsec_capab_ah != NULL) {
842 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
843 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
844 		ill->ill_ipsec_capab_ah = NULL;
845 	}
846 
847 	if (ill->ill_ipsec_capab_esp != NULL) {
848 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
849 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
850 		ill->ill_ipsec_capab_esp = NULL;
851 	}
852 
853 	if (ill->ill_mdt_capab != NULL) {
854 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
855 		ill->ill_mdt_capab = NULL;
856 	}
857 
858 	if (ill->ill_hcksum_capab != NULL) {
859 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
860 		ill->ill_hcksum_capab = NULL;
861 	}
862 
863 	if (ill->ill_zerocopy_capab != NULL) {
864 		kmem_free(ill->ill_zerocopy_capab,
865 		    sizeof (ill_zerocopy_capab_t));
866 		ill->ill_zerocopy_capab = NULL;
867 	}
868 
869 	if (ill->ill_lso_capab != NULL) {
870 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
871 		ill->ill_lso_capab = NULL;
872 	}
873 
874 	if (ill->ill_dld_capab != NULL) {
875 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
876 		ill->ill_dld_capab = NULL;
877 	}
878 
879 	while (ill->ill_ipif != NULL)
880 		ipif_free_tail(ill->ill_ipif);
881 
882 	/*
883 	 * We have removed all references to ilm from conn and the ones joined
884 	 * within the kernel.
885 	 *
886 	 * We don't walk conns, mrts and ires because
887 	 *
888 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
889 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
890 	 *    ill references.
891 	 */
892 	ASSERT(ilm_walk_ill(ill) == 0);
893 	/*
894 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
895 	 * could free the phyint. No more reference to the phyint after this
896 	 * point.
897 	 */
898 	(void) ill_glist_delete(ill);
899 
900 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
901 	if (ill->ill_ndd_name != NULL)
902 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
903 	rw_exit(&ipst->ips_ip_g_nd_lock);
904 
905 	if (ill->ill_frag_ptr != NULL) {
906 		uint_t count;
907 
908 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
909 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
910 		}
911 		mi_free(ill->ill_frag_ptr);
912 		ill->ill_frag_ptr = NULL;
913 		ill->ill_frag_hash_tbl = NULL;
914 	}
915 
916 	freemsg(ill->ill_nd_lla_mp);
917 	/* Free all retained control messages. */
918 	mpp = &ill->ill_first_mp_to_free;
919 	do {
920 		while (mpp[0]) {
921 			mblk_t  *mp;
922 			mblk_t  *mp1;
923 
924 			mp = mpp[0];
925 			mpp[0] = mp->b_next;
926 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
927 				mp1->b_next = NULL;
928 				mp1->b_prev = NULL;
929 			}
930 			freemsg(mp);
931 		}
932 	} while (mpp++ != &ill->ill_last_mp_to_free);
933 
934 	ill_free_mib(ill);
935 
936 #ifdef DEBUG
937 	ill_trace_cleanup(ill);
938 #endif
939 
940 	/* Drop refcnt here */
941 	netstack_rele(ill->ill_ipst->ips_netstack);
942 	ill->ill_ipst = NULL;
943 }
944 
945 static void
946 ill_free_mib(ill_t *ill)
947 {
948 	ip_stack_t *ipst = ill->ill_ipst;
949 
950 	/*
951 	 * MIB statistics must not be lost, so when an interface
952 	 * goes away the counter values will be added to the global
953 	 * MIBs.
954 	 */
955 	if (ill->ill_ip_mib != NULL) {
956 		if (ill->ill_isv6) {
957 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
958 			    ill->ill_ip_mib);
959 		} else {
960 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
961 			    ill->ill_ip_mib);
962 		}
963 
964 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
965 		ill->ill_ip_mib = NULL;
966 	}
967 	if (ill->ill_icmp6_mib != NULL) {
968 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
969 		    ill->ill_icmp6_mib);
970 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
971 		ill->ill_icmp6_mib = NULL;
972 	}
973 }
974 
975 /*
976  * Concatenate together a physical address and a sap.
977  *
978  * Sap_lengths are interpreted as follows:
979  *   sap_length == 0	==>	no sap
980  *   sap_length > 0	==>	sap is at the head of the dlpi address
981  *   sap_length < 0	==>	sap is at the tail of the dlpi address
982  */
983 static void
984 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
985     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
986 {
987 	uint16_t sap_addr = (uint16_t)sap_src;
988 
989 	if (sap_length == 0) {
990 		if (phys_src == NULL)
991 			bzero(dst, phys_length);
992 		else
993 			bcopy(phys_src, dst, phys_length);
994 	} else if (sap_length < 0) {
995 		if (phys_src == NULL)
996 			bzero(dst, phys_length);
997 		else
998 			bcopy(phys_src, dst, phys_length);
999 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1000 	} else {
1001 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1002 		if (phys_src == NULL)
1003 			bzero((char *)dst + sap_length, phys_length);
1004 		else
1005 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1006 	}
1007 }
1008 
1009 /*
1010  * Generate a dl_unitdata_req mblk for the device and address given.
1011  * addr_length is the length of the physical portion of the address.
1012  * If addr is NULL include an all zero address of the specified length.
1013  * TRUE? In any case, addr_length is taken to be the entire length of the
1014  * dlpi address, including the absolute value of sap_length.
1015  */
1016 mblk_t *
1017 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1018 		t_scalar_t sap_length)
1019 {
1020 	dl_unitdata_req_t *dlur;
1021 	mblk_t	*mp;
1022 	t_scalar_t	abs_sap_length;		/* absolute value */
1023 
1024 	abs_sap_length = ABS(sap_length);
1025 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1026 	    DL_UNITDATA_REQ);
1027 	if (mp == NULL)
1028 		return (NULL);
1029 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1030 	/* HACK: accomodate incompatible DLPI drivers */
1031 	if (addr_length == 8)
1032 		addr_length = 6;
1033 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1034 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1035 	dlur->dl_priority.dl_min = 0;
1036 	dlur->dl_priority.dl_max = 0;
1037 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1038 	    (uchar_t *)&dlur[1]);
1039 	return (mp);
1040 }
1041 
1042 /*
1043  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1044  * Return an error if we already have 1 or more ioctls in progress.
1045  * This is used only for non-exclusive ioctls. Currently this is used
1046  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1047  * and thus need to use ipsq_pending_mp_add.
1048  */
1049 boolean_t
1050 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1051 {
1052 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1053 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1054 	/*
1055 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1056 	 */
1057 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1058 	    (add_mp->b_datap->db_type == M_IOCTL));
1059 
1060 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1061 	/*
1062 	 * Return error if the conn has started closing. The conn
1063 	 * could have finished cleaning up the pending mp list,
1064 	 * If so we should not add another mp to the list negating
1065 	 * the cleanup.
1066 	 */
1067 	if (connp->conn_state_flags & CONN_CLOSING)
1068 		return (B_FALSE);
1069 	/*
1070 	 * Add the pending mp to the head of the list, chained by b_next.
1071 	 * Note down the conn on which the ioctl request came, in b_prev.
1072 	 * This will be used to later get the conn, when we get a response
1073 	 * on the ill queue, from some other module (typically arp)
1074 	 */
1075 	add_mp->b_next = (void *)ill->ill_pending_mp;
1076 	add_mp->b_queue = CONNP_TO_WQ(connp);
1077 	ill->ill_pending_mp = add_mp;
1078 	if (connp != NULL)
1079 		connp->conn_oper_pending_ill = ill;
1080 	return (B_TRUE);
1081 }
1082 
1083 /*
1084  * Retrieve the ill_pending_mp and return it. We have to walk the list
1085  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1086  */
1087 mblk_t *
1088 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1089 {
1090 	mblk_t	*prev = NULL;
1091 	mblk_t	*curr = NULL;
1092 	uint_t	id;
1093 	conn_t	*connp;
1094 
1095 	/*
1096 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1097 	 * up the pending mp, but it does not know the ioc_id and
1098 	 * passes in a zero for it.
1099 	 */
1100 	mutex_enter(&ill->ill_lock);
1101 	if (ioc_id != 0)
1102 		*connpp = NULL;
1103 
1104 	/* Search the list for the appropriate ioctl based on ioc_id */
1105 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1106 	    prev = curr, curr = curr->b_next) {
1107 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1108 		connp = Q_TO_CONN(curr->b_queue);
1109 		/* Match based on the ioc_id or based on the conn */
1110 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1111 			break;
1112 	}
1113 
1114 	if (curr != NULL) {
1115 		/* Unlink the mblk from the pending mp list */
1116 		if (prev != NULL) {
1117 			prev->b_next = curr->b_next;
1118 		} else {
1119 			ASSERT(ill->ill_pending_mp == curr);
1120 			ill->ill_pending_mp = curr->b_next;
1121 		}
1122 
1123 		/*
1124 		 * conn refcnt must have been bumped up at the start of
1125 		 * the ioctl. So we can safely access the conn.
1126 		 */
1127 		ASSERT(CONN_Q(curr->b_queue));
1128 		*connpp = Q_TO_CONN(curr->b_queue);
1129 		curr->b_next = NULL;
1130 		curr->b_queue = NULL;
1131 	}
1132 
1133 	mutex_exit(&ill->ill_lock);
1134 
1135 	return (curr);
1136 }
1137 
1138 /*
1139  * Add the pending mp to the list. There can be only 1 pending mp
1140  * in the list. Any exclusive ioctl that needs to wait for a response
1141  * from another module or driver needs to use this function to set
1142  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1143  * the other module/driver. This is also used while waiting for the
1144  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1145  */
1146 boolean_t
1147 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1148     int waitfor)
1149 {
1150 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1151 
1152 	ASSERT(IAM_WRITER_IPIF(ipif));
1153 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1154 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1155 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1156 	/*
1157 	 * The caller may be using a different ipif than the one passed into
1158 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1159 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1160 	 * that `ipsq_current_ipif == ipif'.
1161 	 */
1162 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1163 
1164 	/*
1165 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1166 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1167 	 */
1168 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1169 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1170 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1171 
1172 	if (connp != NULL) {
1173 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1174 		/*
1175 		 * Return error if the conn has started closing. The conn
1176 		 * could have finished cleaning up the pending mp list,
1177 		 * If so we should not add another mp to the list negating
1178 		 * the cleanup.
1179 		 */
1180 		if (connp->conn_state_flags & CONN_CLOSING)
1181 			return (B_FALSE);
1182 	}
1183 	mutex_enter(&ipsq->ipsq_lock);
1184 	ipsq->ipsq_pending_ipif = ipif;
1185 	/*
1186 	 * Note down the queue in b_queue. This will be returned by
1187 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1188 	 * the processing
1189 	 */
1190 	add_mp->b_next = NULL;
1191 	add_mp->b_queue = q;
1192 	ipsq->ipsq_pending_mp = add_mp;
1193 	ipsq->ipsq_waitfor = waitfor;
1194 
1195 	if (connp != NULL)
1196 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1197 	mutex_exit(&ipsq->ipsq_lock);
1198 	return (B_TRUE);
1199 }
1200 
1201 /*
1202  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1203  * queued in the list.
1204  */
1205 mblk_t *
1206 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1207 {
1208 	mblk_t	*curr = NULL;
1209 
1210 	mutex_enter(&ipsq->ipsq_lock);
1211 	*connpp = NULL;
1212 	if (ipsq->ipsq_pending_mp == NULL) {
1213 		mutex_exit(&ipsq->ipsq_lock);
1214 		return (NULL);
1215 	}
1216 
1217 	/* There can be only 1 such excl message */
1218 	curr = ipsq->ipsq_pending_mp;
1219 	ASSERT(curr != NULL && curr->b_next == NULL);
1220 	ipsq->ipsq_pending_ipif = NULL;
1221 	ipsq->ipsq_pending_mp = NULL;
1222 	ipsq->ipsq_waitfor = 0;
1223 	mutex_exit(&ipsq->ipsq_lock);
1224 
1225 	if (CONN_Q(curr->b_queue)) {
1226 		/*
1227 		 * This mp did a refhold on the conn, at the start of the ioctl.
1228 		 * So we can safely return a pointer to the conn to the caller.
1229 		 */
1230 		*connpp = Q_TO_CONN(curr->b_queue);
1231 	} else {
1232 		*connpp = NULL;
1233 	}
1234 	curr->b_next = NULL;
1235 	curr->b_prev = NULL;
1236 	return (curr);
1237 }
1238 
1239 /*
1240  * Cleanup the ioctl mp queued in ipsq_pending_mp
1241  * - Called in the ill_delete path
1242  * - Called in the M_ERROR or M_HANGUP path on the ill.
1243  * - Called in the conn close path.
1244  */
1245 boolean_t
1246 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1247 {
1248 	mblk_t	*mp;
1249 	ipsq_t	*ipsq;
1250 	queue_t	*q;
1251 	ipif_t	*ipif;
1252 
1253 	ASSERT(IAM_WRITER_ILL(ill));
1254 	ipsq = ill->ill_phyint->phyint_ipsq;
1255 	mutex_enter(&ipsq->ipsq_lock);
1256 	/*
1257 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1258 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1259 	 * even if it is meant for another ill, since we have to enqueue
1260 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1261 	 * If connp is non-null we are called from the conn close path.
1262 	 */
1263 	mp = ipsq->ipsq_pending_mp;
1264 	if (mp == NULL || (connp != NULL &&
1265 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1266 		mutex_exit(&ipsq->ipsq_lock);
1267 		return (B_FALSE);
1268 	}
1269 	/* Now remove from the ipsq_pending_mp */
1270 	ipsq->ipsq_pending_mp = NULL;
1271 	q = mp->b_queue;
1272 	mp->b_next = NULL;
1273 	mp->b_prev = NULL;
1274 	mp->b_queue = NULL;
1275 
1276 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1277 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1278 	if (ill->ill_move_in_progress) {
1279 		ILL_CLEAR_MOVE(ill);
1280 	} else if (ill->ill_up_ipifs) {
1281 		ill_group_cleanup(ill);
1282 	}
1283 
1284 	ipif = ipsq->ipsq_pending_ipif;
1285 	ipsq->ipsq_pending_ipif = NULL;
1286 	ipsq->ipsq_waitfor = 0;
1287 	ipsq->ipsq_current_ipif = NULL;
1288 	ipsq->ipsq_current_ioctl = 0;
1289 	ipsq->ipsq_current_done = B_TRUE;
1290 	mutex_exit(&ipsq->ipsq_lock);
1291 
1292 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1293 		if (connp == NULL) {
1294 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1295 		} else {
1296 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1297 			mutex_enter(&ipif->ipif_ill->ill_lock);
1298 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1299 			mutex_exit(&ipif->ipif_ill->ill_lock);
1300 		}
1301 	} else {
1302 		/*
1303 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1304 		 * be just inet_freemsg. we have to restart it
1305 		 * otherwise the thread will be stuck.
1306 		 */
1307 		inet_freemsg(mp);
1308 	}
1309 	return (B_TRUE);
1310 }
1311 
1312 /*
1313  * The ill is closing. Cleanup all the pending mps. Called exclusively
1314  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1315  * knows this ill, and hence nobody can add an mp to this list
1316  */
1317 static void
1318 ill_pending_mp_cleanup(ill_t *ill)
1319 {
1320 	mblk_t	*mp;
1321 	queue_t	*q;
1322 
1323 	ASSERT(IAM_WRITER_ILL(ill));
1324 
1325 	mutex_enter(&ill->ill_lock);
1326 	/*
1327 	 * Every mp on the pending mp list originating from an ioctl
1328 	 * added 1 to the conn refcnt, at the start of the ioctl.
1329 	 * So bump it down now.  See comments in ip_wput_nondata()
1330 	 */
1331 	while (ill->ill_pending_mp != NULL) {
1332 		mp = ill->ill_pending_mp;
1333 		ill->ill_pending_mp = mp->b_next;
1334 		mutex_exit(&ill->ill_lock);
1335 
1336 		q = mp->b_queue;
1337 		ASSERT(CONN_Q(q));
1338 		mp->b_next = NULL;
1339 		mp->b_prev = NULL;
1340 		mp->b_queue = NULL;
1341 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1342 		mutex_enter(&ill->ill_lock);
1343 	}
1344 	ill->ill_pending_ipif = NULL;
1345 
1346 	mutex_exit(&ill->ill_lock);
1347 }
1348 
1349 /*
1350  * Called in the conn close path and ill delete path
1351  */
1352 static void
1353 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1354 {
1355 	ipsq_t	*ipsq;
1356 	mblk_t	*prev;
1357 	mblk_t	*curr;
1358 	mblk_t	*next;
1359 	queue_t	*q;
1360 	mblk_t	*tmp_list = NULL;
1361 
1362 	ASSERT(IAM_WRITER_ILL(ill));
1363 	if (connp != NULL)
1364 		q = CONNP_TO_WQ(connp);
1365 	else
1366 		q = ill->ill_wq;
1367 
1368 	ipsq = ill->ill_phyint->phyint_ipsq;
1369 	/*
1370 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1371 	 * In the case of ioctl from a conn, there can be only 1 mp
1372 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1373 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1374 	 * ioctls meant for this ill form conn's are not flushed. They will
1375 	 * be processed during ipsq_exit and will not find the ill and will
1376 	 * return error.
1377 	 */
1378 	mutex_enter(&ipsq->ipsq_lock);
1379 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1380 	    curr = next) {
1381 		next = curr->b_next;
1382 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1383 			/* Unlink the mblk from the pending mp list */
1384 			if (prev != NULL) {
1385 				prev->b_next = curr->b_next;
1386 			} else {
1387 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1388 				ipsq->ipsq_xopq_mphead = curr->b_next;
1389 			}
1390 			if (ipsq->ipsq_xopq_mptail == curr)
1391 				ipsq->ipsq_xopq_mptail = prev;
1392 			/*
1393 			 * Create a temporary list and release the ipsq lock
1394 			 * New elements are added to the head of the tmp_list
1395 			 */
1396 			curr->b_next = tmp_list;
1397 			tmp_list = curr;
1398 		} else {
1399 			prev = curr;
1400 		}
1401 	}
1402 	mutex_exit(&ipsq->ipsq_lock);
1403 
1404 	while (tmp_list != NULL) {
1405 		curr = tmp_list;
1406 		tmp_list = curr->b_next;
1407 		curr->b_next = NULL;
1408 		curr->b_prev = NULL;
1409 		curr->b_queue = NULL;
1410 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1411 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1412 			    CONN_CLOSE : NO_COPYOUT, NULL);
1413 		} else {
1414 			/*
1415 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1416 			 * this can't be just inet_freemsg. we have to
1417 			 * restart it otherwise the thread will be stuck.
1418 			 */
1419 			inet_freemsg(curr);
1420 		}
1421 	}
1422 }
1423 
1424 /*
1425  * This conn has started closing. Cleanup any pending ioctl from this conn.
1426  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1427  */
1428 void
1429 conn_ioctl_cleanup(conn_t *connp)
1430 {
1431 	mblk_t *curr;
1432 	ipsq_t	*ipsq;
1433 	ill_t	*ill;
1434 	boolean_t refheld;
1435 
1436 	/*
1437 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1438 	 * ioctl has not yet started, the mp is pending in the list headed by
1439 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1440 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1441 	 * is currently executing now the mp is not queued anywhere but
1442 	 * conn_oper_pending_ill is null. The conn close will wait
1443 	 * till the conn_ref drops to zero.
1444 	 */
1445 	mutex_enter(&connp->conn_lock);
1446 	ill = connp->conn_oper_pending_ill;
1447 	if (ill == NULL) {
1448 		mutex_exit(&connp->conn_lock);
1449 		return;
1450 	}
1451 
1452 	curr = ill_pending_mp_get(ill, &connp, 0);
1453 	if (curr != NULL) {
1454 		mutex_exit(&connp->conn_lock);
1455 		CONN_DEC_REF(connp);
1456 		inet_freemsg(curr);
1457 		return;
1458 	}
1459 	/*
1460 	 * We may not be able to refhold the ill if the ill/ipif
1461 	 * is changing. But we need to make sure that the ill will
1462 	 * not vanish. So we just bump up the ill_waiter count.
1463 	 */
1464 	refheld = ill_waiter_inc(ill);
1465 	mutex_exit(&connp->conn_lock);
1466 	if (refheld) {
1467 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1468 			ill_waiter_dcr(ill);
1469 			/*
1470 			 * Check whether this ioctl has started and is
1471 			 * pending now in ipsq_pending_mp. If it is not
1472 			 * found there then check whether this ioctl has
1473 			 * not even started and is in the ipsq_xopq list.
1474 			 */
1475 			if (!ipsq_pending_mp_cleanup(ill, connp))
1476 				ipsq_xopq_mp_cleanup(ill, connp);
1477 			ipsq = ill->ill_phyint->phyint_ipsq;
1478 			ipsq_exit(ipsq);
1479 			return;
1480 		}
1481 	}
1482 
1483 	/*
1484 	 * The ill is also closing and we could not bump up the
1485 	 * ill_waiter_count or we could not enter the ipsq. Leave
1486 	 * the cleanup to ill_delete
1487 	 */
1488 	mutex_enter(&connp->conn_lock);
1489 	while (connp->conn_oper_pending_ill != NULL)
1490 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1491 	mutex_exit(&connp->conn_lock);
1492 	if (refheld)
1493 		ill_waiter_dcr(ill);
1494 }
1495 
1496 /*
1497  * ipcl_walk function for cleaning up conn_*_ill fields.
1498  */
1499 static void
1500 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1501 {
1502 	ill_t	*ill = (ill_t *)arg;
1503 	ire_t	*ire;
1504 
1505 	mutex_enter(&connp->conn_lock);
1506 	if (connp->conn_multicast_ill == ill) {
1507 		/* Revert to late binding */
1508 		connp->conn_multicast_ill = NULL;
1509 		connp->conn_orig_multicast_ifindex = 0;
1510 	}
1511 	if (connp->conn_incoming_ill == ill)
1512 		connp->conn_incoming_ill = NULL;
1513 	if (connp->conn_outgoing_ill == ill)
1514 		connp->conn_outgoing_ill = NULL;
1515 	if (connp->conn_outgoing_pill == ill)
1516 		connp->conn_outgoing_pill = NULL;
1517 	if (connp->conn_nofailover_ill == ill)
1518 		connp->conn_nofailover_ill = NULL;
1519 	if (connp->conn_dhcpinit_ill == ill) {
1520 		connp->conn_dhcpinit_ill = NULL;
1521 		ASSERT(ill->ill_dhcpinit != 0);
1522 		atomic_dec_32(&ill->ill_dhcpinit);
1523 	}
1524 	if (connp->conn_ire_cache != NULL) {
1525 		ire = connp->conn_ire_cache;
1526 		/*
1527 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1528 		 * interface X and ipif coming from interface Y, if interface
1529 		 * X and Y are part of the same IPMPgroup. Thus whenever
1530 		 * interface X goes down, remove all references to it by
1531 		 * checking both on ire_ipif and ire_stq.
1532 		 */
1533 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1534 		    (ire->ire_type == IRE_CACHE &&
1535 		    ire->ire_stq == ill->ill_wq)) {
1536 			connp->conn_ire_cache = NULL;
1537 			mutex_exit(&connp->conn_lock);
1538 			ire_refrele_notr(ire);
1539 			return;
1540 		}
1541 	}
1542 	mutex_exit(&connp->conn_lock);
1543 }
1544 
1545 /* ARGSUSED */
1546 void
1547 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1548 {
1549 	ill_t	*ill = q->q_ptr;
1550 	ipif_t	*ipif;
1551 
1552 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1553 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1554 		ipif_non_duplicate(ipif);
1555 		ipif_down_tail(ipif);
1556 	}
1557 	freemsg(mp);
1558 	ipsq_current_finish(ipsq);
1559 }
1560 
1561 /*
1562  * ill_down_start is called when we want to down this ill and bring it up again
1563  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1564  * all interfaces, but don't tear down any plumbing.
1565  */
1566 boolean_t
1567 ill_down_start(queue_t *q, mblk_t *mp)
1568 {
1569 	ill_t	*ill = q->q_ptr;
1570 	ipif_t	*ipif;
1571 
1572 	ASSERT(IAM_WRITER_ILL(ill));
1573 
1574 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1575 		(void) ipif_down(ipif, NULL, NULL);
1576 
1577 	ill_down(ill);
1578 
1579 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1580 
1581 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1582 
1583 	/*
1584 	 * Atomically test and add the pending mp if references are active.
1585 	 */
1586 	mutex_enter(&ill->ill_lock);
1587 	if (!ill_is_quiescent(ill)) {
1588 		/* call cannot fail since `conn_t *' argument is NULL */
1589 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1590 		    mp, ILL_DOWN);
1591 		mutex_exit(&ill->ill_lock);
1592 		return (B_FALSE);
1593 	}
1594 	mutex_exit(&ill->ill_lock);
1595 	return (B_TRUE);
1596 }
1597 
1598 static void
1599 ill_down(ill_t *ill)
1600 {
1601 	ip_stack_t	*ipst = ill->ill_ipst;
1602 
1603 	/* Blow off any IREs dependent on this ILL. */
1604 	ire_walk(ill_downi, (char *)ill, ipst);
1605 
1606 	/* Remove any conn_*_ill depending on this ill */
1607 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1608 
1609 	if (ill->ill_group != NULL) {
1610 		illgrp_delete(ill);
1611 	}
1612 }
1613 
1614 /*
1615  * ire_walk routine used to delete every IRE that depends on queues
1616  * associated with 'ill'.  (Always called as writer.)
1617  */
1618 static void
1619 ill_downi(ire_t *ire, char *ill_arg)
1620 {
1621 	ill_t	*ill = (ill_t *)ill_arg;
1622 
1623 	/*
1624 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1625 	 * interface X and ipif coming from interface Y, if interface
1626 	 * X and Y are part of the same IPMP group. Thus whenever interface
1627 	 * X goes down, remove all references to it by checking both
1628 	 * on ire_ipif and ire_stq.
1629 	 */
1630 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1631 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1632 		ire_delete(ire);
1633 	}
1634 }
1635 
1636 /*
1637  * Remove ire/nce from the fastpath list.
1638  */
1639 void
1640 ill_fastpath_nack(ill_t *ill)
1641 {
1642 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1643 }
1644 
1645 /* Consume an M_IOCACK of the fastpath probe. */
1646 void
1647 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1648 {
1649 	mblk_t	*mp1 = mp;
1650 
1651 	/*
1652 	 * If this was the first attempt turn on the fastpath probing.
1653 	 */
1654 	mutex_enter(&ill->ill_lock);
1655 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1656 		ill->ill_dlpi_fastpath_state = IDS_OK;
1657 	mutex_exit(&ill->ill_lock);
1658 
1659 	/* Free the M_IOCACK mblk, hold on to the data */
1660 	mp = mp->b_cont;
1661 	freeb(mp1);
1662 	if (mp == NULL)
1663 		return;
1664 	if (mp->b_cont != NULL) {
1665 		/*
1666 		 * Update all IRE's or NCE's that are waiting for
1667 		 * fastpath update.
1668 		 */
1669 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1670 		mp1 = mp->b_cont;
1671 		freeb(mp);
1672 		mp = mp1;
1673 	} else {
1674 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1675 	}
1676 
1677 	freeb(mp);
1678 }
1679 
1680 /*
1681  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1682  * The data portion of the request is a dl_unitdata_req_t template for
1683  * what we would send downstream in the absence of a fastpath confirmation.
1684  */
1685 int
1686 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1687 {
1688 	struct iocblk	*ioc;
1689 	mblk_t	*mp;
1690 
1691 	if (dlur_mp == NULL)
1692 		return (EINVAL);
1693 
1694 	mutex_enter(&ill->ill_lock);
1695 	switch (ill->ill_dlpi_fastpath_state) {
1696 	case IDS_FAILED:
1697 		/*
1698 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1699 		 * support it.
1700 		 */
1701 		mutex_exit(&ill->ill_lock);
1702 		return (ENOTSUP);
1703 	case IDS_UNKNOWN:
1704 		/* This is the first probe */
1705 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1706 		break;
1707 	default:
1708 		break;
1709 	}
1710 	mutex_exit(&ill->ill_lock);
1711 
1712 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1713 		return (EAGAIN);
1714 
1715 	mp->b_cont = copyb(dlur_mp);
1716 	if (mp->b_cont == NULL) {
1717 		freeb(mp);
1718 		return (EAGAIN);
1719 	}
1720 
1721 	ioc = (struct iocblk *)mp->b_rptr;
1722 	ioc->ioc_count = msgdsize(mp->b_cont);
1723 
1724 	putnext(ill->ill_wq, mp);
1725 	return (0);
1726 }
1727 
1728 void
1729 ill_capability_probe(ill_t *ill)
1730 {
1731 	mblk_t	*mp;
1732 
1733 	ASSERT(IAM_WRITER_ILL(ill));
1734 
1735 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1736 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1737 		return;
1738 
1739 	/*
1740 	 * We are starting a new cycle of capability negotiation.
1741 	 * Free up the capab reset messages of any previous incarnation.
1742 	 * We will do a fresh allocation when we get the response to our probe
1743 	 */
1744 	if (ill->ill_capab_reset_mp != NULL) {
1745 		freemsg(ill->ill_capab_reset_mp);
1746 		ill->ill_capab_reset_mp = NULL;
1747 	}
1748 
1749 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1750 
1751 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1752 	if (mp == NULL)
1753 		return;
1754 
1755 	ill_capability_send(ill, mp);
1756 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1757 }
1758 
1759 void
1760 ill_capability_reset(ill_t *ill, boolean_t reneg)
1761 {
1762 	ASSERT(IAM_WRITER_ILL(ill));
1763 
1764 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1765 		return;
1766 
1767 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1768 
1769 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1770 	ill->ill_capab_reset_mp = NULL;
1771 	/*
1772 	 * We turn off all capabilities except those pertaining to
1773 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1774 	 * which will be turned off by the corresponding reset functions.
1775 	 */
1776 	ill->ill_capabilities &= ~(ILL_CAPAB_MDT | ILL_CAPAB_HCKSUM  |
1777 	    ILL_CAPAB_ZEROCOPY | ILL_CAPAB_AH | ILL_CAPAB_ESP);
1778 }
1779 
1780 static void
1781 ill_capability_reset_alloc(ill_t *ill)
1782 {
1783 	mblk_t *mp;
1784 	size_t	size = 0;
1785 	int	err;
1786 	dl_capability_req_t	*capb;
1787 
1788 	ASSERT(IAM_WRITER_ILL(ill));
1789 	ASSERT(ill->ill_capab_reset_mp == NULL);
1790 
1791 	if (ILL_MDT_CAPABLE(ill))
1792 		size += sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1793 
1794 	if (ILL_HCKSUM_CAPABLE(ill)) {
1795 		size += sizeof (dl_capability_sub_t) +
1796 		    sizeof (dl_capab_hcksum_t);
1797 	}
1798 
1799 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1800 		size += sizeof (dl_capability_sub_t) +
1801 		    sizeof (dl_capab_zerocopy_t);
1802 	}
1803 
1804 	if (ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) {
1805 		size += sizeof (dl_capability_sub_t);
1806 		size += ill_capability_ipsec_reset_size(ill, NULL, NULL,
1807 		    NULL, NULL);
1808 	}
1809 
1810 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1811 		size += sizeof (dl_capability_sub_t) +
1812 		    sizeof (dl_capab_dld_t);
1813 	}
1814 
1815 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1816 	    STR_NOSIG, &err);
1817 
1818 	mp->b_datap->db_type = M_PROTO;
1819 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1820 
1821 	capb = (dl_capability_req_t *)mp->b_rptr;
1822 	capb->dl_primitive = DL_CAPABILITY_REQ;
1823 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1824 	capb->dl_sub_length = size;
1825 
1826 	mp->b_wptr += sizeof (dl_capability_req_t);
1827 
1828 	/*
1829 	 * Each handler fills in the corresponding dl_capability_sub_t
1830 	 * inside the mblk,
1831 	 */
1832 	ill_capability_mdt_reset_fill(ill, mp);
1833 	ill_capability_hcksum_reset_fill(ill, mp);
1834 	ill_capability_zerocopy_reset_fill(ill, mp);
1835 	ill_capability_ipsec_reset_fill(ill, mp);
1836 	ill_capability_dld_reset_fill(ill, mp);
1837 
1838 	ill->ill_capab_reset_mp = mp;
1839 }
1840 
1841 static void
1842 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1843 {
1844 	dl_capab_id_t *id_ic;
1845 	uint_t sub_dl_cap = outers->dl_cap;
1846 	dl_capability_sub_t *inners;
1847 	uint8_t *capend;
1848 
1849 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1850 
1851 	/*
1852 	 * Note: range checks here are not absolutely sufficient to
1853 	 * make us robust against malformed messages sent by drivers;
1854 	 * this is in keeping with the rest of IP's dlpi handling.
1855 	 * (Remember, it's coming from something else in the kernel
1856 	 * address space)
1857 	 */
1858 
1859 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1860 	if (capend > mp->b_wptr) {
1861 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1862 		    "malformed sub-capability too long for mblk");
1863 		return;
1864 	}
1865 
1866 	id_ic = (dl_capab_id_t *)(outers + 1);
1867 
1868 	if (outers->dl_length < sizeof (*id_ic) ||
1869 	    (inners = &id_ic->id_subcap,
1870 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1871 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1872 		    "encapsulated capab type %d too long for mblk",
1873 		    inners->dl_cap);
1874 		return;
1875 	}
1876 
1877 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1878 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1879 		    "isn't as expected; pass-thru module(s) detected, "
1880 		    "discarding capability\n", inners->dl_cap));
1881 		return;
1882 	}
1883 
1884 	/* Process the encapsulated sub-capability */
1885 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1886 }
1887 
1888 /*
1889  * Process Multidata Transmit capability negotiation ack received from a
1890  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1891  * DL_CAPABILITY_ACK message.
1892  */
1893 static void
1894 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1895 {
1896 	mblk_t *nmp = NULL;
1897 	dl_capability_req_t *oc;
1898 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1899 	ill_mdt_capab_t **ill_mdt_capab;
1900 	uint_t sub_dl_cap = isub->dl_cap;
1901 	uint8_t *capend;
1902 
1903 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1904 
1905 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1906 
1907 	/*
1908 	 * Note: range checks here are not absolutely sufficient to
1909 	 * make us robust against malformed messages sent by drivers;
1910 	 * this is in keeping with the rest of IP's dlpi handling.
1911 	 * (Remember, it's coming from something else in the kernel
1912 	 * address space)
1913 	 */
1914 
1915 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 
1922 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1923 
1924 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1925 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1926 		    "unsupported MDT sub-capability (version %d, expected %d)",
1927 		    mdt_ic->mdt_version, MDT_VERSION_2);
1928 		return;
1929 	}
1930 
1931 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1932 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1933 		    "capability isn't as expected; pass-thru module(s) "
1934 		    "detected, discarding capability\n"));
1935 		return;
1936 	}
1937 
1938 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1939 
1940 		if (*ill_mdt_capab == NULL) {
1941 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1942 			    KM_NOSLEEP);
1943 			if (*ill_mdt_capab == NULL) {
1944 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1945 				    "could not enable MDT version %d "
1946 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1947 				    ill->ill_name);
1948 				return;
1949 			}
1950 		}
1951 
1952 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1953 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1954 		    "header spaces, %d max pld bufs, %d span limit)\n",
1955 		    ill->ill_name, MDT_VERSION_2,
1956 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1957 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1958 
1959 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1960 		(*ill_mdt_capab)->ill_mdt_on = 1;
1961 		/*
1962 		 * Round the following values to the nearest 32-bit; ULP
1963 		 * may further adjust them to accomodate for additional
1964 		 * protocol headers.  We pass these values to ULP during
1965 		 * bind time.
1966 		 */
1967 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1968 		    roundup(mdt_ic->mdt_hdr_head, 4);
1969 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1970 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1971 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1972 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1973 
1974 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1975 	} else {
1976 		uint_t size;
1977 		uchar_t *rptr;
1978 
1979 		size = sizeof (dl_capability_req_t) +
1980 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1981 
1982 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1983 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1984 			    "could not enable MDT for %s (ENOMEM)\n",
1985 			    ill->ill_name);
1986 			return;
1987 		}
1988 
1989 		rptr = nmp->b_rptr;
1990 		/* initialize dl_capability_req_t */
1991 		oc = (dl_capability_req_t *)nmp->b_rptr;
1992 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1993 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1994 		    sizeof (dl_capab_mdt_t);
1995 		nmp->b_rptr += sizeof (dl_capability_req_t);
1996 
1997 		/* initialize dl_capability_sub_t */
1998 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1999 		nmp->b_rptr += sizeof (*isub);
2000 
2001 		/* initialize dl_capab_mdt_t */
2002 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2003 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2004 
2005 		nmp->b_rptr = rptr;
2006 
2007 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2008 		    "to enable MDT version %d\n", ill->ill_name,
2009 		    MDT_VERSION_2));
2010 
2011 		/* set ENABLE flag */
2012 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2013 
2014 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2015 		ill_capability_send(ill, nmp);
2016 	}
2017 }
2018 
2019 static void
2020 ill_capability_mdt_reset_fill(ill_t *ill, mblk_t *mp)
2021 {
2022 	dl_capab_mdt_t *mdt_subcap;
2023 	dl_capability_sub_t *dl_subcap;
2024 
2025 	if (!ILL_MDT_CAPABLE(ill))
2026 		return;
2027 
2028 	ASSERT(ill->ill_mdt_capab != NULL);
2029 
2030 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2031 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2032 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2033 
2034 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2035 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2036 	mdt_subcap->mdt_flags = 0;
2037 	mdt_subcap->mdt_hdr_head = 0;
2038 	mdt_subcap->mdt_hdr_tail = 0;
2039 
2040 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2041 }
2042 
2043 static void
2044 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
2045 {
2046 	dl_capability_sub_t *dl_subcap;
2047 
2048 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2049 		return;
2050 
2051 	/*
2052 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
2053 	 * initialized below since it is not used by DLD.
2054 	 */
2055 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2056 	dl_subcap->dl_cap = DL_CAPAB_DLD;
2057 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
2058 
2059 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
2060 }
2061 
2062 /*
2063  * Send a DL_NOTIFY_REQ to the specified ill to enable
2064  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2065  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2066  * acceleration.
2067  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2068  */
2069 static boolean_t
2070 ill_enable_promisc_notify(ill_t *ill)
2071 {
2072 	mblk_t *mp;
2073 	dl_notify_req_t *req;
2074 
2075 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2076 
2077 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2078 	if (mp == NULL)
2079 		return (B_FALSE);
2080 
2081 	req = (dl_notify_req_t *)mp->b_rptr;
2082 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2083 	    DL_NOTE_PROMISC_OFF_PHYS;
2084 
2085 	ill_dlpi_send(ill, mp);
2086 
2087 	return (B_TRUE);
2088 }
2089 
2090 /*
2091  * Allocate an IPsec capability request which will be filled by our
2092  * caller to turn on support for one or more algorithms.
2093  */
2094 static mblk_t *
2095 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2096 {
2097 	mblk_t *nmp;
2098 	dl_capability_req_t	*ocap;
2099 	dl_capab_ipsec_t	*ocip;
2100 	dl_capab_ipsec_t	*icip;
2101 	uint8_t			*ptr;
2102 	icip = (dl_capab_ipsec_t *)(isub + 1);
2103 
2104 	/*
2105 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2106 	 * PROMISC_ON/OFF notification from the provider. We need to
2107 	 * do this before enabling the algorithms to avoid leakage of
2108 	 * cleartext packets.
2109 	 */
2110 
2111 	if (!ill_enable_promisc_notify(ill))
2112 		return (NULL);
2113 
2114 	/*
2115 	 * Allocate new mblk which will contain a new capability
2116 	 * request to enable the capabilities.
2117 	 */
2118 
2119 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2120 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2121 	if (nmp == NULL)
2122 		return (NULL);
2123 
2124 	ptr = nmp->b_rptr;
2125 
2126 	/* initialize dl_capability_req_t */
2127 	ocap = (dl_capability_req_t *)ptr;
2128 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2129 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2130 	ptr += sizeof (dl_capability_req_t);
2131 
2132 	/* initialize dl_capability_sub_t */
2133 	bcopy(isub, ptr, sizeof (*isub));
2134 	ptr += sizeof (*isub);
2135 
2136 	/* initialize dl_capab_ipsec_t */
2137 	ocip = (dl_capab_ipsec_t *)ptr;
2138 	bcopy(icip, ocip, sizeof (*icip));
2139 
2140 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2141 	return (nmp);
2142 }
2143 
2144 /*
2145  * Process an IPsec capability negotiation ack received from a DLS Provider.
2146  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2147  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2148  */
2149 static void
2150 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2151 {
2152 	dl_capab_ipsec_t	*icip;
2153 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2154 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2155 	uint_t cipher, nciphers;
2156 	mblk_t *nmp;
2157 	uint_t alg_len;
2158 	boolean_t need_sadb_dump;
2159 	uint_t sub_dl_cap = isub->dl_cap;
2160 	ill_ipsec_capab_t **ill_capab;
2161 	uint64_t ill_capab_flag;
2162 	uint8_t *capend, *ciphend;
2163 	boolean_t sadb_resync;
2164 
2165 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2166 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2167 
2168 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2169 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2170 		ill_capab_flag = ILL_CAPAB_AH;
2171 	} else {
2172 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2173 		ill_capab_flag = ILL_CAPAB_ESP;
2174 	}
2175 
2176 	/*
2177 	 * If the ill capability structure exists, then this incoming
2178 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2179 	 * If this is so, then we'd need to resynchronize the SADB
2180 	 * after re-enabling the offloaded ciphers.
2181 	 */
2182 	sadb_resync = (*ill_capab != NULL);
2183 
2184 	/*
2185 	 * Note: range checks here are not absolutely sufficient to
2186 	 * make us robust against malformed messages sent by drivers;
2187 	 * this is in keeping with the rest of IP's dlpi handling.
2188 	 * (Remember, it's coming from something else in the kernel
2189 	 * address space)
2190 	 */
2191 
2192 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2193 	if (capend > mp->b_wptr) {
2194 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2195 		    "malformed sub-capability too long for mblk");
2196 		return;
2197 	}
2198 
2199 	/*
2200 	 * There are two types of acks we process here:
2201 	 * 1. acks in reply to a (first form) generic capability req
2202 	 *    (no ENABLE flag set)
2203 	 * 2. acks in reply to a ENABLE capability req.
2204 	 *    (ENABLE flag set)
2205 	 *
2206 	 * We process the subcapability passed as argument as follows:
2207 	 * 1 do initializations
2208 	 *   1.1 initialize nmp = NULL
2209 	 *   1.2 set need_sadb_dump to B_FALSE
2210 	 * 2 for each cipher in subcapability:
2211 	 *   2.1 if ENABLE flag is set:
2212 	 *	2.1.1 update per-ill ipsec capabilities info
2213 	 *	2.1.2 set need_sadb_dump to B_TRUE
2214 	 *   2.2 if ENABLE flag is not set:
2215 	 *	2.2.1 if nmp is NULL:
2216 	 *		2.2.1.1 allocate and initialize nmp
2217 	 *		2.2.1.2 init current pos in nmp
2218 	 *	2.2.2 copy current cipher to current pos in nmp
2219 	 *	2.2.3 set ENABLE flag in nmp
2220 	 *	2.2.4 update current pos
2221 	 * 3 if nmp is not equal to NULL, send enable request
2222 	 *   3.1 send capability request
2223 	 * 4 if need_sadb_dump is B_TRUE
2224 	 *   4.1 enable promiscuous on/off notifications
2225 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2226 	 *	AH or ESP SA's to interface.
2227 	 */
2228 
2229 	nmp = NULL;
2230 	oalg = NULL;
2231 	need_sadb_dump = B_FALSE;
2232 	icip = (dl_capab_ipsec_t *)(isub + 1);
2233 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2234 
2235 	nciphers = icip->cip_nciphers;
2236 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2237 
2238 	if (ciphend > capend) {
2239 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2240 		    "too many ciphers for sub-capability len");
2241 		return;
2242 	}
2243 
2244 	for (cipher = 0; cipher < nciphers; cipher++) {
2245 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2246 
2247 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2248 			/*
2249 			 * TBD: when we provide a way to disable capabilities
2250 			 * from above, need to manage the request-pending state
2251 			 * and fail if we were not expecting this ACK.
2252 			 */
2253 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2254 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2255 
2256 			/*
2257 			 * Update IPsec capabilities for this ill
2258 			 */
2259 
2260 			if (*ill_capab == NULL) {
2261 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2262 				    ("ill_capability_ipsec_ack: "
2263 				    "allocating ipsec_capab for ill\n"));
2264 				*ill_capab = ill_ipsec_capab_alloc();
2265 
2266 				if (*ill_capab == NULL) {
2267 					cmn_err(CE_WARN,
2268 					    "ill_capability_ipsec_ack: "
2269 					    "could not enable IPsec Hardware "
2270 					    "acceleration for %s (ENOMEM)\n",
2271 					    ill->ill_name);
2272 					return;
2273 				}
2274 			}
2275 
2276 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2277 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2278 
2279 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2280 				cmn_err(CE_WARN,
2281 				    "ill_capability_ipsec_ack: "
2282 				    "malformed IPsec algorithm id %d",
2283 				    ialg->alg_prim);
2284 				continue;
2285 			}
2286 
2287 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2288 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2289 				    ialg->alg_prim);
2290 			} else {
2291 				ipsec_capab_algparm_t *alp;
2292 
2293 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2294 				    ialg->alg_prim);
2295 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2296 				    ialg->alg_prim)) {
2297 					cmn_err(CE_WARN,
2298 					    "ill_capability_ipsec_ack: "
2299 					    "no space for IPsec alg id %d",
2300 					    ialg->alg_prim);
2301 					continue;
2302 				}
2303 				alp = &((*ill_capab)->encr_algparm[
2304 				    ialg->alg_prim]);
2305 				alp->minkeylen = ialg->alg_minbits;
2306 				alp->maxkeylen = ialg->alg_maxbits;
2307 			}
2308 			ill->ill_capabilities |= ill_capab_flag;
2309 			/*
2310 			 * indicate that a capability was enabled, which
2311 			 * will be used below to kick off a SADB dump
2312 			 * to the ill.
2313 			 */
2314 			need_sadb_dump = B_TRUE;
2315 		} else {
2316 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2317 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2318 			    ialg->alg_prim));
2319 
2320 			if (nmp == NULL) {
2321 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2322 				if (nmp == NULL) {
2323 					/*
2324 					 * Sending the PROMISC_ON/OFF
2325 					 * notification request failed.
2326 					 * We cannot enable the algorithms
2327 					 * since the Provider will not
2328 					 * notify IP of promiscous mode
2329 					 * changes, which could lead
2330 					 * to leakage of packets.
2331 					 */
2332 					cmn_err(CE_WARN,
2333 					    "ill_capability_ipsec_ack: "
2334 					    "could not enable IPsec Hardware "
2335 					    "acceleration for %s (ENOMEM)\n",
2336 					    ill->ill_name);
2337 					return;
2338 				}
2339 				/* ptr to current output alg specifier */
2340 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2341 			}
2342 
2343 			/*
2344 			 * Copy current alg specifier, set ENABLE
2345 			 * flag, and advance to next output alg.
2346 			 * For now we enable all IPsec capabilities.
2347 			 */
2348 			ASSERT(oalg != NULL);
2349 			bcopy(ialg, oalg, alg_len);
2350 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2351 			nmp->b_wptr += alg_len;
2352 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2353 		}
2354 
2355 		/* move to next input algorithm specifier */
2356 		ialg = (dl_capab_ipsec_alg_t *)
2357 		    ((char *)ialg + alg_len);
2358 	}
2359 
2360 	if (nmp != NULL)
2361 		/*
2362 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2363 		 * IPsec hardware acceleration.
2364 		 */
2365 		ill_capability_send(ill, nmp);
2366 
2367 	if (need_sadb_dump)
2368 		/*
2369 		 * An acknowledgement corresponding to a request to
2370 		 * enable acceleration was received, notify SADB.
2371 		 */
2372 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2373 }
2374 
2375 /*
2376  * Given an mblk with enough space in it, create sub-capability entries for
2377  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2378  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2379  * in preparation for the reset the DL_CAPABILITY_REQ message.
2380  */
2381 static void
2382 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2383     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2384 {
2385 	dl_capab_ipsec_t *oipsec;
2386 	dl_capab_ipsec_alg_t *oalg;
2387 	dl_capability_sub_t *dl_subcap;
2388 	int i, k;
2389 
2390 	ASSERT(nciphers > 0);
2391 	ASSERT(ill_cap != NULL);
2392 	ASSERT(mp != NULL);
2393 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2394 
2395 	/* dl_capability_sub_t for "stype" */
2396 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2397 	dl_subcap->dl_cap = stype;
2398 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2399 	mp->b_wptr += sizeof (dl_capability_sub_t);
2400 
2401 	/* dl_capab_ipsec_t for "stype" */
2402 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2403 	oipsec->cip_version = 1;
2404 	oipsec->cip_nciphers = nciphers;
2405 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2406 
2407 	/* create entries for "stype" AUTH ciphers */
2408 	for (i = 0; i < ill_cap->algs_size; i++) {
2409 		for (k = 0; k < BITSPERBYTE; k++) {
2410 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2411 				continue;
2412 
2413 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2414 			bzero((void *)oalg, sizeof (*oalg));
2415 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2416 			oalg->alg_prim = k + (BITSPERBYTE * i);
2417 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2418 		}
2419 	}
2420 	/* create entries for "stype" ENCR ciphers */
2421 	for (i = 0; i < ill_cap->algs_size; i++) {
2422 		for (k = 0; k < BITSPERBYTE; k++) {
2423 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2424 				continue;
2425 
2426 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2427 			bzero((void *)oalg, sizeof (*oalg));
2428 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2429 			oalg->alg_prim = k + (BITSPERBYTE * i);
2430 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2431 		}
2432 	}
2433 }
2434 
2435 /*
2436  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2437  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2438  * POPC instruction, but our macro is more flexible for an arbitrary length
2439  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2440  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2441  * stays that way, we can reduce the number of iterations required.
2442  */
2443 #define	COUNT_1S(val, sum) {					\
2444 	uint8_t x = val & 0xff;					\
2445 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2446 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2447 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2448 }
2449 
2450 /* ARGSUSED */
2451 static int
2452 ill_capability_ipsec_reset_size(ill_t *ill, int *ah_cntp, int *ah_lenp,
2453     int *esp_cntp, int *esp_lenp)
2454 {
2455 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2456 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2457 	uint64_t ill_capabilities = ill->ill_capabilities;
2458 	int ah_cnt = 0, esp_cnt = 0;
2459 	int ah_len = 0, esp_len = 0;
2460 	int i, size = 0;
2461 
2462 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2463 		return (0);
2464 
2465 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2466 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2467 
2468 	/* Find out the number of ciphers for AH */
2469 	if (cap_ah != NULL) {
2470 		for (i = 0; i < cap_ah->algs_size; i++) {
2471 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2472 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2473 		}
2474 		if (ah_cnt > 0) {
2475 			size += sizeof (dl_capability_sub_t) +
2476 			    sizeof (dl_capab_ipsec_t);
2477 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2478 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2479 			size += ah_len;
2480 		}
2481 	}
2482 
2483 	/* Find out the number of ciphers for ESP */
2484 	if (cap_esp != NULL) {
2485 		for (i = 0; i < cap_esp->algs_size; i++) {
2486 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2487 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2488 		}
2489 		if (esp_cnt > 0) {
2490 			size += sizeof (dl_capability_sub_t) +
2491 			    sizeof (dl_capab_ipsec_t);
2492 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2493 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2494 			size += esp_len;
2495 		}
2496 	}
2497 
2498 	if (ah_cntp != NULL)
2499 		*ah_cntp = ah_cnt;
2500 	if (ah_lenp != NULL)
2501 		*ah_lenp = ah_len;
2502 	if (esp_cntp != NULL)
2503 		*esp_cntp = esp_cnt;
2504 	if (esp_lenp != NULL)
2505 		*esp_lenp = esp_len;
2506 
2507 	return (size);
2508 }
2509 
2510 /* ARGSUSED */
2511 static void
2512 ill_capability_ipsec_reset_fill(ill_t *ill, mblk_t *mp)
2513 {
2514 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2515 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2516 	int ah_cnt = 0, esp_cnt = 0;
2517 	int ah_len = 0, esp_len = 0;
2518 	int size;
2519 
2520 	size = ill_capability_ipsec_reset_size(ill, &ah_cnt, &ah_len,
2521 	    &esp_cnt, &esp_len);
2522 	if (size == 0)
2523 		return;
2524 
2525 	/*
2526 	 * Clear the capability flags for IPsec HA but retain the ill
2527 	 * capability structures since it's possible that another thread
2528 	 * is still referring to them.  The structures only get deallocated
2529 	 * when we destroy the ill.
2530 	 *
2531 	 * Various places check the flags to see if the ill is capable of
2532 	 * hardware acceleration, and by clearing them we ensure that new
2533 	 * outbound IPsec packets are sent down encrypted.
2534 	 */
2535 
2536 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2537 	if (ah_cnt > 0) {
2538 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2539 		    cap_ah, mp);
2540 	}
2541 
2542 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2543 	if (esp_cnt > 0) {
2544 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2545 		    cap_esp, mp);
2546 	}
2547 
2548 	/*
2549 	 * At this point we've composed a bunch of sub-capabilities to be
2550 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2551 	 * by the caller.  Upon receiving this reset message, the driver
2552 	 * must stop inbound decryption (by destroying all inbound SAs)
2553 	 * and let the corresponding packets come in encrypted.
2554 	 */
2555 }
2556 
2557 static void
2558 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2559     boolean_t encapsulated)
2560 {
2561 	boolean_t legacy = B_FALSE;
2562 
2563 	/*
2564 	 * Note that only the following two sub-capabilities may be
2565 	 * considered as "legacy", since their original definitions
2566 	 * do not incorporate the dl_mid_t module ID token, and hence
2567 	 * may require the use of the wrapper sub-capability.
2568 	 */
2569 	switch (subp->dl_cap) {
2570 	case DL_CAPAB_IPSEC_AH:
2571 	case DL_CAPAB_IPSEC_ESP:
2572 		legacy = B_TRUE;
2573 		break;
2574 	}
2575 
2576 	/*
2577 	 * For legacy sub-capabilities which don't incorporate a queue_t
2578 	 * pointer in their structures, discard them if we detect that
2579 	 * there are intermediate modules in between IP and the driver.
2580 	 */
2581 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2582 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2583 		    "%d discarded; %d module(s) present below IP\n",
2584 		    subp->dl_cap, ill->ill_lmod_cnt));
2585 		return;
2586 	}
2587 
2588 	switch (subp->dl_cap) {
2589 	case DL_CAPAB_IPSEC_AH:
2590 	case DL_CAPAB_IPSEC_ESP:
2591 		ill_capability_ipsec_ack(ill, mp, subp);
2592 		break;
2593 	case DL_CAPAB_MDT:
2594 		ill_capability_mdt_ack(ill, mp, subp);
2595 		break;
2596 	case DL_CAPAB_HCKSUM:
2597 		ill_capability_hcksum_ack(ill, mp, subp);
2598 		break;
2599 	case DL_CAPAB_ZEROCOPY:
2600 		ill_capability_zerocopy_ack(ill, mp, subp);
2601 		break;
2602 	case DL_CAPAB_DLD:
2603 		ill_capability_dld_ack(ill, mp, subp);
2604 		break;
2605 	default:
2606 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2607 		    subp->dl_cap));
2608 	}
2609 }
2610 
2611 /*
2612  * Process a hardware checksum offload capability negotiation ack received
2613  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
2614  * of a DL_CAPABILITY_ACK message.
2615  */
2616 static void
2617 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2618 {
2619 	dl_capability_req_t	*ocap;
2620 	dl_capab_hcksum_t	*ihck, *ohck;
2621 	ill_hcksum_capab_t	**ill_hcksum;
2622 	mblk_t			*nmp = NULL;
2623 	uint_t			sub_dl_cap = isub->dl_cap;
2624 	uint8_t			*capend;
2625 
2626 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
2627 
2628 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
2629 
2630 	/*
2631 	 * Note: range checks here are not absolutely sufficient to
2632 	 * make us robust against malformed messages sent by drivers;
2633 	 * this is in keeping with the rest of IP's dlpi handling.
2634 	 * (Remember, it's coming from something else in the kernel
2635 	 * address space)
2636 	 */
2637 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2638 	if (capend > mp->b_wptr) {
2639 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2640 		    "malformed sub-capability too long for mblk");
2641 		return;
2642 	}
2643 
2644 	/*
2645 	 * There are two types of acks we process here:
2646 	 * 1. acks in reply to a (first form) generic capability req
2647 	 *    (no ENABLE flag set)
2648 	 * 2. acks in reply to a ENABLE capability req.
2649 	 *    (ENABLE flag set)
2650 	 */
2651 	ihck = (dl_capab_hcksum_t *)(isub + 1);
2652 
2653 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
2654 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
2655 		    "unsupported hardware checksum "
2656 		    "sub-capability (version %d, expected %d)",
2657 		    ihck->hcksum_version, HCKSUM_VERSION_1);
2658 		return;
2659 	}
2660 
2661 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
2662 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
2663 		    "checksum capability isn't as expected; pass-thru "
2664 		    "module(s) detected, discarding capability\n"));
2665 		return;
2666 	}
2667 
2668 #define	CURR_HCKSUM_CAPAB				\
2669 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
2670 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
2671 
2672 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
2673 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
2674 		/* do ENABLE processing */
2675 		if (*ill_hcksum == NULL) {
2676 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
2677 			    KM_NOSLEEP);
2678 
2679 			if (*ill_hcksum == NULL) {
2680 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2681 				    "could not enable hcksum version %d "
2682 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
2683 				    ill->ill_name);
2684 				return;
2685 			}
2686 		}
2687 
2688 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
2689 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
2690 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
2691 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
2692 		    "has enabled hardware checksumming\n ",
2693 		    ill->ill_name));
2694 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
2695 		/*
2696 		 * Enabling hardware checksum offload
2697 		 * Currently IP supports {TCP,UDP}/IPv4
2698 		 * partial and full cksum offload and
2699 		 * IPv4 header checksum offload.
2700 		 * Allocate new mblk which will
2701 		 * contain a new capability request
2702 		 * to enable hardware checksum offload.
2703 		 */
2704 		uint_t	size;
2705 		uchar_t	*rptr;
2706 
2707 		size = sizeof (dl_capability_req_t) +
2708 		    sizeof (dl_capability_sub_t) + isub->dl_length;
2709 
2710 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2711 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
2712 			    "could not enable hardware cksum for %s (ENOMEM)\n",
2713 			    ill->ill_name);
2714 			return;
2715 		}
2716 
2717 		rptr = nmp->b_rptr;
2718 		/* initialize dl_capability_req_t */
2719 		ocap = (dl_capability_req_t *)nmp->b_rptr;
2720 		ocap->dl_sub_offset =
2721 		    sizeof (dl_capability_req_t);
2722 		ocap->dl_sub_length =
2723 		    sizeof (dl_capability_sub_t) +
2724 		    isub->dl_length;
2725 		nmp->b_rptr += sizeof (dl_capability_req_t);
2726 
2727 		/* initialize dl_capability_sub_t */
2728 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2729 		nmp->b_rptr += sizeof (*isub);
2730 
2731 		/* initialize dl_capab_hcksum_t */
2732 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
2733 		bcopy(ihck, ohck, sizeof (*ihck));
2734 
2735 		nmp->b_rptr = rptr;
2736 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2737 
2738 		/* Set ENABLE flag */
2739 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
2740 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
2741 
2742 		/*
2743 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2744 		 * hardware checksum acceleration.
2745 		 */
2746 		ill_capability_send(ill, nmp);
2747 	} else {
2748 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
2749 		    "advertised %x hardware checksum capability flags\n",
2750 		    ill->ill_name, ihck->hcksum_txflags));
2751 	}
2752 }
2753 
2754 static void
2755 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
2756 {
2757 	dl_capab_hcksum_t *hck_subcap;
2758 	dl_capability_sub_t *dl_subcap;
2759 
2760 	if (!ILL_HCKSUM_CAPABLE(ill))
2761 		return;
2762 
2763 	ASSERT(ill->ill_hcksum_capab != NULL);
2764 
2765 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2766 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
2767 	dl_subcap->dl_length = sizeof (*hck_subcap);
2768 
2769 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
2770 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
2771 	hck_subcap->hcksum_txflags = 0;
2772 
2773 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
2774 }
2775 
2776 static void
2777 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2778 {
2779 	mblk_t *nmp = NULL;
2780 	dl_capability_req_t *oc;
2781 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
2782 	ill_zerocopy_capab_t **ill_zerocopy_capab;
2783 	uint_t sub_dl_cap = isub->dl_cap;
2784 	uint8_t *capend;
2785 
2786 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
2787 
2788 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
2789 
2790 	/*
2791 	 * Note: range checks here are not absolutely sufficient to
2792 	 * make us robust against malformed messages sent by drivers;
2793 	 * this is in keeping with the rest of IP's dlpi handling.
2794 	 * (Remember, it's coming from something else in the kernel
2795 	 * address space)
2796 	 */
2797 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2798 	if (capend > mp->b_wptr) {
2799 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2800 		    "malformed sub-capability too long for mblk");
2801 		return;
2802 	}
2803 
2804 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
2805 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
2806 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
2807 		    "unsupported ZEROCOPY sub-capability (version %d, "
2808 		    "expected %d)", zc_ic->zerocopy_version,
2809 		    ZEROCOPY_VERSION_1);
2810 		return;
2811 	}
2812 
2813 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
2814 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
2815 		    "capability isn't as expected; pass-thru module(s) "
2816 		    "detected, discarding capability\n"));
2817 		return;
2818 	}
2819 
2820 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
2821 		if (*ill_zerocopy_capab == NULL) {
2822 			*ill_zerocopy_capab =
2823 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
2824 			    KM_NOSLEEP);
2825 
2826 			if (*ill_zerocopy_capab == NULL) {
2827 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2828 				    "could not enable Zero-copy version %d "
2829 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
2830 				    ill->ill_name);
2831 				return;
2832 			}
2833 		}
2834 
2835 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
2836 		    "supports Zero-copy version %d\n", ill->ill_name,
2837 		    ZEROCOPY_VERSION_1));
2838 
2839 		(*ill_zerocopy_capab)->ill_zerocopy_version =
2840 		    zc_ic->zerocopy_version;
2841 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
2842 		    zc_ic->zerocopy_flags;
2843 
2844 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
2845 	} else {
2846 		uint_t size;
2847 		uchar_t *rptr;
2848 
2849 		size = sizeof (dl_capability_req_t) +
2850 		    sizeof (dl_capability_sub_t) +
2851 		    sizeof (dl_capab_zerocopy_t);
2852 
2853 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2854 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
2855 			    "could not enable zerocopy for %s (ENOMEM)\n",
2856 			    ill->ill_name);
2857 			return;
2858 		}
2859 
2860 		rptr = nmp->b_rptr;
2861 		/* initialize dl_capability_req_t */
2862 		oc = (dl_capability_req_t *)rptr;
2863 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2864 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2865 		    sizeof (dl_capab_zerocopy_t);
2866 		rptr += sizeof (dl_capability_req_t);
2867 
2868 		/* initialize dl_capability_sub_t */
2869 		bcopy(isub, rptr, sizeof (*isub));
2870 		rptr += sizeof (*isub);
2871 
2872 		/* initialize dl_capab_zerocopy_t */
2873 		zc_oc = (dl_capab_zerocopy_t *)rptr;
2874 		*zc_oc = *zc_ic;
2875 
2876 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
2877 		    "to enable zero-copy version %d\n", ill->ill_name,
2878 		    ZEROCOPY_VERSION_1));
2879 
2880 		/* set VMSAFE_MEM flag */
2881 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
2882 
2883 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
2884 		ill_capability_send(ill, nmp);
2885 	}
2886 }
2887 
2888 static void
2889 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
2890 {
2891 	dl_capab_zerocopy_t *zerocopy_subcap;
2892 	dl_capability_sub_t *dl_subcap;
2893 
2894 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
2895 		return;
2896 
2897 	ASSERT(ill->ill_zerocopy_capab != NULL);
2898 
2899 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2900 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
2901 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
2902 
2903 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
2904 	zerocopy_subcap->zerocopy_version =
2905 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
2906 	zerocopy_subcap->zerocopy_flags = 0;
2907 
2908 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
2909 }
2910 
2911 /*
2912  * DLD capability
2913  * Refer to dld.h for more information regarding the purpose and usage
2914  * of this capability.
2915  */
2916 static void
2917 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2918 {
2919 	dl_capab_dld_t		*dld_ic, dld;
2920 	uint_t			sub_dl_cap = isub->dl_cap;
2921 	uint8_t			*capend;
2922 	ill_dld_capab_t		*idc;
2923 
2924 	ASSERT(IAM_WRITER_ILL(ill));
2925 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
2926 
2927 	/*
2928 	 * Note: range checks here are not absolutely sufficient to
2929 	 * make us robust against malformed messages sent by drivers;
2930 	 * this is in keeping with the rest of IP's dlpi handling.
2931 	 * (Remember, it's coming from something else in the kernel
2932 	 * address space)
2933 	 */
2934 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2935 	if (capend > mp->b_wptr) {
2936 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
2937 		    "malformed sub-capability too long for mblk");
2938 		return;
2939 	}
2940 	dld_ic = (dl_capab_dld_t *)(isub + 1);
2941 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
2942 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
2943 		    "unsupported DLD sub-capability (version %d, "
2944 		    "expected %d)", dld_ic->dld_version,
2945 		    DLD_CURRENT_VERSION);
2946 		return;
2947 	}
2948 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
2949 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
2950 		    "capability isn't as expected; pass-thru module(s) "
2951 		    "detected, discarding capability\n"));
2952 		return;
2953 	}
2954 
2955 	/*
2956 	 * Copy locally to ensure alignment.
2957 	 */
2958 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
2959 
2960 	if ((idc = ill->ill_dld_capab) == NULL) {
2961 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
2962 		if (idc == NULL) {
2963 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
2964 			    "could not enable DLD version %d "
2965 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
2966 			    ill->ill_name);
2967 			return;
2968 		}
2969 		idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
2970 		idc->idc_capab_dh = (void *)dld.dld_capab_handle;
2971 		ill->ill_dld_capab = idc;
2972 	}
2973 	ip1dbg(("ill_capability_dld_ack: interface %s "
2974 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
2975 
2976 	ill_capability_dld_enable(ill);
2977 }
2978 
2979 /*
2980  * Typically capability negotiation between IP and the driver happens via
2981  * DLPI message exchange. However GLD also offers a direct function call
2982  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
2983  * But arbitrary function calls into IP or GLD are not permitted, since both
2984  * of them are protected by their own perimeter mechanism. The perimeter can
2985  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
2986  * these perimeters is IP -> MAC. Thus for example to enable the squeue
2987  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
2988  * to enter the mac perimeter and then do the direct function calls into
2989  * GLD to enable squeue polling. The ring related callbacks from the mac into
2990  * the stack to add, bind, quiesce, restart or cleanup a ring are all
2991  * protected by the mac perimeter.
2992  */
2993 static void
2994 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
2995 {
2996 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2997 	int			err;
2998 
2999 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
3000 	    DLD_ENABLE);
3001 	ASSERT(err == 0);
3002 }
3003 
3004 static void
3005 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
3006 {
3007 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3008 	int			err;
3009 
3010 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
3011 	    DLD_DISABLE);
3012 	ASSERT(err == 0);
3013 }
3014 
3015 boolean_t
3016 ill_mac_perim_held(ill_t *ill)
3017 {
3018 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3019 
3020 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
3021 	    DLD_QUERY));
3022 }
3023 
3024 static void
3025 ill_capability_direct_enable(ill_t *ill)
3026 {
3027 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3028 	ill_dld_direct_t	*idd = &idc->idc_direct;
3029 	dld_capab_direct_t	direct;
3030 	int			rc;
3031 
3032 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3033 
3034 	bzero(&direct, sizeof (direct));
3035 	direct.di_rx_cf = (uintptr_t)ip_input;
3036 	direct.di_rx_ch = ill;
3037 
3038 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
3039 	    DLD_ENABLE);
3040 	if (rc == 0) {
3041 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
3042 		idd->idd_tx_dh = direct.di_tx_dh;
3043 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
3044 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
3045 		/*
3046 		 * One time registration of flow enable callback function
3047 		 */
3048 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
3049 		    ill_flow_enable, ill);
3050 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
3051 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
3052 	} else {
3053 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
3054 		    "capability, rc = %d\n", rc);
3055 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
3056 	}
3057 }
3058 
3059 static void
3060 ill_capability_poll_enable(ill_t *ill)
3061 {
3062 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
3063 	dld_capab_poll_t	poll;
3064 	int			rc;
3065 
3066 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3067 
3068 	bzero(&poll, sizeof (poll));
3069 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
3070 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
3071 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
3072 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
3073 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
3074 	poll.poll_ring_ch = ill;
3075 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
3076 	    DLD_ENABLE);
3077 	if (rc == 0) {
3078 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
3079 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
3080 	} else {
3081 		ip1dbg(("warning: could not enable POLL "
3082 		    "capability, rc = %d\n", rc));
3083 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
3084 	}
3085 }
3086 
3087 /*
3088  * Enable the LSO capability.
3089  */
3090 static void
3091 ill_capability_lso_enable(ill_t *ill)
3092 {
3093 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
3094 	dld_capab_lso_t	lso;
3095 	int rc;
3096 
3097 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
3098 
3099 	if (ill->ill_lso_capab == NULL) {
3100 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3101 		    KM_NOSLEEP);
3102 		if (ill->ill_lso_capab == NULL) {
3103 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
3104 			    "could not enable LSO for %s (ENOMEM)\n",
3105 			    ill->ill_name);
3106 			return;
3107 		}
3108 	}
3109 
3110 	bzero(&lso, sizeof (lso));
3111 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
3112 	    DLD_ENABLE)) == 0) {
3113 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
3114 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
3115 		ill->ill_capabilities |= ILL_CAPAB_DLD_LSO;
3116 		ip1dbg(("ill_capability_lso_enable: interface %s "
3117 		    "has enabled LSO\n ", ill->ill_name));
3118 	} else {
3119 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
3120 		ill->ill_lso_capab = NULL;
3121 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
3122 	}
3123 }
3124 
3125 static void
3126 ill_capability_dld_enable(ill_t *ill)
3127 {
3128 	mac_perim_handle_t mph;
3129 
3130 	ASSERT(IAM_WRITER_ILL(ill));
3131 
3132 	if (ill->ill_isv6)
3133 		return;
3134 
3135 	ill_mac_perim_enter(ill, &mph);
3136 	if (!ill->ill_isv6) {
3137 		ill_capability_direct_enable(ill);
3138 		ill_capability_poll_enable(ill);
3139 		ill_capability_lso_enable(ill);
3140 	}
3141 	ill->ill_capabilities |= ILL_CAPAB_DLD;
3142 	ill_mac_perim_exit(ill, mph);
3143 }
3144 
3145 static void
3146 ill_capability_dld_disable(ill_t *ill)
3147 {
3148 	ill_dld_capab_t	*idc;
3149 	ill_dld_direct_t *idd;
3150 	mac_perim_handle_t	mph;
3151 
3152 	ASSERT(IAM_WRITER_ILL(ill));
3153 
3154 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
3155 		return;
3156 
3157 	ill_mac_perim_enter(ill, &mph);
3158 
3159 	idc = ill->ill_dld_capab;
3160 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
3161 		/*
3162 		 * For performance we avoid locks in the transmit data path
3163 		 * and don't maintain a count of the number of threads using
3164 		 * direct calls. Thus some threads could be using direct
3165 		 * transmit calls to GLD, even after the capability mechanism
3166 		 * turns it off. This is still safe since the handles used in
3167 		 * the direct calls continue to be valid until the unplumb is
3168 		 * completed. Remove the callback that was added (1-time) at
3169 		 * capab enable time.
3170 		 */
3171 		mutex_enter(&ill->ill_lock);
3172 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
3173 		mutex_exit(&ill->ill_lock);
3174 		if (ill->ill_flownotify_mh != NULL) {
3175 			idd = &idc->idc_direct;
3176 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
3177 			    ill->ill_flownotify_mh);
3178 			ill->ill_flownotify_mh = NULL;
3179 		}
3180 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
3181 		    NULL, DLD_DISABLE);
3182 	}
3183 
3184 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
3185 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
3186 		ip_squeue_clean_all(ill);
3187 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
3188 		    NULL, DLD_DISABLE);
3189 	}
3190 
3191 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_LSO) != 0) {
3192 		ASSERT(ill->ill_lso_capab != NULL);
3193 		/*
3194 		 * Clear the capability flag for LSO but retain the
3195 		 * ill_lso_capab structure since it's possible that another
3196 		 * thread is still referring to it.  The structure only gets
3197 		 * deallocated when we destroy the ill.
3198 		 */
3199 
3200 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_LSO;
3201 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
3202 		    NULL, DLD_DISABLE);
3203 	}
3204 
3205 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
3206 	ill_mac_perim_exit(ill, mph);
3207 }
3208 
3209 /*
3210  * Capability Negotiation protocol
3211  *
3212  * We don't wait for DLPI capability operations to finish during interface
3213  * bringup or teardown. Doing so would introduce more asynchrony and the
3214  * interface up/down operations will need multiple return and restarts.
3215  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
3216  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
3217  * exclusive operation won't start until the DLPI operations of the previous
3218  * exclusive operation complete.
3219  *
3220  * The capability state machine is shown below.
3221  *
3222  * state		next state		event, action
3223  *
3224  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
3225  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
3226  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
3227  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
3228  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
3229  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
3230  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
3231  *						    ill_capability_probe.
3232  */
3233 
3234 /*
3235  * Dedicated thread started from ip_stack_init that handles capability
3236  * disable. This thread ensures the taskq dispatch does not fail by waiting
3237  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
3238  * that direct calls to DLD are done in a cv_waitable context.
3239  */
3240 void
3241 ill_taskq_dispatch(ip_stack_t *ipst)
3242 {
3243 	callb_cpr_t cprinfo;
3244 	char 	name[64];
3245 	mblk_t	*mp;
3246 
3247 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
3248 	    ipst->ips_netstack->netstack_stackid);
3249 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
3250 	    name);
3251 	mutex_enter(&ipst->ips_capab_taskq_lock);
3252 
3253 	for (;;) {
3254 		mp = list_head(&ipst->ips_capab_taskq_list);
3255 		while (mp != NULL) {
3256 			list_remove(&ipst->ips_capab_taskq_list, mp);
3257 			mutex_exit(&ipst->ips_capab_taskq_lock);
3258 			VERIFY(taskq_dispatch(system_taskq,
3259 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
3260 			mutex_enter(&ipst->ips_capab_taskq_lock);
3261 			mp = list_head(&ipst->ips_capab_taskq_list);
3262 		}
3263 
3264 		if (ipst->ips_capab_taskq_quit)
3265 			break;
3266 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
3267 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
3268 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
3269 	}
3270 	VERIFY(list_head(&ipst->ips_capab_taskq_list) == NULL);
3271 	CALLB_CPR_EXIT(&cprinfo);
3272 	thread_exit();
3273 }
3274 
3275 /*
3276  * Consume a new-style hardware capabilities negotiation ack.
3277  * Called via taskq on receipt of DL_CAPABBILITY_ACK.
3278  */
3279 static void
3280 ill_capability_ack_thr(void *arg)
3281 {
3282 	mblk_t	*mp = arg;
3283 	dl_capability_ack_t *capp;
3284 	dl_capability_sub_t *subp, *endp;
3285 	ill_t	*ill;
3286 	boolean_t reneg;
3287 
3288 	ill = (ill_t *)mp->b_prev;
3289 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
3290 
3291 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
3292 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
3293 		/*
3294 		 * We have received the ack for our DL_CAPAB reset request.
3295 		 * There isnt' anything in the message that needs processing.
3296 		 * All message based capabilities have been disabled, now
3297 		 * do the function call based capability disable.
3298 		 */
3299 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
3300 		ill_capability_dld_disable(ill);
3301 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
3302 		if (reneg)
3303 			ill_capability_probe(ill);
3304 		goto done;
3305 	}
3306 
3307 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
3308 		ill->ill_dlpi_capab_state = IDCS_OK;
3309 
3310 	capp = (dl_capability_ack_t *)mp->b_rptr;
3311 
3312 	if (capp->dl_sub_length == 0) {
3313 		/* no new-style capabilities */
3314 		goto done;
3315 	}
3316 
3317 	/* make sure the driver supplied correct dl_sub_length */
3318 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3319 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3320 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3321 		goto done;
3322 	}
3323 
3324 
3325 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3326 	/*
3327 	 * There are sub-capabilities. Process the ones we know about.
3328 	 * Loop until we don't have room for another sub-cap header..
3329 	 */
3330 	for (subp = SC(capp, capp->dl_sub_offset),
3331 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3332 	    subp <= endp;
3333 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3334 
3335 		switch (subp->dl_cap) {
3336 		case DL_CAPAB_ID_WRAPPER:
3337 			ill_capability_id_ack(ill, mp, subp);
3338 			break;
3339 		default:
3340 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3341 			break;
3342 		}
3343 	}
3344 #undef SC
3345 done:
3346 	inet_freemsg(mp);
3347 	ill_capability_done(ill);
3348 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
3349 }
3350 
3351 /*
3352  * This needs to be started in a taskq thread to provide a cv_waitable
3353  * context.
3354  */
3355 void
3356 ill_capability_ack(ill_t *ill, mblk_t *mp)
3357 {
3358 	ip_stack_t	*ipst = ill->ill_ipst;
3359 
3360 	mp->b_prev = (mblk_t *)ill;
3361 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
3362 	    TQ_NOSLEEP) != 0)
3363 		return;
3364 
3365 	/*
3366 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
3367 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
3368 	 */
3369 	mutex_enter(&ipst->ips_capab_taskq_lock);
3370 	list_insert_tail(&ipst->ips_capab_taskq_list, mp);
3371 	cv_signal(&ipst->ips_capab_taskq_cv);
3372 	mutex_exit(&ipst->ips_capab_taskq_lock);
3373 }
3374 
3375 /*
3376  * This routine is called to scan the fragmentation reassembly table for
3377  * the specified ILL for any packets that are starting to smell.
3378  * dead_interval is the maximum time in seconds that will be tolerated.  It
3379  * will either be the value specified in ip_g_frag_timeout, or zero if the
3380  * ILL is shutting down and it is time to blow everything off.
3381  *
3382  * It returns the number of seconds (as a time_t) that the next frag timer
3383  * should be scheduled for, 0 meaning that the timer doesn't need to be
3384  * re-started.  Note that the method of calculating next_timeout isn't
3385  * entirely accurate since time will flow between the time we grab
3386  * current_time and the time we schedule the next timeout.  This isn't a
3387  * big problem since this is the timer for sending an ICMP reassembly time
3388  * exceeded messages, and it doesn't have to be exactly accurate.
3389  *
3390  * This function is
3391  * sometimes called as writer, although this is not required.
3392  */
3393 time_t
3394 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3395 {
3396 	ipfb_t	*ipfb;
3397 	ipfb_t	*endp;
3398 	ipf_t	*ipf;
3399 	ipf_t	*ipfnext;
3400 	mblk_t	*mp;
3401 	time_t	current_time = gethrestime_sec();
3402 	time_t	next_timeout = 0;
3403 	uint32_t	hdr_length;
3404 	mblk_t	*send_icmp_head;
3405 	mblk_t	*send_icmp_head_v6;
3406 	zoneid_t zoneid;
3407 	ip_stack_t *ipst = ill->ill_ipst;
3408 
3409 	ipfb = ill->ill_frag_hash_tbl;
3410 	if (ipfb == NULL)
3411 		return (B_FALSE);
3412 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3413 	/* Walk the frag hash table. */
3414 	for (; ipfb < endp; ipfb++) {
3415 		send_icmp_head = NULL;
3416 		send_icmp_head_v6 = NULL;
3417 		mutex_enter(&ipfb->ipfb_lock);
3418 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3419 			time_t frag_time = current_time - ipf->ipf_timestamp;
3420 			time_t frag_timeout;
3421 
3422 			if (frag_time < dead_interval) {
3423 				/*
3424 				 * There are some outstanding fragments
3425 				 * that will timeout later.  Make note of
3426 				 * the time so that we can reschedule the
3427 				 * next timeout appropriately.
3428 				 */
3429 				frag_timeout = dead_interval - frag_time;
3430 				if (next_timeout == 0 ||
3431 				    frag_timeout < next_timeout) {
3432 					next_timeout = frag_timeout;
3433 				}
3434 				break;
3435 			}
3436 			/* Time's up.  Get it out of here. */
3437 			hdr_length = ipf->ipf_nf_hdr_len;
3438 			ipfnext = ipf->ipf_hash_next;
3439 			if (ipfnext)
3440 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3441 			*ipf->ipf_ptphn = ipfnext;
3442 			mp = ipf->ipf_mp->b_cont;
3443 			for (; mp; mp = mp->b_cont) {
3444 				/* Extra points for neatness. */
3445 				IP_REASS_SET_START(mp, 0);
3446 				IP_REASS_SET_END(mp, 0);
3447 			}
3448 			mp = ipf->ipf_mp->b_cont;
3449 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3450 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3451 			ipfb->ipfb_count -= ipf->ipf_count;
3452 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3453 			ipfb->ipfb_frag_pkts--;
3454 			/*
3455 			 * We do not send any icmp message from here because
3456 			 * we currently are holding the ipfb_lock for this
3457 			 * hash chain. If we try and send any icmp messages
3458 			 * from here we may end up via a put back into ip
3459 			 * trying to get the same lock, causing a recursive
3460 			 * mutex panic. Instead we build a list and send all
3461 			 * the icmp messages after we have dropped the lock.
3462 			 */
3463 			if (ill->ill_isv6) {
3464 				if (hdr_length != 0) {
3465 					mp->b_next = send_icmp_head_v6;
3466 					send_icmp_head_v6 = mp;
3467 				} else {
3468 					freemsg(mp);
3469 				}
3470 			} else {
3471 				if (hdr_length != 0) {
3472 					mp->b_next = send_icmp_head;
3473 					send_icmp_head = mp;
3474 				} else {
3475 					freemsg(mp);
3476 				}
3477 			}
3478 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3479 			freeb(ipf->ipf_mp);
3480 		}
3481 		mutex_exit(&ipfb->ipfb_lock);
3482 		/*
3483 		 * Now need to send any icmp messages that we delayed from
3484 		 * above.
3485 		 */
3486 		while (send_icmp_head_v6 != NULL) {
3487 			ip6_t *ip6h;
3488 
3489 			mp = send_icmp_head_v6;
3490 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3491 			mp->b_next = NULL;
3492 			if (mp->b_datap->db_type == M_CTL)
3493 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3494 			else
3495 				ip6h = (ip6_t *)mp->b_rptr;
3496 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3497 			    ill, ipst);
3498 			if (zoneid == ALL_ZONES) {
3499 				freemsg(mp);
3500 			} else {
3501 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3502 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3503 				    B_FALSE, zoneid, ipst);
3504 			}
3505 		}
3506 		while (send_icmp_head != NULL) {
3507 			ipaddr_t dst;
3508 
3509 			mp = send_icmp_head;
3510 			send_icmp_head = send_icmp_head->b_next;
3511 			mp->b_next = NULL;
3512 
3513 			if (mp->b_datap->db_type == M_CTL)
3514 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3515 			else
3516 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3517 
3518 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3519 			if (zoneid == ALL_ZONES) {
3520 				freemsg(mp);
3521 			} else {
3522 				icmp_time_exceeded(ill->ill_wq, mp,
3523 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3524 				    ipst);
3525 			}
3526 		}
3527 	}
3528 	/*
3529 	 * A non-dying ILL will use the return value to decide whether to
3530 	 * restart the frag timer, and for how long.
3531 	 */
3532 	return (next_timeout);
3533 }
3534 
3535 /*
3536  * This routine is called when the approximate count of mblk memory used
3537  * for the specified ILL has exceeded max_count.
3538  */
3539 void
3540 ill_frag_prune(ill_t *ill, uint_t max_count)
3541 {
3542 	ipfb_t	*ipfb;
3543 	ipf_t	*ipf;
3544 	size_t	count;
3545 
3546 	/*
3547 	 * If we are here within ip_min_frag_prune_time msecs remove
3548 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3549 	 * ill_frag_free_num_pkts.
3550 	 */
3551 	mutex_enter(&ill->ill_lock);
3552 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3553 	    (ip_min_frag_prune_time != 0 ?
3554 	    ip_min_frag_prune_time : msec_per_tick)) {
3555 
3556 		ill->ill_frag_free_num_pkts++;
3557 
3558 	} else {
3559 		ill->ill_frag_free_num_pkts = 0;
3560 	}
3561 	ill->ill_last_frag_clean_time = lbolt;
3562 	mutex_exit(&ill->ill_lock);
3563 
3564 	/*
3565 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3566 	 */
3567 	if (ill->ill_frag_free_num_pkts != 0) {
3568 		int ix;
3569 
3570 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3571 			ipfb = &ill->ill_frag_hash_tbl[ix];
3572 			mutex_enter(&ipfb->ipfb_lock);
3573 			if (ipfb->ipfb_ipf != NULL) {
3574 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3575 				    ill->ill_frag_free_num_pkts);
3576 			}
3577 			mutex_exit(&ipfb->ipfb_lock);
3578 		}
3579 	}
3580 	/*
3581 	 * While the reassembly list for this ILL is too big, prune a fragment
3582 	 * queue by age, oldest first.
3583 	 */
3584 	while (ill->ill_frag_count > max_count) {
3585 		int	ix;
3586 		ipfb_t	*oipfb = NULL;
3587 		uint_t	oldest = UINT_MAX;
3588 
3589 		count = 0;
3590 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3591 			ipfb = &ill->ill_frag_hash_tbl[ix];
3592 			mutex_enter(&ipfb->ipfb_lock);
3593 			ipf = ipfb->ipfb_ipf;
3594 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3595 				oldest = ipf->ipf_gen;
3596 				oipfb = ipfb;
3597 			}
3598 			count += ipfb->ipfb_count;
3599 			mutex_exit(&ipfb->ipfb_lock);
3600 		}
3601 		if (oipfb == NULL)
3602 			break;
3603 
3604 		if (count <= max_count)
3605 			return;	/* Somebody beat us to it, nothing to do */
3606 		mutex_enter(&oipfb->ipfb_lock);
3607 		ipf = oipfb->ipfb_ipf;
3608 		if (ipf != NULL) {
3609 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3610 		}
3611 		mutex_exit(&oipfb->ipfb_lock);
3612 	}
3613 }
3614 
3615 /*
3616  * free 'free_cnt' fragmented packets starting at ipf.
3617  */
3618 void
3619 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3620 {
3621 	size_t	count;
3622 	mblk_t	*mp;
3623 	mblk_t	*tmp;
3624 	ipf_t **ipfp = ipf->ipf_ptphn;
3625 
3626 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3627 	ASSERT(ipfp != NULL);
3628 	ASSERT(ipf != NULL);
3629 
3630 	while (ipf != NULL && free_cnt-- > 0) {
3631 		count = ipf->ipf_count;
3632 		mp = ipf->ipf_mp;
3633 		ipf = ipf->ipf_hash_next;
3634 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3635 			IP_REASS_SET_START(tmp, 0);
3636 			IP_REASS_SET_END(tmp, 0);
3637 		}
3638 		atomic_add_32(&ill->ill_frag_count, -count);
3639 		ASSERT(ipfb->ipfb_count >= count);
3640 		ipfb->ipfb_count -= count;
3641 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3642 		ipfb->ipfb_frag_pkts--;
3643 		freemsg(mp);
3644 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3645 	}
3646 
3647 	if (ipf)
3648 		ipf->ipf_ptphn = ipfp;
3649 	ipfp[0] = ipf;
3650 }
3651 
3652 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3653 	"obsolete and may be removed in a future release of Solaris.  Use " \
3654 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3655 
3656 /*
3657  * For obsolete per-interface forwarding configuration;
3658  * called in response to ND_GET.
3659  */
3660 /* ARGSUSED */
3661 static int
3662 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3663 {
3664 	ill_t *ill = (ill_t *)cp;
3665 
3666 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3667 
3668 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3669 	return (0);
3670 }
3671 
3672 /*
3673  * For obsolete per-interface forwarding configuration;
3674  * called in response to ND_SET.
3675  */
3676 /* ARGSUSED */
3677 static int
3678 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3679     cred_t *ioc_cr)
3680 {
3681 	long value;
3682 	int retval;
3683 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3684 
3685 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3686 
3687 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3688 	    value < 0 || value > 1) {
3689 		return (EINVAL);
3690 	}
3691 
3692 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3693 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3694 	rw_exit(&ipst->ips_ill_g_lock);
3695 	return (retval);
3696 }
3697 
3698 /*
3699  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3700  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3701  * up RTS_IFINFO routing socket messages for each interface whose flags we
3702  * change.
3703  */
3704 int
3705 ill_forward_set(ill_t *ill, boolean_t enable)
3706 {
3707 	ill_group_t *illgrp;
3708 	ip_stack_t	*ipst = ill->ill_ipst;
3709 
3710 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3711 
3712 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3713 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3714 		return (0);
3715 
3716 	if (IS_LOOPBACK(ill))
3717 		return (EINVAL);
3718 
3719 	/*
3720 	 * If the ill is in an IPMP group, set the forwarding policy on all
3721 	 * members of the group to the same value.
3722 	 */
3723 	illgrp = ill->ill_group;
3724 	if (illgrp != NULL) {
3725 		ill_t *tmp_ill;
3726 
3727 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3728 		    tmp_ill = tmp_ill->ill_group_next) {
3729 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3730 			    (enable ? "Enabling" : "Disabling"),
3731 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3732 			    tmp_ill->ill_name));
3733 			mutex_enter(&tmp_ill->ill_lock);
3734 			if (enable)
3735 				tmp_ill->ill_flags |= ILLF_ROUTER;
3736 			else
3737 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3738 			mutex_exit(&tmp_ill->ill_lock);
3739 			if (tmp_ill->ill_isv6)
3740 				ill_set_nce_router_flags(tmp_ill, enable);
3741 			/* Notify routing socket listeners of this change. */
3742 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3743 		}
3744 	} else {
3745 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3746 		    (enable ? "Enabling" : "Disabling"),
3747 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3748 		mutex_enter(&ill->ill_lock);
3749 		if (enable)
3750 			ill->ill_flags |= ILLF_ROUTER;
3751 		else
3752 			ill->ill_flags &= ~ILLF_ROUTER;
3753 		mutex_exit(&ill->ill_lock);
3754 		if (ill->ill_isv6)
3755 			ill_set_nce_router_flags(ill, enable);
3756 		/* Notify routing socket listeners of this change. */
3757 		ip_rts_ifmsg(ill->ill_ipif);
3758 	}
3759 
3760 	return (0);
3761 }
3762 
3763 /*
3764  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3765  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3766  * set or clear.
3767  */
3768 static void
3769 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3770 {
3771 	ipif_t *ipif;
3772 	nce_t *nce;
3773 
3774 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3775 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3776 		if (nce != NULL) {
3777 			mutex_enter(&nce->nce_lock);
3778 			if (enable)
3779 				nce->nce_flags |= NCE_F_ISROUTER;
3780 			else
3781 				nce->nce_flags &= ~NCE_F_ISROUTER;
3782 			mutex_exit(&nce->nce_lock);
3783 			NCE_REFRELE(nce);
3784 		}
3785 	}
3786 }
3787 
3788 /*
3789  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3790  * for this ill.  Make sure the v6/v4 question has been answered about this
3791  * ill.  The creation of this ndd variable is only for backwards compatibility.
3792  * The preferred way to control per-interface IP forwarding is through the
3793  * ILLF_ROUTER interface flag.
3794  */
3795 static int
3796 ill_set_ndd_name(ill_t *ill)
3797 {
3798 	char *suffix;
3799 	ip_stack_t	*ipst = ill->ill_ipst;
3800 
3801 	ASSERT(IAM_WRITER_ILL(ill));
3802 
3803 	if (ill->ill_isv6)
3804 		suffix = ipv6_forward_suffix;
3805 	else
3806 		suffix = ipv4_forward_suffix;
3807 
3808 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3809 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3810 	/*
3811 	 * Copies over the '\0'.
3812 	 * Note that strlen(suffix) is always bounded.
3813 	 */
3814 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3815 	    strlen(suffix) + 1);
3816 
3817 	/*
3818 	 * Use of the nd table requires holding the reader lock.
3819 	 * Modifying the nd table thru nd_load/nd_unload requires
3820 	 * the writer lock.
3821 	 */
3822 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
3823 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3824 	    nd_ill_forward_set, (caddr_t)ill)) {
3825 		/*
3826 		 * If the nd_load failed, it only meant that it could not
3827 		 * allocate a new bunch of room for further NDD expansion.
3828 		 * Because of that, the ill_ndd_name will be set to 0, and
3829 		 * this interface is at the mercy of the global ip_forwarding
3830 		 * variable.
3831 		 */
3832 		rw_exit(&ipst->ips_ip_g_nd_lock);
3833 		ill->ill_ndd_name = NULL;
3834 		return (ENOMEM);
3835 	}
3836 	rw_exit(&ipst->ips_ip_g_nd_lock);
3837 	return (0);
3838 }
3839 
3840 /*
3841  * Intializes the context structure and returns the first ill in the list
3842  * cuurently start_list and end_list can have values:
3843  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3844  * IP_V4_G_HEAD		Traverse IPV4 list only.
3845  * IP_V6_G_HEAD		Traverse IPV6 list only.
3846  */
3847 
3848 /*
3849  * We don't check for CONDEMNED ills here. Caller must do that if
3850  * necessary under the ill lock.
3851  */
3852 ill_t *
3853 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
3854     ip_stack_t *ipst)
3855 {
3856 	ill_if_t *ifp;
3857 	ill_t *ill;
3858 	avl_tree_t *avl_tree;
3859 
3860 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3861 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3862 
3863 	/*
3864 	 * setup the lists to search
3865 	 */
3866 	if (end_list != MAX_G_HEADS) {
3867 		ctx->ctx_current_list = start_list;
3868 		ctx->ctx_last_list = end_list;
3869 	} else {
3870 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3871 		ctx->ctx_current_list = 0;
3872 	}
3873 
3874 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3875 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3876 		if (ifp != (ill_if_t *)
3877 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3878 			avl_tree = &ifp->illif_avl_by_ppa;
3879 			ill = avl_first(avl_tree);
3880 			/*
3881 			 * ill is guaranteed to be non NULL or ifp should have
3882 			 * not existed.
3883 			 */
3884 			ASSERT(ill != NULL);
3885 			return (ill);
3886 		}
3887 		ctx->ctx_current_list++;
3888 	}
3889 
3890 	return (NULL);
3891 }
3892 
3893 /*
3894  * returns the next ill in the list. ill_first() must have been called
3895  * before calling ill_next() or bad things will happen.
3896  */
3897 
3898 /*
3899  * We don't check for CONDEMNED ills here. Caller must do that if
3900  * necessary under the ill lock.
3901  */
3902 ill_t *
3903 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3904 {
3905 	ill_if_t *ifp;
3906 	ill_t *ill;
3907 	ip_stack_t	*ipst = lastill->ill_ipst;
3908 
3909 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3910 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
3911 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3912 	    AVL_AFTER)) != NULL) {
3913 		return (ill);
3914 	}
3915 
3916 	/* goto next ill_ifp in the list. */
3917 	ifp = lastill->ill_ifptr->illif_next;
3918 
3919 	/* make sure not at end of circular list */
3920 	while (ifp ==
3921 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
3922 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3923 			return (NULL);
3924 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
3925 	}
3926 
3927 	return (avl_first(&ifp->illif_avl_by_ppa));
3928 }
3929 
3930 /*
3931  * Check interface name for correct format which is name+ppa.
3932  * name can contain characters and digits, the right most digits
3933  * make up the ppa number. use of octal is not allowed, name must contain
3934  * a ppa, return pointer to the start of ppa.
3935  * In case of error return NULL.
3936  */
3937 static char *
3938 ill_get_ppa_ptr(char *name)
3939 {
3940 	int namelen = mi_strlen(name);
3941 
3942 	int len = namelen;
3943 
3944 	name += len;
3945 	while (len > 0) {
3946 		name--;
3947 		if (*name < '0' || *name > '9')
3948 			break;
3949 		len--;
3950 	}
3951 
3952 	/* empty string, all digits, or no trailing digits */
3953 	if (len == 0 || len == (int)namelen)
3954 		return (NULL);
3955 
3956 	name++;
3957 	/* check for attempted use of octal */
3958 	if (*name == '0' && len != (int)namelen - 1)
3959 		return (NULL);
3960 	return (name);
3961 }
3962 
3963 /*
3964  * use avl tree to locate the ill.
3965  */
3966 static ill_t *
3967 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
3968     ipsq_func_t func, int *error, ip_stack_t *ipst)
3969 {
3970 	char *ppa_ptr = NULL;
3971 	int len;
3972 	uint_t ppa;
3973 	ill_t *ill = NULL;
3974 	ill_if_t *ifp;
3975 	int list;
3976 	ipsq_t *ipsq;
3977 
3978 	if (error != NULL)
3979 		*error = 0;
3980 
3981 	/*
3982 	 * get ppa ptr
3983 	 */
3984 	if (isv6)
3985 		list = IP_V6_G_HEAD;
3986 	else
3987 		list = IP_V4_G_HEAD;
3988 
3989 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
3990 		if (error != NULL)
3991 			*error = ENXIO;
3992 		return (NULL);
3993 	}
3994 
3995 	len = ppa_ptr - name + 1;
3996 
3997 	ppa = stoi(&ppa_ptr);
3998 
3999 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4000 
4001 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4002 		/*
4003 		 * match is done on len - 1 as the name is not null
4004 		 * terminated it contains ppa in addition to the interface
4005 		 * name.
4006 		 */
4007 		if ((ifp->illif_name_len == len) &&
4008 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4009 			break;
4010 		} else {
4011 			ifp = ifp->illif_next;
4012 		}
4013 	}
4014 
4015 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4016 		/*
4017 		 * Even the interface type does not exist.
4018 		 */
4019 		if (error != NULL)
4020 			*error = ENXIO;
4021 		return (NULL);
4022 	}
4023 
4024 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4025 	if (ill != NULL) {
4026 		/*
4027 		 * The block comment at the start of ipif_down
4028 		 * explains the use of the macros used below
4029 		 */
4030 		GRAB_CONN_LOCK(q);
4031 		mutex_enter(&ill->ill_lock);
4032 		if (ILL_CAN_LOOKUP(ill)) {
4033 			ill_refhold_locked(ill);
4034 			mutex_exit(&ill->ill_lock);
4035 			RELEASE_CONN_LOCK(q);
4036 			return (ill);
4037 		} else if (ILL_CAN_WAIT(ill, q)) {
4038 			ipsq = ill->ill_phyint->phyint_ipsq;
4039 			mutex_enter(&ipsq->ipsq_lock);
4040 			mutex_exit(&ill->ill_lock);
4041 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4042 			mutex_exit(&ipsq->ipsq_lock);
4043 			RELEASE_CONN_LOCK(q);
4044 			if (error != NULL)
4045 				*error = EINPROGRESS;
4046 			return (NULL);
4047 		}
4048 		mutex_exit(&ill->ill_lock);
4049 		RELEASE_CONN_LOCK(q);
4050 	}
4051 	if (error != NULL)
4052 		*error = ENXIO;
4053 	return (NULL);
4054 }
4055 
4056 /*
4057  * comparison function for use with avl.
4058  */
4059 static int
4060 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4061 {
4062 	uint_t ppa;
4063 	uint_t ill_ppa;
4064 
4065 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4066 
4067 	ppa = *((uint_t *)ppa_ptr);
4068 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4069 	/*
4070 	 * We want the ill with the lowest ppa to be on the
4071 	 * top.
4072 	 */
4073 	if (ill_ppa < ppa)
4074 		return (1);
4075 	if (ill_ppa > ppa)
4076 		return (-1);
4077 	return (0);
4078 }
4079 
4080 /*
4081  * remove an interface type from the global list.
4082  */
4083 static void
4084 ill_delete_interface_type(ill_if_t *interface)
4085 {
4086 	ASSERT(interface != NULL);
4087 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4088 
4089 	avl_destroy(&interface->illif_avl_by_ppa);
4090 	if (interface->illif_ppa_arena != NULL)
4091 		vmem_destroy(interface->illif_ppa_arena);
4092 
4093 	remque(interface);
4094 
4095 	mi_free(interface);
4096 }
4097 
4098 /*
4099  * remove ill from the global list.
4100  */
4101 static void
4102 ill_glist_delete(ill_t *ill)
4103 {
4104 	ip_stack_t	*ipst;
4105 
4106 	if (ill == NULL)
4107 		return;
4108 	ipst = ill->ill_ipst;
4109 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4110 
4111 	/*
4112 	 * If the ill was never inserted into the AVL tree
4113 	 * we skip the if branch.
4114 	 */
4115 	if (ill->ill_ifptr != NULL) {
4116 		/*
4117 		 * remove from AVL tree and free ppa number
4118 		 */
4119 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4120 
4121 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4122 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4123 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4124 		}
4125 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4126 			ill_delete_interface_type(ill->ill_ifptr);
4127 		}
4128 
4129 		/*
4130 		 * Indicate ill is no longer in the list.
4131 		 */
4132 		ill->ill_ifptr = NULL;
4133 		ill->ill_name_length = 0;
4134 		ill->ill_name[0] = '\0';
4135 		ill->ill_ppa = UINT_MAX;
4136 	}
4137 
4138 	/* Generate one last event for this ill. */
4139 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
4140 	    ill->ill_name_length);
4141 
4142 	ill_phyint_free(ill);
4143 	rw_exit(&ipst->ips_ill_g_lock);
4144 }
4145 
4146 /*
4147  * allocate a ppa, if the number of plumbed interfaces of this type are
4148  * less than ill_no_arena do a linear search to find a unused ppa.
4149  * When the number goes beyond ill_no_arena switch to using an arena.
4150  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4151  * is the return value for an error condition, so allocation starts at one
4152  * and is decremented by one.
4153  */
4154 static int
4155 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4156 {
4157 	ill_t *tmp_ill;
4158 	uint_t start, end;
4159 	int ppa;
4160 
4161 	if (ifp->illif_ppa_arena == NULL &&
4162 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4163 		/*
4164 		 * Create an arena.
4165 		 */
4166 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4167 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4168 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4169 			/* allocate what has already been assigned */
4170 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4171 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4172 		    tmp_ill, AVL_AFTER)) {
4173 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4174 			    1,		/* size */
4175 			    1,		/* align/quantum */
4176 			    0,		/* phase */
4177 			    0,		/* nocross */
4178 			    /* minaddr */
4179 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4180 			    /* maxaddr */
4181 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4182 			    VM_NOSLEEP|VM_FIRSTFIT);
4183 			if (ppa == 0) {
4184 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4185 				    " failed while switching"));
4186 				vmem_destroy(ifp->illif_ppa_arena);
4187 				ifp->illif_ppa_arena = NULL;
4188 				break;
4189 			}
4190 		}
4191 	}
4192 
4193 	if (ifp->illif_ppa_arena != NULL) {
4194 		if (ill->ill_ppa == UINT_MAX) {
4195 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4196 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4197 			if (ppa == 0)
4198 				return (EAGAIN);
4199 			ill->ill_ppa = --ppa;
4200 		} else {
4201 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4202 			    1, 		/* size */
4203 			    1, 		/* align/quantum */
4204 			    0, 		/* phase */
4205 			    0, 		/* nocross */
4206 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4207 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4208 			    VM_NOSLEEP|VM_FIRSTFIT);
4209 			/*
4210 			 * Most likely the allocation failed because
4211 			 * the requested ppa was in use.
4212 			 */
4213 			if (ppa == 0)
4214 				return (EEXIST);
4215 		}
4216 		return (0);
4217 	}
4218 
4219 	/*
4220 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4221 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4222 	 */
4223 	if (ill->ill_ppa == UINT_MAX) {
4224 		end = UINT_MAX - 1;
4225 		start = 0;
4226 	} else {
4227 		end = start = ill->ill_ppa;
4228 	}
4229 
4230 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4231 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4232 		if (start++ >= end) {
4233 			if (ill->ill_ppa == UINT_MAX)
4234 				return (EAGAIN);
4235 			else
4236 				return (EEXIST);
4237 		}
4238 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4239 	}
4240 	ill->ill_ppa = start;
4241 	return (0);
4242 }
4243 
4244 /*
4245  * Insert ill into the list of configured ill's. Once this function completes,
4246  * the ill is globally visible and is available through lookups. More precisely
4247  * this happens after the caller drops the ill_g_lock.
4248  */
4249 static int
4250 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4251 {
4252 	ill_if_t *ill_interface;
4253 	avl_index_t where = 0;
4254 	int error;
4255 	int name_length;
4256 	int index;
4257 	boolean_t check_length = B_FALSE;
4258 	ip_stack_t	*ipst = ill->ill_ipst;
4259 
4260 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4261 
4262 	name_length = mi_strlen(name) + 1;
4263 
4264 	if (isv6)
4265 		index = IP_V6_G_HEAD;
4266 	else
4267 		index = IP_V4_G_HEAD;
4268 
4269 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4270 	/*
4271 	 * Search for interface type based on name
4272 	 */
4273 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4274 		if ((ill_interface->illif_name_len == name_length) &&
4275 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4276 			break;
4277 		}
4278 		ill_interface = ill_interface->illif_next;
4279 	}
4280 
4281 	/*
4282 	 * Interface type not found, create one.
4283 	 */
4284 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4285 
4286 		ill_g_head_t ghead;
4287 
4288 		/*
4289 		 * allocate ill_if_t structure
4290 		 */
4291 
4292 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4293 		if (ill_interface == NULL) {
4294 			return (ENOMEM);
4295 		}
4296 
4297 
4298 
4299 		(void) strcpy(ill_interface->illif_name, name);
4300 		ill_interface->illif_name_len = name_length;
4301 
4302 		avl_create(&ill_interface->illif_avl_by_ppa,
4303 		    ill_compare_ppa, sizeof (ill_t),
4304 		    offsetof(struct ill_s, ill_avl_byppa));
4305 
4306 		/*
4307 		 * link the structure in the back to maintain order
4308 		 * of configuration for ifconfig output.
4309 		 */
4310 		ghead = ipst->ips_ill_g_heads[index];
4311 		insque(ill_interface, ghead.ill_g_list_tail);
4312 
4313 	}
4314 
4315 	if (ill->ill_ppa == UINT_MAX)
4316 		check_length = B_TRUE;
4317 
4318 	error = ill_alloc_ppa(ill_interface, ill);
4319 	if (error != 0) {
4320 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4321 			ill_delete_interface_type(ill->ill_ifptr);
4322 		return (error);
4323 	}
4324 
4325 	/*
4326 	 * When the ppa is choosen by the system, check that there is
4327 	 * enough space to insert ppa. if a specific ppa was passed in this
4328 	 * check is not required as the interface name passed in will have
4329 	 * the right ppa in it.
4330 	 */
4331 	if (check_length) {
4332 		/*
4333 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4334 		 */
4335 		char buf[sizeof (uint_t) * 3];
4336 
4337 		/*
4338 		 * convert ppa to string to calculate the amount of space
4339 		 * required for it in the name.
4340 		 */
4341 		numtos(ill->ill_ppa, buf);
4342 
4343 		/* Do we have enough space to insert ppa ? */
4344 
4345 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4346 			/* Free ppa and interface type struct */
4347 			if (ill_interface->illif_ppa_arena != NULL) {
4348 				vmem_free(ill_interface->illif_ppa_arena,
4349 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4350 			}
4351 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4352 				ill_delete_interface_type(ill->ill_ifptr);
4353 
4354 			return (EINVAL);
4355 		}
4356 	}
4357 
4358 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4359 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4360 
4361 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4362 	    &where);
4363 	ill->ill_ifptr = ill_interface;
4364 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4365 
4366 	ill_phyint_reinit(ill);
4367 	return (0);
4368 }
4369 
4370 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4371 static boolean_t
4372 ipsq_init(ill_t *ill)
4373 {
4374 	ipsq_t  *ipsq;
4375 
4376 	/* Init the ipsq and impicitly enter as writer */
4377 	ill->ill_phyint->phyint_ipsq =
4378 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4379 	if (ill->ill_phyint->phyint_ipsq == NULL)
4380 		return (B_FALSE);
4381 	ipsq = ill->ill_phyint->phyint_ipsq;
4382 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4383 	ill->ill_phyint->phyint_ipsq_next = NULL;
4384 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4385 	ipsq->ipsq_refs = 1;
4386 	ipsq->ipsq_writer = curthread;
4387 	ipsq->ipsq_reentry_cnt = 1;
4388 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4389 #ifdef DEBUG
4390 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4391 	    IPSQ_STACK_DEPTH);
4392 #endif
4393 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4394 	return (B_TRUE);
4395 }
4396 
4397 /*
4398  * ill_init is called by ip_open when a device control stream is opened.
4399  * It does a few initializations, and shoots a DL_INFO_REQ message down
4400  * to the driver.  The response is later picked up in ip_rput_dlpi and
4401  * used to set up default mechanisms for talking to the driver.  (Always
4402  * called as writer.)
4403  *
4404  * If this function returns error, ip_open will call ip_close which in
4405  * turn will call ill_delete to clean up any memory allocated here that
4406  * is not yet freed.
4407  */
4408 int
4409 ill_init(queue_t *q, ill_t *ill)
4410 {
4411 	int	count;
4412 	dl_info_req_t	*dlir;
4413 	mblk_t	*info_mp;
4414 	uchar_t *frag_ptr;
4415 
4416 	/*
4417 	 * The ill is initialized to zero by mi_alloc*(). In addition
4418 	 * some fields already contain valid values, initialized in
4419 	 * ip_open(), before we reach here.
4420 	 */
4421 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4422 
4423 	ill->ill_rq = q;
4424 	ill->ill_wq = WR(q);
4425 
4426 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4427 	    BPRI_HI);
4428 	if (info_mp == NULL)
4429 		return (ENOMEM);
4430 
4431 	/*
4432 	 * Allocate sufficient space to contain our fragment hash table and
4433 	 * the device name.
4434 	 */
4435 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4436 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4437 	if (frag_ptr == NULL) {
4438 		freemsg(info_mp);
4439 		return (ENOMEM);
4440 	}
4441 	ill->ill_frag_ptr = frag_ptr;
4442 	ill->ill_frag_free_num_pkts = 0;
4443 	ill->ill_last_frag_clean_time = 0;
4444 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4445 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4446 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4447 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4448 		    NULL, MUTEX_DEFAULT, NULL);
4449 	}
4450 
4451 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4452 	if (ill->ill_phyint == NULL) {
4453 		freemsg(info_mp);
4454 		mi_free(frag_ptr);
4455 		return (ENOMEM);
4456 	}
4457 
4458 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4459 	/*
4460 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4461 	 * at this point because of the following reason. If we can't
4462 	 * enter the ipsq at some point and cv_wait, the writer that
4463 	 * wakes us up tries to locate us using the list of all phyints
4464 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4465 	 * If we don't set it now, we risk a missed wakeup.
4466 	 */
4467 	ill->ill_phyint->phyint_illv4 = ill;
4468 	ill->ill_ppa = UINT_MAX;
4469 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4470 
4471 	if (!ipsq_init(ill)) {
4472 		freemsg(info_mp);
4473 		mi_free(frag_ptr);
4474 		mi_free(ill->ill_phyint);
4475 		return (ENOMEM);
4476 	}
4477 
4478 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4479 
4480 	/* Frag queue limit stuff */
4481 	ill->ill_frag_count = 0;
4482 	ill->ill_ipf_gen = 0;
4483 
4484 	ill->ill_global_timer = INFINITY;
4485 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4486 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4487 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4488 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4489 
4490 	/*
4491 	 * Initialize IPv6 configuration variables.  The IP module is always
4492 	 * opened as an IPv4 module.  Instead tracking down the cases where
4493 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4494 	 * here for convenience, this has no effect until the ill is set to do
4495 	 * IPv6.
4496 	 */
4497 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4498 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4499 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4500 	ill->ill_max_buf = ND_MAX_Q;
4501 	ill->ill_refcnt = 0;
4502 
4503 	/* Send down the Info Request to the driver. */
4504 	info_mp->b_datap->db_type = M_PCPROTO;
4505 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4506 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4507 	dlir->dl_primitive = DL_INFO_REQ;
4508 
4509 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4510 
4511 	qprocson(q);
4512 	ill_dlpi_send(ill, info_mp);
4513 
4514 	return (0);
4515 }
4516 
4517 /*
4518  * ill_dls_info
4519  * creates datalink socket info from the device.
4520  */
4521 int
4522 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4523 {
4524 	size_t	len;
4525 	ill_t	*ill = ipif->ipif_ill;
4526 
4527 	sdl->sdl_family = AF_LINK;
4528 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4529 	sdl->sdl_type = ill->ill_type;
4530 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4531 	len = strlen(sdl->sdl_data);
4532 	ASSERT(len < 256);
4533 	sdl->sdl_nlen = (uchar_t)len;
4534 	sdl->sdl_alen = ill->ill_phys_addr_length;
4535 	sdl->sdl_slen = 0;
4536 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4537 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4538 
4539 	return (sizeof (struct sockaddr_dl));
4540 }
4541 
4542 /*
4543  * ill_xarp_info
4544  * creates xarp info from the device.
4545  */
4546 static int
4547 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4548 {
4549 	sdl->sdl_family = AF_LINK;
4550 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4551 	sdl->sdl_type = ill->ill_type;
4552 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4553 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4554 	sdl->sdl_alen = ill->ill_phys_addr_length;
4555 	sdl->sdl_slen = 0;
4556 	return (sdl->sdl_nlen);
4557 }
4558 
4559 static int
4560 loopback_kstat_update(kstat_t *ksp, int rw)
4561 {
4562 	kstat_named_t *kn;
4563 	netstackid_t	stackid;
4564 	netstack_t	*ns;
4565 	ip_stack_t	*ipst;
4566 
4567 	if (ksp == NULL || ksp->ks_data == NULL)
4568 		return (EIO);
4569 
4570 	if (rw == KSTAT_WRITE)
4571 		return (EACCES);
4572 
4573 	kn = KSTAT_NAMED_PTR(ksp);
4574 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4575 
4576 	ns = netstack_find_by_stackid(stackid);
4577 	if (ns == NULL)
4578 		return (-1);
4579 
4580 	ipst = ns->netstack_ip;
4581 	if (ipst == NULL) {
4582 		netstack_rele(ns);
4583 		return (-1);
4584 	}
4585 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4586 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4587 	netstack_rele(ns);
4588 	return (0);
4589 }
4590 
4591 /*
4592  * Has ifindex been plumbed already.
4593  * Compares both phyint_ifindex and phyint_group_ifindex.
4594  */
4595 static boolean_t
4596 phyint_exists(uint_t index, ip_stack_t *ipst)
4597 {
4598 	phyint_t *phyi;
4599 
4600 	ASSERT(index != 0);
4601 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4602 	/*
4603 	 * Indexes are stored in the phyint - a common structure
4604 	 * to both IPv4 and IPv6.
4605 	 */
4606 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4607 	for (; phyi != NULL;
4608 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4609 	    phyi, AVL_AFTER)) {
4610 		if (phyi->phyint_ifindex == index ||
4611 		    phyi->phyint_group_ifindex == index)
4612 			return (B_TRUE);
4613 	}
4614 	return (B_FALSE);
4615 }
4616 
4617 /* Pick a unique ifindex */
4618 boolean_t
4619 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4620 {
4621 	uint_t starting_index;
4622 
4623 	if (!ipst->ips_ill_index_wrap) {
4624 		*indexp = ipst->ips_ill_index++;
4625 		if (ipst->ips_ill_index == 0) {
4626 			/* Reached the uint_t limit Next time wrap  */
4627 			ipst->ips_ill_index_wrap = B_TRUE;
4628 		}
4629 		return (B_TRUE);
4630 	}
4631 
4632 	/*
4633 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4634 	 * at this point and don't want to call any function that attempts
4635 	 * to get the lock again.
4636 	 */
4637 	starting_index = ipst->ips_ill_index++;
4638 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4639 		if (ipst->ips_ill_index != 0 &&
4640 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4641 			/* found unused index - use it */
4642 			*indexp = ipst->ips_ill_index;
4643 			return (B_TRUE);
4644 		}
4645 	}
4646 
4647 	/*
4648 	 * all interface indicies are inuse.
4649 	 */
4650 	return (B_FALSE);
4651 }
4652 
4653 /*
4654  * Assign a unique interface index for the phyint.
4655  */
4656 static boolean_t
4657 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4658 {
4659 	ASSERT(phyi->phyint_ifindex == 0);
4660 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4661 }
4662 
4663 /*
4664  * Return a pointer to the ill which matches the supplied name.  Note that
4665  * the ill name length includes the null termination character.  (May be
4666  * called as writer.)
4667  * If do_alloc and the interface is "lo0" it will be automatically created.
4668  * Cannot bump up reference on condemned ills. So dup detect can't be done
4669  * using this func.
4670  */
4671 ill_t *
4672 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4673     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4674     ip_stack_t *ipst)
4675 {
4676 	ill_t	*ill;
4677 	ipif_t	*ipif;
4678 	kstat_named_t	*kn;
4679 	boolean_t isloopback;
4680 	ipsq_t *old_ipsq;
4681 	in6_addr_t ov6addr;
4682 
4683 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4684 
4685 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4686 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4687 	rw_exit(&ipst->ips_ill_g_lock);
4688 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4689 		return (ill);
4690 
4691 	/*
4692 	 * Couldn't find it.  Does this happen to be a lookup for the
4693 	 * loopback device and are we allowed to allocate it?
4694 	 */
4695 	if (!isloopback || !do_alloc)
4696 		return (NULL);
4697 
4698 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4699 
4700 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4701 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4702 		rw_exit(&ipst->ips_ill_g_lock);
4703 		return (ill);
4704 	}
4705 
4706 	/* Create the loopback device on demand */
4707 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4708 	    sizeof (ipif_loopback_name), BPRI_MED));
4709 	if (ill == NULL)
4710 		goto done;
4711 
4712 	*ill = ill_null;
4713 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4714 	ill->ill_ipst = ipst;
4715 	netstack_hold(ipst->ips_netstack);
4716 	/*
4717 	 * For exclusive stacks we set the zoneid to zero
4718 	 * to make IP operate as if in the global zone.
4719 	 */
4720 	ill->ill_zoneid = GLOBAL_ZONEID;
4721 
4722 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4723 	if (ill->ill_phyint == NULL)
4724 		goto done;
4725 
4726 	if (isv6)
4727 		ill->ill_phyint->phyint_illv6 = ill;
4728 	else
4729 		ill->ill_phyint->phyint_illv4 = ill;
4730 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4731 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4732 	/* Add room for tcp+ip headers */
4733 	if (isv6) {
4734 		ill->ill_isv6 = B_TRUE;
4735 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4736 	} else {
4737 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4738 	}
4739 	if (!ill_allocate_mibs(ill))
4740 		goto done;
4741 	ill->ill_max_mtu = ill->ill_max_frag;
4742 	/*
4743 	 * ipif_loopback_name can't be pointed at directly because its used
4744 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4745 	 * from the glist, ill_glist_delete() sets the first character of
4746 	 * ill_name to '\0'.
4747 	 */
4748 	ill->ill_name = (char *)ill + sizeof (*ill);
4749 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4750 	ill->ill_name_length = sizeof (ipif_loopback_name);
4751 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4752 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4753 
4754 	ill->ill_global_timer = INFINITY;
4755 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4756 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4757 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4758 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4759 
4760 	/* No resolver here. */
4761 	ill->ill_net_type = IRE_LOOPBACK;
4762 
4763 	/* Initialize the ipsq */
4764 	if (!ipsq_init(ill))
4765 		goto done;
4766 
4767 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4768 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4769 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4770 #ifdef DEBUG
4771 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4772 #endif
4773 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4774 	if (ipif == NULL)
4775 		goto done;
4776 
4777 	ill->ill_flags = ILLF_MULTICAST;
4778 
4779 	ov6addr = ipif->ipif_v6lcl_addr;
4780 	/* Set up default loopback address and mask. */
4781 	if (!isv6) {
4782 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4783 
4784 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4785 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4786 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4787 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4788 		    ipif->ipif_v6subnet);
4789 		ill->ill_flags |= ILLF_IPV4;
4790 	} else {
4791 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4792 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4793 		ipif->ipif_v6net_mask = ipv6_all_ones;
4794 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4795 		    ipif->ipif_v6subnet);
4796 		ill->ill_flags |= ILLF_IPV6;
4797 	}
4798 
4799 	/*
4800 	 * Chain us in at the end of the ill list. hold the ill
4801 	 * before we make it globally visible. 1 for the lookup.
4802 	 */
4803 	ill->ill_refcnt = 0;
4804 	ill_refhold(ill);
4805 
4806 	ill->ill_frag_count = 0;
4807 	ill->ill_frag_free_num_pkts = 0;
4808 	ill->ill_last_frag_clean_time = 0;
4809 
4810 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4811 
4812 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4813 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4814 
4815 	/* Let SCTP know so that it can add this to its list */
4816 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4817 
4818 	/*
4819 	 * We have already assigned ipif_v6lcl_addr above, but we need to
4820 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
4821 	 * requires to be after ill_glist_insert() since we need the
4822 	 * ill_index set. Pass on ipv6_loopback as the old address.
4823 	 */
4824 	sctp_update_ipif_addr(ipif, ov6addr);
4825 
4826 	/*
4827 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4828 	 */
4829 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4830 		/* Loopback ills aren't in any IPMP group */
4831 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4832 		ipsq_delete(old_ipsq);
4833 	}
4834 
4835 	/*
4836 	 * Delay this till the ipif is allocated as ipif_allocate
4837 	 * de-references ill_phyint for getting the ifindex. We
4838 	 * can't do this before ipif_allocate because ill_phyint_reinit
4839 	 * -> phyint_assign_ifindex expects ipif to be present.
4840 	 */
4841 	mutex_enter(&ill->ill_phyint->phyint_lock);
4842 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4843 	mutex_exit(&ill->ill_phyint->phyint_lock);
4844 
4845 	if (ipst->ips_loopback_ksp == NULL) {
4846 		/* Export loopback interface statistics */
4847 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
4848 		    ipif_loopback_name, "net",
4849 		    KSTAT_TYPE_NAMED, 2, 0,
4850 		    ipst->ips_netstack->netstack_stackid);
4851 		if (ipst->ips_loopback_ksp != NULL) {
4852 			ipst->ips_loopback_ksp->ks_update =
4853 			    loopback_kstat_update;
4854 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
4855 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4856 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4857 			ipst->ips_loopback_ksp->ks_private =
4858 			    (void *)(uintptr_t)ipst->ips_netstack->
4859 			    netstack_stackid;
4860 			kstat_install(ipst->ips_loopback_ksp);
4861 		}
4862 	}
4863 
4864 	if (error != NULL)
4865 		*error = 0;
4866 	*did_alloc = B_TRUE;
4867 	rw_exit(&ipst->ips_ill_g_lock);
4868 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
4869 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
4870 	return (ill);
4871 done:
4872 	if (ill != NULL) {
4873 		if (ill->ill_phyint != NULL) {
4874 			ipsq_t	*ipsq;
4875 
4876 			ipsq = ill->ill_phyint->phyint_ipsq;
4877 			if (ipsq != NULL) {
4878 				ipsq->ipsq_ipst = NULL;
4879 				kmem_free(ipsq, sizeof (ipsq_t));
4880 			}
4881 			mi_free(ill->ill_phyint);
4882 		}
4883 		ill_free_mib(ill);
4884 		if (ill->ill_ipst != NULL)
4885 			netstack_rele(ill->ill_ipst->ips_netstack);
4886 		mi_free(ill);
4887 	}
4888 	rw_exit(&ipst->ips_ill_g_lock);
4889 	if (error != NULL)
4890 		*error = ENOMEM;
4891 	return (NULL);
4892 }
4893 
4894 /*
4895  * For IPP calls - use the ip_stack_t for global stack.
4896  */
4897 ill_t *
4898 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
4899     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
4900 {
4901 	ip_stack_t	*ipst;
4902 	ill_t		*ill;
4903 
4904 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
4905 	if (ipst == NULL) {
4906 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
4907 		return (NULL);
4908 	}
4909 
4910 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
4911 	netstack_rele(ipst->ips_netstack);
4912 	return (ill);
4913 }
4914 
4915 /*
4916  * Return a pointer to the ill which matches the index and IP version type.
4917  */
4918 ill_t *
4919 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4920     ipsq_func_t func, int *err, ip_stack_t *ipst)
4921 {
4922 	ill_t	*ill;
4923 	ipsq_t  *ipsq;
4924 	phyint_t *phyi;
4925 
4926 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4927 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4928 
4929 	if (err != NULL)
4930 		*err = 0;
4931 
4932 	/*
4933 	 * Indexes are stored in the phyint - a common structure
4934 	 * to both IPv4 and IPv6.
4935 	 */
4936 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4937 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4938 	    (void *) &index, NULL);
4939 	if (phyi != NULL) {
4940 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4941 		if (ill != NULL) {
4942 			/*
4943 			 * The block comment at the start of ipif_down
4944 			 * explains the use of the macros used below
4945 			 */
4946 			GRAB_CONN_LOCK(q);
4947 			mutex_enter(&ill->ill_lock);
4948 			if (ILL_CAN_LOOKUP(ill)) {
4949 				ill_refhold_locked(ill);
4950 				mutex_exit(&ill->ill_lock);
4951 				RELEASE_CONN_LOCK(q);
4952 				rw_exit(&ipst->ips_ill_g_lock);
4953 				return (ill);
4954 			} else if (ILL_CAN_WAIT(ill, q)) {
4955 				ipsq = ill->ill_phyint->phyint_ipsq;
4956 				mutex_enter(&ipsq->ipsq_lock);
4957 				rw_exit(&ipst->ips_ill_g_lock);
4958 				mutex_exit(&ill->ill_lock);
4959 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4960 				mutex_exit(&ipsq->ipsq_lock);
4961 				RELEASE_CONN_LOCK(q);
4962 				if (err != NULL)
4963 					*err = EINPROGRESS;
4964 				return (NULL);
4965 			}
4966 			RELEASE_CONN_LOCK(q);
4967 			mutex_exit(&ill->ill_lock);
4968 		}
4969 	}
4970 	rw_exit(&ipst->ips_ill_g_lock);
4971 	if (err != NULL)
4972 		*err = ENXIO;
4973 	return (NULL);
4974 }
4975 
4976 /*
4977  * Return the ifindex next in sequence after the passed in ifindex.
4978  * If there is no next ifindex for the given protocol, return 0.
4979  */
4980 uint_t
4981 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
4982 {
4983 	phyint_t *phyi;
4984 	phyint_t *phyi_initial;
4985 	uint_t   ifindex;
4986 
4987 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4988 
4989 	if (index == 0) {
4990 		phyi = avl_first(
4991 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4992 	} else {
4993 		phyi = phyi_initial = avl_find(
4994 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4995 		    (void *) &index, NULL);
4996 	}
4997 
4998 	for (; phyi != NULL;
4999 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5000 	    phyi, AVL_AFTER)) {
5001 		/*
5002 		 * If we're not returning the first interface in the tree
5003 		 * and we still haven't moved past the phyint_t that
5004 		 * corresponds to index, avl_walk needs to be called again
5005 		 */
5006 		if (!((index != 0) && (phyi == phyi_initial))) {
5007 			if (isv6) {
5008 				if ((phyi->phyint_illv6) &&
5009 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5010 				    (phyi->phyint_illv6->ill_isv6 == 1))
5011 					break;
5012 			} else {
5013 				if ((phyi->phyint_illv4) &&
5014 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5015 				    (phyi->phyint_illv4->ill_isv6 == 0))
5016 					break;
5017 			}
5018 		}
5019 	}
5020 
5021 	rw_exit(&ipst->ips_ill_g_lock);
5022 
5023 	if (phyi != NULL)
5024 		ifindex = phyi->phyint_ifindex;
5025 	else
5026 		ifindex = 0;
5027 
5028 	return (ifindex);
5029 }
5030 
5031 /*
5032  * Return the ifindex for the named interface.
5033  * If there is no next ifindex for the interface, return 0.
5034  */
5035 uint_t
5036 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5037 {
5038 	phyint_t	*phyi;
5039 	avl_index_t	where = 0;
5040 	uint_t		ifindex;
5041 
5042 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5043 
5044 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5045 	    name, &where)) == NULL) {
5046 		rw_exit(&ipst->ips_ill_g_lock);
5047 		return (0);
5048 	}
5049 
5050 	ifindex = phyi->phyint_ifindex;
5051 
5052 	rw_exit(&ipst->ips_ill_g_lock);
5053 
5054 	return (ifindex);
5055 }
5056 
5057 /*
5058  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5059  * that gives a running thread a reference to the ill. This reference must be
5060  * released by the thread when it is done accessing the ill and related
5061  * objects. ill_refcnt can not be used to account for static references
5062  * such as other structures pointing to an ill. Callers must generally
5063  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5064  * or be sure that the ill is not being deleted or changing state before
5065  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5066  * ill won't change any of its critical state such as address, netmask etc.
5067  */
5068 void
5069 ill_refhold(ill_t *ill)
5070 {
5071 	mutex_enter(&ill->ill_lock);
5072 	ill->ill_refcnt++;
5073 	ILL_TRACE_REF(ill);
5074 	mutex_exit(&ill->ill_lock);
5075 }
5076 
5077 void
5078 ill_refhold_locked(ill_t *ill)
5079 {
5080 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5081 	ill->ill_refcnt++;
5082 	ILL_TRACE_REF(ill);
5083 }
5084 
5085 int
5086 ill_check_and_refhold(ill_t *ill)
5087 {
5088 	mutex_enter(&ill->ill_lock);
5089 	if (ILL_CAN_LOOKUP(ill)) {
5090 		ill_refhold_locked(ill);
5091 		mutex_exit(&ill->ill_lock);
5092 		return (0);
5093 	}
5094 	mutex_exit(&ill->ill_lock);
5095 	return (ILL_LOOKUP_FAILED);
5096 }
5097 
5098 /*
5099  * Must not be called while holding any locks. Otherwise if this is
5100  * the last reference to be released, there is a chance of recursive mutex
5101  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5102  * to restart an ioctl.
5103  */
5104 void
5105 ill_refrele(ill_t *ill)
5106 {
5107 	mutex_enter(&ill->ill_lock);
5108 	ASSERT(ill->ill_refcnt != 0);
5109 	ill->ill_refcnt--;
5110 	ILL_UNTRACE_REF(ill);
5111 	if (ill->ill_refcnt != 0) {
5112 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5113 		mutex_exit(&ill->ill_lock);
5114 		return;
5115 	}
5116 
5117 	/* Drops the ill_lock */
5118 	ipif_ill_refrele_tail(ill);
5119 }
5120 
5121 /*
5122  * Obtain a weak reference count on the ill. This reference ensures the
5123  * ill won't be freed, but the ill may change any of its critical state
5124  * such as netmask, address etc. Returns an error if the ill has started
5125  * closing.
5126  */
5127 boolean_t
5128 ill_waiter_inc(ill_t *ill)
5129 {
5130 	mutex_enter(&ill->ill_lock);
5131 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5132 		mutex_exit(&ill->ill_lock);
5133 		return (B_FALSE);
5134 	}
5135 	ill->ill_waiters++;
5136 	mutex_exit(&ill->ill_lock);
5137 	return (B_TRUE);
5138 }
5139 
5140 void
5141 ill_waiter_dcr(ill_t *ill)
5142 {
5143 	mutex_enter(&ill->ill_lock);
5144 	ill->ill_waiters--;
5145 	if (ill->ill_waiters == 0)
5146 		cv_broadcast(&ill->ill_cv);
5147 	mutex_exit(&ill->ill_lock);
5148 }
5149 
5150 /*
5151  * Named Dispatch routine to produce a formatted report on all ILLs.
5152  * This report is accessed by using the ndd utility to "get" ND variable
5153  * "ip_ill_status".
5154  */
5155 /* ARGSUSED */
5156 int
5157 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5158 {
5159 	ill_t		*ill;
5160 	ill_walk_context_t ctx;
5161 	ip_stack_t	*ipst;
5162 
5163 	ipst = CONNQ_TO_IPST(q);
5164 
5165 	(void) mi_mpprintf(mp,
5166 	    "ILL      " MI_COL_HDRPAD_STR
5167 	/*   01234567[89ABCDEF] */
5168 	    "rq       " MI_COL_HDRPAD_STR
5169 	/*   01234567[89ABCDEF] */
5170 	    "wq       " MI_COL_HDRPAD_STR
5171 	/*   01234567[89ABCDEF] */
5172 	    "upcnt mxfrg err name");
5173 	/*   12345 12345 123 xxxxxxxx  */
5174 
5175 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5176 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5177 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5178 		(void) mi_mpprintf(mp,
5179 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5180 		    "%05u %05u %03d %s",
5181 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5182 		    ill->ill_ipif_up_count,
5183 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5184 	}
5185 	rw_exit(&ipst->ips_ill_g_lock);
5186 
5187 	return (0);
5188 }
5189 
5190 /*
5191  * Named Dispatch routine to produce a formatted report on all IPIFs.
5192  * This report is accessed by using the ndd utility to "get" ND variable
5193  * "ip_ipif_status".
5194  */
5195 /* ARGSUSED */
5196 int
5197 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5198 {
5199 	char	buf1[INET6_ADDRSTRLEN];
5200 	char	buf2[INET6_ADDRSTRLEN];
5201 	char	buf3[INET6_ADDRSTRLEN];
5202 	char	buf4[INET6_ADDRSTRLEN];
5203 	char	buf5[INET6_ADDRSTRLEN];
5204 	char	buf6[INET6_ADDRSTRLEN];
5205 	char	buf[LIFNAMSIZ];
5206 	ill_t	*ill;
5207 	ipif_t	*ipif;
5208 	nv_t	*nvp;
5209 	uint64_t flags;
5210 	zoneid_t zoneid;
5211 	ill_walk_context_t ctx;
5212 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5213 
5214 	(void) mi_mpprintf(mp,
5215 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5216 	    "\tlocal address\n"
5217 	    "\tsrc address\n"
5218 	    "\tsubnet\n"
5219 	    "\tmask\n"
5220 	    "\tbroadcast\n"
5221 	    "\tp-p-dst");
5222 
5223 	ASSERT(q->q_next == NULL);
5224 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5225 
5226 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5227 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5228 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5229 		for (ipif = ill->ill_ipif; ipif != NULL;
5230 		    ipif = ipif->ipif_next) {
5231 			if (zoneid != GLOBAL_ZONEID &&
5232 			    zoneid != ipif->ipif_zoneid &&
5233 			    ipif->ipif_zoneid != ALL_ZONES)
5234 				continue;
5235 
5236 			ipif_get_name(ipif, buf, sizeof (buf));
5237 			(void) mi_mpprintf(mp,
5238 			    MI_COL_PTRFMT_STR
5239 			    "%04u %05u %u/%u/%u %s %d",
5240 			    (void *)ipif,
5241 			    ipif->ipif_metric, ipif->ipif_mtu,
5242 			    ipif->ipif_ib_pkt_count,
5243 			    ipif->ipif_ob_pkt_count,
5244 			    ipif->ipif_fo_pkt_count,
5245 			    buf,
5246 			    ipif->ipif_zoneid);
5247 
5248 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5249 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5250 
5251 		/* Tack on text strings for any flags. */
5252 		nvp = ipif_nv_tbl;
5253 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5254 			if (nvp->nv_value & flags)
5255 				(void) mi_mpprintf_nr(mp, " %s",
5256 				    nvp->nv_name);
5257 		}
5258 		(void) mi_mpprintf(mp,
5259 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5260 		    inet_ntop(AF_INET6,
5261 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5262 		    inet_ntop(AF_INET6,
5263 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5264 		    inet_ntop(AF_INET6,
5265 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5266 		    inet_ntop(AF_INET6,
5267 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5268 		    inet_ntop(AF_INET6,
5269 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5270 		    inet_ntop(AF_INET6,
5271 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5272 		}
5273 	}
5274 	rw_exit(&ipst->ips_ill_g_lock);
5275 	return (0);
5276 }
5277 
5278 /*
5279  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5280  * driver.  We construct best guess defaults for lower level information that
5281  * we need.  If an interface is brought up without injection of any overriding
5282  * information from outside, we have to be ready to go with these defaults.
5283  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5284  * we primarely want the dl_provider_style.
5285  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5286  * at which point we assume the other part of the information is valid.
5287  */
5288 void
5289 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5290 {
5291 	uchar_t		*brdcst_addr;
5292 	uint_t		brdcst_addr_length, phys_addr_length;
5293 	t_scalar_t	sap_length;
5294 	dl_info_ack_t	*dlia;
5295 	ip_m_t		*ipm;
5296 	dl_qos_cl_sel1_t *sel1;
5297 
5298 	ASSERT(IAM_WRITER_ILL(ill));
5299 
5300 	/*
5301 	 * Till the ill is fully up ILL_CHANGING will be set and
5302 	 * the ill is not globally visible. So no need for a lock.
5303 	 */
5304 	dlia = (dl_info_ack_t *)mp->b_rptr;
5305 	ill->ill_mactype = dlia->dl_mac_type;
5306 
5307 	ipm = ip_m_lookup(dlia->dl_mac_type);
5308 	if (ipm == NULL) {
5309 		ipm = ip_m_lookup(DL_OTHER);
5310 		ASSERT(ipm != NULL);
5311 	}
5312 	ill->ill_media = ipm;
5313 
5314 	/*
5315 	 * When the new DLPI stuff is ready we'll pull lengths
5316 	 * from dlia.
5317 	 */
5318 	if (dlia->dl_version == DL_VERSION_2) {
5319 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5320 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5321 		    brdcst_addr_length);
5322 		if (brdcst_addr == NULL) {
5323 			brdcst_addr_length = 0;
5324 		}
5325 		sap_length = dlia->dl_sap_length;
5326 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5327 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5328 		    brdcst_addr_length, sap_length, phys_addr_length));
5329 	} else {
5330 		brdcst_addr_length = 6;
5331 		brdcst_addr = ip_six_byte_all_ones;
5332 		sap_length = -2;
5333 		phys_addr_length = brdcst_addr_length;
5334 	}
5335 
5336 	ill->ill_bcast_addr_length = brdcst_addr_length;
5337 	ill->ill_phys_addr_length = phys_addr_length;
5338 	ill->ill_sap_length = sap_length;
5339 	ill->ill_max_frag = dlia->dl_max_sdu;
5340 	ill->ill_max_mtu = ill->ill_max_frag;
5341 
5342 	ill->ill_type = ipm->ip_m_type;
5343 
5344 	if (!ill->ill_dlpi_style_set) {
5345 		if (dlia->dl_provider_style == DL_STYLE2)
5346 			ill->ill_needs_attach = 1;
5347 
5348 		/*
5349 		 * Allocate the first ipif on this ill. We don't delay it
5350 		 * further as ioctl handling assumes atleast one ipif to
5351 		 * be present.
5352 		 *
5353 		 * At this point we don't know whether the ill is v4 or v6.
5354 		 * We will know this whan the SIOCSLIFNAME happens and
5355 		 * the correct value for ill_isv6 will be assigned in
5356 		 * ipif_set_values(). We need to hold the ill lock and
5357 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5358 		 * the wakeup.
5359 		 */
5360 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5361 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5362 		mutex_enter(&ill->ill_lock);
5363 		ASSERT(ill->ill_dlpi_style_set == 0);
5364 		ill->ill_dlpi_style_set = 1;
5365 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5366 		cv_broadcast(&ill->ill_cv);
5367 		mutex_exit(&ill->ill_lock);
5368 		freemsg(mp);
5369 		return;
5370 	}
5371 	ASSERT(ill->ill_ipif != NULL);
5372 	/*
5373 	 * We know whether it is IPv4 or IPv6 now, as this is the
5374 	 * second DL_INFO_ACK we are recieving in response to the
5375 	 * DL_INFO_REQ sent in ipif_set_values.
5376 	 */
5377 	if (ill->ill_isv6)
5378 		ill->ill_sap = IP6_DL_SAP;
5379 	else
5380 		ill->ill_sap = IP_DL_SAP;
5381 	/*
5382 	 * Set ipif_mtu which is used to set the IRE's
5383 	 * ire_max_frag value. The driver could have sent
5384 	 * a different mtu from what it sent last time. No
5385 	 * need to call ipif_mtu_change because IREs have
5386 	 * not yet been created.
5387 	 */
5388 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5389 	/*
5390 	 * Clear all the flags that were set based on ill_bcast_addr_length
5391 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5392 	 * changed now and we need to re-evaluate.
5393 	 */
5394 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5395 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5396 
5397 	/*
5398 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5399 	 * changed now.
5400 	 */
5401 	if (ill->ill_bcast_addr_length == 0) {
5402 		if (ill->ill_resolver_mp != NULL)
5403 			freemsg(ill->ill_resolver_mp);
5404 		if (ill->ill_bcast_mp != NULL)
5405 			freemsg(ill->ill_bcast_mp);
5406 		if (ill->ill_flags & ILLF_XRESOLV)
5407 			ill->ill_net_type = IRE_IF_RESOLVER;
5408 		else
5409 			ill->ill_net_type = IRE_IF_NORESOLVER;
5410 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5411 		    ill->ill_phys_addr_length,
5412 		    ill->ill_sap,
5413 		    ill->ill_sap_length);
5414 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5415 
5416 		if (ill->ill_isv6)
5417 			/*
5418 			 * Note: xresolv interfaces will eventually need NOARP
5419 			 * set here as well, but that will require those
5420 			 * external resolvers to have some knowledge of
5421 			 * that flag and act appropriately. Not to be changed
5422 			 * at present.
5423 			 */
5424 			ill->ill_flags |= ILLF_NONUD;
5425 		else
5426 			ill->ill_flags |= ILLF_NOARP;
5427 
5428 		if (ill->ill_phys_addr_length == 0) {
5429 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5430 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5431 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5432 			} else {
5433 				/* pt-pt supports multicast. */
5434 				ill->ill_flags |= ILLF_MULTICAST;
5435 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5436 			}
5437 		}
5438 	} else {
5439 		ill->ill_net_type = IRE_IF_RESOLVER;
5440 		if (ill->ill_bcast_mp != NULL)
5441 			freemsg(ill->ill_bcast_mp);
5442 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5443 		    ill->ill_bcast_addr_length, ill->ill_sap,
5444 		    ill->ill_sap_length);
5445 		/*
5446 		 * Later detect lack of DLPI driver multicast
5447 		 * capability by catching DL_ENABMULTI errors in
5448 		 * ip_rput_dlpi.
5449 		 */
5450 		ill->ill_flags |= ILLF_MULTICAST;
5451 		if (!ill->ill_isv6)
5452 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5453 	}
5454 	/* By default an interface does not support any CoS marking */
5455 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5456 
5457 	/*
5458 	 * If we get QoS information in DL_INFO_ACK, the device supports
5459 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5460 	 */
5461 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5462 	    dlia->dl_qos_length);
5463 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5464 		ill->ill_flags |= ILLF_COS_ENABLED;
5465 	}
5466 
5467 	/* Clear any previous error indication. */
5468 	ill->ill_error = 0;
5469 	freemsg(mp);
5470 }
5471 
5472 /*
5473  * Perform various checks to verify that an address would make sense as a
5474  * local, remote, or subnet interface address.
5475  */
5476 static boolean_t
5477 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5478 {
5479 	ipaddr_t	net_mask;
5480 
5481 	/*
5482 	 * Don't allow all zeroes, or all ones, but allow
5483 	 * all ones netmask.
5484 	 */
5485 	if ((net_mask = ip_net_mask(addr)) == 0)
5486 		return (B_FALSE);
5487 	/* A given netmask overrides the "guess" netmask */
5488 	if (subnet_mask != 0)
5489 		net_mask = subnet_mask;
5490 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5491 	    (addr == (addr | ~net_mask)))) {
5492 		return (B_FALSE);
5493 	}
5494 
5495 	/*
5496 	 * Even if the netmask is all ones, we do not allow address to be
5497 	 * 255.255.255.255
5498 	 */
5499 	if (addr == INADDR_BROADCAST)
5500 		return (B_FALSE);
5501 
5502 	if (CLASSD(addr))
5503 		return (B_FALSE);
5504 
5505 	return (B_TRUE);
5506 }
5507 
5508 #define	V6_IPIF_LINKLOCAL(p)	\
5509 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5510 
5511 /*
5512  * Compare two given ipifs and check if the second one is better than
5513  * the first one using the order of preference (not taking deprecated
5514  * into acount) specified in ipif_lookup_multicast().
5515  */
5516 static boolean_t
5517 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5518 {
5519 	/* Check the least preferred first. */
5520 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5521 		/* If both ipifs are the same, use the first one. */
5522 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5523 			return (B_FALSE);
5524 		else
5525 			return (B_TRUE);
5526 	}
5527 
5528 	/* For IPv6, check for link local address. */
5529 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5530 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5531 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5532 			/* The second one is equal or less preferred. */
5533 			return (B_FALSE);
5534 		} else {
5535 			return (B_TRUE);
5536 		}
5537 	}
5538 
5539 	/* Then check for point to point interface. */
5540 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5541 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5542 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5543 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5544 			return (B_FALSE);
5545 		} else {
5546 			return (B_TRUE);
5547 		}
5548 	}
5549 
5550 	/* old_ipif is a normal interface, so no need to use the new one. */
5551 	return (B_FALSE);
5552 }
5553 
5554 /*
5555  * Find any non-virtual, not condemned, and up multicast capable interface
5556  * given an IP instance and zoneid.  Order of preference is:
5557  *
5558  * 1. normal
5559  * 1.1 normal, but deprecated
5560  * 2. point to point
5561  * 2.1 point to point, but deprecated
5562  * 3. link local
5563  * 3.1 link local, but deprecated
5564  * 4. loopback.
5565  */
5566 ipif_t *
5567 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5568 {
5569 	ill_t			*ill;
5570 	ill_walk_context_t	ctx;
5571 	ipif_t			*ipif;
5572 	ipif_t			*saved_ipif = NULL;
5573 	ipif_t			*dep_ipif = NULL;
5574 
5575 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5576 	if (isv6)
5577 		ill = ILL_START_WALK_V6(&ctx, ipst);
5578 	else
5579 		ill = ILL_START_WALK_V4(&ctx, ipst);
5580 
5581 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5582 		mutex_enter(&ill->ill_lock);
5583 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5584 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5585 			mutex_exit(&ill->ill_lock);
5586 			continue;
5587 		}
5588 		for (ipif = ill->ill_ipif; ipif != NULL;
5589 		    ipif = ipif->ipif_next) {
5590 			if (zoneid != ipif->ipif_zoneid &&
5591 			    zoneid != ALL_ZONES &&
5592 			    ipif->ipif_zoneid != ALL_ZONES) {
5593 				continue;
5594 			}
5595 			if (!(ipif->ipif_flags & IPIF_UP) ||
5596 			    !IPIF_CAN_LOOKUP(ipif)) {
5597 				continue;
5598 			}
5599 
5600 			/*
5601 			 * Found one candidate.  If it is deprecated,
5602 			 * remember it in dep_ipif.  If it is not deprecated,
5603 			 * remember it in saved_ipif.
5604 			 */
5605 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5606 				if (dep_ipif == NULL) {
5607 					dep_ipif = ipif;
5608 				} else if (ipif_comp_multi(dep_ipif, ipif,
5609 				    isv6)) {
5610 					/*
5611 					 * If the previous dep_ipif does not
5612 					 * belong to the same ill, we've done
5613 					 * a ipif_refhold() on it.  So we need
5614 					 * to release it.
5615 					 */
5616 					if (dep_ipif->ipif_ill != ill)
5617 						ipif_refrele(dep_ipif);
5618 					dep_ipif = ipif;
5619 				}
5620 				continue;
5621 			}
5622 			if (saved_ipif == NULL) {
5623 				saved_ipif = ipif;
5624 			} else {
5625 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5626 					if (saved_ipif->ipif_ill != ill)
5627 						ipif_refrele(saved_ipif);
5628 					saved_ipif = ipif;
5629 				}
5630 			}
5631 		}
5632 		/*
5633 		 * Before going to the next ill, do a ipif_refhold() on the
5634 		 * saved ones.
5635 		 */
5636 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5637 			ipif_refhold_locked(saved_ipif);
5638 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5639 			ipif_refhold_locked(dep_ipif);
5640 		mutex_exit(&ill->ill_lock);
5641 	}
5642 	rw_exit(&ipst->ips_ill_g_lock);
5643 
5644 	/*
5645 	 * If we have only the saved_ipif, return it.  But if we have both
5646 	 * saved_ipif and dep_ipif, check to see which one is better.
5647 	 */
5648 	if (saved_ipif != NULL) {
5649 		if (dep_ipif != NULL) {
5650 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5651 				ipif_refrele(saved_ipif);
5652 				return (dep_ipif);
5653 			} else {
5654 				ipif_refrele(dep_ipif);
5655 				return (saved_ipif);
5656 			}
5657 		}
5658 		return (saved_ipif);
5659 	} else {
5660 		return (dep_ipif);
5661 	}
5662 }
5663 
5664 /*
5665  * This function is called when an application does not specify an interface
5666  * to be used for multicast traffic (joining a group/sending data).  It
5667  * calls ire_lookup_multi() to look for an interface route for the
5668  * specified multicast group.  Doing this allows the administrator to add
5669  * prefix routes for multicast to indicate which interface to be used for
5670  * multicast traffic in the above scenario.  The route could be for all
5671  * multicast (224.0/4), for a single multicast group (a /32 route) or
5672  * anything in between.  If there is no such multicast route, we just find
5673  * any multicast capable interface and return it.  The returned ipif
5674  * is refhold'ed.
5675  */
5676 ipif_t *
5677 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5678 {
5679 	ire_t			*ire;
5680 	ipif_t			*ipif;
5681 
5682 	ire = ire_lookup_multi(group, zoneid, ipst);
5683 	if (ire != NULL) {
5684 		ipif = ire->ire_ipif;
5685 		ipif_refhold(ipif);
5686 		ire_refrele(ire);
5687 		return (ipif);
5688 	}
5689 
5690 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5691 }
5692 
5693 /*
5694  * Look for an ipif with the specified interface address and destination.
5695  * The destination address is used only for matching point-to-point interfaces.
5696  */
5697 ipif_t *
5698 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5699     ipsq_func_t func, int *error, ip_stack_t *ipst)
5700 {
5701 	ipif_t	*ipif;
5702 	ill_t	*ill;
5703 	ill_walk_context_t ctx;
5704 	ipsq_t	*ipsq;
5705 
5706 	if (error != NULL)
5707 		*error = 0;
5708 
5709 	/*
5710 	 * First match all the point-to-point interfaces
5711 	 * before looking at non-point-to-point interfaces.
5712 	 * This is done to avoid returning non-point-to-point
5713 	 * ipif instead of unnumbered point-to-point ipif.
5714 	 */
5715 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5716 	ill = ILL_START_WALK_V4(&ctx, ipst);
5717 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5718 		GRAB_CONN_LOCK(q);
5719 		mutex_enter(&ill->ill_lock);
5720 		for (ipif = ill->ill_ipif; ipif != NULL;
5721 		    ipif = ipif->ipif_next) {
5722 			/* Allow the ipif to be down */
5723 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5724 			    (ipif->ipif_lcl_addr == if_addr) &&
5725 			    (ipif->ipif_pp_dst_addr == dst)) {
5726 				/*
5727 				 * The block comment at the start of ipif_down
5728 				 * explains the use of the macros used below
5729 				 */
5730 				if (IPIF_CAN_LOOKUP(ipif)) {
5731 					ipif_refhold_locked(ipif);
5732 					mutex_exit(&ill->ill_lock);
5733 					RELEASE_CONN_LOCK(q);
5734 					rw_exit(&ipst->ips_ill_g_lock);
5735 					return (ipif);
5736 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5737 					ipsq = ill->ill_phyint->phyint_ipsq;
5738 					mutex_enter(&ipsq->ipsq_lock);
5739 					mutex_exit(&ill->ill_lock);
5740 					rw_exit(&ipst->ips_ill_g_lock);
5741 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5742 					    ill);
5743 					mutex_exit(&ipsq->ipsq_lock);
5744 					RELEASE_CONN_LOCK(q);
5745 					if (error != NULL)
5746 						*error = EINPROGRESS;
5747 					return (NULL);
5748 				}
5749 			}
5750 		}
5751 		mutex_exit(&ill->ill_lock);
5752 		RELEASE_CONN_LOCK(q);
5753 	}
5754 	rw_exit(&ipst->ips_ill_g_lock);
5755 
5756 	/* lookup the ipif based on interface address */
5757 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5758 	    ipst);
5759 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5760 	return (ipif);
5761 }
5762 
5763 /*
5764  * Look for an ipif with the specified address. For point-point links
5765  * we look for matches on either the destination address and the local
5766  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5767  * is set.
5768  * Matches on a specific ill if match_ill is set.
5769  */
5770 ipif_t *
5771 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5772     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
5773 {
5774 	ipif_t  *ipif;
5775 	ill_t   *ill;
5776 	boolean_t ptp = B_FALSE;
5777 	ipsq_t	*ipsq;
5778 	ill_walk_context_t	ctx;
5779 
5780 	if (error != NULL)
5781 		*error = 0;
5782 
5783 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5784 	/*
5785 	 * Repeat twice, first based on local addresses and
5786 	 * next time for pointopoint.
5787 	 */
5788 repeat:
5789 	ill = ILL_START_WALK_V4(&ctx, ipst);
5790 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5791 		if (match_ill != NULL && ill != match_ill) {
5792 			continue;
5793 		}
5794 		GRAB_CONN_LOCK(q);
5795 		mutex_enter(&ill->ill_lock);
5796 		for (ipif = ill->ill_ipif; ipif != NULL;
5797 		    ipif = ipif->ipif_next) {
5798 			if (zoneid != ALL_ZONES &&
5799 			    zoneid != ipif->ipif_zoneid &&
5800 			    ipif->ipif_zoneid != ALL_ZONES)
5801 				continue;
5802 			/* Allow the ipif to be down */
5803 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5804 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5805 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5806 			    (ipif->ipif_pp_dst_addr == addr))) {
5807 				/*
5808 				 * The block comment at the start of ipif_down
5809 				 * explains the use of the macros used below
5810 				 */
5811 				if (IPIF_CAN_LOOKUP(ipif)) {
5812 					ipif_refhold_locked(ipif);
5813 					mutex_exit(&ill->ill_lock);
5814 					RELEASE_CONN_LOCK(q);
5815 					rw_exit(&ipst->ips_ill_g_lock);
5816 					return (ipif);
5817 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5818 					ipsq = ill->ill_phyint->phyint_ipsq;
5819 					mutex_enter(&ipsq->ipsq_lock);
5820 					mutex_exit(&ill->ill_lock);
5821 					rw_exit(&ipst->ips_ill_g_lock);
5822 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5823 					    ill);
5824 					mutex_exit(&ipsq->ipsq_lock);
5825 					RELEASE_CONN_LOCK(q);
5826 					if (error != NULL)
5827 						*error = EINPROGRESS;
5828 					return (NULL);
5829 				}
5830 			}
5831 		}
5832 		mutex_exit(&ill->ill_lock);
5833 		RELEASE_CONN_LOCK(q);
5834 	}
5835 
5836 	/* If we already did the ptp case, then we are done */
5837 	if (ptp) {
5838 		rw_exit(&ipst->ips_ill_g_lock);
5839 		if (error != NULL)
5840 			*error = ENXIO;
5841 		return (NULL);
5842 	}
5843 	ptp = B_TRUE;
5844 	goto repeat;
5845 }
5846 
5847 /*
5848  * Look for an ipif with the specified address. For point-point links
5849  * we look for matches on either the destination address and the local
5850  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5851  * is set.
5852  * Matches on a specific ill if match_ill is set.
5853  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
5854  */
5855 zoneid_t
5856 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
5857 {
5858 	zoneid_t zoneid;
5859 	ipif_t  *ipif;
5860 	ill_t   *ill;
5861 	boolean_t ptp = B_FALSE;
5862 	ill_walk_context_t	ctx;
5863 
5864 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5865 	/*
5866 	 * Repeat twice, first based on local addresses and
5867 	 * next time for pointopoint.
5868 	 */
5869 repeat:
5870 	ill = ILL_START_WALK_V4(&ctx, ipst);
5871 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5872 		if (match_ill != NULL && ill != match_ill) {
5873 			continue;
5874 		}
5875 		mutex_enter(&ill->ill_lock);
5876 		for (ipif = ill->ill_ipif; ipif != NULL;
5877 		    ipif = ipif->ipif_next) {
5878 			/* Allow the ipif to be down */
5879 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5880 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5881 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5882 			    (ipif->ipif_pp_dst_addr == addr)) &&
5883 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
5884 				zoneid = ipif->ipif_zoneid;
5885 				mutex_exit(&ill->ill_lock);
5886 				rw_exit(&ipst->ips_ill_g_lock);
5887 				/*
5888 				 * If ipif_zoneid was ALL_ZONES then we have
5889 				 * a trusted extensions shared IP address.
5890 				 * In that case GLOBAL_ZONEID works to send.
5891 				 */
5892 				if (zoneid == ALL_ZONES)
5893 					zoneid = GLOBAL_ZONEID;
5894 				return (zoneid);
5895 			}
5896 		}
5897 		mutex_exit(&ill->ill_lock);
5898 	}
5899 
5900 	/* If we already did the ptp case, then we are done */
5901 	if (ptp) {
5902 		rw_exit(&ipst->ips_ill_g_lock);
5903 		return (ALL_ZONES);
5904 	}
5905 	ptp = B_TRUE;
5906 	goto repeat;
5907 }
5908 
5909 /*
5910  * Look for an ipif that matches the specified remote address i.e. the
5911  * ipif that would receive the specified packet.
5912  * First look for directly connected interfaces and then do a recursive
5913  * IRE lookup and pick the first ipif corresponding to the source address in the
5914  * ire.
5915  * Returns: held ipif
5916  */
5917 ipif_t *
5918 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5919 {
5920 	ipif_t	*ipif;
5921 	ire_t	*ire;
5922 	ip_stack_t	*ipst = ill->ill_ipst;
5923 
5924 	ASSERT(!ill->ill_isv6);
5925 
5926 	/*
5927 	 * Someone could be changing this ipif currently or change it
5928 	 * after we return this. Thus  a few packets could use the old
5929 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5930 	 * will atomically be updated or cleaned up with the new value
5931 	 * Thus we don't need a lock to check the flags or other attrs below.
5932 	 */
5933 	mutex_enter(&ill->ill_lock);
5934 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5935 		if (!IPIF_CAN_LOOKUP(ipif))
5936 			continue;
5937 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5938 		    ipif->ipif_zoneid != ALL_ZONES)
5939 			continue;
5940 		/* Allow the ipif to be down */
5941 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5942 			if ((ipif->ipif_pp_dst_addr == addr) ||
5943 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5944 			    ipif->ipif_lcl_addr == addr)) {
5945 				ipif_refhold_locked(ipif);
5946 				mutex_exit(&ill->ill_lock);
5947 				return (ipif);
5948 			}
5949 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5950 			ipif_refhold_locked(ipif);
5951 			mutex_exit(&ill->ill_lock);
5952 			return (ipif);
5953 		}
5954 	}
5955 	mutex_exit(&ill->ill_lock);
5956 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5957 	    NULL, MATCH_IRE_RECURSIVE, ipst);
5958 	if (ire != NULL) {
5959 		/*
5960 		 * The callers of this function wants to know the
5961 		 * interface on which they have to send the replies
5962 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5963 		 * derived from different ills, we really don't care
5964 		 * what we return here.
5965 		 */
5966 		ipif = ire->ire_ipif;
5967 		if (ipif != NULL) {
5968 			ipif_refhold(ipif);
5969 			ire_refrele(ire);
5970 			return (ipif);
5971 		}
5972 		ire_refrele(ire);
5973 	}
5974 	/* Pick the first interface */
5975 	ipif = ipif_get_next_ipif(NULL, ill);
5976 	return (ipif);
5977 }
5978 
5979 /*
5980  * This func does not prevent refcnt from increasing. But if
5981  * the caller has taken steps to that effect, then this func
5982  * can be used to determine whether the ill has become quiescent
5983  */
5984 static boolean_t
5985 ill_is_quiescent(ill_t *ill)
5986 {
5987 	ipif_t	*ipif;
5988 
5989 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5990 
5991 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5992 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
5993 			return (B_FALSE);
5994 		}
5995 	}
5996 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
5997 		return (B_FALSE);
5998 	}
5999 	return (B_TRUE);
6000 }
6001 
6002 boolean_t
6003 ill_is_freeable(ill_t *ill)
6004 {
6005 	ipif_t	*ipif;
6006 
6007 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6008 
6009 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6010 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6011 			return (B_FALSE);
6012 		}
6013 	}
6014 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6015 		return (B_FALSE);
6016 	}
6017 	return (B_TRUE);
6018 }
6019 
6020 /*
6021  * This func does not prevent refcnt from increasing. But if
6022  * the caller has taken steps to that effect, then this func
6023  * can be used to determine whether the ipif has become quiescent
6024  */
6025 static boolean_t
6026 ipif_is_quiescent(ipif_t *ipif)
6027 {
6028 	ill_t *ill;
6029 
6030 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6031 
6032 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6033 		return (B_FALSE);
6034 	}
6035 
6036 	ill = ipif->ipif_ill;
6037 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6038 	    ill->ill_logical_down) {
6039 		return (B_TRUE);
6040 	}
6041 
6042 	/* This is the last ipif going down or being deleted on this ill */
6043 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6044 		return (B_FALSE);
6045 	}
6046 
6047 	return (B_TRUE);
6048 }
6049 
6050 /*
6051  * return true if the ipif can be destroyed: the ipif has to be quiescent
6052  * with zero references from ire/nce/ilm to it.
6053  */
6054 static boolean_t
6055 ipif_is_freeable(ipif_t *ipif)
6056 {
6057 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6058 	ASSERT(ipif->ipif_id != 0);
6059 	return (ipif->ipif_refcnt == 0 && IPIF_FREE_OK(ipif));
6060 }
6061 
6062 /*
6063  * This func does not prevent refcnt from increasing. But if
6064  * the caller has taken steps to that effect, then this func
6065  * can be used to determine whether the ipifs marked with IPIF_MOVING
6066  * have become quiescent and can be moved in a failover/failback.
6067  */
6068 static ipif_t *
6069 ill_quiescent_to_move(ill_t *ill)
6070 {
6071 	ipif_t  *ipif;
6072 
6073 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6074 
6075 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6076 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6077 			if (ipif->ipif_refcnt != 0 ||
6078 			    !IPIF_DOWN_OK(ipif)) {
6079 				return (ipif);
6080 			}
6081 		}
6082 	}
6083 	return (NULL);
6084 }
6085 
6086 /*
6087  * The ipif/ill/ire has been refreled. Do the tail processing.
6088  * Determine if the ipif or ill in question has become quiescent and if so
6089  * wakeup close and/or restart any queued pending ioctl that is waiting
6090  * for the ipif_down (or ill_down)
6091  */
6092 void
6093 ipif_ill_refrele_tail(ill_t *ill)
6094 {
6095 	mblk_t	*mp;
6096 	conn_t	*connp;
6097 	ipsq_t	*ipsq;
6098 	ipif_t	*ipif;
6099 	dl_notify_ind_t *dlindp;
6100 
6101 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6102 
6103 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6104 	    ill_is_freeable(ill)) {
6105 		/* ill_close may be waiting */
6106 		cv_broadcast(&ill->ill_cv);
6107 	}
6108 
6109 	/* ipsq can't change because ill_lock  is held */
6110 	ipsq = ill->ill_phyint->phyint_ipsq;
6111 	if (ipsq->ipsq_waitfor == 0) {
6112 		/* Not waiting for anything, just return. */
6113 		mutex_exit(&ill->ill_lock);
6114 		return;
6115 	}
6116 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6117 	    ipsq->ipsq_pending_ipif != NULL);
6118 	/*
6119 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6120 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6121 	 * be zero for restarting an ioctl that ends up downing the ill.
6122 	 */
6123 	ipif = ipsq->ipsq_pending_ipif;
6124 	if (ipif->ipif_ill != ill) {
6125 		/* The ioctl is pending on some other ill. */
6126 		mutex_exit(&ill->ill_lock);
6127 		return;
6128 	}
6129 
6130 	switch (ipsq->ipsq_waitfor) {
6131 	case IPIF_DOWN:
6132 		if (!ipif_is_quiescent(ipif)) {
6133 			mutex_exit(&ill->ill_lock);
6134 			return;
6135 		}
6136 		break;
6137 	case IPIF_FREE:
6138 		if (!ipif_is_freeable(ipif)) {
6139 			mutex_exit(&ill->ill_lock);
6140 			return;
6141 		}
6142 		break;
6143 
6144 	case ILL_DOWN:
6145 		if (!ill_is_quiescent(ill)) {
6146 			mutex_exit(&ill->ill_lock);
6147 			return;
6148 		}
6149 		break;
6150 	case ILL_FREE:
6151 		/*
6152 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6153 		 * waits synchronously in ip_close, and no message is queued in
6154 		 * ipsq_pending_mp at all in this case
6155 		 */
6156 		if (!ill_is_freeable(ill)) {
6157 			mutex_exit(&ill->ill_lock);
6158 			return;
6159 		}
6160 		break;
6161 
6162 	case ILL_MOVE_OK:
6163 		if (ill_quiescent_to_move(ill) != NULL) {
6164 			mutex_exit(&ill->ill_lock);
6165 			return;
6166 		}
6167 		break;
6168 	default:
6169 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6170 		    (void *)ipsq, ipsq->ipsq_waitfor);
6171 	}
6172 
6173 	/*
6174 	 * Incr refcnt for the qwriter_ip call below which
6175 	 * does a refrele
6176 	 */
6177 	ill_refhold_locked(ill);
6178 	mp = ipsq_pending_mp_get(ipsq, &connp);
6179 	mutex_exit(&ill->ill_lock);
6180 
6181 	ASSERT(mp != NULL);
6182 	/*
6183 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6184 	 * we can only get here when the current operation decides it
6185 	 * it needs to quiesce via ipsq_pending_mp_add().
6186 	 */
6187 	switch (mp->b_datap->db_type) {
6188 	case M_PCPROTO:
6189 	case M_PROTO:
6190 		/*
6191 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6192 		 */
6193 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6194 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6195 
6196 		switch (dlindp->dl_notification) {
6197 		case DL_NOTE_PHYS_ADDR:
6198 			qwriter_ip(ill, ill->ill_rq, mp,
6199 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6200 			return;
6201 		default:
6202 			ASSERT(0);
6203 		}
6204 		break;
6205 
6206 	case M_ERROR:
6207 	case M_HANGUP:
6208 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6209 		    B_TRUE);
6210 		return;
6211 
6212 	case M_IOCTL:
6213 	case M_IOCDATA:
6214 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6215 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6216 		return;
6217 
6218 	default:
6219 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6220 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6221 	}
6222 }
6223 
6224 #ifdef DEBUG
6225 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6226 static void
6227 th_trace_rrecord(th_trace_t *th_trace)
6228 {
6229 	tr_buf_t *tr_buf;
6230 	uint_t lastref;
6231 
6232 	lastref = th_trace->th_trace_lastref;
6233 	lastref++;
6234 	if (lastref == TR_BUF_MAX)
6235 		lastref = 0;
6236 	th_trace->th_trace_lastref = lastref;
6237 	tr_buf = &th_trace->th_trbuf[lastref];
6238 	tr_buf->tr_time = lbolt;
6239 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6240 }
6241 
6242 static void
6243 th_trace_free(void *value)
6244 {
6245 	th_trace_t *th_trace = value;
6246 
6247 	ASSERT(th_trace->th_refcnt == 0);
6248 	kmem_free(th_trace, sizeof (*th_trace));
6249 }
6250 
6251 /*
6252  * Find or create the per-thread hash table used to track object references.
6253  * The ipst argument is NULL if we shouldn't allocate.
6254  *
6255  * Accesses per-thread data, so there's no need to lock here.
6256  */
6257 static mod_hash_t *
6258 th_trace_gethash(ip_stack_t *ipst)
6259 {
6260 	th_hash_t *thh;
6261 
6262 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6263 		mod_hash_t *mh;
6264 		char name[256];
6265 		size_t objsize, rshift;
6266 		int retv;
6267 
6268 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6269 			return (NULL);
6270 		(void) snprintf(name, sizeof (name), "th_trace_%p",
6271 		    (void *)curthread);
6272 
6273 		/*
6274 		 * We use mod_hash_create_extended here rather than the more
6275 		 * obvious mod_hash_create_ptrhash because the latter has a
6276 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6277 		 * block.
6278 		 */
6279 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6280 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6281 		rshift = highbit(objsize);
6282 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6283 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6284 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6285 		if (mh == NULL) {
6286 			kmem_free(thh, sizeof (*thh));
6287 			return (NULL);
6288 		}
6289 		thh->thh_hash = mh;
6290 		thh->thh_ipst = ipst;
6291 		/*
6292 		 * We trace ills, ipifs, ires, and nces.  All of these are
6293 		 * per-IP-stack, so the lock on the thread list is as well.
6294 		 */
6295 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6296 		list_insert_tail(&ip_thread_list, thh);
6297 		rw_exit(&ip_thread_rwlock);
6298 		retv = tsd_set(ip_thread_data, thh);
6299 		ASSERT(retv == 0);
6300 	}
6301 	return (thh != NULL ? thh->thh_hash : NULL);
6302 }
6303 
6304 boolean_t
6305 th_trace_ref(const void *obj, ip_stack_t *ipst)
6306 {
6307 	th_trace_t *th_trace;
6308 	mod_hash_t *mh;
6309 	mod_hash_val_t val;
6310 
6311 	if ((mh = th_trace_gethash(ipst)) == NULL)
6312 		return (B_FALSE);
6313 
6314 	/*
6315 	 * Attempt to locate the trace buffer for this obj and thread.
6316 	 * If it does not exist, then allocate a new trace buffer and
6317 	 * insert into the hash.
6318 	 */
6319 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6320 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6321 		if (th_trace == NULL)
6322 			return (B_FALSE);
6323 
6324 		th_trace->th_id = curthread;
6325 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6326 		    (mod_hash_val_t)th_trace) != 0) {
6327 			kmem_free(th_trace, sizeof (th_trace_t));
6328 			return (B_FALSE);
6329 		}
6330 	} else {
6331 		th_trace = (th_trace_t *)val;
6332 	}
6333 
6334 	ASSERT(th_trace->th_refcnt >= 0 &&
6335 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6336 
6337 	th_trace->th_refcnt++;
6338 	th_trace_rrecord(th_trace);
6339 	return (B_TRUE);
6340 }
6341 
6342 /*
6343  * For the purpose of tracing a reference release, we assume that global
6344  * tracing is always on and that the same thread initiated the reference hold
6345  * is releasing.
6346  */
6347 void
6348 th_trace_unref(const void *obj)
6349 {
6350 	int retv;
6351 	mod_hash_t *mh;
6352 	th_trace_t *th_trace;
6353 	mod_hash_val_t val;
6354 
6355 	mh = th_trace_gethash(NULL);
6356 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6357 	ASSERT(retv == 0);
6358 	th_trace = (th_trace_t *)val;
6359 
6360 	ASSERT(th_trace->th_refcnt > 0);
6361 	th_trace->th_refcnt--;
6362 	th_trace_rrecord(th_trace);
6363 }
6364 
6365 /*
6366  * If tracing has been disabled, then we assume that the reference counts are
6367  * now useless, and we clear them out before destroying the entries.
6368  */
6369 void
6370 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6371 {
6372 	th_hash_t	*thh;
6373 	mod_hash_t	*mh;
6374 	mod_hash_val_t	val;
6375 	th_trace_t	*th_trace;
6376 	int		retv;
6377 
6378 	rw_enter(&ip_thread_rwlock, RW_READER);
6379 	for (thh = list_head(&ip_thread_list); thh != NULL;
6380 	    thh = list_next(&ip_thread_list, thh)) {
6381 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6382 		    &val) == 0) {
6383 			th_trace = (th_trace_t *)val;
6384 			if (trace_disable)
6385 				th_trace->th_refcnt = 0;
6386 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6387 			ASSERT(retv == 0);
6388 		}
6389 	}
6390 	rw_exit(&ip_thread_rwlock);
6391 }
6392 
6393 void
6394 ipif_trace_ref(ipif_t *ipif)
6395 {
6396 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6397 
6398 	if (ipif->ipif_trace_disable)
6399 		return;
6400 
6401 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6402 		ipif->ipif_trace_disable = B_TRUE;
6403 		ipif_trace_cleanup(ipif);
6404 	}
6405 }
6406 
6407 void
6408 ipif_untrace_ref(ipif_t *ipif)
6409 {
6410 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6411 
6412 	if (!ipif->ipif_trace_disable)
6413 		th_trace_unref(ipif);
6414 }
6415 
6416 void
6417 ill_trace_ref(ill_t *ill)
6418 {
6419 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6420 
6421 	if (ill->ill_trace_disable)
6422 		return;
6423 
6424 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6425 		ill->ill_trace_disable = B_TRUE;
6426 		ill_trace_cleanup(ill);
6427 	}
6428 }
6429 
6430 void
6431 ill_untrace_ref(ill_t *ill)
6432 {
6433 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6434 
6435 	if (!ill->ill_trace_disable)
6436 		th_trace_unref(ill);
6437 }
6438 
6439 /*
6440  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6441  * failure, ipif_trace_disable is set.
6442  */
6443 static void
6444 ipif_trace_cleanup(const ipif_t *ipif)
6445 {
6446 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6447 }
6448 
6449 /*
6450  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6451  * failure, ill_trace_disable is set.
6452  */
6453 static void
6454 ill_trace_cleanup(const ill_t *ill)
6455 {
6456 	th_trace_cleanup(ill, ill->ill_trace_disable);
6457 }
6458 #endif /* DEBUG */
6459 
6460 void
6461 ipif_refhold_locked(ipif_t *ipif)
6462 {
6463 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6464 	ipif->ipif_refcnt++;
6465 	IPIF_TRACE_REF(ipif);
6466 }
6467 
6468 void
6469 ipif_refhold(ipif_t *ipif)
6470 {
6471 	ill_t	*ill;
6472 
6473 	ill = ipif->ipif_ill;
6474 	mutex_enter(&ill->ill_lock);
6475 	ipif->ipif_refcnt++;
6476 	IPIF_TRACE_REF(ipif);
6477 	mutex_exit(&ill->ill_lock);
6478 }
6479 
6480 /*
6481  * Must not be called while holding any locks. Otherwise if this is
6482  * the last reference to be released there is a chance of recursive mutex
6483  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6484  * to restart an ioctl.
6485  */
6486 void
6487 ipif_refrele(ipif_t *ipif)
6488 {
6489 	ill_t	*ill;
6490 
6491 	ill = ipif->ipif_ill;
6492 
6493 	mutex_enter(&ill->ill_lock);
6494 	ASSERT(ipif->ipif_refcnt != 0);
6495 	ipif->ipif_refcnt--;
6496 	IPIF_UNTRACE_REF(ipif);
6497 	if (ipif->ipif_refcnt != 0) {
6498 		mutex_exit(&ill->ill_lock);
6499 		return;
6500 	}
6501 
6502 	/* Drops the ill_lock */
6503 	ipif_ill_refrele_tail(ill);
6504 }
6505 
6506 ipif_t *
6507 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6508 {
6509 	ipif_t	*ipif;
6510 
6511 	mutex_enter(&ill->ill_lock);
6512 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6513 	    ipif != NULL; ipif = ipif->ipif_next) {
6514 		if (!IPIF_CAN_LOOKUP(ipif))
6515 			continue;
6516 		ipif_refhold_locked(ipif);
6517 		mutex_exit(&ill->ill_lock);
6518 		return (ipif);
6519 	}
6520 	mutex_exit(&ill->ill_lock);
6521 	return (NULL);
6522 }
6523 
6524 /*
6525  * TODO: make this table extendible at run time
6526  * Return a pointer to the mac type info for 'mac_type'
6527  */
6528 static ip_m_t *
6529 ip_m_lookup(t_uscalar_t mac_type)
6530 {
6531 	ip_m_t	*ipm;
6532 
6533 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6534 		if (ipm->ip_m_mac_type == mac_type)
6535 			return (ipm);
6536 	return (NULL);
6537 }
6538 
6539 /*
6540  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6541  * ipif_arg is passed in to associate it with the correct interface.
6542  * We may need to restart this operation if the ipif cannot be looked up
6543  * due to an exclusive operation that is currently in progress. The restart
6544  * entry point is specified by 'func'
6545  */
6546 int
6547 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6548     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6549     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6550     struct rtsa_s *sp, ip_stack_t *ipst)
6551 {
6552 	ire_t	*ire;
6553 	ire_t	*gw_ire = NULL;
6554 	ipif_t	*ipif = NULL;
6555 	boolean_t ipif_refheld = B_FALSE;
6556 	uint_t	type;
6557 	int	match_flags = MATCH_IRE_TYPE;
6558 	int	error;
6559 	tsol_gc_t *gc = NULL;
6560 	tsol_gcgrp_t *gcgrp = NULL;
6561 	boolean_t gcgrp_xtraref = B_FALSE;
6562 
6563 	ip1dbg(("ip_rt_add:"));
6564 
6565 	if (ire_arg != NULL)
6566 		*ire_arg = NULL;
6567 
6568 	/*
6569 	 * If this is the case of RTF_HOST being set, then we set the netmask
6570 	 * to all ones (regardless if one was supplied).
6571 	 */
6572 	if (flags & RTF_HOST)
6573 		mask = IP_HOST_MASK;
6574 
6575 	/*
6576 	 * Prevent routes with a zero gateway from being created (since
6577 	 * interfaces can currently be plumbed and brought up no assigned
6578 	 * address).
6579 	 */
6580 	if (gw_addr == 0)
6581 		return (ENETUNREACH);
6582 	/*
6583 	 * Get the ipif, if any, corresponding to the gw_addr
6584 	 */
6585 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6586 	    ipst);
6587 	if (ipif != NULL) {
6588 		if (IS_VNI(ipif->ipif_ill)) {
6589 			ipif_refrele(ipif);
6590 			return (EINVAL);
6591 		}
6592 		ipif_refheld = B_TRUE;
6593 	} else if (error == EINPROGRESS) {
6594 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6595 		return (EINPROGRESS);
6596 	} else {
6597 		error = 0;
6598 	}
6599 
6600 	if (ipif != NULL) {
6601 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6602 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6603 	} else {
6604 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6605 	}
6606 
6607 	/*
6608 	 * GateD will attempt to create routes with a loopback interface
6609 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6610 	 * these routes to be added, but create them as interface routes
6611 	 * since the gateway is an interface address.
6612 	 */
6613 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6614 		flags &= ~RTF_GATEWAY;
6615 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6616 		    mask == IP_HOST_MASK) {
6617 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6618 			    ALL_ZONES, NULL, match_flags, ipst);
6619 			if (ire != NULL) {
6620 				ire_refrele(ire);
6621 				if (ipif_refheld)
6622 					ipif_refrele(ipif);
6623 				return (EEXIST);
6624 			}
6625 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6626 			    "for 0x%x\n", (void *)ipif,
6627 			    ipif->ipif_ire_type,
6628 			    ntohl(ipif->ipif_lcl_addr)));
6629 			ire = ire_create(
6630 			    (uchar_t *)&dst_addr,	/* dest address */
6631 			    (uchar_t *)&mask,		/* mask */
6632 			    (uchar_t *)&ipif->ipif_src_addr,
6633 			    NULL,			/* no gateway */
6634 			    &ipif->ipif_mtu,
6635 			    NULL,
6636 			    ipif->ipif_rq,		/* recv-from queue */
6637 			    NULL,			/* no send-to queue */
6638 			    ipif->ipif_ire_type,	/* LOOPBACK */
6639 			    ipif,
6640 			    0,
6641 			    0,
6642 			    0,
6643 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6644 			    RTF_PRIVATE : 0,
6645 			    &ire_uinfo_null,
6646 			    NULL,
6647 			    NULL,
6648 			    ipst);
6649 
6650 			if (ire == NULL) {
6651 				if (ipif_refheld)
6652 					ipif_refrele(ipif);
6653 				return (ENOMEM);
6654 			}
6655 			error = ire_add(&ire, q, mp, func, B_FALSE);
6656 			if (error == 0)
6657 				goto save_ire;
6658 			if (ipif_refheld)
6659 				ipif_refrele(ipif);
6660 			return (error);
6661 
6662 		}
6663 	}
6664 
6665 	/*
6666 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6667 	 * and the gateway address provided is one of the system's interface
6668 	 * addresses.  By using the routing socket interface and supplying an
6669 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6670 	 * specifying an interface route to be created is available which uses
6671 	 * the interface index that specifies the outgoing interface rather than
6672 	 * the address of an outgoing interface (which may not be able to
6673 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6674 	 * flag, routes can be specified which not only specify the next-hop to
6675 	 * be used when routing to a certain prefix, but also which outgoing
6676 	 * interface should be used.
6677 	 *
6678 	 * Previously, interfaces would have unique addresses assigned to them
6679 	 * and so the address assigned to a particular interface could be used
6680 	 * to identify a particular interface.  One exception to this was the
6681 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6682 	 *
6683 	 * With the advent of IPv6 and its link-local addresses, this
6684 	 * restriction was relaxed and interfaces could share addresses between
6685 	 * themselves.  In fact, typically all of the link-local interfaces on
6686 	 * an IPv6 node or router will have the same link-local address.  In
6687 	 * order to differentiate between these interfaces, the use of an
6688 	 * interface index is necessary and this index can be carried inside a
6689 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6690 	 * of using the interface index, however, is that all of the ipif's that
6691 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6692 	 * cannot be used to differentiate between ipif's (or logical
6693 	 * interfaces) that belong to the same ill (physical interface).
6694 	 *
6695 	 * For example, in the following case involving IPv4 interfaces and
6696 	 * logical interfaces
6697 	 *
6698 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6699 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6700 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6701 	 *
6702 	 * the ipif's corresponding to each of these interface routes can be
6703 	 * uniquely identified by the "gateway" (actually interface address).
6704 	 *
6705 	 * In this case involving multiple IPv6 default routes to a particular
6706 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6707 	 * default route is of interest:
6708 	 *
6709 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6710 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6711 	 */
6712 
6713 	/* RTF_GATEWAY not set */
6714 	if (!(flags & RTF_GATEWAY)) {
6715 		queue_t	*stq;
6716 
6717 		if (sp != NULL) {
6718 			ip2dbg(("ip_rt_add: gateway security attributes "
6719 			    "cannot be set with interface route\n"));
6720 			if (ipif_refheld)
6721 				ipif_refrele(ipif);
6722 			return (EINVAL);
6723 		}
6724 
6725 		/*
6726 		 * As the interface index specified with the RTA_IFP sockaddr is
6727 		 * the same for all ipif's off of an ill, the matching logic
6728 		 * below uses MATCH_IRE_ILL if such an index was specified.
6729 		 * This means that routes sharing the same prefix when added
6730 		 * using a RTA_IFP sockaddr must have distinct interface
6731 		 * indices (namely, they must be on distinct ill's).
6732 		 *
6733 		 * On the other hand, since the gateway address will usually be
6734 		 * different for each ipif on the system, the matching logic
6735 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6736 		 * route.  This means that interface routes for the same prefix
6737 		 * can be created if they belong to distinct ipif's and if a
6738 		 * RTA_IFP sockaddr is not present.
6739 		 */
6740 		if (ipif_arg != NULL) {
6741 			if (ipif_refheld)  {
6742 				ipif_refrele(ipif);
6743 				ipif_refheld = B_FALSE;
6744 			}
6745 			ipif = ipif_arg;
6746 			match_flags |= MATCH_IRE_ILL;
6747 		} else {
6748 			/*
6749 			 * Check the ipif corresponding to the gw_addr
6750 			 */
6751 			if (ipif == NULL)
6752 				return (ENETUNREACH);
6753 			match_flags |= MATCH_IRE_IPIF;
6754 		}
6755 		ASSERT(ipif != NULL);
6756 
6757 		/*
6758 		 * We check for an existing entry at this point.
6759 		 *
6760 		 * Since a netmask isn't passed in via the ioctl interface
6761 		 * (SIOCADDRT), we don't check for a matching netmask in that
6762 		 * case.
6763 		 */
6764 		if (!ioctl_msg)
6765 			match_flags |= MATCH_IRE_MASK;
6766 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6767 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6768 		if (ire != NULL) {
6769 			ire_refrele(ire);
6770 			if (ipif_refheld)
6771 				ipif_refrele(ipif);
6772 			return (EEXIST);
6773 		}
6774 
6775 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6776 		    ? ipif->ipif_rq : ipif->ipif_wq;
6777 
6778 		/*
6779 		 * Create a copy of the IRE_LOOPBACK,
6780 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6781 		 * the modified address and netmask.
6782 		 */
6783 		ire = ire_create(
6784 		    (uchar_t *)&dst_addr,
6785 		    (uint8_t *)&mask,
6786 		    (uint8_t *)&ipif->ipif_src_addr,
6787 		    NULL,
6788 		    &ipif->ipif_mtu,
6789 		    NULL,
6790 		    NULL,
6791 		    stq,
6792 		    ipif->ipif_net_type,
6793 		    ipif,
6794 		    0,
6795 		    0,
6796 		    0,
6797 		    flags,
6798 		    &ire_uinfo_null,
6799 		    NULL,
6800 		    NULL,
6801 		    ipst);
6802 		if (ire == NULL) {
6803 			if (ipif_refheld)
6804 				ipif_refrele(ipif);
6805 			return (ENOMEM);
6806 		}
6807 
6808 		/*
6809 		 * Some software (for example, GateD and Sun Cluster) attempts
6810 		 * to create (what amount to) IRE_PREFIX routes with the
6811 		 * loopback address as the gateway.  This is primarily done to
6812 		 * set up prefixes with the RTF_REJECT flag set (for example,
6813 		 * when generating aggregate routes.)
6814 		 *
6815 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6816 		 * IRE_LOOPBACK, then we map the request into a
6817 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
6818 		 * these interface routes, by definition, can only be that.
6819 		 *
6820 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6821 		 * routine, but rather using ire_create() directly.
6822 		 *
6823 		 */
6824 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
6825 			ire->ire_type = IRE_IF_NORESOLVER;
6826 			ire->ire_flags |= RTF_BLACKHOLE;
6827 		}
6828 
6829 		error = ire_add(&ire, q, mp, func, B_FALSE);
6830 		if (error == 0)
6831 			goto save_ire;
6832 
6833 		/*
6834 		 * In the result of failure, ire_add() will have already
6835 		 * deleted the ire in question, so there is no need to
6836 		 * do that here.
6837 		 */
6838 		if (ipif_refheld)
6839 			ipif_refrele(ipif);
6840 		return (error);
6841 	}
6842 	if (ipif_refheld) {
6843 		ipif_refrele(ipif);
6844 		ipif_refheld = B_FALSE;
6845 	}
6846 
6847 	/*
6848 	 * Get an interface IRE for the specified gateway.
6849 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6850 	 * gateway, it is currently unreachable and we fail the request
6851 	 * accordingly.
6852 	 */
6853 	ipif = ipif_arg;
6854 	if (ipif_arg != NULL)
6855 		match_flags |= MATCH_IRE_ILL;
6856 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6857 	    ALL_ZONES, 0, NULL, match_flags, ipst);
6858 	if (gw_ire == NULL)
6859 		return (ENETUNREACH);
6860 
6861 	/*
6862 	 * We create one of three types of IREs as a result of this request
6863 	 * based on the netmask.  A netmask of all ones (which is automatically
6864 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6865 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6866 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6867 	 * destination prefix.
6868 	 */
6869 	if (mask == IP_HOST_MASK)
6870 		type = IRE_HOST;
6871 	else if (mask == 0)
6872 		type = IRE_DEFAULT;
6873 	else
6874 		type = IRE_PREFIX;
6875 
6876 	/* check for a duplicate entry */
6877 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6878 	    NULL, ALL_ZONES, 0, NULL,
6879 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
6880 	if (ire != NULL) {
6881 		ire_refrele(gw_ire);
6882 		ire_refrele(ire);
6883 		return (EEXIST);
6884 	}
6885 
6886 	/* Security attribute exists */
6887 	if (sp != NULL) {
6888 		tsol_gcgrp_addr_t ga;
6889 
6890 		/* find or create the gateway credentials group */
6891 		ga.ga_af = AF_INET;
6892 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6893 
6894 		/* we hold reference to it upon success */
6895 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6896 		if (gcgrp == NULL) {
6897 			ire_refrele(gw_ire);
6898 			return (ENOMEM);
6899 		}
6900 
6901 		/*
6902 		 * Create and add the security attribute to the group; a
6903 		 * reference to the group is made upon allocating a new
6904 		 * entry successfully.  If it finds an already-existing
6905 		 * entry for the security attribute in the group, it simply
6906 		 * returns it and no new reference is made to the group.
6907 		 */
6908 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6909 		if (gc == NULL) {
6910 			/* release reference held by gcgrp_lookup */
6911 			GCGRP_REFRELE(gcgrp);
6912 			ire_refrele(gw_ire);
6913 			return (ENOMEM);
6914 		}
6915 	}
6916 
6917 	/* Create the IRE. */
6918 	ire = ire_create(
6919 	    (uchar_t *)&dst_addr,		/* dest address */
6920 	    (uchar_t *)&mask,			/* mask */
6921 	    /* src address assigned by the caller? */
6922 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6923 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
6924 	    (uchar_t *)&gw_addr,		/* gateway address */
6925 	    &gw_ire->ire_max_frag,
6926 	    NULL,				/* no src nce */
6927 	    NULL,				/* no recv-from queue */
6928 	    NULL,				/* no send-to queue */
6929 	    (ushort_t)type,			/* IRE type */
6930 	    ipif_arg,
6931 	    0,
6932 	    0,
6933 	    0,
6934 	    flags,
6935 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6936 	    gc,					/* security attribute */
6937 	    NULL,
6938 	    ipst);
6939 
6940 	/*
6941 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6942 	 * reference to the 'gcgrp'. We can now release the extra reference
6943 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6944 	 */
6945 	if (gcgrp_xtraref)
6946 		GCGRP_REFRELE(gcgrp);
6947 	if (ire == NULL) {
6948 		if (gc != NULL)
6949 			GC_REFRELE(gc);
6950 		ire_refrele(gw_ire);
6951 		return (ENOMEM);
6952 	}
6953 
6954 	/*
6955 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6956 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6957 	 */
6958 
6959 	/* Add the new IRE. */
6960 	error = ire_add(&ire, q, mp, func, B_FALSE);
6961 	if (error != 0) {
6962 		/*
6963 		 * In the result of failure, ire_add() will have already
6964 		 * deleted the ire in question, so there is no need to
6965 		 * do that here.
6966 		 */
6967 		ire_refrele(gw_ire);
6968 		return (error);
6969 	}
6970 
6971 	if (flags & RTF_MULTIRT) {
6972 		/*
6973 		 * Invoke the CGTP (multirouting) filtering module
6974 		 * to add the dst address in the filtering database.
6975 		 * Replicated inbound packets coming from that address
6976 		 * will be filtered to discard the duplicates.
6977 		 * It is not necessary to call the CGTP filter hook
6978 		 * when the dst address is a broadcast or multicast,
6979 		 * because an IP source address cannot be a broadcast
6980 		 * or a multicast.
6981 		 */
6982 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6983 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
6984 		if (ire_dst != NULL) {
6985 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
6986 			ire_refrele(ire_dst);
6987 			goto save_ire;
6988 		}
6989 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
6990 		    !CLASSD(ire->ire_addr)) {
6991 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
6992 			    ipst->ips_netstack->netstack_stackid,
6993 			    ire->ire_addr,
6994 			    ire->ire_gateway_addr,
6995 			    ire->ire_src_addr,
6996 			    gw_ire->ire_src_addr);
6997 			if (res != 0) {
6998 				ire_refrele(gw_ire);
6999 				ire_delete(ire);
7000 				return (res);
7001 			}
7002 		}
7003 	}
7004 
7005 	/*
7006 	 * Now that the prefix IRE entry has been created, delete any
7007 	 * existing gateway IRE cache entries as well as any IRE caches
7008 	 * using the gateway, and force them to be created through
7009 	 * ip_newroute.
7010 	 */
7011 	if (gc != NULL) {
7012 		ASSERT(gcgrp != NULL);
7013 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7014 	}
7015 
7016 save_ire:
7017 	if (gw_ire != NULL) {
7018 		ire_refrele(gw_ire);
7019 	}
7020 	if (ipif != NULL) {
7021 		/*
7022 		 * Save enough information so that we can recreate the IRE if
7023 		 * the interface goes down and then up.  The metrics associated
7024 		 * with the route will be saved as well when rts_setmetrics() is
7025 		 * called after the IRE has been created.  In the case where
7026 		 * memory cannot be allocated, none of this information will be
7027 		 * saved.
7028 		 */
7029 		ipif_save_ire(ipif, ire);
7030 	}
7031 	if (ioctl_msg)
7032 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7033 	if (ire_arg != NULL) {
7034 		/*
7035 		 * Store the ire that was successfully added into where ire_arg
7036 		 * points to so that callers don't have to look it up
7037 		 * themselves (but they are responsible for ire_refrele()ing
7038 		 * the ire when they are finished with it).
7039 		 */
7040 		*ire_arg = ire;
7041 	} else {
7042 		ire_refrele(ire);		/* Held in ire_add */
7043 	}
7044 	if (ipif_refheld)
7045 		ipif_refrele(ipif);
7046 	return (0);
7047 }
7048 
7049 /*
7050  * ip_rt_delete is called to delete an IPv4 route.
7051  * ipif_arg is passed in to associate it with the correct interface.
7052  * We may need to restart this operation if the ipif cannot be looked up
7053  * due to an exclusive operation that is currently in progress. The restart
7054  * entry point is specified by 'func'
7055  */
7056 /* ARGSUSED4 */
7057 int
7058 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7059     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7060     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7061 {
7062 	ire_t	*ire = NULL;
7063 	ipif_t	*ipif;
7064 	boolean_t ipif_refheld = B_FALSE;
7065 	uint_t	type;
7066 	uint_t	match_flags = MATCH_IRE_TYPE;
7067 	int	err = 0;
7068 
7069 	ip1dbg(("ip_rt_delete:"));
7070 	/*
7071 	 * If this is the case of RTF_HOST being set, then we set the netmask
7072 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7073 	 */
7074 	if (flags & RTF_HOST) {
7075 		mask = IP_HOST_MASK;
7076 		match_flags |= MATCH_IRE_MASK;
7077 	} else if (rtm_addrs & RTA_NETMASK) {
7078 		match_flags |= MATCH_IRE_MASK;
7079 	}
7080 
7081 	/*
7082 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7083 	 * we check if the gateway address is one of our interfaces first,
7084 	 * and fall back on RTF_GATEWAY routes.
7085 	 *
7086 	 * This makes it possible to delete an original
7087 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7088 	 *
7089 	 * As the interface index specified with the RTA_IFP sockaddr is the
7090 	 * same for all ipif's off of an ill, the matching logic below uses
7091 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7092 	 * sharing the same prefix and interface index as the the route
7093 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7094 	 * is specified in the request.
7095 	 *
7096 	 * On the other hand, since the gateway address will usually be
7097 	 * different for each ipif on the system, the matching logic
7098 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7099 	 * route.  This means that interface routes for the same prefix can be
7100 	 * uniquely identified if they belong to distinct ipif's and if a
7101 	 * RTA_IFP sockaddr is not present.
7102 	 *
7103 	 * For more detail on specifying routes by gateway address and by
7104 	 * interface index, see the comments in ip_rt_add().
7105 	 */
7106 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7107 	    ipst);
7108 	if (ipif != NULL)
7109 		ipif_refheld = B_TRUE;
7110 	else if (err == EINPROGRESS)
7111 		return (err);
7112 	else
7113 		err = 0;
7114 	if (ipif != NULL) {
7115 		if (ipif_arg != NULL) {
7116 			if (ipif_refheld) {
7117 				ipif_refrele(ipif);
7118 				ipif_refheld = B_FALSE;
7119 			}
7120 			ipif = ipif_arg;
7121 			match_flags |= MATCH_IRE_ILL;
7122 		} else {
7123 			match_flags |= MATCH_IRE_IPIF;
7124 		}
7125 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7126 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7127 			    ALL_ZONES, NULL, match_flags, ipst);
7128 		}
7129 		if (ire == NULL) {
7130 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7131 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7132 			    match_flags, ipst);
7133 		}
7134 	}
7135 
7136 	if (ire == NULL) {
7137 		/*
7138 		 * At this point, the gateway address is not one of our own
7139 		 * addresses or a matching interface route was not found.  We
7140 		 * set the IRE type to lookup based on whether
7141 		 * this is a host route, a default route or just a prefix.
7142 		 *
7143 		 * If an ipif_arg was passed in, then the lookup is based on an
7144 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7145 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7146 		 * set as the route being looked up is not a traditional
7147 		 * interface route.
7148 		 */
7149 		match_flags &= ~MATCH_IRE_IPIF;
7150 		match_flags |= MATCH_IRE_GW;
7151 		if (ipif_arg != NULL)
7152 			match_flags |= MATCH_IRE_ILL;
7153 		if (mask == IP_HOST_MASK)
7154 			type = IRE_HOST;
7155 		else if (mask == 0)
7156 			type = IRE_DEFAULT;
7157 		else
7158 			type = IRE_PREFIX;
7159 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7160 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7161 	}
7162 
7163 	if (ipif_refheld)
7164 		ipif_refrele(ipif);
7165 
7166 	/* ipif is not refheld anymore */
7167 	if (ire == NULL)
7168 		return (ESRCH);
7169 
7170 	if (ire->ire_flags & RTF_MULTIRT) {
7171 		/*
7172 		 * Invoke the CGTP (multirouting) filtering module
7173 		 * to remove the dst address from the filtering database.
7174 		 * Packets coming from that address will no longer be
7175 		 * filtered to remove duplicates.
7176 		 */
7177 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7178 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7179 			    ipst->ips_netstack->netstack_stackid,
7180 			    ire->ire_addr, ire->ire_gateway_addr);
7181 		}
7182 		ip_cgtp_bcast_delete(ire, ipst);
7183 	}
7184 
7185 	ipif = ire->ire_ipif;
7186 	if (ipif != NULL)
7187 		ipif_remove_ire(ipif, ire);
7188 	if (ioctl_msg)
7189 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7190 	ire_delete(ire);
7191 	ire_refrele(ire);
7192 	return (err);
7193 }
7194 
7195 /*
7196  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7197  */
7198 /* ARGSUSED */
7199 int
7200 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7201     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7202 {
7203 	ipaddr_t dst_addr;
7204 	ipaddr_t gw_addr;
7205 	ipaddr_t mask;
7206 	int error = 0;
7207 	mblk_t *mp1;
7208 	struct rtentry *rt;
7209 	ipif_t *ipif = NULL;
7210 	ip_stack_t	*ipst;
7211 
7212 	ASSERT(q->q_next == NULL);
7213 	ipst = CONNQ_TO_IPST(q);
7214 
7215 	ip1dbg(("ip_siocaddrt:"));
7216 	/* Existence of mp1 verified in ip_wput_nondata */
7217 	mp1 = mp->b_cont->b_cont;
7218 	rt = (struct rtentry *)mp1->b_rptr;
7219 
7220 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7221 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7222 
7223 	/*
7224 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7225 	 * to a particular host address.  In this case, we set the netmask to
7226 	 * all ones for the particular destination address.  Otherwise,
7227 	 * determine the netmask to be used based on dst_addr and the interfaces
7228 	 * in use.
7229 	 */
7230 	if (rt->rt_flags & RTF_HOST) {
7231 		mask = IP_HOST_MASK;
7232 	} else {
7233 		/*
7234 		 * Note that ip_subnet_mask returns a zero mask in the case of
7235 		 * default (an all-zeroes address).
7236 		 */
7237 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7238 	}
7239 
7240 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7241 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7242 	if (ipif != NULL)
7243 		ipif_refrele(ipif);
7244 	return (error);
7245 }
7246 
7247 /*
7248  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7249  */
7250 /* ARGSUSED */
7251 int
7252 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7253     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7254 {
7255 	ipaddr_t dst_addr;
7256 	ipaddr_t gw_addr;
7257 	ipaddr_t mask;
7258 	int error;
7259 	mblk_t *mp1;
7260 	struct rtentry *rt;
7261 	ipif_t *ipif = NULL;
7262 	ip_stack_t	*ipst;
7263 
7264 	ASSERT(q->q_next == NULL);
7265 	ipst = CONNQ_TO_IPST(q);
7266 
7267 	ip1dbg(("ip_siocdelrt:"));
7268 	/* Existence of mp1 verified in ip_wput_nondata */
7269 	mp1 = mp->b_cont->b_cont;
7270 	rt = (struct rtentry *)mp1->b_rptr;
7271 
7272 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7273 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7274 
7275 	/*
7276 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7277 	 * to a particular host address.  In this case, we set the netmask to
7278 	 * all ones for the particular destination address.  Otherwise,
7279 	 * determine the netmask to be used based on dst_addr and the interfaces
7280 	 * in use.
7281 	 */
7282 	if (rt->rt_flags & RTF_HOST) {
7283 		mask = IP_HOST_MASK;
7284 	} else {
7285 		/*
7286 		 * Note that ip_subnet_mask returns a zero mask in the case of
7287 		 * default (an all-zeroes address).
7288 		 */
7289 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7290 	}
7291 
7292 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7293 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7294 	    mp, ip_process_ioctl, ipst);
7295 	if (ipif != NULL)
7296 		ipif_refrele(ipif);
7297 	return (error);
7298 }
7299 
7300 /*
7301  * Enqueue the mp onto the ipsq, chained by b_next.
7302  * b_prev stores the function to be executed later, and b_queue the queue
7303  * where this mp originated.
7304  */
7305 void
7306 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7307     ill_t *pending_ill)
7308 {
7309 	conn_t	*connp = NULL;
7310 
7311 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7312 	ASSERT(func != NULL);
7313 
7314 	mp->b_queue = q;
7315 	mp->b_prev = (void *)func;
7316 	mp->b_next = NULL;
7317 
7318 	switch (type) {
7319 	case CUR_OP:
7320 		if (ipsq->ipsq_mptail != NULL) {
7321 			ASSERT(ipsq->ipsq_mphead != NULL);
7322 			ipsq->ipsq_mptail->b_next = mp;
7323 		} else {
7324 			ASSERT(ipsq->ipsq_mphead == NULL);
7325 			ipsq->ipsq_mphead = mp;
7326 		}
7327 		ipsq->ipsq_mptail = mp;
7328 		break;
7329 
7330 	case NEW_OP:
7331 		if (ipsq->ipsq_xopq_mptail != NULL) {
7332 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7333 			ipsq->ipsq_xopq_mptail->b_next = mp;
7334 		} else {
7335 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7336 			ipsq->ipsq_xopq_mphead = mp;
7337 		}
7338 		ipsq->ipsq_xopq_mptail = mp;
7339 		break;
7340 	default:
7341 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7342 	}
7343 
7344 	if (CONN_Q(q) && pending_ill != NULL) {
7345 		connp = Q_TO_CONN(q);
7346 
7347 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7348 		connp->conn_oper_pending_ill = pending_ill;
7349 	}
7350 }
7351 
7352 /*
7353  * Return the mp at the head of the ipsq. After emptying the ipsq
7354  * look at the next ioctl, if this ioctl is complete. Otherwise
7355  * return, we will resume when we complete the current ioctl.
7356  * The current ioctl will wait till it gets a response from the
7357  * driver below.
7358  */
7359 static mblk_t *
7360 ipsq_dq(ipsq_t *ipsq)
7361 {
7362 	mblk_t	*mp;
7363 
7364 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7365 
7366 	mp = ipsq->ipsq_mphead;
7367 	if (mp != NULL) {
7368 		ipsq->ipsq_mphead = mp->b_next;
7369 		if (ipsq->ipsq_mphead == NULL)
7370 			ipsq->ipsq_mptail = NULL;
7371 		mp->b_next = NULL;
7372 		return (mp);
7373 	}
7374 	if (ipsq->ipsq_current_ipif != NULL)
7375 		return (NULL);
7376 	mp = ipsq->ipsq_xopq_mphead;
7377 	if (mp != NULL) {
7378 		ipsq->ipsq_xopq_mphead = mp->b_next;
7379 		if (ipsq->ipsq_xopq_mphead == NULL)
7380 			ipsq->ipsq_xopq_mptail = NULL;
7381 		mp->b_next = NULL;
7382 		return (mp);
7383 	}
7384 	return (NULL);
7385 }
7386 
7387 /*
7388  * Enter the ipsq corresponding to ill, by waiting synchronously till
7389  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7390  * will have to drain completely before ipsq_enter returns success.
7391  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7392  * and the ipsq_exit logic will start the next enqueued ioctl after
7393  * completion of the current ioctl. If 'force' is used, we don't wait
7394  * for the enqueued ioctls. This is needed when a conn_close wants to
7395  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7396  * of an ill can also use this option. But we dont' use it currently.
7397  */
7398 #define	ENTER_SQ_WAIT_TICKS 100
7399 boolean_t
7400 ipsq_enter(ill_t *ill, boolean_t force, int type)
7401 {
7402 	ipsq_t	*ipsq;
7403 	boolean_t waited_enough = B_FALSE;
7404 
7405 	/*
7406 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7407 	 * Since the <ill-ipsq> assocs could change while we wait for the
7408 	 * writer, it is easier to wait on a fixed global rather than try to
7409 	 * cv_wait on a changing ipsq.
7410 	 */
7411 	mutex_enter(&ill->ill_lock);
7412 	for (;;) {
7413 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7414 			mutex_exit(&ill->ill_lock);
7415 			return (B_FALSE);
7416 		}
7417 
7418 		ipsq = ill->ill_phyint->phyint_ipsq;
7419 		mutex_enter(&ipsq->ipsq_lock);
7420 		if (ipsq->ipsq_writer == NULL &&
7421 		    (type == CUR_OP || ipsq->ipsq_current_ipif == NULL ||
7422 		    waited_enough)) {
7423 			break;
7424 		} else if (ipsq->ipsq_writer != NULL) {
7425 			mutex_exit(&ipsq->ipsq_lock);
7426 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7427 		} else {
7428 			mutex_exit(&ipsq->ipsq_lock);
7429 			if (force) {
7430 				(void) cv_timedwait(&ill->ill_cv,
7431 				    &ill->ill_lock,
7432 				    lbolt + ENTER_SQ_WAIT_TICKS);
7433 				waited_enough = B_TRUE;
7434 				continue;
7435 			} else {
7436 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7437 			}
7438 		}
7439 	}
7440 
7441 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7442 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7443 	ipsq->ipsq_writer = curthread;
7444 	ipsq->ipsq_reentry_cnt++;
7445 #ifdef DEBUG
7446 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7447 #endif
7448 	mutex_exit(&ipsq->ipsq_lock);
7449 	mutex_exit(&ill->ill_lock);
7450 	return (B_TRUE);
7451 }
7452 
7453 boolean_t
7454 ill_perim_enter(ill_t *ill)
7455 {
7456 	return (ipsq_enter(ill, B_FALSE, CUR_OP));
7457 }
7458 
7459 void
7460 ill_perim_exit(ill_t *ill)
7461 {
7462 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
7463 }
7464 
7465 /*
7466  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7467  * certain critical operations like plumbing (i.e. most set ioctls),
7468  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7469  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7470  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7471  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7472  * threads executing in the ipsq. Responses from the driver pertain to the
7473  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7474  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7475  *
7476  * If a thread does not want to reenter the ipsq when it is already writer,
7477  * it must make sure that the specified reentry point to be called later
7478  * when the ipsq is empty, nor any code path starting from the specified reentry
7479  * point must never ever try to enter the ipsq again. Otherwise it can lead
7480  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7481  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7482  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7483  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7484  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7485  * ioctl if the current ioctl has completed. If the current ioctl is still
7486  * in progress it simply returns. The current ioctl could be waiting for
7487  * a response from another module (arp_ or the driver or could be waiting for
7488  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7489  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7490  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7491  * ipsq_current_ipif is clear which happens only on ioctl completion.
7492  */
7493 
7494 /*
7495  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7496  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7497  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7498  * completion.
7499  */
7500 ipsq_t *
7501 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7502     ipsq_func_t func, int type, boolean_t reentry_ok)
7503 {
7504 	ipsq_t	*ipsq;
7505 
7506 	/* Only 1 of ipif or ill can be specified */
7507 	ASSERT((ipif != NULL) ^ (ill != NULL));
7508 	if (ipif != NULL)
7509 		ill = ipif->ipif_ill;
7510 
7511 	/*
7512 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7513 	 * ipsq of an ill can't change when ill_lock is held.
7514 	 */
7515 	GRAB_CONN_LOCK(q);
7516 	mutex_enter(&ill->ill_lock);
7517 	ipsq = ill->ill_phyint->phyint_ipsq;
7518 	mutex_enter(&ipsq->ipsq_lock);
7519 
7520 	/*
7521 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7522 	 *    (Note: If the caller does not specify reentry_ok then neither
7523 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7524 	 *    again. Otherwise it can lead to an infinite loop
7525 	 * 2. Enter the ipsq if there is no current writer and this attempted
7526 	 *    entry is part of the current ioctl or operation
7527 	 * 3. Enter the ipsq if there is no current writer and this is a new
7528 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7529 	 *    empty and there is no ioctl (or operation) currently in progress
7530 	 */
7531 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7532 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7533 	    ipsq->ipsq_current_ipif == NULL))) ||
7534 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7535 		/* Success. */
7536 		ipsq->ipsq_reentry_cnt++;
7537 		ipsq->ipsq_writer = curthread;
7538 		mutex_exit(&ipsq->ipsq_lock);
7539 		mutex_exit(&ill->ill_lock);
7540 		RELEASE_CONN_LOCK(q);
7541 #ifdef DEBUG
7542 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7543 		    IPSQ_STACK_DEPTH);
7544 #endif
7545 		return (ipsq);
7546 	}
7547 
7548 	ipsq_enq(ipsq, q, mp, func, type, ill);
7549 
7550 	mutex_exit(&ipsq->ipsq_lock);
7551 	mutex_exit(&ill->ill_lock);
7552 	RELEASE_CONN_LOCK(q);
7553 	return (NULL);
7554 }
7555 
7556 /*
7557  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7558  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7559  * cannot be entered, the mp is queued for completion.
7560  */
7561 void
7562 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7563     boolean_t reentry_ok)
7564 {
7565 	ipsq_t	*ipsq;
7566 
7567 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7568 
7569 	/*
7570 	 * Drop the caller's refhold on the ill.  This is safe since we either
7571 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7572 	 * IPSQ, in which case we return without accessing ill anymore.  This
7573 	 * is needed because func needs to see the correct refcount.
7574 	 * e.g. removeif can work only then.
7575 	 */
7576 	ill_refrele(ill);
7577 	if (ipsq != NULL) {
7578 		(*func)(ipsq, q, mp, NULL);
7579 		ipsq_exit(ipsq);
7580 	}
7581 }
7582 
7583 /*
7584  * If there are more than ILL_GRP_CNT ills in a group,
7585  * we use kmem alloc'd buffers, else use the stack
7586  */
7587 #define	ILL_GRP_CNT	14
7588 /*
7589  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7590  * Called by a thread that is currently exclusive on this ipsq.
7591  */
7592 void
7593 ipsq_exit(ipsq_t *ipsq)
7594 {
7595 	queue_t	*q;
7596 	mblk_t	*mp;
7597 	ipsq_func_t	func;
7598 	int	next;
7599 	ill_t	**ill_list = NULL;
7600 	size_t	ill_list_size = 0;
7601 	int	cnt = 0;
7602 	boolean_t need_ipsq_free = B_FALSE;
7603 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7604 
7605 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7606 	mutex_enter(&ipsq->ipsq_lock);
7607 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7608 	if (ipsq->ipsq_reentry_cnt != 1) {
7609 		ipsq->ipsq_reentry_cnt--;
7610 		mutex_exit(&ipsq->ipsq_lock);
7611 		return;
7612 	}
7613 
7614 	mp = ipsq_dq(ipsq);
7615 	while (mp != NULL) {
7616 again:
7617 		mutex_exit(&ipsq->ipsq_lock);
7618 		func = (ipsq_func_t)mp->b_prev;
7619 		q = (queue_t *)mp->b_queue;
7620 		mp->b_prev = NULL;
7621 		mp->b_queue = NULL;
7622 
7623 		/*
7624 		 * If 'q' is an conn queue, it is valid, since we did a
7625 		 * a refhold on the connp, at the start of the ioctl.
7626 		 * If 'q' is an ill queue, it is valid, since close of an
7627 		 * ill will clean up the 'ipsq'.
7628 		 */
7629 		(*func)(ipsq, q, mp, NULL);
7630 
7631 		mutex_enter(&ipsq->ipsq_lock);
7632 		mp = ipsq_dq(ipsq);
7633 	}
7634 
7635 	mutex_exit(&ipsq->ipsq_lock);
7636 
7637 	/*
7638 	 * Need to grab the locks in the right order. Need to
7639 	 * atomically check (under ipsq_lock) that there are no
7640 	 * messages before relinquishing the ipsq. Also need to
7641 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7642 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7643 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7644 	 * to grab ill_g_lock as writer.
7645 	 */
7646 	rw_enter(&ipst->ips_ill_g_lock,
7647 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7648 
7649 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7650 	if (ipsq->ipsq_refs != 0) {
7651 		/* At most 2 ills v4/v6 per phyint */
7652 		cnt = ipsq->ipsq_refs << 1;
7653 		ill_list_size = cnt * sizeof (ill_t *);
7654 		/*
7655 		 * If memory allocation fails, we will do the split
7656 		 * the next time ipsq_exit is called for whatever reason.
7657 		 * As long as the ipsq_split flag is set the need to
7658 		 * split is remembered.
7659 		 */
7660 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7661 		if (ill_list != NULL)
7662 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7663 	}
7664 	mutex_enter(&ipsq->ipsq_lock);
7665 	mp = ipsq_dq(ipsq);
7666 	if (mp != NULL) {
7667 		/* oops, some message has landed up, we can't get out */
7668 		if (ill_list != NULL)
7669 			ill_unlock_ills(ill_list, cnt);
7670 		rw_exit(&ipst->ips_ill_g_lock);
7671 		if (ill_list != NULL)
7672 			kmem_free(ill_list, ill_list_size);
7673 		ill_list = NULL;
7674 		ill_list_size = 0;
7675 		cnt = 0;
7676 		goto again;
7677 	}
7678 
7679 	/*
7680 	 * Split only if no ioctl is pending and if memory alloc succeeded
7681 	 * above.
7682 	 */
7683 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7684 	    ill_list != NULL) {
7685 		/*
7686 		 * No new ill can join this ipsq since we are holding the
7687 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7688 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7689 		 * If so we will retry on the next ipsq_exit.
7690 		 */
7691 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7692 	}
7693 
7694 	/*
7695 	 * We are holding the ipsq lock, hence no new messages can
7696 	 * land up on the ipsq, and there are no messages currently.
7697 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7698 	 * atomically while holding ill locks.
7699 	 */
7700 	ipsq->ipsq_writer = NULL;
7701 	ipsq->ipsq_reentry_cnt--;
7702 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7703 #ifdef DEBUG
7704 	ipsq->ipsq_depth = 0;
7705 #endif
7706 	mutex_exit(&ipsq->ipsq_lock);
7707 	/*
7708 	 * For IPMP this should wake up all ills in this ipsq.
7709 	 * We need to hold the ill_lock while waking up waiters to
7710 	 * avoid missed wakeups. But there is no need to acquire all
7711 	 * the ill locks and then wakeup. If we have not acquired all
7712 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7713 	 * wakes up ills one at a time after getting the right ill_lock
7714 	 */
7715 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7716 	if (ill_list != NULL)
7717 		ill_unlock_ills(ill_list, cnt);
7718 	if (ipsq->ipsq_refs == 0)
7719 		need_ipsq_free = B_TRUE;
7720 	rw_exit(&ipst->ips_ill_g_lock);
7721 	if (ill_list != 0)
7722 		kmem_free(ill_list, ill_list_size);
7723 
7724 	if (need_ipsq_free) {
7725 		/*
7726 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7727 		 * looked up. ipsq can be looked up only thru ill or phyint
7728 		 * and there are no ills/phyint on this ipsq.
7729 		 */
7730 		ipsq_delete(ipsq);
7731 	}
7732 
7733 	/*
7734 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7735 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7736 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7737 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7738 	 * (executing the timeout, waiting to get inside the IPSQ).
7739 	 *
7740 	 * However, there is one exception to the above: if this thread *is*
7741 	 * the IGMP/MLD timeout handler thread, then we must not start its
7742 	 * timer until the current handler is done.
7743 	 */
7744 	mutex_enter(&ipst->ips_igmp_timer_lock);
7745 	if (curthread != ipst->ips_igmp_timer_thread) {
7746 		next = ipst->ips_igmp_deferred_next;
7747 		ipst->ips_igmp_deferred_next = INFINITY;
7748 		mutex_exit(&ipst->ips_igmp_timer_lock);
7749 
7750 		if (next != INFINITY)
7751 			igmp_start_timers(next, ipst);
7752 	} else {
7753 		mutex_exit(&ipst->ips_igmp_timer_lock);
7754 	}
7755 
7756 	mutex_enter(&ipst->ips_mld_timer_lock);
7757 	if (curthread != ipst->ips_mld_timer_thread) {
7758 		next = ipst->ips_mld_deferred_next;
7759 		ipst->ips_mld_deferred_next = INFINITY;
7760 		mutex_exit(&ipst->ips_mld_timer_lock);
7761 
7762 		if (next != INFINITY)
7763 			mld_start_timers(next, ipst);
7764 	} else {
7765 		mutex_exit(&ipst->ips_mld_timer_lock);
7766 	}
7767 }
7768 
7769 /*
7770  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7771  * and `ioccmd'.
7772  */
7773 void
7774 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7775 {
7776 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7777 
7778 	mutex_enter(&ipsq->ipsq_lock);
7779 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7780 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7781 	ipsq->ipsq_current_done = B_FALSE;
7782 	ipsq->ipsq_current_ipif = ipif;
7783 	ipsq->ipsq_current_ioctl = ioccmd;
7784 	mutex_exit(&ipsq->ipsq_lock);
7785 }
7786 
7787 /*
7788  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
7789  * the next exclusive operation to begin once we ipsq_exit().  However, if
7790  * pending DLPI operations remain, then we will wait for the queue to drain
7791  * before allowing the next exclusive operation to begin.  This ensures that
7792  * DLPI operations from one exclusive operation are never improperly processed
7793  * as part of a subsequent exclusive operation.
7794  */
7795 void
7796 ipsq_current_finish(ipsq_t *ipsq)
7797 {
7798 	ipif_t *ipif = ipsq->ipsq_current_ipif;
7799 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
7800 
7801 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7802 
7803 	/*
7804 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
7805 	 * (but in that case, IPIF_CHANGING will already be clear and no
7806 	 * pending DLPI messages can remain).
7807 	 */
7808 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
7809 		ill_t *ill = ipif->ipif_ill;
7810 
7811 		mutex_enter(&ill->ill_lock);
7812 		dlpi_pending = ill->ill_dlpi_pending;
7813 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
7814 		mutex_exit(&ill->ill_lock);
7815 	}
7816 
7817 	mutex_enter(&ipsq->ipsq_lock);
7818 	ipsq->ipsq_current_ioctl = 0;
7819 	ipsq->ipsq_current_done = B_TRUE;
7820 	if (dlpi_pending == DL_PRIM_INVAL)
7821 		ipsq->ipsq_current_ipif = NULL;
7822 	mutex_exit(&ipsq->ipsq_lock);
7823 }
7824 
7825 /*
7826  * The ill is closing. Flush all messages on the ipsq that originated
7827  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7828  * for this ill since ipsq_enter could not have entered until then.
7829  * New messages can't be queued since the CONDEMNED flag is set.
7830  */
7831 static void
7832 ipsq_flush(ill_t *ill)
7833 {
7834 	queue_t	*q;
7835 	mblk_t	*prev;
7836 	mblk_t	*mp;
7837 	mblk_t	*mp_next;
7838 	ipsq_t	*ipsq;
7839 
7840 	ASSERT(IAM_WRITER_ILL(ill));
7841 	ipsq = ill->ill_phyint->phyint_ipsq;
7842 	/*
7843 	 * Flush any messages sent up by the driver.
7844 	 */
7845 	mutex_enter(&ipsq->ipsq_lock);
7846 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7847 		mp_next = mp->b_next;
7848 		q = mp->b_queue;
7849 		if (q == ill->ill_rq || q == ill->ill_wq) {
7850 			/* Remove the mp from the ipsq */
7851 			if (prev == NULL)
7852 				ipsq->ipsq_mphead = mp->b_next;
7853 			else
7854 				prev->b_next = mp->b_next;
7855 			if (ipsq->ipsq_mptail == mp) {
7856 				ASSERT(mp_next == NULL);
7857 				ipsq->ipsq_mptail = prev;
7858 			}
7859 			inet_freemsg(mp);
7860 		} else {
7861 			prev = mp;
7862 		}
7863 	}
7864 	mutex_exit(&ipsq->ipsq_lock);
7865 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7866 	ipsq_xopq_mp_cleanup(ill, NULL);
7867 	ill_pending_mp_cleanup(ill);
7868 }
7869 
7870 /* ARGSUSED */
7871 int
7872 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7873     ip_ioctl_cmd_t *ipip, void *ifreq)
7874 {
7875 	ill_t	*ill;
7876 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7877 	boolean_t isv6;
7878 	conn_t	*connp;
7879 	ip_stack_t	*ipst;
7880 
7881 	connp = Q_TO_CONN(q);
7882 	ipst = connp->conn_netstack->netstack_ip;
7883 	isv6 = connp->conn_af_isv6;
7884 	/*
7885 	 * Set original index.
7886 	 * Failover and failback move logical interfaces
7887 	 * from one physical interface to another.  The
7888 	 * original index indicates the parent of a logical
7889 	 * interface, in other words, the physical interface
7890 	 * the logical interface will be moved back to on
7891 	 * failback.
7892 	 */
7893 
7894 	/*
7895 	 * Don't allow the original index to be changed
7896 	 * for non-failover addresses, autoconfigured
7897 	 * addresses, or IPv6 link local addresses.
7898 	 */
7899 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7900 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7901 		return (EINVAL);
7902 	}
7903 	/*
7904 	 * The new original index must be in use by some
7905 	 * physical interface.
7906 	 */
7907 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7908 	    NULL, NULL, ipst);
7909 	if (ill == NULL)
7910 		return (ENXIO);
7911 	ill_refrele(ill);
7912 
7913 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7914 	/*
7915 	 * When this ipif gets failed back, don't
7916 	 * preserve the original id, as it is no
7917 	 * longer applicable.
7918 	 */
7919 	ipif->ipif_orig_ipifid = 0;
7920 	/*
7921 	 * For IPv4, change the original index of any
7922 	 * multicast addresses associated with the
7923 	 * ipif to the new value.
7924 	 */
7925 	if (!isv6) {
7926 		ilm_t *ilm;
7927 
7928 		mutex_enter(&ipif->ipif_ill->ill_lock);
7929 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7930 		    ilm = ilm->ilm_next) {
7931 			if (ilm->ilm_ipif == ipif) {
7932 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7933 			}
7934 		}
7935 		mutex_exit(&ipif->ipif_ill->ill_lock);
7936 	}
7937 	return (0);
7938 }
7939 
7940 /* ARGSUSED */
7941 int
7942 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7943     ip_ioctl_cmd_t *ipip, void *ifreq)
7944 {
7945 	struct lifreq *lifr = (struct lifreq *)ifreq;
7946 
7947 	/*
7948 	 * Get the original interface index i.e the one
7949 	 * before FAILOVER if it ever happened.
7950 	 */
7951 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7952 	return (0);
7953 }
7954 
7955 /*
7956  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7957  * refhold and return the associated ipif
7958  */
7959 /* ARGSUSED */
7960 int
7961 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7962     cmd_info_t *ci, ipsq_func_t func)
7963 {
7964 	boolean_t exists;
7965 	struct iftun_req *ta;
7966 	ipif_t  *ipif;
7967 	ill_t   *ill;
7968 	boolean_t isv6;
7969 	mblk_t  *mp1;
7970 	int error;
7971 	conn_t  *connp;
7972 	ip_stack_t  *ipst;
7973 
7974 	/* Existence verified in ip_wput_nondata */
7975 	mp1 = mp->b_cont->b_cont;
7976 	ta = (struct iftun_req *)mp1->b_rptr;
7977 	/*
7978 	 * Null terminate the string to protect against buffer
7979 	 * overrun. String was generated by user code and may not
7980 	 * be trusted.
7981 	 */
7982 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7983 
7984 	connp = Q_TO_CONN(q);
7985 	isv6 = connp->conn_af_isv6;
7986 	ipst = connp->conn_netstack->netstack_ip;
7987 
7988 	/* Disallows implicit create */
7989 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7990 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7991 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
7992 	if (ipif == NULL)
7993 		return (error);
7994 
7995 	if (ipif->ipif_id != 0) {
7996 		/*
7997 		 * We really don't want to set/get tunnel parameters
7998 		 * on virtual tunnel interfaces.  Only allow the
7999 		 * base tunnel to do these.
8000 		 */
8001 		ipif_refrele(ipif);
8002 		return (EINVAL);
8003 	}
8004 
8005 	/*
8006 	 * Send down to tunnel mod for ioctl processing.
8007 	 * Will finish ioctl in ip_rput_other().
8008 	 */
8009 	ill = ipif->ipif_ill;
8010 	if (ill->ill_net_type == IRE_LOOPBACK) {
8011 		ipif_refrele(ipif);
8012 		return (EOPNOTSUPP);
8013 	}
8014 
8015 	if (ill->ill_wq == NULL) {
8016 		ipif_refrele(ipif);
8017 		return (ENXIO);
8018 	}
8019 	/*
8020 	 * Mark the ioctl as coming from an IPv6 interface for
8021 	 * tun's convenience.
8022 	 */
8023 	if (ill->ill_isv6)
8024 		ta->ifta_flags |= 0x80000000;
8025 	ci->ci_ipif = ipif;
8026 	return (0);
8027 }
8028 
8029 /*
8030  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8031  * and return the associated ipif.
8032  * Return value:
8033  *	Non zero: An error has occurred. ci may not be filled out.
8034  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8035  *	a held ipif in ci.ci_ipif.
8036  */
8037 int
8038 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8039     cmd_info_t *ci, ipsq_func_t func)
8040 {
8041 	sin_t		*sin;
8042 	sin6_t		*sin6;
8043 	char		*name;
8044 	struct ifreq    *ifr;
8045 	struct lifreq    *lifr;
8046 	ipif_t		*ipif = NULL;
8047 	ill_t		*ill;
8048 	conn_t		*connp;
8049 	boolean_t	isv6;
8050 	boolean_t	exists;
8051 	int		err;
8052 	mblk_t		*mp1;
8053 	zoneid_t	zoneid;
8054 	ip_stack_t	*ipst;
8055 
8056 	if (q->q_next != NULL) {
8057 		ill = (ill_t *)q->q_ptr;
8058 		isv6 = ill->ill_isv6;
8059 		connp = NULL;
8060 		zoneid = ALL_ZONES;
8061 		ipst = ill->ill_ipst;
8062 	} else {
8063 		ill = NULL;
8064 		connp = Q_TO_CONN(q);
8065 		isv6 = connp->conn_af_isv6;
8066 		zoneid = connp->conn_zoneid;
8067 		if (zoneid == GLOBAL_ZONEID) {
8068 			/* global zone can access ipifs in all zones */
8069 			zoneid = ALL_ZONES;
8070 		}
8071 		ipst = connp->conn_netstack->netstack_ip;
8072 	}
8073 
8074 	/* Has been checked in ip_wput_nondata */
8075 	mp1 = mp->b_cont->b_cont;
8076 
8077 	if (ipip->ipi_cmd_type == IF_CMD) {
8078 		/* This a old style SIOC[GS]IF* command */
8079 		ifr = (struct ifreq *)mp1->b_rptr;
8080 		/*
8081 		 * Null terminate the string to protect against buffer
8082 		 * overrun. String was generated by user code and may not
8083 		 * be trusted.
8084 		 */
8085 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8086 		sin = (sin_t *)&ifr->ifr_addr;
8087 		name = ifr->ifr_name;
8088 		ci->ci_sin = sin;
8089 		ci->ci_sin6 = NULL;
8090 		ci->ci_lifr = (struct lifreq *)ifr;
8091 	} else {
8092 		/* This a new style SIOC[GS]LIF* command */
8093 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8094 		lifr = (struct lifreq *)mp1->b_rptr;
8095 		/*
8096 		 * Null terminate the string to protect against buffer
8097 		 * overrun. String was generated by user code and may not
8098 		 * be trusted.
8099 		 */
8100 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8101 		name = lifr->lifr_name;
8102 		sin = (sin_t *)&lifr->lifr_addr;
8103 		sin6 = (sin6_t *)&lifr->lifr_addr;
8104 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8105 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8106 			    LIFNAMSIZ);
8107 		}
8108 		ci->ci_sin = sin;
8109 		ci->ci_sin6 = sin6;
8110 		ci->ci_lifr = lifr;
8111 	}
8112 
8113 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8114 		/*
8115 		 * The ioctl will be failed if the ioctl comes down
8116 		 * an conn stream
8117 		 */
8118 		if (ill == NULL) {
8119 			/*
8120 			 * Not an ill queue, return EINVAL same as the
8121 			 * old error code.
8122 			 */
8123 			return (ENXIO);
8124 		}
8125 		ipif = ill->ill_ipif;
8126 		ipif_refhold(ipif);
8127 	} else {
8128 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8129 		    &exists, isv6, zoneid,
8130 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8131 		    ipst);
8132 		if (ipif == NULL) {
8133 			if (err == EINPROGRESS)
8134 				return (err);
8135 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8136 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8137 				/*
8138 				 * Need to try both v4 and v6 since this
8139 				 * ioctl can come down either v4 or v6
8140 				 * socket. The lifreq.lifr_family passed
8141 				 * down by this ioctl is AF_UNSPEC.
8142 				 */
8143 				ipif = ipif_lookup_on_name(name,
8144 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8145 				    zoneid, (connp == NULL) ? q :
8146 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8147 				if (err == EINPROGRESS)
8148 					return (err);
8149 			}
8150 			err = 0;	/* Ensure we don't use it below */
8151 		}
8152 	}
8153 
8154 	/*
8155 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8156 	 */
8157 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8158 		ipif_refrele(ipif);
8159 		return (ENXIO);
8160 	}
8161 
8162 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8163 	    name[0] == '\0') {
8164 		/*
8165 		 * Handle a or a SIOC?IF* with a null name
8166 		 * during plumb (on the ill queue before the I_PLINK).
8167 		 */
8168 		ipif = ill->ill_ipif;
8169 		ipif_refhold(ipif);
8170 	}
8171 
8172 	if (ipif == NULL)
8173 		return (ENXIO);
8174 
8175 	/*
8176 	 * Allow only GET operations if this ipif has been created
8177 	 * temporarily due to a MOVE operation.
8178 	 */
8179 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8180 		ipif_refrele(ipif);
8181 		return (EINVAL);
8182 	}
8183 
8184 	ci->ci_ipif = ipif;
8185 	return (0);
8186 }
8187 
8188 /*
8189  * Return the total number of ipifs.
8190  */
8191 static uint_t
8192 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8193 {
8194 	uint_t numifs = 0;
8195 	ill_t	*ill;
8196 	ill_walk_context_t	ctx;
8197 	ipif_t	*ipif;
8198 
8199 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8200 	ill = ILL_START_WALK_V4(&ctx, ipst);
8201 
8202 	while (ill != NULL) {
8203 		for (ipif = ill->ill_ipif; ipif != NULL;
8204 		    ipif = ipif->ipif_next) {
8205 			if (ipif->ipif_zoneid == zoneid ||
8206 			    ipif->ipif_zoneid == ALL_ZONES)
8207 				numifs++;
8208 		}
8209 		ill = ill_next(&ctx, ill);
8210 	}
8211 	rw_exit(&ipst->ips_ill_g_lock);
8212 	return (numifs);
8213 }
8214 
8215 /*
8216  * Return the total number of ipifs.
8217  */
8218 static uint_t
8219 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8220 {
8221 	uint_t numifs = 0;
8222 	ill_t	*ill;
8223 	ipif_t	*ipif;
8224 	ill_walk_context_t	ctx;
8225 
8226 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8227 
8228 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8229 	if (family == AF_INET)
8230 		ill = ILL_START_WALK_V4(&ctx, ipst);
8231 	else if (family == AF_INET6)
8232 		ill = ILL_START_WALK_V6(&ctx, ipst);
8233 	else
8234 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8235 
8236 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8237 		for (ipif = ill->ill_ipif; ipif != NULL;
8238 		    ipif = ipif->ipif_next) {
8239 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8240 			    !(lifn_flags & LIFC_NOXMIT))
8241 				continue;
8242 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8243 			    !(lifn_flags & LIFC_TEMPORARY))
8244 				continue;
8245 			if (((ipif->ipif_flags &
8246 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8247 			    IPIF_DEPRECATED)) ||
8248 			    IS_LOOPBACK(ill) ||
8249 			    !(ipif->ipif_flags & IPIF_UP)) &&
8250 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8251 				continue;
8252 
8253 			if (zoneid != ipif->ipif_zoneid &&
8254 			    ipif->ipif_zoneid != ALL_ZONES &&
8255 			    (zoneid != GLOBAL_ZONEID ||
8256 			    !(lifn_flags & LIFC_ALLZONES)))
8257 				continue;
8258 
8259 			numifs++;
8260 		}
8261 	}
8262 	rw_exit(&ipst->ips_ill_g_lock);
8263 	return (numifs);
8264 }
8265 
8266 uint_t
8267 ip_get_lifsrcofnum(ill_t *ill)
8268 {
8269 	uint_t numifs = 0;
8270 	ill_t	*ill_head = ill;
8271 	ip_stack_t	*ipst = ill->ill_ipst;
8272 
8273 	/*
8274 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8275 	 * other thread may be trying to relink the ILLs in this usesrc group
8276 	 * and adjusting the ill_usesrc_grp_next pointers
8277 	 */
8278 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8279 	if ((ill->ill_usesrc_ifindex == 0) &&
8280 	    (ill->ill_usesrc_grp_next != NULL)) {
8281 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8282 		    ill = ill->ill_usesrc_grp_next)
8283 			numifs++;
8284 	}
8285 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8286 
8287 	return (numifs);
8288 }
8289 
8290 /* Null values are passed in for ipif, sin, and ifreq */
8291 /* ARGSUSED */
8292 int
8293 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8294     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8295 {
8296 	int *nump;
8297 	conn_t *connp = Q_TO_CONN(q);
8298 
8299 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8300 
8301 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8302 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8303 
8304 	*nump = ip_get_numifs(connp->conn_zoneid,
8305 	    connp->conn_netstack->netstack_ip);
8306 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8307 	return (0);
8308 }
8309 
8310 /* Null values are passed in for ipif, sin, and ifreq */
8311 /* ARGSUSED */
8312 int
8313 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8314     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8315 {
8316 	struct lifnum *lifn;
8317 	mblk_t	*mp1;
8318 	conn_t *connp = Q_TO_CONN(q);
8319 
8320 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8321 
8322 	/* Existence checked in ip_wput_nondata */
8323 	mp1 = mp->b_cont->b_cont;
8324 
8325 	lifn = (struct lifnum *)mp1->b_rptr;
8326 	switch (lifn->lifn_family) {
8327 	case AF_UNSPEC:
8328 	case AF_INET:
8329 	case AF_INET6:
8330 		break;
8331 	default:
8332 		return (EAFNOSUPPORT);
8333 	}
8334 
8335 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8336 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8337 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8338 	return (0);
8339 }
8340 
8341 /* ARGSUSED */
8342 int
8343 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8344     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8345 {
8346 	STRUCT_HANDLE(ifconf, ifc);
8347 	mblk_t *mp1;
8348 	struct iocblk *iocp;
8349 	struct ifreq *ifr;
8350 	ill_walk_context_t	ctx;
8351 	ill_t	*ill;
8352 	ipif_t	*ipif;
8353 	struct sockaddr_in *sin;
8354 	int32_t	ifclen;
8355 	zoneid_t zoneid;
8356 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8357 
8358 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8359 
8360 	ip1dbg(("ip_sioctl_get_ifconf"));
8361 	/* Existence verified in ip_wput_nondata */
8362 	mp1 = mp->b_cont->b_cont;
8363 	iocp = (struct iocblk *)mp->b_rptr;
8364 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8365 
8366 	/*
8367 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8368 	 * the user buffer address and length into which the list of struct
8369 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8370 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8371 	 * the SIOCGIFCONF operation was redefined to simply provide
8372 	 * a large output buffer into which we are supposed to jam the ifreq
8373 	 * array.  The same ioctl command code was used, despite the fact that
8374 	 * both the applications and the kernel code had to change, thus making
8375 	 * it impossible to support both interfaces.
8376 	 *
8377 	 * For reasons not good enough to try to explain, the following
8378 	 * algorithm is used for deciding what to do with one of these:
8379 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8380 	 * form with the output buffer coming down as the continuation message.
8381 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8382 	 * and we have to copy in the ifconf structure to find out how big the
8383 	 * output buffer is and where to copy out to.  Sure no problem...
8384 	 *
8385 	 */
8386 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8387 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8388 		int numifs = 0;
8389 		size_t ifc_bufsize;
8390 
8391 		/*
8392 		 * Must be (better be!) continuation of a TRANSPARENT
8393 		 * IOCTL.  We just copied in the ifconf structure.
8394 		 */
8395 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8396 		    (struct ifconf *)mp1->b_rptr);
8397 
8398 		/*
8399 		 * Allocate a buffer to hold requested information.
8400 		 *
8401 		 * If ifc_len is larger than what is needed, we only
8402 		 * allocate what we will use.
8403 		 *
8404 		 * If ifc_len is smaller than what is needed, return
8405 		 * EINVAL.
8406 		 *
8407 		 * XXX: the ill_t structure can hava 2 counters, for
8408 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8409 		 * number of interfaces for a device, so we don't need
8410 		 * to count them here...
8411 		 */
8412 		numifs = ip_get_numifs(zoneid, ipst);
8413 
8414 		ifclen = STRUCT_FGET(ifc, ifc_len);
8415 		ifc_bufsize = numifs * sizeof (struct ifreq);
8416 		if (ifc_bufsize > ifclen) {
8417 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8418 				/* old behaviour */
8419 				return (EINVAL);
8420 			} else {
8421 				ifc_bufsize = ifclen;
8422 			}
8423 		}
8424 
8425 		mp1 = mi_copyout_alloc(q, mp,
8426 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8427 		if (mp1 == NULL)
8428 			return (ENOMEM);
8429 
8430 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8431 	}
8432 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8433 	/*
8434 	 * the SIOCGIFCONF ioctl only knows about
8435 	 * IPv4 addresses, so don't try to tell
8436 	 * it about interfaces with IPv6-only
8437 	 * addresses. (Last parm 'isv6' is B_FALSE)
8438 	 */
8439 
8440 	ifr = (struct ifreq *)mp1->b_rptr;
8441 
8442 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8443 	ill = ILL_START_WALK_V4(&ctx, ipst);
8444 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8445 		for (ipif = ill->ill_ipif; ipif != NULL;
8446 		    ipif = ipif->ipif_next) {
8447 			if (zoneid != ipif->ipif_zoneid &&
8448 			    ipif->ipif_zoneid != ALL_ZONES)
8449 				continue;
8450 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8451 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8452 					/* old behaviour */
8453 					rw_exit(&ipst->ips_ill_g_lock);
8454 					return (EINVAL);
8455 				} else {
8456 					goto if_copydone;
8457 				}
8458 			}
8459 			ipif_get_name(ipif, ifr->ifr_name,
8460 			    sizeof (ifr->ifr_name));
8461 			sin = (sin_t *)&ifr->ifr_addr;
8462 			*sin = sin_null;
8463 			sin->sin_family = AF_INET;
8464 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8465 			ifr++;
8466 		}
8467 	}
8468 if_copydone:
8469 	rw_exit(&ipst->ips_ill_g_lock);
8470 	mp1->b_wptr = (uchar_t *)ifr;
8471 
8472 	if (STRUCT_BUF(ifc) != NULL) {
8473 		STRUCT_FSET(ifc, ifc_len,
8474 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8475 	}
8476 	return (0);
8477 }
8478 
8479 /*
8480  * Get the interfaces using the address hosted on the interface passed in,
8481  * as a source adddress
8482  */
8483 /* ARGSUSED */
8484 int
8485 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8486     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8487 {
8488 	mblk_t *mp1;
8489 	ill_t	*ill, *ill_head;
8490 	ipif_t	*ipif, *orig_ipif;
8491 	int	numlifs = 0;
8492 	size_t	lifs_bufsize, lifsmaxlen;
8493 	struct	lifreq *lifr;
8494 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8495 	uint_t	ifindex;
8496 	zoneid_t zoneid;
8497 	int err = 0;
8498 	boolean_t isv6 = B_FALSE;
8499 	struct	sockaddr_in	*sin;
8500 	struct	sockaddr_in6	*sin6;
8501 	STRUCT_HANDLE(lifsrcof, lifs);
8502 	ip_stack_t		*ipst;
8503 
8504 	ipst = CONNQ_TO_IPST(q);
8505 
8506 	ASSERT(q->q_next == NULL);
8507 
8508 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8509 
8510 	/* Existence verified in ip_wput_nondata */
8511 	mp1 = mp->b_cont->b_cont;
8512 
8513 	/*
8514 	 * Must be (better be!) continuation of a TRANSPARENT
8515 	 * IOCTL.  We just copied in the lifsrcof structure.
8516 	 */
8517 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8518 	    (struct lifsrcof *)mp1->b_rptr);
8519 
8520 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8521 		return (EINVAL);
8522 
8523 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8524 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8525 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8526 	    ip_process_ioctl, &err, ipst);
8527 	if (ipif == NULL) {
8528 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8529 		    ifindex));
8530 		return (err);
8531 	}
8532 
8533 	/* Allocate a buffer to hold requested information */
8534 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8535 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8536 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8537 	/* The actual size needed is always returned in lifs_len */
8538 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8539 
8540 	/* If the amount we need is more than what is passed in, abort */
8541 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8542 		ipif_refrele(ipif);
8543 		return (0);
8544 	}
8545 
8546 	mp1 = mi_copyout_alloc(q, mp,
8547 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8548 	if (mp1 == NULL) {
8549 		ipif_refrele(ipif);
8550 		return (ENOMEM);
8551 	}
8552 
8553 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8554 	bzero(mp1->b_rptr, lifs_bufsize);
8555 
8556 	lifr = (struct lifreq *)mp1->b_rptr;
8557 
8558 	ill = ill_head = ipif->ipif_ill;
8559 	orig_ipif = ipif;
8560 
8561 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8562 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8563 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8564 
8565 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8566 	for (; (ill != NULL) && (ill != ill_head);
8567 	    ill = ill->ill_usesrc_grp_next) {
8568 
8569 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8570 			break;
8571 
8572 		ipif = ill->ill_ipif;
8573 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8574 		if (ipif->ipif_isv6) {
8575 			sin6 = (sin6_t *)&lifr->lifr_addr;
8576 			*sin6 = sin6_null;
8577 			sin6->sin6_family = AF_INET6;
8578 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8579 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8580 			    &ipif->ipif_v6net_mask);
8581 		} else {
8582 			sin = (sin_t *)&lifr->lifr_addr;
8583 			*sin = sin_null;
8584 			sin->sin_family = AF_INET;
8585 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8586 			lifr->lifr_addrlen = ip_mask_to_plen(
8587 			    ipif->ipif_net_mask);
8588 		}
8589 		lifr++;
8590 	}
8591 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8592 	rw_exit(&ipst->ips_ill_g_lock);
8593 	ipif_refrele(orig_ipif);
8594 	mp1->b_wptr = (uchar_t *)lifr;
8595 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8596 
8597 	return (0);
8598 }
8599 
8600 /* ARGSUSED */
8601 int
8602 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8603     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8604 {
8605 	mblk_t *mp1;
8606 	int	list;
8607 	ill_t	*ill;
8608 	ipif_t	*ipif;
8609 	int	flags;
8610 	int	numlifs = 0;
8611 	size_t	lifc_bufsize;
8612 	struct	lifreq *lifr;
8613 	sa_family_t	family;
8614 	struct	sockaddr_in	*sin;
8615 	struct	sockaddr_in6	*sin6;
8616 	ill_walk_context_t	ctx;
8617 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8618 	int32_t	lifclen;
8619 	zoneid_t zoneid;
8620 	STRUCT_HANDLE(lifconf, lifc);
8621 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8622 
8623 	ip1dbg(("ip_sioctl_get_lifconf"));
8624 
8625 	ASSERT(q->q_next == NULL);
8626 
8627 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8628 
8629 	/* Existence verified in ip_wput_nondata */
8630 	mp1 = mp->b_cont->b_cont;
8631 
8632 	/*
8633 	 * An extended version of SIOCGIFCONF that takes an
8634 	 * additional address family and flags field.
8635 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8636 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8637 	 * interfaces are omitted.
8638 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8639 	 * unless LIFC_TEMPORARY is specified.
8640 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8641 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8642 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8643 	 * has priority over LIFC_NOXMIT.
8644 	 */
8645 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8646 
8647 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8648 		return (EINVAL);
8649 
8650 	/*
8651 	 * Must be (better be!) continuation of a TRANSPARENT
8652 	 * IOCTL.  We just copied in the lifconf structure.
8653 	 */
8654 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8655 
8656 	family = STRUCT_FGET(lifc, lifc_family);
8657 	flags = STRUCT_FGET(lifc, lifc_flags);
8658 
8659 	switch (family) {
8660 	case AF_UNSPEC:
8661 		/*
8662 		 * walk all ILL's.
8663 		 */
8664 		list = MAX_G_HEADS;
8665 		break;
8666 	case AF_INET:
8667 		/*
8668 		 * walk only IPV4 ILL's.
8669 		 */
8670 		list = IP_V4_G_HEAD;
8671 		break;
8672 	case AF_INET6:
8673 		/*
8674 		 * walk only IPV6 ILL's.
8675 		 */
8676 		list = IP_V6_G_HEAD;
8677 		break;
8678 	default:
8679 		return (EAFNOSUPPORT);
8680 	}
8681 
8682 	/*
8683 	 * Allocate a buffer to hold requested information.
8684 	 *
8685 	 * If lifc_len is larger than what is needed, we only
8686 	 * allocate what we will use.
8687 	 *
8688 	 * If lifc_len is smaller than what is needed, return
8689 	 * EINVAL.
8690 	 */
8691 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8692 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8693 	lifclen = STRUCT_FGET(lifc, lifc_len);
8694 	if (lifc_bufsize > lifclen) {
8695 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8696 			return (EINVAL);
8697 		else
8698 			lifc_bufsize = lifclen;
8699 	}
8700 
8701 	mp1 = mi_copyout_alloc(q, mp,
8702 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8703 	if (mp1 == NULL)
8704 		return (ENOMEM);
8705 
8706 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8707 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8708 
8709 	lifr = (struct lifreq *)mp1->b_rptr;
8710 
8711 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8712 	ill = ill_first(list, list, &ctx, ipst);
8713 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8714 		for (ipif = ill->ill_ipif; ipif != NULL;
8715 		    ipif = ipif->ipif_next) {
8716 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8717 			    !(flags & LIFC_NOXMIT))
8718 				continue;
8719 
8720 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8721 			    !(flags & LIFC_TEMPORARY))
8722 				continue;
8723 
8724 			if (((ipif->ipif_flags &
8725 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8726 			    IPIF_DEPRECATED)) ||
8727 			    IS_LOOPBACK(ill) ||
8728 			    !(ipif->ipif_flags & IPIF_UP)) &&
8729 			    (flags & LIFC_EXTERNAL_SOURCE))
8730 				continue;
8731 
8732 			if (zoneid != ipif->ipif_zoneid &&
8733 			    ipif->ipif_zoneid != ALL_ZONES &&
8734 			    (zoneid != GLOBAL_ZONEID ||
8735 			    !(flags & LIFC_ALLZONES)))
8736 				continue;
8737 
8738 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8739 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8740 					rw_exit(&ipst->ips_ill_g_lock);
8741 					return (EINVAL);
8742 				} else {
8743 					goto lif_copydone;
8744 				}
8745 			}
8746 
8747 			ipif_get_name(ipif, lifr->lifr_name,
8748 			    sizeof (lifr->lifr_name));
8749 			if (ipif->ipif_isv6) {
8750 				sin6 = (sin6_t *)&lifr->lifr_addr;
8751 				*sin6 = sin6_null;
8752 				sin6->sin6_family = AF_INET6;
8753 				sin6->sin6_addr =
8754 				    ipif->ipif_v6lcl_addr;
8755 				lifr->lifr_addrlen =
8756 				    ip_mask_to_plen_v6(
8757 				    &ipif->ipif_v6net_mask);
8758 			} else {
8759 				sin = (sin_t *)&lifr->lifr_addr;
8760 				*sin = sin_null;
8761 				sin->sin_family = AF_INET;
8762 				sin->sin_addr.s_addr =
8763 				    ipif->ipif_lcl_addr;
8764 				lifr->lifr_addrlen =
8765 				    ip_mask_to_plen(
8766 				    ipif->ipif_net_mask);
8767 			}
8768 			lifr++;
8769 		}
8770 	}
8771 lif_copydone:
8772 	rw_exit(&ipst->ips_ill_g_lock);
8773 
8774 	mp1->b_wptr = (uchar_t *)lifr;
8775 	if (STRUCT_BUF(lifc) != NULL) {
8776 		STRUCT_FSET(lifc, lifc_len,
8777 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8778 	}
8779 	return (0);
8780 }
8781 
8782 /* ARGSUSED */
8783 int
8784 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8785     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8786 {
8787 	ip_stack_t	*ipst;
8788 
8789 	if (q->q_next == NULL)
8790 		ipst = CONNQ_TO_IPST(q);
8791 	else
8792 		ipst = ILLQ_TO_IPST(q);
8793 
8794 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8795 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8796 	return (0);
8797 }
8798 
8799 static void
8800 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8801 {
8802 	ip6_asp_t *table;
8803 	size_t table_size;
8804 	mblk_t *data_mp;
8805 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8806 	ip_stack_t	*ipst;
8807 
8808 	if (q->q_next == NULL)
8809 		ipst = CONNQ_TO_IPST(q);
8810 	else
8811 		ipst = ILLQ_TO_IPST(q);
8812 
8813 	/* These two ioctls are I_STR only */
8814 	if (iocp->ioc_count == TRANSPARENT) {
8815 		miocnak(q, mp, 0, EINVAL);
8816 		return;
8817 	}
8818 
8819 	data_mp = mp->b_cont;
8820 	if (data_mp == NULL) {
8821 		/* The user passed us a NULL argument */
8822 		table = NULL;
8823 		table_size = iocp->ioc_count;
8824 	} else {
8825 		/*
8826 		 * The user provided a table.  The stream head
8827 		 * may have copied in the user data in chunks,
8828 		 * so make sure everything is pulled up
8829 		 * properly.
8830 		 */
8831 		if (MBLKL(data_mp) < iocp->ioc_count) {
8832 			mblk_t *new_data_mp;
8833 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8834 			    NULL) {
8835 				miocnak(q, mp, 0, ENOMEM);
8836 				return;
8837 			}
8838 			freemsg(data_mp);
8839 			data_mp = new_data_mp;
8840 			mp->b_cont = data_mp;
8841 		}
8842 		table = (ip6_asp_t *)data_mp->b_rptr;
8843 		table_size = iocp->ioc_count;
8844 	}
8845 
8846 	switch (iocp->ioc_cmd) {
8847 	case SIOCGIP6ADDRPOLICY:
8848 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
8849 		if (iocp->ioc_rval == -1)
8850 			iocp->ioc_error = EINVAL;
8851 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8852 		else if (table != NULL &&
8853 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8854 			ip6_asp_t *src = table;
8855 			ip6_asp32_t *dst = (void *)table;
8856 			int count = table_size / sizeof (ip6_asp_t);
8857 			int i;
8858 
8859 			/*
8860 			 * We need to do an in-place shrink of the array
8861 			 * to match the alignment attributes of the
8862 			 * 32-bit ABI looking at it.
8863 			 */
8864 			/* LINTED: logical expression always true: op "||" */
8865 			ASSERT(sizeof (*src) > sizeof (*dst));
8866 			for (i = 1; i < count; i++)
8867 				bcopy(src + i, dst + i, sizeof (*dst));
8868 		}
8869 #endif
8870 		break;
8871 
8872 	case SIOCSIP6ADDRPOLICY:
8873 		ASSERT(mp->b_prev == NULL);
8874 		mp->b_prev = (void *)q;
8875 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8876 		/*
8877 		 * We pass in the datamodel here so that the ip6_asp_replace()
8878 		 * routine can handle converting from 32-bit to native formats
8879 		 * where necessary.
8880 		 *
8881 		 * A better way to handle this might be to convert the inbound
8882 		 * data structure here, and hang it off a new 'mp'; thus the
8883 		 * ip6_asp_replace() logic would always be dealing with native
8884 		 * format data structures..
8885 		 *
8886 		 * (An even simpler way to handle these ioctls is to just
8887 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8888 		 * and just recompile everything that depends on it.)
8889 		 */
8890 #endif
8891 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
8892 		    iocp->ioc_flag & IOC_MODELS);
8893 		return;
8894 	}
8895 
8896 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8897 	qreply(q, mp);
8898 }
8899 
8900 static void
8901 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8902 {
8903 	mblk_t 		*data_mp;
8904 	struct dstinforeq	*dir;
8905 	uint8_t		*end, *cur;
8906 	in6_addr_t	*daddr, *saddr;
8907 	ipaddr_t	v4daddr;
8908 	ire_t		*ire;
8909 	char		*slabel, *dlabel;
8910 	boolean_t	isipv4;
8911 	int		match_ire;
8912 	ill_t		*dst_ill;
8913 	ipif_t		*src_ipif, *ire_ipif;
8914 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8915 	zoneid_t	zoneid;
8916 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
8917 
8918 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8919 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8920 
8921 	/*
8922 	 * This ioctl is I_STR only, and must have a
8923 	 * data mblk following the M_IOCTL mblk.
8924 	 */
8925 	data_mp = mp->b_cont;
8926 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8927 		miocnak(q, mp, 0, EINVAL);
8928 		return;
8929 	}
8930 
8931 	if (MBLKL(data_mp) < iocp->ioc_count) {
8932 		mblk_t *new_data_mp;
8933 
8934 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8935 			miocnak(q, mp, 0, ENOMEM);
8936 			return;
8937 		}
8938 		freemsg(data_mp);
8939 		data_mp = new_data_mp;
8940 		mp->b_cont = data_mp;
8941 	}
8942 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8943 
8944 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8945 	    end - cur >= sizeof (struct dstinforeq);
8946 	    cur += sizeof (struct dstinforeq)) {
8947 		dir = (struct dstinforeq *)cur;
8948 		daddr = &dir->dir_daddr;
8949 		saddr = &dir->dir_saddr;
8950 
8951 		/*
8952 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8953 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8954 		 * and ipif_select_source[_v6]() do not.
8955 		 */
8956 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8957 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
8958 
8959 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8960 		if (isipv4) {
8961 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8962 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8963 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
8964 		} else {
8965 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8966 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
8967 		}
8968 		if (ire == NULL) {
8969 			dir->dir_dreachable = 0;
8970 
8971 			/* move on to next dst addr */
8972 			continue;
8973 		}
8974 		dir->dir_dreachable = 1;
8975 
8976 		ire_ipif = ire->ire_ipif;
8977 		if (ire_ipif == NULL)
8978 			goto next_dst;
8979 
8980 		/*
8981 		 * We expect to get back an interface ire or a
8982 		 * gateway ire cache entry.  For both types, the
8983 		 * output interface is ire_ipif->ipif_ill.
8984 		 */
8985 		dst_ill = ire_ipif->ipif_ill;
8986 		dir->dir_dmactype = dst_ill->ill_mactype;
8987 
8988 		if (isipv4) {
8989 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8990 		} else {
8991 			src_ipif = ipif_select_source_v6(dst_ill,
8992 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
8993 			    zoneid);
8994 		}
8995 		if (src_ipif == NULL)
8996 			goto next_dst;
8997 
8998 		*saddr = src_ipif->ipif_v6lcl_addr;
8999 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9000 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9001 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9002 		dir->dir_sdeprecated =
9003 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9004 		ipif_refrele(src_ipif);
9005 next_dst:
9006 		ire_refrele(ire);
9007 	}
9008 	miocack(q, mp, iocp->ioc_count, 0);
9009 }
9010 
9011 /*
9012  * Check if this is an address assigned to this machine.
9013  * Skips interfaces that are down by using ire checks.
9014  * Translates mapped addresses to v4 addresses and then
9015  * treats them as such, returning true if the v4 address
9016  * associated with this mapped address is configured.
9017  * Note: Applications will have to be careful what they do
9018  * with the response; use of mapped addresses limits
9019  * what can be done with the socket, especially with
9020  * respect to socket options and ioctls - neither IPv4
9021  * options nor IPv6 sticky options/ancillary data options
9022  * may be used.
9023  */
9024 /* ARGSUSED */
9025 int
9026 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9027     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9028 {
9029 	struct sioc_addrreq *sia;
9030 	sin_t *sin;
9031 	ire_t *ire;
9032 	mblk_t *mp1;
9033 	zoneid_t zoneid;
9034 	ip_stack_t	*ipst;
9035 
9036 	ip1dbg(("ip_sioctl_tmyaddr"));
9037 
9038 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9039 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9040 	ipst = CONNQ_TO_IPST(q);
9041 
9042 	/* Existence verified in ip_wput_nondata */
9043 	mp1 = mp->b_cont->b_cont;
9044 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9045 	sin = (sin_t *)&sia->sa_addr;
9046 	switch (sin->sin_family) {
9047 	case AF_INET6: {
9048 		sin6_t *sin6 = (sin6_t *)sin;
9049 
9050 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9051 			ipaddr_t v4_addr;
9052 
9053 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9054 			    v4_addr);
9055 			ire = ire_ctable_lookup(v4_addr, 0,
9056 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9057 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9058 		} else {
9059 			in6_addr_t v6addr;
9060 
9061 			v6addr = sin6->sin6_addr;
9062 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9063 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9064 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9065 		}
9066 		break;
9067 	}
9068 	case AF_INET: {
9069 		ipaddr_t v4addr;
9070 
9071 		v4addr = sin->sin_addr.s_addr;
9072 		ire = ire_ctable_lookup(v4addr, 0,
9073 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9074 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9075 		break;
9076 	}
9077 	default:
9078 		return (EAFNOSUPPORT);
9079 	}
9080 	if (ire != NULL) {
9081 		sia->sa_res = 1;
9082 		ire_refrele(ire);
9083 	} else {
9084 		sia->sa_res = 0;
9085 	}
9086 	return (0);
9087 }
9088 
9089 /*
9090  * Check if this is an address assigned on-link i.e. neighbor,
9091  * and makes sure it's reachable from the current zone.
9092  * Returns true for my addresses as well.
9093  * Translates mapped addresses to v4 addresses and then
9094  * treats them as such, returning true if the v4 address
9095  * associated with this mapped address is configured.
9096  * Note: Applications will have to be careful what they do
9097  * with the response; use of mapped addresses limits
9098  * what can be done with the socket, especially with
9099  * respect to socket options and ioctls - neither IPv4
9100  * options nor IPv6 sticky options/ancillary data options
9101  * may be used.
9102  */
9103 /* ARGSUSED */
9104 int
9105 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9106     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9107 {
9108 	struct sioc_addrreq *sia;
9109 	sin_t *sin;
9110 	mblk_t	*mp1;
9111 	ire_t *ire = NULL;
9112 	zoneid_t zoneid;
9113 	ip_stack_t	*ipst;
9114 
9115 	ip1dbg(("ip_sioctl_tonlink"));
9116 
9117 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9118 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9119 	ipst = CONNQ_TO_IPST(q);
9120 
9121 	/* Existence verified in ip_wput_nondata */
9122 	mp1 = mp->b_cont->b_cont;
9123 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9124 	sin = (sin_t *)&sia->sa_addr;
9125 
9126 	/*
9127 	 * Match addresses with a zero gateway field to avoid
9128 	 * routes going through a router.
9129 	 * Exclude broadcast and multicast addresses.
9130 	 */
9131 	switch (sin->sin_family) {
9132 	case AF_INET6: {
9133 		sin6_t *sin6 = (sin6_t *)sin;
9134 
9135 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9136 			ipaddr_t v4_addr;
9137 
9138 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9139 			    v4_addr);
9140 			if (!CLASSD(v4_addr)) {
9141 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9142 				    NULL, NULL, zoneid, NULL,
9143 				    MATCH_IRE_GW, ipst);
9144 			}
9145 		} else {
9146 			in6_addr_t v6addr;
9147 			in6_addr_t v6gw;
9148 
9149 			v6addr = sin6->sin6_addr;
9150 			v6gw = ipv6_all_zeros;
9151 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9152 				ire = ire_route_lookup_v6(&v6addr, 0,
9153 				    &v6gw, 0, NULL, NULL, zoneid,
9154 				    NULL, MATCH_IRE_GW, ipst);
9155 			}
9156 		}
9157 		break;
9158 	}
9159 	case AF_INET: {
9160 		ipaddr_t v4addr;
9161 
9162 		v4addr = sin->sin_addr.s_addr;
9163 		if (!CLASSD(v4addr)) {
9164 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9165 			    NULL, NULL, zoneid, NULL,
9166 			    MATCH_IRE_GW, ipst);
9167 		}
9168 		break;
9169 	}
9170 	default:
9171 		return (EAFNOSUPPORT);
9172 	}
9173 	sia->sa_res = 0;
9174 	if (ire != NULL) {
9175 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9176 		    IRE_LOCAL|IRE_LOOPBACK)) {
9177 			sia->sa_res = 1;
9178 		}
9179 		ire_refrele(ire);
9180 	}
9181 	return (0);
9182 }
9183 
9184 /*
9185  * TBD: implement when kernel maintaines a list of site prefixes.
9186  */
9187 /* ARGSUSED */
9188 int
9189 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9190     ip_ioctl_cmd_t *ipip, void *ifreq)
9191 {
9192 	return (ENXIO);
9193 }
9194 
9195 /* ARGSUSED */
9196 int
9197 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9198     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9199 {
9200 	ill_t		*ill;
9201 	mblk_t		*mp1;
9202 	conn_t		*connp;
9203 	boolean_t	success;
9204 
9205 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9206 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9207 	/* ioctl comes down on an conn */
9208 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9209 	connp = Q_TO_CONN(q);
9210 
9211 	mp->b_datap->db_type = M_IOCTL;
9212 
9213 	/*
9214 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9215 	 * The original mp contains contaminated b_next values due to 'mi',
9216 	 * which is needed to do the mi_copy_done. Unfortunately if we
9217 	 * send down the original mblk itself and if we are popped due to an
9218 	 * an unplumb before the response comes back from tunnel,
9219 	 * the streamhead (which does a freemsg) will see this contaminated
9220 	 * message and the assertion in freemsg about non-null b_next/b_prev
9221 	 * will panic a DEBUG kernel.
9222 	 */
9223 	mp1 = copymsg(mp);
9224 	if (mp1 == NULL)
9225 		return (ENOMEM);
9226 
9227 	ill = ipif->ipif_ill;
9228 	mutex_enter(&connp->conn_lock);
9229 	mutex_enter(&ill->ill_lock);
9230 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9231 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9232 		    mp, 0);
9233 	} else {
9234 		success = ill_pending_mp_add(ill, connp, mp);
9235 	}
9236 	mutex_exit(&ill->ill_lock);
9237 	mutex_exit(&connp->conn_lock);
9238 
9239 	if (success) {
9240 		ip1dbg(("sending down tunparam request "));
9241 		putnext(ill->ill_wq, mp1);
9242 		return (EINPROGRESS);
9243 	} else {
9244 		/* The conn has started closing */
9245 		freemsg(mp1);
9246 		return (EINTR);
9247 	}
9248 }
9249 
9250 /*
9251  * ARP IOCTLs.
9252  * How does IP get in the business of fronting ARP configuration/queries?
9253  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9254  * are by tradition passed in through a datagram socket.  That lands in IP.
9255  * As it happens, this is just as well since the interface is quite crude in
9256  * that it passes in no information about protocol or hardware types, or
9257  * interface association.  After making the protocol assumption, IP is in
9258  * the position to look up the name of the ILL, which ARP will need, and
9259  * format a request that can be handled by ARP.  The request is passed up
9260  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9261  * back a response.  ARP supports its own set of more general IOCTLs, in
9262  * case anyone is interested.
9263  */
9264 /* ARGSUSED */
9265 int
9266 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9267     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9268 {
9269 	mblk_t *mp1;
9270 	mblk_t *mp2;
9271 	mblk_t *pending_mp;
9272 	ipaddr_t ipaddr;
9273 	area_t *area;
9274 	struct iocblk *iocp;
9275 	conn_t *connp;
9276 	struct arpreq *ar;
9277 	struct xarpreq *xar;
9278 	int flags, alength;
9279 	char *lladdr;
9280 	ip_stack_t	*ipst;
9281 	ill_t *ill = ipif->ipif_ill;
9282 	boolean_t if_arp_ioctl = B_FALSE;
9283 
9284 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9285 	connp = Q_TO_CONN(q);
9286 	ipst = connp->conn_netstack->netstack_ip;
9287 
9288 	if (ipip->ipi_cmd_type == XARP_CMD) {
9289 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9290 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9291 		ar = NULL;
9292 
9293 		flags = xar->xarp_flags;
9294 		lladdr = LLADDR(&xar->xarp_ha);
9295 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9296 		/*
9297 		 * Validate against user's link layer address length
9298 		 * input and name and addr length limits.
9299 		 */
9300 		alength = ill->ill_phys_addr_length;
9301 		if (ipip->ipi_cmd == SIOCSXARP) {
9302 			if (alength != xar->xarp_ha.sdl_alen ||
9303 			    (alength + xar->xarp_ha.sdl_nlen >
9304 			    sizeof (xar->xarp_ha.sdl_data)))
9305 				return (EINVAL);
9306 		}
9307 	} else {
9308 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9309 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9310 		xar = NULL;
9311 
9312 		flags = ar->arp_flags;
9313 		lladdr = ar->arp_ha.sa_data;
9314 		/*
9315 		 * Theoretically, the sa_family could tell us what link
9316 		 * layer type this operation is trying to deal with. By
9317 		 * common usage AF_UNSPEC means ethernet. We'll assume
9318 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9319 		 * for now. Our new SIOC*XARP ioctls can be used more
9320 		 * generally.
9321 		 *
9322 		 * If the underlying media happens to have a non 6 byte
9323 		 * address, arp module will fail set/get, but the del
9324 		 * operation will succeed.
9325 		 */
9326 		alength = 6;
9327 		if ((ipip->ipi_cmd != SIOCDARP) &&
9328 		    (alength != ill->ill_phys_addr_length)) {
9329 			return (EINVAL);
9330 		}
9331 	}
9332 
9333 	/*
9334 	 * We are going to pass up to ARP a packet chain that looks
9335 	 * like:
9336 	 *
9337 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9338 	 *
9339 	 * Get a copy of the original IOCTL mblk to head the chain,
9340 	 * to be sent up (in mp1). Also get another copy to store
9341 	 * in the ill_pending_mp list, for matching the response
9342 	 * when it comes back from ARP.
9343 	 */
9344 	mp1 = copyb(mp);
9345 	pending_mp = copymsg(mp);
9346 	if (mp1 == NULL || pending_mp == NULL) {
9347 		if (mp1 != NULL)
9348 			freeb(mp1);
9349 		if (pending_mp != NULL)
9350 			inet_freemsg(pending_mp);
9351 		return (ENOMEM);
9352 	}
9353 
9354 	ipaddr = sin->sin_addr.s_addr;
9355 
9356 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9357 	    (caddr_t)&ipaddr);
9358 	if (mp2 == NULL) {
9359 		freeb(mp1);
9360 		inet_freemsg(pending_mp);
9361 		return (ENOMEM);
9362 	}
9363 	/* Put together the chain. */
9364 	mp1->b_cont = mp2;
9365 	mp1->b_datap->db_type = M_IOCTL;
9366 	mp2->b_cont = mp;
9367 	mp2->b_datap->db_type = M_DATA;
9368 
9369 	iocp = (struct iocblk *)mp1->b_rptr;
9370 
9371 	/*
9372 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9373 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9374 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9375 	 * ioc_count field; set ioc_count to be correct.
9376 	 */
9377 	iocp->ioc_count = MBLKL(mp1->b_cont);
9378 
9379 	/*
9380 	 * Set the proper command in the ARP message.
9381 	 * Convert the SIOC{G|S|D}ARP calls into our
9382 	 * AR_ENTRY_xxx calls.
9383 	 */
9384 	area = (area_t *)mp2->b_rptr;
9385 	switch (iocp->ioc_cmd) {
9386 	case SIOCDARP:
9387 	case SIOCDXARP:
9388 		/*
9389 		 * We defer deleting the corresponding IRE until
9390 		 * we return from arp.
9391 		 */
9392 		area->area_cmd = AR_ENTRY_DELETE;
9393 		area->area_proto_mask_offset = 0;
9394 		break;
9395 	case SIOCGARP:
9396 	case SIOCGXARP:
9397 		area->area_cmd = AR_ENTRY_SQUERY;
9398 		area->area_proto_mask_offset = 0;
9399 		break;
9400 	case SIOCSARP:
9401 	case SIOCSXARP:
9402 		/*
9403 		 * Delete the corresponding ire to make sure IP will
9404 		 * pick up any change from arp.
9405 		 */
9406 		if (!if_arp_ioctl) {
9407 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9408 		} else {
9409 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9410 			if (ipif != NULL) {
9411 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9412 				    ipst);
9413 				ipif_refrele(ipif);
9414 			}
9415 		}
9416 		break;
9417 	}
9418 	iocp->ioc_cmd = area->area_cmd;
9419 
9420 	/*
9421 	 * Fill in the rest of the ARP operation fields.
9422 	 */
9423 	area->area_hw_addr_length = alength;
9424 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9425 
9426 	/* Translate the flags. */
9427 	if (flags & ATF_PERM)
9428 		area->area_flags |= ACE_F_PERMANENT;
9429 	if (flags & ATF_PUBL)
9430 		area->area_flags |= ACE_F_PUBLISH;
9431 	if (flags & ATF_AUTHORITY)
9432 		area->area_flags |= ACE_F_AUTHORITY;
9433 
9434 	/*
9435 	 * Before sending 'mp' to ARP, we have to clear the b_next
9436 	 * and b_prev. Otherwise if STREAMS encounters such a message
9437 	 * in freemsg(), (because ARP can close any time) it can cause
9438 	 * a panic. But mi code needs the b_next and b_prev values of
9439 	 * mp->b_cont, to complete the ioctl. So we store it here
9440 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9441 	 * when the response comes down from ARP.
9442 	 */
9443 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9444 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9445 	mp->b_cont->b_next = NULL;
9446 	mp->b_cont->b_prev = NULL;
9447 
9448 	mutex_enter(&connp->conn_lock);
9449 	mutex_enter(&ill->ill_lock);
9450 	/* conn has not yet started closing, hence this can't fail */
9451 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9452 	mutex_exit(&ill->ill_lock);
9453 	mutex_exit(&connp->conn_lock);
9454 
9455 	/*
9456 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9457 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9458 	 */
9459 	putnext(ill->ill_rq, mp1);
9460 	return (EINPROGRESS);
9461 }
9462 
9463 /*
9464  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9465  * the associated sin and refhold and return the associated ipif via `ci'.
9466  */
9467 int
9468 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9469     cmd_info_t *ci, ipsq_func_t func)
9470 {
9471 	mblk_t	*mp1;
9472 	int	err;
9473 	sin_t	*sin;
9474 	conn_t	*connp;
9475 	ipif_t	*ipif;
9476 	ire_t	*ire = NULL;
9477 	ill_t	*ill = NULL;
9478 	boolean_t exists;
9479 	ip_stack_t *ipst;
9480 	struct arpreq *ar;
9481 	struct xarpreq *xar;
9482 	struct sockaddr_dl *sdl;
9483 
9484 	/* ioctl comes down on a conn */
9485 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9486 	connp = Q_TO_CONN(q);
9487 	if (connp->conn_af_isv6)
9488 		return (ENXIO);
9489 
9490 	ipst = connp->conn_netstack->netstack_ip;
9491 
9492 	/* Verified in ip_wput_nondata */
9493 	mp1 = mp->b_cont->b_cont;
9494 
9495 	if (ipip->ipi_cmd_type == XARP_CMD) {
9496 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9497 		xar = (struct xarpreq *)mp1->b_rptr;
9498 		sin = (sin_t *)&xar->xarp_pa;
9499 		sdl = &xar->xarp_ha;
9500 
9501 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9502 			return (ENXIO);
9503 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9504 			return (EINVAL);
9505 	} else {
9506 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9507 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9508 		ar = (struct arpreq *)mp1->b_rptr;
9509 		sin = (sin_t *)&ar->arp_pa;
9510 	}
9511 
9512 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9513 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9514 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9515 		    mp, func, &err, ipst);
9516 		if (ipif == NULL)
9517 			return (err);
9518 		if (ipif->ipif_id != 0 ||
9519 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9520 			ipif_refrele(ipif);
9521 			return (ENXIO);
9522 		}
9523 	} else {
9524 		/*
9525 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9526 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9527 		 * case, a simple forwarding table lookup will return the
9528 		 * IRE_IF_RESOLVER for the first interface in the group, which
9529 		 * might not be the interface on which the requested IP
9530 		 * address was resolved due to the ill selection algorithm
9531 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9532 		 * lookup first: if the IRE cache entry for the IP address is
9533 		 * still there, it will contain the ill pointer for the right
9534 		 * interface, so we use that. If the cache entry has been
9535 		 * flushed, we fall back to the forwarding table lookup. This
9536 		 * should be rare enough since IRE cache entries have a longer
9537 		 * life expectancy than ARP cache entries.
9538 		 */
9539 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9540 		    ipst);
9541 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9542 		    ((ill = ire_to_ill(ire)) == NULL) ||
9543 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9544 			if (ire != NULL)
9545 				ire_refrele(ire);
9546 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9547 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9548 			    NULL, MATCH_IRE_TYPE, ipst);
9549 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9550 
9551 				if (ire != NULL)
9552 					ire_refrele(ire);
9553 				return (ENXIO);
9554 			}
9555 		}
9556 		ASSERT(ire != NULL && ill != NULL);
9557 		ipif = ill->ill_ipif;
9558 		ipif_refhold(ipif);
9559 		ire_refrele(ire);
9560 	}
9561 	ci->ci_sin = sin;
9562 	ci->ci_ipif = ipif;
9563 	return (0);
9564 }
9565 
9566 /*
9567  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9568  * atomically set/clear the muxids. Also complete the ioctl by acking or
9569  * naking it.  Note that the code is structured such that the link type,
9570  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9571  * its clones use the persistent link, while pppd(1M) and perhaps many
9572  * other daemons may use non-persistent link.  When combined with some
9573  * ill_t states, linking and unlinking lower streams may be used as
9574  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9575  */
9576 /* ARGSUSED */
9577 void
9578 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9579 {
9580 	mblk_t		*mp1, *mp2;
9581 	struct linkblk	*li;
9582 	struct ipmx_s	*ipmxp;
9583 	ill_t		*ill;
9584 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9585 	int		err = 0;
9586 	boolean_t	entered_ipsq = B_FALSE;
9587 	boolean_t	islink;
9588 	ip_stack_t	*ipst;
9589 
9590 	if (CONN_Q(q))
9591 		ipst = CONNQ_TO_IPST(q);
9592 	else
9593 		ipst = ILLQ_TO_IPST(q);
9594 
9595 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9596 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9597 
9598 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9599 
9600 	mp1 = mp->b_cont;	/* This is the linkblk info */
9601 	li = (struct linkblk *)mp1->b_rptr;
9602 
9603 	/*
9604 	 * ARP has added this special mblk, and the utility is asking us
9605 	 * to perform consistency checks, and also atomically set the
9606 	 * muxid. Ifconfig is an example.  It achieves this by using
9607 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9608 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9609 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9610 	 * and other comments in this routine for more details.
9611 	 */
9612 	mp2 = mp1->b_cont;	/* This is added by ARP */
9613 
9614 	/*
9615 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9616 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9617 	 * get the special mblk above.  For backward compatibility, we
9618 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9619 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9620 	 * not atomic, and can leave the streams unplumbable if the utility
9621 	 * is interrupted before it does the SIOCSLIFMUXID.
9622 	 */
9623 	if (mp2 == NULL) {
9624 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9625 		if (err == EINPROGRESS)
9626 			return;
9627 		goto done;
9628 	}
9629 
9630 	/*
9631 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9632 	 * ARP has appended this last mblk to tell us whether the lower stream
9633 	 * is an arp-dev stream or an IP module stream.
9634 	 */
9635 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9636 	if (ipmxp->ipmx_arpdev_stream) {
9637 		/*
9638 		 * The lower stream is the arp-dev stream.
9639 		 */
9640 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9641 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9642 		if (ill == NULL) {
9643 			if (err == EINPROGRESS)
9644 				return;
9645 			err = EINVAL;
9646 			goto done;
9647 		}
9648 
9649 		if (ipsq == NULL) {
9650 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9651 			    NEW_OP, B_TRUE);
9652 			if (ipsq == NULL) {
9653 				ill_refrele(ill);
9654 				return;
9655 			}
9656 			entered_ipsq = B_TRUE;
9657 		}
9658 		ASSERT(IAM_WRITER_ILL(ill));
9659 		ill_refrele(ill);
9660 
9661 		/*
9662 		 * To ensure consistency between IP and ARP, the following
9663 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9664 		 * This is because the muxid's are stored in the IP stream on
9665 		 * the ill.
9666 		 *
9667 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9668 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9669 		 * not yet plinked, and it also checks that the corresponding
9670 		 * IP stream is already plinked.
9671 		 *
9672 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9673 		 * punlinking the IP stream. IP does not allow punlink of the
9674 		 * IP stream unless the arp stream has been punlinked.
9675 		 */
9676 		if ((islink &&
9677 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9678 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9679 			err = EINVAL;
9680 			goto done;
9681 		}
9682 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9683 	} else {
9684 		/*
9685 		 * The lower stream is probably an IP module stream.  Do
9686 		 * consistency checking.
9687 		 */
9688 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9689 		if (err == EINPROGRESS)
9690 			return;
9691 	}
9692 done:
9693 	if (err == 0)
9694 		miocack(q, mp, 0, 0);
9695 	else
9696 		miocnak(q, mp, 0, err);
9697 
9698 	/* Conn was refheld in ip_sioctl_copyin_setup */
9699 	if (CONN_Q(q))
9700 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9701 	if (entered_ipsq)
9702 		ipsq_exit(ipsq);
9703 }
9704 
9705 /*
9706  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9707  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9708  * module stream).  If `doconsist' is set, then do the extended consistency
9709  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9710  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9711  * an error code on failure.
9712  */
9713 static int
9714 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9715     struct linkblk *li, boolean_t doconsist)
9716 {
9717 	ill_t  		*ill;
9718 	queue_t		*ipwq, *dwq;
9719 	const char	*name;
9720 	struct qinit	*qinfo;
9721 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9722 	boolean_t	entered_ipsq = B_FALSE;
9723 
9724 	/*
9725 	 * Walk the lower stream to verify it's the IP module stream.
9726 	 * The IP module is identified by its name, wput function,
9727 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9728 	 * (li->l_qbot) will not vanish until this ioctl completes.
9729 	 */
9730 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9731 		qinfo = ipwq->q_qinfo;
9732 		name = qinfo->qi_minfo->mi_idname;
9733 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9734 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9735 			break;
9736 		}
9737 	}
9738 
9739 	/*
9740 	 * If this isn't an IP module stream, bail.
9741 	 */
9742 	if (ipwq == NULL)
9743 		return (0);
9744 
9745 	ill = ipwq->q_ptr;
9746 	ASSERT(ill != NULL);
9747 
9748 	if (ipsq == NULL) {
9749 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9750 		    NEW_OP, B_TRUE);
9751 		if (ipsq == NULL)
9752 			return (EINPROGRESS);
9753 		entered_ipsq = B_TRUE;
9754 	}
9755 	ASSERT(IAM_WRITER_ILL(ill));
9756 
9757 	if (doconsist) {
9758 		/*
9759 		 * Consistency checking requires that I_{P}LINK occurs
9760 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9761 		 * occurs prior to clearing ill_arp_muxid.
9762 		 */
9763 		if ((islink && ill->ill_ip_muxid != 0) ||
9764 		    (!islink && ill->ill_arp_muxid != 0)) {
9765 			if (entered_ipsq)
9766 				ipsq_exit(ipsq);
9767 			return (EINVAL);
9768 		}
9769 	}
9770 
9771 	/*
9772 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9773 	 * the read queue of the module immediately below IP in the ill.
9774 	 * These are used during the capability negotiation below.
9775 	 */
9776 	ill->ill_lmod_rq = NULL;
9777 	ill->ill_lmod_cnt = 0;
9778 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9779 		ill->ill_lmod_rq = RD(dwq);
9780 		for (; dwq != NULL; dwq = dwq->q_next)
9781 			ill->ill_lmod_cnt++;
9782 	}
9783 
9784 	if (doconsist)
9785 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9786 
9787 	/*
9788 	 * Mark the ipsq busy until the capability operations initiated below
9789 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
9790 	 * returns, but the capability operation may complete asynchronously
9791 	 * much later.
9792 	 */
9793 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
9794 	/*
9795 	 * If there's at least one up ipif on this ill, then we're bound to
9796 	 * the underlying driver via DLPI.  In that case, renegotiate
9797 	 * capabilities to account for any possible change in modules
9798 	 * interposed between IP and the driver.
9799 	 */
9800 	if (ill->ill_ipif_up_count > 0) {
9801 		if (islink)
9802 			ill_capability_probe(ill);
9803 		else
9804 			ill_capability_reset(ill, B_FALSE);
9805 	}
9806 	ipsq_current_finish(ipsq);
9807 
9808 	if (entered_ipsq)
9809 		ipsq_exit(ipsq);
9810 
9811 	return (0);
9812 }
9813 
9814 /*
9815  * Search the ioctl command in the ioctl tables and return a pointer
9816  * to the ioctl command information. The ioctl command tables are
9817  * static and fully populated at compile time.
9818  */
9819 ip_ioctl_cmd_t *
9820 ip_sioctl_lookup(int ioc_cmd)
9821 {
9822 	int index;
9823 	ip_ioctl_cmd_t *ipip;
9824 	ip_ioctl_cmd_t *ipip_end;
9825 
9826 	if (ioc_cmd == IPI_DONTCARE)
9827 		return (NULL);
9828 
9829 	/*
9830 	 * Do a 2 step search. First search the indexed table
9831 	 * based on the least significant byte of the ioctl cmd.
9832 	 * If we don't find a match, then search the misc table
9833 	 * serially.
9834 	 */
9835 	index = ioc_cmd & 0xFF;
9836 	if (index < ip_ndx_ioctl_count) {
9837 		ipip = &ip_ndx_ioctl_table[index];
9838 		if (ipip->ipi_cmd == ioc_cmd) {
9839 			/* Found a match in the ndx table */
9840 			return (ipip);
9841 		}
9842 	}
9843 
9844 	/* Search the misc table */
9845 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9846 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9847 		if (ipip->ipi_cmd == ioc_cmd)
9848 			/* Found a match in the misc table */
9849 			return (ipip);
9850 	}
9851 
9852 	return (NULL);
9853 }
9854 
9855 /*
9856  * Wrapper function for resuming deferred ioctl processing
9857  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9858  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9859  */
9860 /* ARGSUSED */
9861 void
9862 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9863     void *dummy_arg)
9864 {
9865 	ip_sioctl_copyin_setup(q, mp);
9866 }
9867 
9868 /*
9869  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9870  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9871  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9872  * We establish here the size of the block to be copied in.  mi_copyin
9873  * arranges for this to happen, an processing continues in ip_wput with
9874  * an M_IOCDATA message.
9875  */
9876 void
9877 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9878 {
9879 	int	copyin_size;
9880 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9881 	ip_ioctl_cmd_t *ipip;
9882 	cred_t *cr;
9883 	ip_stack_t	*ipst;
9884 
9885 	if (CONN_Q(q))
9886 		ipst = CONNQ_TO_IPST(q);
9887 	else
9888 		ipst = ILLQ_TO_IPST(q);
9889 
9890 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9891 	if (ipip == NULL) {
9892 		/*
9893 		 * The ioctl is not one we understand or own.
9894 		 * Pass it along to be processed down stream,
9895 		 * if this is a module instance of IP, else nak
9896 		 * the ioctl.
9897 		 */
9898 		if (q->q_next == NULL) {
9899 			goto nak;
9900 		} else {
9901 			putnext(q, mp);
9902 			return;
9903 		}
9904 	}
9905 
9906 	/*
9907 	 * If this is deferred, then we will do all the checks when we
9908 	 * come back.
9909 	 */
9910 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9911 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9912 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9913 		return;
9914 	}
9915 
9916 	/*
9917 	 * Only allow a very small subset of IP ioctls on this stream if
9918 	 * IP is a module and not a driver. Allowing ioctls to be processed
9919 	 * in this case may cause assert failures or data corruption.
9920 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9921 	 * ioctls allowed on an IP module stream, after which this stream
9922 	 * normally becomes a multiplexor (at which time the stream head
9923 	 * will fail all ioctls).
9924 	 */
9925 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9926 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9927 			/*
9928 			 * Pass common Streams ioctls which the IP
9929 			 * module does not own or consume along to
9930 			 * be processed down stream.
9931 			 */
9932 			putnext(q, mp);
9933 			return;
9934 		} else {
9935 			goto nak;
9936 		}
9937 	}
9938 
9939 	/* Make sure we have ioctl data to process. */
9940 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9941 		goto nak;
9942 
9943 	/*
9944 	 * Prefer dblk credential over ioctl credential; some synthesized
9945 	 * ioctls have kcred set because there's no way to crhold()
9946 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9947 	 * the framework; the caller of ioctl needs to hold the reference
9948 	 * for the duration of the call).
9949 	 */
9950 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9951 
9952 	/* Make sure normal users don't send down privileged ioctls */
9953 	if ((ipip->ipi_flags & IPI_PRIV) &&
9954 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9955 		/* We checked the privilege earlier but log it here */
9956 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9957 		return;
9958 	}
9959 
9960 	/*
9961 	 * The ioctl command tables can only encode fixed length
9962 	 * ioctl data. If the length is variable, the table will
9963 	 * encode the length as zero. Such special cases are handled
9964 	 * below in the switch.
9965 	 */
9966 	if (ipip->ipi_copyin_size != 0) {
9967 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9968 		return;
9969 	}
9970 
9971 	switch (iocp->ioc_cmd) {
9972 	case O_SIOCGIFCONF:
9973 	case SIOCGIFCONF:
9974 		/*
9975 		 * This IOCTL is hilarious.  See comments in
9976 		 * ip_sioctl_get_ifconf for the story.
9977 		 */
9978 		if (iocp->ioc_count == TRANSPARENT)
9979 			copyin_size = SIZEOF_STRUCT(ifconf,
9980 			    iocp->ioc_flag);
9981 		else
9982 			copyin_size = iocp->ioc_count;
9983 		mi_copyin(q, mp, NULL, copyin_size);
9984 		return;
9985 
9986 	case O_SIOCGLIFCONF:
9987 	case SIOCGLIFCONF:
9988 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9989 		mi_copyin(q, mp, NULL, copyin_size);
9990 		return;
9991 
9992 	case SIOCGLIFSRCOF:
9993 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9994 		mi_copyin(q, mp, NULL, copyin_size);
9995 		return;
9996 	case SIOCGIP6ADDRPOLICY:
9997 		ip_sioctl_ip6addrpolicy(q, mp);
9998 		ip6_asp_table_refrele(ipst);
9999 		return;
10000 
10001 	case SIOCSIP6ADDRPOLICY:
10002 		ip_sioctl_ip6addrpolicy(q, mp);
10003 		return;
10004 
10005 	case SIOCGDSTINFO:
10006 		ip_sioctl_dstinfo(q, mp);
10007 		ip6_asp_table_refrele(ipst);
10008 		return;
10009 
10010 	case I_PLINK:
10011 	case I_PUNLINK:
10012 	case I_LINK:
10013 	case I_UNLINK:
10014 		/*
10015 		 * We treat non-persistent link similarly as the persistent
10016 		 * link case, in terms of plumbing/unplumbing, as well as
10017 		 * dynamic re-plumbing events indicator.  See comments
10018 		 * in ip_sioctl_plink() for more.
10019 		 *
10020 		 * Request can be enqueued in the 'ipsq' while waiting
10021 		 * to become exclusive. So bump up the conn ref.
10022 		 */
10023 		if (CONN_Q(q))
10024 			CONN_INC_REF(Q_TO_CONN(q));
10025 		ip_sioctl_plink(NULL, q, mp, NULL);
10026 		return;
10027 
10028 	case ND_GET:
10029 	case ND_SET:
10030 		/*
10031 		 * Use of the nd table requires holding the reader lock.
10032 		 * Modifying the nd table thru nd_load/nd_unload requires
10033 		 * the writer lock.
10034 		 */
10035 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10036 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10037 			rw_exit(&ipst->ips_ip_g_nd_lock);
10038 
10039 			if (iocp->ioc_error)
10040 				iocp->ioc_count = 0;
10041 			mp->b_datap->db_type = M_IOCACK;
10042 			qreply(q, mp);
10043 			return;
10044 		}
10045 		rw_exit(&ipst->ips_ip_g_nd_lock);
10046 		/*
10047 		 * We don't understand this subioctl of ND_GET / ND_SET.
10048 		 * Maybe intended for some driver / module below us
10049 		 */
10050 		if (q->q_next) {
10051 			putnext(q, mp);
10052 		} else {
10053 			iocp->ioc_error = ENOENT;
10054 			mp->b_datap->db_type = M_IOCNAK;
10055 			iocp->ioc_count = 0;
10056 			qreply(q, mp);
10057 		}
10058 		return;
10059 
10060 	case IP_IOCTL:
10061 		ip_wput_ioctl(q, mp);
10062 		return;
10063 	default:
10064 		cmn_err(CE_PANIC, "should not happen ");
10065 	}
10066 nak:
10067 	if (mp->b_cont != NULL) {
10068 		freemsg(mp->b_cont);
10069 		mp->b_cont = NULL;
10070 	}
10071 	iocp->ioc_error = EINVAL;
10072 	mp->b_datap->db_type = M_IOCNAK;
10073 	iocp->ioc_count = 0;
10074 	qreply(q, mp);
10075 }
10076 
10077 /* ip_wput hands off ARP IOCTL responses to us */
10078 void
10079 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10080 {
10081 	struct arpreq *ar;
10082 	struct xarpreq *xar;
10083 	area_t	*area;
10084 	mblk_t	*area_mp;
10085 	struct iocblk *iocp;
10086 	mblk_t	*orig_ioc_mp, *tmp;
10087 	struct iocblk	*orig_iocp;
10088 	ill_t *ill;
10089 	conn_t *connp = NULL;
10090 	uint_t ioc_id;
10091 	mblk_t *pending_mp;
10092 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10093 	int *flagsp;
10094 	char *storage = NULL;
10095 	sin_t *sin;
10096 	ipaddr_t addr;
10097 	int err;
10098 	ip_stack_t *ipst;
10099 
10100 	ill = q->q_ptr;
10101 	ASSERT(ill != NULL);
10102 	ipst = ill->ill_ipst;
10103 
10104 	/*
10105 	 * We should get back from ARP a packet chain that looks like:
10106 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10107 	 */
10108 	if (!(area_mp = mp->b_cont) ||
10109 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10110 	    !(orig_ioc_mp = area_mp->b_cont) ||
10111 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10112 		freemsg(mp);
10113 		return;
10114 	}
10115 
10116 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10117 
10118 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10119 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10120 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10121 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10122 		x_arp_ioctl = B_TRUE;
10123 		xar = (struct xarpreq *)tmp->b_rptr;
10124 		sin = (sin_t *)&xar->xarp_pa;
10125 		flagsp = &xar->xarp_flags;
10126 		storage = xar->xarp_ha.sdl_data;
10127 		if (xar->xarp_ha.sdl_nlen != 0)
10128 			ifx_arp_ioctl = B_TRUE;
10129 	} else {
10130 		ar = (struct arpreq *)tmp->b_rptr;
10131 		sin = (sin_t *)&ar->arp_pa;
10132 		flagsp = &ar->arp_flags;
10133 		storage = ar->arp_ha.sa_data;
10134 	}
10135 
10136 	iocp = (struct iocblk *)mp->b_rptr;
10137 
10138 	/*
10139 	 * Pick out the originating queue based on the ioc_id.
10140 	 */
10141 	ioc_id = iocp->ioc_id;
10142 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10143 	if (pending_mp == NULL) {
10144 		ASSERT(connp == NULL);
10145 		inet_freemsg(mp);
10146 		return;
10147 	}
10148 	ASSERT(connp != NULL);
10149 	q = CONNP_TO_WQ(connp);
10150 
10151 	/* Uncouple the internally generated IOCTL from the original one */
10152 	area = (area_t *)area_mp->b_rptr;
10153 	area_mp->b_cont = NULL;
10154 
10155 	/*
10156 	 * Restore the b_next and b_prev used by mi code. This is needed
10157 	 * to complete the ioctl using mi* functions. We stored them in
10158 	 * the pending mp prior to sending the request to ARP.
10159 	 */
10160 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10161 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10162 	inet_freemsg(pending_mp);
10163 
10164 	/*
10165 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10166 	 * Catch the case where there is an IRE_CACHE by no entry in the
10167 	 * arp table.
10168 	 */
10169 	addr = sin->sin_addr.s_addr;
10170 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10171 		ire_t			*ire;
10172 		dl_unitdata_req_t	*dlup;
10173 		mblk_t			*llmp;
10174 		int			addr_len;
10175 		ill_t			*ipsqill = NULL;
10176 
10177 		if (ifx_arp_ioctl) {
10178 			/*
10179 			 * There's no need to lookup the ill, since
10180 			 * we've already done that when we started
10181 			 * processing the ioctl and sent the message
10182 			 * to ARP on that ill.  So use the ill that
10183 			 * is stored in q->q_ptr.
10184 			 */
10185 			ipsqill = ill;
10186 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10187 			    ipsqill->ill_ipif, ALL_ZONES,
10188 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10189 		} else {
10190 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10191 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10192 			if (ire != NULL)
10193 				ipsqill = ire_to_ill(ire);
10194 		}
10195 
10196 		if ((x_arp_ioctl) && (ipsqill != NULL))
10197 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10198 
10199 		if (ire != NULL) {
10200 			/*
10201 			 * Since the ire obtained from cachetable is used for
10202 			 * mac addr copying below, treat an incomplete ire as if
10203 			 * as if we never found it.
10204 			 */
10205 			if (ire->ire_nce != NULL &&
10206 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10207 				ire_refrele(ire);
10208 				ire = NULL;
10209 				ipsqill = NULL;
10210 				goto errack;
10211 			}
10212 			*flagsp = ATF_INUSE;
10213 			llmp = (ire->ire_nce != NULL ?
10214 			    ire->ire_nce->nce_res_mp : NULL);
10215 			if (llmp != NULL && ipsqill != NULL) {
10216 				uchar_t *macaddr;
10217 
10218 				addr_len = ipsqill->ill_phys_addr_length;
10219 				if (x_arp_ioctl && ((addr_len +
10220 				    ipsqill->ill_name_length) >
10221 				    sizeof (xar->xarp_ha.sdl_data))) {
10222 					ire_refrele(ire);
10223 					freemsg(mp);
10224 					ip_ioctl_finish(q, orig_ioc_mp,
10225 					    EINVAL, NO_COPYOUT, NULL);
10226 					return;
10227 				}
10228 				*flagsp |= ATF_COM;
10229 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10230 				if (ipsqill->ill_sap_length < 0)
10231 					macaddr = llmp->b_rptr +
10232 					    dlup->dl_dest_addr_offset;
10233 				else
10234 					macaddr = llmp->b_rptr +
10235 					    dlup->dl_dest_addr_offset +
10236 					    ipsqill->ill_sap_length;
10237 				/*
10238 				 * For SIOCGARP, MAC address length
10239 				 * validation has already been done
10240 				 * before the ioctl was issued to ARP to
10241 				 * allow it to progress only on 6 byte
10242 				 * addressable (ethernet like) media. Thus
10243 				 * the mac address copying can not overwrite
10244 				 * the sa_data area below.
10245 				 */
10246 				bcopy(macaddr, storage, addr_len);
10247 			}
10248 			/* Ditch the internal IOCTL. */
10249 			freemsg(mp);
10250 			ire_refrele(ire);
10251 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10252 			return;
10253 		}
10254 	}
10255 
10256 	/*
10257 	 * Delete the coresponding IRE_CACHE if any.
10258 	 * Reset the error if there was one (in case there was no entry
10259 	 * in arp.)
10260 	 */
10261 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10262 		ipif_t *ipintf = NULL;
10263 
10264 		if (ifx_arp_ioctl) {
10265 			/*
10266 			 * There's no need to lookup the ill, since
10267 			 * we've already done that when we started
10268 			 * processing the ioctl and sent the message
10269 			 * to ARP on that ill.  So use the ill that
10270 			 * is stored in q->q_ptr.
10271 			 */
10272 			ipintf = ill->ill_ipif;
10273 		}
10274 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10275 			/*
10276 			 * The address in "addr" may be an entry for a
10277 			 * router. If that's true, then any off-net
10278 			 * IRE_CACHE entries that go through the router
10279 			 * with address "addr" must be clobbered. Use
10280 			 * ire_walk to achieve this goal.
10281 			 */
10282 			if (ifx_arp_ioctl)
10283 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10284 				    ire_delete_cache_gw, (char *)&addr, ill);
10285 			else
10286 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10287 				    ALL_ZONES, ipst);
10288 			iocp->ioc_error = 0;
10289 		}
10290 	}
10291 errack:
10292 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10293 		err = iocp->ioc_error;
10294 		freemsg(mp);
10295 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10296 		return;
10297 	}
10298 
10299 	/*
10300 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10301 	 * the area_t into the struct {x}arpreq.
10302 	 */
10303 	if (x_arp_ioctl) {
10304 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10305 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10306 		    sizeof (xar->xarp_ha.sdl_data)) {
10307 			freemsg(mp);
10308 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10309 			    NULL);
10310 			return;
10311 		}
10312 	}
10313 	*flagsp = ATF_INUSE;
10314 	if (area->area_flags & ACE_F_PERMANENT)
10315 		*flagsp |= ATF_PERM;
10316 	if (area->area_flags & ACE_F_PUBLISH)
10317 		*flagsp |= ATF_PUBL;
10318 	if (area->area_flags & ACE_F_AUTHORITY)
10319 		*flagsp |= ATF_AUTHORITY;
10320 	if (area->area_hw_addr_length != 0) {
10321 		*flagsp |= ATF_COM;
10322 		/*
10323 		 * For SIOCGARP, MAC address length validation has
10324 		 * already been done before the ioctl was issued to ARP
10325 		 * to allow it to progress only on 6 byte addressable
10326 		 * (ethernet like) media. Thus the mac address copying
10327 		 * can not overwrite the sa_data area below.
10328 		 */
10329 		bcopy((char *)area + area->area_hw_addr_offset,
10330 		    storage, area->area_hw_addr_length);
10331 	}
10332 
10333 	/* Ditch the internal IOCTL. */
10334 	freemsg(mp);
10335 	/* Complete the original. */
10336 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10337 }
10338 
10339 /*
10340  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10341  * interface) create the next available logical interface for this
10342  * physical interface.
10343  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10344  * ipif with the specified name.
10345  *
10346  * If the address family is not AF_UNSPEC then set the address as well.
10347  *
10348  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10349  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10350  *
10351  * Executed as a writer on the ill or ill group.
10352  * So no lock is needed to traverse the ipif chain, or examine the
10353  * phyint flags.
10354  */
10355 /* ARGSUSED */
10356 int
10357 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10358     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10359 {
10360 	mblk_t	*mp1;
10361 	struct lifreq *lifr;
10362 	boolean_t	isv6;
10363 	boolean_t	exists;
10364 	char 	*name;
10365 	char	*endp;
10366 	char	*cp;
10367 	int	namelen;
10368 	ipif_t	*ipif;
10369 	long	id;
10370 	ipsq_t	*ipsq;
10371 	ill_t	*ill;
10372 	sin_t	*sin;
10373 	int	err = 0;
10374 	boolean_t found_sep = B_FALSE;
10375 	conn_t	*connp;
10376 	zoneid_t zoneid;
10377 	int	orig_ifindex = 0;
10378 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10379 
10380 	ASSERT(q->q_next == NULL);
10381 	ip1dbg(("ip_sioctl_addif\n"));
10382 	/* Existence of mp1 has been checked in ip_wput_nondata */
10383 	mp1 = mp->b_cont->b_cont;
10384 	/*
10385 	 * Null terminate the string to protect against buffer
10386 	 * overrun. String was generated by user code and may not
10387 	 * be trusted.
10388 	 */
10389 	lifr = (struct lifreq *)mp1->b_rptr;
10390 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10391 	name = lifr->lifr_name;
10392 	ASSERT(CONN_Q(q));
10393 	connp = Q_TO_CONN(q);
10394 	isv6 = connp->conn_af_isv6;
10395 	zoneid = connp->conn_zoneid;
10396 	namelen = mi_strlen(name);
10397 	if (namelen == 0)
10398 		return (EINVAL);
10399 
10400 	exists = B_FALSE;
10401 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10402 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10403 		/*
10404 		 * Allow creating lo0 using SIOCLIFADDIF.
10405 		 * can't be any other writer thread. So can pass null below
10406 		 * for the last 4 args to ipif_lookup_name.
10407 		 */
10408 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10409 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10410 		/* Prevent any further action */
10411 		if (ipif == NULL) {
10412 			return (ENOBUFS);
10413 		} else if (!exists) {
10414 			/* We created the ipif now and as writer */
10415 			ipif_refrele(ipif);
10416 			return (0);
10417 		} else {
10418 			ill = ipif->ipif_ill;
10419 			ill_refhold(ill);
10420 			ipif_refrele(ipif);
10421 		}
10422 	} else {
10423 		/* Look for a colon in the name. */
10424 		endp = &name[namelen];
10425 		for (cp = endp; --cp > name; ) {
10426 			if (*cp == IPIF_SEPARATOR_CHAR) {
10427 				found_sep = B_TRUE;
10428 				/*
10429 				 * Reject any non-decimal aliases for plumbing
10430 				 * of logical interfaces. Aliases with leading
10431 				 * zeroes are also rejected as they introduce
10432 				 * ambiguity in the naming of the interfaces.
10433 				 * Comparing with "0" takes care of all such
10434 				 * cases.
10435 				 */
10436 				if ((strncmp("0", cp+1, 1)) == 0)
10437 					return (EINVAL);
10438 
10439 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10440 				    id <= 0 || *endp != '\0') {
10441 					return (EINVAL);
10442 				}
10443 				*cp = '\0';
10444 				break;
10445 			}
10446 		}
10447 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10448 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10449 		if (found_sep)
10450 			*cp = IPIF_SEPARATOR_CHAR;
10451 		if (ill == NULL)
10452 			return (err);
10453 	}
10454 
10455 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10456 	    B_TRUE);
10457 
10458 	/*
10459 	 * Release the refhold due to the lookup, now that we are excl
10460 	 * or we are just returning
10461 	 */
10462 	ill_refrele(ill);
10463 
10464 	if (ipsq == NULL)
10465 		return (EINPROGRESS);
10466 
10467 	/*
10468 	 * If the interface is failed, inactive or offlined, look for a working
10469 	 * interface in the ill group and create the ipif there. If we can't
10470 	 * find a good interface, create the ipif anyway so that in.mpathd can
10471 	 * move it to the first repaired interface.
10472 	 */
10473 	if ((ill->ill_phyint->phyint_flags &
10474 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10475 	    ill->ill_phyint->phyint_groupname_len != 0) {
10476 		phyint_t *phyi;
10477 		char *groupname = ill->ill_phyint->phyint_groupname;
10478 
10479 		/*
10480 		 * We're looking for a working interface, but it doesn't matter
10481 		 * if it's up or down; so instead of following the group lists,
10482 		 * we look at each physical interface and compare the groupname.
10483 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10484 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10485 		 * Otherwise we create the ipif on the failed interface.
10486 		 */
10487 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10488 		phyi = avl_first(&ipst->ips_phyint_g_list->
10489 		    phyint_list_avl_by_index);
10490 		for (; phyi != NULL;
10491 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10492 		    phyint_list_avl_by_index,
10493 		    phyi, AVL_AFTER)) {
10494 			if (phyi->phyint_groupname_len == 0)
10495 				continue;
10496 			ASSERT(phyi->phyint_groupname != NULL);
10497 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10498 			    !(phyi->phyint_flags &
10499 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10500 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10501 			    (phyi->phyint_illv4 != NULL))) {
10502 				break;
10503 			}
10504 		}
10505 		rw_exit(&ipst->ips_ill_g_lock);
10506 
10507 		if (phyi != NULL) {
10508 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10509 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10510 			    phyi->phyint_illv4);
10511 		}
10512 	}
10513 
10514 	/*
10515 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10516 	 * before or after us.
10517 	 */
10518 	ASSERT(IAM_WRITER_ILL(ill));
10519 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10520 
10521 	if (found_sep && orig_ifindex == 0) {
10522 		/* Now see if there is an IPIF with this unit number. */
10523 		for (ipif = ill->ill_ipif; ipif != NULL;
10524 		    ipif = ipif->ipif_next) {
10525 			if (ipif->ipif_id == id) {
10526 				err = EEXIST;
10527 				goto done;
10528 			}
10529 		}
10530 	}
10531 
10532 	/*
10533 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10534 	 * of lo0. We never come here when we plumb lo0:0. It
10535 	 * happens in ipif_lookup_on_name.
10536 	 * The specified unit number is ignored when we create the ipif on a
10537 	 * different interface. However, we save it in ipif_orig_ipifid below so
10538 	 * that the ipif fails back to the right position.
10539 	 */
10540 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10541 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10542 		err = ENOBUFS;
10543 		goto done;
10544 	}
10545 
10546 	/* Return created name with ioctl */
10547 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10548 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10549 	ip1dbg(("created %s\n", lifr->lifr_name));
10550 
10551 	/* Set address */
10552 	sin = (sin_t *)&lifr->lifr_addr;
10553 	if (sin->sin_family != AF_UNSPEC) {
10554 		err = ip_sioctl_addr(ipif, sin, q, mp,
10555 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10556 	}
10557 
10558 	/* Set ifindex and unit number for failback */
10559 	if (err == 0 && orig_ifindex != 0) {
10560 		ipif->ipif_orig_ifindex = orig_ifindex;
10561 		if (found_sep) {
10562 			ipif->ipif_orig_ipifid = id;
10563 		}
10564 	}
10565 
10566 done:
10567 	ipsq_exit(ipsq);
10568 	return (err);
10569 }
10570 
10571 /*
10572  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10573  * interface) delete it based on the IP address (on this physical interface).
10574  * Otherwise delete it based on the ipif_id.
10575  * Also, special handling to allow a removeif of lo0.
10576  */
10577 /* ARGSUSED */
10578 int
10579 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10580     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10581 {
10582 	conn_t		*connp;
10583 	ill_t		*ill = ipif->ipif_ill;
10584 	boolean_t	 success;
10585 	ip_stack_t	*ipst;
10586 
10587 	ipst = CONNQ_TO_IPST(q);
10588 
10589 	ASSERT(q->q_next == NULL);
10590 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10591 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10592 	ASSERT(IAM_WRITER_IPIF(ipif));
10593 
10594 	connp = Q_TO_CONN(q);
10595 	/*
10596 	 * Special case for unplumbing lo0 (the loopback physical interface).
10597 	 * If unplumbing lo0, the incoming address structure has been
10598 	 * initialized to all zeros. When unplumbing lo0, all its logical
10599 	 * interfaces must be removed too.
10600 	 *
10601 	 * Note that this interface may be called to remove a specific
10602 	 * loopback logical interface (eg, lo0:1). But in that case
10603 	 * ipif->ipif_id != 0 so that the code path for that case is the
10604 	 * same as any other interface (meaning it skips the code directly
10605 	 * below).
10606 	 */
10607 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10608 		if (sin->sin_family == AF_UNSPEC &&
10609 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10610 			/*
10611 			 * Mark it condemned. No new ref. will be made to ill.
10612 			 */
10613 			mutex_enter(&ill->ill_lock);
10614 			ill->ill_state_flags |= ILL_CONDEMNED;
10615 			for (ipif = ill->ill_ipif; ipif != NULL;
10616 			    ipif = ipif->ipif_next) {
10617 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10618 			}
10619 			mutex_exit(&ill->ill_lock);
10620 
10621 			ipif = ill->ill_ipif;
10622 			/* unplumb the loopback interface */
10623 			ill_delete(ill);
10624 			mutex_enter(&connp->conn_lock);
10625 			mutex_enter(&ill->ill_lock);
10626 			ASSERT(ill->ill_group == NULL);
10627 
10628 			/* Are any references to this ill active */
10629 			if (ill_is_freeable(ill)) {
10630 				mutex_exit(&ill->ill_lock);
10631 				mutex_exit(&connp->conn_lock);
10632 				ill_delete_tail(ill);
10633 				mi_free(ill);
10634 				return (0);
10635 			}
10636 			success = ipsq_pending_mp_add(connp, ipif,
10637 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10638 			mutex_exit(&connp->conn_lock);
10639 			mutex_exit(&ill->ill_lock);
10640 			if (success)
10641 				return (EINPROGRESS);
10642 			else
10643 				return (EINTR);
10644 		}
10645 	}
10646 
10647 	/*
10648 	 * We are exclusive on the ipsq, so an ill move will be serialized
10649 	 * before or after us.
10650 	 */
10651 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10652 
10653 	if (ipif->ipif_id == 0) {
10654 
10655 		ipsq_t *ipsq;
10656 
10657 		/* Find based on address */
10658 		if (ipif->ipif_isv6) {
10659 			sin6_t *sin6;
10660 
10661 			if (sin->sin_family != AF_INET6)
10662 				return (EAFNOSUPPORT);
10663 
10664 			sin6 = (sin6_t *)sin;
10665 			/* We are a writer, so we should be able to lookup */
10666 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10667 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10668 			if (ipif == NULL) {
10669 				/*
10670 				 * Maybe the address in on another interface in
10671 				 * the same IPMP group? We check this below.
10672 				 */
10673 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10674 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10675 				    ipst);
10676 			}
10677 		} else {
10678 			ipaddr_t addr;
10679 
10680 			if (sin->sin_family != AF_INET)
10681 				return (EAFNOSUPPORT);
10682 
10683 			addr = sin->sin_addr.s_addr;
10684 			/* We are a writer, so we should be able to lookup */
10685 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10686 			    NULL, NULL, NULL, ipst);
10687 			if (ipif == NULL) {
10688 				/*
10689 				 * Maybe the address in on another interface in
10690 				 * the same IPMP group? We check this below.
10691 				 */
10692 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10693 				    NULL, NULL, NULL, NULL, ipst);
10694 			}
10695 		}
10696 		if (ipif == NULL) {
10697 			return (EADDRNOTAVAIL);
10698 		}
10699 
10700 		/*
10701 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10702 		 * lifr_name of the physical interface but with an ip address
10703 		 * lifr_addr of a logical interface plumbed over it.
10704 		 * So update ipsq_current_ipif once ipif points to the
10705 		 * correct interface after doing ipif_lookup_addr().
10706 		 */
10707 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10708 		ASSERT(ipsq != NULL);
10709 
10710 		mutex_enter(&ipsq->ipsq_lock);
10711 		ipsq->ipsq_current_ipif = ipif;
10712 		mutex_exit(&ipsq->ipsq_lock);
10713 
10714 		/*
10715 		 * When the address to be removed is hosted on a different
10716 		 * interface, we check if the interface is in the same IPMP
10717 		 * group as the specified one; if so we proceed with the
10718 		 * removal.
10719 		 * ill->ill_group is NULL when the ill is down, so we have to
10720 		 * compare the group names instead.
10721 		 */
10722 		if (ipif->ipif_ill != ill &&
10723 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10724 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10725 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10726 		    ill->ill_phyint->phyint_groupname) != 0)) {
10727 			ipif_refrele(ipif);
10728 			return (EADDRNOTAVAIL);
10729 		}
10730 
10731 		/* This is a writer */
10732 		ipif_refrele(ipif);
10733 	}
10734 
10735 	/*
10736 	 * Can not delete instance zero since it is tied to the ill.
10737 	 */
10738 	if (ipif->ipif_id == 0)
10739 		return (EBUSY);
10740 
10741 	mutex_enter(&ill->ill_lock);
10742 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10743 	mutex_exit(&ill->ill_lock);
10744 
10745 	ipif_free(ipif);
10746 
10747 	mutex_enter(&connp->conn_lock);
10748 	mutex_enter(&ill->ill_lock);
10749 
10750 
10751 	/* Are any references to this ipif active */
10752 	if (ipif_is_freeable(ipif)) {
10753 		mutex_exit(&ill->ill_lock);
10754 		mutex_exit(&connp->conn_lock);
10755 		ipif_non_duplicate(ipif);
10756 		ipif_down_tail(ipif);
10757 		ipif_free_tail(ipif); /* frees ipif */
10758 		return (0);
10759 	}
10760 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10761 	    IPIF_FREE);
10762 	mutex_exit(&ill->ill_lock);
10763 	mutex_exit(&connp->conn_lock);
10764 	if (success)
10765 		return (EINPROGRESS);
10766 	else
10767 		return (EINTR);
10768 }
10769 
10770 /*
10771  * Restart the removeif ioctl. The refcnt has gone down to 0.
10772  * The ipif is already condemned. So can't find it thru lookups.
10773  */
10774 /* ARGSUSED */
10775 int
10776 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10777     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10778 {
10779 	ill_t *ill = ipif->ipif_ill;
10780 
10781 	ASSERT(IAM_WRITER_IPIF(ipif));
10782 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10783 
10784 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10785 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10786 
10787 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10788 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
10789 		ill_delete_tail(ill);
10790 		mi_free(ill);
10791 		return (0);
10792 	}
10793 
10794 	ipif_non_duplicate(ipif);
10795 	ipif_down_tail(ipif);
10796 	ipif_free_tail(ipif);
10797 
10798 	ILL_UNMARK_CHANGING(ill);
10799 	return (0);
10800 }
10801 
10802 /*
10803  * Set the local interface address.
10804  * Allow an address of all zero when the interface is down.
10805  */
10806 /* ARGSUSED */
10807 int
10808 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10809     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10810 {
10811 	int err = 0;
10812 	in6_addr_t v6addr;
10813 	boolean_t need_up = B_FALSE;
10814 
10815 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10816 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10817 
10818 	ASSERT(IAM_WRITER_IPIF(ipif));
10819 
10820 	if (ipif->ipif_isv6) {
10821 		sin6_t *sin6;
10822 		ill_t *ill;
10823 		phyint_t *phyi;
10824 
10825 		if (sin->sin_family != AF_INET6)
10826 			return (EAFNOSUPPORT);
10827 
10828 		sin6 = (sin6_t *)sin;
10829 		v6addr = sin6->sin6_addr;
10830 		ill = ipif->ipif_ill;
10831 		phyi = ill->ill_phyint;
10832 
10833 		/*
10834 		 * Enforce that true multicast interfaces have a link-local
10835 		 * address for logical unit 0.
10836 		 */
10837 		if (ipif->ipif_id == 0 &&
10838 		    (ill->ill_flags & ILLF_MULTICAST) &&
10839 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10840 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10841 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10842 			return (EADDRNOTAVAIL);
10843 		}
10844 
10845 		/*
10846 		 * up interfaces shouldn't have the unspecified address
10847 		 * unless they also have the IPIF_NOLOCAL flags set and
10848 		 * have a subnet assigned.
10849 		 */
10850 		if ((ipif->ipif_flags & IPIF_UP) &&
10851 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10852 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10853 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10854 			return (EADDRNOTAVAIL);
10855 		}
10856 
10857 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10858 			return (EADDRNOTAVAIL);
10859 	} else {
10860 		ipaddr_t addr;
10861 
10862 		if (sin->sin_family != AF_INET)
10863 			return (EAFNOSUPPORT);
10864 
10865 		addr = sin->sin_addr.s_addr;
10866 
10867 		/* Allow 0 as the local address. */
10868 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10869 			return (EADDRNOTAVAIL);
10870 
10871 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10872 	}
10873 
10874 	/*
10875 	 * Even if there is no change we redo things just to rerun
10876 	 * ipif_set_default.
10877 	 */
10878 	if (ipif->ipif_flags & IPIF_UP) {
10879 		/*
10880 		 * Setting a new local address, make sure
10881 		 * we have net and subnet bcast ire's for
10882 		 * the old address if we need them.
10883 		 */
10884 		if (!ipif->ipif_isv6)
10885 			ipif_check_bcast_ires(ipif);
10886 		/*
10887 		 * If the interface is already marked up,
10888 		 * we call ipif_down which will take care
10889 		 * of ditching any IREs that have been set
10890 		 * up based on the old interface address.
10891 		 */
10892 		err = ipif_logical_down(ipif, q, mp);
10893 		if (err == EINPROGRESS)
10894 			return (err);
10895 		ipif_down_tail(ipif);
10896 		need_up = 1;
10897 	}
10898 
10899 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10900 	return (err);
10901 }
10902 
10903 int
10904 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10905     boolean_t need_up)
10906 {
10907 	in6_addr_t v6addr;
10908 	in6_addr_t ov6addr;
10909 	ipaddr_t addr;
10910 	sin6_t	*sin6;
10911 	int	sinlen;
10912 	int	err = 0;
10913 	ill_t	*ill = ipif->ipif_ill;
10914 	boolean_t need_dl_down;
10915 	boolean_t need_arp_down;
10916 	struct iocblk *iocp;
10917 
10918 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
10919 
10920 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10921 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
10922 	ASSERT(IAM_WRITER_IPIF(ipif));
10923 
10924 	/* Must cancel any pending timer before taking the ill_lock */
10925 	if (ipif->ipif_recovery_id != 0)
10926 		(void) untimeout(ipif->ipif_recovery_id);
10927 	ipif->ipif_recovery_id = 0;
10928 
10929 	if (ipif->ipif_isv6) {
10930 		sin6 = (sin6_t *)sin;
10931 		v6addr = sin6->sin6_addr;
10932 		sinlen = sizeof (struct sockaddr_in6);
10933 	} else {
10934 		addr = sin->sin_addr.s_addr;
10935 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10936 		sinlen = sizeof (struct sockaddr_in);
10937 	}
10938 	mutex_enter(&ill->ill_lock);
10939 	ov6addr = ipif->ipif_v6lcl_addr;
10940 	ipif->ipif_v6lcl_addr = v6addr;
10941 	sctp_update_ipif_addr(ipif, ov6addr);
10942 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10943 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10944 	} else {
10945 		ipif->ipif_v6src_addr = v6addr;
10946 	}
10947 	ipif->ipif_addr_ready = 0;
10948 
10949 	/*
10950 	 * If the interface was previously marked as a duplicate, then since
10951 	 * we've now got a "new" address, it should no longer be considered a
10952 	 * duplicate -- even if the "new" address is the same as the old one.
10953 	 * Note that if all ipifs are down, we may have a pending ARP down
10954 	 * event to handle.  This is because we want to recover from duplicates
10955 	 * and thus delay tearing down ARP until the duplicates have been
10956 	 * removed or disabled.
10957 	 */
10958 	need_dl_down = need_arp_down = B_FALSE;
10959 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10960 		need_arp_down = !need_up;
10961 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10962 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10963 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10964 			need_dl_down = B_TRUE;
10965 		}
10966 	}
10967 
10968 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
10969 	    !ill->ill_is_6to4tun) {
10970 		queue_t *wqp = ill->ill_wq;
10971 
10972 		/*
10973 		 * The local address of this interface is a 6to4 address,
10974 		 * check if this interface is in fact a 6to4 tunnel or just
10975 		 * an interface configured with a 6to4 address.  We are only
10976 		 * interested in the former.
10977 		 */
10978 		if (wqp != NULL) {
10979 			while ((wqp->q_next != NULL) &&
10980 			    (wqp->q_next->q_qinfo != NULL) &&
10981 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10982 
10983 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10984 				    == TUN6TO4_MODID) {
10985 					/* set for use in IP */
10986 					ill->ill_is_6to4tun = 1;
10987 					break;
10988 				}
10989 				wqp = wqp->q_next;
10990 			}
10991 		}
10992 	}
10993 
10994 	ipif_set_default(ipif);
10995 
10996 	/*
10997 	 * When publishing an interface address change event, we only notify
10998 	 * the event listeners of the new address.  It is assumed that if they
10999 	 * actively care about the addresses assigned that they will have
11000 	 * already discovered the previous address assigned (if there was one.)
11001 	 *
11002 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11003 	 */
11004 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11005 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
11006 		    NE_ADDRESS_CHANGE, sin, sinlen);
11007 	}
11008 
11009 	mutex_exit(&ill->ill_lock);
11010 
11011 	if (need_up) {
11012 		/*
11013 		 * Now bring the interface back up.  If this
11014 		 * is the only IPIF for the ILL, ipif_up
11015 		 * will have to re-bind to the device, so
11016 		 * we may get back EINPROGRESS, in which
11017 		 * case, this IOCTL will get completed in
11018 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11019 		 */
11020 		err = ipif_up(ipif, q, mp);
11021 	}
11022 
11023 	if (need_dl_down)
11024 		ill_dl_down(ill);
11025 	if (need_arp_down)
11026 		ipif_arp_down(ipif);
11027 
11028 	return (err);
11029 }
11030 
11031 
11032 /*
11033  * Restart entry point to restart the address set operation after the
11034  * refcounts have dropped to zero.
11035  */
11036 /* ARGSUSED */
11037 int
11038 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11039     ip_ioctl_cmd_t *ipip, void *ifreq)
11040 {
11041 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11042 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11043 	ASSERT(IAM_WRITER_IPIF(ipif));
11044 	ipif_down_tail(ipif);
11045 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11046 }
11047 
11048 /* ARGSUSED */
11049 int
11050 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11051     ip_ioctl_cmd_t *ipip, void *if_req)
11052 {
11053 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11054 	struct lifreq *lifr = (struct lifreq *)if_req;
11055 
11056 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11057 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11058 	/*
11059 	 * The net mask and address can't change since we have a
11060 	 * reference to the ipif. So no lock is necessary.
11061 	 */
11062 	if (ipif->ipif_isv6) {
11063 		*sin6 = sin6_null;
11064 		sin6->sin6_family = AF_INET6;
11065 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11066 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11067 		lifr->lifr_addrlen =
11068 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11069 	} else {
11070 		*sin = sin_null;
11071 		sin->sin_family = AF_INET;
11072 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11073 		if (ipip->ipi_cmd_type == LIF_CMD) {
11074 			lifr->lifr_addrlen =
11075 			    ip_mask_to_plen(ipif->ipif_net_mask);
11076 		}
11077 	}
11078 	return (0);
11079 }
11080 
11081 /*
11082  * Set the destination address for a pt-pt interface.
11083  */
11084 /* ARGSUSED */
11085 int
11086 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11087     ip_ioctl_cmd_t *ipip, void *if_req)
11088 {
11089 	int err = 0;
11090 	in6_addr_t v6addr;
11091 	boolean_t need_up = B_FALSE;
11092 
11093 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11094 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11095 	ASSERT(IAM_WRITER_IPIF(ipif));
11096 
11097 	if (ipif->ipif_isv6) {
11098 		sin6_t *sin6;
11099 
11100 		if (sin->sin_family != AF_INET6)
11101 			return (EAFNOSUPPORT);
11102 
11103 		sin6 = (sin6_t *)sin;
11104 		v6addr = sin6->sin6_addr;
11105 
11106 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11107 			return (EADDRNOTAVAIL);
11108 	} else {
11109 		ipaddr_t addr;
11110 
11111 		if (sin->sin_family != AF_INET)
11112 			return (EAFNOSUPPORT);
11113 
11114 		addr = sin->sin_addr.s_addr;
11115 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11116 			return (EADDRNOTAVAIL);
11117 
11118 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11119 	}
11120 
11121 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11122 		return (0);	/* No change */
11123 
11124 	if (ipif->ipif_flags & IPIF_UP) {
11125 		/*
11126 		 * If the interface is already marked up,
11127 		 * we call ipif_down which will take care
11128 		 * of ditching any IREs that have been set
11129 		 * up based on the old pp dst address.
11130 		 */
11131 		err = ipif_logical_down(ipif, q, mp);
11132 		if (err == EINPROGRESS)
11133 			return (err);
11134 		ipif_down_tail(ipif);
11135 		need_up = B_TRUE;
11136 	}
11137 	/*
11138 	 * could return EINPROGRESS. If so ioctl will complete in
11139 	 * ip_rput_dlpi_writer
11140 	 */
11141 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11142 	return (err);
11143 }
11144 
11145 static int
11146 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11147     boolean_t need_up)
11148 {
11149 	in6_addr_t v6addr;
11150 	ill_t	*ill = ipif->ipif_ill;
11151 	int	err = 0;
11152 	boolean_t need_dl_down;
11153 	boolean_t need_arp_down;
11154 
11155 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11156 	    ipif->ipif_id, (void *)ipif));
11157 
11158 	/* Must cancel any pending timer before taking the ill_lock */
11159 	if (ipif->ipif_recovery_id != 0)
11160 		(void) untimeout(ipif->ipif_recovery_id);
11161 	ipif->ipif_recovery_id = 0;
11162 
11163 	if (ipif->ipif_isv6) {
11164 		sin6_t *sin6;
11165 
11166 		sin6 = (sin6_t *)sin;
11167 		v6addr = sin6->sin6_addr;
11168 	} else {
11169 		ipaddr_t addr;
11170 
11171 		addr = sin->sin_addr.s_addr;
11172 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11173 	}
11174 	mutex_enter(&ill->ill_lock);
11175 	/* Set point to point destination address. */
11176 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11177 		/*
11178 		 * Allow this as a means of creating logical
11179 		 * pt-pt interfaces on top of e.g. an Ethernet.
11180 		 * XXX Undocumented HACK for testing.
11181 		 * pt-pt interfaces are created with NUD disabled.
11182 		 */
11183 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11184 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11185 		if (ipif->ipif_isv6)
11186 			ill->ill_flags |= ILLF_NONUD;
11187 	}
11188 
11189 	/*
11190 	 * If the interface was previously marked as a duplicate, then since
11191 	 * we've now got a "new" address, it should no longer be considered a
11192 	 * duplicate -- even if the "new" address is the same as the old one.
11193 	 * Note that if all ipifs are down, we may have a pending ARP down
11194 	 * event to handle.
11195 	 */
11196 	need_dl_down = need_arp_down = B_FALSE;
11197 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11198 		need_arp_down = !need_up;
11199 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11200 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11201 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11202 			need_dl_down = B_TRUE;
11203 		}
11204 	}
11205 
11206 	/* Set the new address. */
11207 	ipif->ipif_v6pp_dst_addr = v6addr;
11208 	/* Make sure subnet tracks pp_dst */
11209 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11210 	mutex_exit(&ill->ill_lock);
11211 
11212 	if (need_up) {
11213 		/*
11214 		 * Now bring the interface back up.  If this
11215 		 * is the only IPIF for the ILL, ipif_up
11216 		 * will have to re-bind to the device, so
11217 		 * we may get back EINPROGRESS, in which
11218 		 * case, this IOCTL will get completed in
11219 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11220 		 */
11221 		err = ipif_up(ipif, q, mp);
11222 	}
11223 
11224 	if (need_dl_down)
11225 		ill_dl_down(ill);
11226 
11227 	if (need_arp_down)
11228 		ipif_arp_down(ipif);
11229 	return (err);
11230 }
11231 
11232 /*
11233  * Restart entry point to restart the dstaddress set operation after the
11234  * refcounts have dropped to zero.
11235  */
11236 /* ARGSUSED */
11237 int
11238 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11239     ip_ioctl_cmd_t *ipip, void *ifreq)
11240 {
11241 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11242 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11243 	ipif_down_tail(ipif);
11244 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11245 }
11246 
11247 /* ARGSUSED */
11248 int
11249 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11250     ip_ioctl_cmd_t *ipip, void *if_req)
11251 {
11252 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11253 
11254 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11255 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11256 	/*
11257 	 * Get point to point destination address. The addresses can't
11258 	 * change since we hold a reference to the ipif.
11259 	 */
11260 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11261 		return (EADDRNOTAVAIL);
11262 
11263 	if (ipif->ipif_isv6) {
11264 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11265 		*sin6 = sin6_null;
11266 		sin6->sin6_family = AF_INET6;
11267 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11268 	} else {
11269 		*sin = sin_null;
11270 		sin->sin_family = AF_INET;
11271 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11272 	}
11273 	return (0);
11274 }
11275 
11276 /*
11277  * part of ipmp, make this func return the active/inactive state and
11278  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11279  */
11280 /*
11281  * This function either sets or clears the IFF_INACTIVE flag.
11282  *
11283  * As long as there are some addresses or multicast memberships on the
11284  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11285  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11286  * will be used for outbound packets.
11287  *
11288  * Caller needs to verify the validity of setting IFF_INACTIVE.
11289  */
11290 static void
11291 phyint_inactive(phyint_t *phyi)
11292 {
11293 	ill_t *ill_v4;
11294 	ill_t *ill_v6;
11295 	ipif_t *ipif;
11296 	ilm_t *ilm;
11297 
11298 	ill_v4 = phyi->phyint_illv4;
11299 	ill_v6 = phyi->phyint_illv6;
11300 
11301 	/*
11302 	 * No need for a lock while traversing the list since iam
11303 	 * a writer
11304 	 */
11305 	if (ill_v4 != NULL) {
11306 		ASSERT(IAM_WRITER_ILL(ill_v4));
11307 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11308 		    ipif = ipif->ipif_next) {
11309 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11310 				mutex_enter(&phyi->phyint_lock);
11311 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11312 				mutex_exit(&phyi->phyint_lock);
11313 				return;
11314 			}
11315 		}
11316 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11317 		    ilm = ilm->ilm_next) {
11318 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11319 				mutex_enter(&phyi->phyint_lock);
11320 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11321 				mutex_exit(&phyi->phyint_lock);
11322 				return;
11323 			}
11324 		}
11325 	}
11326 	if (ill_v6 != NULL) {
11327 		ill_v6 = phyi->phyint_illv6;
11328 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11329 		    ipif = ipif->ipif_next) {
11330 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11331 				mutex_enter(&phyi->phyint_lock);
11332 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11333 				mutex_exit(&phyi->phyint_lock);
11334 				return;
11335 			}
11336 		}
11337 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11338 		    ilm = ilm->ilm_next) {
11339 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11340 				mutex_enter(&phyi->phyint_lock);
11341 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11342 				mutex_exit(&phyi->phyint_lock);
11343 				return;
11344 			}
11345 		}
11346 	}
11347 	mutex_enter(&phyi->phyint_lock);
11348 	phyi->phyint_flags |= PHYI_INACTIVE;
11349 	mutex_exit(&phyi->phyint_lock);
11350 }
11351 
11352 /*
11353  * This function is called only when the phyint flags change. Currently
11354  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11355  * that we can select a good ill.
11356  */
11357 static void
11358 ip_redo_nomination(phyint_t *phyi)
11359 {
11360 	ill_t *ill_v4;
11361 
11362 	ill_v4 = phyi->phyint_illv4;
11363 
11364 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11365 		ASSERT(IAM_WRITER_ILL(ill_v4));
11366 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11367 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11368 	}
11369 }
11370 
11371 /*
11372  * Heuristic to check if ill is INACTIVE.
11373  * Checks if ill has an ipif with an usable ip address.
11374  *
11375  * Return values:
11376  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11377  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11378  */
11379 static boolean_t
11380 ill_is_inactive(ill_t *ill)
11381 {
11382 	ipif_t *ipif;
11383 
11384 	/* Check whether it is in an IPMP group */
11385 	if (ill->ill_phyint->phyint_groupname == NULL)
11386 		return (B_FALSE);
11387 
11388 	if (ill->ill_ipif_up_count == 0)
11389 		return (B_TRUE);
11390 
11391 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11392 		uint64_t flags = ipif->ipif_flags;
11393 
11394 		/*
11395 		 * This ipif is usable if it is IPIF_UP and not a
11396 		 * dedicated test address.  A dedicated test address
11397 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11398 		 * (note in particular that V6 test addresses are
11399 		 * link-local data addresses and thus are marked
11400 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11401 		 */
11402 		if ((flags & IPIF_UP) &&
11403 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11404 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11405 			return (B_FALSE);
11406 	}
11407 	return (B_TRUE);
11408 }
11409 
11410 /*
11411  * Set interface flags.
11412  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11413  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11414  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11415  *
11416  * NOTE : We really don't enforce that ipif_id zero should be used
11417  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11418  *	  is because applications generally does SICGLIFFLAGS and
11419  *	  ORs in the new flags (that affects the logical) and does a
11420  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11421  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11422  *	  flags that will be turned on is correct with respect to
11423  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11424  */
11425 /* ARGSUSED */
11426 int
11427 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11428     ip_ioctl_cmd_t *ipip, void *if_req)
11429 {
11430 	uint64_t turn_on;
11431 	uint64_t turn_off;
11432 	int	err;
11433 	phyint_t *phyi;
11434 	ill_t *ill;
11435 	uint64_t intf_flags;
11436 	boolean_t phyint_flags_modified = B_FALSE;
11437 	uint64_t flags;
11438 	struct ifreq *ifr;
11439 	struct lifreq *lifr;
11440 	boolean_t set_linklocal = B_FALSE;
11441 	boolean_t zero_source = B_FALSE;
11442 	ip_stack_t *ipst;
11443 
11444 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11445 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11446 
11447 	ASSERT(IAM_WRITER_IPIF(ipif));
11448 
11449 	ill = ipif->ipif_ill;
11450 	phyi = ill->ill_phyint;
11451 	ipst = ill->ill_ipst;
11452 
11453 	if (ipip->ipi_cmd_type == IF_CMD) {
11454 		ifr = (struct ifreq *)if_req;
11455 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11456 	} else {
11457 		lifr = (struct lifreq *)if_req;
11458 		flags = lifr->lifr_flags;
11459 	}
11460 
11461 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11462 
11463 	/*
11464 	 * Have the flags been set correctly until now?
11465 	 */
11466 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11467 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11468 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11469 	/*
11470 	 * Compare the new flags to the old, and partition
11471 	 * into those coming on and those going off.
11472 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11473 	 */
11474 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11475 		flags |= intf_flags & ~0xFFFF;
11476 
11477 	/*
11478 	 * First check which bits will change and then which will
11479 	 * go on and off
11480 	 */
11481 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11482 	if (!turn_on)
11483 		return (0);	/* No change */
11484 
11485 	turn_off = intf_flags & turn_on;
11486 	turn_on ^= turn_off;
11487 	err = 0;
11488 
11489 	/*
11490 	 * Don't allow any bits belonging to the logical interface
11491 	 * to be set or cleared on the replacement ipif that was
11492 	 * created temporarily during a MOVE.
11493 	 */
11494 	if (ipif->ipif_replace_zero &&
11495 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11496 		return (EINVAL);
11497 	}
11498 
11499 	/*
11500 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11501 	 * IPv6 interfaces.
11502 	 */
11503 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11504 		return (EINVAL);
11505 
11506 	/*
11507 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11508 	 */
11509 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11510 		return (EINVAL);
11511 
11512 	/*
11513 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11514 	 * interfaces.  It makes no sense in that context.
11515 	 */
11516 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11517 		return (EINVAL);
11518 
11519 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11520 		zero_source = B_TRUE;
11521 
11522 	/*
11523 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11524 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11525 	 * If the link local address isn't set, and can be set, it will get
11526 	 * set later on in this function.
11527 	 */
11528 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11529 	    (flags & IFF_UP) && !zero_source &&
11530 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11531 		if (ipif_cant_setlinklocal(ipif))
11532 			return (EINVAL);
11533 		set_linklocal = B_TRUE;
11534 	}
11535 
11536 	/*
11537 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11538 	 * same time. No need to grab ill_g_usesrc_lock here, see
11539 	 * synchronization notes in ip.c
11540 	 */
11541 	if (turn_on & PHYI_STANDBY &&
11542 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11543 		return (EINVAL);
11544 	}
11545 
11546 	/*
11547 	 * If we modify physical interface flags, we'll potentially need to
11548 	 * send up two routing socket messages for the changes (one for the
11549 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11550 	 */
11551 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11552 		phyint_flags_modified = B_TRUE;
11553 
11554 	/*
11555 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11556 	 * we need to flush the IRE_CACHES belonging to this ill.
11557 	 * We handle this case here without doing the DOWN/UP dance
11558 	 * like it is done for other flags. If some other flags are
11559 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11560 	 * below will handle it by bringing it down and then
11561 	 * bringing it UP.
11562 	 */
11563 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11564 		ill_t *ill_v4, *ill_v6;
11565 
11566 		ill_v4 = phyi->phyint_illv4;
11567 		ill_v6 = phyi->phyint_illv6;
11568 
11569 		/*
11570 		 * First set the INACTIVE flag if needed. Then delete the ires.
11571 		 * ire_add will atomically prevent creating new IRE_CACHEs
11572 		 * unless hidden flag is set.
11573 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11574 		 */
11575 		if ((turn_on & PHYI_FAILED) &&
11576 		    ((intf_flags & PHYI_STANDBY) ||
11577 		    !ipst->ips_ipmp_enable_failback)) {
11578 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11579 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11580 		}
11581 		if ((turn_off & PHYI_FAILED) &&
11582 		    ((intf_flags & PHYI_STANDBY) ||
11583 		    (!ipst->ips_ipmp_enable_failback &&
11584 		    ill_is_inactive(ill)))) {
11585 			phyint_inactive(phyi);
11586 		}
11587 
11588 		if (turn_on & PHYI_STANDBY) {
11589 			/*
11590 			 * We implicitly set INACTIVE only when STANDBY is set.
11591 			 * INACTIVE is also set on non-STANDBY phyint when user
11592 			 * disables FAILBACK using configuration file.
11593 			 * Do not allow STANDBY to be set on such INACTIVE
11594 			 * phyint
11595 			 */
11596 			if (phyi->phyint_flags & PHYI_INACTIVE)
11597 				return (EINVAL);
11598 			if (!(phyi->phyint_flags & PHYI_FAILED))
11599 				phyint_inactive(phyi);
11600 		}
11601 		if (turn_off & PHYI_STANDBY) {
11602 			if (ipst->ips_ipmp_enable_failback) {
11603 				/*
11604 				 * Reset PHYI_INACTIVE.
11605 				 */
11606 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11607 			} else if (ill_is_inactive(ill) &&
11608 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11609 				/*
11610 				 * Need to set INACTIVE, when user sets
11611 				 * STANDBY on a non-STANDBY phyint and
11612 				 * later resets STANDBY
11613 				 */
11614 				phyint_inactive(phyi);
11615 			}
11616 		}
11617 		/*
11618 		 * We should always send up a message so that the
11619 		 * daemons come to know of it. Note that the zeroth
11620 		 * interface can be down and the check below for IPIF_UP
11621 		 * will not make sense as we are actually setting
11622 		 * a phyint flag here. We assume that the ipif used
11623 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11624 		 * send up any message for non-zero ipifs).
11625 		 */
11626 		phyint_flags_modified = B_TRUE;
11627 
11628 		if (ill_v4 != NULL) {
11629 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11630 			    IRE_CACHE, ill_stq_cache_delete,
11631 			    (char *)ill_v4, ill_v4);
11632 			illgrp_reset_schednext(ill_v4);
11633 		}
11634 		if (ill_v6 != NULL) {
11635 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11636 			    IRE_CACHE, ill_stq_cache_delete,
11637 			    (char *)ill_v6, ill_v6);
11638 			illgrp_reset_schednext(ill_v6);
11639 		}
11640 	}
11641 
11642 	/*
11643 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11644 	 * status of the interface and, if the interface is part of an IPMP
11645 	 * group, all other interfaces that are part of the same IPMP
11646 	 * group.
11647 	 */
11648 	if ((turn_on | turn_off) & ILLF_ROUTER)
11649 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11650 
11651 	/*
11652 	 * If the interface is not UP and we are not going to
11653 	 * bring it UP, record the flags and return. When the
11654 	 * interface comes UP later, the right actions will be
11655 	 * taken.
11656 	 */
11657 	if (!(ipif->ipif_flags & IPIF_UP) &&
11658 	    !(turn_on & IPIF_UP)) {
11659 		/* Record new flags in their respective places. */
11660 		mutex_enter(&ill->ill_lock);
11661 		mutex_enter(&ill->ill_phyint->phyint_lock);
11662 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11663 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11664 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11665 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11666 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11667 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11668 		mutex_exit(&ill->ill_lock);
11669 		mutex_exit(&ill->ill_phyint->phyint_lock);
11670 
11671 		/*
11672 		 * We do the broadcast and nomination here rather
11673 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11674 		 * the case of FAILBACK from INACTIVE standby to the
11675 		 * interface that has been repaired, PHYI_FAILED has not
11676 		 * been cleared yet. If there are only two interfaces in
11677 		 * that group, all we have is a FAILED and INACTIVE
11678 		 * interface. If we do the nomination soon after a failback,
11679 		 * the broadcast nomination code would select the
11680 		 * INACTIVE interface for receiving broadcasts as FAILED is
11681 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11682 		 * receive broadcast packets, we need to redo nomination
11683 		 * when the FAILED is cleared here. Thus, in general we
11684 		 * always do the nomination here for FAILED, STANDBY
11685 		 * and OFFLINE.
11686 		 */
11687 		if (((turn_on | turn_off) &
11688 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11689 			ip_redo_nomination(phyi);
11690 		}
11691 		if (phyint_flags_modified) {
11692 			if (phyi->phyint_illv4 != NULL) {
11693 				ip_rts_ifmsg(phyi->phyint_illv4->
11694 				    ill_ipif);
11695 			}
11696 			if (phyi->phyint_illv6 != NULL) {
11697 				ip_rts_ifmsg(phyi->phyint_illv6->
11698 				    ill_ipif);
11699 			}
11700 		}
11701 		return (0);
11702 	} else if (set_linklocal || zero_source) {
11703 		mutex_enter(&ill->ill_lock);
11704 		if (set_linklocal)
11705 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11706 		if (zero_source)
11707 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11708 		mutex_exit(&ill->ill_lock);
11709 	}
11710 
11711 	/*
11712 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11713 	 * or point-to-point interfaces with an unspecified destination. We do
11714 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11715 	 * have a subnet assigned, which is how in.ndpd currently manages its
11716 	 * onlink prefix list when no addresses are configured with those
11717 	 * prefixes.
11718 	 */
11719 	if (ipif->ipif_isv6 &&
11720 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11721 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11722 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11723 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11724 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11725 		return (EINVAL);
11726 	}
11727 
11728 	/*
11729 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11730 	 * from being brought up.
11731 	 */
11732 	if (!ipif->ipif_isv6 &&
11733 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11734 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11735 		return (EINVAL);
11736 	}
11737 
11738 	/*
11739 	 * The only flag changes that we currently take specific action on
11740 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11741 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11742 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11743 	 * the flags and bringing it back up again.
11744 	 */
11745 	if ((turn_on|turn_off) &
11746 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11747 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11748 		/*
11749 		 * Taking this ipif down, make sure we have
11750 		 * valid net and subnet bcast ire's for other
11751 		 * logical interfaces, if we need them.
11752 		 */
11753 		if (!ipif->ipif_isv6)
11754 			ipif_check_bcast_ires(ipif);
11755 
11756 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11757 		    !(turn_off & IPIF_UP)) {
11758 			if (ipif->ipif_flags & IPIF_UP)
11759 				ill->ill_logical_down = 1;
11760 			turn_on &= ~IPIF_UP;
11761 		}
11762 		err = ipif_down(ipif, q, mp);
11763 		ip1dbg(("ipif_down returns %d err ", err));
11764 		if (err == EINPROGRESS)
11765 			return (err);
11766 		ipif_down_tail(ipif);
11767 	}
11768 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11769 }
11770 
11771 static int
11772 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
11773 {
11774 	ill_t	*ill;
11775 	phyint_t *phyi;
11776 	uint64_t turn_on;
11777 	uint64_t turn_off;
11778 	uint64_t intf_flags;
11779 	boolean_t phyint_flags_modified = B_FALSE;
11780 	int	err = 0;
11781 	boolean_t set_linklocal = B_FALSE;
11782 	boolean_t zero_source = B_FALSE;
11783 
11784 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11785 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
11786 
11787 	ASSERT(IAM_WRITER_IPIF(ipif));
11788 
11789 	ill = ipif->ipif_ill;
11790 	phyi = ill->ill_phyint;
11791 
11792 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11793 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11794 
11795 	turn_off = intf_flags & turn_on;
11796 	turn_on ^= turn_off;
11797 
11798 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11799 		phyint_flags_modified = B_TRUE;
11800 
11801 	/*
11802 	 * Now we change the flags. Track current value of
11803 	 * other flags in their respective places.
11804 	 */
11805 	mutex_enter(&ill->ill_lock);
11806 	mutex_enter(&phyi->phyint_lock);
11807 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11808 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11809 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11810 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11811 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11812 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11813 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11814 		set_linklocal = B_TRUE;
11815 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11816 	}
11817 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11818 		zero_source = B_TRUE;
11819 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11820 	}
11821 	mutex_exit(&ill->ill_lock);
11822 	mutex_exit(&phyi->phyint_lock);
11823 
11824 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11825 		ip_redo_nomination(phyi);
11826 
11827 	if (set_linklocal)
11828 		(void) ipif_setlinklocal(ipif);
11829 
11830 	if (zero_source)
11831 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11832 	else
11833 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11834 
11835 	if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
11836 		/*
11837 		 * XXX ipif_up really does not know whether a phyint flags
11838 		 * was modified or not. So, it sends up information on
11839 		 * only one routing sockets message. As we don't bring up
11840 		 * the interface and also set STANDBY/FAILED simultaneously
11841 		 * it should be okay.
11842 		 */
11843 		err = ipif_up(ipif, q, mp);
11844 	} else {
11845 		/*
11846 		 * Make sure routing socket sees all changes to the flags.
11847 		 * ipif_up_done* handles this when we use ipif_up.
11848 		 */
11849 		if (phyint_flags_modified) {
11850 			if (phyi->phyint_illv4 != NULL) {
11851 				ip_rts_ifmsg(phyi->phyint_illv4->
11852 				    ill_ipif);
11853 			}
11854 			if (phyi->phyint_illv6 != NULL) {
11855 				ip_rts_ifmsg(phyi->phyint_illv6->
11856 				    ill_ipif);
11857 			}
11858 		} else {
11859 			ip_rts_ifmsg(ipif);
11860 		}
11861 		/*
11862 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
11863 		 * this in need_up case.
11864 		 */
11865 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11866 	}
11867 	return (err);
11868 }
11869 
11870 /*
11871  * Restart the flags operation now that the refcounts have dropped to zero.
11872  */
11873 /* ARGSUSED */
11874 int
11875 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11876     ip_ioctl_cmd_t *ipip, void *if_req)
11877 {
11878 	uint64_t flags;
11879 	struct ifreq *ifr = if_req;
11880 	struct lifreq *lifr = if_req;
11881 
11882 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11883 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11884 
11885 	ipif_down_tail(ipif);
11886 	if (ipip->ipi_cmd_type == IF_CMD) {
11887 		/* cast to uint16_t prevents unwanted sign extension */
11888 		flags = (uint16_t)ifr->ifr_flags;
11889 	} else {
11890 		flags = lifr->lifr_flags;
11891 	}
11892 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
11893 }
11894 
11895 /*
11896  * Can operate on either a module or a driver queue.
11897  */
11898 /* ARGSUSED */
11899 int
11900 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11901     ip_ioctl_cmd_t *ipip, void *if_req)
11902 {
11903 	/*
11904 	 * Has the flags been set correctly till now ?
11905 	 */
11906 	ill_t *ill = ipif->ipif_ill;
11907 	phyint_t *phyi = ill->ill_phyint;
11908 
11909 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11910 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11911 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11912 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11913 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11914 
11915 	/*
11916 	 * Need a lock since some flags can be set even when there are
11917 	 * references to the ipif.
11918 	 */
11919 	mutex_enter(&ill->ill_lock);
11920 	if (ipip->ipi_cmd_type == IF_CMD) {
11921 		struct ifreq *ifr = (struct ifreq *)if_req;
11922 
11923 		/* Get interface flags (low 16 only). */
11924 		ifr->ifr_flags = ((ipif->ipif_flags |
11925 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11926 	} else {
11927 		struct lifreq *lifr = (struct lifreq *)if_req;
11928 
11929 		/* Get interface flags. */
11930 		lifr->lifr_flags = ipif->ipif_flags |
11931 		    ill->ill_flags | phyi->phyint_flags;
11932 	}
11933 	mutex_exit(&ill->ill_lock);
11934 	return (0);
11935 }
11936 
11937 /* ARGSUSED */
11938 int
11939 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11940     ip_ioctl_cmd_t *ipip, void *if_req)
11941 {
11942 	int mtu;
11943 	int ip_min_mtu;
11944 	struct ifreq	*ifr;
11945 	struct lifreq *lifr;
11946 	ire_t	*ire;
11947 	ip_stack_t *ipst;
11948 
11949 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11950 	    ipif->ipif_id, (void *)ipif));
11951 	if (ipip->ipi_cmd_type == IF_CMD) {
11952 		ifr = (struct ifreq *)if_req;
11953 		mtu = ifr->ifr_metric;
11954 	} else {
11955 		lifr = (struct lifreq *)if_req;
11956 		mtu = lifr->lifr_mtu;
11957 	}
11958 
11959 	if (ipif->ipif_isv6)
11960 		ip_min_mtu = IPV6_MIN_MTU;
11961 	else
11962 		ip_min_mtu = IP_MIN_MTU;
11963 
11964 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11965 		return (EINVAL);
11966 
11967 	/*
11968 	 * Change the MTU size in all relevant ire's.
11969 	 * Mtu change Vs. new ire creation - protocol below.
11970 	 * First change ipif_mtu and the ire_max_frag of the
11971 	 * interface ire. Then do an ire walk and change the
11972 	 * ire_max_frag of all affected ires. During ire_add
11973 	 * under the bucket lock, set the ire_max_frag of the
11974 	 * new ire being created from the ipif/ire from which
11975 	 * it is being derived. If an mtu change happens after
11976 	 * the ire is added, the new ire will be cleaned up.
11977 	 * Conversely if the mtu change happens before the ire
11978 	 * is added, ire_add will see the new value of the mtu.
11979 	 */
11980 	ipif->ipif_mtu = mtu;
11981 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11982 
11983 	if (ipif->ipif_isv6)
11984 		ire = ipif_to_ire_v6(ipif);
11985 	else
11986 		ire = ipif_to_ire(ipif);
11987 	if (ire != NULL) {
11988 		ire->ire_max_frag = ipif->ipif_mtu;
11989 		ire_refrele(ire);
11990 	}
11991 	ipst = ipif->ipif_ill->ill_ipst;
11992 	if (ipif->ipif_flags & IPIF_UP) {
11993 		if (ipif->ipif_isv6)
11994 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11995 			    ipst);
11996 		else
11997 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
11998 			    ipst);
11999 	}
12000 	/* Update the MTU in SCTP's list */
12001 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12002 	return (0);
12003 }
12004 
12005 /* Get interface MTU. */
12006 /* ARGSUSED */
12007 int
12008 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12009 	ip_ioctl_cmd_t *ipip, void *if_req)
12010 {
12011 	struct ifreq	*ifr;
12012 	struct lifreq	*lifr;
12013 
12014 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12015 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12016 	if (ipip->ipi_cmd_type == IF_CMD) {
12017 		ifr = (struct ifreq *)if_req;
12018 		ifr->ifr_metric = ipif->ipif_mtu;
12019 	} else {
12020 		lifr = (struct lifreq *)if_req;
12021 		lifr->lifr_mtu = ipif->ipif_mtu;
12022 	}
12023 	return (0);
12024 }
12025 
12026 /* Set interface broadcast address. */
12027 /* ARGSUSED2 */
12028 int
12029 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12030 	ip_ioctl_cmd_t *ipip, void *if_req)
12031 {
12032 	ipaddr_t addr;
12033 	ire_t	*ire;
12034 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12035 
12036 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12037 	    ipif->ipif_id));
12038 
12039 	ASSERT(IAM_WRITER_IPIF(ipif));
12040 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12041 		return (EADDRNOTAVAIL);
12042 
12043 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12044 
12045 	if (sin->sin_family != AF_INET)
12046 		return (EAFNOSUPPORT);
12047 
12048 	addr = sin->sin_addr.s_addr;
12049 	if (ipif->ipif_flags & IPIF_UP) {
12050 		/*
12051 		 * If we are already up, make sure the new
12052 		 * broadcast address makes sense.  If it does,
12053 		 * there should be an IRE for it already.
12054 		 * Don't match on ipif, only on the ill
12055 		 * since we are sharing these now. Don't use
12056 		 * MATCH_IRE_ILL_GROUP as we are looking for
12057 		 * the broadcast ire on this ill and each ill
12058 		 * in the group has its own broadcast ire.
12059 		 */
12060 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12061 		    ipif, ALL_ZONES, NULL,
12062 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12063 		if (ire == NULL) {
12064 			return (EINVAL);
12065 		} else {
12066 			ire_refrele(ire);
12067 		}
12068 	}
12069 	/*
12070 	 * Changing the broadcast addr for this ipif.
12071 	 * Make sure we have valid net and subnet bcast
12072 	 * ire's for other logical interfaces, if needed.
12073 	 */
12074 	if (addr != ipif->ipif_brd_addr)
12075 		ipif_check_bcast_ires(ipif);
12076 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12077 	return (0);
12078 }
12079 
12080 /* Get interface broadcast address. */
12081 /* ARGSUSED */
12082 int
12083 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12084     ip_ioctl_cmd_t *ipip, void *if_req)
12085 {
12086 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12087 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12088 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12089 		return (EADDRNOTAVAIL);
12090 
12091 	/* IPIF_BROADCAST not possible with IPv6 */
12092 	ASSERT(!ipif->ipif_isv6);
12093 	*sin = sin_null;
12094 	sin->sin_family = AF_INET;
12095 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12096 	return (0);
12097 }
12098 
12099 /*
12100  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12101  */
12102 /* ARGSUSED */
12103 int
12104 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12105     ip_ioctl_cmd_t *ipip, void *if_req)
12106 {
12107 	int err = 0;
12108 	in6_addr_t v6mask;
12109 
12110 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12111 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12112 
12113 	ASSERT(IAM_WRITER_IPIF(ipif));
12114 
12115 	if (ipif->ipif_isv6) {
12116 		sin6_t *sin6;
12117 
12118 		if (sin->sin_family != AF_INET6)
12119 			return (EAFNOSUPPORT);
12120 
12121 		sin6 = (sin6_t *)sin;
12122 		v6mask = sin6->sin6_addr;
12123 	} else {
12124 		ipaddr_t mask;
12125 
12126 		if (sin->sin_family != AF_INET)
12127 			return (EAFNOSUPPORT);
12128 
12129 		mask = sin->sin_addr.s_addr;
12130 		V4MASK_TO_V6(mask, v6mask);
12131 	}
12132 
12133 	/*
12134 	 * No big deal if the interface isn't already up, or the mask
12135 	 * isn't really changing, or this is pt-pt.
12136 	 */
12137 	if (!(ipif->ipif_flags & IPIF_UP) ||
12138 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12139 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12140 		ipif->ipif_v6net_mask = v6mask;
12141 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12142 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12143 			    ipif->ipif_v6net_mask,
12144 			    ipif->ipif_v6subnet);
12145 		}
12146 		return (0);
12147 	}
12148 	/*
12149 	 * Make sure we have valid net and subnet broadcast ire's
12150 	 * for the old netmask, if needed by other logical interfaces.
12151 	 */
12152 	if (!ipif->ipif_isv6)
12153 		ipif_check_bcast_ires(ipif);
12154 
12155 	err = ipif_logical_down(ipif, q, mp);
12156 	if (err == EINPROGRESS)
12157 		return (err);
12158 	ipif_down_tail(ipif);
12159 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12160 	return (err);
12161 }
12162 
12163 static int
12164 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12165 {
12166 	in6_addr_t v6mask;
12167 	int err = 0;
12168 
12169 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12170 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12171 
12172 	if (ipif->ipif_isv6) {
12173 		sin6_t *sin6;
12174 
12175 		sin6 = (sin6_t *)sin;
12176 		v6mask = sin6->sin6_addr;
12177 	} else {
12178 		ipaddr_t mask;
12179 
12180 		mask = sin->sin_addr.s_addr;
12181 		V4MASK_TO_V6(mask, v6mask);
12182 	}
12183 
12184 	ipif->ipif_v6net_mask = v6mask;
12185 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12186 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12187 		    ipif->ipif_v6subnet);
12188 	}
12189 	err = ipif_up(ipif, q, mp);
12190 
12191 	if (err == 0 || err == EINPROGRESS) {
12192 		/*
12193 		 * The interface must be DL_BOUND if this packet has to
12194 		 * go out on the wire. Since we only go through a logical
12195 		 * down and are bound with the driver during an internal
12196 		 * down/up that is satisfied.
12197 		 */
12198 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12199 			/* Potentially broadcast an address mask reply. */
12200 			ipif_mask_reply(ipif);
12201 		}
12202 	}
12203 	return (err);
12204 }
12205 
12206 /* ARGSUSED */
12207 int
12208 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12209     ip_ioctl_cmd_t *ipip, void *if_req)
12210 {
12211 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12212 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12213 	ipif_down_tail(ipif);
12214 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12215 }
12216 
12217 /* Get interface net mask. */
12218 /* ARGSUSED */
12219 int
12220 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12221     ip_ioctl_cmd_t *ipip, void *if_req)
12222 {
12223 	struct lifreq *lifr = (struct lifreq *)if_req;
12224 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12225 
12226 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12227 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12228 
12229 	/*
12230 	 * net mask can't change since we have a reference to the ipif.
12231 	 */
12232 	if (ipif->ipif_isv6) {
12233 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12234 		*sin6 = sin6_null;
12235 		sin6->sin6_family = AF_INET6;
12236 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12237 		lifr->lifr_addrlen =
12238 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12239 	} else {
12240 		*sin = sin_null;
12241 		sin->sin_family = AF_INET;
12242 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12243 		if (ipip->ipi_cmd_type == LIF_CMD) {
12244 			lifr->lifr_addrlen =
12245 			    ip_mask_to_plen(ipif->ipif_net_mask);
12246 		}
12247 	}
12248 	return (0);
12249 }
12250 
12251 /* ARGSUSED */
12252 int
12253 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12254     ip_ioctl_cmd_t *ipip, void *if_req)
12255 {
12256 
12257 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12258 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12259 	/*
12260 	 * Set interface metric.  We don't use this for
12261 	 * anything but we keep track of it in case it is
12262 	 * important to routing applications or such.
12263 	 */
12264 	if (ipip->ipi_cmd_type == IF_CMD) {
12265 		struct ifreq    *ifr;
12266 
12267 		ifr = (struct ifreq *)if_req;
12268 		ipif->ipif_metric = ifr->ifr_metric;
12269 	} else {
12270 		struct lifreq   *lifr;
12271 
12272 		lifr = (struct lifreq *)if_req;
12273 		ipif->ipif_metric = lifr->lifr_metric;
12274 	}
12275 	return (0);
12276 }
12277 
12278 /* ARGSUSED */
12279 int
12280 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12281     ip_ioctl_cmd_t *ipip, void *if_req)
12282 {
12283 	/* Get interface metric. */
12284 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12285 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12286 	if (ipip->ipi_cmd_type == IF_CMD) {
12287 		struct ifreq    *ifr;
12288 
12289 		ifr = (struct ifreq *)if_req;
12290 		ifr->ifr_metric = ipif->ipif_metric;
12291 	} else {
12292 		struct lifreq   *lifr;
12293 
12294 		lifr = (struct lifreq *)if_req;
12295 		lifr->lifr_metric = ipif->ipif_metric;
12296 	}
12297 
12298 	return (0);
12299 }
12300 
12301 /* ARGSUSED */
12302 int
12303 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12304     ip_ioctl_cmd_t *ipip, void *if_req)
12305 {
12306 
12307 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12308 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12309 	/*
12310 	 * Set the muxid returned from I_PLINK.
12311 	 */
12312 	if (ipip->ipi_cmd_type == IF_CMD) {
12313 		struct ifreq *ifr = (struct ifreq *)if_req;
12314 
12315 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12316 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12317 	} else {
12318 		struct lifreq *lifr = (struct lifreq *)if_req;
12319 
12320 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12321 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12322 	}
12323 	return (0);
12324 }
12325 
12326 /* ARGSUSED */
12327 int
12328 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12329     ip_ioctl_cmd_t *ipip, void *if_req)
12330 {
12331 
12332 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12333 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12334 	/*
12335 	 * Get the muxid saved in ill for I_PUNLINK.
12336 	 */
12337 	if (ipip->ipi_cmd_type == IF_CMD) {
12338 		struct ifreq *ifr = (struct ifreq *)if_req;
12339 
12340 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12341 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12342 	} else {
12343 		struct lifreq *lifr = (struct lifreq *)if_req;
12344 
12345 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12346 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12347 	}
12348 	return (0);
12349 }
12350 
12351 /*
12352  * Set the subnet prefix. Does not modify the broadcast address.
12353  */
12354 /* ARGSUSED */
12355 int
12356 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12357     ip_ioctl_cmd_t *ipip, void *if_req)
12358 {
12359 	int err = 0;
12360 	in6_addr_t v6addr;
12361 	in6_addr_t v6mask;
12362 	boolean_t need_up = B_FALSE;
12363 	int addrlen;
12364 
12365 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12366 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12367 
12368 	ASSERT(IAM_WRITER_IPIF(ipif));
12369 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12370 
12371 	if (ipif->ipif_isv6) {
12372 		sin6_t *sin6;
12373 
12374 		if (sin->sin_family != AF_INET6)
12375 			return (EAFNOSUPPORT);
12376 
12377 		sin6 = (sin6_t *)sin;
12378 		v6addr = sin6->sin6_addr;
12379 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12380 			return (EADDRNOTAVAIL);
12381 	} else {
12382 		ipaddr_t addr;
12383 
12384 		if (sin->sin_family != AF_INET)
12385 			return (EAFNOSUPPORT);
12386 
12387 		addr = sin->sin_addr.s_addr;
12388 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12389 			return (EADDRNOTAVAIL);
12390 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12391 		/* Add 96 bits */
12392 		addrlen += IPV6_ABITS - IP_ABITS;
12393 	}
12394 
12395 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12396 		return (EINVAL);
12397 
12398 	/* Check if bits in the address is set past the mask */
12399 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12400 		return (EINVAL);
12401 
12402 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12403 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12404 		return (0);	/* No change */
12405 
12406 	if (ipif->ipif_flags & IPIF_UP) {
12407 		/*
12408 		 * If the interface is already marked up,
12409 		 * we call ipif_down which will take care
12410 		 * of ditching any IREs that have been set
12411 		 * up based on the old interface address.
12412 		 */
12413 		err = ipif_logical_down(ipif, q, mp);
12414 		if (err == EINPROGRESS)
12415 			return (err);
12416 		ipif_down_tail(ipif);
12417 		need_up = B_TRUE;
12418 	}
12419 
12420 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12421 	return (err);
12422 }
12423 
12424 static int
12425 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12426     queue_t *q, mblk_t *mp, boolean_t need_up)
12427 {
12428 	ill_t	*ill = ipif->ipif_ill;
12429 	int	err = 0;
12430 
12431 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12432 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12433 
12434 	/* Set the new address. */
12435 	mutex_enter(&ill->ill_lock);
12436 	ipif->ipif_v6net_mask = v6mask;
12437 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12438 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12439 		    ipif->ipif_v6subnet);
12440 	}
12441 	mutex_exit(&ill->ill_lock);
12442 
12443 	if (need_up) {
12444 		/*
12445 		 * Now bring the interface back up.  If this
12446 		 * is the only IPIF for the ILL, ipif_up
12447 		 * will have to re-bind to the device, so
12448 		 * we may get back EINPROGRESS, in which
12449 		 * case, this IOCTL will get completed in
12450 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12451 		 */
12452 		err = ipif_up(ipif, q, mp);
12453 		if (err == EINPROGRESS)
12454 			return (err);
12455 	}
12456 	return (err);
12457 }
12458 
12459 /* ARGSUSED */
12460 int
12461 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12462     ip_ioctl_cmd_t *ipip, void *if_req)
12463 {
12464 	int	addrlen;
12465 	in6_addr_t v6addr;
12466 	in6_addr_t v6mask;
12467 	struct lifreq *lifr = (struct lifreq *)if_req;
12468 
12469 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12470 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12471 	ipif_down_tail(ipif);
12472 
12473 	addrlen = lifr->lifr_addrlen;
12474 	if (ipif->ipif_isv6) {
12475 		sin6_t *sin6;
12476 
12477 		sin6 = (sin6_t *)sin;
12478 		v6addr = sin6->sin6_addr;
12479 	} else {
12480 		ipaddr_t addr;
12481 
12482 		addr = sin->sin_addr.s_addr;
12483 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12484 		addrlen += IPV6_ABITS - IP_ABITS;
12485 	}
12486 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12487 
12488 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12489 }
12490 
12491 /* ARGSUSED */
12492 int
12493 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12494     ip_ioctl_cmd_t *ipip, void *if_req)
12495 {
12496 	struct lifreq *lifr = (struct lifreq *)if_req;
12497 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12498 
12499 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12500 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12501 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12502 
12503 	if (ipif->ipif_isv6) {
12504 		*sin6 = sin6_null;
12505 		sin6->sin6_family = AF_INET6;
12506 		sin6->sin6_addr = ipif->ipif_v6subnet;
12507 		lifr->lifr_addrlen =
12508 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12509 	} else {
12510 		*sin = sin_null;
12511 		sin->sin_family = AF_INET;
12512 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12513 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12514 	}
12515 	return (0);
12516 }
12517 
12518 /*
12519  * Set the IPv6 address token.
12520  */
12521 /* ARGSUSED */
12522 int
12523 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12524     ip_ioctl_cmd_t *ipi, void *if_req)
12525 {
12526 	ill_t *ill = ipif->ipif_ill;
12527 	int err;
12528 	in6_addr_t v6addr;
12529 	in6_addr_t v6mask;
12530 	boolean_t need_up = B_FALSE;
12531 	int i;
12532 	sin6_t *sin6 = (sin6_t *)sin;
12533 	struct lifreq *lifr = (struct lifreq *)if_req;
12534 	int addrlen;
12535 
12536 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12537 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12538 	ASSERT(IAM_WRITER_IPIF(ipif));
12539 
12540 	addrlen = lifr->lifr_addrlen;
12541 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12542 	if (ipif->ipif_id != 0)
12543 		return (EINVAL);
12544 
12545 	if (!ipif->ipif_isv6)
12546 		return (EINVAL);
12547 
12548 	if (addrlen > IPV6_ABITS)
12549 		return (EINVAL);
12550 
12551 	v6addr = sin6->sin6_addr;
12552 
12553 	/*
12554 	 * The length of the token is the length from the end.  To get
12555 	 * the proper mask for this, compute the mask of the bits not
12556 	 * in the token; ie. the prefix, and then xor to get the mask.
12557 	 */
12558 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12559 		return (EINVAL);
12560 	for (i = 0; i < 4; i++) {
12561 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12562 	}
12563 
12564 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12565 	    ill->ill_token_length == addrlen)
12566 		return (0);	/* No change */
12567 
12568 	if (ipif->ipif_flags & IPIF_UP) {
12569 		err = ipif_logical_down(ipif, q, mp);
12570 		if (err == EINPROGRESS)
12571 			return (err);
12572 		ipif_down_tail(ipif);
12573 		need_up = B_TRUE;
12574 	}
12575 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12576 	return (err);
12577 }
12578 
12579 static int
12580 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12581     mblk_t *mp, boolean_t need_up)
12582 {
12583 	in6_addr_t v6addr;
12584 	in6_addr_t v6mask;
12585 	ill_t	*ill = ipif->ipif_ill;
12586 	int	i;
12587 	int	err = 0;
12588 
12589 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12590 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12591 	v6addr = sin6->sin6_addr;
12592 	/*
12593 	 * The length of the token is the length from the end.  To get
12594 	 * the proper mask for this, compute the mask of the bits not
12595 	 * in the token; ie. the prefix, and then xor to get the mask.
12596 	 */
12597 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12598 	for (i = 0; i < 4; i++)
12599 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12600 
12601 	mutex_enter(&ill->ill_lock);
12602 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12603 	ill->ill_token_length = addrlen;
12604 	mutex_exit(&ill->ill_lock);
12605 
12606 	if (need_up) {
12607 		/*
12608 		 * Now bring the interface back up.  If this
12609 		 * is the only IPIF for the ILL, ipif_up
12610 		 * will have to re-bind to the device, so
12611 		 * we may get back EINPROGRESS, in which
12612 		 * case, this IOCTL will get completed in
12613 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12614 		 */
12615 		err = ipif_up(ipif, q, mp);
12616 		if (err == EINPROGRESS)
12617 			return (err);
12618 	}
12619 	return (err);
12620 }
12621 
12622 /* ARGSUSED */
12623 int
12624 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12625     ip_ioctl_cmd_t *ipi, void *if_req)
12626 {
12627 	ill_t *ill;
12628 	sin6_t *sin6 = (sin6_t *)sin;
12629 	struct lifreq *lifr = (struct lifreq *)if_req;
12630 
12631 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12632 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12633 	if (ipif->ipif_id != 0)
12634 		return (EINVAL);
12635 
12636 	ill = ipif->ipif_ill;
12637 	if (!ill->ill_isv6)
12638 		return (ENXIO);
12639 
12640 	*sin6 = sin6_null;
12641 	sin6->sin6_family = AF_INET6;
12642 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12643 	sin6->sin6_addr = ill->ill_token;
12644 	lifr->lifr_addrlen = ill->ill_token_length;
12645 	return (0);
12646 }
12647 
12648 /*
12649  * Set (hardware) link specific information that might override
12650  * what was acquired through the DL_INFO_ACK.
12651  * The logic is as follows.
12652  *
12653  * become exclusive
12654  * set CHANGING flag
12655  * change mtu on affected IREs
12656  * clear CHANGING flag
12657  *
12658  * An ire add that occurs before the CHANGING flag is set will have its mtu
12659  * changed by the ip_sioctl_lnkinfo.
12660  *
12661  * During the time the CHANGING flag is set, no new ires will be added to the
12662  * bucket, and ire add will fail (due the CHANGING flag).
12663  *
12664  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12665  * before it is added to the bucket.
12666  *
12667  * Obviously only 1 thread can set the CHANGING flag and we need to become
12668  * exclusive to set the flag.
12669  */
12670 /* ARGSUSED */
12671 int
12672 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12673     ip_ioctl_cmd_t *ipi, void *if_req)
12674 {
12675 	ill_t		*ill = ipif->ipif_ill;
12676 	ipif_t		*nipif;
12677 	int		ip_min_mtu;
12678 	boolean_t	mtu_walk = B_FALSE;
12679 	struct lifreq	*lifr = (struct lifreq *)if_req;
12680 	lif_ifinfo_req_t *lir;
12681 	ire_t		*ire;
12682 
12683 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12684 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12685 	lir = &lifr->lifr_ifinfo;
12686 	ASSERT(IAM_WRITER_IPIF(ipif));
12687 
12688 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12689 	if (ipif->ipif_id != 0)
12690 		return (EINVAL);
12691 
12692 	/* Set interface MTU. */
12693 	if (ipif->ipif_isv6)
12694 		ip_min_mtu = IPV6_MIN_MTU;
12695 	else
12696 		ip_min_mtu = IP_MIN_MTU;
12697 
12698 	/*
12699 	 * Verify values before we set anything. Allow zero to
12700 	 * mean unspecified.
12701 	 */
12702 	if (lir->lir_maxmtu != 0 &&
12703 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12704 	    lir->lir_maxmtu < ip_min_mtu))
12705 		return (EINVAL);
12706 	if (lir->lir_reachtime != 0 &&
12707 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12708 		return (EINVAL);
12709 	if (lir->lir_reachretrans != 0 &&
12710 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12711 		return (EINVAL);
12712 
12713 	mutex_enter(&ill->ill_lock);
12714 	ill->ill_state_flags |= ILL_CHANGING;
12715 	for (nipif = ill->ill_ipif; nipif != NULL;
12716 	    nipif = nipif->ipif_next) {
12717 		nipif->ipif_state_flags |= IPIF_CHANGING;
12718 	}
12719 
12720 	mutex_exit(&ill->ill_lock);
12721 
12722 	if (lir->lir_maxmtu != 0) {
12723 		ill->ill_max_mtu = lir->lir_maxmtu;
12724 		ill->ill_mtu_userspecified = 1;
12725 		mtu_walk = B_TRUE;
12726 	}
12727 
12728 	if (lir->lir_reachtime != 0)
12729 		ill->ill_reachable_time = lir->lir_reachtime;
12730 
12731 	if (lir->lir_reachretrans != 0)
12732 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12733 
12734 	ill->ill_max_hops = lir->lir_maxhops;
12735 
12736 	ill->ill_max_buf = ND_MAX_Q;
12737 
12738 	if (mtu_walk) {
12739 		/*
12740 		 * Set the MTU on all ipifs associated with this ill except
12741 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12742 		 */
12743 		for (nipif = ill->ill_ipif; nipif != NULL;
12744 		    nipif = nipif->ipif_next) {
12745 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12746 				continue;
12747 
12748 			nipif->ipif_mtu = ill->ill_max_mtu;
12749 
12750 			if (!(nipif->ipif_flags & IPIF_UP))
12751 				continue;
12752 
12753 			if (nipif->ipif_isv6)
12754 				ire = ipif_to_ire_v6(nipif);
12755 			else
12756 				ire = ipif_to_ire(nipif);
12757 			if (ire != NULL) {
12758 				ire->ire_max_frag = ipif->ipif_mtu;
12759 				ire_refrele(ire);
12760 			}
12761 
12762 			ire_walk_ill(MATCH_IRE_ILL, 0, ipif_mtu_change,
12763 			    nipif, ill);
12764 		}
12765 	}
12766 
12767 	mutex_enter(&ill->ill_lock);
12768 	for (nipif = ill->ill_ipif; nipif != NULL;
12769 	    nipif = nipif->ipif_next) {
12770 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12771 	}
12772 	ILL_UNMARK_CHANGING(ill);
12773 	mutex_exit(&ill->ill_lock);
12774 
12775 	return (0);
12776 }
12777 
12778 /* ARGSUSED */
12779 int
12780 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12781     ip_ioctl_cmd_t *ipi, void *if_req)
12782 {
12783 	struct lif_ifinfo_req *lir;
12784 	ill_t *ill = ipif->ipif_ill;
12785 
12786 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12787 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12788 	if (ipif->ipif_id != 0)
12789 		return (EINVAL);
12790 
12791 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12792 	lir->lir_maxhops = ill->ill_max_hops;
12793 	lir->lir_reachtime = ill->ill_reachable_time;
12794 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12795 	lir->lir_maxmtu = ill->ill_max_mtu;
12796 
12797 	return (0);
12798 }
12799 
12800 /*
12801  * Return best guess as to the subnet mask for the specified address.
12802  * Based on the subnet masks for all the configured interfaces.
12803  *
12804  * We end up returning a zero mask in the case of default, multicast or
12805  * experimental.
12806  */
12807 static ipaddr_t
12808 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
12809 {
12810 	ipaddr_t net_mask;
12811 	ill_t	*ill;
12812 	ipif_t	*ipif;
12813 	ill_walk_context_t ctx;
12814 	ipif_t	*fallback_ipif = NULL;
12815 
12816 	net_mask = ip_net_mask(addr);
12817 	if (net_mask == 0) {
12818 		*ipifp = NULL;
12819 		return (0);
12820 	}
12821 
12822 	/* Let's check to see if this is maybe a local subnet route. */
12823 	/* this function only applies to IPv4 interfaces */
12824 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
12825 	ill = ILL_START_WALK_V4(&ctx, ipst);
12826 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12827 		mutex_enter(&ill->ill_lock);
12828 		for (ipif = ill->ill_ipif; ipif != NULL;
12829 		    ipif = ipif->ipif_next) {
12830 			if (!IPIF_CAN_LOOKUP(ipif))
12831 				continue;
12832 			if (!(ipif->ipif_flags & IPIF_UP))
12833 				continue;
12834 			if ((ipif->ipif_subnet & net_mask) ==
12835 			    (addr & net_mask)) {
12836 				/*
12837 				 * Don't trust pt-pt interfaces if there are
12838 				 * other interfaces.
12839 				 */
12840 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12841 					if (fallback_ipif == NULL) {
12842 						ipif_refhold_locked(ipif);
12843 						fallback_ipif = ipif;
12844 					}
12845 					continue;
12846 				}
12847 
12848 				/*
12849 				 * Fine. Just assume the same net mask as the
12850 				 * directly attached subnet interface is using.
12851 				 */
12852 				ipif_refhold_locked(ipif);
12853 				mutex_exit(&ill->ill_lock);
12854 				rw_exit(&ipst->ips_ill_g_lock);
12855 				if (fallback_ipif != NULL)
12856 					ipif_refrele(fallback_ipif);
12857 				*ipifp = ipif;
12858 				return (ipif->ipif_net_mask);
12859 			}
12860 		}
12861 		mutex_exit(&ill->ill_lock);
12862 	}
12863 	rw_exit(&ipst->ips_ill_g_lock);
12864 
12865 	*ipifp = fallback_ipif;
12866 	return ((fallback_ipif != NULL) ?
12867 	    fallback_ipif->ipif_net_mask : net_mask);
12868 }
12869 
12870 /*
12871  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12872  */
12873 static void
12874 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12875 {
12876 	IOCP	iocp;
12877 	ipft_t	*ipft;
12878 	ipllc_t	*ipllc;
12879 	mblk_t	*mp1;
12880 	cred_t	*cr;
12881 	int	error = 0;
12882 	conn_t	*connp;
12883 
12884 	ip1dbg(("ip_wput_ioctl"));
12885 	iocp = (IOCP)mp->b_rptr;
12886 	mp1 = mp->b_cont;
12887 	if (mp1 == NULL) {
12888 		iocp->ioc_error = EINVAL;
12889 		mp->b_datap->db_type = M_IOCNAK;
12890 		iocp->ioc_count = 0;
12891 		qreply(q, mp);
12892 		return;
12893 	}
12894 
12895 	/*
12896 	 * These IOCTLs provide various control capabilities to
12897 	 * upstream agents such as ULPs and processes.	There
12898 	 * are currently two such IOCTLs implemented.  They
12899 	 * are used by TCP to provide update information for
12900 	 * existing IREs and to forcibly delete an IRE for a
12901 	 * host that is not responding, thereby forcing an
12902 	 * attempt at a new route.
12903 	 */
12904 	iocp->ioc_error = EINVAL;
12905 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12906 		goto done;
12907 
12908 	ipllc = (ipllc_t *)mp1->b_rptr;
12909 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12910 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12911 			break;
12912 	}
12913 	/*
12914 	 * prefer credential from mblk over ioctl;
12915 	 * see ip_sioctl_copyin_setup
12916 	 */
12917 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12918 
12919 	/*
12920 	 * Refhold the conn in case the request gets queued up in some lookup
12921 	 */
12922 	ASSERT(CONN_Q(q));
12923 	connp = Q_TO_CONN(q);
12924 	CONN_INC_REF(connp);
12925 	if (ipft->ipft_pfi &&
12926 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12927 	    pullupmsg(mp1, ipft->ipft_min_size))) {
12928 		error = (*ipft->ipft_pfi)(q,
12929 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12930 	}
12931 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12932 		/*
12933 		 * CONN_OPER_PENDING_DONE happens in the function called
12934 		 * through ipft_pfi above.
12935 		 */
12936 		return;
12937 	}
12938 
12939 	CONN_OPER_PENDING_DONE(connp);
12940 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12941 		freemsg(mp);
12942 		return;
12943 	}
12944 	iocp->ioc_error = error;
12945 
12946 done:
12947 	mp->b_datap->db_type = M_IOCACK;
12948 	if (iocp->ioc_error)
12949 		iocp->ioc_count = 0;
12950 	qreply(q, mp);
12951 }
12952 
12953 /*
12954  * Lookup an ipif using the sequence id (ipif_seqid)
12955  */
12956 ipif_t *
12957 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12958 {
12959 	ipif_t *ipif;
12960 
12961 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12962 
12963 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12964 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12965 			return (ipif);
12966 	}
12967 	return (NULL);
12968 }
12969 
12970 /*
12971  * Assign a unique id for the ipif. This is used later when we send
12972  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12973  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12974  * IRE is added, we verify that ipif has not disappeared.
12975  */
12976 
12977 static void
12978 ipif_assign_seqid(ipif_t *ipif)
12979 {
12980 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12981 
12982 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
12983 }
12984 
12985 /*
12986  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12987  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12988  * be inserted into the first space available in the list. The value of
12989  * ipif_id will then be set to the appropriate value for its position.
12990  */
12991 static int
12992 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12993 {
12994 	ill_t *ill;
12995 	ipif_t *tipif;
12996 	ipif_t **tipifp;
12997 	int id;
12998 	ip_stack_t	*ipst;
12999 
13000 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13001 	    IAM_WRITER_IPIF(ipif));
13002 
13003 	ill = ipif->ipif_ill;
13004 	ASSERT(ill != NULL);
13005 	ipst = ill->ill_ipst;
13006 
13007 	/*
13008 	 * In the case of lo0:0 we already hold the ill_g_lock.
13009 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13010 	 * ipif_insert. Another such caller is ipif_move.
13011 	 */
13012 	if (acquire_g_lock)
13013 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13014 	if (acquire_ill_lock)
13015 		mutex_enter(&ill->ill_lock);
13016 	id = ipif->ipif_id;
13017 	tipifp = &(ill->ill_ipif);
13018 	if (id == -1) {	/* need to find a real id */
13019 		id = 0;
13020 		while ((tipif = *tipifp) != NULL) {
13021 			ASSERT(tipif->ipif_id >= id);
13022 			if (tipif->ipif_id != id)
13023 				break; /* non-consecutive id */
13024 			id++;
13025 			tipifp = &(tipif->ipif_next);
13026 		}
13027 		/* limit number of logical interfaces */
13028 		if (id >= ipst->ips_ip_addrs_per_if) {
13029 			if (acquire_ill_lock)
13030 				mutex_exit(&ill->ill_lock);
13031 			if (acquire_g_lock)
13032 				rw_exit(&ipst->ips_ill_g_lock);
13033 			return (-1);
13034 		}
13035 		ipif->ipif_id = id; /* assign new id */
13036 	} else if (id < ipst->ips_ip_addrs_per_if) {
13037 		/* we have a real id; insert ipif in the right place */
13038 		while ((tipif = *tipifp) != NULL) {
13039 			ASSERT(tipif->ipif_id != id);
13040 			if (tipif->ipif_id > id)
13041 				break; /* found correct location */
13042 			tipifp = &(tipif->ipif_next);
13043 		}
13044 	} else {
13045 		if (acquire_ill_lock)
13046 			mutex_exit(&ill->ill_lock);
13047 		if (acquire_g_lock)
13048 			rw_exit(&ipst->ips_ill_g_lock);
13049 		return (-1);
13050 	}
13051 
13052 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13053 
13054 	ipif->ipif_next = tipif;
13055 	*tipifp = ipif;
13056 	if (acquire_ill_lock)
13057 		mutex_exit(&ill->ill_lock);
13058 	if (acquire_g_lock)
13059 		rw_exit(&ipst->ips_ill_g_lock);
13060 	return (0);
13061 }
13062 
13063 static void
13064 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13065 {
13066 	ipif_t	**ipifp;
13067 	ill_t	*ill = ipif->ipif_ill;
13068 
13069 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13070 	if (acquire_ill_lock)
13071 		mutex_enter(&ill->ill_lock);
13072 	else
13073 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13074 
13075 	ipifp = &ill->ill_ipif;
13076 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13077 		if (*ipifp == ipif) {
13078 			*ipifp = ipif->ipif_next;
13079 			break;
13080 		}
13081 	}
13082 
13083 	if (acquire_ill_lock)
13084 		mutex_exit(&ill->ill_lock);
13085 }
13086 
13087 /*
13088  * Allocate and initialize a new interface control structure.  (Always
13089  * called as writer.)
13090  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13091  * is not part of the global linked list of ills. ipif_seqid is unique
13092  * in the system and to preserve the uniqueness, it is assigned only
13093  * when ill becomes part of the global list. At that point ill will
13094  * have a name. If it doesn't get assigned here, it will get assigned
13095  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13096  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13097  * the interface flags or any other information from the DL_INFO_ACK for
13098  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13099  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13100  * second DL_INFO_ACK comes in from the driver.
13101  */
13102 static ipif_t *
13103 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13104 {
13105 	ipif_t	*ipif;
13106 	phyint_t *phyi;
13107 
13108 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13109 	    ill->ill_name, id, (void *)ill));
13110 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13111 
13112 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13113 		return (NULL);
13114 	*ipif = ipif_zero;	/* start clean */
13115 
13116 	ipif->ipif_ill = ill;
13117 	ipif->ipif_id = id;	/* could be -1 */
13118 	/*
13119 	 * Inherit the zoneid from the ill; for the shared stack instance
13120 	 * this is always the global zone
13121 	 */
13122 	ipif->ipif_zoneid = ill->ill_zoneid;
13123 
13124 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13125 
13126 	ipif->ipif_refcnt = 0;
13127 	ipif->ipif_saved_ire_cnt = 0;
13128 
13129 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13130 		mi_free(ipif);
13131 		return (NULL);
13132 	}
13133 	/* -1 id should have been replaced by real id */
13134 	id = ipif->ipif_id;
13135 	ASSERT(id >= 0);
13136 
13137 	if (ill->ill_name[0] != '\0')
13138 		ipif_assign_seqid(ipif);
13139 
13140 	/*
13141 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13142 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13143 	 * ioctl sets ipif_orig_ipifid to zero.
13144 	 */
13145 	ipif->ipif_orig_ipifid = id;
13146 
13147 	/*
13148 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13149 	 * The ipif is still not up and can't be looked up until the
13150 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13151 	 */
13152 	mutex_enter(&ill->ill_lock);
13153 	mutex_enter(&ill->ill_phyint->phyint_lock);
13154 	/*
13155 	 * Set the running flag when logical interface zero is created.
13156 	 * For subsequent logical interfaces, a DLPI link down
13157 	 * notification message may have cleared the running flag to
13158 	 * indicate the link is down, so we shouldn't just blindly set it.
13159 	 */
13160 	if (id == 0)
13161 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13162 	ipif->ipif_ire_type = ire_type;
13163 	phyi = ill->ill_phyint;
13164 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13165 
13166 	if (ipif->ipif_isv6) {
13167 		ill->ill_flags |= ILLF_IPV6;
13168 	} else {
13169 		ipaddr_t inaddr_any = INADDR_ANY;
13170 
13171 		ill->ill_flags |= ILLF_IPV4;
13172 
13173 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13174 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13175 		    &ipif->ipif_v6lcl_addr);
13176 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13177 		    &ipif->ipif_v6src_addr);
13178 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13179 		    &ipif->ipif_v6subnet);
13180 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13181 		    &ipif->ipif_v6net_mask);
13182 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13183 		    &ipif->ipif_v6brd_addr);
13184 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13185 		    &ipif->ipif_v6pp_dst_addr);
13186 	}
13187 
13188 	/*
13189 	 * Don't set the interface flags etc. now, will do it in
13190 	 * ip_ll_subnet_defaults.
13191 	 */
13192 	if (!initialize) {
13193 		mutex_exit(&ill->ill_lock);
13194 		mutex_exit(&ill->ill_phyint->phyint_lock);
13195 		return (ipif);
13196 	}
13197 	ipif->ipif_mtu = ill->ill_max_mtu;
13198 
13199 	if (ill->ill_bcast_addr_length != 0) {
13200 		/*
13201 		 * Later detect lack of DLPI driver multicast
13202 		 * capability by catching DL_ENABMULTI errors in
13203 		 * ip_rput_dlpi.
13204 		 */
13205 		ill->ill_flags |= ILLF_MULTICAST;
13206 		if (!ipif->ipif_isv6)
13207 			ipif->ipif_flags |= IPIF_BROADCAST;
13208 	} else {
13209 		if (ill->ill_net_type != IRE_LOOPBACK) {
13210 			if (ipif->ipif_isv6)
13211 				/*
13212 				 * Note: xresolv interfaces will eventually need
13213 				 * NOARP set here as well, but that will require
13214 				 * those external resolvers to have some
13215 				 * knowledge of that flag and act appropriately.
13216 				 * Not to be changed at present.
13217 				 */
13218 				ill->ill_flags |= ILLF_NONUD;
13219 			else
13220 				ill->ill_flags |= ILLF_NOARP;
13221 		}
13222 		if (ill->ill_phys_addr_length == 0) {
13223 			if (ill->ill_media &&
13224 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13225 				ipif->ipif_flags |= IPIF_NOXMIT;
13226 				phyi->phyint_flags |= PHYI_VIRTUAL;
13227 			} else {
13228 				/* pt-pt supports multicast. */
13229 				ill->ill_flags |= ILLF_MULTICAST;
13230 				if (ill->ill_net_type == IRE_LOOPBACK) {
13231 					phyi->phyint_flags |=
13232 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13233 				} else {
13234 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13235 				}
13236 			}
13237 		}
13238 	}
13239 	mutex_exit(&ill->ill_lock);
13240 	mutex_exit(&ill->ill_phyint->phyint_lock);
13241 	return (ipif);
13242 }
13243 
13244 /*
13245  * If appropriate, send a message up to the resolver delete the entry
13246  * for the address of this interface which is going out of business.
13247  * (Always called as writer).
13248  *
13249  * NOTE : We need to check for NULL mps as some of the fields are
13250  *	  initialized only for some interface types. See ipif_resolver_up()
13251  *	  for details.
13252  */
13253 void
13254 ipif_arp_down(ipif_t *ipif)
13255 {
13256 	mblk_t	*mp;
13257 	ill_t	*ill = ipif->ipif_ill;
13258 
13259 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13260 	ASSERT(IAM_WRITER_IPIF(ipif));
13261 
13262 	/* Delete the mapping for the local address */
13263 	mp = ipif->ipif_arp_del_mp;
13264 	if (mp != NULL) {
13265 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13266 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13267 		putnext(ill->ill_rq, mp);
13268 		ipif->ipif_arp_del_mp = NULL;
13269 	}
13270 
13271 	/*
13272 	 * If this is the last ipif that is going down and there are no
13273 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13274 	 * clean up ARP completely.
13275 	 */
13276 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13277 
13278 		/* Send up AR_INTERFACE_DOWN message */
13279 		mp = ill->ill_arp_down_mp;
13280 		if (mp != NULL) {
13281 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13282 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13283 			    ipif->ipif_id));
13284 			putnext(ill->ill_rq, mp);
13285 			ill->ill_arp_down_mp = NULL;
13286 		}
13287 
13288 		/* Tell ARP to delete the multicast mappings */
13289 		mp = ill->ill_arp_del_mapping_mp;
13290 		if (mp != NULL) {
13291 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13292 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13293 			    ipif->ipif_id));
13294 			putnext(ill->ill_rq, mp);
13295 			ill->ill_arp_del_mapping_mp = NULL;
13296 		}
13297 	}
13298 }
13299 
13300 /*
13301  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13302  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13303  * that it wants the add_mp allocated in this function to be returned
13304  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13305  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13306  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13307  * as it does a ipif_arp_down after calling this function - which will
13308  * remove what we add here.
13309  *
13310  * Returns -1 on failures and 0 on success.
13311  */
13312 int
13313 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13314 {
13315 	mblk_t	*del_mp = NULL;
13316 	mblk_t *add_mp = NULL;
13317 	mblk_t *mp;
13318 	ill_t	*ill = ipif->ipif_ill;
13319 	phyint_t *phyi = ill->ill_phyint;
13320 	ipaddr_t addr, mask, extract_mask = 0;
13321 	arma_t	*arma;
13322 	uint8_t *maddr, *bphys_addr;
13323 	uint32_t hw_start;
13324 	dl_unitdata_req_t *dlur;
13325 
13326 	ASSERT(IAM_WRITER_IPIF(ipif));
13327 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13328 		return (0);
13329 
13330 	/*
13331 	 * Delete the existing mapping from ARP. Normally ipif_down
13332 	 * -> ipif_arp_down should send this up to ARP. The only
13333 	 * reason we would find this when we are switching from
13334 	 * Multicast to Broadcast where we did not do a down.
13335 	 */
13336 	mp = ill->ill_arp_del_mapping_mp;
13337 	if (mp != NULL) {
13338 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13339 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13340 		putnext(ill->ill_rq, mp);
13341 		ill->ill_arp_del_mapping_mp = NULL;
13342 	}
13343 
13344 	if (arp_add_mapping_mp != NULL)
13345 		*arp_add_mapping_mp = NULL;
13346 
13347 	/*
13348 	 * Check that the address is not to long for the constant
13349 	 * length reserved in the template arma_t.
13350 	 */
13351 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13352 		return (-1);
13353 
13354 	/* Add mapping mblk */
13355 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13356 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13357 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13358 	    (caddr_t)&addr);
13359 	if (add_mp == NULL)
13360 		return (-1);
13361 	arma = (arma_t *)add_mp->b_rptr;
13362 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13363 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13364 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13365 
13366 	/*
13367 	 * Determine the broadcast address.
13368 	 */
13369 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13370 	if (ill->ill_sap_length < 0)
13371 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13372 	else
13373 		bphys_addr = (uchar_t *)dlur +
13374 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13375 	/*
13376 	 * Check PHYI_MULTI_BCAST and length of physical
13377 	 * address to determine if we use the mapping or the
13378 	 * broadcast address.
13379 	 */
13380 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13381 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13382 		    bphys_addr, maddr, &hw_start, &extract_mask))
13383 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13384 
13385 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13386 	    (ill->ill_flags & ILLF_MULTICAST)) {
13387 		/* Make sure this will not match the "exact" entry. */
13388 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13389 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13390 		    (caddr_t)&addr);
13391 		if (del_mp == NULL) {
13392 			freemsg(add_mp);
13393 			return (-1);
13394 		}
13395 		bcopy(&extract_mask, (char *)arma +
13396 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13397 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13398 			/* Use link-layer broadcast address for MULTI_BCAST */
13399 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13400 			ip2dbg(("ipif_arp_setup_multicast: adding"
13401 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13402 		} else {
13403 			arma->arma_hw_mapping_start = hw_start;
13404 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13405 			    " ARP setup for %s\n", ill->ill_name));
13406 		}
13407 	} else {
13408 		freemsg(add_mp);
13409 		ASSERT(del_mp == NULL);
13410 		/* It is neither MULTICAST nor MULTI_BCAST */
13411 		return (0);
13412 	}
13413 	ASSERT(add_mp != NULL && del_mp != NULL);
13414 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13415 	ill->ill_arp_del_mapping_mp = del_mp;
13416 	if (arp_add_mapping_mp != NULL) {
13417 		/* The caller just wants the mblks allocated */
13418 		*arp_add_mapping_mp = add_mp;
13419 	} else {
13420 		/* The caller wants us to send it to arp */
13421 		putnext(ill->ill_rq, add_mp);
13422 	}
13423 	return (0);
13424 }
13425 
13426 /*
13427  * Get the resolver set up for a new interface address.
13428  * (Always called as writer.)
13429  * Called both for IPv4 and IPv6 interfaces,
13430  * though it only sets up the resolver for v6
13431  * if it's an xresolv interface (one using an external resolver).
13432  * Honors ILLF_NOARP.
13433  * The enumerated value res_act is used to tune the behavior.
13434  * If set to Res_act_initial, then we set up all the resolver
13435  * structures for a new interface.  If set to Res_act_move, then
13436  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13437  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13438  * asynchronous hardware address change notification.  If set to
13439  * Res_act_defend, then we tell ARP that it needs to send a single
13440  * gratuitous message in defense of the address.
13441  * Returns error on failure.
13442  */
13443 int
13444 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13445 {
13446 	caddr_t	addr;
13447 	mblk_t	*arp_up_mp = NULL;
13448 	mblk_t	*arp_down_mp = NULL;
13449 	mblk_t	*arp_add_mp = NULL;
13450 	mblk_t	*arp_del_mp = NULL;
13451 	mblk_t	*arp_add_mapping_mp = NULL;
13452 	mblk_t	*arp_del_mapping_mp = NULL;
13453 	ill_t	*ill = ipif->ipif_ill;
13454 	uchar_t	*area_p = NULL;
13455 	uchar_t	*ared_p = NULL;
13456 	int	err = ENOMEM;
13457 	boolean_t was_dup;
13458 
13459 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13460 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13461 	ASSERT(IAM_WRITER_IPIF(ipif));
13462 
13463 	was_dup = B_FALSE;
13464 	if (res_act == Res_act_initial) {
13465 		ipif->ipif_addr_ready = 0;
13466 		/*
13467 		 * We're bringing an interface up here.  There's no way that we
13468 		 * should need to shut down ARP now.
13469 		 */
13470 		mutex_enter(&ill->ill_lock);
13471 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13472 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13473 			ill->ill_ipif_dup_count--;
13474 			was_dup = B_TRUE;
13475 		}
13476 		mutex_exit(&ill->ill_lock);
13477 	}
13478 	if (ipif->ipif_recovery_id != 0)
13479 		(void) untimeout(ipif->ipif_recovery_id);
13480 	ipif->ipif_recovery_id = 0;
13481 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13482 		ipif->ipif_addr_ready = 1;
13483 		return (0);
13484 	}
13485 	/* NDP will set the ipif_addr_ready flag when it's ready */
13486 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13487 		return (0);
13488 
13489 	if (ill->ill_isv6) {
13490 		/*
13491 		 * External resolver for IPv6
13492 		 */
13493 		ASSERT(res_act == Res_act_initial);
13494 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13495 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13496 			area_p = (uchar_t *)&ip6_area_template;
13497 			ared_p = (uchar_t *)&ip6_ared_template;
13498 		}
13499 	} else {
13500 		/*
13501 		 * IPv4 arp case. If the ARP stream has already started
13502 		 * closing, fail this request for ARP bringup. Else
13503 		 * record the fact that an ARP bringup is pending.
13504 		 */
13505 		mutex_enter(&ill->ill_lock);
13506 		if (ill->ill_arp_closing) {
13507 			mutex_exit(&ill->ill_lock);
13508 			err = EINVAL;
13509 			goto failed;
13510 		} else {
13511 			if (ill->ill_ipif_up_count == 0 &&
13512 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13513 				ill->ill_arp_bringup_pending = 1;
13514 			mutex_exit(&ill->ill_lock);
13515 		}
13516 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13517 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13518 			area_p = (uchar_t *)&ip_area_template;
13519 			ared_p = (uchar_t *)&ip_ared_template;
13520 		}
13521 	}
13522 
13523 	/*
13524 	 * Add an entry for the local address in ARP only if it
13525 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13526 	 */
13527 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13528 		area_t *area;
13529 
13530 		/* Now ask ARP to publish our address. */
13531 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13532 		if (arp_add_mp == NULL)
13533 			goto failed;
13534 		area = (area_t *)arp_add_mp->b_rptr;
13535 		if (res_act != Res_act_initial) {
13536 			/*
13537 			 * Copy the new hardware address and length into
13538 			 * arp_add_mp to be sent to ARP.
13539 			 */
13540 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13541 			bcopy(ill->ill_phys_addr,
13542 			    ((char *)area + area->area_hw_addr_offset),
13543 			    area->area_hw_addr_length);
13544 		}
13545 
13546 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13547 		    ACE_F_MYADDR;
13548 
13549 		if (res_act == Res_act_defend) {
13550 			area->area_flags |= ACE_F_DEFEND;
13551 			/*
13552 			 * If we're just defending our address now, then
13553 			 * there's no need to set up ARP multicast mappings.
13554 			 * The publish command is enough.
13555 			 */
13556 			goto done;
13557 		}
13558 
13559 		if (res_act != Res_act_initial)
13560 			goto arp_setup_multicast;
13561 
13562 		/*
13563 		 * Allocate an ARP deletion message so we know we can tell ARP
13564 		 * when the interface goes down.
13565 		 */
13566 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13567 		if (arp_del_mp == NULL)
13568 			goto failed;
13569 
13570 	} else {
13571 		if (res_act != Res_act_initial)
13572 			goto done;
13573 	}
13574 	/*
13575 	 * Need to bring up ARP or setup multicast mapping only
13576 	 * when the first interface is coming UP.
13577 	 */
13578 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13579 	    was_dup) {
13580 		goto done;
13581 	}
13582 
13583 	/*
13584 	 * Allocate an ARP down message (to be saved) and an ARP up
13585 	 * message.
13586 	 */
13587 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13588 	if (arp_down_mp == NULL)
13589 		goto failed;
13590 
13591 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13592 	if (arp_up_mp == NULL)
13593 		goto failed;
13594 
13595 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13596 		goto done;
13597 
13598 arp_setup_multicast:
13599 	/*
13600 	 * Setup the multicast mappings. This function initializes
13601 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13602 	 * IPv6.
13603 	 */
13604 	if (!ill->ill_isv6) {
13605 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13606 		if (err != 0)
13607 			goto failed;
13608 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13609 		ASSERT(arp_add_mapping_mp != NULL);
13610 	}
13611 
13612 done:
13613 	if (arp_del_mp != NULL) {
13614 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13615 		ipif->ipif_arp_del_mp = arp_del_mp;
13616 	}
13617 	if (arp_down_mp != NULL) {
13618 		ASSERT(ill->ill_arp_down_mp == NULL);
13619 		ill->ill_arp_down_mp = arp_down_mp;
13620 	}
13621 	if (arp_del_mapping_mp != NULL) {
13622 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13623 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13624 	}
13625 	if (arp_up_mp != NULL) {
13626 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13627 		    ill->ill_name, ipif->ipif_id));
13628 		putnext(ill->ill_rq, arp_up_mp);
13629 	}
13630 	if (arp_add_mp != NULL) {
13631 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13632 		    ill->ill_name, ipif->ipif_id));
13633 		/*
13634 		 * If it's an extended ARP implementation, then we'll wait to
13635 		 * hear that DAD has finished before using the interface.
13636 		 */
13637 		if (!ill->ill_arp_extend)
13638 			ipif->ipif_addr_ready = 1;
13639 		putnext(ill->ill_rq, arp_add_mp);
13640 	} else {
13641 		ipif->ipif_addr_ready = 1;
13642 	}
13643 	if (arp_add_mapping_mp != NULL) {
13644 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13645 		    ill->ill_name, ipif->ipif_id));
13646 		putnext(ill->ill_rq, arp_add_mapping_mp);
13647 	}
13648 	if (res_act != Res_act_initial)
13649 		return (0);
13650 
13651 	if (ill->ill_flags & ILLF_NOARP)
13652 		err = ill_arp_off(ill);
13653 	else
13654 		err = ill_arp_on(ill);
13655 	if (err != 0) {
13656 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13657 		freemsg(ipif->ipif_arp_del_mp);
13658 		freemsg(ill->ill_arp_down_mp);
13659 		freemsg(ill->ill_arp_del_mapping_mp);
13660 		ipif->ipif_arp_del_mp = NULL;
13661 		ill->ill_arp_down_mp = NULL;
13662 		ill->ill_arp_del_mapping_mp = NULL;
13663 		return (err);
13664 	}
13665 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13666 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13667 
13668 failed:
13669 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13670 	freemsg(arp_add_mp);
13671 	freemsg(arp_del_mp);
13672 	freemsg(arp_add_mapping_mp);
13673 	freemsg(arp_up_mp);
13674 	freemsg(arp_down_mp);
13675 	ill->ill_arp_bringup_pending = 0;
13676 	return (err);
13677 }
13678 
13679 /*
13680  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13681  * just gone back up.
13682  */
13683 static void
13684 ipif_arp_start_dad(ipif_t *ipif)
13685 {
13686 	ill_t *ill = ipif->ipif_ill;
13687 	mblk_t *arp_add_mp;
13688 	area_t *area;
13689 
13690 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13691 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13692 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13693 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13694 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13695 		/*
13696 		 * If we can't contact ARP for some reason, that's not really a
13697 		 * problem.  Just send out the routing socket notification that
13698 		 * DAD completion would have done, and continue.
13699 		 */
13700 		ipif_mask_reply(ipif);
13701 		ipif_up_notify(ipif);
13702 		ipif->ipif_addr_ready = 1;
13703 		return;
13704 	}
13705 
13706 	/* Setting the 'unverified' flag restarts DAD */
13707 	area = (area_t *)arp_add_mp->b_rptr;
13708 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13709 	    ACE_F_UNVERIFIED;
13710 	putnext(ill->ill_rq, arp_add_mp);
13711 }
13712 
13713 static void
13714 ipif_ndp_start_dad(ipif_t *ipif)
13715 {
13716 	nce_t *nce;
13717 
13718 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13719 	if (nce == NULL)
13720 		return;
13721 
13722 	if (!ndp_restart_dad(nce)) {
13723 		/*
13724 		 * If we can't restart DAD for some reason, that's not really a
13725 		 * problem.  Just send out the routing socket notification that
13726 		 * DAD completion would have done, and continue.
13727 		 */
13728 		ipif_up_notify(ipif);
13729 		ipif->ipif_addr_ready = 1;
13730 	}
13731 	NCE_REFRELE(nce);
13732 }
13733 
13734 /*
13735  * Restart duplicate address detection on all interfaces on the given ill.
13736  *
13737  * This is called when an interface transitions from down to up
13738  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13739  *
13740  * Note that since the underlying physical link has transitioned, we must cause
13741  * at least one routing socket message to be sent here, either via DAD
13742  * completion or just by default on the first ipif.  (If we don't do this, then
13743  * in.mpathd will see long delays when doing link-based failure recovery.)
13744  */
13745 void
13746 ill_restart_dad(ill_t *ill, boolean_t went_up)
13747 {
13748 	ipif_t *ipif;
13749 
13750 	if (ill == NULL)
13751 		return;
13752 
13753 	/*
13754 	 * If layer two doesn't support duplicate address detection, then just
13755 	 * send the routing socket message now and be done with it.
13756 	 */
13757 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
13758 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
13759 		ip_rts_ifmsg(ill->ill_ipif);
13760 		return;
13761 	}
13762 
13763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13764 		if (went_up) {
13765 			if (ipif->ipif_flags & IPIF_UP) {
13766 				if (ill->ill_isv6)
13767 					ipif_ndp_start_dad(ipif);
13768 				else
13769 					ipif_arp_start_dad(ipif);
13770 			} else if (ill->ill_isv6 &&
13771 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
13772 				/*
13773 				 * For IPv4, the ARP module itself will
13774 				 * automatically start the DAD process when it
13775 				 * sees DL_NOTE_LINK_UP.  We respond to the
13776 				 * AR_CN_READY at the completion of that task.
13777 				 * For IPv6, we must kick off the bring-up
13778 				 * process now.
13779 				 */
13780 				ndp_do_recovery(ipif);
13781 			} else {
13782 				/*
13783 				 * Unfortunately, the first ipif is "special"
13784 				 * and represents the underlying ill in the
13785 				 * routing socket messages.  Thus, when this
13786 				 * one ipif is down, we must still notify so
13787 				 * that the user knows the IFF_RUNNING status
13788 				 * change.  (If the first ipif is up, then
13789 				 * we'll handle eventual routing socket
13790 				 * notification via DAD completion.)
13791 				 */
13792 				if (ipif == ill->ill_ipif)
13793 					ip_rts_ifmsg(ill->ill_ipif);
13794 			}
13795 		} else {
13796 			/*
13797 			 * After link down, we'll need to send a new routing
13798 			 * message when the link comes back, so clear
13799 			 * ipif_addr_ready.
13800 			 */
13801 			ipif->ipif_addr_ready = 0;
13802 		}
13803 	}
13804 
13805 	/*
13806 	 * If we've torn down links, then notify the user right away.
13807 	 */
13808 	if (!went_up)
13809 		ip_rts_ifmsg(ill->ill_ipif);
13810 }
13811 
13812 /*
13813  * Wakeup all threads waiting to enter the ipsq, and sleeping
13814  * on any of the ills in this ipsq. The ill_lock of the ill
13815  * must be held so that waiters don't miss wakeups
13816  */
13817 static void
13818 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13819 {
13820 	phyint_t *phyint;
13821 
13822 	phyint = ipsq->ipsq_phyint_list;
13823 	while (phyint != NULL) {
13824 		if (phyint->phyint_illv4) {
13825 			if (!caller_holds_lock)
13826 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13827 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13828 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13829 			if (!caller_holds_lock)
13830 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13831 		}
13832 		if (phyint->phyint_illv6) {
13833 			if (!caller_holds_lock)
13834 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13835 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13836 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13837 			if (!caller_holds_lock)
13838 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13839 		}
13840 		phyint = phyint->phyint_ipsq_next;
13841 	}
13842 }
13843 
13844 static ipsq_t *
13845 ipsq_create(char *groupname, ip_stack_t *ipst)
13846 {
13847 	ipsq_t	*ipsq;
13848 
13849 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
13850 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13851 	if (ipsq == NULL) {
13852 		return (NULL);
13853 	}
13854 
13855 	if (groupname != NULL)
13856 		(void) strcpy(ipsq->ipsq_name, groupname);
13857 	else
13858 		ipsq->ipsq_name[0] = '\0';
13859 
13860 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13861 	ipsq->ipsq_flags |= IPSQ_GROUP;
13862 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
13863 	ipst->ips_ipsq_g_head = ipsq;
13864 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
13865 	return (ipsq);
13866 }
13867 
13868 /*
13869  * Return an ipsq correspoding to the groupname. If 'create' is true
13870  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13871  * uniquely with an IPMP group. However during IPMP groupname operations,
13872  * multiple IPMP groups may be associated with a single ipsq. But no
13873  * IPMP group can be associated with more than 1 ipsq at any time.
13874  * For example
13875  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13876  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13877  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13878  *
13879  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13880  * status shown below during the execution of the above command.
13881  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13882  *
13883  * After the completion of the above groupname command we return to the stable
13884  * state shown below.
13885  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13886  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13887  *
13888  * Because of the above, we don't search based on the ipsq_name since that
13889  * would miss the correct ipsq during certain windows as shown above.
13890  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13891  * natural state.
13892  */
13893 static ipsq_t *
13894 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
13895     ip_stack_t *ipst)
13896 {
13897 	ipsq_t	*ipsq;
13898 	int	group_len;
13899 	phyint_t *phyint;
13900 
13901 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
13902 
13903 	group_len = strlen(groupname);
13904 	ASSERT(group_len != 0);
13905 	group_len++;
13906 
13907 	for (ipsq = ipst->ips_ipsq_g_head;
13908 	    ipsq != NULL;
13909 	    ipsq = ipsq->ipsq_next) {
13910 		/*
13911 		 * When an ipsq is being split, and ill_split_ipsq
13912 		 * calls this function, we exclude it from being considered.
13913 		 */
13914 		if (ipsq == exclude_ipsq)
13915 			continue;
13916 
13917 		/*
13918 		 * Compare against the ipsq_name. The groupname change happens
13919 		 * in 2 phases. The 1st phase merges the from group into
13920 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13921 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13922 		 * ipsq_name. At this point the phyint_groupname has not been
13923 		 * updated.
13924 		 */
13925 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13926 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13927 			/*
13928 			 * Verify that an ipmp groupname is exactly
13929 			 * part of 1 ipsq and is not found in any other
13930 			 * ipsq.
13931 			 */
13932 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
13933 			    NULL);
13934 			return (ipsq);
13935 		}
13936 
13937 		/*
13938 		 * Comparison against ipsq_name alone is not sufficient.
13939 		 * In the case when groups are currently being
13940 		 * merged, the ipsq could hold other IPMP groups temporarily.
13941 		 * so we walk the phyint list and compare against the
13942 		 * phyint_groupname as well.
13943 		 */
13944 		phyint = ipsq->ipsq_phyint_list;
13945 		while (phyint != NULL) {
13946 			if ((group_len == phyint->phyint_groupname_len) &&
13947 			    (bcmp(phyint->phyint_groupname, groupname,
13948 			    group_len) == 0)) {
13949 				/*
13950 				 * Verify that an ipmp groupname is exactly
13951 				 * part of 1 ipsq and is not found in any other
13952 				 * ipsq.
13953 				 */
13954 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
13955 				    ipst) == NULL);
13956 				return (ipsq);
13957 			}
13958 			phyint = phyint->phyint_ipsq_next;
13959 		}
13960 	}
13961 	if (create)
13962 		ipsq = ipsq_create(groupname, ipst);
13963 	return (ipsq);
13964 }
13965 
13966 static void
13967 ipsq_delete(ipsq_t *ipsq)
13968 {
13969 	ipsq_t *nipsq;
13970 	ipsq_t *pipsq = NULL;
13971 	ip_stack_t *ipst = ipsq->ipsq_ipst;
13972 
13973 	/*
13974 	 * We don't hold the ipsq lock, but we are sure no new
13975 	 * messages can land up, since the ipsq_refs is zero.
13976 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13977 	 * is associated with this ipsq. (Lookups are based on ill_name
13978 	 * or phyint_groupname)
13979 	 */
13980 	ASSERT(ipsq->ipsq_refs == 0);
13981 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13982 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13983 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13984 		/*
13985 		 * This is not the ipsq of an IPMP group.
13986 		 */
13987 		ipsq->ipsq_ipst = NULL;
13988 		kmem_free(ipsq, sizeof (ipsq_t));
13989 		return;
13990 	}
13991 
13992 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13993 
13994 	/*
13995 	 * Locate the ipsq  before we can remove it from
13996 	 * the singly linked list of ipsq's.
13997 	 */
13998 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
13999 	    nipsq = nipsq->ipsq_next) {
14000 		if (nipsq == ipsq) {
14001 			break;
14002 		}
14003 		pipsq = nipsq;
14004 	}
14005 
14006 	ASSERT(nipsq == ipsq);
14007 
14008 	/* unlink ipsq from the list */
14009 	if (pipsq != NULL)
14010 		pipsq->ipsq_next = ipsq->ipsq_next;
14011 	else
14012 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14013 	ipsq->ipsq_ipst = NULL;
14014 	kmem_free(ipsq, sizeof (ipsq_t));
14015 	rw_exit(&ipst->ips_ill_g_lock);
14016 }
14017 
14018 static void
14019 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14020     queue_t *q)
14021 {
14022 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14023 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14024 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14025 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14026 	ASSERT(current_mp != NULL);
14027 
14028 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14029 	    NEW_OP, NULL);
14030 
14031 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14032 	    new_ipsq->ipsq_xopq_mphead != NULL);
14033 
14034 	/*
14035 	 * move from old ipsq to the new ipsq.
14036 	 */
14037 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14038 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14039 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14040 
14041 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14042 }
14043 
14044 void
14045 ill_group_cleanup(ill_t *ill)
14046 {
14047 	ill_t *ill_v4;
14048 	ill_t *ill_v6;
14049 	ipif_t *ipif;
14050 
14051 	ill_v4 = ill->ill_phyint->phyint_illv4;
14052 	ill_v6 = ill->ill_phyint->phyint_illv6;
14053 
14054 	if (ill_v4 != NULL) {
14055 		mutex_enter(&ill_v4->ill_lock);
14056 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14057 		    ipif = ipif->ipif_next) {
14058 			IPIF_UNMARK_MOVING(ipif);
14059 		}
14060 		ill_v4->ill_up_ipifs = B_FALSE;
14061 		mutex_exit(&ill_v4->ill_lock);
14062 	}
14063 
14064 	if (ill_v6 != NULL) {
14065 		mutex_enter(&ill_v6->ill_lock);
14066 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14067 		    ipif = ipif->ipif_next) {
14068 			IPIF_UNMARK_MOVING(ipif);
14069 		}
14070 		ill_v6->ill_up_ipifs = B_FALSE;
14071 		mutex_exit(&ill_v6->ill_lock);
14072 	}
14073 }
14074 /*
14075  * This function is called when an ill has had a change in its group status
14076  * to bring up all the ipifs that were up before the change.
14077  */
14078 int
14079 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14080 {
14081 	ipif_t *ipif;
14082 	ill_t *ill_v4;
14083 	ill_t *ill_v6;
14084 	ill_t *from_ill;
14085 	int err = 0;
14086 
14087 	ASSERT(IAM_WRITER_ILL(ill));
14088 
14089 	/*
14090 	 * Except for ipif_state_flags and ill_state_flags the other
14091 	 * fields of the ipif/ill that are modified below are protected
14092 	 * implicitly since we are a writer. We would have tried to down
14093 	 * even an ipif that was already down, in ill_down_ipifs. So we
14094 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14095 	 */
14096 	ill_v4 = ill->ill_phyint->phyint_illv4;
14097 	ill_v6 = ill->ill_phyint->phyint_illv6;
14098 	if (ill_v4 != NULL) {
14099 		ill_v4->ill_up_ipifs = B_TRUE;
14100 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14101 		    ipif = ipif->ipif_next) {
14102 			mutex_enter(&ill_v4->ill_lock);
14103 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14104 			IPIF_UNMARK_MOVING(ipif);
14105 			mutex_exit(&ill_v4->ill_lock);
14106 			if (ipif->ipif_was_up) {
14107 				if (!(ipif->ipif_flags & IPIF_UP))
14108 					err = ipif_up(ipif, q, mp);
14109 				ipif->ipif_was_up = B_FALSE;
14110 				if (err != 0) {
14111 					/*
14112 					 * Can there be any other error ?
14113 					 */
14114 					ASSERT(err == EINPROGRESS);
14115 					return (err);
14116 				}
14117 			}
14118 		}
14119 		mutex_enter(&ill_v4->ill_lock);
14120 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14121 		mutex_exit(&ill_v4->ill_lock);
14122 		ill_v4->ill_up_ipifs = B_FALSE;
14123 		if (ill_v4->ill_move_in_progress) {
14124 			ASSERT(ill_v4->ill_move_peer != NULL);
14125 			ill_v4->ill_move_in_progress = B_FALSE;
14126 			from_ill = ill_v4->ill_move_peer;
14127 			from_ill->ill_move_in_progress = B_FALSE;
14128 			from_ill->ill_move_peer = NULL;
14129 			mutex_enter(&from_ill->ill_lock);
14130 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14131 			mutex_exit(&from_ill->ill_lock);
14132 			if (ill_v6 == NULL) {
14133 				if (from_ill->ill_phyint->phyint_flags &
14134 				    PHYI_STANDBY) {
14135 					phyint_inactive(from_ill->ill_phyint);
14136 				}
14137 				if (ill_v4->ill_phyint->phyint_flags &
14138 				    PHYI_STANDBY) {
14139 					phyint_inactive(ill_v4->ill_phyint);
14140 				}
14141 			}
14142 			ill_v4->ill_move_peer = NULL;
14143 		}
14144 	}
14145 
14146 	if (ill_v6 != NULL) {
14147 		ill_v6->ill_up_ipifs = B_TRUE;
14148 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14149 		    ipif = ipif->ipif_next) {
14150 			mutex_enter(&ill_v6->ill_lock);
14151 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14152 			IPIF_UNMARK_MOVING(ipif);
14153 			mutex_exit(&ill_v6->ill_lock);
14154 			if (ipif->ipif_was_up) {
14155 				if (!(ipif->ipif_flags & IPIF_UP))
14156 					err = ipif_up(ipif, q, mp);
14157 				ipif->ipif_was_up = B_FALSE;
14158 				if (err != 0) {
14159 					/*
14160 					 * Can there be any other error ?
14161 					 */
14162 					ASSERT(err == EINPROGRESS);
14163 					return (err);
14164 				}
14165 			}
14166 		}
14167 		mutex_enter(&ill_v6->ill_lock);
14168 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14169 		mutex_exit(&ill_v6->ill_lock);
14170 		ill_v6->ill_up_ipifs = B_FALSE;
14171 		if (ill_v6->ill_move_in_progress) {
14172 			ASSERT(ill_v6->ill_move_peer != NULL);
14173 			ill_v6->ill_move_in_progress = B_FALSE;
14174 			from_ill = ill_v6->ill_move_peer;
14175 			from_ill->ill_move_in_progress = B_FALSE;
14176 			from_ill->ill_move_peer = NULL;
14177 			mutex_enter(&from_ill->ill_lock);
14178 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14179 			mutex_exit(&from_ill->ill_lock);
14180 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14181 				phyint_inactive(from_ill->ill_phyint);
14182 			}
14183 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14184 				phyint_inactive(ill_v6->ill_phyint);
14185 			}
14186 			ill_v6->ill_move_peer = NULL;
14187 		}
14188 	}
14189 	return (0);
14190 }
14191 
14192 /*
14193  * bring down all the approriate ipifs.
14194  */
14195 /* ARGSUSED */
14196 static void
14197 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14198 {
14199 	ipif_t *ipif;
14200 
14201 	ASSERT(IAM_WRITER_ILL(ill));
14202 
14203 	/*
14204 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14205 	 * are modified below are protected implicitly since we are a writer
14206 	 */
14207 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14208 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14209 			continue;
14210 		/*
14211 		 * Don't bring down the LINK LOCAL addresses as they are tied
14212 		 * to physical interface and they don't move. Treat them as
14213 		 * IPIF_NOFAILOVER.
14214 		 */
14215 		if (chk_nofailover && ill->ill_isv6 &&
14216 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))
14217 			continue;
14218 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14219 			/*
14220 			 * We go through the ipif_down logic even if the ipif
14221 			 * is already down, since routes can be added based
14222 			 * on down ipifs. Going through ipif_down once again
14223 			 * will delete any IREs created based on these routes.
14224 			 */
14225 			if (ipif->ipif_flags & IPIF_UP)
14226 				ipif->ipif_was_up = B_TRUE;
14227 			/*
14228 			 * If called with chk_nofailover true ipif is moving.
14229 			 */
14230 			mutex_enter(&ill->ill_lock);
14231 			if (chk_nofailover) {
14232 				ipif->ipif_state_flags |=
14233 				    IPIF_MOVING | IPIF_CHANGING;
14234 			} else {
14235 				ipif->ipif_state_flags |= IPIF_CHANGING;
14236 			}
14237 			mutex_exit(&ill->ill_lock);
14238 			/*
14239 			 * Need to re-create net/subnet bcast ires if
14240 			 * they are dependent on ipif.
14241 			 */
14242 			if (!ipif->ipif_isv6)
14243 				ipif_check_bcast_ires(ipif);
14244 			(void) ipif_logical_down(ipif, NULL, NULL);
14245 			ipif_non_duplicate(ipif);
14246 			ipif_down_tail(ipif);
14247 		}
14248 	}
14249 }
14250 
14251 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14252 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14253 	(ipsq)->ipsq_refs++;				\
14254 }
14255 
14256 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14257 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14258 	(ipsq)->ipsq_refs--;				\
14259 	if ((ipsq)->ipsq_refs == 0)				\
14260 		(ipsq)->ipsq_name[0] = '\0'; 		\
14261 }
14262 
14263 /*
14264  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14265  * new_ipsq.
14266  */
14267 static void
14268 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14269 {
14270 	phyint_t *phyint;
14271 	phyint_t *next_phyint;
14272 
14273 	/*
14274 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14275 	 * writer and the ill_lock of the ill in question. Also the dest
14276 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14277 	 */
14278 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14279 
14280 	phyint = cur_ipsq->ipsq_phyint_list;
14281 	cur_ipsq->ipsq_phyint_list = NULL;
14282 	while (phyint != NULL) {
14283 		next_phyint = phyint->phyint_ipsq_next;
14284 		IPSQ_DEC_REF(cur_ipsq, ipst);
14285 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14286 		new_ipsq->ipsq_phyint_list = phyint;
14287 		IPSQ_INC_REF(new_ipsq, ipst);
14288 		phyint->phyint_ipsq = new_ipsq;
14289 		phyint = next_phyint;
14290 	}
14291 }
14292 
14293 #define	SPLIT_SUCCESS		0
14294 #define	SPLIT_NOT_NEEDED	1
14295 #define	SPLIT_FAILED		2
14296 
14297 int
14298 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14299     ip_stack_t *ipst)
14300 {
14301 	ipsq_t *newipsq = NULL;
14302 
14303 	/*
14304 	 * Assertions denote pre-requisites for changing the ipsq of
14305 	 * a phyint
14306 	 */
14307 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14308 	/*
14309 	 * <ill-phyint> assocs can't change while ill_g_lock
14310 	 * is held as writer. See ill_phyint_reinit()
14311 	 */
14312 	ASSERT(phyint->phyint_illv4 == NULL ||
14313 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14314 	ASSERT(phyint->phyint_illv6 == NULL ||
14315 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14316 
14317 	if ((phyint->phyint_groupname_len !=
14318 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14319 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14320 	    phyint->phyint_groupname_len) != 0)) {
14321 		/*
14322 		 * Once we fail in creating a new ipsq due to memory shortage,
14323 		 * don't attempt to create new ipsq again, based on another
14324 		 * phyint, since we want all phyints belonging to an IPMP group
14325 		 * to be in the same ipsq even in the event of mem alloc fails.
14326 		 */
14327 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14328 		    cur_ipsq, ipst);
14329 		if (newipsq == NULL) {
14330 			/* Memory allocation failure */
14331 			return (SPLIT_FAILED);
14332 		} else {
14333 			/* ipsq_refs protected by ill_g_lock (writer) */
14334 			IPSQ_DEC_REF(cur_ipsq, ipst);
14335 			phyint->phyint_ipsq = newipsq;
14336 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14337 			newipsq->ipsq_phyint_list = phyint;
14338 			IPSQ_INC_REF(newipsq, ipst);
14339 			return (SPLIT_SUCCESS);
14340 		}
14341 	}
14342 	return (SPLIT_NOT_NEEDED);
14343 }
14344 
14345 /*
14346  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14347  * to do this split
14348  */
14349 static int
14350 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14351 {
14352 	ipsq_t *newipsq;
14353 
14354 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14355 	/*
14356 	 * <ill-phyint> assocs can't change while ill_g_lock
14357 	 * is held as writer. See ill_phyint_reinit()
14358 	 */
14359 
14360 	ASSERT(phyint->phyint_illv4 == NULL ||
14361 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14362 	ASSERT(phyint->phyint_illv6 == NULL ||
14363 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14364 
14365 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14366 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14367 		/*
14368 		 * ipsq_init failed due to no memory
14369 		 * caller will use the same ipsq
14370 		 */
14371 		return (SPLIT_FAILED);
14372 	}
14373 
14374 	/* ipsq_ref is protected by ill_g_lock (writer) */
14375 	IPSQ_DEC_REF(cur_ipsq, ipst);
14376 
14377 	/*
14378 	 * This is a new ipsq that is unknown to the world.
14379 	 * So we don't need to hold ipsq_lock,
14380 	 */
14381 	newipsq = phyint->phyint_ipsq;
14382 	newipsq->ipsq_writer = NULL;
14383 	newipsq->ipsq_reentry_cnt--;
14384 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14385 #ifdef DEBUG
14386 	newipsq->ipsq_depth = 0;
14387 #endif
14388 
14389 	return (SPLIT_SUCCESS);
14390 }
14391 
14392 /*
14393  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14394  * ipsq's representing their individual groups or themselves. Return
14395  * whether split needs to be retried again later.
14396  */
14397 static boolean_t
14398 ill_split_ipsq(ipsq_t *cur_ipsq)
14399 {
14400 	phyint_t *phyint;
14401 	phyint_t *next_phyint;
14402 	int	error;
14403 	boolean_t need_retry = B_FALSE;
14404 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14405 
14406 	phyint = cur_ipsq->ipsq_phyint_list;
14407 	cur_ipsq->ipsq_phyint_list = NULL;
14408 	while (phyint != NULL) {
14409 		next_phyint = phyint->phyint_ipsq_next;
14410 		/*
14411 		 * 'created' will tell us whether the callee actually
14412 		 * created an ipsq. Lack of memory may force the callee
14413 		 * to return without creating an ipsq.
14414 		 */
14415 		if (phyint->phyint_groupname == NULL) {
14416 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14417 		} else {
14418 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14419 			    need_retry, ipst);
14420 		}
14421 
14422 		switch (error) {
14423 		case SPLIT_FAILED:
14424 			need_retry = B_TRUE;
14425 			/* FALLTHRU */
14426 		case SPLIT_NOT_NEEDED:
14427 			/*
14428 			 * Keep it on the list.
14429 			 */
14430 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14431 			cur_ipsq->ipsq_phyint_list = phyint;
14432 			break;
14433 		case SPLIT_SUCCESS:
14434 			break;
14435 		default:
14436 			ASSERT(0);
14437 		}
14438 
14439 		phyint = next_phyint;
14440 	}
14441 	return (need_retry);
14442 }
14443 
14444 /*
14445  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14446  * and return the ills in the list. This list will be
14447  * needed to unlock all the ills later on by the caller.
14448  * The <ill-ipsq> associations could change between the
14449  * lock and unlock. Hence the unlock can't traverse the
14450  * ipsq to get the list of ills.
14451  */
14452 static int
14453 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14454 {
14455 	int	cnt = 0;
14456 	phyint_t	*phyint;
14457 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14458 
14459 	/*
14460 	 * The caller holds ill_g_lock to ensure that the ill memberships
14461 	 * of the ipsq don't change
14462 	 */
14463 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14464 
14465 	phyint = ipsq->ipsq_phyint_list;
14466 	while (phyint != NULL) {
14467 		if (phyint->phyint_illv4 != NULL) {
14468 			ASSERT(cnt < list_max);
14469 			list[cnt++] = phyint->phyint_illv4;
14470 		}
14471 		if (phyint->phyint_illv6 != NULL) {
14472 			ASSERT(cnt < list_max);
14473 			list[cnt++] = phyint->phyint_illv6;
14474 		}
14475 		phyint = phyint->phyint_ipsq_next;
14476 	}
14477 	ill_lock_ills(list, cnt);
14478 	return (cnt);
14479 }
14480 
14481 void
14482 ill_lock_ills(ill_t **list, int cnt)
14483 {
14484 	int	i;
14485 
14486 	if (cnt > 1) {
14487 		boolean_t try_again;
14488 		do {
14489 			try_again = B_FALSE;
14490 			for (i = 0; i < cnt - 1; i++) {
14491 				if (list[i] < list[i + 1]) {
14492 					ill_t	*tmp;
14493 
14494 					/* swap the elements */
14495 					tmp = list[i];
14496 					list[i] = list[i + 1];
14497 					list[i + 1] = tmp;
14498 					try_again = B_TRUE;
14499 				}
14500 			}
14501 		} while (try_again);
14502 	}
14503 
14504 	for (i = 0; i < cnt; i++) {
14505 		if (i == 0) {
14506 			if (list[i] != NULL)
14507 				mutex_enter(&list[i]->ill_lock);
14508 			else
14509 				return;
14510 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14511 			mutex_enter(&list[i]->ill_lock);
14512 		}
14513 	}
14514 }
14515 
14516 void
14517 ill_unlock_ills(ill_t **list, int cnt)
14518 {
14519 	int	i;
14520 
14521 	for (i = 0; i < cnt; i++) {
14522 		if ((i == 0) && (list[i] != NULL)) {
14523 			mutex_exit(&list[i]->ill_lock);
14524 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14525 			mutex_exit(&list[i]->ill_lock);
14526 		}
14527 	}
14528 }
14529 
14530 /*
14531  * Merge all the ills from 1 ipsq group into another ipsq group.
14532  * The source ipsq group is specified by the ipsq associated with
14533  * 'from_ill'. The destination ipsq group is specified by the ipsq
14534  * associated with 'to_ill' or 'groupname' respectively.
14535  * Note that ipsq itself does not have a reference count mechanism
14536  * and functions don't look up an ipsq and pass it around. Instead
14537  * functions pass around an ill or groupname, and the ipsq is looked
14538  * up from the ill or groupname and the required operation performed
14539  * atomically with the lookup on the ipsq.
14540  */
14541 static int
14542 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14543     queue_t *q)
14544 {
14545 	ipsq_t *old_ipsq;
14546 	ipsq_t *new_ipsq;
14547 	ill_t	**ill_list;
14548 	int	cnt;
14549 	size_t	ill_list_size;
14550 	boolean_t became_writer_on_new_sq = B_FALSE;
14551 	ip_stack_t	*ipst = from_ill->ill_ipst;
14552 
14553 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14554 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14555 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14556 
14557 	/*
14558 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14559 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14560 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14561 	 */
14562 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14563 
14564 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14565 	if (groupname != NULL)
14566 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14567 	else {
14568 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14569 	}
14570 
14571 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14572 
14573 	/*
14574 	 * both groups are on the same ipsq.
14575 	 */
14576 	if (old_ipsq == new_ipsq) {
14577 		rw_exit(&ipst->ips_ill_g_lock);
14578 		return (0);
14579 	}
14580 
14581 	cnt = old_ipsq->ipsq_refs << 1;
14582 	ill_list_size = cnt * sizeof (ill_t *);
14583 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14584 	if (ill_list == NULL) {
14585 		rw_exit(&ipst->ips_ill_g_lock);
14586 		return (ENOMEM);
14587 	}
14588 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14589 
14590 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14591 	mutex_enter(&new_ipsq->ipsq_lock);
14592 	if ((new_ipsq->ipsq_writer == NULL &&
14593 	    new_ipsq->ipsq_current_ipif == NULL) ||
14594 	    (new_ipsq->ipsq_writer == curthread)) {
14595 		new_ipsq->ipsq_writer = curthread;
14596 		new_ipsq->ipsq_reentry_cnt++;
14597 		became_writer_on_new_sq = B_TRUE;
14598 	}
14599 
14600 	/*
14601 	 * We are holding ill_g_lock as writer and all the ill locks of
14602 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14603 	 * message can land up on the old ipsq even though we don't hold the
14604 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14605 	 */
14606 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14607 
14608 	/*
14609 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14610 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14611 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14612 	 */
14613 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14614 
14615 	/*
14616 	 * Mark the new ipsq as needing a split since it is currently
14617 	 * being shared by more than 1 IPMP group. The split will
14618 	 * occur at the end of ipsq_exit
14619 	 */
14620 	new_ipsq->ipsq_split = B_TRUE;
14621 
14622 	/* Now release all the locks */
14623 	mutex_exit(&new_ipsq->ipsq_lock);
14624 	ill_unlock_ills(ill_list, cnt);
14625 	rw_exit(&ipst->ips_ill_g_lock);
14626 
14627 	kmem_free(ill_list, ill_list_size);
14628 
14629 	/*
14630 	 * If we succeeded in becoming writer on the new ipsq, then
14631 	 * drain the new ipsq and start processing  all enqueued messages
14632 	 * including the current ioctl we are processing which is either
14633 	 * a set groupname or failover/failback.
14634 	 */
14635 	if (became_writer_on_new_sq)
14636 		ipsq_exit(new_ipsq);
14637 
14638 	/*
14639 	 * syncq has been changed and all the messages have been moved.
14640 	 */
14641 	mutex_enter(&old_ipsq->ipsq_lock);
14642 	old_ipsq->ipsq_current_ipif = NULL;
14643 	old_ipsq->ipsq_current_ioctl = 0;
14644 	old_ipsq->ipsq_current_done = B_TRUE;
14645 	mutex_exit(&old_ipsq->ipsq_lock);
14646 	return (EINPROGRESS);
14647 }
14648 
14649 /*
14650  * Delete and add the loopback copy and non-loopback copy of
14651  * the BROADCAST ire corresponding to ill and addr. Used to
14652  * group broadcast ires together when ill becomes part of
14653  * a group.
14654  *
14655  * This function is also called when ill is leaving the group
14656  * so that the ires belonging to the group gets re-grouped.
14657  */
14658 static void
14659 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14660 {
14661 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14662 	ire_t **ire_ptpn = &ire_head;
14663 	ip_stack_t	*ipst = ill->ill_ipst;
14664 
14665 	/*
14666 	 * The loopback and non-loopback IREs are inserted in the order in which
14667 	 * they're found, on the basis that they are correctly ordered (loopback
14668 	 * first).
14669 	 */
14670 	for (;;) {
14671 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14672 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14673 		if (ire == NULL)
14674 			break;
14675 
14676 		/*
14677 		 * we are passing in KM_SLEEP because it is not easy to
14678 		 * go back to a sane state in case of memory failure.
14679 		 */
14680 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14681 		ASSERT(nire != NULL);
14682 		bzero(nire, sizeof (ire_t));
14683 		/*
14684 		 * Don't use ire_max_frag directly since we don't
14685 		 * hold on to 'ire' until we add the new ire 'nire' and
14686 		 * we don't want the new ire to have a dangling reference
14687 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14688 		 * be in sync with the ipif_mtu of the associate ipif.
14689 		 * For eg. this happens as a result of SIOCSLIFNAME,
14690 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14691 		 * the driver. A change in ire_max_frag triggered as
14692 		 * as a result of path mtu discovery, or due to an
14693 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14694 		 * route change -mtu command does not apply to broadcast ires.
14695 		 *
14696 		 * XXX We need a recovery strategy here if ire_init fails
14697 		 */
14698 		if (ire_init(nire,
14699 		    (uchar_t *)&ire->ire_addr,
14700 		    (uchar_t *)&ire->ire_mask,
14701 		    (uchar_t *)&ire->ire_src_addr,
14702 		    (uchar_t *)&ire->ire_gateway_addr,
14703 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14704 		    &ire->ire_ipif->ipif_mtu,
14705 		    ire->ire_nce,
14706 		    ire->ire_rfq,
14707 		    ire->ire_stq,
14708 		    ire->ire_type,
14709 		    ire->ire_ipif,
14710 		    ire->ire_cmask,
14711 		    ire->ire_phandle,
14712 		    ire->ire_ihandle,
14713 		    ire->ire_flags,
14714 		    &ire->ire_uinfo,
14715 		    NULL,
14716 		    NULL,
14717 		    ipst) == NULL) {
14718 			cmn_err(CE_PANIC, "ire_init() failed");
14719 		}
14720 		ire_delete(ire);
14721 		ire_refrele(ire);
14722 
14723 		/*
14724 		 * The newly created IREs are inserted at the tail of the list
14725 		 * starting with ire_head. As we've just allocated them no one
14726 		 * knows about them so it's safe.
14727 		 */
14728 		*ire_ptpn = nire;
14729 		ire_ptpn = &nire->ire_next;
14730 	}
14731 
14732 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14733 		int error;
14734 		ire_t *oire;
14735 		/* unlink the IRE from our list before calling ire_add() */
14736 		nire_next = nire->ire_next;
14737 		nire->ire_next = NULL;
14738 
14739 		/* ire_add adds the ire at the right place in the list */
14740 		oire = nire;
14741 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14742 		ASSERT(error == 0);
14743 		ASSERT(oire == nire);
14744 		ire_refrele(nire);	/* Held in ire_add */
14745 	}
14746 }
14747 
14748 /*
14749  * This function is usually called when an ill is inserted in
14750  * a group and all the ipifs are already UP. As all the ipifs
14751  * are already UP, the broadcast ires have already been created
14752  * and been inserted. But, ire_add_v4 would not have grouped properly.
14753  * We need to re-group for the benefit of ip_wput_ire which
14754  * expects BROADCAST ires to be grouped properly to avoid sending
14755  * more than one copy of the broadcast packet per group.
14756  *
14757  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14758  *	  because when ipif_up_done ends up calling this, ires have
14759  *        already been added before illgrp_insert i.e before ill_group
14760  *	  has been initialized.
14761  */
14762 static void
14763 ill_group_bcast_for_xmit(ill_t *ill)
14764 {
14765 	ill_group_t *illgrp;
14766 	ipif_t *ipif;
14767 	ipaddr_t addr;
14768 	ipaddr_t net_mask;
14769 	ipaddr_t subnet_netmask;
14770 
14771 	illgrp = ill->ill_group;
14772 
14773 	/*
14774 	 * This function is called even when an ill is deleted from
14775 	 * the group. Hence, illgrp could be null.
14776 	 */
14777 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14778 		return;
14779 
14780 	/*
14781 	 * Delete all the BROADCAST ires matching this ill and add
14782 	 * them back. This time, ire_add_v4 should take care of
14783 	 * grouping them with others because ill is part of the
14784 	 * group.
14785 	 */
14786 	ill_bcast_delete_and_add(ill, 0);
14787 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14788 
14789 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14790 
14791 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14792 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14793 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14794 		} else {
14795 			net_mask = htonl(IN_CLASSA_NET);
14796 		}
14797 		addr = net_mask & ipif->ipif_subnet;
14798 		ill_bcast_delete_and_add(ill, addr);
14799 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14800 
14801 		subnet_netmask = ipif->ipif_net_mask;
14802 		addr = ipif->ipif_subnet;
14803 		ill_bcast_delete_and_add(ill, addr);
14804 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14805 	}
14806 }
14807 
14808 /*
14809  * This function is called from illgrp_delete when ill is being deleted
14810  * from the group.
14811  *
14812  * As ill is not there in the group anymore, any address belonging
14813  * to this ill should be cleared of IRE_MARK_NORECV.
14814  */
14815 static void
14816 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14817 {
14818 	ire_t *ire;
14819 	irb_t *irb;
14820 	ip_stack_t	*ipst = ill->ill_ipst;
14821 
14822 	ASSERT(ill->ill_group == NULL);
14823 
14824 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14825 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14826 
14827 	if (ire != NULL) {
14828 		/*
14829 		 * IPMP and plumbing operations are serialized on the ipsq, so
14830 		 * no one will insert or delete a broadcast ire under our feet.
14831 		 */
14832 		irb = ire->ire_bucket;
14833 		rw_enter(&irb->irb_lock, RW_READER);
14834 		ire_refrele(ire);
14835 
14836 		for (; ire != NULL; ire = ire->ire_next) {
14837 			if (ire->ire_addr != addr)
14838 				break;
14839 			if (ire_to_ill(ire) != ill)
14840 				continue;
14841 
14842 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14843 			ire->ire_marks &= ~IRE_MARK_NORECV;
14844 		}
14845 		rw_exit(&irb->irb_lock);
14846 	}
14847 }
14848 
14849 ire_t *
14850 irep_insert(ill_group_t *illgrp, ipaddr_t addr, ire_t *ire, ire_t ***pirep)
14851 {
14852 	boolean_t first = B_TRUE;
14853 	ire_t *clear_ire = NULL;
14854 	ire_t *start_ire = NULL;
14855 	uint64_t match_flags;
14856 	uint64_t phyi_flags;
14857 	boolean_t fallback = B_FALSE;
14858 
14859 	/*
14860 	 * irb_lock must be held by the caller.
14861 	 * Get to the first ire matching the address and the
14862 	 * group. If the address does not match we are done
14863 	 * as we could not find the IRE. If the address matches
14864 	 * we should get to the first one matching the group.
14865 	 */
14866 	while (ire != NULL) {
14867 		if (ire->ire_addr != addr ||
14868 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14869 			break;
14870 		}
14871 		ire = ire->ire_next;
14872 	}
14873 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14874 	start_ire = ire;
14875 redo:
14876 	while (ire != NULL && ire->ire_addr == addr &&
14877 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14878 		/*
14879 		 * The first ire for any address within a group
14880 		 * should always be the one with IRE_MARK_NORECV cleared
14881 		 * so that ip_wput_ire can avoid searching for one.
14882 		 * Note down the insertion point which will be used
14883 		 * later.
14884 		 */
14885 		if (first && (*pirep == NULL))
14886 			*pirep = ire->ire_ptpn;
14887 		/*
14888 		 * PHYI_FAILED is set when the interface fails.
14889 		 * This interface might have become good, but the
14890 		 * daemon has not yet detected. We should still
14891 		 * not receive on this. PHYI_OFFLINE should never
14892 		 * be picked as this has been offlined and soon
14893 		 * be removed.
14894 		 */
14895 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14896 		if (phyi_flags & PHYI_OFFLINE) {
14897 			ire->ire_marks |= IRE_MARK_NORECV;
14898 			ire = ire->ire_next;
14899 			continue;
14900 		}
14901 		if (phyi_flags & match_flags) {
14902 			ire->ire_marks |= IRE_MARK_NORECV;
14903 			ire = ire->ire_next;
14904 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14905 			    PHYI_INACTIVE) {
14906 				fallback = B_TRUE;
14907 			}
14908 			continue;
14909 		}
14910 		if (first) {
14911 			/*
14912 			 * We will move this to the front of the list later
14913 			 * on.
14914 			 */
14915 			clear_ire = ire;
14916 			ire->ire_marks &= ~IRE_MARK_NORECV;
14917 		} else {
14918 			ire->ire_marks |= IRE_MARK_NORECV;
14919 		}
14920 		first = B_FALSE;
14921 		ire = ire->ire_next;
14922 	}
14923 	/*
14924 	 * If we never nominated anybody, try nominating at least
14925 	 * an INACTIVE, if we found one. Do it only once though.
14926 	 */
14927 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14928 	    fallback) {
14929 		match_flags = PHYI_FAILED;
14930 		ire = start_ire;
14931 		*pirep = NULL;
14932 		goto redo;
14933 	}
14934 	return (clear_ire);
14935 }
14936 
14937 /*
14938  * This function must be called only after the broadcast ires
14939  * have been grouped together. For a given address addr, nominate
14940  * only one of the ires whose interface is not FAILED or OFFLINE.
14941  *
14942  * This is also called when an ipif goes down, so that we can nominate
14943  * a different ire with the same address for receiving.
14944  */
14945 static void
14946 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
14947 {
14948 	irb_t *irb;
14949 	ire_t *ire;
14950 	ire_t *ire1;
14951 	ire_t *save_ire;
14952 	ire_t **irep = NULL;
14953 	ire_t *clear_ire = NULL;
14954 	ire_t	*new_lb_ire;
14955 	ire_t	*new_nlb_ire;
14956 	boolean_t new_lb_ire_used = B_FALSE;
14957 	boolean_t new_nlb_ire_used = B_FALSE;
14958 	boolean_t refrele_lb_ire = B_FALSE;
14959 	boolean_t refrele_nlb_ire = B_FALSE;
14960 	uint_t	max_frag;
14961 
14962 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14963 	    NULL, MATCH_IRE_TYPE, ipst);
14964 	/*
14965 	 * We may not be able to find some ires if a previous
14966 	 * ire_create failed. This happens when an ipif goes
14967 	 * down and we are unable to create BROADCAST ires due
14968 	 * to memory failure. Thus, we have to check for NULL
14969 	 * below. This should handle the case for LOOPBACK,
14970 	 * POINTOPOINT and interfaces with some POINTOPOINT
14971 	 * logicals for which there are no BROADCAST ires.
14972 	 */
14973 	if (ire == NULL)
14974 		return;
14975 	/*
14976 	 * Currently IRE_BROADCASTS are deleted when an ipif
14977 	 * goes down which runs exclusively. Thus, setting
14978 	 * IRE_MARK_RCVD should not race with ire_delete marking
14979 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14980 	 * be consistent with other parts of the code that walks
14981 	 * a given bucket.
14982 	 */
14983 	save_ire = ire;
14984 	irb = ire->ire_bucket;
14985 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14986 	if (new_lb_ire == NULL) {
14987 		ire_refrele(ire);
14988 		return;
14989 	}
14990 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14991 	if (new_nlb_ire == NULL) {
14992 		ire_refrele(ire);
14993 		kmem_cache_free(ire_cache, new_lb_ire);
14994 		return;
14995 	}
14996 	IRB_REFHOLD(irb);
14997 	rw_enter(&irb->irb_lock, RW_WRITER);
14998 	clear_ire = irep_insert(illgrp, addr, ire, &irep);
14999 
15000 	/*
15001 	 * irep non-NULL indicates that we entered the while loop
15002 	 * above. If clear_ire is at the insertion point, we don't
15003 	 * have to do anything. clear_ire will be NULL if all the
15004 	 * interfaces are failed.
15005 	 *
15006 	 * We cannot unlink and reinsert the ire at the right place
15007 	 * in the list since there can be other walkers of this bucket.
15008 	 * Instead we delete and recreate the ire
15009 	 */
15010 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15011 		ire_t *clear_ire_stq = NULL;
15012 		ire_t *clr_ire = NULL;
15013 		ire_t *ire_next = NULL;
15014 
15015 		if (clear_ire->ire_stq == NULL)
15016 			ire_next = clear_ire->ire_next;
15017 
15018 		rw_exit(&irb->irb_lock);
15019 
15020 		bzero(new_lb_ire, sizeof (ire_t));
15021 		/* XXX We need a recovery strategy here. */
15022 		if (ire_init(new_lb_ire,
15023 		    (uchar_t *)&clear_ire->ire_addr,
15024 		    (uchar_t *)&clear_ire->ire_mask,
15025 		    (uchar_t *)&clear_ire->ire_src_addr,
15026 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15027 		    &clear_ire->ire_max_frag,
15028 		    NULL, /* let ire_nce_init derive the resolver info */
15029 		    clear_ire->ire_rfq,
15030 		    clear_ire->ire_stq,
15031 		    clear_ire->ire_type,
15032 		    clear_ire->ire_ipif,
15033 		    clear_ire->ire_cmask,
15034 		    clear_ire->ire_phandle,
15035 		    clear_ire->ire_ihandle,
15036 		    clear_ire->ire_flags,
15037 		    &clear_ire->ire_uinfo,
15038 		    NULL,
15039 		    NULL,
15040 		    ipst) == NULL)
15041 			cmn_err(CE_PANIC, "ire_init() failed");
15042 
15043 		refrele_lb_ire = B_TRUE;
15044 
15045 		if (ire_next != NULL &&
15046 		    ire_next->ire_stq != NULL &&
15047 		    ire_next->ire_addr == clear_ire->ire_addr &&
15048 		    ire_next->ire_ipif->ipif_ill ==
15049 		    clear_ire->ire_ipif->ipif_ill) {
15050 			clear_ire_stq = ire_next;
15051 
15052 			bzero(new_nlb_ire, sizeof (ire_t));
15053 			/* XXX We need a recovery strategy here. */
15054 			if (ire_init(new_nlb_ire,
15055 			    (uchar_t *)&clear_ire_stq->ire_addr,
15056 			    (uchar_t *)&clear_ire_stq->ire_mask,
15057 			    (uchar_t *)&clear_ire_stq->ire_src_addr,
15058 			    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15059 			    &clear_ire_stq->ire_max_frag,
15060 			    NULL,
15061 			    clear_ire_stq->ire_rfq,
15062 			    clear_ire_stq->ire_stq,
15063 			    clear_ire_stq->ire_type,
15064 			    clear_ire_stq->ire_ipif,
15065 			    clear_ire_stq->ire_cmask,
15066 			    clear_ire_stq->ire_phandle,
15067 			    clear_ire_stq->ire_ihandle,
15068 			    clear_ire_stq->ire_flags,
15069 			    &clear_ire_stq->ire_uinfo,
15070 			    NULL,
15071 			    NULL,
15072 			    ipst) == NULL)
15073 				cmn_err(CE_PANIC, "ire_init() failed");
15074 
15075 				refrele_nlb_ire = B_TRUE;
15076 			}
15077 
15078 		rw_enter(&irb->irb_lock, RW_WRITER);
15079 		/*
15080 		 * irb_lock was dropped across call to ire_init() due to
15081 		 * lock ordering issue with ipst->ips_ndp{4,6}->ndp_g_lock
15082 		 * mutex lock. Therefore irep could have changed. call
15083 		 * irep_insert() to get the new insertion point (irep) and
15084 		 * recheck all known conditions.
15085 		 */
15086 		irep = NULL;
15087 		clr_ire = irep_insert(illgrp, addr, save_ire, &irep);
15088 		if ((irep != NULL) && (*irep != clear_ire) &&
15089 		    (clr_ire == clear_ire)) {
15090 			if ((clear_ire_stq != NULL) &&
15091 			    (clr_ire->ire_next != clear_ire_stq))
15092 				clear_ire_stq = NULL;
15093 			/*
15094 			 * Delete the ire. We can't call ire_delete() since
15095 			 * we are holding the bucket lock. We can't release the
15096 			 * bucket lock since we can't allow irep to change.
15097 			 * So just mark it CONDEMNED.
15098 			 * The IRB_REFRELE will delete the ire from the list
15099 			 * and do the refrele.
15100 			 */
15101 			clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15102 			irb->irb_marks |= IRB_MARK_CONDEMNED;
15103 
15104 			if (clear_ire_stq != NULL &&
15105 			    clear_ire_stq->ire_nce != NULL) {
15106 				nce_fastpath_list_delete(
15107 				    clear_ire_stq->ire_nce);
15108 				clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15109 			}
15110 
15111 			/*
15112 			 * Also take care of otherfields like ib/ob pkt count
15113 			 * etc. Need to dup them.
15114 			 * ditto in ill_bcast_delete_and_add
15115 			 */
15116 
15117 			/* Set the max_frag before adding the ire */
15118 			max_frag = *new_lb_ire->ire_max_fragp;
15119 			new_lb_ire->ire_max_fragp = NULL;
15120 			new_lb_ire->ire_max_frag = max_frag;
15121 
15122 			/* Add the new ire's. Insert at *irep */
15123 			new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15124 			ire1 = *irep;
15125 			if (ire1 != NULL)
15126 				ire1->ire_ptpn = &new_lb_ire->ire_next;
15127 			new_lb_ire->ire_next = ire1;
15128 			/* Link the new one in. */
15129 			new_lb_ire->ire_ptpn = irep;
15130 			membar_producer();
15131 			*irep = new_lb_ire;
15132 			new_lb_ire_used = B_TRUE;
15133 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15134 			    ire_stats_inserted);
15135 			new_lb_ire->ire_bucket->irb_ire_cnt++;
15136 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *),
15137 			    new_lb_ire->ire_ipif,
15138 			    (char *), "ire", (void *), new_lb_ire);
15139 			new_lb_ire->ire_ipif->ipif_ire_cnt++;
15140 
15141 			if (clear_ire_stq != NULL) {
15142 				ill_t	*ire_ill;
15143 				/* Set the max_frag before adding the ire */
15144 				max_frag = *new_nlb_ire->ire_max_fragp;
15145 				new_nlb_ire->ire_max_fragp = NULL;
15146 				new_nlb_ire->ire_max_frag = max_frag;
15147 
15148 				new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15149 				irep = &new_lb_ire->ire_next;
15150 				/* Add the new ire. Insert at *irep */
15151 				ire1 = *irep;
15152 				if (ire1 != NULL)
15153 					ire1->ire_ptpn = &new_nlb_ire->ire_next;
15154 				new_nlb_ire->ire_next = ire1;
15155 				/* Link the new one in. */
15156 				new_nlb_ire->ire_ptpn = irep;
15157 				membar_producer();
15158 				*irep = new_nlb_ire;
15159 				new_nlb_ire_used = B_TRUE;
15160 				BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15161 				    ire_stats_inserted);
15162 				new_nlb_ire->ire_bucket->irb_ire_cnt++;
15163 				DTRACE_PROBE3(ipif__incr__cnt,
15164 				    (ipif_t *), new_nlb_ire->ire_ipif,
15165 				    (char *), "ire", (void *), new_nlb_ire);
15166 				new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15167 				DTRACE_PROBE3(ill__incr__cnt,
15168 				    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15169 				    (char *), "ire", (void *), new_nlb_ire);
15170 				ire_ill = (ill_t *)new_nlb_ire->ire_stq->q_ptr;
15171 				ire_ill->ill_ire_cnt++;
15172 			}
15173 		}
15174 	}
15175 	ire_refrele(save_ire);
15176 	rw_exit(&irb->irb_lock);
15177 	/*
15178 	 * Since we dropped the irb_lock across call to ire_init()
15179 	 * and rechecking known conditions, it is possible that
15180 	 * the checks might fail, therefore undo the work done by
15181 	 * ire_init() by calling ire_refrele() on the newly created ire.
15182 	 */
15183 	if (!new_lb_ire_used) {
15184 		if (refrele_lb_ire) {
15185 			ire_refrele(new_lb_ire);
15186 		} else {
15187 			kmem_cache_free(ire_cache, new_lb_ire);
15188 		}
15189 	}
15190 	if (!new_nlb_ire_used) {
15191 		if (refrele_nlb_ire) {
15192 			ire_refrele(new_nlb_ire);
15193 		} else {
15194 			kmem_cache_free(ire_cache, new_nlb_ire);
15195 		}
15196 	}
15197 	IRB_REFRELE(irb);
15198 }
15199 
15200 /*
15201  * Whenever an ipif goes down we have to renominate a different
15202  * broadcast ire to receive. Whenever an ipif comes up, we need
15203  * to make sure that we have only one nominated to receive.
15204  */
15205 static void
15206 ipif_renominate_bcast(ipif_t *ipif)
15207 {
15208 	ill_t *ill = ipif->ipif_ill;
15209 	ipaddr_t subnet_addr;
15210 	ipaddr_t net_addr;
15211 	ipaddr_t net_mask = 0;
15212 	ipaddr_t subnet_netmask;
15213 	ipaddr_t addr;
15214 	ill_group_t *illgrp;
15215 	ip_stack_t	*ipst = ill->ill_ipst;
15216 
15217 	illgrp = ill->ill_group;
15218 	/*
15219 	 * If this is the last ipif going down, it might take
15220 	 * the ill out of the group. In that case ipif_down ->
15221 	 * illgrp_delete takes care of doing the nomination.
15222 	 * ipif_down does not call for this case.
15223 	 */
15224 	ASSERT(illgrp != NULL);
15225 
15226 	/* There could not have been any ires associated with this */
15227 	if (ipif->ipif_subnet == 0)
15228 		return;
15229 
15230 	ill_mark_bcast(illgrp, 0, ipst);
15231 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15232 
15233 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15234 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15235 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15236 	} else {
15237 		net_mask = htonl(IN_CLASSA_NET);
15238 	}
15239 	addr = net_mask & ipif->ipif_subnet;
15240 	ill_mark_bcast(illgrp, addr, ipst);
15241 
15242 	net_addr = ~net_mask | addr;
15243 	ill_mark_bcast(illgrp, net_addr, ipst);
15244 
15245 	subnet_netmask = ipif->ipif_net_mask;
15246 	addr = ipif->ipif_subnet;
15247 	ill_mark_bcast(illgrp, addr, ipst);
15248 
15249 	subnet_addr = ~subnet_netmask | addr;
15250 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15251 }
15252 
15253 /*
15254  * Whenever we form or delete ill groups, we need to nominate one set of
15255  * BROADCAST ires for receiving in the group.
15256  *
15257  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15258  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15259  *    for ill_ipif_up_count to be non-zero. This is the only case where
15260  *    ill_ipif_up_count is zero and we would still find the ires.
15261  *
15262  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15263  *    ipif is UP and we just have to do the nomination.
15264  *
15265  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15266  *    from the group. So, we have to do the nomination.
15267  *
15268  * Because of (3), there could be just one ill in the group. But we have
15269  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15270  * Thus, this function does not optimize when there is only one ill as
15271  * it is not correct for (3).
15272  */
15273 static void
15274 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15275 {
15276 	ill_t *ill;
15277 	ipif_t *ipif;
15278 	ipaddr_t subnet_addr;
15279 	ipaddr_t prev_subnet_addr = 0;
15280 	ipaddr_t net_addr;
15281 	ipaddr_t prev_net_addr = 0;
15282 	ipaddr_t net_mask = 0;
15283 	ipaddr_t subnet_netmask;
15284 	ipaddr_t addr;
15285 	ip_stack_t	*ipst;
15286 
15287 	/*
15288 	 * When the last memeber is leaving, there is nothing to
15289 	 * nominate.
15290 	 */
15291 	if (illgrp->illgrp_ill_count == 0) {
15292 		ASSERT(illgrp->illgrp_ill == NULL);
15293 		return;
15294 	}
15295 
15296 	ill = illgrp->illgrp_ill;
15297 	ASSERT(!ill->ill_isv6);
15298 	ipst = ill->ill_ipst;
15299 	/*
15300 	 * We assume that ires with same address and belonging to the
15301 	 * same group, has been grouped together. Nominating a *single*
15302 	 * ill in the group for sending and receiving broadcast is done
15303 	 * by making sure that the first BROADCAST ire (which will be
15304 	 * the one returned by ire_ctable_lookup for ip_rput and the
15305 	 * one that will be used in ip_wput_ire) will be the one that
15306 	 * will not have IRE_MARK_NORECV set.
15307 	 *
15308 	 * 1) ip_rput checks and discards packets received on ires marked
15309 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15310 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15311 	 *    first ire in the group for every broadcast address in the group.
15312 	 *    ip_rput will accept packets only on the first ire i.e only
15313 	 *    one copy of the ill.
15314 	 *
15315 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15316 	 *    packet for the whole group. It needs to send out on the ill
15317 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15318 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15319 	 *    the copy echoed back on other port where the ire is not marked
15320 	 *    with IRE_MARK_NORECV.
15321 	 *
15322 	 * Note that we just need to have the first IRE either loopback or
15323 	 * non-loopback (either of them may not exist if ire_create failed
15324 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15325 	 * always hit the first one and hence will always accept one copy.
15326 	 *
15327 	 * We have a broadcast ire per ill for all the unique prefixes
15328 	 * hosted on that ill. As we don't have a way of knowing the
15329 	 * unique prefixes on a given ill and hence in the whole group,
15330 	 * we just call ill_mark_bcast on all the prefixes that exist
15331 	 * in the group. For the common case of one prefix, the code
15332 	 * below optimizes by remebering the last address used for
15333 	 * markng. In the case of multiple prefixes, this will still
15334 	 * optimize depending the order of prefixes.
15335 	 *
15336 	 * The only unique address across the whole group is 0.0.0.0 and
15337 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15338 	 * the first ire in the bucket for receiving and disables the
15339 	 * others.
15340 	 */
15341 	ill_mark_bcast(illgrp, 0, ipst);
15342 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15343 	for (; ill != NULL; ill = ill->ill_group_next) {
15344 
15345 		for (ipif = ill->ill_ipif; ipif != NULL;
15346 		    ipif = ipif->ipif_next) {
15347 
15348 			if (!(ipif->ipif_flags & IPIF_UP) ||
15349 			    ipif->ipif_subnet == 0) {
15350 				continue;
15351 			}
15352 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15353 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15354 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15355 			} else {
15356 				net_mask = htonl(IN_CLASSA_NET);
15357 			}
15358 			addr = net_mask & ipif->ipif_subnet;
15359 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15360 				ill_mark_bcast(illgrp, addr, ipst);
15361 				net_addr = ~net_mask | addr;
15362 				ill_mark_bcast(illgrp, net_addr, ipst);
15363 			}
15364 			prev_net_addr = addr;
15365 
15366 			subnet_netmask = ipif->ipif_net_mask;
15367 			addr = ipif->ipif_subnet;
15368 			if (prev_subnet_addr == 0 ||
15369 			    prev_subnet_addr != addr) {
15370 				ill_mark_bcast(illgrp, addr, ipst);
15371 				subnet_addr = ~subnet_netmask | addr;
15372 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15373 			}
15374 			prev_subnet_addr = addr;
15375 		}
15376 	}
15377 }
15378 
15379 /*
15380  * This function is called while forming ill groups.
15381  *
15382  * Currently, we handle only allmulti groups. We want to join
15383  * allmulti on only one of the ills in the groups. In future,
15384  * when we have link aggregation, we may have to join normal
15385  * multicast groups on multiple ills as switch does inbound load
15386  * balancing. Following are the functions that calls this
15387  * function :
15388  *
15389  * 1) ill_recover_multicast : Interface is coming back UP.
15390  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15391  *    will call ill_recover_multicast to recover all the multicast
15392  *    groups. We need to make sure that only one member is joined
15393  *    in the ill group.
15394  *
15395  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15396  *    Somebody is joining allmulti. We need to make sure that only one
15397  *    member is joined in the group.
15398  *
15399  * 3) illgrp_insert : If allmulti has already joined, we need to make
15400  *    sure that only one member is joined in the group.
15401  *
15402  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15403  *    allmulti who we have nominated. We need to pick someother ill.
15404  *
15405  * 5) illgrp_delete : The ill we nominated is leaving the group,
15406  *    we need to pick a new ill to join the group.
15407  *
15408  * For (1), (2), (5) - we just have to check whether there is
15409  * a good ill joined in the group. If we could not find any ills
15410  * joined the group, we should join.
15411  *
15412  * For (4), the one that was nominated to receive, left the group.
15413  * There could be nobody joined in the group when this function is
15414  * called.
15415  *
15416  * For (3) - we need to explicitly check whether there are multiple
15417  * ills joined in the group.
15418  *
15419  * For simplicity, we don't differentiate any of the above cases. We
15420  * just leave the group if it is joined on any of them and join on
15421  * the first good ill.
15422  */
15423 int
15424 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15425 {
15426 	ilm_t *ilm;
15427 	ill_t *ill;
15428 	ill_t *fallback_inactive_ill = NULL;
15429 	ill_t *fallback_failed_ill = NULL;
15430 	int ret = 0;
15431 
15432 	/*
15433 	 * Leave the allmulti on all the ills and start fresh.
15434 	 */
15435 	for (ill = illgrp->illgrp_ill; ill != NULL;
15436 	    ill = ill->ill_group_next) {
15437 		if (ill->ill_join_allmulti)
15438 			ill_leave_allmulti(ill);
15439 	}
15440 
15441 	/*
15442 	 * Choose a good ill. Fallback to inactive or failed if
15443 	 * none available. We need to fallback to FAILED in the
15444 	 * case where we have 2 interfaces in a group - where
15445 	 * one of them is failed and another is a good one and
15446 	 * the good one (not marked inactive) is leaving the group.
15447 	 */
15448 	for (ill = illgrp->illgrp_ill; ill != NULL; ill = ill->ill_group_next) {
15449 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15450 			continue;
15451 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15452 			fallback_failed_ill = ill;
15453 			continue;
15454 		}
15455 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15456 			fallback_inactive_ill = ill;
15457 			continue;
15458 		}
15459 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15460 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15461 				ret = ill_join_allmulti(ill);
15462 				/*
15463 				 * ill_join_allmulti() can fail because of
15464 				 * memory failures so make sure we join at
15465 				 * least on one ill.
15466 				 */
15467 				if (ill->ill_join_allmulti)
15468 					return (0);
15469 			}
15470 		}
15471 	}
15472 	if (ret != 0) {
15473 		/*
15474 		 * If we tried nominating above and failed to do so,
15475 		 * return error. We might have tried multiple times.
15476 		 * But, return the latest error.
15477 		 */
15478 		return (ret);
15479 	}
15480 	if ((ill = fallback_inactive_ill) != NULL) {
15481 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15482 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr))
15483 				return (ill_join_allmulti(ill));
15484 		}
15485 	} else if ((ill = fallback_failed_ill) != NULL) {
15486 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15487 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr))
15488 				return (ill_join_allmulti(ill));
15489 		}
15490 	}
15491 	return (0);
15492 }
15493 
15494 /*
15495  * This function is called from illgrp_delete after it is
15496  * deleted from the group to reschedule responsibilities
15497  * to a different ill.
15498  */
15499 static void
15500 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15501 {
15502 	ilm_t	*ilm;
15503 	ipif_t	*ipif;
15504 	ipaddr_t subnet_addr;
15505 	ipaddr_t net_addr;
15506 	ipaddr_t net_mask = 0;
15507 	ipaddr_t subnet_netmask;
15508 	ipaddr_t addr;
15509 	ip_stack_t *ipst = ill->ill_ipst;
15510 
15511 	ASSERT(ill->ill_group == NULL);
15512 	/*
15513 	 * Broadcast Responsibility:
15514 	 *
15515 	 * 1. If this ill has been nominated for receiving broadcast
15516 	 * packets, we need to find a new one. Before we find a new
15517 	 * one, we need to re-group the ires that are part of this new
15518 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15519 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15520 	 * thing for us.
15521 	 *
15522 	 * 2. If this ill was not nominated for receiving broadcast
15523 	 * packets, we need to clear the IRE_MARK_NORECV flag
15524 	 * so that we continue to send up broadcast packets.
15525 	 */
15526 	if (!ill->ill_isv6) {
15527 		/*
15528 		 * Case 1 above : No optimization here. Just redo the
15529 		 * nomination.
15530 		 */
15531 		ill_group_bcast_for_xmit(ill);
15532 		ill_nominate_bcast_rcv(illgrp);
15533 
15534 		/*
15535 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15536 		 */
15537 		ill_clear_bcast_mark(ill, 0);
15538 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15539 
15540 		for (ipif = ill->ill_ipif; ipif != NULL;
15541 		    ipif = ipif->ipif_next) {
15542 
15543 			if (!(ipif->ipif_flags & IPIF_UP) ||
15544 			    ipif->ipif_subnet == 0) {
15545 				continue;
15546 			}
15547 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15548 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15549 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15550 			} else {
15551 				net_mask = htonl(IN_CLASSA_NET);
15552 			}
15553 			addr = net_mask & ipif->ipif_subnet;
15554 			ill_clear_bcast_mark(ill, addr);
15555 
15556 			net_addr = ~net_mask | addr;
15557 			ill_clear_bcast_mark(ill, net_addr);
15558 
15559 			subnet_netmask = ipif->ipif_net_mask;
15560 			addr = ipif->ipif_subnet;
15561 			ill_clear_bcast_mark(ill, addr);
15562 
15563 			subnet_addr = ~subnet_netmask | addr;
15564 			ill_clear_bcast_mark(ill, subnet_addr);
15565 		}
15566 	}
15567 
15568 	/*
15569 	 * Multicast Responsibility.
15570 	 *
15571 	 * If we have joined allmulti on this one, find a new member
15572 	 * in the group to join allmulti. As this ill is already part
15573 	 * of allmulti, we don't have to join on this one.
15574 	 *
15575 	 * If we have not joined allmulti on this one, there is no
15576 	 * responsibility to handoff. But we need to take new
15577 	 * responsibility i.e, join allmulti on this one if we need
15578 	 * to.
15579 	 */
15580 	if (ill->ill_join_allmulti) {
15581 		(void) ill_nominate_mcast_rcv(illgrp);
15582 	} else {
15583 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15584 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15585 				(void) ill_join_allmulti(ill);
15586 				break;
15587 			}
15588 		}
15589 	}
15590 
15591 	/*
15592 	 * We intentionally do the flushing of IRE_CACHES only matching
15593 	 * on the ill and not on groups. Note that we are already deleted
15594 	 * from the group.
15595 	 *
15596 	 * This will make sure that all IRE_CACHES whose stq is pointing
15597 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15598 	 * deleted and IRE_CACHES that are not pointing at this ill will
15599 	 * be left alone.
15600 	 */
15601 	ire_walk_ill(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
15602 	    illgrp_cache_delete, ill, ill);
15603 
15604 	/*
15605 	 * Some conn may have cached one of the IREs deleted above. By removing
15606 	 * the ire reference, we clean up the extra reference to the ill held in
15607 	 * ire->ire_stq.
15608 	 */
15609 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15610 
15611 	/*
15612 	 * Re-do source address selection for all the members in the
15613 	 * group, if they borrowed source address from one of the ipifs
15614 	 * in this ill.
15615 	 */
15616 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15617 		if (ill->ill_isv6) {
15618 			ipif_update_other_ipifs_v6(ipif, illgrp);
15619 		} else {
15620 			ipif_update_other_ipifs(ipif, illgrp);
15621 		}
15622 	}
15623 }
15624 
15625 /*
15626  * Delete the ill from the group. The caller makes sure that it is
15627  * in a group and it okay to delete from the group. So, we always
15628  * delete here.
15629  */
15630 static void
15631 illgrp_delete(ill_t *ill)
15632 {
15633 	ill_group_t *illgrp;
15634 	ill_group_t *tmpg;
15635 	ill_t *tmp_ill;
15636 	ip_stack_t	*ipst = ill->ill_ipst;
15637 
15638 	/*
15639 	 * Reset illgrp_ill_schednext if it was pointing at us.
15640 	 * We need to do this before we set ill_group to NULL.
15641 	 */
15642 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15643 	mutex_enter(&ill->ill_lock);
15644 
15645 	illgrp_reset_schednext(ill);
15646 
15647 	illgrp = ill->ill_group;
15648 
15649 	/* Delete the ill from illgrp. */
15650 	if (illgrp->illgrp_ill == ill) {
15651 		illgrp->illgrp_ill = ill->ill_group_next;
15652 	} else {
15653 		tmp_ill = illgrp->illgrp_ill;
15654 		while (tmp_ill->ill_group_next != ill) {
15655 			tmp_ill = tmp_ill->ill_group_next;
15656 			ASSERT(tmp_ill != NULL);
15657 		}
15658 		tmp_ill->ill_group_next = ill->ill_group_next;
15659 	}
15660 	ill->ill_group = NULL;
15661 	ill->ill_group_next = NULL;
15662 
15663 	illgrp->illgrp_ill_count--;
15664 	mutex_exit(&ill->ill_lock);
15665 	rw_exit(&ipst->ips_ill_g_lock);
15666 
15667 	/*
15668 	 * As this ill is leaving the group, we need to hand off
15669 	 * the responsibilities to the other ills in the group, if
15670 	 * this ill had some responsibilities.
15671 	 */
15672 
15673 	ill_handoff_responsibility(ill, illgrp);
15674 
15675 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15676 
15677 	if (illgrp->illgrp_ill_count == 0) {
15678 
15679 		ASSERT(illgrp->illgrp_ill == NULL);
15680 		if (ill->ill_isv6) {
15681 			if (illgrp == ipst->ips_illgrp_head_v6) {
15682 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15683 			} else {
15684 				tmpg = ipst->ips_illgrp_head_v6;
15685 				while (tmpg->illgrp_next != illgrp) {
15686 					tmpg = tmpg->illgrp_next;
15687 					ASSERT(tmpg != NULL);
15688 				}
15689 				tmpg->illgrp_next = illgrp->illgrp_next;
15690 			}
15691 		} else {
15692 			if (illgrp == ipst->ips_illgrp_head_v4) {
15693 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15694 			} else {
15695 				tmpg = ipst->ips_illgrp_head_v4;
15696 				while (tmpg->illgrp_next != illgrp) {
15697 					tmpg = tmpg->illgrp_next;
15698 					ASSERT(tmpg != NULL);
15699 				}
15700 				tmpg->illgrp_next = illgrp->illgrp_next;
15701 			}
15702 		}
15703 		mutex_destroy(&illgrp->illgrp_lock);
15704 		mi_free(illgrp);
15705 	}
15706 	rw_exit(&ipst->ips_ill_g_lock);
15707 
15708 	/*
15709 	 * Even though the ill is out of the group its not necessary
15710 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15711 	 * We will split the ipsq when phyint_groupname is set to NULL.
15712 	 */
15713 
15714 	/*
15715 	 * Send a routing sockets message if we are deleting from
15716 	 * groups with names.
15717 	 */
15718 	if (ill->ill_phyint->phyint_groupname_len != 0)
15719 		ip_rts_ifmsg(ill->ill_ipif);
15720 }
15721 
15722 /*
15723  * Re-do source address selection. This is normally called when
15724  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15725  * ipif comes up.
15726  */
15727 void
15728 ill_update_source_selection(ill_t *ill)
15729 {
15730 	ipif_t *ipif;
15731 
15732 	ASSERT(IAM_WRITER_ILL(ill));
15733 
15734 	if (ill->ill_group != NULL)
15735 		ill = ill->ill_group->illgrp_ill;
15736 
15737 	for (; ill != NULL; ill = ill->ill_group_next) {
15738 		for (ipif = ill->ill_ipif; ipif != NULL;
15739 		    ipif = ipif->ipif_next) {
15740 			if (ill->ill_isv6)
15741 				ipif_recreate_interface_routes_v6(NULL, ipif);
15742 			else
15743 				ipif_recreate_interface_routes(NULL, ipif);
15744 		}
15745 	}
15746 }
15747 
15748 /*
15749  * Insert ill in a group headed by illgrp_head. The caller can either
15750  * pass a groupname in which case we search for a group with the
15751  * same name to insert in or pass a group to insert in. This function
15752  * would only search groups with names.
15753  *
15754  * NOTE : The caller should make sure that there is at least one ipif
15755  *	  UP on this ill so that illgrp_scheduler can pick this ill
15756  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15757  *	  already sent a DL_UNBIND to the driver and we don't want to
15758  *	  send anymore packets. We don't assert for ipif_up_count
15759  *	  to be greater than zero, because ipif_up_done wants to call
15760  *	  this function before bumping up the ipif_up_count. See
15761  *	  ipif_up_done() for details.
15762  */
15763 int
15764 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15765     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15766 {
15767 	ill_group_t *illgrp;
15768 	ill_t *prev_ill;
15769 	phyint_t *phyi;
15770 	ip_stack_t	*ipst = ill->ill_ipst;
15771 
15772 	ASSERT(ill->ill_group == NULL);
15773 
15774 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15775 	mutex_enter(&ill->ill_lock);
15776 
15777 	if (groupname != NULL) {
15778 		/*
15779 		 * Look for a group with a matching groupname to insert.
15780 		 */
15781 		for (illgrp = *illgrp_head; illgrp != NULL;
15782 		    illgrp = illgrp->illgrp_next) {
15783 
15784 			ill_t *tmp_ill;
15785 
15786 			/*
15787 			 * If we have an ill_group_t in the list which has
15788 			 * no ill_t assigned then we must be in the process of
15789 			 * removing this group. We skip this as illgrp_delete()
15790 			 * will remove it from the list.
15791 			 */
15792 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15793 				ASSERT(illgrp->illgrp_ill_count == 0);
15794 				continue;
15795 			}
15796 
15797 			ASSERT(tmp_ill->ill_phyint != NULL);
15798 			phyi = tmp_ill->ill_phyint;
15799 			/*
15800 			 * Look at groups which has names only.
15801 			 */
15802 			if (phyi->phyint_groupname_len == 0)
15803 				continue;
15804 			/*
15805 			 * Names are stored in the phyint common to both
15806 			 * IPv4 and IPv6.
15807 			 */
15808 			if (mi_strcmp(phyi->phyint_groupname,
15809 			    groupname) == 0) {
15810 				break;
15811 			}
15812 		}
15813 	} else {
15814 		/*
15815 		 * If the caller passes in a NULL "grp_to_insert", we
15816 		 * allocate one below and insert this singleton.
15817 		 */
15818 		illgrp = grp_to_insert;
15819 	}
15820 
15821 	ill->ill_group_next = NULL;
15822 
15823 	if (illgrp == NULL) {
15824 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15825 		if (illgrp == NULL) {
15826 			return (ENOMEM);
15827 		}
15828 		illgrp->illgrp_next = *illgrp_head;
15829 		*illgrp_head = illgrp;
15830 		illgrp->illgrp_ill = ill;
15831 		illgrp->illgrp_ill_count = 1;
15832 		ill->ill_group = illgrp;
15833 		/*
15834 		 * Used in illgrp_scheduler to protect multiple threads
15835 		 * from traversing the list.
15836 		 */
15837 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15838 	} else {
15839 		ASSERT(ill->ill_net_type ==
15840 		    illgrp->illgrp_ill->ill_net_type);
15841 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15842 
15843 		/* Insert ill at tail of this group */
15844 		prev_ill = illgrp->illgrp_ill;
15845 		while (prev_ill->ill_group_next != NULL)
15846 			prev_ill = prev_ill->ill_group_next;
15847 		prev_ill->ill_group_next = ill;
15848 		ill->ill_group = illgrp;
15849 		illgrp->illgrp_ill_count++;
15850 		/*
15851 		 * Inherit group properties. Currently only forwarding
15852 		 * is the property we try to keep the same with all the
15853 		 * ills. When there are more, we will abstract this into
15854 		 * a function.
15855 		 */
15856 		ill->ill_flags &= ~ILLF_ROUTER;
15857 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15858 	}
15859 	mutex_exit(&ill->ill_lock);
15860 	rw_exit(&ipst->ips_ill_g_lock);
15861 
15862 	/*
15863 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15864 	 *    may be zero as it has not yet been bumped. But the ires
15865 	 *    have already been added. So, we do the nomination here
15866 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15867 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15868 	 *    ill_ipif_up_count here while nominating broadcast ires for
15869 	 *    receive.
15870 	 *
15871 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15872 	 *    to group them properly as ire_add() has already happened
15873 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15874 	 *    case, we need to do it here anyway.
15875 	 */
15876 	if (!ill->ill_isv6) {
15877 		ill_group_bcast_for_xmit(ill);
15878 		ill_nominate_bcast_rcv(illgrp);
15879 	}
15880 
15881 	if (!ipif_is_coming_up) {
15882 		/*
15883 		 * When ipif_up_done() calls this function, the multicast
15884 		 * groups have not been joined yet. So, there is no point in
15885 		 * nomination. ill_join_allmulti() will handle groups when
15886 		 * ill_recover_multicast() is called from ipif_up_done() later.
15887 		 */
15888 		(void) ill_nominate_mcast_rcv(illgrp);
15889 		/*
15890 		 * ipif_up_done calls ill_update_source_selection
15891 		 * anyway. Moreover, we don't want to re-create
15892 		 * interface routes while ipif_up_done() still has reference
15893 		 * to them. Refer to ipif_up_done() for more details.
15894 		 */
15895 		ill_update_source_selection(ill);
15896 	}
15897 
15898 	/*
15899 	 * Send a routing sockets message if we are inserting into
15900 	 * groups with names.
15901 	 */
15902 	if (groupname != NULL)
15903 		ip_rts_ifmsg(ill->ill_ipif);
15904 	return (0);
15905 }
15906 
15907 /*
15908  * Return the first phyint matching the groupname. There could
15909  * be more than one when there are ill groups.
15910  *
15911  * If 'usable' is set, then we exclude ones that are marked with any of
15912  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
15913  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
15914  * emulation of ipmp.
15915  */
15916 phyint_t *
15917 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
15918 {
15919 	phyint_t *phyi;
15920 
15921 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
15922 	/*
15923 	 * Group names are stored in the phyint - a common structure
15924 	 * to both IPv4 and IPv6.
15925 	 */
15926 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
15927 	for (; phyi != NULL;
15928 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15929 	    phyi, AVL_AFTER)) {
15930 		if (phyi->phyint_groupname_len == 0)
15931 			continue;
15932 		/*
15933 		 * Skip the ones that should not be used since the callers
15934 		 * sometime use this for sending packets.
15935 		 */
15936 		if (usable && (phyi->phyint_flags &
15937 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
15938 			continue;
15939 
15940 		ASSERT(phyi->phyint_groupname != NULL);
15941 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15942 			return (phyi);
15943 	}
15944 	return (NULL);
15945 }
15946 
15947 
15948 /*
15949  * Return the first usable phyint matching the group index. By 'usable'
15950  * we exclude ones that are marked ununsable with any of
15951  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
15952  *
15953  * Used only for the ipmp/netinfo emulation of ipmp.
15954  */
15955 phyint_t *
15956 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
15957 {
15958 	phyint_t *phyi;
15959 
15960 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
15961 
15962 	if (!ipst->ips_ipmp_hook_emulation)
15963 		return (NULL);
15964 
15965 	/*
15966 	 * Group indicies are stored in the phyint - a common structure
15967 	 * to both IPv4 and IPv6.
15968 	 */
15969 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
15970 	for (; phyi != NULL;
15971 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15972 	    phyi, AVL_AFTER)) {
15973 		/* Ignore the ones that do not have a group */
15974 		if (phyi->phyint_groupname_len == 0)
15975 			continue;
15976 
15977 		ASSERT(phyi->phyint_group_ifindex != 0);
15978 		/*
15979 		 * Skip the ones that should not be used since the callers
15980 		 * sometime use this for sending packets.
15981 		 */
15982 		if (phyi->phyint_flags &
15983 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
15984 			continue;
15985 		if (phyi->phyint_group_ifindex == group_ifindex)
15986 			return (phyi);
15987 	}
15988 	return (NULL);
15989 }
15990 
15991 /*
15992  * MT notes on creation and deletion of IPMP groups
15993  *
15994  * Creation and deletion of IPMP groups introduce the need to merge or
15995  * split the associated serialization objects i.e the ipsq's. Normally all
15996  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15997  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15998  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15999  * is a need to change the <ill-ipsq> association and we have to operate on both
16000  * the source and destination IPMP groups. For eg. attempting to set the
16001  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16002  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16003  * source or destination IPMP group are mapped to a single ipsq for executing
16004  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16005  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16006  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16007  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16008  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16009  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16010  *
16011  * In the above example the ioctl handling code locates the current ipsq of hme0
16012  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16013  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16014  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16015  * the destination ipsq. If the destination ipsq is not busy, it also enters
16016  * the destination ipsq exclusively. Now the actual groupname setting operation
16017  * can proceed. If the destination ipsq is busy, the operation is enqueued
16018  * on the destination (merged) ipsq and will be handled in the unwind from
16019  * ipsq_exit.
16020  *
16021  * To prevent other threads accessing the ill while the group name change is
16022  * in progres, we bring down the ipifs which also removes the ill from the
16023  * group. The group is changed in phyint and when the first ipif on the ill
16024  * is brought up, the ill is inserted into the right IPMP group by
16025  * illgrp_insert.
16026  */
16027 /* ARGSUSED */
16028 int
16029 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16030     ip_ioctl_cmd_t *ipip, void *ifreq)
16031 {
16032 	int i;
16033 	char *tmp;
16034 	int namelen;
16035 	ill_t *ill = ipif->ipif_ill;
16036 	ill_t *ill_v4, *ill_v6;
16037 	int err = 0;
16038 	phyint_t *phyi;
16039 	phyint_t *phyi_tmp;
16040 	struct lifreq *lifr;
16041 	mblk_t	*mp1;
16042 	char *groupname;
16043 	ipsq_t *ipsq;
16044 	ip_stack_t	*ipst = ill->ill_ipst;
16045 
16046 	ASSERT(IAM_WRITER_IPIF(ipif));
16047 
16048 	/* Existance verified in ip_wput_nondata */
16049 	mp1 = mp->b_cont->b_cont;
16050 	lifr = (struct lifreq *)mp1->b_rptr;
16051 	groupname = lifr->lifr_groupname;
16052 
16053 	if (ipif->ipif_id != 0)
16054 		return (EINVAL);
16055 
16056 	phyi = ill->ill_phyint;
16057 	ASSERT(phyi != NULL);
16058 
16059 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16060 		return (EINVAL);
16061 
16062 	tmp = groupname;
16063 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16064 		;
16065 
16066 	if (i == LIFNAMSIZ) {
16067 		/* no null termination */
16068 		return (EINVAL);
16069 	}
16070 
16071 	/*
16072 	 * Calculate the namelen exclusive of the null
16073 	 * termination character.
16074 	 */
16075 	namelen = tmp - groupname;
16076 
16077 	ill_v4 = phyi->phyint_illv4;
16078 	ill_v6 = phyi->phyint_illv6;
16079 
16080 	/*
16081 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16082 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16083 	 * synchronization notes in ip.c
16084 	 */
16085 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16086 		return (EINVAL);
16087 	}
16088 
16089 	/*
16090 	 * mark the ill as changing.
16091 	 * this should queue all new requests on the syncq.
16092 	 */
16093 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16094 
16095 	if (ill_v4 != NULL)
16096 		ill_v4->ill_state_flags |= ILL_CHANGING;
16097 	if (ill_v6 != NULL)
16098 		ill_v6->ill_state_flags |= ILL_CHANGING;
16099 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16100 
16101 	if (namelen == 0) {
16102 		/*
16103 		 * Null string means remove this interface from the
16104 		 * existing group.
16105 		 */
16106 		if (phyi->phyint_groupname_len == 0) {
16107 			/*
16108 			 * Never was in a group.
16109 			 */
16110 			err = 0;
16111 			goto done;
16112 		}
16113 
16114 		/*
16115 		 * IPv4 or IPv6 may be temporarily out of the group when all
16116 		 * the ipifs are down. Thus, we need to check for ill_group to
16117 		 * be non-NULL.
16118 		 */
16119 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16120 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16121 			mutex_enter(&ill_v4->ill_lock);
16122 			if (!ill_is_quiescent(ill_v4)) {
16123 				/*
16124 				 * ipsq_pending_mp_add will not fail since
16125 				 * connp is NULL
16126 				 */
16127 				(void) ipsq_pending_mp_add(NULL,
16128 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16129 				mutex_exit(&ill_v4->ill_lock);
16130 				err = EINPROGRESS;
16131 				goto done;
16132 			}
16133 			mutex_exit(&ill_v4->ill_lock);
16134 		}
16135 
16136 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16137 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16138 			mutex_enter(&ill_v6->ill_lock);
16139 			if (!ill_is_quiescent(ill_v6)) {
16140 				(void) ipsq_pending_mp_add(NULL,
16141 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16142 				mutex_exit(&ill_v6->ill_lock);
16143 				err = EINPROGRESS;
16144 				goto done;
16145 			}
16146 			mutex_exit(&ill_v6->ill_lock);
16147 		}
16148 
16149 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16150 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16151 		mutex_enter(&phyi->phyint_lock);
16152 		ASSERT(phyi->phyint_groupname != NULL);
16153 		mi_free(phyi->phyint_groupname);
16154 		phyi->phyint_groupname = NULL;
16155 		phyi->phyint_groupname_len = 0;
16156 
16157 		/* Restore the ifindex used to be the per interface one */
16158 		phyi->phyint_group_ifindex = 0;
16159 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16160 		mutex_exit(&phyi->phyint_lock);
16161 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16162 		rw_exit(&ipst->ips_ill_g_lock);
16163 		err = ill_up_ipifs(ill, q, mp);
16164 
16165 		/*
16166 		 * set the split flag so that the ipsq can be split
16167 		 */
16168 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16169 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16170 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16171 
16172 	} else {
16173 		if (phyi->phyint_groupname_len != 0) {
16174 			ASSERT(phyi->phyint_groupname != NULL);
16175 			/* Are we inserting in the same group ? */
16176 			if (mi_strcmp(groupname,
16177 			    phyi->phyint_groupname) == 0) {
16178 				err = 0;
16179 				goto done;
16180 			}
16181 		}
16182 
16183 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16184 		/*
16185 		 * Merge ipsq for the group's.
16186 		 * This check is here as multiple groups/ills might be
16187 		 * sharing the same ipsq.
16188 		 * If we have to merege than the operation is restarted
16189 		 * on the new ipsq.
16190 		 */
16191 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16192 		if (phyi->phyint_ipsq != ipsq) {
16193 			rw_exit(&ipst->ips_ill_g_lock);
16194 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16195 			goto done;
16196 		}
16197 		/*
16198 		 * Running exclusive on new ipsq.
16199 		 */
16200 
16201 		ASSERT(ipsq != NULL);
16202 		ASSERT(ipsq->ipsq_writer == curthread);
16203 
16204 		/*
16205 		 * Check whether the ill_type and ill_net_type matches before
16206 		 * we allocate any memory so that the cleanup is easier.
16207 		 *
16208 		 * We can't group dissimilar ones as we can't load spread
16209 		 * packets across the group because of potential link-level
16210 		 * header differences.
16211 		 */
16212 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16213 		if (phyi_tmp != NULL) {
16214 			if ((ill_v4 != NULL &&
16215 			    phyi_tmp->phyint_illv4 != NULL) &&
16216 			    ((ill_v4->ill_net_type !=
16217 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16218 			    (ill_v4->ill_type !=
16219 			    phyi_tmp->phyint_illv4->ill_type))) {
16220 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16221 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16222 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16223 				rw_exit(&ipst->ips_ill_g_lock);
16224 				return (EINVAL);
16225 			}
16226 			if ((ill_v6 != NULL &&
16227 			    phyi_tmp->phyint_illv6 != NULL) &&
16228 			    ((ill_v6->ill_net_type !=
16229 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16230 			    (ill_v6->ill_type !=
16231 			    phyi_tmp->phyint_illv6->ill_type))) {
16232 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16233 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16234 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16235 				rw_exit(&ipst->ips_ill_g_lock);
16236 				return (EINVAL);
16237 			}
16238 		}
16239 
16240 		rw_exit(&ipst->ips_ill_g_lock);
16241 
16242 		/*
16243 		 * bring down all v4 ipifs.
16244 		 */
16245 		if (ill_v4 != NULL) {
16246 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16247 		}
16248 
16249 		/*
16250 		 * bring down all v6 ipifs.
16251 		 */
16252 		if (ill_v6 != NULL) {
16253 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16254 		}
16255 
16256 		/*
16257 		 * make sure all ipifs are down and there are no active
16258 		 * references. Call to ipsq_pending_mp_add will not fail
16259 		 * since connp is NULL.
16260 		 */
16261 		if (ill_v4 != NULL) {
16262 			mutex_enter(&ill_v4->ill_lock);
16263 			if (!ill_is_quiescent(ill_v4)) {
16264 				(void) ipsq_pending_mp_add(NULL,
16265 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16266 				mutex_exit(&ill_v4->ill_lock);
16267 				err = EINPROGRESS;
16268 				goto done;
16269 			}
16270 			mutex_exit(&ill_v4->ill_lock);
16271 		}
16272 
16273 		if (ill_v6 != NULL) {
16274 			mutex_enter(&ill_v6->ill_lock);
16275 			if (!ill_is_quiescent(ill_v6)) {
16276 				(void) ipsq_pending_mp_add(NULL,
16277 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16278 				mutex_exit(&ill_v6->ill_lock);
16279 				err = EINPROGRESS;
16280 				goto done;
16281 			}
16282 			mutex_exit(&ill_v6->ill_lock);
16283 		}
16284 
16285 		/*
16286 		 * allocate including space for null terminator
16287 		 * before we insert.
16288 		 */
16289 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16290 		if (tmp == NULL)
16291 			return (ENOMEM);
16292 
16293 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16294 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16295 		mutex_enter(&phyi->phyint_lock);
16296 		if (phyi->phyint_groupname_len != 0) {
16297 			ASSERT(phyi->phyint_groupname != NULL);
16298 			mi_free(phyi->phyint_groupname);
16299 		}
16300 
16301 		/*
16302 		 * setup the new group name.
16303 		 */
16304 		phyi->phyint_groupname = tmp;
16305 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16306 		phyi->phyint_groupname_len = namelen + 1;
16307 
16308 		if (ipst->ips_ipmp_hook_emulation) {
16309 			/*
16310 			 * If the group already exists we use the existing
16311 			 * group_ifindex, otherwise we pick a new index here.
16312 			 */
16313 			if (phyi_tmp != NULL) {
16314 				phyi->phyint_group_ifindex =
16315 				    phyi_tmp->phyint_group_ifindex;
16316 			} else {
16317 				/* XXX We need a recovery strategy here. */
16318 				if (!ip_assign_ifindex(
16319 				    &phyi->phyint_group_ifindex, ipst))
16320 					cmn_err(CE_PANIC,
16321 					    "ip_assign_ifindex() failed");
16322 			}
16323 		}
16324 		/*
16325 		 * Select whether the netinfo and hook use the per-interface
16326 		 * or per-group ifindex.
16327 		 */
16328 		if (ipst->ips_ipmp_hook_emulation)
16329 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16330 		else
16331 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16332 
16333 		if (ipst->ips_ipmp_hook_emulation &&
16334 		    phyi_tmp != NULL) {
16335 			/* First phyint in group - group PLUMB event */
16336 			ill_nic_event_plumb(ill, B_TRUE);
16337 		}
16338 		mutex_exit(&phyi->phyint_lock);
16339 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16340 		rw_exit(&ipst->ips_ill_g_lock);
16341 
16342 		err = ill_up_ipifs(ill, q, mp);
16343 	}
16344 
16345 done:
16346 	/*
16347 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16348 	 */
16349 	if (err != EINPROGRESS) {
16350 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16351 		if (ill_v4 != NULL)
16352 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16353 		if (ill_v6 != NULL)
16354 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16355 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16356 	}
16357 	return (err);
16358 }
16359 
16360 /* ARGSUSED */
16361 int
16362 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16363     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16364 {
16365 	ill_t *ill;
16366 	phyint_t *phyi;
16367 	struct lifreq *lifr;
16368 	mblk_t	*mp1;
16369 
16370 	/* Existence verified in ip_wput_nondata */
16371 	mp1 = mp->b_cont->b_cont;
16372 	lifr = (struct lifreq *)mp1->b_rptr;
16373 	ill = ipif->ipif_ill;
16374 	phyi = ill->ill_phyint;
16375 
16376 	lifr->lifr_groupname[0] = '\0';
16377 	/*
16378 	 * ill_group may be null if all the interfaces
16379 	 * are down. But still, the phyint should always
16380 	 * hold the name.
16381 	 */
16382 	if (phyi->phyint_groupname_len != 0) {
16383 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16384 		    phyi->phyint_groupname_len);
16385 	}
16386 
16387 	return (0);
16388 }
16389 
16390 
16391 typedef struct conn_move_s {
16392 	ill_t	*cm_from_ill;
16393 	ill_t	*cm_to_ill;
16394 	int	cm_ifindex;
16395 } conn_move_t;
16396 
16397 /*
16398  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16399  */
16400 static void
16401 conn_move(conn_t *connp, caddr_t arg)
16402 {
16403 	conn_move_t *connm;
16404 	int ifindex;
16405 	int i;
16406 	ill_t *from_ill;
16407 	ill_t *to_ill;
16408 	ilg_t *ilg;
16409 	ilm_t *ret_ilm;
16410 
16411 	connm = (conn_move_t *)arg;
16412 	ifindex = connm->cm_ifindex;
16413 	from_ill = connm->cm_from_ill;
16414 	to_ill = connm->cm_to_ill;
16415 
16416 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16417 
16418 	/* All multicast fields protected by conn_lock */
16419 	mutex_enter(&connp->conn_lock);
16420 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16421 	if ((connp->conn_outgoing_ill == from_ill) &&
16422 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16423 		connp->conn_outgoing_ill = to_ill;
16424 		connp->conn_incoming_ill = to_ill;
16425 	}
16426 
16427 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16428 
16429 	if ((connp->conn_multicast_ill == from_ill) &&
16430 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16431 		connp->conn_multicast_ill = connm->cm_to_ill;
16432 	}
16433 
16434 	/*
16435 	 * Change the ilg_ill to point to the new one. This assumes
16436 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16437 	 * has been told to receive packets on this interface.
16438 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16439 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16440 	 * some ilms may not have moved. We check to see whether
16441 	 * the ilms have moved to to_ill. We can't check on from_ill
16442 	 * as in the process of moving, we could have split an ilm
16443 	 * in to two - which has the same orig_ifindex and v6group.
16444 	 *
16445 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16446 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16447 	 */
16448 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16449 		ilg = &connp->conn_ilg[i];
16450 		if ((ilg->ilg_ill == from_ill) &&
16451 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16452 			/* ifindex != 0 indicates failback */
16453 			if (ifindex != 0) {
16454 				connp->conn_ilg[i].ilg_ill = to_ill;
16455 				continue;
16456 			}
16457 
16458 			mutex_enter(&to_ill->ill_lock);
16459 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16460 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16461 			    connp->conn_zoneid);
16462 			mutex_exit(&to_ill->ill_lock);
16463 
16464 			if (ret_ilm != NULL)
16465 				connp->conn_ilg[i].ilg_ill = to_ill;
16466 		}
16467 	}
16468 	mutex_exit(&connp->conn_lock);
16469 }
16470 
16471 static void
16472 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16473 {
16474 	conn_move_t connm;
16475 	ip_stack_t	*ipst = from_ill->ill_ipst;
16476 
16477 	connm.cm_from_ill = from_ill;
16478 	connm.cm_to_ill = to_ill;
16479 	connm.cm_ifindex = ifindex;
16480 
16481 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16482 }
16483 
16484 /*
16485  * ilm has been moved from from_ill to to_ill.
16486  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16487  * appropriately.
16488  *
16489  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16490  *	  the code there de-references ipif_ill to get the ill to
16491  *	  send multicast requests. It does not work as ipif is on its
16492  *	  move and already moved when this function is called.
16493  *	  Thus, we need to use from_ill and to_ill send down multicast
16494  *	  requests.
16495  */
16496 static void
16497 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16498 {
16499 	ipif_t *ipif;
16500 	ilm_t *ilm;
16501 
16502 	/*
16503 	 * See whether we need to send down DL_ENABMULTI_REQ on
16504 	 * to_ill as ilm has just been added.
16505 	 */
16506 	ASSERT(IAM_WRITER_ILL(to_ill));
16507 	ASSERT(IAM_WRITER_ILL(from_ill));
16508 
16509 	ILM_WALKER_HOLD(to_ill);
16510 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16511 
16512 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16513 			continue;
16514 		/*
16515 		 * no locks held, ill/ipif cannot dissappear as long
16516 		 * as we are writer.
16517 		 */
16518 		ipif = to_ill->ill_ipif;
16519 		/*
16520 		 * No need to hold any lock as we are the writer and this
16521 		 * can only be changed by a writer.
16522 		 */
16523 		ilm->ilm_is_new = B_FALSE;
16524 
16525 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16526 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16527 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16528 			    "resolver\n"));
16529 			continue;		/* Must be IRE_IF_NORESOLVER */
16530 		}
16531 
16532 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16533 			ip1dbg(("ilm_send_multicast_reqs: "
16534 			    "to_ill MULTI_BCAST\n"));
16535 			goto from;
16536 		}
16537 
16538 		if (to_ill->ill_isv6)
16539 			mld_joingroup(ilm);
16540 		else
16541 			igmp_joingroup(ilm);
16542 
16543 		if (to_ill->ill_ipif_up_count == 0) {
16544 			/*
16545 			 * Nobody there. All multicast addresses will be
16546 			 * re-joined when we get the DL_BIND_ACK bringing the
16547 			 * interface up.
16548 			 */
16549 			ilm->ilm_notify_driver = B_FALSE;
16550 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16551 			goto from;
16552 		}
16553 
16554 		/*
16555 		 * For allmulti address, we want to join on only one interface.
16556 		 * Checking for ilm_numentries_v6 is not correct as you may
16557 		 * find an ilm with zero address on to_ill, but we may not
16558 		 * have nominated to_ill for receiving. Thus, if we have
16559 		 * nominated from_ill (ill_join_allmulti is set), nominate
16560 		 * only if to_ill is not already nominated (to_ill normally
16561 		 * should not have been nominated if "from_ill" has already
16562 		 * been nominated. As we don't prevent failovers from happening
16563 		 * across groups, we don't assert).
16564 		 */
16565 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16566 			/*
16567 			 * There is no need to hold ill locks as we are
16568 			 * writer on both ills and when ill_join_allmulti()
16569 			 * is called the thread is always a writer.
16570 			 */
16571 			if (from_ill->ill_join_allmulti &&
16572 			    !to_ill->ill_join_allmulti) {
16573 				(void) ill_join_allmulti(to_ill);
16574 			}
16575 		} else if (ilm->ilm_notify_driver) {
16576 
16577 			/*
16578 			 * This is a newly moved ilm so we need to tell the
16579 			 * driver about the new group. There can be more than
16580 			 * one ilm's for the same group in the list each with a
16581 			 * different orig_ifindex. We have to inform the driver
16582 			 * once. In ilm_move_v[4,6] we only set the flag
16583 			 * ilm_notify_driver for the first ilm.
16584 			 */
16585 
16586 			(void) ip_ll_send_enabmulti_req(to_ill,
16587 			    &ilm->ilm_v6addr);
16588 		}
16589 
16590 		ilm->ilm_notify_driver = B_FALSE;
16591 
16592 		/*
16593 		 * See whether we need to send down DL_DISABMULTI_REQ on
16594 		 * from_ill as ilm has just been removed.
16595 		 */
16596 from:
16597 		ipif = from_ill->ill_ipif;
16598 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16599 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16600 			ip1dbg(("ilm_send_multicast_reqs: "
16601 			    "from_ill not resolver\n"));
16602 			continue;		/* Must be IRE_IF_NORESOLVER */
16603 		}
16604 
16605 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16606 			ip1dbg(("ilm_send_multicast_reqs: "
16607 			    "from_ill MULTI_BCAST\n"));
16608 			continue;
16609 		}
16610 
16611 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16612 			if (from_ill->ill_join_allmulti)
16613 				ill_leave_allmulti(from_ill);
16614 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16615 			(void) ip_ll_send_disabmulti_req(from_ill,
16616 			    &ilm->ilm_v6addr);
16617 		}
16618 	}
16619 	ILM_WALKER_RELE(to_ill);
16620 }
16621 
16622 /*
16623  * This function is called when all multicast memberships needs
16624  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16625  * called only once unlike the IPv4 counterpart where it is called after
16626  * every logical interface is moved. The reason is due to multicast
16627  * memberships are joined using an interface address in IPv4 while in
16628  * IPv6, interface index is used.
16629  */
16630 static void
16631 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16632 {
16633 	ilm_t	*ilm;
16634 	ilm_t	*ilm_next;
16635 	ilm_t	*new_ilm;
16636 	ilm_t	**ilmp;
16637 	int	count;
16638 	char buf[INET6_ADDRSTRLEN];
16639 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16640 	ip_stack_t	*ipst = from_ill->ill_ipst;
16641 
16642 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16643 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16644 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16645 
16646 	if (ifindex == 0) {
16647 		/*
16648 		 * Form the solicited node mcast address which is used later.
16649 		 */
16650 		ipif_t *ipif;
16651 
16652 		ipif = from_ill->ill_ipif;
16653 		ASSERT(ipif->ipif_id == 0);
16654 
16655 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16656 	}
16657 
16658 	ilmp = &from_ill->ill_ilm;
16659 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16660 		ilm_next = ilm->ilm_next;
16661 
16662 		if (ilm->ilm_flags & ILM_DELETED) {
16663 			ilmp = &ilm->ilm_next;
16664 			continue;
16665 		}
16666 
16667 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16668 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16669 		ASSERT(ilm->ilm_orig_ifindex != 0);
16670 		if (ilm->ilm_orig_ifindex == ifindex) {
16671 			/*
16672 			 * We are failing back multicast memberships.
16673 			 * If the same ilm exists in to_ill, it means somebody
16674 			 * has joined the same group there e.g. ff02::1
16675 			 * is joined within the kernel when the interfaces
16676 			 * came UP.
16677 			 */
16678 			ASSERT(ilm->ilm_ipif == NULL);
16679 			if (new_ilm != NULL) {
16680 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16681 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16682 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16683 					new_ilm->ilm_is_new = B_TRUE;
16684 				}
16685 			} else {
16686 				/*
16687 				 * check if we can just move the ilm
16688 				 */
16689 				if (from_ill->ill_ilm_walker_cnt != 0) {
16690 					/*
16691 					 * We have walkers we cannot move
16692 					 * the ilm, so allocate a new ilm,
16693 					 * this (old) ilm will be marked
16694 					 * ILM_DELETED at the end of the loop
16695 					 * and will be freed when the
16696 					 * last walker exits.
16697 					 */
16698 					new_ilm = (ilm_t *)mi_zalloc
16699 					    (sizeof (ilm_t));
16700 					if (new_ilm == NULL) {
16701 						ip0dbg(("ilm_move_v6: "
16702 						    "FAILBACK of IPv6"
16703 						    " multicast address %s : "
16704 						    "from %s to"
16705 						    " %s failed : ENOMEM \n",
16706 						    inet_ntop(AF_INET6,
16707 						    &ilm->ilm_v6addr, buf,
16708 						    sizeof (buf)),
16709 						    from_ill->ill_name,
16710 						    to_ill->ill_name));
16711 
16712 							ilmp = &ilm->ilm_next;
16713 							continue;
16714 					}
16715 					*new_ilm = *ilm;
16716 					/*
16717 					 * we don't want new_ilm linked to
16718 					 * ilm's filter list.
16719 					 */
16720 					new_ilm->ilm_filter = NULL;
16721 				} else {
16722 					/*
16723 					 * No walkers we can move the ilm.
16724 					 * lets take it out of the list.
16725 					 */
16726 					*ilmp = ilm->ilm_next;
16727 					ilm->ilm_next = NULL;
16728 					DTRACE_PROBE3(ill__decr__cnt,
16729 					    (ill_t *), from_ill,
16730 					    (char *), "ilm", (void *), ilm);
16731 					ASSERT(from_ill->ill_ilm_cnt > 0);
16732 					from_ill->ill_ilm_cnt--;
16733 
16734 					new_ilm = ilm;
16735 				}
16736 
16737 				/*
16738 				 * if this is the first ilm for the group
16739 				 * set ilm_notify_driver so that we notify the
16740 				 * driver in ilm_send_multicast_reqs.
16741 				 */
16742 				if (ilm_lookup_ill_v6(to_ill,
16743 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16744 					new_ilm->ilm_notify_driver = B_TRUE;
16745 
16746 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16747 				    (char *), "ilm", (void *), new_ilm);
16748 				new_ilm->ilm_ill = to_ill;
16749 				to_ill->ill_ilm_cnt++;
16750 
16751 				/* Add to the to_ill's list */
16752 				new_ilm->ilm_next = to_ill->ill_ilm;
16753 				to_ill->ill_ilm = new_ilm;
16754 				/*
16755 				 * set the flag so that mld_joingroup is
16756 				 * called in ilm_send_multicast_reqs().
16757 				 */
16758 				new_ilm->ilm_is_new = B_TRUE;
16759 			}
16760 			goto bottom;
16761 		} else if (ifindex != 0) {
16762 			/*
16763 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16764 			 * has not matched above, look at the next ilm.
16765 			 */
16766 			ilmp = &ilm->ilm_next;
16767 			continue;
16768 		}
16769 		/*
16770 		 * If we are here, it means ifindex is 0. Failover
16771 		 * everything.
16772 		 *
16773 		 * We need to handle solicited node mcast address
16774 		 * and all_nodes mcast address differently as they
16775 		 * are joined witin the kenrel (ipif_multicast_up)
16776 		 * and potentially from the userland. We are called
16777 		 * after the ipifs of from_ill has been moved.
16778 		 * If we still find ilms on ill with solicited node
16779 		 * mcast address or all_nodes mcast address, it must
16780 		 * belong to the UP interface that has not moved e.g.
16781 		 * ipif_id 0 with the link local prefix does not move.
16782 		 * We join this on the new ill accounting for all the
16783 		 * userland memberships so that applications don't
16784 		 * see any failure.
16785 		 *
16786 		 * We need to make sure that we account only for the
16787 		 * solicited node and all node multicast addresses
16788 		 * that was brought UP on these. In the case of
16789 		 * a failover from A to B, we might have ilms belonging
16790 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16791 		 * for the membership from the userland. If we are failing
16792 		 * over from B to C now, we will find the ones belonging
16793 		 * to A on B. These don't account for the ill_ipif_up_count.
16794 		 * They just move from B to C. The check below on
16795 		 * ilm_orig_ifindex ensures that.
16796 		 */
16797 		if ((ilm->ilm_orig_ifindex ==
16798 		    from_ill->ill_phyint->phyint_ifindex) &&
16799 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16800 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16801 		    &ilm->ilm_v6addr))) {
16802 			ASSERT(ilm->ilm_refcnt > 0);
16803 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16804 			/*
16805 			 * For indentation reasons, we are not using a
16806 			 * "else" here.
16807 			 */
16808 			if (count == 0) {
16809 				ilmp = &ilm->ilm_next;
16810 				continue;
16811 			}
16812 			ilm->ilm_refcnt -= count;
16813 			if (new_ilm != NULL) {
16814 				/*
16815 				 * Can find one with the same
16816 				 * ilm_orig_ifindex, if we are failing
16817 				 * over to a STANDBY. This happens
16818 				 * when somebody wants to join a group
16819 				 * on a STANDBY interface and we
16820 				 * internally join on a different one.
16821 				 * If we had joined on from_ill then, a
16822 				 * failover now will find a new ilm
16823 				 * with this index.
16824 				 */
16825 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16826 				    " new ilm on %s, group address %s\n",
16827 				    to_ill->ill_name,
16828 				    inet_ntop(AF_INET6,
16829 				    &ilm->ilm_v6addr, buf,
16830 				    sizeof (buf))));
16831 				new_ilm->ilm_refcnt += count;
16832 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16833 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16834 					new_ilm->ilm_is_new = B_TRUE;
16835 				}
16836 			} else {
16837 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16838 				if (new_ilm == NULL) {
16839 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16840 					    " multicast address %s : from %s to"
16841 					    " %s failed : ENOMEM \n",
16842 					    inet_ntop(AF_INET6,
16843 					    &ilm->ilm_v6addr, buf,
16844 					    sizeof (buf)), from_ill->ill_name,
16845 					    to_ill->ill_name));
16846 					ilmp = &ilm->ilm_next;
16847 					continue;
16848 				}
16849 				*new_ilm = *ilm;
16850 				new_ilm->ilm_filter = NULL;
16851 				new_ilm->ilm_refcnt = count;
16852 				new_ilm->ilm_timer = INFINITY;
16853 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16854 				new_ilm->ilm_is_new = B_TRUE;
16855 				/*
16856 				 * If the to_ill has not joined this
16857 				 * group we need to tell the driver in
16858 				 * ill_send_multicast_reqs.
16859 				 */
16860 				if (ilm_lookup_ill_v6(to_ill,
16861 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16862 					new_ilm->ilm_notify_driver = B_TRUE;
16863 
16864 				new_ilm->ilm_ill = to_ill;
16865 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16866 				    (char *), "ilm", (void *), new_ilm);
16867 				to_ill->ill_ilm_cnt++;
16868 
16869 				/* Add to the to_ill's list */
16870 				new_ilm->ilm_next = to_ill->ill_ilm;
16871 				to_ill->ill_ilm = new_ilm;
16872 				ASSERT(new_ilm->ilm_ipif == NULL);
16873 			}
16874 			if (ilm->ilm_refcnt == 0) {
16875 				goto bottom;
16876 			} else {
16877 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16878 				CLEAR_SLIST(new_ilm->ilm_filter);
16879 				ilmp = &ilm->ilm_next;
16880 			}
16881 			continue;
16882 		} else {
16883 			/*
16884 			 * ifindex = 0 means, move everything pointing at
16885 			 * from_ill. We are doing this becuase ill has
16886 			 * either FAILED or became INACTIVE.
16887 			 *
16888 			 * As we would like to move things later back to
16889 			 * from_ill, we want to retain the identity of this
16890 			 * ilm. Thus, we don't blindly increment the reference
16891 			 * count on the ilms matching the address alone. We
16892 			 * need to match on the ilm_orig_index also. new_ilm
16893 			 * was obtained by matching ilm_orig_index also.
16894 			 */
16895 			if (new_ilm != NULL) {
16896 				/*
16897 				 * This is possible only if a previous restore
16898 				 * was incomplete i.e restore to
16899 				 * ilm_orig_ifindex left some ilms because
16900 				 * of some failures. Thus when we are failing
16901 				 * again, we might find our old friends there.
16902 				 */
16903 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16904 				    " on %s, group address %s\n",
16905 				    to_ill->ill_name,
16906 				    inet_ntop(AF_INET6,
16907 				    &ilm->ilm_v6addr, buf,
16908 				    sizeof (buf))));
16909 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16910 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16911 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16912 					new_ilm->ilm_is_new = B_TRUE;
16913 				}
16914 			} else {
16915 				if (from_ill->ill_ilm_walker_cnt != 0) {
16916 					new_ilm = (ilm_t *)
16917 					    mi_zalloc(sizeof (ilm_t));
16918 					if (new_ilm == NULL) {
16919 						ip0dbg(("ilm_move_v6: "
16920 						    "FAILOVER of IPv6"
16921 						    " multicast address %s : "
16922 						    "from %s to"
16923 						    " %s failed : ENOMEM \n",
16924 						    inet_ntop(AF_INET6,
16925 						    &ilm->ilm_v6addr, buf,
16926 						    sizeof (buf)),
16927 						    from_ill->ill_name,
16928 						    to_ill->ill_name));
16929 
16930 							ilmp = &ilm->ilm_next;
16931 							continue;
16932 					}
16933 					*new_ilm = *ilm;
16934 					new_ilm->ilm_filter = NULL;
16935 				} else {
16936 					*ilmp = ilm->ilm_next;
16937 					DTRACE_PROBE3(ill__decr__cnt,
16938 					    (ill_t *), from_ill,
16939 					    (char *), "ilm", (void *), ilm);
16940 					ASSERT(from_ill->ill_ilm_cnt > 0);
16941 					from_ill->ill_ilm_cnt--;
16942 
16943 					new_ilm = ilm;
16944 				}
16945 				/*
16946 				 * If the to_ill has not joined this
16947 				 * group we need to tell the driver in
16948 				 * ill_send_multicast_reqs.
16949 				 */
16950 				if (ilm_lookup_ill_v6(to_ill,
16951 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16952 					new_ilm->ilm_notify_driver = B_TRUE;
16953 
16954 				/* Add to the to_ill's list */
16955 				new_ilm->ilm_next = to_ill->ill_ilm;
16956 				to_ill->ill_ilm = new_ilm;
16957 				ASSERT(ilm->ilm_ipif == NULL);
16958 				new_ilm->ilm_ill = to_ill;
16959 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16960 				    (char *), "ilm", (void *), new_ilm);
16961 				to_ill->ill_ilm_cnt++;
16962 				new_ilm->ilm_is_new = B_TRUE;
16963 			}
16964 
16965 		}
16966 
16967 bottom:
16968 		/*
16969 		 * Revert multicast filter state to (EXCLUDE, NULL).
16970 		 * new_ilm->ilm_is_new should already be set if needed.
16971 		 */
16972 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16973 		CLEAR_SLIST(new_ilm->ilm_filter);
16974 		/*
16975 		 * We allocated/got a new ilm, free the old one.
16976 		 */
16977 		if (new_ilm != ilm) {
16978 			if (from_ill->ill_ilm_walker_cnt == 0) {
16979 				*ilmp = ilm->ilm_next;
16980 
16981 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
16982 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
16983 				    from_ill, (char *), "ilm", (void *), ilm);
16984 				ASSERT(from_ill->ill_ilm_cnt > 0);
16985 				from_ill->ill_ilm_cnt--;
16986 
16987 				ilm_inactive(ilm); /* frees this ilm */
16988 
16989 			} else {
16990 				ilm->ilm_flags |= ILM_DELETED;
16991 				from_ill->ill_ilm_cleanup_reqd = 1;
16992 				ilmp = &ilm->ilm_next;
16993 			}
16994 		}
16995 	}
16996 }
16997 
16998 /*
16999  * Move all the multicast memberships to to_ill. Called when
17000  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17001  * different from IPv6 counterpart as multicast memberships are associated
17002  * with ills in IPv6. This function is called after every ipif is moved
17003  * unlike IPv6, where it is moved only once.
17004  */
17005 static void
17006 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17007 {
17008 	ilm_t	*ilm;
17009 	ilm_t	*ilm_next;
17010 	ilm_t	*new_ilm;
17011 	ilm_t	**ilmp;
17012 	ip_stack_t	*ipst = from_ill->ill_ipst;
17013 
17014 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17015 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17016 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17017 
17018 	ilmp = &from_ill->ill_ilm;
17019 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17020 		ilm_next = ilm->ilm_next;
17021 
17022 		if (ilm->ilm_flags & ILM_DELETED) {
17023 			ilmp = &ilm->ilm_next;
17024 			continue;
17025 		}
17026 
17027 		ASSERT(ilm->ilm_ipif != NULL);
17028 
17029 		if (ilm->ilm_ipif != ipif) {
17030 			ilmp = &ilm->ilm_next;
17031 			continue;
17032 		}
17033 
17034 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17035 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17036 			new_ilm = ilm_lookup_ipif(ipif,
17037 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17038 			if (new_ilm != NULL) {
17039 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17040 				/*
17041 				 * We still need to deal with the from_ill.
17042 				 */
17043 				new_ilm->ilm_is_new = B_TRUE;
17044 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17045 				CLEAR_SLIST(new_ilm->ilm_filter);
17046 				ASSERT(ilm->ilm_ipif == ipif);
17047 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17048 				if (from_ill->ill_ilm_walker_cnt == 0) {
17049 					DTRACE_PROBE3(ill__decr__cnt,
17050 					    (ill_t *), from_ill,
17051 					    (char *), "ilm", (void *), ilm);
17052 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17053 				}
17054 				goto delete_ilm;
17055 			}
17056 			/*
17057 			 * If we could not find one e.g. ipif is
17058 			 * still down on to_ill, we add this ilm
17059 			 * on ill_new to preserve the reference
17060 			 * count.
17061 			 */
17062 		}
17063 		/*
17064 		 * When ipifs move, ilms always move with it
17065 		 * to the NEW ill. Thus we should never be
17066 		 * able to find ilm till we really move it here.
17067 		 */
17068 		ASSERT(ilm_lookup_ipif(ipif,
17069 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17070 
17071 		if (from_ill->ill_ilm_walker_cnt != 0) {
17072 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17073 			if (new_ilm == NULL) {
17074 				char buf[INET6_ADDRSTRLEN];
17075 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17076 				    " multicast address %s : "
17077 				    "from %s to"
17078 				    " %s failed : ENOMEM \n",
17079 				    inet_ntop(AF_INET,
17080 				    &ilm->ilm_v6addr, buf,
17081 				    sizeof (buf)),
17082 				    from_ill->ill_name,
17083 				    to_ill->ill_name));
17084 
17085 				ilmp = &ilm->ilm_next;
17086 				continue;
17087 			}
17088 			*new_ilm = *ilm;
17089 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17090 			    (char *), "ilm", (void *), ilm);
17091 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17092 			/* We don't want new_ilm linked to ilm's filter list */
17093 			new_ilm->ilm_filter = NULL;
17094 		} else {
17095 			/* Remove from the list */
17096 			*ilmp = ilm->ilm_next;
17097 			new_ilm = ilm;
17098 		}
17099 
17100 		/*
17101 		 * If we have never joined this group on the to_ill
17102 		 * make sure we tell the driver.
17103 		 */
17104 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17105 		    ALL_ZONES) == NULL)
17106 			new_ilm->ilm_notify_driver = B_TRUE;
17107 
17108 		/* Add to the to_ill's list */
17109 		new_ilm->ilm_next = to_ill->ill_ilm;
17110 		to_ill->ill_ilm = new_ilm;
17111 		new_ilm->ilm_is_new = B_TRUE;
17112 
17113 		/*
17114 		 * Revert multicast filter state to (EXCLUDE, NULL)
17115 		 */
17116 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17117 		CLEAR_SLIST(new_ilm->ilm_filter);
17118 
17119 		/*
17120 		 * Delete only if we have allocated a new ilm.
17121 		 */
17122 		if (new_ilm != ilm) {
17123 delete_ilm:
17124 			if (from_ill->ill_ilm_walker_cnt == 0) {
17125 				/* Remove from the list */
17126 				*ilmp = ilm->ilm_next;
17127 				ilm->ilm_next = NULL;
17128 				DTRACE_PROBE3(ipif__decr__cnt,
17129 				    (ipif_t *), ilm->ilm_ipif,
17130 				    (char *), "ilm", (void *), ilm);
17131 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17132 				ilm->ilm_ipif->ipif_ilm_cnt--;
17133 				ilm_inactive(ilm);
17134 			} else {
17135 				ilm->ilm_flags |= ILM_DELETED;
17136 				from_ill->ill_ilm_cleanup_reqd = 1;
17137 				ilmp = &ilm->ilm_next;
17138 			}
17139 		}
17140 	}
17141 }
17142 
17143 static uint_t
17144 ipif_get_id(ill_t *ill, uint_t id)
17145 {
17146 	uint_t	unit;
17147 	ipif_t	*tipif;
17148 	boolean_t found = B_FALSE;
17149 	ip_stack_t	*ipst = ill->ill_ipst;
17150 
17151 	/*
17152 	 * During failback, we want to go back to the same id
17153 	 * instead of the smallest id so that the original
17154 	 * configuration is maintained. id is non-zero in that
17155 	 * case.
17156 	 */
17157 	if (id != 0) {
17158 		/*
17159 		 * While failing back, if we still have an ipif with
17160 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17161 		 * as soon as we return from this function. It was
17162 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17163 		 * we can choose the smallest id. Thus we return zero
17164 		 * in that case ignoring the hint.
17165 		 */
17166 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17167 			return (0);
17168 		for (tipif = ill->ill_ipif; tipif != NULL;
17169 		    tipif = tipif->ipif_next) {
17170 			if (tipif->ipif_id == id) {
17171 				found = B_TRUE;
17172 				break;
17173 			}
17174 		}
17175 		/*
17176 		 * If somebody already plumbed another logical
17177 		 * with the same id, we won't be able to find it.
17178 		 */
17179 		if (!found)
17180 			return (id);
17181 	}
17182 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17183 		found = B_FALSE;
17184 		for (tipif = ill->ill_ipif; tipif != NULL;
17185 		    tipif = tipif->ipif_next) {
17186 			if (tipif->ipif_id == unit) {
17187 				found = B_TRUE;
17188 				break;
17189 			}
17190 		}
17191 		if (!found)
17192 			break;
17193 	}
17194 	return (unit);
17195 }
17196 
17197 /* ARGSUSED */
17198 static int
17199 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17200     ipif_t **rep_ipif_ptr)
17201 {
17202 	ill_t	*from_ill;
17203 	ipif_t	*rep_ipif;
17204 	uint_t	unit;
17205 	int err = 0;
17206 	ipif_t	*to_ipif;
17207 	struct iocblk	*iocp;
17208 	boolean_t failback_cmd;
17209 	boolean_t remove_ipif;
17210 	int	rc;
17211 	ip_stack_t	*ipst;
17212 
17213 	ASSERT(IAM_WRITER_ILL(to_ill));
17214 	ASSERT(IAM_WRITER_IPIF(ipif));
17215 
17216 	iocp = (struct iocblk *)mp->b_rptr;
17217 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17218 	remove_ipif = B_FALSE;
17219 
17220 	from_ill = ipif->ipif_ill;
17221 	ipst = from_ill->ill_ipst;
17222 
17223 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17224 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17225 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17226 
17227 	/*
17228 	 * Don't move LINK LOCAL addresses as they are tied to
17229 	 * physical interface.
17230 	 */
17231 	if (from_ill->ill_isv6 &&
17232 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17233 		ipif->ipif_was_up = B_FALSE;
17234 		IPIF_UNMARK_MOVING(ipif);
17235 		return (0);
17236 	}
17237 
17238 	/*
17239 	 * We set the ipif_id to maximum so that the search for
17240 	 * ipif_id will pick the lowest number i.e 0 in the
17241 	 * following 2 cases :
17242 	 *
17243 	 * 1) We have a replacement ipif at the head of to_ill.
17244 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17245 	 *    on to_ill and hence the MOVE might fail. We want to
17246 	 *    remove it only if we could move the ipif. Thus, by
17247 	 *    setting it to the MAX value, we make the search in
17248 	 *    ipif_get_id return the zeroth id.
17249 	 *
17250 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17251 	 *    we might just have a zero address plumbed on the ipif
17252 	 *    with zero id in the case of IPv4. We remove that while
17253 	 *    doing the failback. We want to remove it only if we
17254 	 *    could move the ipif. Thus, by setting it to the MAX
17255 	 *    value, we make the search in ipif_get_id return the
17256 	 *    zeroth id.
17257 	 *
17258 	 * Both (1) and (2) are done only when when we are moving
17259 	 * an ipif (either due to failover/failback) which originally
17260 	 * belonged to this interface i.e the ipif_orig_ifindex is
17261 	 * the same as to_ill's ifindex. This is needed so that
17262 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17263 	 * from B -> A (B is being removed from the group) and
17264 	 * FAILBACK from A -> B restores the original configuration.
17265 	 * Without the check for orig_ifindex, the second FAILOVER
17266 	 * could make the ipif belonging to B replace the A's zeroth
17267 	 * ipif and the subsequent failback re-creating the replacement
17268 	 * ipif again.
17269 	 *
17270 	 * NOTE : We created the replacement ipif when we did a
17271 	 * FAILOVER (See below). We could check for FAILBACK and
17272 	 * then look for replacement ipif to be removed. But we don't
17273 	 * want to do that because we wan't to allow the possibility
17274 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17275 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17276 	 * from B -> A.
17277 	 */
17278 	to_ipif = to_ill->ill_ipif;
17279 	if ((to_ill->ill_phyint->phyint_ifindex ==
17280 	    ipif->ipif_orig_ifindex) &&
17281 	    to_ipif->ipif_replace_zero) {
17282 		ASSERT(to_ipif->ipif_id == 0);
17283 		remove_ipif = B_TRUE;
17284 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17285 	}
17286 	/*
17287 	 * Find the lowest logical unit number on the to_ill.
17288 	 * If we are failing back, try to get the original id
17289 	 * rather than the lowest one so that the original
17290 	 * configuration is maintained.
17291 	 *
17292 	 * XXX need a better scheme for this.
17293 	 */
17294 	if (failback_cmd) {
17295 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17296 	} else {
17297 		unit = ipif_get_id(to_ill, 0);
17298 	}
17299 
17300 	/* Reset back to zero in case we fail below */
17301 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17302 		to_ipif->ipif_id = 0;
17303 
17304 	if (unit == ipst->ips_ip_addrs_per_if) {
17305 		ipif->ipif_was_up = B_FALSE;
17306 		IPIF_UNMARK_MOVING(ipif);
17307 		return (EINVAL);
17308 	}
17309 
17310 	/*
17311 	 * ipif is ready to move from "from_ill" to "to_ill".
17312 	 *
17313 	 * 1) If we are moving ipif with id zero, create a
17314 	 *    replacement ipif for this ipif on from_ill. If this fails
17315 	 *    fail the MOVE operation.
17316 	 *
17317 	 * 2) Remove the replacement ipif on to_ill if any.
17318 	 *    We could remove the replacement ipif when we are moving
17319 	 *    the ipif with id zero. But what if somebody already
17320 	 *    unplumbed it ? Thus we always remove it if it is present.
17321 	 *    We want to do it only if we are sure we are going to
17322 	 *    move the ipif to to_ill which is why there are no
17323 	 *    returns due to error till ipif is linked to to_ill.
17324 	 *    Note that the first ipif that we failback will always
17325 	 *    be zero if it is present.
17326 	 */
17327 	if (ipif->ipif_id == 0) {
17328 		ipaddr_t inaddr_any = INADDR_ANY;
17329 
17330 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17331 		if (rep_ipif == NULL) {
17332 			ipif->ipif_was_up = B_FALSE;
17333 			IPIF_UNMARK_MOVING(ipif);
17334 			return (ENOMEM);
17335 		}
17336 		*rep_ipif = ipif_zero;
17337 		/*
17338 		 * Before we put the ipif on the list, store the addresses
17339 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17340 		 * assumes so. This logic is not any different from what
17341 		 * ipif_allocate does.
17342 		 */
17343 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17344 		    &rep_ipif->ipif_v6lcl_addr);
17345 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17346 		    &rep_ipif->ipif_v6src_addr);
17347 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17348 		    &rep_ipif->ipif_v6subnet);
17349 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17350 		    &rep_ipif->ipif_v6net_mask);
17351 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17352 		    &rep_ipif->ipif_v6brd_addr);
17353 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17354 		    &rep_ipif->ipif_v6pp_dst_addr);
17355 		/*
17356 		 * We mark IPIF_NOFAILOVER so that this can never
17357 		 * move.
17358 		 */
17359 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17360 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17361 		rep_ipif->ipif_replace_zero = B_TRUE;
17362 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17363 		    MUTEX_DEFAULT, NULL);
17364 		rep_ipif->ipif_id = 0;
17365 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17366 		rep_ipif->ipif_ill = from_ill;
17367 		rep_ipif->ipif_orig_ifindex =
17368 		    from_ill->ill_phyint->phyint_ifindex;
17369 		/* Insert at head */
17370 		rep_ipif->ipif_next = from_ill->ill_ipif;
17371 		from_ill->ill_ipif = rep_ipif;
17372 		/*
17373 		 * We don't really care to let apps know about
17374 		 * this interface.
17375 		 */
17376 	}
17377 
17378 	if (remove_ipif) {
17379 		/*
17380 		 * We set to a max value above for this case to get
17381 		 * id zero. ASSERT that we did get one.
17382 		 */
17383 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17384 		rep_ipif = to_ipif;
17385 		to_ill->ill_ipif = rep_ipif->ipif_next;
17386 		rep_ipif->ipif_next = NULL;
17387 		/*
17388 		 * If some apps scanned and find this interface,
17389 		 * it is time to let them know, so that they can
17390 		 * delete it.
17391 		 */
17392 
17393 		*rep_ipif_ptr = rep_ipif;
17394 	}
17395 
17396 	/* Get it out of the ILL interface list. */
17397 	ipif_remove(ipif, B_FALSE);
17398 
17399 	/* Assign the new ill */
17400 	ipif->ipif_ill = to_ill;
17401 	ipif->ipif_id = unit;
17402 	/* id has already been checked */
17403 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17404 	ASSERT(rc == 0);
17405 	/* Let SCTP update its list */
17406 	sctp_move_ipif(ipif, from_ill, to_ill);
17407 	/*
17408 	 * Handle the failover and failback of ipif_t between
17409 	 * ill_t that have differing maximum mtu values.
17410 	 */
17411 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17412 		if (ipif->ipif_saved_mtu == 0) {
17413 			/*
17414 			 * As this ipif_t is moving to an ill_t
17415 			 * that has a lower ill_max_mtu, its
17416 			 * ipif_mtu needs to be saved so it can
17417 			 * be restored during failback or during
17418 			 * failover to an ill_t which has a
17419 			 * higher ill_max_mtu.
17420 			 */
17421 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17422 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17423 		} else {
17424 			/*
17425 			 * The ipif_t is, once again, moving to
17426 			 * an ill_t that has a lower maximum mtu
17427 			 * value.
17428 			 */
17429 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17430 		}
17431 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17432 	    ipif->ipif_saved_mtu != 0) {
17433 		/*
17434 		 * The mtu of this ipif_t had to be reduced
17435 		 * during an earlier failover; this is an
17436 		 * opportunity for it to be increased (either as
17437 		 * part of another failover or a failback).
17438 		 */
17439 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17440 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17441 			ipif->ipif_saved_mtu = 0;
17442 		} else {
17443 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17444 		}
17445 	}
17446 
17447 	/*
17448 	 * We preserve all the other fields of the ipif including
17449 	 * ipif_saved_ire_mp. The routes that are saved here will
17450 	 * be recreated on the new interface and back on the old
17451 	 * interface when we move back.
17452 	 */
17453 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17454 
17455 	return (err);
17456 }
17457 
17458 static int
17459 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17460     int ifindex, ipif_t **rep_ipif_ptr)
17461 {
17462 	ipif_t *mipif;
17463 	ipif_t *ipif_next;
17464 	int err;
17465 
17466 	/*
17467 	 * We don't really try to MOVE back things if some of the
17468 	 * operations fail. The daemon will take care of moving again
17469 	 * later on.
17470 	 */
17471 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17472 		ipif_next = mipif->ipif_next;
17473 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17474 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17475 
17476 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17477 
17478 			/*
17479 			 * When the MOVE fails, it is the job of the
17480 			 * application to take care of this properly
17481 			 * i.e try again if it is ENOMEM.
17482 			 */
17483 			if (mipif->ipif_ill != from_ill) {
17484 				/*
17485 				 * ipif has moved.
17486 				 *
17487 				 * Move the multicast memberships associated
17488 				 * with this ipif to the new ill. For IPv6, we
17489 				 * do it once after all the ipifs are moved
17490 				 * (in ill_move) as they are not associated
17491 				 * with ipifs.
17492 				 *
17493 				 * We need to move the ilms as the ipif has
17494 				 * already been moved to a new ill even
17495 				 * in the case of errors. Neither
17496 				 * ilm_free(ipif) will find the ilm
17497 				 * when somebody unplumbs this ipif nor
17498 				 * ilm_delete(ilm) will be able to find the
17499 				 * ilm, if we don't move now.
17500 				 */
17501 				if (!from_ill->ill_isv6)
17502 					ilm_move_v4(from_ill, to_ill, mipif);
17503 			}
17504 
17505 			if (err != 0)
17506 				return (err);
17507 		}
17508 	}
17509 	return (0);
17510 }
17511 
17512 static int
17513 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17514 {
17515 	int ifindex;
17516 	int err;
17517 	struct iocblk	*iocp;
17518 	ipif_t	*ipif;
17519 	ipif_t *rep_ipif_ptr = NULL;
17520 	ipif_t	*from_ipif = NULL;
17521 	boolean_t check_rep_if = B_FALSE;
17522 	ip_stack_t	*ipst = from_ill->ill_ipst;
17523 
17524 	iocp = (struct iocblk *)mp->b_rptr;
17525 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17526 		/*
17527 		 * Move everything pointing at from_ill to to_ill.
17528 		 * We acheive this by passing in 0 as ifindex.
17529 		 */
17530 		ifindex = 0;
17531 	} else {
17532 		/*
17533 		 * Move everything pointing at from_ill whose original
17534 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17535 		 * We acheive this by passing in ifindex rather than 0.
17536 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17537 		 */
17538 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17539 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17540 	}
17541 
17542 	/*
17543 	 * Determine if there is at least one ipif that would move from
17544 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17545 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17546 	 * the move, in which case we need to quiesce the replacement ipif also.
17547 	 */
17548 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17549 	    from_ipif = from_ipif->ipif_next) {
17550 		if (((ifindex == 0) ||
17551 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17552 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17553 			check_rep_if = B_TRUE;
17554 			break;
17555 		}
17556 	}
17557 
17558 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17559 
17560 	GRAB_ILL_LOCKS(from_ill, to_ill);
17561 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17562 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17563 		    mp, ILL_MOVE_OK);
17564 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17565 		return (EINPROGRESS);
17566 	}
17567 
17568 	/* Check if the replacement ipif is quiescent to delete */
17569 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17570 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17571 		to_ill->ill_ipif->ipif_state_flags |=
17572 		    IPIF_MOVING | IPIF_CHANGING;
17573 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17574 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17575 			    mp, ILL_MOVE_OK);
17576 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17577 			return (EINPROGRESS);
17578 		}
17579 	}
17580 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17581 
17582 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17583 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17584 	GRAB_ILL_LOCKS(from_ill, to_ill);
17585 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17586 
17587 	/* ilm_move is done inside ipif_move for IPv4 */
17588 	if (err == 0 && from_ill->ill_isv6)
17589 		ilm_move_v6(from_ill, to_ill, ifindex);
17590 
17591 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17592 	rw_exit(&ipst->ips_ill_g_lock);
17593 
17594 	/*
17595 	 * send rts messages and multicast messages.
17596 	 */
17597 	if (rep_ipif_ptr != NULL) {
17598 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17599 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17600 			rep_ipif_ptr->ipif_recovery_id = 0;
17601 		}
17602 		ip_rts_ifmsg(rep_ipif_ptr);
17603 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17604 #ifdef DEBUG
17605 		ipif_trace_cleanup(rep_ipif_ptr);
17606 #endif
17607 		mi_free(rep_ipif_ptr);
17608 	}
17609 
17610 	conn_move_ill(from_ill, to_ill, ifindex);
17611 
17612 	return (err);
17613 }
17614 
17615 /*
17616  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17617  * Also checks for the validity of the arguments.
17618  * Note: We are already exclusive inside the from group.
17619  * It is upto the caller to release refcnt on the to_ill's.
17620  */
17621 static int
17622 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17623     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17624 {
17625 	int dst_index;
17626 	ipif_t *ipif_v4, *ipif_v6;
17627 	struct lifreq *lifr;
17628 	mblk_t *mp1;
17629 	boolean_t exists;
17630 	sin_t	*sin;
17631 	int	err = 0;
17632 	ip_stack_t	*ipst;
17633 
17634 	if (CONN_Q(q))
17635 		ipst = CONNQ_TO_IPST(q);
17636 	else
17637 		ipst = ILLQ_TO_IPST(q);
17638 
17639 	if ((mp1 = mp->b_cont) == NULL)
17640 		return (EPROTO);
17641 
17642 	if ((mp1 = mp1->b_cont) == NULL)
17643 		return (EPROTO);
17644 
17645 	lifr = (struct lifreq *)mp1->b_rptr;
17646 	sin = (sin_t *)&lifr->lifr_addr;
17647 
17648 	/*
17649 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17650 	 * specific operations.
17651 	 */
17652 	if (sin->sin_family != AF_UNSPEC)
17653 		return (EINVAL);
17654 
17655 	/*
17656 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17657 	 * NULLs for the last 4 args and we know the lookup won't fail
17658 	 * with EINPROGRESS.
17659 	 */
17660 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17661 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17662 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17663 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17664 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17665 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17666 
17667 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17668 		return (ENXIO);
17669 
17670 	if (ipif_v4 != NULL) {
17671 		ASSERT(ipif_v4->ipif_refcnt != 0);
17672 		if (ipif_v4->ipif_id != 0) {
17673 			err = EINVAL;
17674 			goto done;
17675 		}
17676 
17677 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17678 		*ill_from_v4 = ipif_v4->ipif_ill;
17679 	}
17680 
17681 	if (ipif_v6 != NULL) {
17682 		ASSERT(ipif_v6->ipif_refcnt != 0);
17683 		if (ipif_v6->ipif_id != 0) {
17684 			err = EINVAL;
17685 			goto done;
17686 		}
17687 
17688 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17689 		*ill_from_v6 = ipif_v6->ipif_ill;
17690 	}
17691 
17692 	err = 0;
17693 	dst_index = lifr->lifr_movetoindex;
17694 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17695 	    q, mp, ip_process_ioctl, &err, ipst);
17696 	if (err != 0) {
17697 		/*
17698 		 * A move may be in progress, EINPROGRESS looking up the "to"
17699 		 * ill means changes already done to the "from" ipsq need to
17700 		 * be undone to avoid potential deadlocks.
17701 		 *
17702 		 * ENXIO will usually be because there is only v6 on the ill,
17703 		 * that's not treated as an error unless an ENXIO is also
17704 		 * seen when looking up the v6 "to" ill.
17705 		 *
17706 		 * If EINPROGRESS, the mp has been enqueued and can not be
17707 		 * used to look up the v6 "to" ill, but a preemptive clean
17708 		 * up of changes to the v6 "from" ipsq is done.
17709 		 */
17710 		if (err == EINPROGRESS) {
17711 			if (*ill_from_v4 != NULL) {
17712 				ill_t   *from_ill;
17713 				ipsq_t  *from_ipsq;
17714 
17715 				from_ill = ipif_v4->ipif_ill;
17716 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17717 
17718 				mutex_enter(&from_ipsq->ipsq_lock);
17719 				from_ipsq->ipsq_current_ipif = NULL;
17720 				mutex_exit(&from_ipsq->ipsq_lock);
17721 			}
17722 			if (*ill_from_v6 != NULL) {
17723 				ill_t   *from_ill;
17724 				ipsq_t  *from_ipsq;
17725 
17726 				from_ill = ipif_v6->ipif_ill;
17727 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17728 
17729 				mutex_enter(&from_ipsq->ipsq_lock);
17730 				from_ipsq->ipsq_current_ipif = NULL;
17731 				mutex_exit(&from_ipsq->ipsq_lock);
17732 			}
17733 			goto done;
17734 		}
17735 		ASSERT(err == ENXIO);
17736 		err = 0;
17737 	}
17738 
17739 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17740 	    q, mp, ip_process_ioctl, &err, ipst);
17741 	if (err != 0) {
17742 		/*
17743 		 * A move may be in progress, EINPROGRESS looking up the "to"
17744 		 * ill means changes already done to the "from" ipsq need to
17745 		 * be undone to avoid potential deadlocks.
17746 		 */
17747 		if (err == EINPROGRESS) {
17748 			if (*ill_from_v6 != NULL) {
17749 				ill_t   *from_ill;
17750 				ipsq_t  *from_ipsq;
17751 
17752 				from_ill = ipif_v6->ipif_ill;
17753 				from_ipsq = from_ill->ill_phyint->phyint_ipsq;
17754 
17755 				mutex_enter(&from_ipsq->ipsq_lock);
17756 				from_ipsq->ipsq_current_ipif = NULL;
17757 				mutex_exit(&from_ipsq->ipsq_lock);
17758 			}
17759 			goto done;
17760 		}
17761 		ASSERT(err == ENXIO);
17762 
17763 		/* Both v4 and v6 lookup failed */
17764 		if (*ill_to_v4 == NULL) {
17765 			err = ENXIO;
17766 			goto done;
17767 		}
17768 		err = 0;
17769 	}
17770 
17771 	/*
17772 	 * If we have something to MOVE i.e "from" not NULL,
17773 	 * "to" should be non-NULL.
17774 	 */
17775 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17776 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17777 		err = EINVAL;
17778 	}
17779 
17780 done:
17781 	if (ipif_v4 != NULL)
17782 		ipif_refrele(ipif_v4);
17783 	if (ipif_v6 != NULL)
17784 		ipif_refrele(ipif_v6);
17785 	return (err);
17786 }
17787 
17788 /*
17789  * FAILOVER and FAILBACK are modelled as MOVE operations.
17790  *
17791  * We don't check whether the MOVE is within the same group or
17792  * not, because this ioctl can be used as a generic mechanism
17793  * to failover from interface A to B, though things will function
17794  * only if they are really part of the same group. Moreover,
17795  * all ipifs may be down and hence temporarily out of the group.
17796  *
17797  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17798  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17799  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17800  * have been deleted and there are no active references. Once quiescent the
17801  * ipif's are moved and brought up on the new ill.
17802  *
17803  * Normally the source ill and destination ill belong to the same IPMP group
17804  * and hence the same ipsq_t. In the event they don't belong to the same
17805  * same group the two ipsq's are first merged into one ipsq - that of the
17806  * to_ill. The multicast memberships on the source and destination ill cannot
17807  * change during the move operation since multicast joins/leaves also have to
17808  * execute on the same ipsq and are hence serialized.
17809  */
17810 /* ARGSUSED */
17811 int
17812 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17813     ip_ioctl_cmd_t *ipip, void *ifreq)
17814 {
17815 	ill_t *ill_to_v4 = NULL;
17816 	ill_t *ill_to_v6 = NULL;
17817 	ill_t *ill_from_v4 = NULL;
17818 	ill_t *ill_from_v6 = NULL;
17819 	int err = 0;
17820 
17821 	/*
17822 	 * setup from and to ill's, we can get EINPROGRESS only for
17823 	 * to_ill's.
17824 	 */
17825 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17826 	    &ill_to_v4, &ill_to_v6);
17827 
17828 	if (err != 0) {
17829 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17830 		goto done;
17831 	}
17832 
17833 	/*
17834 	 * nothing to do.
17835 	 */
17836 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17837 		goto done;
17838 	}
17839 
17840 	/*
17841 	 * nothing to do.
17842 	 */
17843 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17844 		goto done;
17845 	}
17846 
17847 	/*
17848 	 * Mark the ill as changing.
17849 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17850 	 * in ill_up_ipifs in case of error they are cleared below.
17851 	 */
17852 
17853 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17854 	if (ill_from_v4 != NULL)
17855 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17856 	if (ill_from_v6 != NULL)
17857 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17858 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17859 
17860 	/*
17861 	 * Make sure that both src and dst are
17862 	 * in the same syncq group. If not make it happen.
17863 	 * We are not holding any locks because we are the writer
17864 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17865 	 * to protect to_ipsq against changing.
17866 	 */
17867 	if (ill_from_v4 != NULL) {
17868 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17869 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17870 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17871 			    NULL, mp, q);
17872 			goto err_ret;
17873 
17874 		}
17875 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17876 	} else {
17877 
17878 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17879 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17880 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17881 			    NULL, mp, q);
17882 			goto err_ret;
17883 
17884 		}
17885 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17886 	}
17887 
17888 	/*
17889 	 * Now that the ipsq's have been merged and we are the writer
17890 	 * lets mark to_ill as changing as well.
17891 	 */
17892 
17893 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17894 	if (ill_to_v4 != NULL)
17895 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17896 	if (ill_to_v6 != NULL)
17897 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17898 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17899 
17900 	/*
17901 	 * Its ok for us to proceed with the move even if
17902 	 * ill_pending_mp is non null on one of the from ill's as the reply
17903 	 * should not be looking at the ipif, it should only care about the
17904 	 * ill itself.
17905 	 */
17906 
17907 	/*
17908 	 * lets move ipv4 first.
17909 	 */
17910 	if (ill_from_v4 != NULL) {
17911 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17912 		ill_from_v4->ill_move_in_progress = B_TRUE;
17913 		ill_to_v4->ill_move_in_progress = B_TRUE;
17914 		ill_to_v4->ill_move_peer = ill_from_v4;
17915 		ill_from_v4->ill_move_peer = ill_to_v4;
17916 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17917 	}
17918 
17919 	/*
17920 	 * Now lets move ipv6.
17921 	 */
17922 	if (err == 0 && ill_from_v6 != NULL) {
17923 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17924 		ill_from_v6->ill_move_in_progress = B_TRUE;
17925 		ill_to_v6->ill_move_in_progress = B_TRUE;
17926 		ill_to_v6->ill_move_peer = ill_from_v6;
17927 		ill_from_v6->ill_move_peer = ill_to_v6;
17928 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17929 	}
17930 
17931 err_ret:
17932 	/*
17933 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17934 	 * moved to become quiescent.
17935 	 */
17936 	if (err == EINPROGRESS) {
17937 		goto done;
17938 	}
17939 
17940 	/*
17941 	 * if err is set ill_up_ipifs will not be called
17942 	 * lets clear the flags.
17943 	 */
17944 
17945 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17946 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17947 	/*
17948 	 * Some of the clearing may be redundant. But it is simple
17949 	 * not making any extra checks.
17950 	 */
17951 	if (ill_from_v6 != NULL) {
17952 		ill_from_v6->ill_move_in_progress = B_FALSE;
17953 		ill_from_v6->ill_move_peer = NULL;
17954 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17955 	}
17956 	if (ill_from_v4 != NULL) {
17957 		ill_from_v4->ill_move_in_progress = B_FALSE;
17958 		ill_from_v4->ill_move_peer = NULL;
17959 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17960 	}
17961 	if (ill_to_v6 != NULL) {
17962 		ill_to_v6->ill_move_in_progress = B_FALSE;
17963 		ill_to_v6->ill_move_peer = NULL;
17964 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17965 	}
17966 	if (ill_to_v4 != NULL) {
17967 		ill_to_v4->ill_move_in_progress = B_FALSE;
17968 		ill_to_v4->ill_move_peer = NULL;
17969 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17970 	}
17971 
17972 	/*
17973 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
17974 	 * Do this always to maintain proper state i.e even in case of errors.
17975 	 * As phyint_inactive looks at both v4 and v6 interfaces,
17976 	 * we need not call on both v4 and v6 interfaces.
17977 	 */
17978 	if (ill_from_v4 != NULL) {
17979 		if ((ill_from_v4->ill_phyint->phyint_flags &
17980 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17981 			phyint_inactive(ill_from_v4->ill_phyint);
17982 		}
17983 	} else if (ill_from_v6 != NULL) {
17984 		if ((ill_from_v6->ill_phyint->phyint_flags &
17985 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17986 			phyint_inactive(ill_from_v6->ill_phyint);
17987 		}
17988 	}
17989 
17990 	if (ill_to_v4 != NULL) {
17991 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17992 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17993 		}
17994 	} else if (ill_to_v6 != NULL) {
17995 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17996 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17997 		}
17998 	}
17999 
18000 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18001 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18002 
18003 no_err:
18004 	/*
18005 	 * lets bring the interfaces up on the to_ill.
18006 	 */
18007 	if (err == 0) {
18008 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18009 		    q, mp);
18010 	}
18011 
18012 	if (err == 0) {
18013 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18014 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18015 
18016 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18017 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18018 	}
18019 done:
18020 
18021 	if (ill_to_v4 != NULL) {
18022 		ill_refrele(ill_to_v4);
18023 	}
18024 	if (ill_to_v6 != NULL) {
18025 		ill_refrele(ill_to_v6);
18026 	}
18027 
18028 	return (err);
18029 }
18030 
18031 static void
18032 ill_dl_down(ill_t *ill)
18033 {
18034 	/*
18035 	 * The ill is down; unbind but stay attached since we're still
18036 	 * associated with a PPA. If we have negotiated DLPI capabilites
18037 	 * with the data link service provider (IDS_OK) then reset them.
18038 	 * The interval between unbinding and rebinding is potentially
18039 	 * unbounded hence we cannot assume things will be the same.
18040 	 * The DLPI capabilities will be probed again when the data link
18041 	 * is brought up.
18042 	 */
18043 	mblk_t	*mp = ill->ill_unbind_mp;
18044 
18045 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18046 
18047 	ill->ill_unbind_mp = NULL;
18048 	if (mp != NULL) {
18049 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18050 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18051 		    ill->ill_name));
18052 		mutex_enter(&ill->ill_lock);
18053 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18054 		mutex_exit(&ill->ill_lock);
18055 		/*
18056 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
18057 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
18058 		 * ill_capability_dld_disable disable rightaway. If this is not
18059 		 * an unplumb operation then the disable happens on receipt of
18060 		 * the capab ack via ip_rput_dlpi_writer ->
18061 		 * ill_capability_ack_thr. In both cases the order of
18062 		 * the operations seen by DLD is capability disable followed
18063 		 * by DL_UNBIND. Also the DLD capability disable needs a
18064 		 * cv_wait'able context.
18065 		 */
18066 		if (ill->ill_state_flags & ILL_CONDEMNED)
18067 			ill_capability_dld_disable(ill);
18068 		ill_capability_reset(ill, B_FALSE);
18069 		ill_dlpi_send(ill, mp);
18070 	}
18071 
18072 	/*
18073 	 * Toss all of our multicast memberships.  We could keep them, but
18074 	 * then we'd have to do bookkeeping of any joins and leaves performed
18075 	 * by the application while the the interface is down (we can't just
18076 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18077 	 * on a downed interface).
18078 	 */
18079 	ill_leave_multicast(ill);
18080 
18081 	mutex_enter(&ill->ill_lock);
18082 	ill->ill_dl_up = 0;
18083 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
18084 	mutex_exit(&ill->ill_lock);
18085 }
18086 
18087 static void
18088 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18089 {
18090 	union DL_primitives *dlp;
18091 	t_uscalar_t prim;
18092 
18093 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18094 
18095 	dlp = (union DL_primitives *)mp->b_rptr;
18096 	prim = dlp->dl_primitive;
18097 
18098 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18099 	    dl_primstr(prim), prim, ill->ill_name));
18100 
18101 	switch (prim) {
18102 	case DL_PHYS_ADDR_REQ:
18103 	{
18104 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18105 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18106 		break;
18107 	}
18108 	case DL_BIND_REQ:
18109 		mutex_enter(&ill->ill_lock);
18110 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18111 		mutex_exit(&ill->ill_lock);
18112 		break;
18113 	}
18114 
18115 	/*
18116 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18117 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18118 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18119 	 */
18120 	mutex_enter(&ill->ill_lock);
18121 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18122 	    (prim == DL_UNBIND_REQ)) {
18123 		ill->ill_dlpi_pending = prim;
18124 	}
18125 	mutex_exit(&ill->ill_lock);
18126 	putnext(ill->ill_wq, mp);
18127 }
18128 
18129 /*
18130  * Helper function for ill_dlpi_send().
18131  */
18132 /* ARGSUSED */
18133 static void
18134 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18135 {
18136 	ill_dlpi_send(q->q_ptr, mp);
18137 }
18138 
18139 /*
18140  * Send a DLPI control message to the driver but make sure there
18141  * is only one outstanding message. Uses ill_dlpi_pending to tell
18142  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18143  * when an ACK or a NAK is received to process the next queued message.
18144  */
18145 void
18146 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18147 {
18148 	mblk_t **mpp;
18149 
18150 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18151 
18152 	/*
18153 	 * To ensure that any DLPI requests for current exclusive operation
18154 	 * are always completely sent before any DLPI messages for other
18155 	 * operations, require writer access before enqueuing.
18156 	 */
18157 	if (!IAM_WRITER_ILL(ill)) {
18158 		ill_refhold(ill);
18159 		/* qwriter_ip() does the ill_refrele() */
18160 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18161 		    NEW_OP, B_TRUE);
18162 		return;
18163 	}
18164 
18165 	mutex_enter(&ill->ill_lock);
18166 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18167 		/* Must queue message. Tail insertion */
18168 		mpp = &ill->ill_dlpi_deferred;
18169 		while (*mpp != NULL)
18170 			mpp = &((*mpp)->b_next);
18171 
18172 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18173 		    ill->ill_name));
18174 
18175 		*mpp = mp;
18176 		mutex_exit(&ill->ill_lock);
18177 		return;
18178 	}
18179 	mutex_exit(&ill->ill_lock);
18180 	ill_dlpi_dispatch(ill, mp);
18181 }
18182 
18183 static void
18184 ill_capability_send(ill_t *ill, mblk_t *mp)
18185 {
18186 	ill->ill_capab_pending_cnt++;
18187 	ill_dlpi_send(ill, mp);
18188 }
18189 
18190 void
18191 ill_capability_done(ill_t *ill)
18192 {
18193 	ASSERT(ill->ill_capab_pending_cnt != 0);
18194 
18195 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
18196 
18197 	ill->ill_capab_pending_cnt--;
18198 	if (ill->ill_capab_pending_cnt == 0 &&
18199 	    ill->ill_dlpi_capab_state == IDCS_OK)
18200 		ill_capability_reset_alloc(ill);
18201 }
18202 
18203 /*
18204  * Send all deferred DLPI messages without waiting for their ACKs.
18205  */
18206 void
18207 ill_dlpi_send_deferred(ill_t *ill)
18208 {
18209 	mblk_t *mp, *nextmp;
18210 
18211 	/*
18212 	 * Clear ill_dlpi_pending so that the message is not queued in
18213 	 * ill_dlpi_send().
18214 	 */
18215 	mutex_enter(&ill->ill_lock);
18216 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18217 	mp = ill->ill_dlpi_deferred;
18218 	ill->ill_dlpi_deferred = NULL;
18219 	mutex_exit(&ill->ill_lock);
18220 
18221 	for (; mp != NULL; mp = nextmp) {
18222 		nextmp = mp->b_next;
18223 		mp->b_next = NULL;
18224 		ill_dlpi_send(ill, mp);
18225 	}
18226 }
18227 
18228 /*
18229  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18230  */
18231 boolean_t
18232 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18233 {
18234 	t_uscalar_t pending;
18235 
18236 	mutex_enter(&ill->ill_lock);
18237 	if (ill->ill_dlpi_pending == prim) {
18238 		mutex_exit(&ill->ill_lock);
18239 		return (B_TRUE);
18240 	}
18241 
18242 	/*
18243 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18244 	 * without waiting, so don't print any warnings in that case.
18245 	 */
18246 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18247 		mutex_exit(&ill->ill_lock);
18248 		return (B_FALSE);
18249 	}
18250 	pending = ill->ill_dlpi_pending;
18251 	mutex_exit(&ill->ill_lock);
18252 
18253 	if (pending == DL_PRIM_INVAL) {
18254 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18255 		    "received unsolicited ack for %s on %s\n",
18256 		    dl_primstr(prim), ill->ill_name);
18257 	} else {
18258 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18259 		    "received unexpected ack for %s on %s (expecting %s)\n",
18260 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18261 	}
18262 	return (B_FALSE);
18263 }
18264 
18265 /*
18266  * Complete the current DLPI operation associated with `prim' on `ill' and
18267  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18268  * operations and the ill's current exclusive IPSQ operation has finished
18269  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18270  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18271  * the comments above ipsq_current_finish() for details.
18272  */
18273 void
18274 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18275 {
18276 	mblk_t *mp;
18277 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18278 
18279 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18280 	mutex_enter(&ill->ill_lock);
18281 
18282 	ASSERT(prim != DL_PRIM_INVAL);
18283 	ASSERT(ill->ill_dlpi_pending == prim);
18284 
18285 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18286 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18287 
18288 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18289 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18290 
18291 		mutex_enter(&ipsq->ipsq_lock);
18292 		if (ipsq->ipsq_current_done)
18293 			ipsq->ipsq_current_ipif = NULL;
18294 		mutex_exit(&ipsq->ipsq_lock);
18295 
18296 		cv_signal(&ill->ill_cv);
18297 		mutex_exit(&ill->ill_lock);
18298 		return;
18299 	}
18300 
18301 	ill->ill_dlpi_deferred = mp->b_next;
18302 	mp->b_next = NULL;
18303 	mutex_exit(&ill->ill_lock);
18304 
18305 	ill_dlpi_dispatch(ill, mp);
18306 }
18307 
18308 void
18309 conn_delete_ire(conn_t *connp, caddr_t arg)
18310 {
18311 	ipif_t	*ipif = (ipif_t *)arg;
18312 	ire_t	*ire;
18313 
18314 	/*
18315 	 * Look at the cached ires on conns which has pointers to ipifs.
18316 	 * We just call ire_refrele which clears up the reference
18317 	 * to ire. Called when a conn closes. Also called from ipif_free
18318 	 * to cleanup indirect references to the stale ipif via the cached ire.
18319 	 */
18320 	mutex_enter(&connp->conn_lock);
18321 	ire = connp->conn_ire_cache;
18322 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18323 		connp->conn_ire_cache = NULL;
18324 		mutex_exit(&connp->conn_lock);
18325 		IRE_REFRELE_NOTR(ire);
18326 		return;
18327 	}
18328 	mutex_exit(&connp->conn_lock);
18329 
18330 }
18331 
18332 /*
18333  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18334  * of IREs. Those IREs may have been previously cached in the conn structure.
18335  * This ipcl_walk() walker function releases all references to such IREs based
18336  * on the condemned flag.
18337  */
18338 /* ARGSUSED */
18339 void
18340 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18341 {
18342 	ire_t	*ire;
18343 
18344 	mutex_enter(&connp->conn_lock);
18345 	ire = connp->conn_ire_cache;
18346 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18347 		connp->conn_ire_cache = NULL;
18348 		mutex_exit(&connp->conn_lock);
18349 		IRE_REFRELE_NOTR(ire);
18350 		return;
18351 	}
18352 	mutex_exit(&connp->conn_lock);
18353 }
18354 
18355 /*
18356  * Take down a specific interface, but don't lose any information about it.
18357  * Also delete interface from its interface group (ifgrp).
18358  * (Always called as writer.)
18359  * This function goes through the down sequence even if the interface is
18360  * already down. There are 2 reasons.
18361  * a. Currently we permit interface routes that depend on down interfaces
18362  *    to be added. This behaviour itself is questionable. However it appears
18363  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18364  *    time. We go thru the cleanup in order to remove these routes.
18365  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18366  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18367  *    down, but we need to cleanup i.e. do ill_dl_down and
18368  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18369  *
18370  * IP-MT notes:
18371  *
18372  * Model of reference to interfaces.
18373  *
18374  * The following members in ipif_t track references to the ipif.
18375  *	int     ipif_refcnt;    Active reference count
18376  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18377  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18378  *
18379  * The following members in ill_t track references to the ill.
18380  *	int             ill_refcnt;     active refcnt
18381  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18382  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18383  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18384  *
18385  * Reference to an ipif or ill can be obtained in any of the following ways.
18386  *
18387  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18388  * Pointers to ipif / ill from other data structures viz ire and conn.
18389  * Implicit reference to the ipif / ill by holding a reference to the ire.
18390  *
18391  * The ipif/ill lookup functions return a reference held ipif / ill.
18392  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18393  * This is a purely dynamic reference count associated with threads holding
18394  * references to the ipif / ill. Pointers from other structures do not
18395  * count towards this reference count.
18396  *
18397  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18398  * associated with the ipif/ill. This is incremented whenever a new
18399  * ire is created referencing the ipif/ill. This is done atomically inside
18400  * ire_add_v[46] where the ire is actually added to the ire hash table.
18401  * The count is decremented in ire_inactive where the ire is destroyed.
18402  *
18403  * nce's reference ill's thru nce_ill and the count of nce's associated with
18404  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18405  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18406  * table. Similarly it is decremented in ndp_inactive() where the nce
18407  * is destroyed.
18408  *
18409  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18410  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18411  * in ilm_walker_cleanup() or ilm_delete().
18412  *
18413  * Flow of ioctls involving interface down/up
18414  *
18415  * The following is the sequence of an attempt to set some critical flags on an
18416  * up interface.
18417  * ip_sioctl_flags
18418  * ipif_down
18419  * wait for ipif to be quiescent
18420  * ipif_down_tail
18421  * ip_sioctl_flags_tail
18422  *
18423  * All set ioctls that involve down/up sequence would have a skeleton similar
18424  * to the above. All the *tail functions are called after the refcounts have
18425  * dropped to the appropriate values.
18426  *
18427  * The mechanism to quiesce an ipif is as follows.
18428  *
18429  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18430  * on the ipif. Callers either pass a flag requesting wait or the lookup
18431  *  functions will return NULL.
18432  *
18433  * Delete all ires referencing this ipif
18434  *
18435  * Any thread attempting to do an ipif_refhold on an ipif that has been
18436  * obtained thru a cached pointer will first make sure that
18437  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18438  * increment the refcount.
18439  *
18440  * The above guarantees that the ipif refcount will eventually come down to
18441  * zero and the ipif will quiesce, once all threads that currently hold a
18442  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18443  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18444  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18445  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18446  * in ip.h
18447  *
18448  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18449  *
18450  * Threads trying to lookup an ipif or ill can pass a flag requesting
18451  * wait and restart if the ipif / ill cannot be looked up currently.
18452  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18453  * failure if the ipif is currently undergoing an exclusive operation, and
18454  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18455  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18456  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18457  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18458  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18459  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18460  * until we release the ipsq_lock, even though the the ill/ipif state flags
18461  * can change after we drop the ill_lock.
18462  *
18463  * An attempt to send out a packet using an ipif that is currently
18464  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18465  * operation and restart it later when the exclusive condition on the ipif ends.
18466  * This is an example of not passing the wait flag to the lookup functions. For
18467  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18468  * out a multicast packet on that ipif will fail while the ipif is
18469  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18470  * currently IPIF_CHANGING will also fail.
18471  */
18472 int
18473 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18474 {
18475 	ill_t		*ill = ipif->ipif_ill;
18476 	phyint_t	*phyi;
18477 	conn_t		*connp;
18478 	boolean_t	success;
18479 	boolean_t	ipif_was_up = B_FALSE;
18480 	ip_stack_t	*ipst = ill->ill_ipst;
18481 
18482 	ASSERT(IAM_WRITER_IPIF(ipif));
18483 
18484 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18485 
18486 	if (ipif->ipif_flags & IPIF_UP) {
18487 		mutex_enter(&ill->ill_lock);
18488 		ipif->ipif_flags &= ~IPIF_UP;
18489 		ASSERT(ill->ill_ipif_up_count > 0);
18490 		--ill->ill_ipif_up_count;
18491 		mutex_exit(&ill->ill_lock);
18492 		ipif_was_up = B_TRUE;
18493 		/* Update status in SCTP's list */
18494 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18495 		ill_nic_event_dispatch(ipif->ipif_ill,
18496 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
18497 	}
18498 
18499 	/*
18500 	 * Blow away memberships we established in ipif_multicast_up().
18501 	 */
18502 	ipif_multicast_down(ipif);
18503 
18504 	/*
18505 	 * Remove from the mapping for __sin6_src_id. We insert only
18506 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18507 	 * stored as mapped addresses, we need to check for mapped
18508 	 * INADDR_ANY also.
18509 	 */
18510 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18511 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18512 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18513 		int err;
18514 
18515 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18516 		    ipif->ipif_zoneid, ipst);
18517 		if (err != 0) {
18518 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18519 		}
18520 	}
18521 
18522 	/*
18523 	 * Before we delete the ill from the group (if any), we need
18524 	 * to make sure that we delete all the routes dependent on
18525 	 * this and also any ipifs dependent on this ipif for
18526 	 * source address. We need to do before we delete from
18527 	 * the group because
18528 	 *
18529 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18530 	 *
18531 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18532 	 *    for re-doing source address selection. Note that
18533 	 *    ipif_select_source[_v6] called from
18534 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18535 	 *    because we have already marked down here i.e cleared
18536 	 *    IPIF_UP.
18537 	 */
18538 	if (ipif->ipif_isv6) {
18539 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18540 		    ipst);
18541 	} else {
18542 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18543 		    ipst);
18544 	}
18545 
18546 	/*
18547 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18548 	 * ires have been deleted above. Otherwise a thread could end up
18549 	 * caching an ire in a conn after we have finished the cleanup of the
18550 	 * conn. The caching is done after making sure that the ire is not yet
18551 	 * condemned. Also documented in the block comment above ip_output
18552 	 */
18553 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18554 	/* Also, delete the ires cached in SCTP */
18555 	sctp_ire_cache_flush(ipif);
18556 
18557 	/*
18558 	 * Update any other ipifs which have used "our" local address as
18559 	 * a source address. This entails removing and recreating IRE_INTERFACE
18560 	 * entries for such ipifs.
18561 	 */
18562 	if (ipif->ipif_isv6)
18563 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18564 	else
18565 		ipif_update_other_ipifs(ipif, ill->ill_group);
18566 
18567 	if (ipif_was_up) {
18568 		/*
18569 		 * Check whether it is last ipif to leave this group.
18570 		 * If this is the last ipif to leave, we should remove
18571 		 * this ill from the group as ipif_select_source will not
18572 		 * be able to find any useful ipifs if this ill is selected
18573 		 * for load balancing.
18574 		 *
18575 		 * For nameless groups, we should call ifgrp_delete if this
18576 		 * belongs to some group. As this ipif is going down, we may
18577 		 * need to reconstruct groups.
18578 		 */
18579 		phyi = ill->ill_phyint;
18580 		/*
18581 		 * If the phyint_groupname_len is 0, it may or may not
18582 		 * be in the nameless group. If the phyint_groupname_len is
18583 		 * not 0, then this ill should be part of some group.
18584 		 * As we always insert this ill in the group if
18585 		 * phyint_groupname_len is not zero when the first ipif
18586 		 * comes up (in ipif_up_done), it should be in a group
18587 		 * when the namelen is not 0.
18588 		 *
18589 		 * NOTE : When we delete the ill from the group,it will
18590 		 * blow away all the IRE_CACHES pointing either at this ipif or
18591 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18592 		 * should be pointing at this ill.
18593 		 */
18594 		ASSERT(phyi->phyint_groupname_len == 0 ||
18595 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18596 
18597 		if (phyi->phyint_groupname_len != 0) {
18598 			if (ill->ill_ipif_up_count == 0)
18599 				illgrp_delete(ill);
18600 		}
18601 
18602 		/*
18603 		 * If we have deleted some of the broadcast ires associated
18604 		 * with this ipif, we need to re-nominate somebody else if
18605 		 * the ires that we deleted were the nominated ones.
18606 		 */
18607 		if (ill->ill_group != NULL && !ill->ill_isv6)
18608 			ipif_renominate_bcast(ipif);
18609 	}
18610 
18611 	/*
18612 	 * neighbor-discovery or arp entries for this interface.
18613 	 */
18614 	ipif_ndp_down(ipif);
18615 
18616 	/*
18617 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18618 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18619 	 * and ill_delete -> ipif_free -> ipif_down
18620 	 */
18621 	if (mp == NULL) {
18622 		ASSERT(q == NULL);
18623 		return (0);
18624 	}
18625 
18626 	if (CONN_Q(q)) {
18627 		connp = Q_TO_CONN(q);
18628 		mutex_enter(&connp->conn_lock);
18629 	} else {
18630 		connp = NULL;
18631 	}
18632 	mutex_enter(&ill->ill_lock);
18633 	/*
18634 	 * Are there any ire's pointing to this ipif that are still active ?
18635 	 * If this is the last ipif going down, are there any ire's pointing
18636 	 * to this ill that are still active ?
18637 	 */
18638 	if (ipif_is_quiescent(ipif)) {
18639 		mutex_exit(&ill->ill_lock);
18640 		if (connp != NULL)
18641 			mutex_exit(&connp->conn_lock);
18642 		return (0);
18643 	}
18644 
18645 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18646 	    ill->ill_name, (void *)ill));
18647 	/*
18648 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18649 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18650 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18651 	 */
18652 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18653 	if (!success) {
18654 		/* The conn is closing. So just return */
18655 		ASSERT(connp != NULL);
18656 		mutex_exit(&ill->ill_lock);
18657 		mutex_exit(&connp->conn_lock);
18658 		return (EINTR);
18659 	}
18660 
18661 	mutex_exit(&ill->ill_lock);
18662 	if (connp != NULL)
18663 		mutex_exit(&connp->conn_lock);
18664 	return (EINPROGRESS);
18665 }
18666 
18667 void
18668 ipif_down_tail(ipif_t *ipif)
18669 {
18670 	ill_t	*ill = ipif->ipif_ill;
18671 
18672 	/*
18673 	 * Skip any loopback interface (null wq).
18674 	 * If this is the last logical interface on the ill
18675 	 * have ill_dl_down tell the driver we are gone (unbind)
18676 	 * Note that lun 0 can ipif_down even though
18677 	 * there are other logical units that are up.
18678 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18679 	 */
18680 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18681 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18682 	    ill->ill_dl_up) {
18683 		ill_dl_down(ill);
18684 	}
18685 	ill->ill_logical_down = 0;
18686 
18687 	/*
18688 	 * Have to be after removing the routes in ipif_down_delete_ire.
18689 	 */
18690 	if (ipif->ipif_isv6) {
18691 		if (ill->ill_flags & ILLF_XRESOLV)
18692 			ipif_arp_down(ipif);
18693 	} else {
18694 		ipif_arp_down(ipif);
18695 	}
18696 
18697 	ip_rts_ifmsg(ipif);
18698 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18699 }
18700 
18701 /*
18702  * Bring interface logically down without bringing the physical interface
18703  * down e.g. when the netmask is changed. This avoids long lasting link
18704  * negotiations between an ethernet interface and a certain switches.
18705  */
18706 static int
18707 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18708 {
18709 	/*
18710 	 * The ill_logical_down flag is a transient flag. It is set here
18711 	 * and is cleared once the down has completed in ipif_down_tail.
18712 	 * This flag does not indicate whether the ill stream is in the
18713 	 * DL_BOUND state with the driver. Instead this flag is used by
18714 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18715 	 * the driver. The state of the ill stream i.e. whether it is
18716 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18717 	 */
18718 	ipif->ipif_ill->ill_logical_down = 1;
18719 	return (ipif_down(ipif, q, mp));
18720 }
18721 
18722 /*
18723  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18724  * If the usesrc client ILL is already part of a usesrc group or not,
18725  * in either case a ire_stq with the matching usesrc client ILL will
18726  * locate the IRE's that need to be deleted. We want IREs to be created
18727  * with the new source address.
18728  */
18729 static void
18730 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18731 {
18732 	ill_t	*ucill = (ill_t *)ill_arg;
18733 
18734 	ASSERT(IAM_WRITER_ILL(ucill));
18735 
18736 	if (ire->ire_stq == NULL)
18737 		return;
18738 
18739 	if ((ire->ire_type == IRE_CACHE) &&
18740 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18741 		ire_delete(ire);
18742 }
18743 
18744 /*
18745  * ire_walk routine to delete every IRE dependent on the interface
18746  * address that is going down.	(Always called as writer.)
18747  * Works for both v4 and v6.
18748  * In addition for checking for ire_ipif matches it also checks for
18749  * IRE_CACHE entries which have the same source address as the
18750  * disappearing ipif since ipif_select_source might have picked
18751  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18752  * care of any IRE_INTERFACE with the disappearing source address.
18753  */
18754 static void
18755 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18756 {
18757 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18758 	ill_t *ire_ill;
18759 	ill_t *ipif_ill;
18760 
18761 	ASSERT(IAM_WRITER_IPIF(ipif));
18762 	if (ire->ire_ipif == NULL)
18763 		return;
18764 
18765 	/*
18766 	 * For IPv4, we derive source addresses for an IRE from ipif's
18767 	 * belonging to the same IPMP group as the IRE's outgoing
18768 	 * interface.  If an IRE's outgoing interface isn't in the
18769 	 * same IPMP group as a particular ipif, then that ipif
18770 	 * couldn't have been used as a source address for this IRE.
18771 	 *
18772 	 * For IPv6, source addresses are only restricted to the IPMP group
18773 	 * if the IRE is for a link-local address or a multicast address.
18774 	 * Otherwise, source addresses for an IRE can be chosen from
18775 	 * interfaces other than the the outgoing interface for that IRE.
18776 	 *
18777 	 * For source address selection details, see ipif_select_source()
18778 	 * and ipif_select_source_v6().
18779 	 */
18780 	if (ire->ire_ipversion == IPV4_VERSION ||
18781 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18782 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18783 		ire_ill = ire->ire_ipif->ipif_ill;
18784 		ipif_ill = ipif->ipif_ill;
18785 
18786 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18787 			return;
18788 		}
18789 	}
18790 
18791 	if (ire->ire_ipif != ipif) {
18792 		/*
18793 		 * Look for a matching source address.
18794 		 */
18795 		if (ire->ire_type != IRE_CACHE)
18796 			return;
18797 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18798 			return;
18799 
18800 		if (ire->ire_ipversion == IPV4_VERSION) {
18801 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18802 				return;
18803 		} else {
18804 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18805 			    &ipif->ipif_v6lcl_addr))
18806 				return;
18807 		}
18808 		ire_delete(ire);
18809 		return;
18810 	}
18811 	/*
18812 	 * ire_delete() will do an ire_flush_cache which will delete
18813 	 * all ire_ipif matches
18814 	 */
18815 	ire_delete(ire);
18816 }
18817 
18818 /*
18819  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18820  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18821  * 2) when an interface is brought up or down (on that ill).
18822  * This ensures that the IRE_CACHE entries don't retain stale source
18823  * address selection results.
18824  */
18825 void
18826 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18827 {
18828 	ill_t	*ill = (ill_t *)ill_arg;
18829 	ill_t	*ipif_ill;
18830 
18831 	ASSERT(IAM_WRITER_ILL(ill));
18832 	/*
18833 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18834 	 * Hence this should be IRE_CACHE.
18835 	 */
18836 	ASSERT(ire->ire_type == IRE_CACHE);
18837 
18838 	/*
18839 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18840 	 * We are only interested in IRE_CACHES that has borrowed
18841 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18842 	 * for which we need to look at ire_ipif->ipif_ill match
18843 	 * with ill.
18844 	 */
18845 	ASSERT(ire->ire_ipif != NULL);
18846 	ipif_ill = ire->ire_ipif->ipif_ill;
18847 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18848 	    ipif_ill->ill_group == ill->ill_group)) {
18849 		ire_delete(ire);
18850 	}
18851 }
18852 
18853 /*
18854  * Delete all the ire whose stq references ill_arg.
18855  */
18856 static void
18857 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18858 {
18859 	ill_t	*ill = (ill_t *)ill_arg;
18860 	ill_t	*ire_ill;
18861 
18862 	ASSERT(IAM_WRITER_ILL(ill));
18863 	/*
18864 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18865 	 * Hence this should be IRE_CACHE.
18866 	 */
18867 	ASSERT(ire->ire_type == IRE_CACHE);
18868 
18869 	/*
18870 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18871 	 * matches ill. We are only interested in IRE_CACHES that
18872 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18873 	 * filtering here.
18874 	 */
18875 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18876 
18877 	if (ire_ill == ill)
18878 		ire_delete(ire);
18879 }
18880 
18881 /*
18882  * This is called when an ill leaves the group. We want to delete
18883  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18884  * pointing at ill.
18885  */
18886 static void
18887 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18888 {
18889 	ill_t	*ill = (ill_t *)ill_arg;
18890 
18891 	ASSERT(IAM_WRITER_ILL(ill));
18892 	ASSERT(ill->ill_group == NULL);
18893 	/*
18894 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18895 	 * Hence this should be IRE_CACHE.
18896 	 */
18897 	ASSERT(ire->ire_type == IRE_CACHE);
18898 	/*
18899 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18900 	 * matches ill. We are interested in both.
18901 	 */
18902 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18903 	    (ire->ire_ipif->ipif_ill == ill));
18904 
18905 	ire_delete(ire);
18906 }
18907 
18908 /*
18909  * Initiate deallocate of an IPIF. Always called as writer. Called by
18910  * ill_delete or ip_sioctl_removeif.
18911  */
18912 static void
18913 ipif_free(ipif_t *ipif)
18914 {
18915 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18916 
18917 	ASSERT(IAM_WRITER_IPIF(ipif));
18918 
18919 	if (ipif->ipif_recovery_id != 0)
18920 		(void) untimeout(ipif->ipif_recovery_id);
18921 	ipif->ipif_recovery_id = 0;
18922 
18923 	/* Remove conn references */
18924 	reset_conn_ipif(ipif);
18925 
18926 	/*
18927 	 * Make sure we have valid net and subnet broadcast ire's for the
18928 	 * other ipif's which share them with this ipif.
18929 	 */
18930 	if (!ipif->ipif_isv6)
18931 		ipif_check_bcast_ires(ipif);
18932 
18933 	/*
18934 	 * Take down the interface. We can be called either from ill_delete
18935 	 * or from ip_sioctl_removeif.
18936 	 */
18937 	(void) ipif_down(ipif, NULL, NULL);
18938 
18939 	/*
18940 	 * Now that the interface is down, there's no chance it can still
18941 	 * become a duplicate.  Cancel any timer that may have been set while
18942 	 * tearing down.
18943 	 */
18944 	if (ipif->ipif_recovery_id != 0)
18945 		(void) untimeout(ipif->ipif_recovery_id);
18946 	ipif->ipif_recovery_id = 0;
18947 
18948 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18949 	/* Remove pointers to this ill in the multicast routing tables */
18950 	reset_mrt_vif_ipif(ipif);
18951 	rw_exit(&ipst->ips_ill_g_lock);
18952 }
18953 
18954 /*
18955  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
18956  * also ill_move().
18957  */
18958 static void
18959 ipif_free_tail(ipif_t *ipif)
18960 {
18961 	mblk_t	*mp;
18962 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
18963 
18964 	/*
18965 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18966 	 */
18967 	mutex_enter(&ipif->ipif_saved_ire_lock);
18968 	mp = ipif->ipif_saved_ire_mp;
18969 	ipif->ipif_saved_ire_mp = NULL;
18970 	mutex_exit(&ipif->ipif_saved_ire_lock);
18971 	freemsg(mp);
18972 
18973 	/*
18974 	 * Need to hold both ill_g_lock and ill_lock while
18975 	 * inserting or removing an ipif from the linked list
18976 	 * of ipifs hanging off the ill.
18977 	 */
18978 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
18979 
18980 	ASSERT(ilm_walk_ipif(ipif) == 0);
18981 
18982 #ifdef DEBUG
18983 	ipif_trace_cleanup(ipif);
18984 #endif
18985 
18986 	/* Ask SCTP to take it out of it list */
18987 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18988 
18989 	/* Get it out of the ILL interface list. */
18990 	ipif_remove(ipif, B_TRUE);
18991 	rw_exit(&ipst->ips_ill_g_lock);
18992 
18993 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18994 
18995 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
18996 	ASSERT(ipif->ipif_recovery_id == 0);
18997 
18998 	/* Free the memory. */
18999 	mi_free(ipif);
19000 }
19001 
19002 /*
19003  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19004  * is zero.
19005  */
19006 void
19007 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19008 {
19009 	char	lbuf[LIFNAMSIZ];
19010 	char	*name;
19011 	size_t	name_len;
19012 
19013 	buf[0] = '\0';
19014 	name = ipif->ipif_ill->ill_name;
19015 	name_len = ipif->ipif_ill->ill_name_length;
19016 	if (ipif->ipif_id != 0) {
19017 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19018 		    ipif->ipif_id);
19019 		name = lbuf;
19020 		name_len = mi_strlen(name) + 1;
19021 	}
19022 	len -= 1;
19023 	buf[len] = '\0';
19024 	len = MIN(len, name_len);
19025 	bcopy(name, buf, len);
19026 }
19027 
19028 /*
19029  * Find an IPIF based on the name passed in.  Names can be of the
19030  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19031  * The <phys> string can have forms like <dev><#> (e.g., le0),
19032  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19033  * When there is no colon, the implied unit id is zero. <phys> must
19034  * correspond to the name of an ILL.  (May be called as writer.)
19035  */
19036 static ipif_t *
19037 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19038     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19039     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19040 {
19041 	char	*cp;
19042 	char	*endp;
19043 	long	id;
19044 	ill_t	*ill;
19045 	ipif_t	*ipif;
19046 	uint_t	ire_type;
19047 	boolean_t did_alloc = B_FALSE;
19048 	ipsq_t	*ipsq;
19049 
19050 	if (error != NULL)
19051 		*error = 0;
19052 
19053 	/*
19054 	 * If the caller wants to us to create the ipif, make sure we have a
19055 	 * valid zoneid
19056 	 */
19057 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19058 
19059 	if (namelen == 0) {
19060 		if (error != NULL)
19061 			*error = ENXIO;
19062 		return (NULL);
19063 	}
19064 
19065 	*exists = B_FALSE;
19066 	/* Look for a colon in the name. */
19067 	endp = &name[namelen];
19068 	for (cp = endp; --cp > name; ) {
19069 		if (*cp == IPIF_SEPARATOR_CHAR)
19070 			break;
19071 	}
19072 
19073 	if (*cp == IPIF_SEPARATOR_CHAR) {
19074 		/*
19075 		 * Reject any non-decimal aliases for logical
19076 		 * interfaces. Aliases with leading zeroes
19077 		 * are also rejected as they introduce ambiguity
19078 		 * in the naming of the interfaces.
19079 		 * In order to confirm with existing semantics,
19080 		 * and to not break any programs/script relying
19081 		 * on that behaviour, if<0>:0 is considered to be
19082 		 * a valid interface.
19083 		 *
19084 		 * If alias has two or more digits and the first
19085 		 * is zero, fail.
19086 		 */
19087 		if (&cp[2] < endp && cp[1] == '0') {
19088 			if (error != NULL)
19089 				*error = EINVAL;
19090 			return (NULL);
19091 		}
19092 	}
19093 
19094 	if (cp <= name) {
19095 		cp = endp;
19096 	} else {
19097 		*cp = '\0';
19098 	}
19099 
19100 	/*
19101 	 * Look up the ILL, based on the portion of the name
19102 	 * before the slash. ill_lookup_on_name returns a held ill.
19103 	 * Temporary to check whether ill exists already. If so
19104 	 * ill_lookup_on_name will clear it.
19105 	 */
19106 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19107 	    q, mp, func, error, &did_alloc, ipst);
19108 	if (cp != endp)
19109 		*cp = IPIF_SEPARATOR_CHAR;
19110 	if (ill == NULL)
19111 		return (NULL);
19112 
19113 	/* Establish the unit number in the name. */
19114 	id = 0;
19115 	if (cp < endp && *endp == '\0') {
19116 		/* If there was a colon, the unit number follows. */
19117 		cp++;
19118 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19119 			ill_refrele(ill);
19120 			if (error != NULL)
19121 				*error = ENXIO;
19122 			return (NULL);
19123 		}
19124 	}
19125 
19126 	GRAB_CONN_LOCK(q);
19127 	mutex_enter(&ill->ill_lock);
19128 	/* Now see if there is an IPIF with this unit number. */
19129 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19130 		if (ipif->ipif_id == id) {
19131 			if (zoneid != ALL_ZONES &&
19132 			    zoneid != ipif->ipif_zoneid &&
19133 			    ipif->ipif_zoneid != ALL_ZONES) {
19134 				mutex_exit(&ill->ill_lock);
19135 				RELEASE_CONN_LOCK(q);
19136 				ill_refrele(ill);
19137 				if (error != NULL)
19138 					*error = ENXIO;
19139 				return (NULL);
19140 			}
19141 			/*
19142 			 * The block comment at the start of ipif_down
19143 			 * explains the use of the macros used below
19144 			 */
19145 			if (IPIF_CAN_LOOKUP(ipif)) {
19146 				ipif_refhold_locked(ipif);
19147 				mutex_exit(&ill->ill_lock);
19148 				if (!did_alloc)
19149 					*exists = B_TRUE;
19150 				/*
19151 				 * Drop locks before calling ill_refrele
19152 				 * since it can potentially call into
19153 				 * ipif_ill_refrele_tail which can end up
19154 				 * in trying to acquire any lock.
19155 				 */
19156 				RELEASE_CONN_LOCK(q);
19157 				ill_refrele(ill);
19158 				return (ipif);
19159 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19160 				ipsq = ill->ill_phyint->phyint_ipsq;
19161 				mutex_enter(&ipsq->ipsq_lock);
19162 				mutex_exit(&ill->ill_lock);
19163 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19164 				mutex_exit(&ipsq->ipsq_lock);
19165 				RELEASE_CONN_LOCK(q);
19166 				ill_refrele(ill);
19167 				if (error != NULL)
19168 					*error = EINPROGRESS;
19169 				return (NULL);
19170 			}
19171 		}
19172 	}
19173 	RELEASE_CONN_LOCK(q);
19174 
19175 	if (!do_alloc) {
19176 		mutex_exit(&ill->ill_lock);
19177 		ill_refrele(ill);
19178 		if (error != NULL)
19179 			*error = ENXIO;
19180 		return (NULL);
19181 	}
19182 
19183 	/*
19184 	 * If none found, atomically allocate and return a new one.
19185 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19186 	 * to support "receive only" use of lo0:1 etc. as is still done
19187 	 * below as an initial guess.
19188 	 * However, this is now likely to be overriden later in ipif_up_done()
19189 	 * when we know for sure what address has been configured on the
19190 	 * interface, since we might have more than one loopback interface
19191 	 * with a loopback address, e.g. in the case of zones, and all the
19192 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19193 	 */
19194 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19195 		ire_type = IRE_LOOPBACK;
19196 	else
19197 		ire_type = IRE_LOCAL;
19198 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19199 	if (ipif != NULL)
19200 		ipif_refhold_locked(ipif);
19201 	else if (error != NULL)
19202 		*error = ENOMEM;
19203 	mutex_exit(&ill->ill_lock);
19204 	ill_refrele(ill);
19205 	return (ipif);
19206 }
19207 
19208 /*
19209  * This routine is called whenever a new address comes up on an ipif.  If
19210  * we are configured to respond to address mask requests, then we are supposed
19211  * to broadcast an address mask reply at this time.  This routine is also
19212  * called if we are already up, but a netmask change is made.  This is legal
19213  * but might not make the system manager very popular.	(May be called
19214  * as writer.)
19215  */
19216 void
19217 ipif_mask_reply(ipif_t *ipif)
19218 {
19219 	icmph_t	*icmph;
19220 	ipha_t	*ipha;
19221 	mblk_t	*mp;
19222 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19223 
19224 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19225 
19226 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19227 		return;
19228 
19229 	/* ICMP mask reply is IPv4 only */
19230 	ASSERT(!ipif->ipif_isv6);
19231 	/* ICMP mask reply is not for a loopback interface */
19232 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19233 
19234 	mp = allocb(REPLY_LEN, BPRI_HI);
19235 	if (mp == NULL)
19236 		return;
19237 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19238 
19239 	ipha = (ipha_t *)mp->b_rptr;
19240 	bzero(ipha, REPLY_LEN);
19241 	*ipha = icmp_ipha;
19242 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19243 	ipha->ipha_src = ipif->ipif_src_addr;
19244 	ipha->ipha_dst = ipif->ipif_brd_addr;
19245 	ipha->ipha_length = htons(REPLY_LEN);
19246 	ipha->ipha_ident = 0;
19247 
19248 	icmph = (icmph_t *)&ipha[1];
19249 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19250 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19251 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19252 
19253 	put(ipif->ipif_wq, mp);
19254 
19255 #undef	REPLY_LEN
19256 }
19257 
19258 /*
19259  * When the mtu in the ipif changes, we call this routine through ire_walk
19260  * to update all the relevant IREs.
19261  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19262  */
19263 static void
19264 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19265 {
19266 	ipif_t *ipif = (ipif_t *)ipif_arg;
19267 
19268 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19269 		return;
19270 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19271 }
19272 
19273 /*
19274  * When the mtu in the ill changes, we call this routine through ire_walk
19275  * to update all the relevant IREs.
19276  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19277  */
19278 void
19279 ill_mtu_change(ire_t *ire, char *ill_arg)
19280 {
19281 	ill_t	*ill = (ill_t *)ill_arg;
19282 
19283 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19284 		return;
19285 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19286 }
19287 
19288 /*
19289  * Join the ipif specific multicast groups.
19290  * Must be called after a mapping has been set up in the resolver.  (Always
19291  * called as writer.)
19292  */
19293 void
19294 ipif_multicast_up(ipif_t *ipif)
19295 {
19296 	int err, index;
19297 	ill_t *ill;
19298 
19299 	ASSERT(IAM_WRITER_IPIF(ipif));
19300 
19301 	ill = ipif->ipif_ill;
19302 	index = ill->ill_phyint->phyint_ifindex;
19303 
19304 	ip1dbg(("ipif_multicast_up\n"));
19305 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19306 		return;
19307 
19308 	if (ipif->ipif_isv6) {
19309 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19310 			return;
19311 
19312 		/* Join the all hosts multicast address */
19313 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19314 		/*
19315 		 * Passing B_TRUE means we have to join the multicast
19316 		 * membership on this interface even though this is
19317 		 * FAILED. If we join on a different one in the group,
19318 		 * we will not be able to delete the membership later
19319 		 * as we currently don't track where we join when we
19320 		 * join within the kernel unlike applications where
19321 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19322 		 * for more on this.
19323 		 */
19324 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19325 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19326 		if (err != 0) {
19327 			ip0dbg(("ipif_multicast_up: "
19328 			    "all_hosts_mcast failed %d\n",
19329 			    err));
19330 			return;
19331 		}
19332 		/*
19333 		 * Enable multicast for the solicited node multicast address
19334 		 */
19335 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19336 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19337 
19338 			ipv6_multi.s6_addr32[3] |=
19339 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19340 
19341 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19342 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19343 			    NULL);
19344 			if (err != 0) {
19345 				ip0dbg(("ipif_multicast_up: solicited MC"
19346 				    " failed %d\n", err));
19347 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19348 				    ill, ill->ill_phyint->phyint_ifindex,
19349 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19350 				return;
19351 			}
19352 		}
19353 	} else {
19354 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19355 			return;
19356 
19357 		/* Join the all hosts multicast address */
19358 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19359 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19360 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19361 		if (err) {
19362 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19363 			return;
19364 		}
19365 	}
19366 	ipif->ipif_multicast_up = 1;
19367 }
19368 
19369 /*
19370  * Blow away any multicast groups that we joined in ipif_multicast_up().
19371  * (Explicit memberships are blown away in ill_leave_multicast() when the
19372  * ill is brought down.)
19373  */
19374 static void
19375 ipif_multicast_down(ipif_t *ipif)
19376 {
19377 	int err;
19378 
19379 	ASSERT(IAM_WRITER_IPIF(ipif));
19380 
19381 	ip1dbg(("ipif_multicast_down\n"));
19382 	if (!ipif->ipif_multicast_up)
19383 		return;
19384 
19385 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19386 
19387 	if (!ipif->ipif_isv6) {
19388 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19389 		    B_TRUE);
19390 		if (err != 0)
19391 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19392 
19393 		ipif->ipif_multicast_up = 0;
19394 		return;
19395 	}
19396 
19397 	/*
19398 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19399 	 * we should look for ilms on this ill rather than the ones that have
19400 	 * been failed over here.  They are here temporarily. As
19401 	 * ipif_multicast_up has joined on this ill, we should delete only
19402 	 * from this ill.
19403 	 */
19404 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19405 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19406 	    B_TRUE, B_TRUE);
19407 	if (err != 0) {
19408 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19409 		    err));
19410 	}
19411 	/*
19412 	 * Disable multicast for the solicited node multicast address
19413 	 */
19414 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19415 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19416 
19417 		ipv6_multi.s6_addr32[3] |=
19418 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19419 
19420 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19421 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19422 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19423 
19424 		if (err != 0) {
19425 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19426 			    err));
19427 		}
19428 	}
19429 
19430 	ipif->ipif_multicast_up = 0;
19431 }
19432 
19433 /*
19434  * Used when an interface comes up to recreate any extra routes on this
19435  * interface.
19436  */
19437 static ire_t **
19438 ipif_recover_ire(ipif_t *ipif)
19439 {
19440 	mblk_t	*mp;
19441 	ire_t	**ipif_saved_irep;
19442 	ire_t	**irep;
19443 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19444 
19445 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19446 	    ipif->ipif_id));
19447 
19448 	mutex_enter(&ipif->ipif_saved_ire_lock);
19449 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19450 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19451 	if (ipif_saved_irep == NULL) {
19452 		mutex_exit(&ipif->ipif_saved_ire_lock);
19453 		return (NULL);
19454 	}
19455 
19456 	irep = ipif_saved_irep;
19457 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19458 		ire_t		*ire;
19459 		queue_t		*rfq;
19460 		queue_t		*stq;
19461 		ifrt_t		*ifrt;
19462 		uchar_t		*src_addr;
19463 		uchar_t		*gateway_addr;
19464 		ushort_t	type;
19465 
19466 		/*
19467 		 * When the ire was initially created and then added in
19468 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19469 		 * in the case of a traditional interface route, or as one of
19470 		 * the IRE_OFFSUBNET types (with the exception of
19471 		 * IRE_HOST types ire which is created by icmp_redirect() and
19472 		 * which we don't need to save or recover).  In the case where
19473 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19474 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19475 		 * to satisfy software like GateD and Sun Cluster which creates
19476 		 * routes using the the loopback interface's address as a
19477 		 * gateway.
19478 		 *
19479 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19480 		 * ire_create() will be called in the same way here as
19481 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19482 		 * the route looks like a traditional interface route (where
19483 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19484 		 * the saved ifrt->ifrt_type.  This means that in the case where
19485 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19486 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19487 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19488 		 */
19489 		ifrt = (ifrt_t *)mp->b_rptr;
19490 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19491 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19492 			rfq = NULL;
19493 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19494 			    ? ipif->ipif_rq : ipif->ipif_wq;
19495 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19496 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19497 			    : (uint8_t *)&ipif->ipif_src_addr;
19498 			gateway_addr = NULL;
19499 			type = ipif->ipif_net_type;
19500 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19501 			/* Recover multiroute broadcast IRE. */
19502 			rfq = ipif->ipif_rq;
19503 			stq = ipif->ipif_wq;
19504 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19505 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19506 			    : (uint8_t *)&ipif->ipif_src_addr;
19507 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19508 			type = ifrt->ifrt_type;
19509 		} else {
19510 			rfq = NULL;
19511 			stq = NULL;
19512 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19513 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19514 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19515 			type = ifrt->ifrt_type;
19516 		}
19517 
19518 		/*
19519 		 * Create a copy of the IRE with the saved address and netmask.
19520 		 */
19521 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19522 		    "0x%x/0x%x\n",
19523 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19524 		    ntohl(ifrt->ifrt_addr),
19525 		    ntohl(ifrt->ifrt_mask)));
19526 		ire = ire_create(
19527 		    (uint8_t *)&ifrt->ifrt_addr,
19528 		    (uint8_t *)&ifrt->ifrt_mask,
19529 		    src_addr,
19530 		    gateway_addr,
19531 		    &ifrt->ifrt_max_frag,
19532 		    NULL,
19533 		    rfq,
19534 		    stq,
19535 		    type,
19536 		    ipif,
19537 		    0,
19538 		    0,
19539 		    0,
19540 		    ifrt->ifrt_flags,
19541 		    &ifrt->ifrt_iulp_info,
19542 		    NULL,
19543 		    NULL,
19544 		    ipst);
19545 
19546 		if (ire == NULL) {
19547 			mutex_exit(&ipif->ipif_saved_ire_lock);
19548 			kmem_free(ipif_saved_irep,
19549 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19550 			return (NULL);
19551 		}
19552 
19553 		/*
19554 		 * Some software (for example, GateD and Sun Cluster) attempts
19555 		 * to create (what amount to) IRE_PREFIX routes with the
19556 		 * loopback address as the gateway.  This is primarily done to
19557 		 * set up prefixes with the RTF_REJECT flag set (for example,
19558 		 * when generating aggregate routes.)
19559 		 *
19560 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19561 		 * IRE_LOOPBACK, then we map the request into a
19562 		 * IRE_IF_NORESOLVER.
19563 		 */
19564 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19565 			ire->ire_type = IRE_IF_NORESOLVER;
19566 		/*
19567 		 * ire held by ire_add, will be refreled' towards the
19568 		 * the end of ipif_up_done
19569 		 */
19570 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19571 		*irep = ire;
19572 		irep++;
19573 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19574 	}
19575 	mutex_exit(&ipif->ipif_saved_ire_lock);
19576 	return (ipif_saved_irep);
19577 }
19578 
19579 /*
19580  * Used to set the netmask and broadcast address to default values when the
19581  * interface is brought up.  (Always called as writer.)
19582  */
19583 static void
19584 ipif_set_default(ipif_t *ipif)
19585 {
19586 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19587 
19588 	if (!ipif->ipif_isv6) {
19589 		/*
19590 		 * Interface holds an IPv4 address. Default
19591 		 * mask is the natural netmask.
19592 		 */
19593 		if (!ipif->ipif_net_mask) {
19594 			ipaddr_t	v4mask;
19595 
19596 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19597 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19598 		}
19599 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19600 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19601 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19602 		} else {
19603 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19604 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19605 		}
19606 		/*
19607 		 * NOTE: SunOS 4.X does this even if the broadcast address
19608 		 * has been already set thus we do the same here.
19609 		 */
19610 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19611 			ipaddr_t	v4addr;
19612 
19613 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19614 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19615 		}
19616 	} else {
19617 		/*
19618 		 * Interface holds an IPv6-only address.  Default
19619 		 * mask is all-ones.
19620 		 */
19621 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19622 			ipif->ipif_v6net_mask = ipv6_all_ones;
19623 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19624 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19625 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19626 		} else {
19627 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19628 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19629 		}
19630 	}
19631 }
19632 
19633 /*
19634  * Return 0 if this address can be used as local address without causing
19635  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19636  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19637  * Special checks are needed to allow the same IPv6 link-local address
19638  * on different ills.
19639  * TODO: allowing the same site-local address on different ill's.
19640  */
19641 int
19642 ip_addr_availability_check(ipif_t *new_ipif)
19643 {
19644 	in6_addr_t our_v6addr;
19645 	ill_t *ill;
19646 	ipif_t *ipif;
19647 	ill_walk_context_t ctx;
19648 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19649 
19650 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19651 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19652 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19653 
19654 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19655 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19656 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19657 		return (0);
19658 
19659 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19660 
19661 	if (new_ipif->ipif_isv6)
19662 		ill = ILL_START_WALK_V6(&ctx, ipst);
19663 	else
19664 		ill = ILL_START_WALK_V4(&ctx, ipst);
19665 
19666 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19667 		for (ipif = ill->ill_ipif; ipif != NULL;
19668 		    ipif = ipif->ipif_next) {
19669 			if ((ipif == new_ipif) ||
19670 			    !(ipif->ipif_flags & IPIF_UP) ||
19671 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19672 				continue;
19673 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19674 			    &our_v6addr)) {
19675 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19676 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19677 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19678 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19679 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19680 				    new_ipif->ipif_ill != ill)
19681 					continue;
19682 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19683 				    new_ipif->ipif_ill != ill)
19684 					continue;
19685 				else if (new_ipif->ipif_zoneid !=
19686 				    ipif->ipif_zoneid &&
19687 				    ipif->ipif_zoneid != ALL_ZONES &&
19688 				    IS_LOOPBACK(ill))
19689 					continue;
19690 				else if (new_ipif->ipif_ill == ill)
19691 					return (EADDRINUSE);
19692 				else
19693 					return (EADDRNOTAVAIL);
19694 			}
19695 		}
19696 	}
19697 
19698 	return (0);
19699 }
19700 
19701 /*
19702  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19703  * IREs for the ipif.
19704  * When the routine returns EINPROGRESS then mp has been consumed and
19705  * the ioctl will be acked from ip_rput_dlpi.
19706  */
19707 static int
19708 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19709 {
19710 	ill_t	*ill = ipif->ipif_ill;
19711 	boolean_t isv6 = ipif->ipif_isv6;
19712 	int	err = 0;
19713 	boolean_t success;
19714 
19715 	ASSERT(IAM_WRITER_IPIF(ipif));
19716 
19717 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19718 
19719 	/* Shouldn't get here if it is already up. */
19720 	if (ipif->ipif_flags & IPIF_UP)
19721 		return (EALREADY);
19722 
19723 	/* Skip arp/ndp for any loopback interface. */
19724 	if (ill->ill_wq != NULL) {
19725 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19726 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19727 
19728 		if (!ill->ill_dl_up) {
19729 			/*
19730 			 * ill_dl_up is not yet set. i.e. we are yet to
19731 			 * DL_BIND with the driver and this is the first
19732 			 * logical interface on the ill to become "up".
19733 			 * Tell the driver to get going (via DL_BIND_REQ).
19734 			 * Note that changing "significant" IFF_ flags
19735 			 * address/netmask etc cause a down/up dance, but
19736 			 * does not cause an unbind (DL_UNBIND) with the driver
19737 			 */
19738 			return (ill_dl_up(ill, ipif, mp, q));
19739 		}
19740 
19741 		/*
19742 		 * ipif_resolver_up may end up sending an
19743 		 * AR_INTERFACE_UP message to ARP, which would, in
19744 		 * turn send a DLPI message to the driver. ioctls are
19745 		 * serialized and so we cannot send more than one
19746 		 * interface up message at a time. If ipif_resolver_up
19747 		 * does send an interface up message to ARP, we get
19748 		 * EINPROGRESS and we will complete in ip_arp_done.
19749 		 */
19750 
19751 		ASSERT(connp != NULL || !CONN_Q(q));
19752 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19753 		if (connp != NULL)
19754 			mutex_enter(&connp->conn_lock);
19755 		mutex_enter(&ill->ill_lock);
19756 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19757 		mutex_exit(&ill->ill_lock);
19758 		if (connp != NULL)
19759 			mutex_exit(&connp->conn_lock);
19760 		if (!success)
19761 			return (EINTR);
19762 
19763 		/*
19764 		 * Crank up IPv6 neighbor discovery
19765 		 * Unlike ARP, this should complete when
19766 		 * ipif_ndp_up returns. However, for
19767 		 * ILLF_XRESOLV interfaces we also send a
19768 		 * AR_INTERFACE_UP to the external resolver.
19769 		 * That ioctl will complete in ip_rput.
19770 		 */
19771 		if (isv6) {
19772 			err = ipif_ndp_up(ipif);
19773 			if (err != 0) {
19774 				if (err != EINPROGRESS)
19775 					mp = ipsq_pending_mp_get(ipsq, &connp);
19776 				return (err);
19777 			}
19778 		}
19779 		/* Now, ARP */
19780 		err = ipif_resolver_up(ipif, Res_act_initial);
19781 		if (err == EINPROGRESS) {
19782 			/* We will complete it in ip_arp_done */
19783 			return (err);
19784 		}
19785 		mp = ipsq_pending_mp_get(ipsq, &connp);
19786 		ASSERT(mp != NULL);
19787 		if (err != 0)
19788 			return (err);
19789 	} else {
19790 		/*
19791 		 * Interfaces without underlying hardware don't do duplicate
19792 		 * address detection.
19793 		 */
19794 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19795 		ipif->ipif_addr_ready = 1;
19796 	}
19797 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19798 }
19799 
19800 /*
19801  * Perform a bind for the physical device.
19802  * When the routine returns EINPROGRESS then mp has been consumed and
19803  * the ioctl will be acked from ip_rput_dlpi.
19804  * Allocate an unbind message and save it until ipif_down.
19805  */
19806 static int
19807 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19808 {
19809 	areq_t	*areq;
19810 	mblk_t	*areq_mp = NULL;
19811 	mblk_t	*bind_mp = NULL;
19812 	mblk_t	*unbind_mp = NULL;
19813 	conn_t	*connp;
19814 	boolean_t success;
19815 	uint16_t sap_addr;
19816 
19817 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19818 	ASSERT(IAM_WRITER_ILL(ill));
19819 	ASSERT(mp != NULL);
19820 
19821 	/* Create a resolver cookie for ARP */
19822 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19823 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19824 		if (areq_mp == NULL)
19825 			return (ENOMEM);
19826 
19827 		freemsg(ill->ill_resolver_mp);
19828 		ill->ill_resolver_mp = areq_mp;
19829 		areq = (areq_t *)areq_mp->b_rptr;
19830 		sap_addr = ill->ill_sap;
19831 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19832 	}
19833 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19834 	    DL_BIND_REQ);
19835 	if (bind_mp == NULL)
19836 		goto bad;
19837 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19838 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19839 
19840 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19841 	if (unbind_mp == NULL)
19842 		goto bad;
19843 
19844 	/*
19845 	 * Record state needed to complete this operation when the
19846 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19847 	 */
19848 	ASSERT(WR(q)->q_next == NULL);
19849 	connp = Q_TO_CONN(q);
19850 
19851 	mutex_enter(&connp->conn_lock);
19852 	mutex_enter(&ipif->ipif_ill->ill_lock);
19853 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19854 	mutex_exit(&ipif->ipif_ill->ill_lock);
19855 	mutex_exit(&connp->conn_lock);
19856 	if (!success)
19857 		goto bad;
19858 
19859 	/*
19860 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19861 	 * the interface goes down.
19862 	 */
19863 	ASSERT(ill->ill_unbind_mp == NULL);
19864 	ill->ill_unbind_mp = unbind_mp;
19865 
19866 	ill_dlpi_send(ill, bind_mp);
19867 	/* Send down link-layer capabilities probe if not already done. */
19868 	ill_capability_probe(ill);
19869 
19870 	/*
19871 	 * Sysid used to rely on the fact that netboots set domainname
19872 	 * and the like. Now that miniroot boots aren't strictly netboots
19873 	 * and miniroot network configuration is driven from userland
19874 	 * these things still need to be set. This situation can be detected
19875 	 * by comparing the interface being configured here to the one
19876 	 * dhcifname was set to reference by the boot loader. Once sysid is
19877 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19878 	 */
19879 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
19880 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
19881 	    (strlen(srpc_domain) == 0)) {
19882 		if (dhcpinit() != 0)
19883 			cmn_err(CE_WARN, "no cached dhcp response");
19884 	}
19885 
19886 	/*
19887 	 * This operation will complete in ip_rput_dlpi with either
19888 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19889 	 */
19890 	return (EINPROGRESS);
19891 bad:
19892 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19893 	/*
19894 	 * We don't have to check for possible removal from illgrp
19895 	 * as we have not yet inserted in illgrp. For groups
19896 	 * without names, this ipif is still not UP and hence
19897 	 * this could not have possibly had any influence in forming
19898 	 * groups.
19899 	 */
19900 
19901 	freemsg(bind_mp);
19902 	freemsg(unbind_mp);
19903 	return (ENOMEM);
19904 }
19905 
19906 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19907 
19908 /*
19909  * DLPI and ARP is up.
19910  * Create all the IREs associated with an interface bring up multicast.
19911  * Set the interface flag and finish other initialization
19912  * that potentially had to be differed to after DL_BIND_ACK.
19913  */
19914 int
19915 ipif_up_done(ipif_t *ipif)
19916 {
19917 	ire_t	*ire_array[20];
19918 	ire_t	**irep = ire_array;
19919 	ire_t	**irep1;
19920 	ipaddr_t net_mask = 0;
19921 	ipaddr_t subnet_mask, route_mask;
19922 	ill_t	*ill = ipif->ipif_ill;
19923 	queue_t	*stq;
19924 	ipif_t	 *src_ipif;
19925 	ipif_t   *tmp_ipif;
19926 	boolean_t	flush_ire_cache = B_TRUE;
19927 	int	err = 0;
19928 	phyint_t *phyi;
19929 	ire_t	**ipif_saved_irep = NULL;
19930 	int ipif_saved_ire_cnt;
19931 	int	cnt;
19932 	boolean_t	src_ipif_held = B_FALSE;
19933 	boolean_t	ire_added = B_FALSE;
19934 	boolean_t	loopback = B_FALSE;
19935 	ip_stack_t	*ipst = ill->ill_ipst;
19936 
19937 	ip1dbg(("ipif_up_done(%s:%u)\n",
19938 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
19939 	/* Check if this is a loopback interface */
19940 	if (ipif->ipif_ill->ill_wq == NULL)
19941 		loopback = B_TRUE;
19942 
19943 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19944 	/*
19945 	 * If all other interfaces for this ill are down or DEPRECATED,
19946 	 * or otherwise unsuitable for source address selection, remove
19947 	 * any IRE_CACHE entries for this ill to make sure source
19948 	 * address selection gets to take this new ipif into account.
19949 	 * No need to hold ill_lock while traversing the ipif list since
19950 	 * we are writer
19951 	 */
19952 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19953 	    tmp_ipif = tmp_ipif->ipif_next) {
19954 		if (((tmp_ipif->ipif_flags &
19955 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
19956 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
19957 		    (tmp_ipif == ipif))
19958 			continue;
19959 		/* first useable pre-existing interface */
19960 		flush_ire_cache = B_FALSE;
19961 		break;
19962 	}
19963 	if (flush_ire_cache)
19964 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19965 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19966 
19967 	/*
19968 	 * Figure out which way the send-to queue should go.  Only
19969 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19970 	 * should show up here.
19971 	 */
19972 	switch (ill->ill_net_type) {
19973 	case IRE_IF_RESOLVER:
19974 		stq = ill->ill_rq;
19975 		break;
19976 	case IRE_IF_NORESOLVER:
19977 	case IRE_LOOPBACK:
19978 		stq = ill->ill_wq;
19979 		break;
19980 	default:
19981 		return (EINVAL);
19982 	}
19983 
19984 	if (IS_LOOPBACK(ill)) {
19985 		/*
19986 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19987 		 * ipif_lookup_on_name(), but in the case of zones we can have
19988 		 * several loopback addresses on lo0. So all the interfaces with
19989 		 * loopback addresses need to be marked IRE_LOOPBACK.
19990 		 */
19991 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19992 		    htonl(INADDR_LOOPBACK))
19993 			ipif->ipif_ire_type = IRE_LOOPBACK;
19994 		else
19995 			ipif->ipif_ire_type = IRE_LOCAL;
19996 	}
19997 
19998 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19999 		/*
20000 		 * Can't use our source address. Select a different
20001 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20002 		 */
20003 		src_ipif = ipif_select_source(ipif->ipif_ill,
20004 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20005 		if (src_ipif == NULL)
20006 			src_ipif = ipif;	/* Last resort */
20007 		else
20008 			src_ipif_held = B_TRUE;
20009 	} else {
20010 		src_ipif = ipif;
20011 	}
20012 
20013 	/* Create all the IREs associated with this interface */
20014 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20015 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20016 
20017 		/*
20018 		 * If we're on a labeled system then make sure that zone-
20019 		 * private addresses have proper remote host database entries.
20020 		 */
20021 		if (is_system_labeled() &&
20022 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20023 		    !tsol_check_interface_address(ipif))
20024 			return (EINVAL);
20025 
20026 		/* Register the source address for __sin6_src_id */
20027 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20028 		    ipif->ipif_zoneid, ipst);
20029 		if (err != 0) {
20030 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20031 			return (err);
20032 		}
20033 
20034 		/* If the interface address is set, create the local IRE. */
20035 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20036 		    (void *)ipif,
20037 		    ipif->ipif_ire_type,
20038 		    ntohl(ipif->ipif_lcl_addr)));
20039 		*irep++ = ire_create(
20040 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20041 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20042 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20043 		    NULL,				/* no gateway */
20044 		    &ip_loopback_mtuplus,		/* max frag size */
20045 		    NULL,
20046 		    ipif->ipif_rq,			/* recv-from queue */
20047 		    NULL,				/* no send-to queue */
20048 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20049 		    ipif,
20050 		    0,
20051 		    0,
20052 		    0,
20053 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20054 		    RTF_PRIVATE : 0,
20055 		    &ire_uinfo_null,
20056 		    NULL,
20057 		    NULL,
20058 		    ipst);
20059 	} else {
20060 		ip1dbg((
20061 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20062 		    ipif->ipif_ire_type,
20063 		    ntohl(ipif->ipif_lcl_addr),
20064 		    (uint_t)ipif->ipif_flags));
20065 	}
20066 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20067 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20068 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20069 	} else {
20070 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20071 	}
20072 
20073 	subnet_mask = ipif->ipif_net_mask;
20074 
20075 	/*
20076 	 * If mask was not specified, use natural netmask of
20077 	 * interface address. Also, store this mask back into the
20078 	 * ipif struct.
20079 	 */
20080 	if (subnet_mask == 0) {
20081 		subnet_mask = net_mask;
20082 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20083 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20084 		    ipif->ipif_v6subnet);
20085 	}
20086 
20087 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20088 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20089 	    ipif->ipif_subnet != INADDR_ANY) {
20090 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20091 
20092 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20093 			route_mask = IP_HOST_MASK;
20094 		} else {
20095 			route_mask = subnet_mask;
20096 		}
20097 
20098 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20099 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20100 		    (void *)ipif, (void *)ill,
20101 		    ill->ill_net_type,
20102 		    ntohl(ipif->ipif_subnet)));
20103 		*irep++ = ire_create(
20104 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20105 		    (uchar_t *)&route_mask,		/* mask */
20106 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20107 		    NULL,				/* no gateway */
20108 		    &ipif->ipif_mtu,			/* max frag */
20109 		    NULL,
20110 		    NULL,				/* no recv queue */
20111 		    stq,				/* send-to queue */
20112 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20113 		    ipif,
20114 		    0,
20115 		    0,
20116 		    0,
20117 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20118 		    &ire_uinfo_null,
20119 		    NULL,
20120 		    NULL,
20121 		    ipst);
20122 	}
20123 
20124 	/*
20125 	 * Create any necessary broadcast IREs.
20126 	 */
20127 	if (ipif->ipif_flags & IPIF_BROADCAST)
20128 		irep = ipif_create_bcast_ires(ipif, irep);
20129 
20130 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20131 
20132 	/* If an earlier ire_create failed, get out now */
20133 	for (irep1 = irep; irep1 > ire_array; ) {
20134 		irep1--;
20135 		if (*irep1 == NULL) {
20136 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20137 			err = ENOMEM;
20138 			goto bad;
20139 		}
20140 	}
20141 
20142 	/*
20143 	 * Need to atomically check for ip_addr_availablity_check
20144 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20145 	 * from group also.The ill_g_lock is grabbed as reader
20146 	 * just to make sure no new ills or new ipifs are being added
20147 	 * to the system while we are checking the uniqueness of addresses.
20148 	 */
20149 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20150 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20151 	/* Mark it up, and increment counters. */
20152 	ipif->ipif_flags |= IPIF_UP;
20153 	ill->ill_ipif_up_count++;
20154 	err = ip_addr_availability_check(ipif);
20155 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20156 	rw_exit(&ipst->ips_ill_g_lock);
20157 
20158 	if (err != 0) {
20159 		/*
20160 		 * Our address may already be up on the same ill. In this case,
20161 		 * the ARP entry for our ipif replaced the one for the other
20162 		 * ipif. So we don't want to delete it (otherwise the other ipif
20163 		 * would be unable to send packets).
20164 		 * ip_addr_availability_check() identifies this case for us and
20165 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20166 		 * which is the expected error code.
20167 		 */
20168 		if (err == EADDRINUSE) {
20169 			freemsg(ipif->ipif_arp_del_mp);
20170 			ipif->ipif_arp_del_mp = NULL;
20171 			err = EADDRNOTAVAIL;
20172 		}
20173 		ill->ill_ipif_up_count--;
20174 		ipif->ipif_flags &= ~IPIF_UP;
20175 		goto bad;
20176 	}
20177 
20178 	/*
20179 	 * Add in all newly created IREs.  ire_create_bcast() has
20180 	 * already checked for duplicates of the IRE_BROADCAST type.
20181 	 * We want to add before we call ifgrp_insert which wants
20182 	 * to know whether IRE_IF_RESOLVER exists or not.
20183 	 *
20184 	 * NOTE : We refrele the ire though we may branch to "bad"
20185 	 *	  later on where we do ire_delete. This is okay
20186 	 *	  because nobody can delete it as we are running
20187 	 *	  exclusively.
20188 	 */
20189 	for (irep1 = irep; irep1 > ire_array; ) {
20190 		irep1--;
20191 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20192 		/*
20193 		 * refheld by ire_add. refele towards the end of the func
20194 		 */
20195 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20196 	}
20197 	ire_added = B_TRUE;
20198 	/*
20199 	 * Form groups if possible.
20200 	 *
20201 	 * If we are supposed to be in a ill_group with a name, insert it
20202 	 * now as we know that at least one ipif is UP. Otherwise form
20203 	 * nameless groups.
20204 	 *
20205 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20206 	 * this ipif into the appropriate interface group, or create a
20207 	 * new one. If this is already in a nameless group, we try to form
20208 	 * a bigger group looking at other ills potentially sharing this
20209 	 * ipif's prefix.
20210 	 */
20211 	phyi = ill->ill_phyint;
20212 	if (phyi->phyint_groupname_len != 0) {
20213 		ASSERT(phyi->phyint_groupname != NULL);
20214 		if (ill->ill_ipif_up_count == 1) {
20215 			ASSERT(ill->ill_group == NULL);
20216 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20217 			    phyi->phyint_groupname, NULL, B_TRUE);
20218 			if (err != 0) {
20219 				ip1dbg(("ipif_up_done: illgrp allocation "
20220 				    "failed, error %d\n", err));
20221 				goto bad;
20222 			}
20223 		}
20224 		ASSERT(ill->ill_group != NULL);
20225 	}
20226 
20227 	/*
20228 	 * When this is part of group, we need to make sure that
20229 	 * any broadcast ires created because of this ipif coming
20230 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20231 	 * so that we don't receive duplicate broadcast packets.
20232 	 */
20233 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20234 		ipif_renominate_bcast(ipif);
20235 
20236 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20237 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20238 	ipif_saved_irep = ipif_recover_ire(ipif);
20239 
20240 	if (!loopback) {
20241 		/*
20242 		 * If the broadcast address has been set, make sure it makes
20243 		 * sense based on the interface address.
20244 		 * Only match on ill since we are sharing broadcast addresses.
20245 		 */
20246 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20247 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20248 			ire_t	*ire;
20249 
20250 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20251 			    IRE_BROADCAST, ipif, ALL_ZONES,
20252 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20253 
20254 			if (ire == NULL) {
20255 				/*
20256 				 * If there isn't a matching broadcast IRE,
20257 				 * revert to the default for this netmask.
20258 				 */
20259 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20260 				mutex_enter(&ipif->ipif_ill->ill_lock);
20261 				ipif_set_default(ipif);
20262 				mutex_exit(&ipif->ipif_ill->ill_lock);
20263 			} else {
20264 				ire_refrele(ire);
20265 			}
20266 		}
20267 
20268 	}
20269 
20270 	if (ill->ill_need_recover_multicast) {
20271 		/*
20272 		 * Need to recover all multicast memberships in the driver.
20273 		 * This had to be deferred until we had attached.  The same
20274 		 * code exists in ipif_up_done_v6() to recover IPv6
20275 		 * memberships.
20276 		 *
20277 		 * Note that it would be preferable to unconditionally do the
20278 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
20279 		 * that since ill_join_allmulti() depends on ill_dl_up being
20280 		 * set, and it is not set until we receive a DL_BIND_ACK after
20281 		 * having called ill_dl_up().
20282 		 */
20283 		ill_recover_multicast(ill);
20284 	}
20285 	/* Join the allhosts multicast address */
20286 	ipif_multicast_up(ipif);
20287 
20288 	if (!loopback) {
20289 		/*
20290 		 * See whether anybody else would benefit from the
20291 		 * new ipif that we added. We call this always rather
20292 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20293 		 * ipif is for the benefit of illgrp_insert (done above)
20294 		 * which does not do source address selection as it does
20295 		 * not want to re-create interface routes that we are
20296 		 * having reference to it here.
20297 		 */
20298 		ill_update_source_selection(ill);
20299 	}
20300 
20301 	for (irep1 = irep; irep1 > ire_array; ) {
20302 		irep1--;
20303 		if (*irep1 != NULL) {
20304 			/* was held in ire_add */
20305 			ire_refrele(*irep1);
20306 		}
20307 	}
20308 
20309 	cnt = ipif_saved_ire_cnt;
20310 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20311 		if (*irep1 != NULL) {
20312 			/* was held in ire_add */
20313 			ire_refrele(*irep1);
20314 		}
20315 	}
20316 
20317 	if (!loopback && ipif->ipif_addr_ready) {
20318 		/* Broadcast an address mask reply. */
20319 		ipif_mask_reply(ipif);
20320 	}
20321 	if (ipif_saved_irep != NULL) {
20322 		kmem_free(ipif_saved_irep,
20323 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20324 	}
20325 	if (src_ipif_held)
20326 		ipif_refrele(src_ipif);
20327 
20328 	/*
20329 	 * This had to be deferred until we had bound.  Tell routing sockets and
20330 	 * others that this interface is up if it looks like the address has
20331 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20332 	 * duplicate address detection to do its thing.
20333 	 */
20334 	if (ipif->ipif_addr_ready)
20335 		ipif_up_notify(ipif);
20336 	return (0);
20337 
20338 bad:
20339 	ip1dbg(("ipif_up_done: FAILED \n"));
20340 	/*
20341 	 * We don't have to bother removing from ill groups because
20342 	 *
20343 	 * 1) For groups with names, we insert only when the first ipif
20344 	 *    comes up. In that case if it fails, it will not be in any
20345 	 *    group. So, we need not try to remove for that case.
20346 	 *
20347 	 * 2) For groups without names, either we tried to insert ipif_ill
20348 	 *    in a group as singleton or found some other group to become
20349 	 *    a bigger group. For the former, if it fails we don't have
20350 	 *    anything to do as ipif_ill is not in the group and for the
20351 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20352 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20353 	 */
20354 	while (irep > ire_array) {
20355 		irep--;
20356 		if (*irep != NULL) {
20357 			ire_delete(*irep);
20358 			if (ire_added)
20359 				ire_refrele(*irep);
20360 		}
20361 	}
20362 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20363 
20364 	if (ipif_saved_irep != NULL) {
20365 		kmem_free(ipif_saved_irep,
20366 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20367 	}
20368 	if (src_ipif_held)
20369 		ipif_refrele(src_ipif);
20370 
20371 	ipif_arp_down(ipif);
20372 	return (err);
20373 }
20374 
20375 /*
20376  * Turn off the ARP with the ILLF_NOARP flag.
20377  */
20378 static int
20379 ill_arp_off(ill_t *ill)
20380 {
20381 	mblk_t	*arp_off_mp = NULL;
20382 	mblk_t	*arp_on_mp = NULL;
20383 
20384 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20385 
20386 	ASSERT(IAM_WRITER_ILL(ill));
20387 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20388 
20389 	/*
20390 	 * If the on message is still around we've already done
20391 	 * an arp_off without doing an arp_on thus there is no
20392 	 * work needed.
20393 	 */
20394 	if (ill->ill_arp_on_mp != NULL)
20395 		return (0);
20396 
20397 	/*
20398 	 * Allocate an ARP on message (to be saved) and an ARP off message
20399 	 */
20400 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20401 	if (!arp_off_mp)
20402 		return (ENOMEM);
20403 
20404 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20405 	if (!arp_on_mp)
20406 		goto failed;
20407 
20408 	ASSERT(ill->ill_arp_on_mp == NULL);
20409 	ill->ill_arp_on_mp = arp_on_mp;
20410 
20411 	/* Send an AR_INTERFACE_OFF request */
20412 	putnext(ill->ill_rq, arp_off_mp);
20413 	return (0);
20414 failed:
20415 
20416 	if (arp_off_mp)
20417 		freemsg(arp_off_mp);
20418 	return (ENOMEM);
20419 }
20420 
20421 /*
20422  * Turn on ARP by turning off the ILLF_NOARP flag.
20423  */
20424 static int
20425 ill_arp_on(ill_t *ill)
20426 {
20427 	mblk_t	*mp;
20428 
20429 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20430 
20431 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20432 
20433 	ASSERT(IAM_WRITER_ILL(ill));
20434 	/*
20435 	 * Send an AR_INTERFACE_ON request if we have already done
20436 	 * an arp_off (which allocated the message).
20437 	 */
20438 	if (ill->ill_arp_on_mp != NULL) {
20439 		mp = ill->ill_arp_on_mp;
20440 		ill->ill_arp_on_mp = NULL;
20441 		putnext(ill->ill_rq, mp);
20442 	}
20443 	return (0);
20444 }
20445 
20446 /*
20447  * Called after either deleting ill from the group or when setting
20448  * FAILED or STANDBY on the interface.
20449  */
20450 static void
20451 illgrp_reset_schednext(ill_t *ill)
20452 {
20453 	ill_group_t *illgrp;
20454 	ill_t *save_ill;
20455 
20456 	ASSERT(IAM_WRITER_ILL(ill));
20457 	/*
20458 	 * When called from illgrp_delete, ill_group will be non-NULL.
20459 	 * But when called from ip_sioctl_flags, it could be NULL if
20460 	 * somebody is setting FAILED/INACTIVE on some interface which
20461 	 * is not part of a group.
20462 	 */
20463 	illgrp = ill->ill_group;
20464 	if (illgrp == NULL)
20465 		return;
20466 	if (illgrp->illgrp_ill_schednext != ill)
20467 		return;
20468 
20469 	illgrp->illgrp_ill_schednext = NULL;
20470 	save_ill = ill;
20471 	/*
20472 	 * Choose a good ill to be the next one for
20473 	 * outbound traffic. As the flags FAILED/STANDBY is
20474 	 * not yet marked when called from ip_sioctl_flags,
20475 	 * we check for ill separately.
20476 	 */
20477 	for (ill = illgrp->illgrp_ill; ill != NULL;
20478 	    ill = ill->ill_group_next) {
20479 		if ((ill != save_ill) &&
20480 		    !(ill->ill_phyint->phyint_flags &
20481 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20482 			illgrp->illgrp_ill_schednext = ill;
20483 			return;
20484 		}
20485 	}
20486 }
20487 
20488 /*
20489  * Given an ill, find the next ill in the group to be scheduled.
20490  * (This should be called by ip_newroute() before ire_create().)
20491  * The passed in ill may be pulled out of the group, after we have picked
20492  * up a different outgoing ill from the same group. However ire add will
20493  * atomically check this.
20494  */
20495 ill_t *
20496 illgrp_scheduler(ill_t *ill)
20497 {
20498 	ill_t *retill;
20499 	ill_group_t *illgrp;
20500 	int illcnt;
20501 	int i;
20502 	uint64_t flags;
20503 	ip_stack_t	*ipst = ill->ill_ipst;
20504 
20505 	/*
20506 	 * We don't use a lock to check for the ill_group. If this ill
20507 	 * is currently being inserted we may end up just returning this
20508 	 * ill itself. That is ok.
20509 	 */
20510 	if (ill->ill_group == NULL) {
20511 		ill_refhold(ill);
20512 		return (ill);
20513 	}
20514 
20515 	/*
20516 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20517 	 * a set of stable ills. No ill can be added or deleted or change
20518 	 * group while we hold the reader lock.
20519 	 */
20520 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20521 	if ((illgrp = ill->ill_group) == NULL) {
20522 		rw_exit(&ipst->ips_ill_g_lock);
20523 		ill_refhold(ill);
20524 		return (ill);
20525 	}
20526 
20527 	illcnt = illgrp->illgrp_ill_count;
20528 	mutex_enter(&illgrp->illgrp_lock);
20529 	retill = illgrp->illgrp_ill_schednext;
20530 
20531 	if (retill == NULL)
20532 		retill = illgrp->illgrp_ill;
20533 
20534 	/*
20535 	 * We do a circular search beginning at illgrp_ill_schednext
20536 	 * or illgrp_ill. We don't check the flags against the ill lock
20537 	 * since it can change anytime. The ire creation will be atomic
20538 	 * and will fail if the ill is FAILED or OFFLINE.
20539 	 */
20540 	for (i = 0; i < illcnt; i++) {
20541 		flags = retill->ill_phyint->phyint_flags;
20542 
20543 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20544 		    ILL_CAN_LOOKUP(retill)) {
20545 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20546 			ill_refhold(retill);
20547 			break;
20548 		}
20549 		retill = retill->ill_group_next;
20550 		if (retill == NULL)
20551 			retill = illgrp->illgrp_ill;
20552 	}
20553 	mutex_exit(&illgrp->illgrp_lock);
20554 	rw_exit(&ipst->ips_ill_g_lock);
20555 
20556 	return (i == illcnt ? NULL : retill);
20557 }
20558 
20559 /*
20560  * Checks for availbility of a usable source address (if there is one) when the
20561  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20562  * this selection is done regardless of the destination.
20563  */
20564 boolean_t
20565 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20566 {
20567 	uint_t	ifindex;
20568 	ipif_t	*ipif = NULL;
20569 	ill_t	*uill;
20570 	boolean_t isv6;
20571 	ip_stack_t	*ipst = ill->ill_ipst;
20572 
20573 	ASSERT(ill != NULL);
20574 
20575 	isv6 = ill->ill_isv6;
20576 	ifindex = ill->ill_usesrc_ifindex;
20577 	if (ifindex != 0) {
20578 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20579 		    NULL, ipst);
20580 		if (uill == NULL)
20581 			return (NULL);
20582 		mutex_enter(&uill->ill_lock);
20583 		for (ipif = uill->ill_ipif; ipif != NULL;
20584 		    ipif = ipif->ipif_next) {
20585 			if (!IPIF_CAN_LOOKUP(ipif))
20586 				continue;
20587 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20588 				continue;
20589 			if (!(ipif->ipif_flags & IPIF_UP))
20590 				continue;
20591 			if (ipif->ipif_zoneid != zoneid)
20592 				continue;
20593 			if ((isv6 &&
20594 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20595 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20596 				continue;
20597 			mutex_exit(&uill->ill_lock);
20598 			ill_refrele(uill);
20599 			return (B_TRUE);
20600 		}
20601 		mutex_exit(&uill->ill_lock);
20602 		ill_refrele(uill);
20603 	}
20604 	return (B_FALSE);
20605 }
20606 
20607 /*
20608  * Determine the best source address given a destination address and an ill.
20609  * Prefers non-deprecated over deprecated but will return a deprecated
20610  * address if there is no other choice. If there is a usable source address
20611  * on the interface pointed to by ill_usesrc_ifindex then that is given
20612  * first preference.
20613  *
20614  * Returns NULL if there is no suitable source address for the ill.
20615  * This only occurs when there is no valid source address for the ill.
20616  */
20617 ipif_t *
20618 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20619 {
20620 	ipif_t *ipif;
20621 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20622 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20623 	int index = 0;
20624 	boolean_t wrapped = B_FALSE;
20625 	boolean_t same_subnet_only = B_FALSE;
20626 	boolean_t ipif_same_found, ipif_other_found;
20627 	boolean_t specific_found;
20628 	ill_t	*till, *usill = NULL;
20629 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20630 	ip_stack_t	*ipst = ill->ill_ipst;
20631 
20632 	if (ill->ill_usesrc_ifindex != 0) {
20633 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20634 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20635 		if (usill != NULL)
20636 			ill = usill;	/* Select source from usesrc ILL */
20637 		else
20638 			return (NULL);
20639 	}
20640 
20641 	/*
20642 	 * If we're dealing with an unlabeled destination on a labeled system,
20643 	 * make sure that we ignore source addresses that are incompatible with
20644 	 * the destination's default label.  That destination's default label
20645 	 * must dominate the minimum label on the source address.
20646 	 */
20647 	dst_rhtp = NULL;
20648 	if (is_system_labeled()) {
20649 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20650 		if (dst_rhtp == NULL)
20651 			return (NULL);
20652 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20653 			TPC_RELE(dst_rhtp);
20654 			dst_rhtp = NULL;
20655 		}
20656 	}
20657 
20658 	/*
20659 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20660 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20661 	 * After selecting the right ipif, under ill_lock make sure ipif is
20662 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20663 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20664 	 * but not under a lock.
20665 	 */
20666 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20667 
20668 retry:
20669 	till = ill;
20670 	ipif_arr[0] = NULL;
20671 
20672 	if (till->ill_group != NULL)
20673 		till = till->ill_group->illgrp_ill;
20674 
20675 	/*
20676 	 * Choose one good source address from each ill across the group.
20677 	 * If possible choose a source address in the same subnet as
20678 	 * the destination address.
20679 	 *
20680 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20681 	 * This is okay because of the following.
20682 	 *
20683 	 *    If PHYI_FAILED is set and we still have non-deprecated
20684 	 *    addresses, it means the addresses have not yet been
20685 	 *    failed over to a different interface. We potentially
20686 	 *    select them to create IRE_CACHES, which will be later
20687 	 *    flushed when the addresses move over.
20688 	 *
20689 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20690 	 *    addresses, it means either the user has configured them
20691 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20692 	 *    been moved over. For the former, in.mpathd does a failover
20693 	 *    when the interface becomes INACTIVE and hence we should
20694 	 *    not find them. Once INACTIVE is set, we don't allow them
20695 	 *    to create logical interfaces anymore. For the latter, a
20696 	 *    flush will happen when INACTIVE is cleared which will
20697 	 *    flush the IRE_CACHES.
20698 	 *
20699 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20700 	 *    over soon. We potentially select them to create IRE_CACHEs,
20701 	 *    which will be later flushed when the addresses move over.
20702 	 *
20703 	 * NOTE : As ipif_select_source is called to borrow source address
20704 	 * for an ipif that is part of a group, source address selection
20705 	 * will be re-done whenever the group changes i.e either an
20706 	 * insertion/deletion in the group.
20707 	 *
20708 	 * Fill ipif_arr[] with source addresses, using these rules:
20709 	 *
20710 	 *	1. At most one source address from a given ill ends up
20711 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20712 	 *	   associated with a given ill ends up in ipif_arr[].
20713 	 *
20714 	 *	2. If there is at least one non-deprecated ipif in the
20715 	 *	   IPMP group with a source address on the same subnet as
20716 	 *	   our destination, then fill ipif_arr[] only with
20717 	 *	   source addresses on the same subnet as our destination.
20718 	 *	   Note that because of (1), only the first
20719 	 *	   non-deprecated ipif found with a source address
20720 	 *	   matching the destination ends up in ipif_arr[].
20721 	 *
20722 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20723 	 *	   addresses not in the same subnet as our destination.
20724 	 *	   Again, because of (1), only the first off-subnet source
20725 	 *	   address will be chosen.
20726 	 *
20727 	 *	4. If there are no non-deprecated ipifs, then just use
20728 	 *	   the source address associated with the last deprecated
20729 	 *	   one we find that happens to be on the same subnet,
20730 	 *	   otherwise the first one not in the same subnet.
20731 	 */
20732 	specific_found = B_FALSE;
20733 	for (; till != NULL; till = till->ill_group_next) {
20734 		ipif_same_found = B_FALSE;
20735 		ipif_other_found = B_FALSE;
20736 		for (ipif = till->ill_ipif; ipif != NULL;
20737 		    ipif = ipif->ipif_next) {
20738 			if (!IPIF_CAN_LOOKUP(ipif))
20739 				continue;
20740 			/* Always skip NOLOCAL and ANYCAST interfaces */
20741 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20742 				continue;
20743 			if (!(ipif->ipif_flags & IPIF_UP) ||
20744 			    !ipif->ipif_addr_ready)
20745 				continue;
20746 			if (ipif->ipif_zoneid != zoneid &&
20747 			    ipif->ipif_zoneid != ALL_ZONES)
20748 				continue;
20749 			/*
20750 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20751 			 * but are not valid as source addresses.
20752 			 */
20753 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20754 				continue;
20755 
20756 			/*
20757 			 * Check compatibility of local address for
20758 			 * destination's default label if we're on a labeled
20759 			 * system.  Incompatible addresses can't be used at
20760 			 * all.
20761 			 */
20762 			if (dst_rhtp != NULL) {
20763 				boolean_t incompat;
20764 
20765 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20766 				    IPV4_VERSION, B_FALSE);
20767 				if (src_rhtp == NULL)
20768 					continue;
20769 				incompat =
20770 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20771 				    src_rhtp->tpc_tp.tp_doi !=
20772 				    dst_rhtp->tpc_tp.tp_doi ||
20773 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20774 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20775 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20776 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20777 				TPC_RELE(src_rhtp);
20778 				if (incompat)
20779 					continue;
20780 			}
20781 
20782 			/*
20783 			 * We prefer not to use all all-zones addresses, if we
20784 			 * can avoid it, as they pose problems with unlabeled
20785 			 * destinations.
20786 			 */
20787 			if (ipif->ipif_zoneid != ALL_ZONES) {
20788 				if (!specific_found &&
20789 				    (!same_subnet_only ||
20790 				    (ipif->ipif_net_mask & dst) ==
20791 				    ipif->ipif_subnet)) {
20792 					index = 0;
20793 					specific_found = B_TRUE;
20794 					ipif_other_found = B_FALSE;
20795 				}
20796 			} else {
20797 				if (specific_found)
20798 					continue;
20799 			}
20800 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20801 				if (ipif_dep == NULL ||
20802 				    (ipif->ipif_net_mask & dst) ==
20803 				    ipif->ipif_subnet)
20804 					ipif_dep = ipif;
20805 				continue;
20806 			}
20807 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20808 				/* found a source address in the same subnet */
20809 				if (!same_subnet_only) {
20810 					same_subnet_only = B_TRUE;
20811 					index = 0;
20812 				}
20813 				ipif_same_found = B_TRUE;
20814 			} else {
20815 				if (same_subnet_only || ipif_other_found)
20816 					continue;
20817 				ipif_other_found = B_TRUE;
20818 			}
20819 			ipif_arr[index++] = ipif;
20820 			if (index == MAX_IPIF_SELECT_SOURCE) {
20821 				wrapped = B_TRUE;
20822 				index = 0;
20823 			}
20824 			if (ipif_same_found)
20825 				break;
20826 		}
20827 	}
20828 
20829 	if (ipif_arr[0] == NULL) {
20830 		ipif = ipif_dep;
20831 	} else {
20832 		if (wrapped)
20833 			index = MAX_IPIF_SELECT_SOURCE;
20834 		ipif = ipif_arr[ipif_rand(ipst) % index];
20835 		ASSERT(ipif != NULL);
20836 	}
20837 
20838 	if (ipif != NULL) {
20839 		mutex_enter(&ipif->ipif_ill->ill_lock);
20840 		if (!IPIF_CAN_LOOKUP(ipif)) {
20841 			mutex_exit(&ipif->ipif_ill->ill_lock);
20842 			goto retry;
20843 		}
20844 		ipif_refhold_locked(ipif);
20845 		mutex_exit(&ipif->ipif_ill->ill_lock);
20846 	}
20847 
20848 	rw_exit(&ipst->ips_ill_g_lock);
20849 	if (usill != NULL)
20850 		ill_refrele(usill);
20851 	if (dst_rhtp != NULL)
20852 		TPC_RELE(dst_rhtp);
20853 
20854 #ifdef DEBUG
20855 	if (ipif == NULL) {
20856 		char buf1[INET6_ADDRSTRLEN];
20857 
20858 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20859 		    ill->ill_name,
20860 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20861 	} else {
20862 		char buf1[INET6_ADDRSTRLEN];
20863 		char buf2[INET6_ADDRSTRLEN];
20864 
20865 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20866 		    ipif->ipif_ill->ill_name,
20867 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20868 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20869 		    buf2, sizeof (buf2))));
20870 	}
20871 #endif /* DEBUG */
20872 	return (ipif);
20873 }
20874 
20875 
20876 /*
20877  * If old_ipif is not NULL, see if ipif was derived from old
20878  * ipif and if so, recreate the interface route by re-doing
20879  * source address selection. This happens when ipif_down ->
20880  * ipif_update_other_ipifs calls us.
20881  *
20882  * If old_ipif is NULL, just redo the source address selection
20883  * if needed. This happens when illgrp_insert or ipif_up_done
20884  * calls us.
20885  */
20886 static void
20887 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20888 {
20889 	ire_t *ire;
20890 	ire_t *ipif_ire;
20891 	queue_t *stq;
20892 	ipif_t *nipif;
20893 	ill_t *ill;
20894 	boolean_t need_rele = B_FALSE;
20895 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20896 
20897 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20898 	ASSERT(IAM_WRITER_IPIF(ipif));
20899 
20900 	ill = ipif->ipif_ill;
20901 	if (!(ipif->ipif_flags &
20902 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20903 		/*
20904 		 * Can't possibly have borrowed the source
20905 		 * from old_ipif.
20906 		 */
20907 		return;
20908 	}
20909 
20910 	/*
20911 	 * Is there any work to be done? No work if the address
20912 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20913 	 * ipif_select_source() does not borrow addresses from
20914 	 * NOLOCAL and ANYCAST interfaces).
20915 	 */
20916 	if ((old_ipif != NULL) &&
20917 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20918 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20919 	    (old_ipif->ipif_flags &
20920 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20921 		return;
20922 	}
20923 
20924 	/*
20925 	 * Perform the same checks as when creating the
20926 	 * IRE_INTERFACE in ipif_up_done.
20927 	 */
20928 	if (!(ipif->ipif_flags & IPIF_UP))
20929 		return;
20930 
20931 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20932 	    (ipif->ipif_subnet == INADDR_ANY))
20933 		return;
20934 
20935 	ipif_ire = ipif_to_ire(ipif);
20936 	if (ipif_ire == NULL)
20937 		return;
20938 
20939 	/*
20940 	 * We know that ipif uses some other source for its
20941 	 * IRE_INTERFACE. Is it using the source of this
20942 	 * old_ipif?
20943 	 */
20944 	if (old_ipif != NULL &&
20945 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20946 		ire_refrele(ipif_ire);
20947 		return;
20948 	}
20949 	if (ip_debug > 2) {
20950 		/* ip1dbg */
20951 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20952 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20953 	}
20954 
20955 	stq = ipif_ire->ire_stq;
20956 
20957 	/*
20958 	 * Can't use our source address. Select a different
20959 	 * source address for the IRE_INTERFACE.
20960 	 */
20961 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20962 	if (nipif == NULL) {
20963 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20964 		nipif = ipif;
20965 	} else {
20966 		need_rele = B_TRUE;
20967 	}
20968 
20969 	ire = ire_create(
20970 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20971 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20972 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20973 	    NULL,				/* no gateway */
20974 	    &ipif->ipif_mtu,			/* max frag */
20975 	    NULL,				/* no src nce */
20976 	    NULL,				/* no recv from queue */
20977 	    stq,				/* send-to queue */
20978 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20979 	    ipif,
20980 	    0,
20981 	    0,
20982 	    0,
20983 	    0,
20984 	    &ire_uinfo_null,
20985 	    NULL,
20986 	    NULL,
20987 	    ipst);
20988 
20989 	if (ire != NULL) {
20990 		ire_t *ret_ire;
20991 		int error;
20992 
20993 		/*
20994 		 * We don't need ipif_ire anymore. We need to delete
20995 		 * before we add so that ire_add does not detect
20996 		 * duplicates.
20997 		 */
20998 		ire_delete(ipif_ire);
20999 		ret_ire = ire;
21000 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21001 		ASSERT(error == 0);
21002 		ASSERT(ire == ret_ire);
21003 		/* Held in ire_add */
21004 		ire_refrele(ret_ire);
21005 	}
21006 	/*
21007 	 * Either we are falling through from above or could not
21008 	 * allocate a replacement.
21009 	 */
21010 	ire_refrele(ipif_ire);
21011 	if (need_rele)
21012 		ipif_refrele(nipif);
21013 }
21014 
21015 /*
21016  * This old_ipif is going away.
21017  *
21018  * Determine if any other ipif's is using our address as
21019  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21020  * IPIF_DEPRECATED).
21021  * Find the IRE_INTERFACE for such ipifs and recreate them
21022  * to use an different source address following the rules in
21023  * ipif_up_done.
21024  *
21025  * This function takes an illgrp as an argument so that illgrp_delete
21026  * can call this to update source address even after deleting the
21027  * old_ipif->ipif_ill from the ill group.
21028  */
21029 static void
21030 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21031 {
21032 	ipif_t *ipif;
21033 	ill_t *ill;
21034 	char	buf[INET6_ADDRSTRLEN];
21035 
21036 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21037 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21038 
21039 	ill = old_ipif->ipif_ill;
21040 
21041 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21042 	    ill->ill_name,
21043 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21044 	    buf, sizeof (buf))));
21045 	/*
21046 	 * If this part of a group, look at all ills as ipif_select_source
21047 	 * borrows source address across all the ills in the group.
21048 	 */
21049 	if (illgrp != NULL)
21050 		ill = illgrp->illgrp_ill;
21051 
21052 	for (; ill != NULL; ill = ill->ill_group_next) {
21053 		for (ipif = ill->ill_ipif; ipif != NULL;
21054 		    ipif = ipif->ipif_next) {
21055 
21056 			if (ipif == old_ipif)
21057 				continue;
21058 
21059 			ipif_recreate_interface_routes(old_ipif, ipif);
21060 		}
21061 	}
21062 }
21063 
21064 /* ARGSUSED */
21065 int
21066 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21067 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21068 {
21069 	/*
21070 	 * ill_phyint_reinit merged the v4 and v6 into a single
21071 	 * ipsq. Could also have become part of a ipmp group in the
21072 	 * process, and we might not have been able to complete the
21073 	 * operation in ipif_set_values, if we could not become
21074 	 * exclusive.  If so restart it here.
21075 	 */
21076 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21077 }
21078 
21079 /*
21080  * Can operate on either a module or a driver queue.
21081  * Returns an error if not a module queue.
21082  */
21083 /* ARGSUSED */
21084 int
21085 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21086     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21087 {
21088 	queue_t		*q1 = q;
21089 	char 		*cp;
21090 	char		interf_name[LIFNAMSIZ];
21091 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21092 
21093 	if (q->q_next == NULL) {
21094 		ip1dbg((
21095 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21096 		return (EINVAL);
21097 	}
21098 
21099 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21100 		return (EALREADY);
21101 
21102 	do {
21103 		q1 = q1->q_next;
21104 	} while (q1->q_next);
21105 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21106 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21107 
21108 	/*
21109 	 * Here we are not going to delay the ioack until after
21110 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21111 	 * original ioctl message before sending the requests.
21112 	 */
21113 	return (ipif_set_values(q, mp, interf_name, &ppa));
21114 }
21115 
21116 /* ARGSUSED */
21117 int
21118 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21119     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21120 {
21121 	return (ENXIO);
21122 }
21123 
21124 /*
21125  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21126  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21127  * ire_check_and_create_bcast()).
21128  */
21129 static ire_t **
21130 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21131 {
21132 	ipaddr_t addr;
21133 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21134 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21135 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21136 
21137 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21138 
21139 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21140 
21141 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21142 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21143 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21144 
21145 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21146 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21147 
21148 	/*
21149 	 * For backward compatibility, we create net broadcast IREs based on
21150 	 * the old "IP address class system", since some old machines only
21151 	 * respond to these class derived net broadcast.  However, we must not
21152 	 * create these net broadcast IREs if the subnetmask is shorter than
21153 	 * the IP address class based derived netmask.  Otherwise, we may
21154 	 * create a net broadcast address which is the same as an IP address
21155 	 * on the subnet -- and then TCP will refuse to talk to that address.
21156 	 */
21157 	if (netmask < subnetmask) {
21158 		addr = netmask & ipif->ipif_subnet;
21159 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21160 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21161 		    flags);
21162 	}
21163 
21164 	/*
21165 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21166 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21167 	 * created.  Creating these broadcast IREs will only create confusion
21168 	 * as `addr' will be the same as the IP address.
21169 	 */
21170 	if (subnetmask != 0xFFFFFFFF) {
21171 		addr = ipif->ipif_subnet;
21172 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21173 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21174 		    irep, flags);
21175 	}
21176 
21177 	return (irep);
21178 }
21179 
21180 /*
21181  * Broadcast IRE info structure used in the functions below.  Since we
21182  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21183  */
21184 typedef struct bcast_ireinfo {
21185 	uchar_t		bi_type;	/* BCAST_* value from below */
21186 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21187 			bi_needrep:1,	/* do we need to replace it? */
21188 			bi_haverep:1,	/* have we replaced it? */
21189 			bi_pad:5;
21190 	ipaddr_t	bi_addr;	/* IRE address */
21191 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21192 } bcast_ireinfo_t;
21193 
21194 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21195 
21196 /*
21197  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21198  * return B_TRUE if it should immediately be used to recreate the IRE.
21199  */
21200 static boolean_t
21201 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21202 {
21203 	ipaddr_t addr;
21204 
21205 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21206 
21207 	switch (bireinfop->bi_type) {
21208 	case BCAST_NET:
21209 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21210 		if (addr != bireinfop->bi_addr)
21211 			return (B_FALSE);
21212 		break;
21213 	case BCAST_SUBNET:
21214 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21215 			return (B_FALSE);
21216 		break;
21217 	}
21218 
21219 	bireinfop->bi_needrep = 1;
21220 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21221 		if (bireinfop->bi_backup == NULL)
21222 			bireinfop->bi_backup = ipif;
21223 		return (B_FALSE);
21224 	}
21225 	return (B_TRUE);
21226 }
21227 
21228 /*
21229  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21230  * them ala ire_check_and_create_bcast().
21231  */
21232 static ire_t **
21233 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21234 {
21235 	ipaddr_t mask, addr;
21236 
21237 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21238 
21239 	addr = bireinfop->bi_addr;
21240 	irep = ire_create_bcast(ipif, addr, irep);
21241 
21242 	switch (bireinfop->bi_type) {
21243 	case BCAST_NET:
21244 		mask = ip_net_mask(ipif->ipif_subnet);
21245 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21246 		break;
21247 	case BCAST_SUBNET:
21248 		mask = ipif->ipif_net_mask;
21249 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21250 		break;
21251 	}
21252 
21253 	bireinfop->bi_haverep = 1;
21254 	return (irep);
21255 }
21256 
21257 /*
21258  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21259  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21260  * that are going away are still needed.  If so, have ipif_create_bcast()
21261  * recreate them (except for the deprecated case, as explained below).
21262  */
21263 static ire_t **
21264 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21265     ire_t **irep)
21266 {
21267 	int i;
21268 	ipif_t *ipif;
21269 
21270 	ASSERT(!ill->ill_isv6);
21271 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21272 		/*
21273 		 * Skip this ipif if it's (a) the one being taken down, (b)
21274 		 * not in the same zone, or (c) has no valid local address.
21275 		 */
21276 		if (ipif == test_ipif ||
21277 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21278 		    ipif->ipif_subnet == 0 ||
21279 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21280 		    (IPIF_UP|IPIF_BROADCAST))
21281 			continue;
21282 
21283 		/*
21284 		 * For each dying IRE that hasn't yet been replaced, see if
21285 		 * `ipif' needs it and whether the IRE should be recreated on
21286 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21287 		 * will return B_FALSE even if `ipif' needs the IRE on the
21288 		 * hopes that we'll later find a needy non-deprecated ipif.
21289 		 * However, the ipif is recorded in bi_backup for possible
21290 		 * subsequent use by ipif_check_bcast_ires().
21291 		 */
21292 		for (i = 0; i < BCAST_COUNT; i++) {
21293 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21294 				continue;
21295 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21296 				continue;
21297 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21298 		}
21299 
21300 		/*
21301 		 * If we've replaced all of the broadcast IREs that are going
21302 		 * to be taken down, we know we're done.
21303 		 */
21304 		for (i = 0; i < BCAST_COUNT; i++) {
21305 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21306 				break;
21307 		}
21308 		if (i == BCAST_COUNT)
21309 			break;
21310 	}
21311 	return (irep);
21312 }
21313 
21314 /*
21315  * Check if `test_ipif' (which is going away) is associated with any existing
21316  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21317  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21318  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21319  *
21320  * This is necessary because broadcast IREs are shared.  In particular, a
21321  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21322  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21323  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21324  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21325  * same zone, they will share the same set of broadcast IREs.
21326  *
21327  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21328  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21329  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21330  */
21331 static void
21332 ipif_check_bcast_ires(ipif_t *test_ipif)
21333 {
21334 	ill_t		*ill = test_ipif->ipif_ill;
21335 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21336 	ire_t		**irep1, **irep = &ire_array[0];
21337 	uint_t 		i, willdie;
21338 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21339 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21340 
21341 	ASSERT(!test_ipif->ipif_isv6);
21342 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21343 
21344 	/*
21345 	 * No broadcast IREs for the LOOPBACK interface
21346 	 * or others such as point to point and IPIF_NOXMIT.
21347 	 */
21348 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21349 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21350 		return;
21351 
21352 	bzero(bireinfo, sizeof (bireinfo));
21353 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21354 	bireinfo[0].bi_addr = 0;
21355 
21356 	bireinfo[1].bi_type = BCAST_ALLONES;
21357 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21358 
21359 	bireinfo[2].bi_type = BCAST_NET;
21360 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21361 
21362 	if (test_ipif->ipif_net_mask != 0)
21363 		mask = test_ipif->ipif_net_mask;
21364 	bireinfo[3].bi_type = BCAST_SUBNET;
21365 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21366 
21367 	/*
21368 	 * Figure out what (if any) broadcast IREs will die as a result of
21369 	 * `test_ipif' going away.  If none will die, we're done.
21370 	 */
21371 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21372 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21373 		    test_ipif, ALL_ZONES, NULL,
21374 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21375 		if (ire != NULL) {
21376 			willdie++;
21377 			bireinfo[i].bi_willdie = 1;
21378 			ire_refrele(ire);
21379 		}
21380 	}
21381 
21382 	if (willdie == 0)
21383 		return;
21384 
21385 	/*
21386 	 * Walk through all the ipifs that will be affected by the dying IREs,
21387 	 * and recreate the IREs as necessary.
21388 	 */
21389 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21390 
21391 	/*
21392 	 * Scan through the set of broadcast IREs and see if there are any
21393 	 * that we need to replace that have not yet been replaced.  If so,
21394 	 * replace them using the appropriate backup ipif.
21395 	 */
21396 	for (i = 0; i < BCAST_COUNT; i++) {
21397 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21398 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21399 			    &bireinfo[i], irep);
21400 	}
21401 
21402 	/*
21403 	 * If we can't create all of them, don't add any of them.  (Code in
21404 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21405 	 * non-loopback copy and loopback copy for a given address.)
21406 	 */
21407 	for (irep1 = irep; irep1 > ire_array; ) {
21408 		irep1--;
21409 		if (*irep1 == NULL) {
21410 			ip0dbg(("ipif_check_bcast_ires: can't create "
21411 			    "IRE_BROADCAST, memory allocation failure\n"));
21412 			while (irep > ire_array) {
21413 				irep--;
21414 				if (*irep != NULL)
21415 					ire_delete(*irep);
21416 			}
21417 			return;
21418 		}
21419 	}
21420 
21421 	for (irep1 = irep; irep1 > ire_array; ) {
21422 		irep1--;
21423 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21424 			ire_refrele(*irep1);		/* Held in ire_add */
21425 	}
21426 }
21427 
21428 /*
21429  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21430  * from lifr_flags and the name from lifr_name.
21431  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21432  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21433  * Returns EINPROGRESS when mp has been consumed by queueing it on
21434  * ill_pending_mp and the ioctl will complete in ip_rput.
21435  *
21436  * Can operate on either a module or a driver queue.
21437  * Returns an error if not a module queue.
21438  */
21439 /* ARGSUSED */
21440 int
21441 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21442     ip_ioctl_cmd_t *ipip, void *if_req)
21443 {
21444 	ill_t	*ill = q->q_ptr;
21445 	phyint_t *phyi;
21446 	ip_stack_t *ipst;
21447 	struct lifreq *lifr = if_req;
21448 
21449 	ASSERT(ipif != NULL);
21450 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21451 
21452 	if (q->q_next == NULL) {
21453 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21454 		return (EINVAL);
21455 	}
21456 
21457 	/*
21458 	 * If we are not writer on 'q' then this interface exists already
21459 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21460 	 * so return EALREADY.
21461 	 */
21462 	if (ill != ipif->ipif_ill)
21463 		return (EALREADY);
21464 
21465 	if (ill->ill_name[0] != '\0')
21466 		return (EALREADY);
21467 
21468 	/*
21469 	 * Set all the flags. Allows all kinds of override. Provide some
21470 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21471 	 * unless there is either multicast/broadcast support in the driver
21472 	 * or it is a pt-pt link.
21473 	 */
21474 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21475 		/* Meaningless to IP thus don't allow them to be set. */
21476 		ip1dbg(("ip_setname: EINVAL 1\n"));
21477 		return (EINVAL);
21478 	}
21479 
21480 	/*
21481 	 * If there's another ill already with the requested name, ensure
21482 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21483 	 * fuse together two unrelated ills, which will cause chaos.
21484 	 */
21485 	ipst = ill->ill_ipst;
21486 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21487 	    lifr->lifr_name, NULL);
21488 	if (phyi != NULL) {
21489 		ill_t *ill_mate = phyi->phyint_illv4;
21490 
21491 		if (ill_mate == NULL)
21492 			ill_mate = phyi->phyint_illv6;
21493 		ASSERT(ill_mate != NULL);
21494 
21495 		if (ill_mate->ill_media->ip_m_mac_type !=
21496 		    ill->ill_media->ip_m_mac_type) {
21497 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21498 			    "use the same ill name on differing media\n"));
21499 			return (EINVAL);
21500 		}
21501 	}
21502 
21503 	/*
21504 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21505 	 * ill_bcast_addr_length info.
21506 	 */
21507 	if (!ill->ill_needs_attach &&
21508 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21509 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21510 	    ill->ill_bcast_addr_length == 0)) {
21511 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21512 		ip1dbg(("ip_setname: EINVAL 2\n"));
21513 		return (EINVAL);
21514 	}
21515 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21516 	    ((lifr->lifr_flags & IFF_IPV6) ||
21517 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21518 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21519 		ip1dbg(("ip_setname: EINVAL 3\n"));
21520 		return (EINVAL);
21521 	}
21522 	if (lifr->lifr_flags & IFF_UP) {
21523 		/* Can only be set with SIOCSLIFFLAGS */
21524 		ip1dbg(("ip_setname: EINVAL 4\n"));
21525 		return (EINVAL);
21526 	}
21527 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21528 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21529 		ip1dbg(("ip_setname: EINVAL 5\n"));
21530 		return (EINVAL);
21531 	}
21532 	/*
21533 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21534 	 */
21535 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21536 	    !(lifr->lifr_flags & IFF_IPV6) &&
21537 	    !(ipif->ipif_isv6)) {
21538 		ip1dbg(("ip_setname: EINVAL 6\n"));
21539 		return (EINVAL);
21540 	}
21541 
21542 	/*
21543 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21544 	 * we have all the flags here. So, we assign rather than we OR.
21545 	 * We can't OR the flags here because we don't want to set
21546 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21547 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21548 	 * on lifr_flags value here.
21549 	 */
21550 	/*
21551 	 * This ill has not been inserted into the global list.
21552 	 * So we are still single threaded and don't need any lock
21553 	 */
21554 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21555 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21556 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21557 
21558 	/* We started off as V4. */
21559 	if (ill->ill_flags & ILLF_IPV6) {
21560 		ill->ill_phyint->phyint_illv6 = ill;
21561 		ill->ill_phyint->phyint_illv4 = NULL;
21562 	}
21563 
21564 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21565 }
21566 
21567 /* ARGSUSED */
21568 int
21569 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21570     ip_ioctl_cmd_t *ipip, void *if_req)
21571 {
21572 	/*
21573 	 * ill_phyint_reinit merged the v4 and v6 into a single
21574 	 * ipsq. Could also have become part of a ipmp group in the
21575 	 * process, and we might not have been able to complete the
21576 	 * slifname in ipif_set_values, if we could not become
21577 	 * exclusive.  If so restart it here
21578 	 */
21579 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21580 }
21581 
21582 /*
21583  * Return a pointer to the ipif which matches the index, IP version type and
21584  * zoneid.
21585  */
21586 ipif_t *
21587 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21588     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21589 {
21590 	ill_t	*ill;
21591 	ipif_t	*ipif = NULL;
21592 
21593 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21594 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21595 
21596 	if (err != NULL)
21597 		*err = 0;
21598 
21599 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21600 	if (ill != NULL) {
21601 		mutex_enter(&ill->ill_lock);
21602 		for (ipif = ill->ill_ipif; ipif != NULL;
21603 		    ipif = ipif->ipif_next) {
21604 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21605 			    zoneid == ipif->ipif_zoneid ||
21606 			    ipif->ipif_zoneid == ALL_ZONES)) {
21607 				ipif_refhold_locked(ipif);
21608 				break;
21609 			}
21610 		}
21611 		mutex_exit(&ill->ill_lock);
21612 		ill_refrele(ill);
21613 		if (ipif == NULL && err != NULL)
21614 			*err = ENXIO;
21615 	}
21616 	return (ipif);
21617 }
21618 
21619 typedef struct conn_change_s {
21620 	uint_t cc_old_ifindex;
21621 	uint_t cc_new_ifindex;
21622 } conn_change_t;
21623 
21624 /*
21625  * ipcl_walk function for changing interface index.
21626  */
21627 static void
21628 conn_change_ifindex(conn_t *connp, caddr_t arg)
21629 {
21630 	conn_change_t *connc;
21631 	uint_t old_ifindex;
21632 	uint_t new_ifindex;
21633 	int i;
21634 	ilg_t *ilg;
21635 
21636 	connc = (conn_change_t *)arg;
21637 	old_ifindex = connc->cc_old_ifindex;
21638 	new_ifindex = connc->cc_new_ifindex;
21639 
21640 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21641 		connp->conn_orig_bound_ifindex = new_ifindex;
21642 
21643 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21644 		connp->conn_orig_multicast_ifindex = new_ifindex;
21645 
21646 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21647 		ilg = &connp->conn_ilg[i];
21648 		if (ilg->ilg_orig_ifindex == old_ifindex)
21649 			ilg->ilg_orig_ifindex = new_ifindex;
21650 	}
21651 }
21652 
21653 /*
21654  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21655  * to new_index if it matches the old_index.
21656  *
21657  * Failovers typically happen within a group of ills. But somebody
21658  * can remove an ill from the group after a failover happened. If
21659  * we are setting the ifindex after this, we potentially need to
21660  * look at all the ills rather than just the ones in the group.
21661  * We cut down the work by looking at matching ill_net_types
21662  * and ill_types as we could not possibly grouped them together.
21663  */
21664 static void
21665 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21666 {
21667 	ill_t *ill;
21668 	ipif_t *ipif;
21669 	uint_t old_ifindex;
21670 	uint_t new_ifindex;
21671 	ilm_t *ilm;
21672 	ill_walk_context_t ctx;
21673 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21674 
21675 	old_ifindex = connc->cc_old_ifindex;
21676 	new_ifindex = connc->cc_new_ifindex;
21677 
21678 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21679 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21680 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21681 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21682 		    (ill_orig->ill_type != ill->ill_type)) {
21683 			continue;
21684 		}
21685 		for (ipif = ill->ill_ipif; ipif != NULL;
21686 		    ipif = ipif->ipif_next) {
21687 			if (ipif->ipif_orig_ifindex == old_ifindex)
21688 				ipif->ipif_orig_ifindex = new_ifindex;
21689 		}
21690 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21691 			if (ilm->ilm_orig_ifindex == old_ifindex)
21692 				ilm->ilm_orig_ifindex = new_ifindex;
21693 		}
21694 	}
21695 	rw_exit(&ipst->ips_ill_g_lock);
21696 }
21697 
21698 /*
21699  * We first need to ensure that the new index is unique, and
21700  * then carry the change across both v4 and v6 ill representation
21701  * of the physical interface.
21702  */
21703 /* ARGSUSED */
21704 int
21705 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21706     ip_ioctl_cmd_t *ipip, void *ifreq)
21707 {
21708 	ill_t		*ill;
21709 	ill_t		*ill_other;
21710 	phyint_t	*phyi;
21711 	int		old_index;
21712 	conn_change_t	connc;
21713 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21714 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21715 	uint_t	index;
21716 	ill_t	*ill_v4;
21717 	ill_t	*ill_v6;
21718 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21719 
21720 	if (ipip->ipi_cmd_type == IF_CMD)
21721 		index = ifr->ifr_index;
21722 	else
21723 		index = lifr->lifr_index;
21724 
21725 	/*
21726 	 * Only allow on physical interface. Also, index zero is illegal.
21727 	 *
21728 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21729 	 *
21730 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21731 	 *    implies a possible failback might have to happen. As failback
21732 	 *    depends on the old index, we should fail setting the index.
21733 	 *
21734 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21735 	 *    any addresses or multicast memberships are failed over to
21736 	 *    a non-STANDBY interface. As failback depends on the old
21737 	 *    index, we should fail setting the index for this case also.
21738 	 *
21739 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21740 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21741 	 */
21742 	ill = ipif->ipif_ill;
21743 	phyi = ill->ill_phyint;
21744 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21745 	    ipif->ipif_id != 0 || index == 0) {
21746 		return (EINVAL);
21747 	}
21748 	old_index = phyi->phyint_ifindex;
21749 
21750 	/* If the index is not changing, no work to do */
21751 	if (old_index == index)
21752 		return (0);
21753 
21754 	/*
21755 	 * Use ill_lookup_on_ifindex to determine if the
21756 	 * new index is unused and if so allow the change.
21757 	 */
21758 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21759 	    ipst);
21760 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21761 	    ipst);
21762 	if (ill_v6 != NULL || ill_v4 != NULL) {
21763 		if (ill_v4 != NULL)
21764 			ill_refrele(ill_v4);
21765 		if (ill_v6 != NULL)
21766 			ill_refrele(ill_v6);
21767 		return (EBUSY);
21768 	}
21769 
21770 	/*
21771 	 * The new index is unused. Set it in the phyint.
21772 	 * Locate the other ill so that we can send a routing
21773 	 * sockets message.
21774 	 */
21775 	if (ill->ill_isv6) {
21776 		ill_other = phyi->phyint_illv4;
21777 	} else {
21778 		ill_other = phyi->phyint_illv6;
21779 	}
21780 
21781 	phyi->phyint_ifindex = index;
21782 
21783 	/* Update SCTP's ILL list */
21784 	sctp_ill_reindex(ill, old_index);
21785 
21786 	connc.cc_old_ifindex = old_index;
21787 	connc.cc_new_ifindex = index;
21788 	ip_change_ifindex(ill, &connc);
21789 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21790 
21791 	/* Send the routing sockets message */
21792 	ip_rts_ifmsg(ipif);
21793 	if (ill_other != NULL)
21794 		ip_rts_ifmsg(ill_other->ill_ipif);
21795 
21796 	return (0);
21797 }
21798 
21799 /* ARGSUSED */
21800 int
21801 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21802     ip_ioctl_cmd_t *ipip, void *ifreq)
21803 {
21804 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21805 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21806 
21807 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21808 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21809 	/* Get the interface index */
21810 	if (ipip->ipi_cmd_type == IF_CMD) {
21811 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21812 	} else {
21813 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21814 	}
21815 	return (0);
21816 }
21817 
21818 /* ARGSUSED */
21819 int
21820 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21821     ip_ioctl_cmd_t *ipip, void *ifreq)
21822 {
21823 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21824 
21825 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21826 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21827 	/* Get the interface zone */
21828 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21829 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21830 	return (0);
21831 }
21832 
21833 /*
21834  * Set the zoneid of an interface.
21835  */
21836 /* ARGSUSED */
21837 int
21838 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21839     ip_ioctl_cmd_t *ipip, void *ifreq)
21840 {
21841 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21842 	int err = 0;
21843 	boolean_t need_up = B_FALSE;
21844 	zone_t *zptr;
21845 	zone_status_t status;
21846 	zoneid_t zoneid;
21847 
21848 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21849 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21850 		if (!is_system_labeled())
21851 			return (ENOTSUP);
21852 		zoneid = GLOBAL_ZONEID;
21853 	}
21854 
21855 	/* cannot assign instance zero to a non-global zone */
21856 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21857 		return (ENOTSUP);
21858 
21859 	/*
21860 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21861 	 * the event of a race with the zone shutdown processing, since IP
21862 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21863 	 * interface will be cleaned up even if the zone is shut down
21864 	 * immediately after the status check. If the interface can't be brought
21865 	 * down right away, and the zone is shut down before the restart
21866 	 * function is called, we resolve the possible races by rechecking the
21867 	 * zone status in the restart function.
21868 	 */
21869 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21870 		return (EINVAL);
21871 	status = zone_status_get(zptr);
21872 	zone_rele(zptr);
21873 
21874 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21875 		return (EINVAL);
21876 
21877 	if (ipif->ipif_flags & IPIF_UP) {
21878 		/*
21879 		 * If the interface is already marked up,
21880 		 * we call ipif_down which will take care
21881 		 * of ditching any IREs that have been set
21882 		 * up based on the old interface address.
21883 		 */
21884 		err = ipif_logical_down(ipif, q, mp);
21885 		if (err == EINPROGRESS)
21886 			return (err);
21887 		ipif_down_tail(ipif);
21888 		need_up = B_TRUE;
21889 	}
21890 
21891 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21892 	return (err);
21893 }
21894 
21895 static int
21896 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21897     queue_t *q, mblk_t *mp, boolean_t need_up)
21898 {
21899 	int	err = 0;
21900 	ip_stack_t	*ipst;
21901 
21902 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21903 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21904 
21905 	if (CONN_Q(q))
21906 		ipst = CONNQ_TO_IPST(q);
21907 	else
21908 		ipst = ILLQ_TO_IPST(q);
21909 
21910 	/*
21911 	 * For exclusive stacks we don't allow a different zoneid than
21912 	 * global.
21913 	 */
21914 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
21915 	    zoneid != GLOBAL_ZONEID)
21916 		return (EINVAL);
21917 
21918 	/* Set the new zone id. */
21919 	ipif->ipif_zoneid = zoneid;
21920 
21921 	/* Update sctp list */
21922 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21923 
21924 	if (need_up) {
21925 		/*
21926 		 * Now bring the interface back up.  If this
21927 		 * is the only IPIF for the ILL, ipif_up
21928 		 * will have to re-bind to the device, so
21929 		 * we may get back EINPROGRESS, in which
21930 		 * case, this IOCTL will get completed in
21931 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21932 		 */
21933 		err = ipif_up(ipif, q, mp);
21934 	}
21935 	return (err);
21936 }
21937 
21938 /* ARGSUSED */
21939 int
21940 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21941     ip_ioctl_cmd_t *ipip, void *if_req)
21942 {
21943 	struct lifreq *lifr = (struct lifreq *)if_req;
21944 	zoneid_t zoneid;
21945 	zone_t *zptr;
21946 	zone_status_t status;
21947 
21948 	ASSERT(ipif->ipif_id != 0);
21949 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21950 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21951 		zoneid = GLOBAL_ZONEID;
21952 
21953 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21954 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21955 
21956 	/*
21957 	 * We recheck the zone status to resolve the following race condition:
21958 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21959 	 * 2) hme0:1 is up and can't be brought down right away;
21960 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21961 	 * 3) zone "myzone" is halted; the zone status switches to
21962 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21963 	 * the interfaces to remove - hme0:1 is not returned because it's not
21964 	 * yet in "myzone", so it won't be removed;
21965 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21966 	 * status check here, we would have hme0:1 in "myzone" after it's been
21967 	 * destroyed.
21968 	 * Note that if the status check fails, we need to bring the interface
21969 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21970 	 * ipif_up_done[_v6]().
21971 	 */
21972 	status = ZONE_IS_UNINITIALIZED;
21973 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21974 		status = zone_status_get(zptr);
21975 		zone_rele(zptr);
21976 	}
21977 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21978 		if (ipif->ipif_isv6) {
21979 			(void) ipif_up_done_v6(ipif);
21980 		} else {
21981 			(void) ipif_up_done(ipif);
21982 		}
21983 		return (EINVAL);
21984 	}
21985 
21986 	ipif_down_tail(ipif);
21987 
21988 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21989 	    B_TRUE));
21990 }
21991 
21992 /* ARGSUSED */
21993 int
21994 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21995 	ip_ioctl_cmd_t *ipip, void *ifreq)
21996 {
21997 	struct lifreq	*lifr = ifreq;
21998 
21999 	ASSERT(q->q_next == NULL);
22000 	ASSERT(CONN_Q(q));
22001 
22002 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22003 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22004 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22005 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22006 
22007 	return (0);
22008 }
22009 
22010 /* Find the previous ILL in this usesrc group */
22011 static ill_t *
22012 ill_prev_usesrc(ill_t *uill)
22013 {
22014 	ill_t *ill;
22015 
22016 	for (ill = uill->ill_usesrc_grp_next;
22017 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22018 	    ill = ill->ill_usesrc_grp_next)
22019 		/* do nothing */;
22020 	return (ill);
22021 }
22022 
22023 /*
22024  * Release all members of the usesrc group. This routine is called
22025  * from ill_delete when the interface being unplumbed is the
22026  * group head.
22027  */
22028 static void
22029 ill_disband_usesrc_group(ill_t *uill)
22030 {
22031 	ill_t *next_ill, *tmp_ill;
22032 	ip_stack_t	*ipst = uill->ill_ipst;
22033 
22034 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22035 	next_ill = uill->ill_usesrc_grp_next;
22036 
22037 	do {
22038 		ASSERT(next_ill != NULL);
22039 		tmp_ill = next_ill->ill_usesrc_grp_next;
22040 		ASSERT(tmp_ill != NULL);
22041 		next_ill->ill_usesrc_grp_next = NULL;
22042 		next_ill->ill_usesrc_ifindex = 0;
22043 		next_ill = tmp_ill;
22044 	} while (next_ill->ill_usesrc_ifindex != 0);
22045 	uill->ill_usesrc_grp_next = NULL;
22046 }
22047 
22048 /*
22049  * Remove the client usesrc ILL from the list and relink to a new list
22050  */
22051 int
22052 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22053 {
22054 	ill_t *ill, *tmp_ill;
22055 	ip_stack_t	*ipst = ucill->ill_ipst;
22056 
22057 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22058 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22059 
22060 	/*
22061 	 * Check if the usesrc client ILL passed in is not already
22062 	 * in use as a usesrc ILL i.e one whose source address is
22063 	 * in use OR a usesrc ILL is not already in use as a usesrc
22064 	 * client ILL
22065 	 */
22066 	if ((ucill->ill_usesrc_ifindex == 0) ||
22067 	    (uill->ill_usesrc_ifindex != 0)) {
22068 		return (-1);
22069 	}
22070 
22071 	ill = ill_prev_usesrc(ucill);
22072 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22073 
22074 	/* Remove from the current list */
22075 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22076 		/* Only two elements in the list */
22077 		ASSERT(ill->ill_usesrc_ifindex == 0);
22078 		ill->ill_usesrc_grp_next = NULL;
22079 	} else {
22080 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22081 	}
22082 
22083 	if (ifindex == 0) {
22084 		ucill->ill_usesrc_ifindex = 0;
22085 		ucill->ill_usesrc_grp_next = NULL;
22086 		return (0);
22087 	}
22088 
22089 	ucill->ill_usesrc_ifindex = ifindex;
22090 	tmp_ill = uill->ill_usesrc_grp_next;
22091 	uill->ill_usesrc_grp_next = ucill;
22092 	ucill->ill_usesrc_grp_next =
22093 	    (tmp_ill != NULL) ? tmp_ill : uill;
22094 	return (0);
22095 }
22096 
22097 /*
22098  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22099  * ip.c for locking details.
22100  */
22101 /* ARGSUSED */
22102 int
22103 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22104     ip_ioctl_cmd_t *ipip, void *ifreq)
22105 {
22106 	struct lifreq *lifr = (struct lifreq *)ifreq;
22107 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22108 	    ill_flag_changed = B_FALSE;
22109 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22110 	int err = 0, ret;
22111 	uint_t ifindex;
22112 	phyint_t *us_phyint, *us_cli_phyint;
22113 	ipsq_t *ipsq = NULL;
22114 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22115 
22116 	ASSERT(IAM_WRITER_IPIF(ipif));
22117 	ASSERT(q->q_next == NULL);
22118 	ASSERT(CONN_Q(q));
22119 
22120 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22121 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22122 
22123 	ASSERT(us_cli_phyint != NULL);
22124 
22125 	/*
22126 	 * If the client ILL is being used for IPMP, abort.
22127 	 * Note, this can be done before ipsq_try_enter since we are already
22128 	 * exclusive on this ILL
22129 	 */
22130 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22131 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22132 		return (EINVAL);
22133 	}
22134 
22135 	ifindex = lifr->lifr_index;
22136 	if (ifindex == 0) {
22137 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22138 			/* non usesrc group interface, nothing to reset */
22139 			return (0);
22140 		}
22141 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22142 		/* valid reset request */
22143 		reset_flg = B_TRUE;
22144 	}
22145 
22146 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22147 	    ip_process_ioctl, &err, ipst);
22148 
22149 	if (usesrc_ill == NULL) {
22150 		return (err);
22151 	}
22152 
22153 	/*
22154 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22155 	 * group nor can either of the interfaces be used for standy. So
22156 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22157 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22158 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22159 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22160 	 * the usesrc_cli_ill
22161 	 */
22162 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22163 	    NEW_OP, B_TRUE);
22164 	if (ipsq == NULL) {
22165 		err = EINPROGRESS;
22166 		/* Operation enqueued on the ipsq of the usesrc ILL */
22167 		goto done;
22168 	}
22169 
22170 	/* Check if the usesrc_ill is used for IPMP */
22171 	us_phyint = usesrc_ill->ill_phyint;
22172 	if ((us_phyint->phyint_groupname != NULL) ||
22173 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22174 		err = EINVAL;
22175 		goto done;
22176 	}
22177 
22178 	/*
22179 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22180 	 * already a client then return EINVAL
22181 	 */
22182 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22183 		err = EINVAL;
22184 		goto done;
22185 	}
22186 
22187 	/*
22188 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22189 	 * be then this is a duplicate operation.
22190 	 */
22191 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22192 		err = 0;
22193 		goto done;
22194 	}
22195 
22196 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22197 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22198 	    usesrc_ill->ill_isv6));
22199 
22200 	/*
22201 	 * The next step ensures that no new ires will be created referencing
22202 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22203 	 * we go through an ire walk deleting all ire caches that reference
22204 	 * the client ill. New ires referencing the client ill that are added
22205 	 * to the ire table before the ILL_CHANGING flag is set, will be
22206 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22207 	 * the client ill while the ILL_CHANGING flag is set will be failed
22208 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22209 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22210 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22211 	 * belong to the same usesrc group.
22212 	 */
22213 	mutex_enter(&usesrc_cli_ill->ill_lock);
22214 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22215 	mutex_exit(&usesrc_cli_ill->ill_lock);
22216 	ill_flag_changed = B_TRUE;
22217 
22218 	if (ipif->ipif_isv6)
22219 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22220 		    ALL_ZONES, ipst);
22221 	else
22222 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22223 		    ALL_ZONES, ipst);
22224 
22225 	/*
22226 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22227 	 * and the ill_usesrc_ifindex fields
22228 	 */
22229 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22230 
22231 	if (reset_flg) {
22232 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22233 		if (ret != 0) {
22234 			err = EINVAL;
22235 		}
22236 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22237 		goto done;
22238 	}
22239 
22240 	/*
22241 	 * Four possibilities to consider:
22242 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22243 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22244 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22245 	 * 4. Both are part of their respective usesrc groups
22246 	 */
22247 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22248 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22249 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22250 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22251 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22252 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22253 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22254 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22255 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22256 		/* Insert at head of list */
22257 		usesrc_cli_ill->ill_usesrc_grp_next =
22258 		    usesrc_ill->ill_usesrc_grp_next;
22259 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22260 	} else {
22261 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22262 		    ifindex);
22263 		if (ret != 0)
22264 			err = EINVAL;
22265 	}
22266 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22267 
22268 done:
22269 	if (ill_flag_changed) {
22270 		mutex_enter(&usesrc_cli_ill->ill_lock);
22271 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22272 		mutex_exit(&usesrc_cli_ill->ill_lock);
22273 	}
22274 	if (ipsq != NULL)
22275 		ipsq_exit(ipsq);
22276 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22277 	ill_refrele(usesrc_ill);
22278 	return (err);
22279 }
22280 
22281 /*
22282  * comparison function used by avl.
22283  */
22284 static int
22285 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22286 {
22287 
22288 	uint_t index;
22289 
22290 	ASSERT(phyip != NULL && index_ptr != NULL);
22291 
22292 	index = *((uint_t *)index_ptr);
22293 	/*
22294 	 * let the phyint with the lowest index be on top.
22295 	 */
22296 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22297 		return (1);
22298 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22299 		return (-1);
22300 	return (0);
22301 }
22302 
22303 /*
22304  * comparison function used by avl.
22305  */
22306 static int
22307 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22308 {
22309 	ill_t *ill;
22310 	int res = 0;
22311 
22312 	ASSERT(phyip != NULL && name_ptr != NULL);
22313 
22314 	if (((phyint_t *)phyip)->phyint_illv4)
22315 		ill = ((phyint_t *)phyip)->phyint_illv4;
22316 	else
22317 		ill = ((phyint_t *)phyip)->phyint_illv6;
22318 	ASSERT(ill != NULL);
22319 
22320 	res = strcmp(ill->ill_name, (char *)name_ptr);
22321 	if (res > 0)
22322 		return (1);
22323 	else if (res < 0)
22324 		return (-1);
22325 	return (0);
22326 }
22327 /*
22328  * This function is called from ill_delete when the ill is being
22329  * unplumbed. We remove the reference from the phyint and we also
22330  * free the phyint when there are no more references to it.
22331  */
22332 static void
22333 ill_phyint_free(ill_t *ill)
22334 {
22335 	phyint_t *phyi;
22336 	phyint_t *next_phyint;
22337 	ipsq_t *cur_ipsq;
22338 	ip_stack_t	*ipst = ill->ill_ipst;
22339 
22340 	ASSERT(ill->ill_phyint != NULL);
22341 
22342 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22343 	phyi = ill->ill_phyint;
22344 	ill->ill_phyint = NULL;
22345 	/*
22346 	 * ill_init allocates a phyint always to store the copy
22347 	 * of flags relevant to phyint. At that point in time, we could
22348 	 * not assign the name and hence phyint_illv4/v6 could not be
22349 	 * initialized. Later in ipif_set_values, we assign the name to
22350 	 * the ill, at which point in time we assign phyint_illv4/v6.
22351 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22352 	 */
22353 	if (ill->ill_flags & ILLF_IPV6) {
22354 		phyi->phyint_illv6 = NULL;
22355 	} else {
22356 		phyi->phyint_illv4 = NULL;
22357 	}
22358 	/*
22359 	 * ipif_down removes it from the group when the last ipif goes
22360 	 * down.
22361 	 */
22362 	ASSERT(ill->ill_group == NULL);
22363 
22364 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22365 		return;
22366 
22367 	/*
22368 	 * Make sure this phyint was put in the list.
22369 	 */
22370 	if (phyi->phyint_ifindex > 0) {
22371 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22372 		    phyi);
22373 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22374 		    phyi);
22375 	}
22376 	/*
22377 	 * remove phyint from the ipsq list.
22378 	 */
22379 	cur_ipsq = phyi->phyint_ipsq;
22380 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22381 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22382 	} else {
22383 		next_phyint = cur_ipsq->ipsq_phyint_list;
22384 		while (next_phyint != NULL) {
22385 			if (next_phyint->phyint_ipsq_next == phyi) {
22386 				next_phyint->phyint_ipsq_next =
22387 				    phyi->phyint_ipsq_next;
22388 				break;
22389 			}
22390 			next_phyint = next_phyint->phyint_ipsq_next;
22391 		}
22392 		ASSERT(next_phyint != NULL);
22393 	}
22394 	IPSQ_DEC_REF(cur_ipsq, ipst);
22395 
22396 	if (phyi->phyint_groupname_len != 0) {
22397 		ASSERT(phyi->phyint_groupname != NULL);
22398 		mi_free(phyi->phyint_groupname);
22399 	}
22400 	mi_free(phyi);
22401 }
22402 
22403 /*
22404  * Attach the ill to the phyint structure which can be shared by both
22405  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22406  * function is called from ipif_set_values and ill_lookup_on_name (for
22407  * loopback) where we know the name of the ill. We lookup the ill and if
22408  * there is one present already with the name use that phyint. Otherwise
22409  * reuse the one allocated by ill_init.
22410  */
22411 static void
22412 ill_phyint_reinit(ill_t *ill)
22413 {
22414 	boolean_t isv6 = ill->ill_isv6;
22415 	phyint_t *phyi_old;
22416 	phyint_t *phyi;
22417 	avl_index_t where = 0;
22418 	ill_t	*ill_other = NULL;
22419 	ipsq_t	*ipsq;
22420 	ip_stack_t	*ipst = ill->ill_ipst;
22421 
22422 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22423 
22424 	phyi_old = ill->ill_phyint;
22425 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22426 	    phyi_old->phyint_illv6 == NULL));
22427 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22428 	    phyi_old->phyint_illv4 == NULL));
22429 	ASSERT(phyi_old->phyint_ifindex == 0);
22430 
22431 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22432 	    ill->ill_name, &where);
22433 
22434 	/*
22435 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22436 	 *    the global list of ills. So no other thread could have located
22437 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22438 	 * 2. Now locate the other protocol instance of this ill.
22439 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22440 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22441 	 *    of neither ill can change.
22442 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22443 	 *    other ill.
22444 	 * 5. Release all locks.
22445 	 */
22446 
22447 	/*
22448 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22449 	 * we are initializing IPv4.
22450 	 */
22451 	if (phyi != NULL) {
22452 		ill_other = (isv6) ? phyi->phyint_illv4 :
22453 		    phyi->phyint_illv6;
22454 		ASSERT(ill_other->ill_phyint != NULL);
22455 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22456 		    (!isv6 && ill_other->ill_isv6));
22457 		GRAB_ILL_LOCKS(ill, ill_other);
22458 		/*
22459 		 * We are potentially throwing away phyint_flags which
22460 		 * could be different from the one that we obtain from
22461 		 * ill_other->ill_phyint. But it is okay as we are assuming
22462 		 * that the state maintained within IP is correct.
22463 		 */
22464 		mutex_enter(&phyi->phyint_lock);
22465 		if (isv6) {
22466 			ASSERT(phyi->phyint_illv6 == NULL);
22467 			phyi->phyint_illv6 = ill;
22468 		} else {
22469 			ASSERT(phyi->phyint_illv4 == NULL);
22470 			phyi->phyint_illv4 = ill;
22471 		}
22472 		/*
22473 		 * This is a new ill, currently undergoing SLIFNAME
22474 		 * So we could not have joined an IPMP group until now.
22475 		 */
22476 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22477 		    phyi_old->phyint_groupname == NULL);
22478 
22479 		/*
22480 		 * This phyi_old is going away. Decref ipsq_refs and
22481 		 * assert it is zero. The ipsq itself will be freed in
22482 		 * ipsq_exit
22483 		 */
22484 		ipsq = phyi_old->phyint_ipsq;
22485 		IPSQ_DEC_REF(ipsq, ipst);
22486 		ASSERT(ipsq->ipsq_refs == 0);
22487 		/* Get the singleton phyint out of the ipsq list */
22488 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22489 		ipsq->ipsq_phyint_list = NULL;
22490 		phyi_old->phyint_illv4 = NULL;
22491 		phyi_old->phyint_illv6 = NULL;
22492 		mi_free(phyi_old);
22493 	} else {
22494 		mutex_enter(&ill->ill_lock);
22495 		/*
22496 		 * We don't need to acquire any lock, since
22497 		 * the ill is not yet visible globally  and we
22498 		 * have not yet released the ill_g_lock.
22499 		 */
22500 		phyi = phyi_old;
22501 		mutex_enter(&phyi->phyint_lock);
22502 		/* XXX We need a recovery strategy here. */
22503 		if (!phyint_assign_ifindex(phyi, ipst))
22504 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22505 
22506 		/* No IPMP group yet, thus the hook uses the ifindex */
22507 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22508 
22509 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22510 		    (void *)phyi, where);
22511 
22512 		(void) avl_find(&ipst->ips_phyint_g_list->
22513 		    phyint_list_avl_by_index,
22514 		    &phyi->phyint_ifindex, &where);
22515 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22516 		    (void *)phyi, where);
22517 	}
22518 
22519 	/*
22520 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22521 	 * pending mp is not affected because that is per ill basis.
22522 	 */
22523 	ill->ill_phyint = phyi;
22524 
22525 	/*
22526 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22527 	 * We do this here as when the first ipif was allocated,
22528 	 * ipif_allocate does not know the right interface index.
22529 	 */
22530 
22531 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22532 	/*
22533 	 * Now that the phyint's ifindex has been assigned, complete the
22534 	 * remaining
22535 	 */
22536 
22537 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22538 	if (ill->ill_isv6) {
22539 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22540 		    ill->ill_phyint->phyint_ifindex;
22541 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22542 	} else {
22543 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22544 	}
22545 
22546 	/*
22547 	 * Generate an event within the hooks framework to indicate that
22548 	 * a new interface has just been added to IP.  For this event to
22549 	 * be generated, the network interface must, at least, have an
22550 	 * ifindex assigned to it.
22551 	 *
22552 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22553 	 * that the ordering of delivered events to listeners matches the
22554 	 * order of them in the kernel.
22555 	 *
22556 	 * This function could be called from ill_lookup_on_name. In that case
22557 	 * the interface is loopback "lo", which will not generate a NIC event.
22558 	 */
22559 	if (ill->ill_name_length <= 2 ||
22560 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22561 		/*
22562 		 * Generate nic plumb event for ill_name even if
22563 		 * ipmp_hook_emulation is set. That avoids generating events
22564 		 * for the ill_names should ipmp_hook_emulation be turned on
22565 		 * later.
22566 		 */
22567 		ill_nic_event_plumb(ill, B_FALSE);
22568 	}
22569 	RELEASE_ILL_LOCKS(ill, ill_other);
22570 	mutex_exit(&phyi->phyint_lock);
22571 }
22572 
22573 /*
22574  * Allocate a NE_PLUMB nic info event and store in the ill.
22575  * If 'group' is set we do it for the group name, otherwise the ill name.
22576  * It will be sent when we leave the ipsq.
22577  */
22578 void
22579 ill_nic_event_plumb(ill_t *ill, boolean_t group)
22580 {
22581 	phyint_t	*phyi = ill->ill_phyint;
22582 	char		*name;
22583 	int		namelen;
22584 
22585 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22586 
22587 	if (group) {
22588 		ASSERT(phyi->phyint_groupname_len != 0);
22589 		namelen = phyi->phyint_groupname_len;
22590 		name = phyi->phyint_groupname;
22591 	} else {
22592 		namelen = ill->ill_name_length;
22593 		name = ill->ill_name;
22594 	}
22595 
22596 	ill_nic_event_dispatch(ill, 0, NE_PLUMB, name, namelen);
22597 }
22598 
22599 /*
22600  * Notify any downstream modules of the name of this interface.
22601  * An M_IOCTL is used even though we don't expect a successful reply.
22602  * Any reply message from the driver (presumably an M_IOCNAK) will
22603  * eventually get discarded somewhere upstream.  The message format is
22604  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22605  * to IP.
22606  */
22607 static void
22608 ip_ifname_notify(ill_t *ill, queue_t *q)
22609 {
22610 	mblk_t *mp1, *mp2;
22611 	struct iocblk *iocp;
22612 	struct lifreq *lifr;
22613 
22614 	mp1 = mkiocb(SIOCSLIFNAME);
22615 	if (mp1 == NULL)
22616 		return;
22617 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22618 	if (mp2 == NULL) {
22619 		freeb(mp1);
22620 		return;
22621 	}
22622 
22623 	mp1->b_cont = mp2;
22624 	iocp = (struct iocblk *)mp1->b_rptr;
22625 	iocp->ioc_count = sizeof (struct lifreq);
22626 
22627 	lifr = (struct lifreq *)mp2->b_rptr;
22628 	mp2->b_wptr += sizeof (struct lifreq);
22629 	bzero(lifr, sizeof (struct lifreq));
22630 
22631 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22632 	lifr->lifr_ppa = ill->ill_ppa;
22633 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22634 
22635 	putnext(q, mp1);
22636 }
22637 
22638 static int
22639 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22640 {
22641 	int err;
22642 	ip_stack_t	*ipst = ill->ill_ipst;
22643 
22644 	/* Set the obsolete NDD per-interface forwarding name. */
22645 	err = ill_set_ndd_name(ill);
22646 	if (err != 0) {
22647 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22648 		    err);
22649 	}
22650 
22651 	/* Tell downstream modules where they are. */
22652 	ip_ifname_notify(ill, q);
22653 
22654 	/*
22655 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22656 	 * Error cases are ENOMEM ...
22657 	 */
22658 	err = ill_dl_phys(ill, ipif, mp, q);
22659 
22660 	/*
22661 	 * If there is no IRE expiration timer running, get one started.
22662 	 * igmp and mld timers will be triggered by the first multicast
22663 	 */
22664 	if (ipst->ips_ip_ire_expire_id == 0) {
22665 		/*
22666 		 * acquire the lock and check again.
22667 		 */
22668 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22669 		if (ipst->ips_ip_ire_expire_id == 0) {
22670 			ipst->ips_ip_ire_expire_id = timeout(
22671 			    ip_trash_timer_expire, ipst,
22672 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22673 		}
22674 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22675 	}
22676 
22677 	if (ill->ill_isv6) {
22678 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22679 		if (ipst->ips_mld_slowtimeout_id == 0) {
22680 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22681 			    (void *)ipst,
22682 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22683 		}
22684 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22685 	} else {
22686 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22687 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22688 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22689 			    (void *)ipst,
22690 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22691 		}
22692 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22693 	}
22694 
22695 	return (err);
22696 }
22697 
22698 /*
22699  * Common routine for ppa and ifname setting. Should be called exclusive.
22700  *
22701  * Returns EINPROGRESS when mp has been consumed by queueing it on
22702  * ill_pending_mp and the ioctl will complete in ip_rput.
22703  *
22704  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22705  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22706  * For SLIFNAME, we pass these values back to the userland.
22707  */
22708 static int
22709 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22710 {
22711 	ill_t	*ill;
22712 	ipif_t	*ipif;
22713 	ipsq_t	*ipsq;
22714 	char	*ppa_ptr;
22715 	char	*old_ptr;
22716 	char	old_char;
22717 	int	error;
22718 	ip_stack_t	*ipst;
22719 
22720 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22721 	ASSERT(q->q_next != NULL);
22722 	ASSERT(interf_name != NULL);
22723 
22724 	ill = (ill_t *)q->q_ptr;
22725 	ipst = ill->ill_ipst;
22726 
22727 	ASSERT(ill->ill_ipst != NULL);
22728 	ASSERT(ill->ill_name[0] == '\0');
22729 	ASSERT(IAM_WRITER_ILL(ill));
22730 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22731 	ASSERT(ill->ill_ppa == UINT_MAX);
22732 
22733 	/* The ppa is sent down by ifconfig or is chosen */
22734 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22735 		return (EINVAL);
22736 	}
22737 
22738 	/*
22739 	 * make sure ppa passed in is same as ppa in the name.
22740 	 * This check is not made when ppa == UINT_MAX in that case ppa
22741 	 * in the name could be anything. System will choose a ppa and
22742 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22743 	 */
22744 	if (*new_ppa_ptr != UINT_MAX) {
22745 		/* stoi changes the pointer */
22746 		old_ptr = ppa_ptr;
22747 		/*
22748 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22749 		 * (they don't have an externally visible ppa).  We assign one
22750 		 * here so that we can manage the interface.  Note that in
22751 		 * the past this value was always 0 for DLPI 1 drivers.
22752 		 */
22753 		if (*new_ppa_ptr == 0)
22754 			*new_ppa_ptr = stoi(&old_ptr);
22755 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22756 			return (EINVAL);
22757 	}
22758 	/*
22759 	 * terminate string before ppa
22760 	 * save char at that location.
22761 	 */
22762 	old_char = ppa_ptr[0];
22763 	ppa_ptr[0] = '\0';
22764 
22765 	ill->ill_ppa = *new_ppa_ptr;
22766 	/*
22767 	 * Finish as much work now as possible before calling ill_glist_insert
22768 	 * which makes the ill globally visible and also merges it with the
22769 	 * other protocol instance of this phyint. The remaining work is
22770 	 * done after entering the ipsq which may happen sometime later.
22771 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22772 	 */
22773 	ipif = ill->ill_ipif;
22774 
22775 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22776 	ipif_assign_seqid(ipif);
22777 
22778 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22779 		ill->ill_flags |= ILLF_IPV4;
22780 
22781 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22782 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22783 
22784 	if (ill->ill_flags & ILLF_IPV6) {
22785 
22786 		ill->ill_isv6 = B_TRUE;
22787 		if (ill->ill_rq != NULL) {
22788 			ill->ill_rq->q_qinfo = &iprinitv6;
22789 			ill->ill_wq->q_qinfo = &ipwinitv6;
22790 		}
22791 
22792 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22793 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22794 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22795 		ipif->ipif_v6subnet = ipv6_all_zeros;
22796 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22797 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22798 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22799 		/*
22800 		 * point-to-point or Non-mulicast capable
22801 		 * interfaces won't do NUD unless explicitly
22802 		 * configured to do so.
22803 		 */
22804 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22805 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22806 			ill->ill_flags |= ILLF_NONUD;
22807 		}
22808 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22809 		if (ill->ill_flags & ILLF_NOARP) {
22810 			/*
22811 			 * Note: xresolv interfaces will eventually need
22812 			 * NOARP set here as well, but that will require
22813 			 * those external resolvers to have some
22814 			 * knowledge of that flag and act appropriately.
22815 			 * Not to be changed at present.
22816 			 */
22817 			ill->ill_flags &= ~ILLF_NOARP;
22818 		}
22819 		/*
22820 		 * Set the ILLF_ROUTER flag according to the global
22821 		 * IPv6 forwarding policy.
22822 		 */
22823 		if (ipst->ips_ipv6_forward != 0)
22824 			ill->ill_flags |= ILLF_ROUTER;
22825 	} else if (ill->ill_flags & ILLF_IPV4) {
22826 		ill->ill_isv6 = B_FALSE;
22827 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22828 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22829 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22830 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22831 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22832 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22833 		/*
22834 		 * Set the ILLF_ROUTER flag according to the global
22835 		 * IPv4 forwarding policy.
22836 		 */
22837 		if (ipst->ips_ip_g_forward != 0)
22838 			ill->ill_flags |= ILLF_ROUTER;
22839 	}
22840 
22841 	ASSERT(ill->ill_phyint != NULL);
22842 
22843 	/*
22844 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22845 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22846 	 */
22847 	if (!ill_allocate_mibs(ill))
22848 		return (ENOMEM);
22849 
22850 	/*
22851 	 * Pick a default sap until we get the DL_INFO_ACK back from
22852 	 * the driver.
22853 	 */
22854 	if (ill->ill_sap == 0) {
22855 		if (ill->ill_isv6)
22856 			ill->ill_sap = IP6_DL_SAP;
22857 		else
22858 			ill->ill_sap = IP_DL_SAP;
22859 	}
22860 
22861 	ill->ill_ifname_pending = 1;
22862 	ill->ill_ifname_pending_err = 0;
22863 
22864 	/*
22865 	 * When the first ipif comes up in ipif_up_done(), multicast groups
22866 	 * that were joined while this ill was not bound to the DLPI link need
22867 	 * to be recovered by ill_recover_multicast().
22868 	 */
22869 	ill->ill_need_recover_multicast = 1;
22870 
22871 	ill_refhold(ill);
22872 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
22873 	if ((error = ill_glist_insert(ill, interf_name,
22874 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
22875 		ill->ill_ppa = UINT_MAX;
22876 		ill->ill_name[0] = '\0';
22877 		/*
22878 		 * undo null termination done above.
22879 		 */
22880 		ppa_ptr[0] = old_char;
22881 		rw_exit(&ipst->ips_ill_g_lock);
22882 		ill_refrele(ill);
22883 		return (error);
22884 	}
22885 
22886 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
22887 
22888 	/*
22889 	 * When we return the buffer pointed to by interf_name should contain
22890 	 * the same name as in ill_name.
22891 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
22892 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
22893 	 * so copy full name and update the ppa ptr.
22894 	 * When ppa passed in != UINT_MAX all values are correct just undo
22895 	 * null termination, this saves a bcopy.
22896 	 */
22897 	if (*new_ppa_ptr == UINT_MAX) {
22898 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
22899 		*new_ppa_ptr = ill->ill_ppa;
22900 	} else {
22901 		/*
22902 		 * undo null termination done above.
22903 		 */
22904 		ppa_ptr[0] = old_char;
22905 	}
22906 
22907 	/* Let SCTP know about this ILL */
22908 	sctp_update_ill(ill, SCTP_ILL_INSERT);
22909 
22910 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
22911 	    B_TRUE);
22912 
22913 	rw_exit(&ipst->ips_ill_g_lock);
22914 	ill_refrele(ill);
22915 	if (ipsq == NULL)
22916 		return (EINPROGRESS);
22917 
22918 	/*
22919 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
22920 	 */
22921 	if (ipsq->ipsq_current_ipif == NULL)
22922 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
22923 	else
22924 		ASSERT(ipsq->ipsq_current_ipif == ipif);
22925 
22926 	error = ipif_set_values_tail(ill, ipif, mp, q);
22927 	ipsq_exit(ipsq);
22928 	if (error != 0 && error != EINPROGRESS) {
22929 		/*
22930 		 * restore previous values
22931 		 */
22932 		ill->ill_isv6 = B_FALSE;
22933 	}
22934 	return (error);
22935 }
22936 
22937 
22938 void
22939 ipif_init(ip_stack_t *ipst)
22940 {
22941 	hrtime_t hrt;
22942 	int i;
22943 
22944 	/*
22945 	 * Can't call drv_getparm here as it is too early in the boot.
22946 	 * As we use ipif_src_random just for picking a different
22947 	 * source address everytime, this need not be really random.
22948 	 */
22949 	hrt = gethrtime();
22950 	ipst->ips_ipif_src_random =
22951 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
22952 
22953 	for (i = 0; i < MAX_G_HEADS; i++) {
22954 		ipst->ips_ill_g_heads[i].ill_g_list_head =
22955 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
22956 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
22957 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
22958 	}
22959 
22960 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22961 	    ill_phyint_compare_index,
22962 	    sizeof (phyint_t),
22963 	    offsetof(struct phyint, phyint_avl_by_index));
22964 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22965 	    ill_phyint_compare_name,
22966 	    sizeof (phyint_t),
22967 	    offsetof(struct phyint, phyint_avl_by_name));
22968 }
22969 
22970 /*
22971  * Lookup the ipif corresponding to the onlink destination address. For
22972  * point-to-point interfaces, it matches with remote endpoint destination
22973  * address. For point-to-multipoint interfaces it only tries to match the
22974  * destination with the interface's subnet address. The longest, most specific
22975  * match is found to take care of such rare network configurations like -
22976  * le0: 129.146.1.1/16
22977  * le1: 129.146.2.2/24
22978  * It is used only by SO_DONTROUTE at the moment.
22979  */
22980 ipif_t *
22981 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
22982 {
22983 	ipif_t	*ipif, *best_ipif;
22984 	ill_t	*ill;
22985 	ill_walk_context_t ctx;
22986 
22987 	ASSERT(zoneid != ALL_ZONES);
22988 	best_ipif = NULL;
22989 
22990 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
22991 	ill = ILL_START_WALK_V4(&ctx, ipst);
22992 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22993 		mutex_enter(&ill->ill_lock);
22994 		for (ipif = ill->ill_ipif; ipif != NULL;
22995 		    ipif = ipif->ipif_next) {
22996 			if (!IPIF_CAN_LOOKUP(ipif))
22997 				continue;
22998 			if (ipif->ipif_zoneid != zoneid &&
22999 			    ipif->ipif_zoneid != ALL_ZONES)
23000 				continue;
23001 			/*
23002 			 * Point-to-point case. Look for exact match with
23003 			 * destination address.
23004 			 */
23005 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23006 				if (ipif->ipif_pp_dst_addr == addr) {
23007 					ipif_refhold_locked(ipif);
23008 					mutex_exit(&ill->ill_lock);
23009 					rw_exit(&ipst->ips_ill_g_lock);
23010 					if (best_ipif != NULL)
23011 						ipif_refrele(best_ipif);
23012 					return (ipif);
23013 				}
23014 			} else if (ipif->ipif_subnet == (addr &
23015 			    ipif->ipif_net_mask)) {
23016 				/*
23017 				 * Point-to-multipoint case. Looping through to
23018 				 * find the most specific match. If there are
23019 				 * multiple best match ipif's then prefer ipif's
23020 				 * that are UP. If there is only one best match
23021 				 * ipif and it is DOWN we must still return it.
23022 				 */
23023 				if ((best_ipif == NULL) ||
23024 				    (ipif->ipif_net_mask >
23025 				    best_ipif->ipif_net_mask) ||
23026 				    ((ipif->ipif_net_mask ==
23027 				    best_ipif->ipif_net_mask) &&
23028 				    ((ipif->ipif_flags & IPIF_UP) &&
23029 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23030 					ipif_refhold_locked(ipif);
23031 					mutex_exit(&ill->ill_lock);
23032 					rw_exit(&ipst->ips_ill_g_lock);
23033 					if (best_ipif != NULL)
23034 						ipif_refrele(best_ipif);
23035 					best_ipif = ipif;
23036 					rw_enter(&ipst->ips_ill_g_lock,
23037 					    RW_READER);
23038 					mutex_enter(&ill->ill_lock);
23039 				}
23040 			}
23041 		}
23042 		mutex_exit(&ill->ill_lock);
23043 	}
23044 	rw_exit(&ipst->ips_ill_g_lock);
23045 	return (best_ipif);
23046 }
23047 
23048 /*
23049  * Save enough information so that we can recreate the IRE if
23050  * the interface goes down and then up.
23051  */
23052 static void
23053 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23054 {
23055 	mblk_t	*save_mp;
23056 
23057 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23058 	if (save_mp != NULL) {
23059 		ifrt_t	*ifrt;
23060 
23061 		save_mp->b_wptr += sizeof (ifrt_t);
23062 		ifrt = (ifrt_t *)save_mp->b_rptr;
23063 		bzero(ifrt, sizeof (ifrt_t));
23064 		ifrt->ifrt_type = ire->ire_type;
23065 		ifrt->ifrt_addr = ire->ire_addr;
23066 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23067 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23068 		ifrt->ifrt_mask = ire->ire_mask;
23069 		ifrt->ifrt_flags = ire->ire_flags;
23070 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23071 		mutex_enter(&ipif->ipif_saved_ire_lock);
23072 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23073 		ipif->ipif_saved_ire_mp = save_mp;
23074 		ipif->ipif_saved_ire_cnt++;
23075 		mutex_exit(&ipif->ipif_saved_ire_lock);
23076 	}
23077 }
23078 
23079 static void
23080 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23081 {
23082 	mblk_t	**mpp;
23083 	mblk_t	*mp;
23084 	ifrt_t	*ifrt;
23085 
23086 	/* Remove from ipif_saved_ire_mp list if it is there */
23087 	mutex_enter(&ipif->ipif_saved_ire_lock);
23088 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23089 	    mpp = &(*mpp)->b_cont) {
23090 		/*
23091 		 * On a given ipif, the triple of address, gateway and
23092 		 * mask is unique for each saved IRE (in the case of
23093 		 * ordinary interface routes, the gateway address is
23094 		 * all-zeroes).
23095 		 */
23096 		mp = *mpp;
23097 		ifrt = (ifrt_t *)mp->b_rptr;
23098 		if (ifrt->ifrt_addr == ire->ire_addr &&
23099 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23100 		    ifrt->ifrt_mask == ire->ire_mask) {
23101 			*mpp = mp->b_cont;
23102 			ipif->ipif_saved_ire_cnt--;
23103 			freeb(mp);
23104 			break;
23105 		}
23106 	}
23107 	mutex_exit(&ipif->ipif_saved_ire_lock);
23108 }
23109 
23110 /*
23111  * IP multirouting broadcast routes handling
23112  * Append CGTP broadcast IREs to regular ones created
23113  * at ifconfig time.
23114  */
23115 static void
23116 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23117 {
23118 	ire_t *ire_prim;
23119 
23120 	ASSERT(ire != NULL);
23121 	ASSERT(ire_dst != NULL);
23122 
23123 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23124 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23125 	if (ire_prim != NULL) {
23126 		/*
23127 		 * We are in the special case of broadcasts for
23128 		 * CGTP. We add an IRE_BROADCAST that holds
23129 		 * the RTF_MULTIRT flag, the destination
23130 		 * address of ire_dst and the low level
23131 		 * info of ire_prim. In other words, CGTP
23132 		 * broadcast is added to the redundant ipif.
23133 		 */
23134 		ipif_t *ipif_prim;
23135 		ire_t  *bcast_ire;
23136 
23137 		ipif_prim = ire_prim->ire_ipif;
23138 
23139 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23140 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23141 		    (void *)ire_dst, (void *)ire_prim,
23142 		    (void *)ipif_prim));
23143 
23144 		bcast_ire = ire_create(
23145 		    (uchar_t *)&ire->ire_addr,
23146 		    (uchar_t *)&ip_g_all_ones,
23147 		    (uchar_t *)&ire_dst->ire_src_addr,
23148 		    (uchar_t *)&ire->ire_gateway_addr,
23149 		    &ipif_prim->ipif_mtu,
23150 		    NULL,
23151 		    ipif_prim->ipif_rq,
23152 		    ipif_prim->ipif_wq,
23153 		    IRE_BROADCAST,
23154 		    ipif_prim,
23155 		    0,
23156 		    0,
23157 		    0,
23158 		    ire->ire_flags,
23159 		    &ire_uinfo_null,
23160 		    NULL,
23161 		    NULL,
23162 		    ipst);
23163 
23164 		if (bcast_ire != NULL) {
23165 
23166 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23167 			    B_FALSE) == 0) {
23168 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23169 				    "added bcast_ire %p\n",
23170 				    (void *)bcast_ire));
23171 
23172 				ipif_save_ire(bcast_ire->ire_ipif,
23173 				    bcast_ire);
23174 				ire_refrele(bcast_ire);
23175 			}
23176 		}
23177 		ire_refrele(ire_prim);
23178 	}
23179 }
23180 
23181 
23182 /*
23183  * IP multirouting broadcast routes handling
23184  * Remove the broadcast ire
23185  */
23186 static void
23187 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23188 {
23189 	ire_t *ire_dst;
23190 
23191 	ASSERT(ire != NULL);
23192 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23193 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23194 	if (ire_dst != NULL) {
23195 		ire_t *ire_prim;
23196 
23197 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23198 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23199 		if (ire_prim != NULL) {
23200 			ipif_t *ipif_prim;
23201 			ire_t  *bcast_ire;
23202 
23203 			ipif_prim = ire_prim->ire_ipif;
23204 
23205 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23206 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23207 			    (void *)ire_dst, (void *)ire_prim,
23208 			    (void *)ipif_prim));
23209 
23210 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23211 			    ire->ire_gateway_addr,
23212 			    IRE_BROADCAST,
23213 			    ipif_prim, ALL_ZONES,
23214 			    NULL,
23215 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23216 			    MATCH_IRE_MASK, ipst);
23217 
23218 			if (bcast_ire != NULL) {
23219 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23220 				    "looked up bcast_ire %p\n",
23221 				    (void *)bcast_ire));
23222 				ipif_remove_ire(bcast_ire->ire_ipif,
23223 				    bcast_ire);
23224 				ire_delete(bcast_ire);
23225 				ire_refrele(bcast_ire);
23226 			}
23227 			ire_refrele(ire_prim);
23228 		}
23229 		ire_refrele(ire_dst);
23230 	}
23231 }
23232 
23233 /*
23234  * IPsec hardware acceleration capabilities related functions.
23235  */
23236 
23237 /*
23238  * Free a per-ill IPsec capabilities structure.
23239  */
23240 static void
23241 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23242 {
23243 	if (capab->auth_hw_algs != NULL)
23244 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23245 	if (capab->encr_hw_algs != NULL)
23246 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23247 	if (capab->encr_algparm != NULL)
23248 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23249 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23250 }
23251 
23252 /*
23253  * Allocate a new per-ill IPsec capabilities structure. This structure
23254  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23255  * an array which specifies, for each algorithm, whether this algorithm
23256  * is supported by the ill or not.
23257  */
23258 static ill_ipsec_capab_t *
23259 ill_ipsec_capab_alloc(void)
23260 {
23261 	ill_ipsec_capab_t *capab;
23262 	uint_t nelems;
23263 
23264 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23265 	if (capab == NULL)
23266 		return (NULL);
23267 
23268 	/* we need one bit per algorithm */
23269 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23270 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23271 
23272 	/* allocate memory to store algorithm flags */
23273 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23274 	if (capab->encr_hw_algs == NULL)
23275 		goto nomem;
23276 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23277 	if (capab->auth_hw_algs == NULL)
23278 		goto nomem;
23279 	/*
23280 	 * Leave encr_algparm NULL for now since we won't need it half
23281 	 * the time
23282 	 */
23283 	return (capab);
23284 
23285 nomem:
23286 	ill_ipsec_capab_free(capab);
23287 	return (NULL);
23288 }
23289 
23290 /*
23291  * Resize capability array.  Since we're exclusive, this is OK.
23292  */
23293 static boolean_t
23294 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23295 {
23296 	ipsec_capab_algparm_t *nalp, *oalp;
23297 	uint32_t olen, nlen;
23298 
23299 	oalp = capab->encr_algparm;
23300 	olen = capab->encr_algparm_size;
23301 
23302 	if (oalp != NULL) {
23303 		if (algid < capab->encr_algparm_end)
23304 			return (B_TRUE);
23305 	}
23306 
23307 	nlen = (algid + 1) * sizeof (*nalp);
23308 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23309 	if (nalp == NULL)
23310 		return (B_FALSE);
23311 
23312 	if (oalp != NULL) {
23313 		bcopy(oalp, nalp, olen);
23314 		kmem_free(oalp, olen);
23315 	}
23316 	capab->encr_algparm = nalp;
23317 	capab->encr_algparm_size = nlen;
23318 	capab->encr_algparm_end = algid + 1;
23319 
23320 	return (B_TRUE);
23321 }
23322 
23323 /*
23324  * Compare the capabilities of the specified ill with the protocol
23325  * and algorithms specified by the SA passed as argument.
23326  * If they match, returns B_TRUE, B_FALSE if they do not match.
23327  *
23328  * The ill can be passed as a pointer to it, or by specifying its index
23329  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23330  *
23331  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23332  * packet is eligible for hardware acceleration, and by
23333  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23334  * to a particular ill.
23335  */
23336 boolean_t
23337 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23338     ipsa_t *sa, netstack_t *ns)
23339 {
23340 	boolean_t sa_isv6;
23341 	uint_t algid;
23342 	struct ill_ipsec_capab_s *cpp;
23343 	boolean_t need_refrele = B_FALSE;
23344 	ip_stack_t	*ipst = ns->netstack_ip;
23345 
23346 	if (ill == NULL) {
23347 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23348 		    NULL, NULL, NULL, ipst);
23349 		if (ill == NULL) {
23350 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23351 			return (B_FALSE);
23352 		}
23353 		need_refrele = B_TRUE;
23354 	}
23355 
23356 	/*
23357 	 * Use the address length specified by the SA to determine
23358 	 * if it corresponds to a IPv6 address, and fail the matching
23359 	 * if the isv6 flag passed as argument does not match.
23360 	 * Note: this check is used for SADB capability checking before
23361 	 * sending SA information to an ill.
23362 	 */
23363 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23364 	if (sa_isv6 != ill_isv6)
23365 		/* protocol mismatch */
23366 		goto done;
23367 
23368 	/*
23369 	 * Check if the ill supports the protocol, algorithm(s) and
23370 	 * key size(s) specified by the SA, and get the pointers to
23371 	 * the algorithms supported by the ill.
23372 	 */
23373 	switch (sa->ipsa_type) {
23374 
23375 	case SADB_SATYPE_ESP:
23376 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23377 			/* ill does not support ESP acceleration */
23378 			goto done;
23379 		cpp = ill->ill_ipsec_capab_esp;
23380 		algid = sa->ipsa_auth_alg;
23381 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23382 			goto done;
23383 		algid = sa->ipsa_encr_alg;
23384 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23385 			goto done;
23386 		if (algid < cpp->encr_algparm_end) {
23387 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23388 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23389 				goto done;
23390 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23391 				goto done;
23392 		}
23393 		break;
23394 
23395 	case SADB_SATYPE_AH:
23396 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23397 			/* ill does not support AH acceleration */
23398 			goto done;
23399 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23400 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23401 			goto done;
23402 		break;
23403 	}
23404 
23405 	if (need_refrele)
23406 		ill_refrele(ill);
23407 	return (B_TRUE);
23408 done:
23409 	if (need_refrele)
23410 		ill_refrele(ill);
23411 	return (B_FALSE);
23412 }
23413 
23414 /*
23415  * Add a new ill to the list of IPsec capable ills.
23416  * Called from ill_capability_ipsec_ack() when an ACK was received
23417  * indicating that IPsec hardware processing was enabled for an ill.
23418  *
23419  * ill must point to the ill for which acceleration was enabled.
23420  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23421  */
23422 static void
23423 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23424 {
23425 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23426 	uint_t sa_type;
23427 	uint_t ipproto;
23428 	ip_stack_t	*ipst = ill->ill_ipst;
23429 
23430 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23431 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23432 
23433 	switch (dl_cap) {
23434 	case DL_CAPAB_IPSEC_AH:
23435 		sa_type = SADB_SATYPE_AH;
23436 		ills = &ipst->ips_ipsec_capab_ills_ah;
23437 		ipproto = IPPROTO_AH;
23438 		break;
23439 	case DL_CAPAB_IPSEC_ESP:
23440 		sa_type = SADB_SATYPE_ESP;
23441 		ills = &ipst->ips_ipsec_capab_ills_esp;
23442 		ipproto = IPPROTO_ESP;
23443 		break;
23444 	}
23445 
23446 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23447 
23448 	/*
23449 	 * Add ill index to list of hardware accelerators. If
23450 	 * already in list, do nothing.
23451 	 */
23452 	for (cur_ill = *ills; cur_ill != NULL &&
23453 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23454 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23455 		;
23456 
23457 	if (cur_ill == NULL) {
23458 		/* if this is a new entry for this ill */
23459 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23460 		if (new_ill == NULL) {
23461 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23462 			return;
23463 		}
23464 
23465 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23466 		new_ill->ill_isv6 = ill->ill_isv6;
23467 		new_ill->next = *ills;
23468 		*ills = new_ill;
23469 	} else if (!sadb_resync) {
23470 		/* not resync'ing SADB and an entry exists for this ill */
23471 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23472 		return;
23473 	}
23474 
23475 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23476 
23477 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23478 		/*
23479 		 * IPsec module for protocol loaded, initiate dump
23480 		 * of the SADB to this ill.
23481 		 */
23482 		sadb_ill_download(ill, sa_type);
23483 }
23484 
23485 /*
23486  * Remove an ill from the list of IPsec capable ills.
23487  */
23488 static void
23489 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23490 {
23491 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23492 	ip_stack_t	*ipst = ill->ill_ipst;
23493 
23494 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23495 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23496 
23497 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23498 	    &ipst->ips_ipsec_capab_ills_esp;
23499 
23500 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23501 
23502 	prev_ill = NULL;
23503 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23504 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23505 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23506 		;
23507 	if (cur_ill == NULL) {
23508 		/* entry not found */
23509 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23510 		return;
23511 	}
23512 	if (prev_ill == NULL) {
23513 		/* entry at front of list */
23514 		*ills = NULL;
23515 	} else {
23516 		prev_ill->next = cur_ill->next;
23517 	}
23518 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23519 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23520 }
23521 
23522 /*
23523  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23524  * supporting the specified IPsec protocol acceleration.
23525  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23526  * We free the mblk and, if sa is non-null, release the held referece.
23527  */
23528 void
23529 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23530     netstack_t *ns)
23531 {
23532 	ipsec_capab_ill_t *ici, *cur_ici;
23533 	ill_t *ill;
23534 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23535 	ip_stack_t	*ipst = ns->netstack_ip;
23536 
23537 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23538 	    ipst->ips_ipsec_capab_ills_esp;
23539 
23540 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23541 
23542 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23543 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23544 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23545 
23546 		/*
23547 		 * Handle the case where the ill goes away while the SADB is
23548 		 * attempting to send messages.  If it's going away, it's
23549 		 * nuking its shadow SADB, so we don't care..
23550 		 */
23551 
23552 		if (ill == NULL)
23553 			continue;
23554 
23555 		if (sa != NULL) {
23556 			/*
23557 			 * Make sure capabilities match before
23558 			 * sending SA to ill.
23559 			 */
23560 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23561 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23562 				ill_refrele(ill);
23563 				continue;
23564 			}
23565 
23566 			mutex_enter(&sa->ipsa_lock);
23567 			sa->ipsa_flags |= IPSA_F_HW;
23568 			mutex_exit(&sa->ipsa_lock);
23569 		}
23570 
23571 		/*
23572 		 * Copy template message, and add it to the front
23573 		 * of the mblk ship list. We want to avoid holding
23574 		 * the ipsec_capab_ills_lock while sending the
23575 		 * message to the ills.
23576 		 *
23577 		 * The b_next and b_prev are temporarily used
23578 		 * to build a list of mblks to be sent down, and to
23579 		 * save the ill to which they must be sent.
23580 		 */
23581 		nmp = copymsg(mp);
23582 		if (nmp == NULL) {
23583 			ill_refrele(ill);
23584 			continue;
23585 		}
23586 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23587 		nmp->b_next = mp_ship_list;
23588 		mp_ship_list = nmp;
23589 		nmp->b_prev = (mblk_t *)ill;
23590 	}
23591 
23592 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23593 
23594 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23595 		/* restore the mblk to a sane state */
23596 		next_mp = nmp->b_next;
23597 		nmp->b_next = NULL;
23598 		ill = (ill_t *)nmp->b_prev;
23599 		nmp->b_prev = NULL;
23600 
23601 		ill_dlpi_send(ill, nmp);
23602 		ill_refrele(ill);
23603 	}
23604 
23605 	if (sa != NULL)
23606 		IPSA_REFRELE(sa);
23607 	freemsg(mp);
23608 }
23609 
23610 /*
23611  * Derive an interface id from the link layer address.
23612  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23613  */
23614 static boolean_t
23615 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23616 {
23617 	char		*addr;
23618 
23619 	if (phys_length != ETHERADDRL)
23620 		return (B_FALSE);
23621 
23622 	/* Form EUI-64 like address */
23623 	addr = (char *)&v6addr->s6_addr32[2];
23624 	bcopy((char *)phys_addr, addr, 3);
23625 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23626 	addr[3] = (char)0xff;
23627 	addr[4] = (char)0xfe;
23628 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23629 	return (B_TRUE);
23630 }
23631 
23632 /* ARGSUSED */
23633 static boolean_t
23634 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23635 {
23636 	return (B_FALSE);
23637 }
23638 
23639 /* ARGSUSED */
23640 static boolean_t
23641 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23642     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23643 {
23644 	/*
23645 	 * Multicast address mappings used over Ethernet/802.X.
23646 	 * This address is used as a base for mappings.
23647 	 */
23648 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23649 	    0x00, 0x00, 0x00};
23650 
23651 	/*
23652 	 * Extract low order 32 bits from IPv6 multicast address.
23653 	 * Or that into the link layer address, starting from the
23654 	 * second byte.
23655 	 */
23656 	*hw_start = 2;
23657 	v6_extract_mask->s6_addr32[0] = 0;
23658 	v6_extract_mask->s6_addr32[1] = 0;
23659 	v6_extract_mask->s6_addr32[2] = 0;
23660 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23661 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23662 	return (B_TRUE);
23663 }
23664 
23665 /*
23666  * Indicate by return value whether multicast is supported. If not,
23667  * this code should not touch/change any parameters.
23668  */
23669 /* ARGSUSED */
23670 static boolean_t
23671 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23672     uint32_t *hw_start, ipaddr_t *extract_mask)
23673 {
23674 	/*
23675 	 * Multicast address mappings used over Ethernet/802.X.
23676 	 * This address is used as a base for mappings.
23677 	 */
23678 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23679 	    0x00, 0x00, 0x00 };
23680 
23681 	if (phys_length != ETHERADDRL)
23682 		return (B_FALSE);
23683 
23684 	*extract_mask = htonl(0x007fffff);
23685 	*hw_start = 2;
23686 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23687 	return (B_TRUE);
23688 }
23689 
23690 /*
23691  * Derive IPoIB interface id from the link layer address.
23692  */
23693 static boolean_t
23694 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23695 {
23696 	char		*addr;
23697 
23698 	if (phys_length != 20)
23699 		return (B_FALSE);
23700 	addr = (char *)&v6addr->s6_addr32[2];
23701 	bcopy(phys_addr + 12, addr, 8);
23702 	/*
23703 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23704 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23705 	 * rules. In these cases, the IBA considers these GUIDs to be in
23706 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23707 	 * required; vendors are required not to assign global EUI-64's
23708 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23709 	 * of the interface identifier. Whether the GUID is in modified
23710 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23711 	 * bit set to 1.
23712 	 */
23713 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23714 	return (B_TRUE);
23715 }
23716 
23717 /*
23718  * Note on mapping from multicast IP addresses to IPoIB multicast link
23719  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23720  * The format of an IPoIB multicast address is:
23721  *
23722  *  4 byte QPN      Scope Sign.  Pkey
23723  * +--------------------------------------------+
23724  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23725  * +--------------------------------------------+
23726  *
23727  * The Scope and Pkey components are properties of the IBA port and
23728  * network interface. They can be ascertained from the broadcast address.
23729  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23730  */
23731 
23732 static boolean_t
23733 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23734     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23735 {
23736 	/*
23737 	 * Base IPoIB IPv6 multicast address used for mappings.
23738 	 * Does not contain the IBA scope/Pkey values.
23739 	 */
23740 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23741 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23742 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23743 
23744 	/*
23745 	 * Extract low order 80 bits from IPv6 multicast address.
23746 	 * Or that into the link layer address, starting from the
23747 	 * sixth byte.
23748 	 */
23749 	*hw_start = 6;
23750 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23751 
23752 	/*
23753 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23754 	 */
23755 	*(maddr + 5) = *(bphys_addr + 5);
23756 	*(maddr + 8) = *(bphys_addr + 8);
23757 	*(maddr + 9) = *(bphys_addr + 9);
23758 
23759 	v6_extract_mask->s6_addr32[0] = 0;
23760 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23761 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23762 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23763 	return (B_TRUE);
23764 }
23765 
23766 static boolean_t
23767 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23768     uint32_t *hw_start, ipaddr_t *extract_mask)
23769 {
23770 	/*
23771 	 * Base IPoIB IPv4 multicast address used for mappings.
23772 	 * Does not contain the IBA scope/Pkey values.
23773 	 */
23774 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23775 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23776 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23777 
23778 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23779 		return (B_FALSE);
23780 
23781 	/*
23782 	 * Extract low order 28 bits from IPv4 multicast address.
23783 	 * Or that into the link layer address, starting from the
23784 	 * sixteenth byte.
23785 	 */
23786 	*extract_mask = htonl(0x0fffffff);
23787 	*hw_start = 16;
23788 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23789 
23790 	/*
23791 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23792 	 */
23793 	*(maddr + 5) = *(bphys_addr + 5);
23794 	*(maddr + 8) = *(bphys_addr + 8);
23795 	*(maddr + 9) = *(bphys_addr + 9);
23796 	return (B_TRUE);
23797 }
23798 
23799 /*
23800  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23801  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23802  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23803  * the link-local address is preferred.
23804  */
23805 boolean_t
23806 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23807 {
23808 	ipif_t	*ipif;
23809 	ipif_t	*maybe_ipif = NULL;
23810 
23811 	mutex_enter(&ill->ill_lock);
23812 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23813 		mutex_exit(&ill->ill_lock);
23814 		if (ipifp != NULL)
23815 			*ipifp = NULL;
23816 		return (B_FALSE);
23817 	}
23818 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23819 		if (!IPIF_CAN_LOOKUP(ipif))
23820 			continue;
23821 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23822 		    ipif->ipif_zoneid != ALL_ZONES)
23823 			continue;
23824 		if ((ipif->ipif_flags & flags) != flags)
23825 			continue;
23826 
23827 		if (ipifp == NULL) {
23828 			mutex_exit(&ill->ill_lock);
23829 			ASSERT(maybe_ipif == NULL);
23830 			return (B_TRUE);
23831 		}
23832 		if (!ill->ill_isv6 ||
23833 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23834 			ipif_refhold_locked(ipif);
23835 			mutex_exit(&ill->ill_lock);
23836 			*ipifp = ipif;
23837 			return (B_TRUE);
23838 		}
23839 		if (maybe_ipif == NULL)
23840 			maybe_ipif = ipif;
23841 	}
23842 	if (ipifp != NULL) {
23843 		if (maybe_ipif != NULL)
23844 			ipif_refhold_locked(maybe_ipif);
23845 		*ipifp = maybe_ipif;
23846 	}
23847 	mutex_exit(&ill->ill_lock);
23848 	return (maybe_ipif != NULL);
23849 }
23850 
23851 /*
23852  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23853  */
23854 boolean_t
23855 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23856 {
23857 	ill_t *illg;
23858 	ip_stack_t	*ipst = ill->ill_ipst;
23859 
23860 	/*
23861 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23862 	 */
23863 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23864 		return (B_TRUE);
23865 	}
23866 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23867 	if (ill->ill_group == NULL) {
23868 		/* ill not in a group */
23869 		rw_exit(&ipst->ips_ill_g_lock);
23870 		return (B_FALSE);
23871 	}
23872 
23873 	/*
23874 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23875 	 * group. We need to look for an ipif in the zone on all the ills in the
23876 	 * group.
23877 	 */
23878 	illg = ill->ill_group->illgrp_ill;
23879 	do {
23880 		/*
23881 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23882 		 * that it's not there.
23883 		 */
23884 		if (illg != ill &&
23885 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23886 			break;
23887 		}
23888 	} while ((illg = illg->ill_group_next) != NULL);
23889 	rw_exit(&ipst->ips_ill_g_lock);
23890 	return (illg != NULL);
23891 }
23892 
23893 /*
23894  * Check if this ill is only being used to send ICMP probes for IPMP
23895  */
23896 boolean_t
23897 ill_is_probeonly(ill_t *ill)
23898 {
23899 	/*
23900 	 * Check if the interface is FAILED, or INACTIVE
23901 	 */
23902 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23903 		return (B_TRUE);
23904 
23905 	return (B_FALSE);
23906 }
23907 
23908 /*
23909  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
23910  * If a pointer to an ipif_t is returned then the caller will need to do
23911  * an ill_refrele().
23912  *
23913  * If there is no real interface which matches the ifindex, then it looks
23914  * for a group that has a matching index. In the case of a group match the
23915  * lifidx must be zero. We don't need emulate the logical interfaces
23916  * since IP Filter's use of netinfo doesn't use that.
23917  */
23918 ipif_t *
23919 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
23920     ip_stack_t *ipst)
23921 {
23922 	ipif_t *ipif;
23923 	ill_t *ill;
23924 
23925 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
23926 	    ipst);
23927 
23928 	if (ill == NULL) {
23929 		/* Fallback to group names only if hook_emulation set */
23930 		if (!ipst->ips_ipmp_hook_emulation)
23931 			return (NULL);
23932 
23933 		if (lifidx != 0)
23934 			return (NULL);
23935 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
23936 		if (ill == NULL)
23937 			return (NULL);
23938 	}
23939 
23940 	mutex_enter(&ill->ill_lock);
23941 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23942 		mutex_exit(&ill->ill_lock);
23943 		ill_refrele(ill);
23944 		return (NULL);
23945 	}
23946 
23947 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23948 		if (!IPIF_CAN_LOOKUP(ipif))
23949 			continue;
23950 		if (lifidx == ipif->ipif_id) {
23951 			ipif_refhold_locked(ipif);
23952 			break;
23953 		}
23954 	}
23955 
23956 	mutex_exit(&ill->ill_lock);
23957 	ill_refrele(ill);
23958 	return (ipif);
23959 }
23960 
23961 /*
23962  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
23963  * There is one exceptions IRE_BROADCAST are difficult to recreate,
23964  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
23965  * for details.
23966  */
23967 void
23968 ill_fastpath_flush(ill_t *ill)
23969 {
23970 	ip_stack_t *ipst = ill->ill_ipst;
23971 
23972 	nce_fastpath_list_dispatch(ill, NULL, NULL);
23973 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
23974 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
23975 }
23976 
23977 /*
23978  * Set the physical address information for `ill' to the contents of the
23979  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
23980  * asynchronous if `ill' cannot immediately be quiesced -- in which case
23981  * EINPROGRESS will be returned.
23982  */
23983 int
23984 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
23985 {
23986 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
23987 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
23988 
23989 	ASSERT(IAM_WRITER_IPSQ(ipsq));
23990 
23991 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
23992 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
23993 		/* Changing DL_IPV6_TOKEN is not yet supported */
23994 		return (0);
23995 	}
23996 
23997 	/*
23998 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
23999 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24000 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24001 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24002 	 */
24003 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24004 		freemsg(mp);
24005 		return (ENOMEM);
24006 	}
24007 
24008 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24009 
24010 	/*
24011 	 * If we can quiesce the ill, then set the address.  If not, then
24012 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24013 	 */
24014 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24015 	mutex_enter(&ill->ill_lock);
24016 	if (!ill_is_quiescent(ill)) {
24017 		/* call cannot fail since `conn_t *' argument is NULL */
24018 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24019 		    mp, ILL_DOWN);
24020 		mutex_exit(&ill->ill_lock);
24021 		return (EINPROGRESS);
24022 	}
24023 	mutex_exit(&ill->ill_lock);
24024 
24025 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24026 	return (0);
24027 }
24028 
24029 /*
24030  * Once the ill associated with `q' has quiesced, set its physical address
24031  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24032  * are passed (linked by b_cont), since we sometimes need to save two distinct
24033  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24034  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24035  * is quiesced, we know any stale IREs with the old address information have
24036  * already been removed, so we don't need to call ill_fastpath_flush().
24037  */
24038 /* ARGSUSED */
24039 static void
24040 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24041 {
24042 	ill_t		*ill = q->q_ptr;
24043 	mblk_t		*addrmp2 = unlinkb(addrmp);
24044 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24045 	uint_t		addrlen, addroff;
24046 
24047 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24048 
24049 	addroff	= dlindp->dl_addr_offset;
24050 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24051 
24052 	switch (dlindp->dl_data) {
24053 	case DL_IPV6_LINK_LAYER_ADDR:
24054 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24055 		freemsg(addrmp2);
24056 		break;
24057 
24058 	case DL_CURR_PHYS_ADDR:
24059 		freemsg(ill->ill_phys_addr_mp);
24060 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24061 		ill->ill_phys_addr_mp = addrmp;
24062 		ill->ill_phys_addr_length = addrlen;
24063 
24064 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24065 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24066 		else
24067 			freemsg(addrmp2);
24068 		break;
24069 	default:
24070 		ASSERT(0);
24071 	}
24072 
24073 	/*
24074 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24075 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24076 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24077 	 * brought up.
24078 	 */
24079 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24080 		ipsq_current_finish(ipsq);
24081 }
24082 
24083 /*
24084  * Helper routine for setting the ill_nd_lla fields.
24085  */
24086 void
24087 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24088 {
24089 	freemsg(ill->ill_nd_lla_mp);
24090 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24091 	ill->ill_nd_lla_mp = ndmp;
24092 	ill->ill_nd_lla_len = addrlen;
24093 }
24094 
24095 major_t IP_MAJ;
24096 #define	IP	"ip"
24097 
24098 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24099 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24100 
24101 /*
24102  * Issue REMOVEIF ioctls to have the loopback interfaces
24103  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24104  * the former going away when the user-level processes in the zone
24105  * are killed  * and the latter are cleaned up by the stream head
24106  * str_stack_shutdown callback that undoes all I_PLINKs.
24107  */
24108 void
24109 ip_loopback_cleanup(ip_stack_t *ipst)
24110 {
24111 	int error;
24112 	ldi_handle_t	lh = NULL;
24113 	ldi_ident_t	li = NULL;
24114 	int		rval;
24115 	cred_t		*cr;
24116 	struct strioctl iocb;
24117 	struct lifreq	lifreq;
24118 
24119 	IP_MAJ = ddi_name_to_major(IP);
24120 
24121 #ifdef NS_DEBUG
24122 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24123 	    ipst->ips_netstack->netstack_stackid);
24124 #endif
24125 
24126 	bzero(&lifreq, sizeof (lifreq));
24127 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24128 
24129 	error = ldi_ident_from_major(IP_MAJ, &li);
24130 	if (error) {
24131 #ifdef DEBUG
24132 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24133 		    error);
24134 #endif
24135 		return;
24136 	}
24137 
24138 	cr = zone_get_kcred(netstackid_to_zoneid(
24139 	    ipst->ips_netstack->netstack_stackid));
24140 	ASSERT(cr != NULL);
24141 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24142 	if (error) {
24143 #ifdef DEBUG
24144 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24145 		    error);
24146 #endif
24147 		goto out;
24148 	}
24149 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24150 	iocb.ic_timout = 15;
24151 	iocb.ic_len = sizeof (lifreq);
24152 	iocb.ic_dp = (char *)&lifreq;
24153 
24154 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24155 	/* LINTED - statement has no consequent */
24156 	if (error) {
24157 #ifdef NS_DEBUG
24158 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24159 		    "UDP6 error %d\n", error);
24160 #endif
24161 	}
24162 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24163 	lh = NULL;
24164 
24165 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24166 	if (error) {
24167 #ifdef NS_DEBUG
24168 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24169 		    error);
24170 #endif
24171 		goto out;
24172 	}
24173 
24174 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24175 	iocb.ic_timout = 15;
24176 	iocb.ic_len = sizeof (lifreq);
24177 	iocb.ic_dp = (char *)&lifreq;
24178 
24179 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24180 	/* LINTED - statement has no consequent */
24181 	if (error) {
24182 #ifdef NS_DEBUG
24183 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24184 		    "UDP error %d\n", error);
24185 #endif
24186 	}
24187 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24188 	lh = NULL;
24189 
24190 out:
24191 	/* Close layered handles */
24192 	if (lh)
24193 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24194 	if (li)
24195 		ldi_ident_release(li);
24196 
24197 	crfree(cr);
24198 }
24199 
24200 /*
24201  * This needs to be in-sync with nic_event_t definition
24202  */
24203 static const char *
24204 ill_hook_event2str(nic_event_t event)
24205 {
24206 	switch (event) {
24207 	case NE_PLUMB:
24208 		return ("PLUMB");
24209 	case NE_UNPLUMB:
24210 		return ("UNPLUMB");
24211 	case NE_UP:
24212 		return ("UP");
24213 	case NE_DOWN:
24214 		return ("DOWN");
24215 	case NE_ADDRESS_CHANGE:
24216 		return ("ADDRESS_CHANGE");
24217 	case NE_LIF_UP:
24218 		return ("LIF_UP");
24219 	case NE_LIF_DOWN:
24220 		return ("LIF_DOWN");
24221 	default:
24222 		return ("UNKNOWN");
24223 	}
24224 }
24225 
24226 void
24227 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
24228     nic_event_data_t data, size_t datalen)
24229 {
24230 	ip_stack_t		*ipst = ill->ill_ipst;
24231 	hook_nic_event_int_t	*info;
24232 	const char		*str = NULL;
24233 
24234 	/* create a new nic event info */
24235 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
24236 		goto fail;
24237 
24238 	if (event == NE_UNPLUMB)
24239 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
24240 	else
24241 		info->hnei_event.hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24242 	info->hnei_event.hne_lif = lif;
24243 	info->hnei_event.hne_event = event;
24244 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
24245 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24246 	info->hnei_event.hne_data = NULL;
24247 	info->hnei_event.hne_datalen = 0;
24248 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
24249 
24250 	if (data != NULL && datalen != 0) {
24251 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24252 		if (info->hnei_event.hne_data == NULL)
24253 			goto fail;
24254 		bcopy(data, info->hnei_event.hne_data, datalen);
24255 		info->hnei_event.hne_datalen = datalen;
24256 	}
24257 
24258 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
24259 	    DDI_NOSLEEP) == DDI_SUCCESS)
24260 		return;
24261 
24262 fail:
24263 	if (info != NULL) {
24264 		if (info->hnei_event.hne_data != NULL) {
24265 			kmem_free(info->hnei_event.hne_data,
24266 			    info->hnei_event.hne_datalen);
24267 		}
24268 		kmem_free(info, sizeof (hook_nic_event_t));
24269 	}
24270 	str = ill_hook_event2str(event);
24271 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
24272 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24273 }
24274 
24275 void
24276 ipif_up_notify(ipif_t *ipif)
24277 {
24278 	ip_rts_ifmsg(ipif);
24279 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
24280 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
24281 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
24282 	    NE_LIF_UP, NULL, 0);
24283 }
24284