xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision 4703203d9b3e06246d73931f07359a7ef70f47bf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
135 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
136 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp, boolean_t need_up);
140 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
143     mblk_t *mp, boolean_t need_up);
144 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
145     mblk_t *mp);
146 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
147     queue_t *q, mblk_t *mp, boolean_t need_up);
148 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
149     int ioccmd, struct linkblk *li, boolean_t doconsist);
150 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
151 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
152 static void	ipsq_flush(ill_t *ill);
153 
154 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
155     queue_t *q, mblk_t *mp, boolean_t need_up);
156 static void	ipsq_delete(ipsq_t *);
157 
158 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
159 		    boolean_t initialize);
160 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
161 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
162 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
163 		    boolean_t isv6);
164 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
165 static void	ipif_delete_cache_ire(ire_t *, char *);
166 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
167 static void	ipif_free(ipif_t *ipif);
168 static void	ipif_free_tail(ipif_t *ipif);
169 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
170 static void	ipif_multicast_down(ipif_t *ipif);
171 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
172 static void	ipif_set_default(ipif_t *ipif);
173 static int	ipif_set_values(queue_t *q, mblk_t *mp,
174     char *interf_name, uint_t *ppa);
175 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
176     queue_t *q);
177 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
178     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
179     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
180 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
181 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
182 
183 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
184 static int	ill_arp_off(ill_t *ill);
185 static int	ill_arp_on(ill_t *ill);
186 static void	ill_delete_interface_type(ill_if_t *);
187 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
188 static void	ill_dl_down(ill_t *ill);
189 static void	ill_down(ill_t *ill);
190 static void	ill_downi(ire_t *ire, char *ill_arg);
191 static void	ill_free_mib(ill_t *ill);
192 static void	ill_glist_delete(ill_t *);
193 static boolean_t ill_has_usable_ipif(ill_t *);
194 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
195 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
196 static void	ill_phyint_free(ill_t *ill);
197 static void	ill_phyint_reinit(ill_t *ill);
198 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
199 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
200 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
201 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
202 static void	ill_stq_cache_delete(ire_t *, char *);
203 
204 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
207     in6_addr_t *);
208 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
209     ipaddr_t *);
210 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
211 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
212     in6_addr_t *);
213 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
214     ipaddr_t *);
215 
216 static void	ipif_save_ire(ipif_t *, ire_t *);
217 static void	ipif_remove_ire(ipif_t *, ire_t *);
218 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
219 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
220 
221 /*
222  * Per-ill IPsec capabilities management.
223  */
224 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
225 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
226 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
227 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
228 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
229 static void ill_capability_proto(ill_t *, int, mblk_t *);
230 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
231     boolean_t);
232 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
233 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
235 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
236 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
237 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
239 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
240     dl_capability_sub_t *);
241 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
242 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
243 static void ill_capability_lso_reset(ill_t *, mblk_t **);
244 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
245 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
246 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
247 static void	ill_capability_dls_disable(ill_t *);
248 
249 static void	illgrp_cache_delete(ire_t *, char *);
250 static void	illgrp_delete(ill_t *ill);
251 static void	illgrp_reset_schednext(ill_t *ill);
252 
253 static ill_t	*ill_prev_usesrc(ill_t *);
254 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
255 static void	ill_disband_usesrc_group(ill_t *);
256 
257 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
258 
259 #ifdef DEBUG
260 static	void	ill_trace_cleanup(const ill_t *);
261 static	void	ipif_trace_cleanup(const ipif_t *);
262 #endif
263 
264 /*
265  * if we go over the memory footprint limit more than once in this msec
266  * interval, we'll start pruning aggressively.
267  */
268 int ip_min_frag_prune_time = 0;
269 
270 /*
271  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
272  * and the IPsec DOI
273  */
274 #define	MAX_IPSEC_ALGS	256
275 
276 #define	BITSPERBYTE	8
277 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
278 
279 #define	IPSEC_ALG_ENABLE(algs, algid) \
280 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
281 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
282 
283 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
284 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
285 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
286 
287 typedef uint8_t ipsec_capab_elem_t;
288 
289 /*
290  * Per-algorithm parameters.  Note that at present, only encryption
291  * algorithms have variable keysize (IKE does not provide a way to negotiate
292  * auth algorithm keysize).
293  *
294  * All sizes here are in bits.
295  */
296 typedef struct
297 {
298 	uint16_t	minkeylen;
299 	uint16_t	maxkeylen;
300 } ipsec_capab_algparm_t;
301 
302 /*
303  * Per-ill capabilities.
304  */
305 struct ill_ipsec_capab_s {
306 	ipsec_capab_elem_t *encr_hw_algs;
307 	ipsec_capab_elem_t *auth_hw_algs;
308 	uint32_t algs_size;	/* size of _hw_algs in bytes */
309 	/* algorithm key lengths */
310 	ipsec_capab_algparm_t *encr_algparm;
311 	uint32_t encr_algparm_size;
312 	uint32_t encr_algparm_end;
313 };
314 
315 /*
316  * The field values are larger than strictly necessary for simple
317  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
318  */
319 static area_t	ip_area_template = {
320 	AR_ENTRY_ADD,			/* area_cmd */
321 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
322 					/* area_name_offset */
323 	/* area_name_length temporarily holds this structure length */
324 	sizeof (area_t),			/* area_name_length */
325 	IP_ARP_PROTO_TYPE,		/* area_proto */
326 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
327 	IP_ADDR_LEN,			/* area_proto_addr_length */
328 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
329 					/* area_proto_mask_offset */
330 	0,				/* area_flags */
331 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
332 					/* area_hw_addr_offset */
333 	/* Zero length hw_addr_length means 'use your idea of the address' */
334 	0				/* area_hw_addr_length */
335 };
336 
337 /*
338  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
339  * support
340  */
341 static area_t	ip6_area_template = {
342 	AR_ENTRY_ADD,			/* area_cmd */
343 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
344 					/* area_name_offset */
345 	/* area_name_length temporarily holds this structure length */
346 	sizeof (area_t),			/* area_name_length */
347 	IP_ARP_PROTO_TYPE,		/* area_proto */
348 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
349 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
350 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
351 					/* area_proto_mask_offset */
352 	0,				/* area_flags */
353 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
354 					/* area_hw_addr_offset */
355 	/* Zero length hw_addr_length means 'use your idea of the address' */
356 	0				/* area_hw_addr_length */
357 };
358 
359 static ared_t	ip_ared_template = {
360 	AR_ENTRY_DELETE,
361 	sizeof (ared_t) + IP_ADDR_LEN,
362 	sizeof (ared_t),
363 	IP_ARP_PROTO_TYPE,
364 	sizeof (ared_t),
365 	IP_ADDR_LEN
366 };
367 
368 static ared_t	ip6_ared_template = {
369 	AR_ENTRY_DELETE,
370 	sizeof (ared_t) + IPV6_ADDR_LEN,
371 	sizeof (ared_t),
372 	IP_ARP_PROTO_TYPE,
373 	sizeof (ared_t),
374 	IPV6_ADDR_LEN
375 };
376 
377 /*
378  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
379  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
380  * areq is used).
381  */
382 static areq_t	ip_areq_template = {
383 	AR_ENTRY_QUERY,			/* cmd */
384 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
385 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
386 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
387 	sizeof (areq_t),			/* target addr offset */
388 	IP_ADDR_LEN,			/* target addr_length */
389 	0,				/* flags */
390 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
391 	IP_ADDR_LEN,			/* sender addr length */
392 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
393 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
394 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
395 	/* anything else filled in by the code */
396 };
397 
398 static arc_t	ip_aru_template = {
399 	AR_INTERFACE_UP,
400 	sizeof (arc_t),		/* Name offset */
401 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
402 };
403 
404 static arc_t	ip_ard_template = {
405 	AR_INTERFACE_DOWN,
406 	sizeof (arc_t),		/* Name offset */
407 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
408 };
409 
410 static arc_t	ip_aron_template = {
411 	AR_INTERFACE_ON,
412 	sizeof (arc_t),		/* Name offset */
413 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
414 };
415 
416 static arc_t	ip_aroff_template = {
417 	AR_INTERFACE_OFF,
418 	sizeof (arc_t),		/* Name offset */
419 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
420 };
421 
422 
423 static arma_t	ip_arma_multi_template = {
424 	AR_MAPPING_ADD,
425 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
426 				/* Name offset */
427 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
428 	IP_ARP_PROTO_TYPE,
429 	sizeof (arma_t),			/* proto_addr_offset */
430 	IP_ADDR_LEN,				/* proto_addr_length */
431 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
432 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
433 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
434 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
435 	IP_MAX_HW_LEN,				/* hw_addr_length */
436 	0,					/* hw_mapping_start */
437 };
438 
439 static ipft_t	ip_ioctl_ftbl[] = {
440 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
441 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
442 		IPFT_F_NO_REPLY },
443 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
444 		IPFT_F_NO_REPLY },
445 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
446 	{ 0 }
447 };
448 
449 /* Simple ICMP IP Header Template */
450 static ipha_t icmp_ipha = {
451 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
452 };
453 
454 /* Flag descriptors for ip_ipif_report */
455 static nv_t	ipif_nv_tbl[] = {
456 	{ IPIF_UP,		"UP" },
457 	{ IPIF_BROADCAST,	"BROADCAST" },
458 	{ ILLF_DEBUG,		"DEBUG" },
459 	{ PHYI_LOOPBACK,	"LOOPBACK" },
460 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
461 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
462 	{ PHYI_RUNNING,		"RUNNING" },
463 	{ ILLF_NOARP,		"NOARP" },
464 	{ PHYI_PROMISC,		"PROMISC" },
465 	{ PHYI_ALLMULTI,	"ALLMULTI" },
466 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
467 	{ ILLF_MULTICAST,	"MULTICAST" },
468 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
469 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
470 	{ IPIF_DHCPRUNNING,	"DHCP" },
471 	{ IPIF_PRIVATE,		"PRIVATE" },
472 	{ IPIF_NOXMIT,		"NOXMIT" },
473 	{ IPIF_NOLOCAL,		"NOLOCAL" },
474 	{ IPIF_DEPRECATED,	"DEPRECATED" },
475 	{ IPIF_PREFERRED,	"PREFERRED" },
476 	{ IPIF_TEMPORARY,	"TEMPORARY" },
477 	{ IPIF_ADDRCONF,	"ADDRCONF" },
478 	{ PHYI_VIRTUAL,		"VIRTUAL" },
479 	{ ILLF_ROUTER,		"ROUTER" },
480 	{ ILLF_NONUD,		"NONUD" },
481 	{ IPIF_ANYCAST,		"ANYCAST" },
482 	{ ILLF_NORTEXCH,	"NORTEXCH" },
483 	{ ILLF_IPV4,		"IPV4" },
484 	{ ILLF_IPV6,		"IPV6" },
485 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
486 	{ PHYI_FAILED,		"FAILED" },
487 	{ PHYI_STANDBY,		"STANDBY" },
488 	{ PHYI_INACTIVE,	"INACTIVE" },
489 	{ PHYI_OFFLINE,		"OFFLINE" },
490 };
491 
492 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
493 
494 static ip_m_t	ip_m_tbl[] = {
495 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
496 	    ip_ether_v6intfid },
497 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_nodef_v6intfid },
499 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_ether_v6intfid },
505 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
506 	    ip_ib_v6intfid },
507 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
508 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
509 	    ip_nodef_v6intfid }
510 };
511 
512 static ill_t	ill_null;		/* Empty ILL for init. */
513 char	ipif_loopback_name[] = "lo0";
514 static char *ipv4_forward_suffix = ":ip_forwarding";
515 static char *ipv6_forward_suffix = ":ip6_forwarding";
516 static	sin6_t	sin6_null;	/* Zero address for quick clears */
517 static	sin_t	sin_null;	/* Zero address for quick clears */
518 
519 /* When set search for unused ipif_seqid */
520 static ipif_t	ipif_zero;
521 
522 /*
523  * ppa arena is created after these many
524  * interfaces have been plumbed.
525  */
526 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
527 
528 /*
529  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
530  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
531  * set through platform specific code (Niagara/Ontario).
532  */
533 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
534 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
535 
536 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
537 
538 static uint_t
539 ipif_rand(ip_stack_t *ipst)
540 {
541 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
542 	    12345;
543 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
544 }
545 
546 /*
547  * Allocate per-interface mibs.
548  * Returns true if ok. False otherwise.
549  *  ipsq  may not yet be allocated (loopback case ).
550  */
551 static boolean_t
552 ill_allocate_mibs(ill_t *ill)
553 {
554 	/* Already allocated? */
555 	if (ill->ill_ip_mib != NULL) {
556 		if (ill->ill_isv6)
557 			ASSERT(ill->ill_icmp6_mib != NULL);
558 		return (B_TRUE);
559 	}
560 
561 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
562 	    KM_NOSLEEP);
563 	if (ill->ill_ip_mib == NULL) {
564 		return (B_FALSE);
565 	}
566 
567 	/* Setup static information */
568 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
569 	    sizeof (mib2_ipIfStatsEntry_t));
570 	if (ill->ill_isv6) {
571 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
572 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
573 		    sizeof (mib2_ipv6AddrEntry_t));
574 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
575 		    sizeof (mib2_ipv6RouteEntry_t));
576 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
577 		    sizeof (mib2_ipv6NetToMediaEntry_t));
578 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
579 		    sizeof (ipv6_member_t));
580 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
581 		    sizeof (ipv6_grpsrc_t));
582 	} else {
583 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
584 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
585 		    sizeof (mib2_ipAddrEntry_t));
586 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
587 		    sizeof (mib2_ipRouteEntry_t));
588 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
589 		    sizeof (mib2_ipNetToMediaEntry_t));
590 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
591 		    sizeof (ip_member_t));
592 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
593 		    sizeof (ip_grpsrc_t));
594 
595 		/*
596 		 * For a v4 ill, we are done at this point, because per ill
597 		 * icmp mibs are only used for v6.
598 		 */
599 		return (B_TRUE);
600 	}
601 
602 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
603 	    KM_NOSLEEP);
604 	if (ill->ill_icmp6_mib == NULL) {
605 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
606 		ill->ill_ip_mib = NULL;
607 		return (B_FALSE);
608 	}
609 	/* static icmp info */
610 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
611 	    sizeof (mib2_ipv6IfIcmpEntry_t);
612 	/*
613 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
614 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
615 	 * -> ill_phyint_reinit
616 	 */
617 	return (B_TRUE);
618 }
619 
620 /*
621  * Common code for preparation of ARP commands.  Two points to remember:
622  * 	1) The ill_name is tacked on at the end of the allocated space so
623  *	   the templates name_offset field must contain the total space
624  *	   to allocate less the name length.
625  *
626  *	2) The templates name_length field should contain the *template*
627  *	   length.  We use it as a parameter to bcopy() and then write
628  *	   the real ill_name_length into the name_length field of the copy.
629  * (Always called as writer.)
630  */
631 mblk_t *
632 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
633 {
634 	arc_t	*arc = (arc_t *)template;
635 	char	*cp;
636 	int	len;
637 	mblk_t	*mp;
638 	uint_t	name_length = ill->ill_name_length;
639 	uint_t	template_len = arc->arc_name_length;
640 
641 	len = arc->arc_name_offset + name_length;
642 	mp = allocb(len, BPRI_HI);
643 	if (mp == NULL)
644 		return (NULL);
645 	cp = (char *)mp->b_rptr;
646 	mp->b_wptr = (uchar_t *)&cp[len];
647 	if (template_len)
648 		bcopy(template, cp, template_len);
649 	if (len > template_len)
650 		bzero(&cp[template_len], len - template_len);
651 	mp->b_datap->db_type = M_PROTO;
652 
653 	arc = (arc_t *)cp;
654 	arc->arc_name_length = name_length;
655 	cp = (char *)arc + arc->arc_name_offset;
656 	bcopy(ill->ill_name, cp, name_length);
657 
658 	if (addr) {
659 		area_t	*area = (area_t *)mp->b_rptr;
660 
661 		cp = (char *)area + area->area_proto_addr_offset;
662 		bcopy(addr, cp, area->area_proto_addr_length);
663 		if (area->area_cmd == AR_ENTRY_ADD) {
664 			cp = (char *)area;
665 			len = area->area_proto_addr_length;
666 			if (area->area_proto_mask_offset)
667 				cp += area->area_proto_mask_offset;
668 			else
669 				cp += area->area_proto_addr_offset + len;
670 			while (len-- > 0)
671 				*cp++ = (char)~0;
672 		}
673 	}
674 	return (mp);
675 }
676 
677 mblk_t *
678 ipif_area_alloc(ipif_t *ipif)
679 {
680 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
681 	    (char *)&ipif->ipif_lcl_addr));
682 }
683 
684 mblk_t *
685 ipif_ared_alloc(ipif_t *ipif)
686 {
687 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
688 	    (char *)&ipif->ipif_lcl_addr));
689 }
690 
691 mblk_t *
692 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
693 {
694 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
695 	    (char *)&addr));
696 }
697 
698 /*
699  * Completely vaporize a lower level tap and all associated interfaces.
700  * ill_delete is called only out of ip_close when the device control
701  * stream is being closed.
702  */
703 void
704 ill_delete(ill_t *ill)
705 {
706 	ipif_t	*ipif;
707 	ill_t	*prev_ill;
708 	ip_stack_t	*ipst = ill->ill_ipst;
709 
710 	/*
711 	 * ill_delete may be forcibly entering the ipsq. The previous
712 	 * ioctl may not have completed and may need to be aborted.
713 	 * ipsq_flush takes care of it. If we don't need to enter the
714 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
715 	 * ill_delete_tail is sufficient.
716 	 */
717 	ipsq_flush(ill);
718 
719 	/*
720 	 * Nuke all interfaces.  ipif_free will take down the interface,
721 	 * remove it from the list, and free the data structure.
722 	 * Walk down the ipif list and remove the logical interfaces
723 	 * first before removing the main ipif. We can't unplumb
724 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
725 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
726 	 * POINTOPOINT.
727 	 *
728 	 * If ill_ipif was not properly initialized (i.e low on memory),
729 	 * then no interfaces to clean up. In this case just clean up the
730 	 * ill.
731 	 */
732 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
733 		ipif_free(ipif);
734 
735 	/*
736 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
737 	 * So nobody can be using this mp now. Free the mp allocated for
738 	 * honoring ILLF_NOARP
739 	 */
740 	freemsg(ill->ill_arp_on_mp);
741 	ill->ill_arp_on_mp = NULL;
742 
743 	/* Clean up msgs on pending upcalls for mrouted */
744 	reset_mrt_ill(ill);
745 
746 	/*
747 	 * ipif_free -> reset_conn_ipif will remove all multicast
748 	 * references for IPv4. For IPv6, we need to do it here as
749 	 * it points only at ills.
750 	 */
751 	reset_conn_ill(ill);
752 
753 	/*
754 	 * ill_down will arrange to blow off any IRE's dependent on this
755 	 * ILL, and shut down fragmentation reassembly.
756 	 */
757 	ill_down(ill);
758 
759 	/* Let SCTP know, so that it can remove this from its list. */
760 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
761 
762 	/*
763 	 * If an address on this ILL is being used as a source address then
764 	 * clear out the pointers in other ILLs that point to this ILL.
765 	 */
766 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
767 	if (ill->ill_usesrc_grp_next != NULL) {
768 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
769 			ill_disband_usesrc_group(ill);
770 		} else {	/* consumer of the usesrc ILL */
771 			prev_ill = ill_prev_usesrc(ill);
772 			prev_ill->ill_usesrc_grp_next =
773 			    ill->ill_usesrc_grp_next;
774 		}
775 	}
776 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
777 }
778 
779 static void
780 ipif_non_duplicate(ipif_t *ipif)
781 {
782 	ill_t *ill = ipif->ipif_ill;
783 	mutex_enter(&ill->ill_lock);
784 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
785 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
786 		ASSERT(ill->ill_ipif_dup_count > 0);
787 		ill->ill_ipif_dup_count--;
788 	}
789 	mutex_exit(&ill->ill_lock);
790 }
791 
792 /*
793  * ill_delete_tail is called from ip_modclose after all references
794  * to the closing ill are gone. The wait is done in ip_modclose
795  */
796 void
797 ill_delete_tail(ill_t *ill)
798 {
799 	mblk_t	**mpp;
800 	ipif_t	*ipif;
801 	ip_stack_t	*ipst = ill->ill_ipst;
802 
803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
804 		ipif_non_duplicate(ipif);
805 		ipif_down_tail(ipif);
806 	}
807 
808 	ASSERT(ill->ill_ipif_dup_count == 0 &&
809 	    ill->ill_arp_down_mp == NULL &&
810 	    ill->ill_arp_del_mapping_mp == NULL);
811 
812 	/*
813 	 * If polling capability is enabled (which signifies direct
814 	 * upcall into IP and driver has ill saved as a handle),
815 	 * we need to make sure that unbind has completed before we
816 	 * let the ill disappear and driver no longer has any reference
817 	 * to this ill.
818 	 */
819 	mutex_enter(&ill->ill_lock);
820 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
821 		cv_wait(&ill->ill_cv, &ill->ill_lock);
822 	mutex_exit(&ill->ill_lock);
823 
824 	/*
825 	 * Clean up polling and soft ring capabilities
826 	 */
827 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
828 		ill_capability_dls_disable(ill);
829 
830 	if (ill->ill_net_type != IRE_LOOPBACK)
831 		qprocsoff(ill->ill_rq);
832 
833 	/*
834 	 * We do an ipsq_flush once again now. New messages could have
835 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
836 	 * could also have landed up if an ioctl thread had looked up
837 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
838 	 * enqueued the ioctl when we did the ipsq_flush last time.
839 	 */
840 	ipsq_flush(ill);
841 
842 	/*
843 	 * Free capabilities.
844 	 */
845 	if (ill->ill_ipsec_capab_ah != NULL) {
846 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
847 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
848 		ill->ill_ipsec_capab_ah = NULL;
849 	}
850 
851 	if (ill->ill_ipsec_capab_esp != NULL) {
852 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
853 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
854 		ill->ill_ipsec_capab_esp = NULL;
855 	}
856 
857 	if (ill->ill_mdt_capab != NULL) {
858 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
859 		ill->ill_mdt_capab = NULL;
860 	}
861 
862 	if (ill->ill_hcksum_capab != NULL) {
863 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
864 		ill->ill_hcksum_capab = NULL;
865 	}
866 
867 	if (ill->ill_zerocopy_capab != NULL) {
868 		kmem_free(ill->ill_zerocopy_capab,
869 		    sizeof (ill_zerocopy_capab_t));
870 		ill->ill_zerocopy_capab = NULL;
871 	}
872 
873 	if (ill->ill_lso_capab != NULL) {
874 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
875 		ill->ill_lso_capab = NULL;
876 	}
877 
878 	if (ill->ill_dls_capab != NULL) {
879 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
880 		ill->ill_dls_capab->ill_unbind_conn = NULL;
881 		kmem_free(ill->ill_dls_capab,
882 		    sizeof (ill_dls_capab_t) +
883 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
884 		ill->ill_dls_capab = NULL;
885 	}
886 
887 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
888 
889 	while (ill->ill_ipif != NULL)
890 		ipif_free_tail(ill->ill_ipif);
891 
892 	/*
893 	 * We have removed all references to ilm from conn and the ones joined
894 	 * within the kernel.
895 	 *
896 	 * We don't walk conns, mrts and ires because
897 	 *
898 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
899 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
900 	 *    ill references.
901 	 */
902 	ASSERT(ilm_walk_ill(ill) == 0);
903 	/*
904 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
905 	 * could free the phyint. No more reference to the phyint after this
906 	 * point.
907 	 */
908 	(void) ill_glist_delete(ill);
909 
910 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
911 	if (ill->ill_ndd_name != NULL)
912 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
913 	rw_exit(&ipst->ips_ip_g_nd_lock);
914 
915 
916 	if (ill->ill_frag_ptr != NULL) {
917 		uint_t count;
918 
919 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
920 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
921 		}
922 		mi_free(ill->ill_frag_ptr);
923 		ill->ill_frag_ptr = NULL;
924 		ill->ill_frag_hash_tbl = NULL;
925 	}
926 
927 	freemsg(ill->ill_nd_lla_mp);
928 	/* Free all retained control messages. */
929 	mpp = &ill->ill_first_mp_to_free;
930 	do {
931 		while (mpp[0]) {
932 			mblk_t  *mp;
933 			mblk_t  *mp1;
934 
935 			mp = mpp[0];
936 			mpp[0] = mp->b_next;
937 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
938 				mp1->b_next = NULL;
939 				mp1->b_prev = NULL;
940 			}
941 			freemsg(mp);
942 		}
943 	} while (mpp++ != &ill->ill_last_mp_to_free);
944 
945 	ill_free_mib(ill);
946 
947 #ifdef DEBUG
948 	ill_trace_cleanup(ill);
949 #endif
950 
951 	/* Drop refcnt here */
952 	netstack_rele(ill->ill_ipst->ips_netstack);
953 	ill->ill_ipst = NULL;
954 }
955 
956 static void
957 ill_free_mib(ill_t *ill)
958 {
959 	ip_stack_t *ipst = ill->ill_ipst;
960 
961 	/*
962 	 * MIB statistics must not be lost, so when an interface
963 	 * goes away the counter values will be added to the global
964 	 * MIBs.
965 	 */
966 	if (ill->ill_ip_mib != NULL) {
967 		if (ill->ill_isv6) {
968 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
969 			    ill->ill_ip_mib);
970 		} else {
971 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
972 			    ill->ill_ip_mib);
973 		}
974 
975 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
976 		ill->ill_ip_mib = NULL;
977 	}
978 	if (ill->ill_icmp6_mib != NULL) {
979 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
980 		    ill->ill_icmp6_mib);
981 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
982 		ill->ill_icmp6_mib = NULL;
983 	}
984 }
985 
986 /*
987  * Concatenate together a physical address and a sap.
988  *
989  * Sap_lengths are interpreted as follows:
990  *   sap_length == 0	==>	no sap
991  *   sap_length > 0	==>	sap is at the head of the dlpi address
992  *   sap_length < 0	==>	sap is at the tail of the dlpi address
993  */
994 static void
995 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
996     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
997 {
998 	uint16_t sap_addr = (uint16_t)sap_src;
999 
1000 	if (sap_length == 0) {
1001 		if (phys_src == NULL)
1002 			bzero(dst, phys_length);
1003 		else
1004 			bcopy(phys_src, dst, phys_length);
1005 	} else if (sap_length < 0) {
1006 		if (phys_src == NULL)
1007 			bzero(dst, phys_length);
1008 		else
1009 			bcopy(phys_src, dst, phys_length);
1010 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1011 	} else {
1012 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1013 		if (phys_src == NULL)
1014 			bzero((char *)dst + sap_length, phys_length);
1015 		else
1016 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1017 	}
1018 }
1019 
1020 /*
1021  * Generate a dl_unitdata_req mblk for the device and address given.
1022  * addr_length is the length of the physical portion of the address.
1023  * If addr is NULL include an all zero address of the specified length.
1024  * TRUE? In any case, addr_length is taken to be the entire length of the
1025  * dlpi address, including the absolute value of sap_length.
1026  */
1027 mblk_t *
1028 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1029 		t_scalar_t sap_length)
1030 {
1031 	dl_unitdata_req_t *dlur;
1032 	mblk_t	*mp;
1033 	t_scalar_t	abs_sap_length;		/* absolute value */
1034 
1035 	abs_sap_length = ABS(sap_length);
1036 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1037 	    DL_UNITDATA_REQ);
1038 	if (mp == NULL)
1039 		return (NULL);
1040 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1041 	/* HACK: accomodate incompatible DLPI drivers */
1042 	if (addr_length == 8)
1043 		addr_length = 6;
1044 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1045 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1046 	dlur->dl_priority.dl_min = 0;
1047 	dlur->dl_priority.dl_max = 0;
1048 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1049 	    (uchar_t *)&dlur[1]);
1050 	return (mp);
1051 }
1052 
1053 /*
1054  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1055  * Return an error if we already have 1 or more ioctls in progress.
1056  * This is used only for non-exclusive ioctls. Currently this is used
1057  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1058  * and thus need to use ipsq_pending_mp_add.
1059  */
1060 boolean_t
1061 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1062 {
1063 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1064 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1065 	/*
1066 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1067 	 */
1068 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1069 	    (add_mp->b_datap->db_type == M_IOCTL));
1070 
1071 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1072 	/*
1073 	 * Return error if the conn has started closing. The conn
1074 	 * could have finished cleaning up the pending mp list,
1075 	 * If so we should not add another mp to the list negating
1076 	 * the cleanup.
1077 	 */
1078 	if (connp->conn_state_flags & CONN_CLOSING)
1079 		return (B_FALSE);
1080 	/*
1081 	 * Add the pending mp to the head of the list, chained by b_next.
1082 	 * Note down the conn on which the ioctl request came, in b_prev.
1083 	 * This will be used to later get the conn, when we get a response
1084 	 * on the ill queue, from some other module (typically arp)
1085 	 */
1086 	add_mp->b_next = (void *)ill->ill_pending_mp;
1087 	add_mp->b_queue = CONNP_TO_WQ(connp);
1088 	ill->ill_pending_mp = add_mp;
1089 	if (connp != NULL)
1090 		connp->conn_oper_pending_ill = ill;
1091 	return (B_TRUE);
1092 }
1093 
1094 /*
1095  * Retrieve the ill_pending_mp and return it. We have to walk the list
1096  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1097  */
1098 mblk_t *
1099 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1100 {
1101 	mblk_t	*prev = NULL;
1102 	mblk_t	*curr = NULL;
1103 	uint_t	id;
1104 	conn_t	*connp;
1105 
1106 	/*
1107 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1108 	 * up the pending mp, but it does not know the ioc_id and
1109 	 * passes in a zero for it.
1110 	 */
1111 	mutex_enter(&ill->ill_lock);
1112 	if (ioc_id != 0)
1113 		*connpp = NULL;
1114 
1115 	/* Search the list for the appropriate ioctl based on ioc_id */
1116 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1117 	    prev = curr, curr = curr->b_next) {
1118 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1119 		connp = Q_TO_CONN(curr->b_queue);
1120 		/* Match based on the ioc_id or based on the conn */
1121 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1122 			break;
1123 	}
1124 
1125 	if (curr != NULL) {
1126 		/* Unlink the mblk from the pending mp list */
1127 		if (prev != NULL) {
1128 			prev->b_next = curr->b_next;
1129 		} else {
1130 			ASSERT(ill->ill_pending_mp == curr);
1131 			ill->ill_pending_mp = curr->b_next;
1132 		}
1133 
1134 		/*
1135 		 * conn refcnt must have been bumped up at the start of
1136 		 * the ioctl. So we can safely access the conn.
1137 		 */
1138 		ASSERT(CONN_Q(curr->b_queue));
1139 		*connpp = Q_TO_CONN(curr->b_queue);
1140 		curr->b_next = NULL;
1141 		curr->b_queue = NULL;
1142 	}
1143 
1144 	mutex_exit(&ill->ill_lock);
1145 
1146 	return (curr);
1147 }
1148 
1149 /*
1150  * Add the pending mp to the list. There can be only 1 pending mp
1151  * in the list. Any exclusive ioctl that needs to wait for a response
1152  * from another module or driver needs to use this function to set
1153  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1154  * the other module/driver. This is also used while waiting for the
1155  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1156  */
1157 boolean_t
1158 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1159     int waitfor)
1160 {
1161 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1162 
1163 	ASSERT(IAM_WRITER_IPIF(ipif));
1164 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1165 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1166 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1167 	/*
1168 	 * The caller may be using a different ipif than the one passed into
1169 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1170 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1171 	 * that `ipsq_current_ipif == ipif'.
1172 	 */
1173 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1174 
1175 	/*
1176 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1177 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1178 	 */
1179 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1180 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1181 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1182 
1183 	if (connp != NULL) {
1184 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1185 		/*
1186 		 * Return error if the conn has started closing. The conn
1187 		 * could have finished cleaning up the pending mp list,
1188 		 * If so we should not add another mp to the list negating
1189 		 * the cleanup.
1190 		 */
1191 		if (connp->conn_state_flags & CONN_CLOSING)
1192 			return (B_FALSE);
1193 	}
1194 	mutex_enter(&ipsq->ipsq_lock);
1195 	ipsq->ipsq_pending_ipif = ipif;
1196 	/*
1197 	 * Note down the queue in b_queue. This will be returned by
1198 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1199 	 * the processing
1200 	 */
1201 	add_mp->b_next = NULL;
1202 	add_mp->b_queue = q;
1203 	ipsq->ipsq_pending_mp = add_mp;
1204 	ipsq->ipsq_waitfor = waitfor;
1205 
1206 	if (connp != NULL)
1207 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1208 	mutex_exit(&ipsq->ipsq_lock);
1209 	return (B_TRUE);
1210 }
1211 
1212 /*
1213  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1214  * queued in the list.
1215  */
1216 mblk_t *
1217 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1218 {
1219 	mblk_t	*curr = NULL;
1220 
1221 	mutex_enter(&ipsq->ipsq_lock);
1222 	*connpp = NULL;
1223 	if (ipsq->ipsq_pending_mp == NULL) {
1224 		mutex_exit(&ipsq->ipsq_lock);
1225 		return (NULL);
1226 	}
1227 
1228 	/* There can be only 1 such excl message */
1229 	curr = ipsq->ipsq_pending_mp;
1230 	ASSERT(curr != NULL && curr->b_next == NULL);
1231 	ipsq->ipsq_pending_ipif = NULL;
1232 	ipsq->ipsq_pending_mp = NULL;
1233 	ipsq->ipsq_waitfor = 0;
1234 	mutex_exit(&ipsq->ipsq_lock);
1235 
1236 	if (CONN_Q(curr->b_queue)) {
1237 		/*
1238 		 * This mp did a refhold on the conn, at the start of the ioctl.
1239 		 * So we can safely return a pointer to the conn to the caller.
1240 		 */
1241 		*connpp = Q_TO_CONN(curr->b_queue);
1242 	} else {
1243 		*connpp = NULL;
1244 	}
1245 	curr->b_next = NULL;
1246 	curr->b_prev = NULL;
1247 	return (curr);
1248 }
1249 
1250 /*
1251  * Cleanup the ioctl mp queued in ipsq_pending_mp
1252  * - Called in the ill_delete path
1253  * - Called in the M_ERROR or M_HANGUP path on the ill.
1254  * - Called in the conn close path.
1255  */
1256 boolean_t
1257 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1258 {
1259 	mblk_t	*mp;
1260 	ipsq_t	*ipsq;
1261 	queue_t	*q;
1262 	ipif_t	*ipif;
1263 
1264 	ASSERT(IAM_WRITER_ILL(ill));
1265 	ipsq = ill->ill_phyint->phyint_ipsq;
1266 	mutex_enter(&ipsq->ipsq_lock);
1267 	/*
1268 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1269 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1270 	 * even if it is meant for another ill, since we have to enqueue
1271 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1272 	 * If connp is non-null we are called from the conn close path.
1273 	 */
1274 	mp = ipsq->ipsq_pending_mp;
1275 	if (mp == NULL || (connp != NULL &&
1276 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1277 		mutex_exit(&ipsq->ipsq_lock);
1278 		return (B_FALSE);
1279 	}
1280 	/* Now remove from the ipsq_pending_mp */
1281 	ipsq->ipsq_pending_mp = NULL;
1282 	q = mp->b_queue;
1283 	mp->b_next = NULL;
1284 	mp->b_prev = NULL;
1285 	mp->b_queue = NULL;
1286 
1287 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1288 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1289 	if (ill->ill_move_in_progress) {
1290 		ILL_CLEAR_MOVE(ill);
1291 	} else if (ill->ill_up_ipifs) {
1292 		ill_group_cleanup(ill);
1293 	}
1294 
1295 	ipif = ipsq->ipsq_pending_ipif;
1296 	ipsq->ipsq_pending_ipif = NULL;
1297 	ipsq->ipsq_waitfor = 0;
1298 	ipsq->ipsq_current_ipif = NULL;
1299 	ipsq->ipsq_current_ioctl = 0;
1300 	mutex_exit(&ipsq->ipsq_lock);
1301 
1302 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1303 		if (connp == NULL) {
1304 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1305 		} else {
1306 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1307 			mutex_enter(&ipif->ipif_ill->ill_lock);
1308 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1309 			mutex_exit(&ipif->ipif_ill->ill_lock);
1310 		}
1311 	} else {
1312 		/*
1313 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1314 		 * be just inet_freemsg. we have to restart it
1315 		 * otherwise the thread will be stuck.
1316 		 */
1317 		inet_freemsg(mp);
1318 	}
1319 	return (B_TRUE);
1320 }
1321 
1322 /*
1323  * The ill is closing. Cleanup all the pending mps. Called exclusively
1324  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1325  * knows this ill, and hence nobody can add an mp to this list
1326  */
1327 static void
1328 ill_pending_mp_cleanup(ill_t *ill)
1329 {
1330 	mblk_t	*mp;
1331 	queue_t	*q;
1332 
1333 	ASSERT(IAM_WRITER_ILL(ill));
1334 
1335 	mutex_enter(&ill->ill_lock);
1336 	/*
1337 	 * Every mp on the pending mp list originating from an ioctl
1338 	 * added 1 to the conn refcnt, at the start of the ioctl.
1339 	 * So bump it down now.  See comments in ip_wput_nondata()
1340 	 */
1341 	while (ill->ill_pending_mp != NULL) {
1342 		mp = ill->ill_pending_mp;
1343 		ill->ill_pending_mp = mp->b_next;
1344 		mutex_exit(&ill->ill_lock);
1345 
1346 		q = mp->b_queue;
1347 		ASSERT(CONN_Q(q));
1348 		mp->b_next = NULL;
1349 		mp->b_prev = NULL;
1350 		mp->b_queue = NULL;
1351 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1352 		mutex_enter(&ill->ill_lock);
1353 	}
1354 	ill->ill_pending_ipif = NULL;
1355 
1356 	mutex_exit(&ill->ill_lock);
1357 }
1358 
1359 /*
1360  * Called in the conn close path and ill delete path
1361  */
1362 static void
1363 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1364 {
1365 	ipsq_t	*ipsq;
1366 	mblk_t	*prev;
1367 	mblk_t	*curr;
1368 	mblk_t	*next;
1369 	queue_t	*q;
1370 	mblk_t	*tmp_list = NULL;
1371 
1372 	ASSERT(IAM_WRITER_ILL(ill));
1373 	if (connp != NULL)
1374 		q = CONNP_TO_WQ(connp);
1375 	else
1376 		q = ill->ill_wq;
1377 
1378 	ipsq = ill->ill_phyint->phyint_ipsq;
1379 	/*
1380 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1381 	 * In the case of ioctl from a conn, there can be only 1 mp
1382 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1383 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1384 	 * ioctls meant for this ill form conn's are not flushed. They will
1385 	 * be processed during ipsq_exit and will not find the ill and will
1386 	 * return error.
1387 	 */
1388 	mutex_enter(&ipsq->ipsq_lock);
1389 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1390 	    curr = next) {
1391 		next = curr->b_next;
1392 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1393 			/* Unlink the mblk from the pending mp list */
1394 			if (prev != NULL) {
1395 				prev->b_next = curr->b_next;
1396 			} else {
1397 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1398 				ipsq->ipsq_xopq_mphead = curr->b_next;
1399 			}
1400 			if (ipsq->ipsq_xopq_mptail == curr)
1401 				ipsq->ipsq_xopq_mptail = prev;
1402 			/*
1403 			 * Create a temporary list and release the ipsq lock
1404 			 * New elements are added to the head of the tmp_list
1405 			 */
1406 			curr->b_next = tmp_list;
1407 			tmp_list = curr;
1408 		} else {
1409 			prev = curr;
1410 		}
1411 	}
1412 	mutex_exit(&ipsq->ipsq_lock);
1413 
1414 	while (tmp_list != NULL) {
1415 		curr = tmp_list;
1416 		tmp_list = curr->b_next;
1417 		curr->b_next = NULL;
1418 		curr->b_prev = NULL;
1419 		curr->b_queue = NULL;
1420 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1421 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1422 			    CONN_CLOSE : NO_COPYOUT, NULL);
1423 		} else {
1424 			/*
1425 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1426 			 * this can't be just inet_freemsg. we have to
1427 			 * restart it otherwise the thread will be stuck.
1428 			 */
1429 			inet_freemsg(curr);
1430 		}
1431 	}
1432 }
1433 
1434 /*
1435  * This conn has started closing. Cleanup any pending ioctl from this conn.
1436  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1437  */
1438 void
1439 conn_ioctl_cleanup(conn_t *connp)
1440 {
1441 	mblk_t *curr;
1442 	ipsq_t	*ipsq;
1443 	ill_t	*ill;
1444 	boolean_t refheld;
1445 
1446 	/*
1447 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1448 	 * ioctl has not yet started, the mp is pending in the list headed by
1449 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1450 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1451 	 * is currently executing now the mp is not queued anywhere but
1452 	 * conn_oper_pending_ill is null. The conn close will wait
1453 	 * till the conn_ref drops to zero.
1454 	 */
1455 	mutex_enter(&connp->conn_lock);
1456 	ill = connp->conn_oper_pending_ill;
1457 	if (ill == NULL) {
1458 		mutex_exit(&connp->conn_lock);
1459 		return;
1460 	}
1461 
1462 	curr = ill_pending_mp_get(ill, &connp, 0);
1463 	if (curr != NULL) {
1464 		mutex_exit(&connp->conn_lock);
1465 		CONN_DEC_REF(connp);
1466 		inet_freemsg(curr);
1467 		return;
1468 	}
1469 	/*
1470 	 * We may not be able to refhold the ill if the ill/ipif
1471 	 * is changing. But we need to make sure that the ill will
1472 	 * not vanish. So we just bump up the ill_waiter count.
1473 	 */
1474 	refheld = ill_waiter_inc(ill);
1475 	mutex_exit(&connp->conn_lock);
1476 	if (refheld) {
1477 		if (ipsq_enter(ill, B_TRUE)) {
1478 			ill_waiter_dcr(ill);
1479 			/*
1480 			 * Check whether this ioctl has started and is
1481 			 * pending now in ipsq_pending_mp. If it is not
1482 			 * found there then check whether this ioctl has
1483 			 * not even started and is in the ipsq_xopq list.
1484 			 */
1485 			if (!ipsq_pending_mp_cleanup(ill, connp))
1486 				ipsq_xopq_mp_cleanup(ill, connp);
1487 			ipsq = ill->ill_phyint->phyint_ipsq;
1488 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1489 			return;
1490 		}
1491 	}
1492 
1493 	/*
1494 	 * The ill is also closing and we could not bump up the
1495 	 * ill_waiter_count or we could not enter the ipsq. Leave
1496 	 * the cleanup to ill_delete
1497 	 */
1498 	mutex_enter(&connp->conn_lock);
1499 	while (connp->conn_oper_pending_ill != NULL)
1500 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1501 	mutex_exit(&connp->conn_lock);
1502 	if (refheld)
1503 		ill_waiter_dcr(ill);
1504 }
1505 
1506 /*
1507  * ipcl_walk function for cleaning up conn_*_ill fields.
1508  */
1509 static void
1510 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1511 {
1512 	ill_t	*ill = (ill_t *)arg;
1513 	ire_t	*ire;
1514 
1515 	mutex_enter(&connp->conn_lock);
1516 	if (connp->conn_multicast_ill == ill) {
1517 		/* Revert to late binding */
1518 		connp->conn_multicast_ill = NULL;
1519 		connp->conn_orig_multicast_ifindex = 0;
1520 	}
1521 	if (connp->conn_incoming_ill == ill)
1522 		connp->conn_incoming_ill = NULL;
1523 	if (connp->conn_outgoing_ill == ill)
1524 		connp->conn_outgoing_ill = NULL;
1525 	if (connp->conn_outgoing_pill == ill)
1526 		connp->conn_outgoing_pill = NULL;
1527 	if (connp->conn_nofailover_ill == ill)
1528 		connp->conn_nofailover_ill = NULL;
1529 	if (connp->conn_xmit_if_ill == ill)
1530 		connp->conn_xmit_if_ill = NULL;
1531 	if (connp->conn_ire_cache != NULL) {
1532 		ire = connp->conn_ire_cache;
1533 		/*
1534 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1535 		 * interface X and ipif coming from interface Y, if interface
1536 		 * X and Y are part of the same IPMPgroup. Thus whenever
1537 		 * interface X goes down, remove all references to it by
1538 		 * checking both on ire_ipif and ire_stq.
1539 		 */
1540 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1541 		    (ire->ire_type == IRE_CACHE &&
1542 		    ire->ire_stq == ill->ill_wq)) {
1543 			connp->conn_ire_cache = NULL;
1544 			mutex_exit(&connp->conn_lock);
1545 			ire_refrele_notr(ire);
1546 			return;
1547 		}
1548 	}
1549 	mutex_exit(&connp->conn_lock);
1550 
1551 }
1552 
1553 /* ARGSUSED */
1554 void
1555 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1556 {
1557 	ill_t	*ill = q->q_ptr;
1558 	ipif_t	*ipif;
1559 
1560 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1561 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1562 		ipif_non_duplicate(ipif);
1563 		ipif_down_tail(ipif);
1564 	}
1565 	freemsg(mp);
1566 	ipsq_current_finish(ipsq);
1567 }
1568 
1569 /*
1570  * ill_down_start is called when we want to down this ill and bring it up again
1571  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1572  * all interfaces, but don't tear down any plumbing.
1573  */
1574 boolean_t
1575 ill_down_start(queue_t *q, mblk_t *mp)
1576 {
1577 	ill_t	*ill = q->q_ptr;
1578 	ipif_t	*ipif;
1579 
1580 	ASSERT(IAM_WRITER_ILL(ill));
1581 
1582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1583 		(void) ipif_down(ipif, NULL, NULL);
1584 
1585 	ill_down(ill);
1586 
1587 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1588 
1589 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1590 
1591 	/*
1592 	 * Atomically test and add the pending mp if references are active.
1593 	 */
1594 	mutex_enter(&ill->ill_lock);
1595 	if (!ill_is_quiescent(ill)) {
1596 		/* call cannot fail since `conn_t *' argument is NULL */
1597 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1598 		    mp, ILL_DOWN);
1599 		mutex_exit(&ill->ill_lock);
1600 		return (B_FALSE);
1601 	}
1602 	mutex_exit(&ill->ill_lock);
1603 	return (B_TRUE);
1604 }
1605 
1606 static void
1607 ill_down(ill_t *ill)
1608 {
1609 	ip_stack_t	*ipst = ill->ill_ipst;
1610 
1611 	/* Blow off any IREs dependent on this ILL. */
1612 	ire_walk(ill_downi, (char *)ill, ipst);
1613 
1614 	/* Remove any conn_*_ill depending on this ill */
1615 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1616 
1617 	if (ill->ill_group != NULL) {
1618 		illgrp_delete(ill);
1619 	}
1620 }
1621 
1622 /*
1623  * ire_walk routine used to delete every IRE that depends on queues
1624  * associated with 'ill'.  (Always called as writer.)
1625  */
1626 static void
1627 ill_downi(ire_t *ire, char *ill_arg)
1628 {
1629 	ill_t	*ill = (ill_t *)ill_arg;
1630 
1631 	/*
1632 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1633 	 * interface X and ipif coming from interface Y, if interface
1634 	 * X and Y are part of the same IPMP group. Thus whenever interface
1635 	 * X goes down, remove all references to it by checking both
1636 	 * on ire_ipif and ire_stq.
1637 	 */
1638 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1639 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1640 		ire_delete(ire);
1641 	}
1642 }
1643 
1644 /*
1645  * Remove ire/nce from the fastpath list.
1646  */
1647 void
1648 ill_fastpath_nack(ill_t *ill)
1649 {
1650 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1651 }
1652 
1653 /* Consume an M_IOCACK of the fastpath probe. */
1654 void
1655 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1656 {
1657 	mblk_t	*mp1 = mp;
1658 
1659 	/*
1660 	 * If this was the first attempt turn on the fastpath probing.
1661 	 */
1662 	mutex_enter(&ill->ill_lock);
1663 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1664 		ill->ill_dlpi_fastpath_state = IDS_OK;
1665 	mutex_exit(&ill->ill_lock);
1666 
1667 	/* Free the M_IOCACK mblk, hold on to the data */
1668 	mp = mp->b_cont;
1669 	freeb(mp1);
1670 	if (mp == NULL)
1671 		return;
1672 	if (mp->b_cont != NULL) {
1673 		/*
1674 		 * Update all IRE's or NCE's that are waiting for
1675 		 * fastpath update.
1676 		 */
1677 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1678 		mp1 = mp->b_cont;
1679 		freeb(mp);
1680 		mp = mp1;
1681 	} else {
1682 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1683 	}
1684 
1685 	freeb(mp);
1686 }
1687 
1688 /*
1689  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1690  * The data portion of the request is a dl_unitdata_req_t template for
1691  * what we would send downstream in the absence of a fastpath confirmation.
1692  */
1693 int
1694 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1695 {
1696 	struct iocblk	*ioc;
1697 	mblk_t	*mp;
1698 
1699 	if (dlur_mp == NULL)
1700 		return (EINVAL);
1701 
1702 	mutex_enter(&ill->ill_lock);
1703 	switch (ill->ill_dlpi_fastpath_state) {
1704 	case IDS_FAILED:
1705 		/*
1706 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1707 		 * support it.
1708 		 */
1709 		mutex_exit(&ill->ill_lock);
1710 		return (ENOTSUP);
1711 	case IDS_UNKNOWN:
1712 		/* This is the first probe */
1713 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1714 		break;
1715 	default:
1716 		break;
1717 	}
1718 	mutex_exit(&ill->ill_lock);
1719 
1720 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1721 		return (EAGAIN);
1722 
1723 	mp->b_cont = copyb(dlur_mp);
1724 	if (mp->b_cont == NULL) {
1725 		freeb(mp);
1726 		return (EAGAIN);
1727 	}
1728 
1729 	ioc = (struct iocblk *)mp->b_rptr;
1730 	ioc->ioc_count = msgdsize(mp->b_cont);
1731 
1732 	putnext(ill->ill_wq, mp);
1733 	return (0);
1734 }
1735 
1736 void
1737 ill_capability_probe(ill_t *ill)
1738 {
1739 	/*
1740 	 * Do so only if negotiation is enabled, capabilities are unknown,
1741 	 * and a capability negotiation is not already in progress.
1742 	 */
1743 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN &&
1744 	    ill->ill_dlpi_capab_state != IDS_RENEG)
1745 		return;
1746 
1747 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1748 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1749 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1750 }
1751 
1752 void
1753 ill_capability_reset(ill_t *ill)
1754 {
1755 	mblk_t *sc_mp = NULL;
1756 	mblk_t *tmp;
1757 
1758 	/*
1759 	 * Note here that we reset the state to UNKNOWN, and later send
1760 	 * down the DL_CAPABILITY_REQ without first setting the state to
1761 	 * INPROGRESS.  We do this in order to distinguish the
1762 	 * DL_CAPABILITY_ACK response which may come back in response to
1763 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1764 	 * also handle the case where the driver doesn't send us back
1765 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1766 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1767 	 * features are turned off until the state reaches IDS_OK.
1768 	 */
1769 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1770 
1771 	/*
1772 	 * Disable sub-capabilities and request a list of sub-capability
1773 	 * messages which will be sent down to the driver.  Each handler
1774 	 * allocates the corresponding dl_capability_sub_t inside an
1775 	 * mblk, and links it to the existing sc_mp mblk, or return it
1776 	 * as sc_mp if it's the first sub-capability (the passed in
1777 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1778 	 * sc_mp will be pulled-up, before passing it downstream.
1779 	 */
1780 	ill_capability_mdt_reset(ill, &sc_mp);
1781 	ill_capability_hcksum_reset(ill, &sc_mp);
1782 	ill_capability_zerocopy_reset(ill, &sc_mp);
1783 	ill_capability_ipsec_reset(ill, &sc_mp);
1784 	ill_capability_dls_reset(ill, &sc_mp);
1785 	ill_capability_lso_reset(ill, &sc_mp);
1786 
1787 	/* Nothing to send down in order to disable the capabilities? */
1788 	if (sc_mp == NULL)
1789 		return;
1790 
1791 	tmp = msgpullup(sc_mp, -1);
1792 	freemsg(sc_mp);
1793 	if ((sc_mp = tmp) == NULL) {
1794 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1795 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1796 		return;
1797 	}
1798 
1799 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1800 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1801 }
1802 
1803 /*
1804  * Request or set new-style hardware capabilities supported by DLS provider.
1805  */
1806 static void
1807 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1808 {
1809 	mblk_t *mp;
1810 	dl_capability_req_t *capb;
1811 	size_t size = 0;
1812 	uint8_t *ptr;
1813 
1814 	if (reqp != NULL)
1815 		size = MBLKL(reqp);
1816 
1817 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1818 	if (mp == NULL) {
1819 		freemsg(reqp);
1820 		return;
1821 	}
1822 	ptr = mp->b_rptr;
1823 
1824 	capb = (dl_capability_req_t *)ptr;
1825 	ptr += sizeof (dl_capability_req_t);
1826 
1827 	if (reqp != NULL) {
1828 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1829 		capb->dl_sub_length = size;
1830 		bcopy(reqp->b_rptr, ptr, size);
1831 		ptr += size;
1832 		mp->b_cont = reqp->b_cont;
1833 		freeb(reqp);
1834 	}
1835 	ASSERT(ptr == mp->b_wptr);
1836 
1837 	ill_dlpi_send(ill, mp);
1838 }
1839 
1840 static void
1841 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1842 {
1843 	dl_capab_id_t *id_ic;
1844 	uint_t sub_dl_cap = outers->dl_cap;
1845 	dl_capability_sub_t *inners;
1846 	uint8_t *capend;
1847 
1848 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1849 
1850 	/*
1851 	 * Note: range checks here are not absolutely sufficient to
1852 	 * make us robust against malformed messages sent by drivers;
1853 	 * this is in keeping with the rest of IP's dlpi handling.
1854 	 * (Remember, it's coming from something else in the kernel
1855 	 * address space)
1856 	 */
1857 
1858 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1859 	if (capend > mp->b_wptr) {
1860 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1861 		    "malformed sub-capability too long for mblk");
1862 		return;
1863 	}
1864 
1865 	id_ic = (dl_capab_id_t *)(outers + 1);
1866 
1867 	if (outers->dl_length < sizeof (*id_ic) ||
1868 	    (inners = &id_ic->id_subcap,
1869 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1870 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1871 		    "encapsulated capab type %d too long for mblk",
1872 		    inners->dl_cap);
1873 		return;
1874 	}
1875 
1876 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1877 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1878 		    "isn't as expected; pass-thru module(s) detected, "
1879 		    "discarding capability\n", inners->dl_cap));
1880 		return;
1881 	}
1882 
1883 	/* Process the encapsulated sub-capability */
1884 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1885 }
1886 
1887 /*
1888  * Process Multidata Transmit capability negotiation ack received from a
1889  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1890  * DL_CAPABILITY_ACK message.
1891  */
1892 static void
1893 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1894 {
1895 	mblk_t *nmp = NULL;
1896 	dl_capability_req_t *oc;
1897 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1898 	ill_mdt_capab_t **ill_mdt_capab;
1899 	uint_t sub_dl_cap = isub->dl_cap;
1900 	uint8_t *capend;
1901 
1902 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1903 
1904 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1905 
1906 	/*
1907 	 * Note: range checks here are not absolutely sufficient to
1908 	 * make us robust against malformed messages sent by drivers;
1909 	 * this is in keeping with the rest of IP's dlpi handling.
1910 	 * (Remember, it's coming from something else in the kernel
1911 	 * address space)
1912 	 */
1913 
1914 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1915 	if (capend > mp->b_wptr) {
1916 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1917 		    "malformed sub-capability too long for mblk");
1918 		return;
1919 	}
1920 
1921 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1922 
1923 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1924 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1925 		    "unsupported MDT sub-capability (version %d, expected %d)",
1926 		    mdt_ic->mdt_version, MDT_VERSION_2);
1927 		return;
1928 	}
1929 
1930 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1931 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1932 		    "capability isn't as expected; pass-thru module(s) "
1933 		    "detected, discarding capability\n"));
1934 		return;
1935 	}
1936 
1937 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1938 
1939 		if (*ill_mdt_capab == NULL) {
1940 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1941 			    KM_NOSLEEP);
1942 
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_dlpi_send(ill, nmp);
2016 	}
2017 }
2018 
2019 static void
2020 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2021 {
2022 	mblk_t *mp;
2023 	dl_capab_mdt_t *mdt_subcap;
2024 	dl_capability_sub_t *dl_subcap;
2025 	int size;
2026 
2027 	if (!ILL_MDT_CAPABLE(ill))
2028 		return;
2029 
2030 	ASSERT(ill->ill_mdt_capab != NULL);
2031 	/*
2032 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2033 	 * structure since it's possible that another thread is still
2034 	 * referring to it.  The structure only gets deallocated when
2035 	 * we destroy the ill.
2036 	 */
2037 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2038 
2039 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2040 
2041 	mp = allocb(size, BPRI_HI);
2042 	if (mp == NULL) {
2043 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2044 		    "request to disable MDT\n"));
2045 		return;
2046 	}
2047 
2048 	mp->b_wptr = mp->b_rptr + size;
2049 
2050 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2051 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2052 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2053 
2054 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2055 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2056 	mdt_subcap->mdt_flags = 0;
2057 	mdt_subcap->mdt_hdr_head = 0;
2058 	mdt_subcap->mdt_hdr_tail = 0;
2059 
2060 	if (*sc_mp != NULL)
2061 		linkb(*sc_mp, mp);
2062 	else
2063 		*sc_mp = mp;
2064 }
2065 
2066 /*
2067  * Send a DL_NOTIFY_REQ to the specified ill to enable
2068  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2069  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2070  * acceleration.
2071  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2072  */
2073 static boolean_t
2074 ill_enable_promisc_notify(ill_t *ill)
2075 {
2076 	mblk_t *mp;
2077 	dl_notify_req_t *req;
2078 
2079 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2080 
2081 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2082 	if (mp == NULL)
2083 		return (B_FALSE);
2084 
2085 	req = (dl_notify_req_t *)mp->b_rptr;
2086 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2087 	    DL_NOTE_PROMISC_OFF_PHYS;
2088 
2089 	ill_dlpi_send(ill, mp);
2090 
2091 	return (B_TRUE);
2092 }
2093 
2094 
2095 /*
2096  * Allocate an IPsec capability request which will be filled by our
2097  * caller to turn on support for one or more algorithms.
2098  */
2099 static mblk_t *
2100 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2101 {
2102 	mblk_t *nmp;
2103 	dl_capability_req_t	*ocap;
2104 	dl_capab_ipsec_t	*ocip;
2105 	dl_capab_ipsec_t	*icip;
2106 	uint8_t			*ptr;
2107 	icip = (dl_capab_ipsec_t *)(isub + 1);
2108 
2109 	/*
2110 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2111 	 * PROMISC_ON/OFF notification from the provider. We need to
2112 	 * do this before enabling the algorithms to avoid leakage of
2113 	 * cleartext packets.
2114 	 */
2115 
2116 	if (!ill_enable_promisc_notify(ill))
2117 		return (NULL);
2118 
2119 	/*
2120 	 * Allocate new mblk which will contain a new capability
2121 	 * request to enable the capabilities.
2122 	 */
2123 
2124 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2125 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2126 	if (nmp == NULL)
2127 		return (NULL);
2128 
2129 	ptr = nmp->b_rptr;
2130 
2131 	/* initialize dl_capability_req_t */
2132 	ocap = (dl_capability_req_t *)ptr;
2133 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2134 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2135 	ptr += sizeof (dl_capability_req_t);
2136 
2137 	/* initialize dl_capability_sub_t */
2138 	bcopy(isub, ptr, sizeof (*isub));
2139 	ptr += sizeof (*isub);
2140 
2141 	/* initialize dl_capab_ipsec_t */
2142 	ocip = (dl_capab_ipsec_t *)ptr;
2143 	bcopy(icip, ocip, sizeof (*icip));
2144 
2145 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2146 	return (nmp);
2147 }
2148 
2149 /*
2150  * Process an IPsec capability negotiation ack received from a DLS Provider.
2151  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2152  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2153  */
2154 static void
2155 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2156 {
2157 	dl_capab_ipsec_t	*icip;
2158 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2159 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2160 	uint_t cipher, nciphers;
2161 	mblk_t *nmp;
2162 	uint_t alg_len;
2163 	boolean_t need_sadb_dump;
2164 	uint_t sub_dl_cap = isub->dl_cap;
2165 	ill_ipsec_capab_t **ill_capab;
2166 	uint64_t ill_capab_flag;
2167 	uint8_t *capend, *ciphend;
2168 	boolean_t sadb_resync;
2169 
2170 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2171 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2172 
2173 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2174 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2175 		ill_capab_flag = ILL_CAPAB_AH;
2176 	} else {
2177 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2178 		ill_capab_flag = ILL_CAPAB_ESP;
2179 	}
2180 
2181 	/*
2182 	 * If the ill capability structure exists, then this incoming
2183 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2184 	 * If this is so, then we'd need to resynchronize the SADB
2185 	 * after re-enabling the offloaded ciphers.
2186 	 */
2187 	sadb_resync = (*ill_capab != NULL);
2188 
2189 	/*
2190 	 * Note: range checks here are not absolutely sufficient to
2191 	 * make us robust against malformed messages sent by drivers;
2192 	 * this is in keeping with the rest of IP's dlpi handling.
2193 	 * (Remember, it's coming from something else in the kernel
2194 	 * address space)
2195 	 */
2196 
2197 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2198 	if (capend > mp->b_wptr) {
2199 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2200 		    "malformed sub-capability too long for mblk");
2201 		return;
2202 	}
2203 
2204 	/*
2205 	 * There are two types of acks we process here:
2206 	 * 1. acks in reply to a (first form) generic capability req
2207 	 *    (no ENABLE flag set)
2208 	 * 2. acks in reply to a ENABLE capability req.
2209 	 *    (ENABLE flag set)
2210 	 *
2211 	 * We process the subcapability passed as argument as follows:
2212 	 * 1 do initializations
2213 	 *   1.1 initialize nmp = NULL
2214 	 *   1.2 set need_sadb_dump to B_FALSE
2215 	 * 2 for each cipher in subcapability:
2216 	 *   2.1 if ENABLE flag is set:
2217 	 *	2.1.1 update per-ill ipsec capabilities info
2218 	 *	2.1.2 set need_sadb_dump to B_TRUE
2219 	 *   2.2 if ENABLE flag is not set:
2220 	 *	2.2.1 if nmp is NULL:
2221 	 *		2.2.1.1 allocate and initialize nmp
2222 	 *		2.2.1.2 init current pos in nmp
2223 	 *	2.2.2 copy current cipher to current pos in nmp
2224 	 *	2.2.3 set ENABLE flag in nmp
2225 	 *	2.2.4 update current pos
2226 	 * 3 if nmp is not equal to NULL, send enable request
2227 	 *   3.1 send capability request
2228 	 * 4 if need_sadb_dump is B_TRUE
2229 	 *   4.1 enable promiscuous on/off notifications
2230 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2231 	 *	AH or ESP SA's to interface.
2232 	 */
2233 
2234 	nmp = NULL;
2235 	oalg = NULL;
2236 	need_sadb_dump = B_FALSE;
2237 	icip = (dl_capab_ipsec_t *)(isub + 1);
2238 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2239 
2240 	nciphers = icip->cip_nciphers;
2241 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2242 
2243 	if (ciphend > capend) {
2244 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2245 		    "too many ciphers for sub-capability len");
2246 		return;
2247 	}
2248 
2249 	for (cipher = 0; cipher < nciphers; cipher++) {
2250 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2251 
2252 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2253 			/*
2254 			 * TBD: when we provide a way to disable capabilities
2255 			 * from above, need to manage the request-pending state
2256 			 * and fail if we were not expecting this ACK.
2257 			 */
2258 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2259 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2260 
2261 			/*
2262 			 * Update IPsec capabilities for this ill
2263 			 */
2264 
2265 			if (*ill_capab == NULL) {
2266 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2267 				    ("ill_capability_ipsec_ack: "
2268 				    "allocating ipsec_capab for ill\n"));
2269 				*ill_capab = ill_ipsec_capab_alloc();
2270 
2271 				if (*ill_capab == NULL) {
2272 					cmn_err(CE_WARN,
2273 					    "ill_capability_ipsec_ack: "
2274 					    "could not enable IPsec Hardware "
2275 					    "acceleration for %s (ENOMEM)\n",
2276 					    ill->ill_name);
2277 					return;
2278 				}
2279 			}
2280 
2281 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2282 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2283 
2284 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2285 				cmn_err(CE_WARN,
2286 				    "ill_capability_ipsec_ack: "
2287 				    "malformed IPsec algorithm id %d",
2288 				    ialg->alg_prim);
2289 				continue;
2290 			}
2291 
2292 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2293 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2294 				    ialg->alg_prim);
2295 			} else {
2296 				ipsec_capab_algparm_t *alp;
2297 
2298 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2299 				    ialg->alg_prim);
2300 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2301 				    ialg->alg_prim)) {
2302 					cmn_err(CE_WARN,
2303 					    "ill_capability_ipsec_ack: "
2304 					    "no space for IPsec alg id %d",
2305 					    ialg->alg_prim);
2306 					continue;
2307 				}
2308 				alp = &((*ill_capab)->encr_algparm[
2309 				    ialg->alg_prim]);
2310 				alp->minkeylen = ialg->alg_minbits;
2311 				alp->maxkeylen = ialg->alg_maxbits;
2312 			}
2313 			ill->ill_capabilities |= ill_capab_flag;
2314 			/*
2315 			 * indicate that a capability was enabled, which
2316 			 * will be used below to kick off a SADB dump
2317 			 * to the ill.
2318 			 */
2319 			need_sadb_dump = B_TRUE;
2320 		} else {
2321 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2322 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2323 			    ialg->alg_prim));
2324 
2325 			if (nmp == NULL) {
2326 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2327 				if (nmp == NULL) {
2328 					/*
2329 					 * Sending the PROMISC_ON/OFF
2330 					 * notification request failed.
2331 					 * We cannot enable the algorithms
2332 					 * since the Provider will not
2333 					 * notify IP of promiscous mode
2334 					 * changes, which could lead
2335 					 * to leakage of packets.
2336 					 */
2337 					cmn_err(CE_WARN,
2338 					    "ill_capability_ipsec_ack: "
2339 					    "could not enable IPsec Hardware "
2340 					    "acceleration for %s (ENOMEM)\n",
2341 					    ill->ill_name);
2342 					return;
2343 				}
2344 				/* ptr to current output alg specifier */
2345 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2346 			}
2347 
2348 			/*
2349 			 * Copy current alg specifier, set ENABLE
2350 			 * flag, and advance to next output alg.
2351 			 * For now we enable all IPsec capabilities.
2352 			 */
2353 			ASSERT(oalg != NULL);
2354 			bcopy(ialg, oalg, alg_len);
2355 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2356 			nmp->b_wptr += alg_len;
2357 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2358 		}
2359 
2360 		/* move to next input algorithm specifier */
2361 		ialg = (dl_capab_ipsec_alg_t *)
2362 		    ((char *)ialg + alg_len);
2363 	}
2364 
2365 	if (nmp != NULL)
2366 		/*
2367 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2368 		 * IPsec hardware acceleration.
2369 		 */
2370 		ill_dlpi_send(ill, nmp);
2371 
2372 	if (need_sadb_dump)
2373 		/*
2374 		 * An acknowledgement corresponding to a request to
2375 		 * enable acceleration was received, notify SADB.
2376 		 */
2377 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2378 }
2379 
2380 /*
2381  * Given an mblk with enough space in it, create sub-capability entries for
2382  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2383  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2384  * in preparation for the reset the DL_CAPABILITY_REQ message.
2385  */
2386 static void
2387 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2388     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2389 {
2390 	dl_capab_ipsec_t *oipsec;
2391 	dl_capab_ipsec_alg_t *oalg;
2392 	dl_capability_sub_t *dl_subcap;
2393 	int i, k;
2394 
2395 	ASSERT(nciphers > 0);
2396 	ASSERT(ill_cap != NULL);
2397 	ASSERT(mp != NULL);
2398 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2399 
2400 	/* dl_capability_sub_t for "stype" */
2401 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2402 	dl_subcap->dl_cap = stype;
2403 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2404 	mp->b_wptr += sizeof (dl_capability_sub_t);
2405 
2406 	/* dl_capab_ipsec_t for "stype" */
2407 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2408 	oipsec->cip_version = 1;
2409 	oipsec->cip_nciphers = nciphers;
2410 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2411 
2412 	/* create entries for "stype" AUTH ciphers */
2413 	for (i = 0; i < ill_cap->algs_size; i++) {
2414 		for (k = 0; k < BITSPERBYTE; k++) {
2415 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2416 				continue;
2417 
2418 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2419 			bzero((void *)oalg, sizeof (*oalg));
2420 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2421 			oalg->alg_prim = k + (BITSPERBYTE * i);
2422 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2423 		}
2424 	}
2425 	/* create entries for "stype" ENCR ciphers */
2426 	for (i = 0; i < ill_cap->algs_size; i++) {
2427 		for (k = 0; k < BITSPERBYTE; k++) {
2428 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2429 				continue;
2430 
2431 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2432 			bzero((void *)oalg, sizeof (*oalg));
2433 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2434 			oalg->alg_prim = k + (BITSPERBYTE * i);
2435 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2436 		}
2437 	}
2438 }
2439 
2440 /*
2441  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2442  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2443  * POPC instruction, but our macro is more flexible for an arbitrary length
2444  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2445  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2446  * stays that way, we can reduce the number of iterations required.
2447  */
2448 #define	COUNT_1S(val, sum) {					\
2449 	uint8_t x = val & 0xff;					\
2450 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2451 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2452 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2453 }
2454 
2455 /* ARGSUSED */
2456 static void
2457 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2458 {
2459 	mblk_t *mp;
2460 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2461 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2462 	uint64_t ill_capabilities = ill->ill_capabilities;
2463 	int ah_cnt = 0, esp_cnt = 0;
2464 	int ah_len = 0, esp_len = 0;
2465 	int i, size = 0;
2466 
2467 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2468 		return;
2469 
2470 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2471 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2472 
2473 	/* Find out the number of ciphers for AH */
2474 	if (cap_ah != NULL) {
2475 		for (i = 0; i < cap_ah->algs_size; i++) {
2476 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2477 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2478 		}
2479 		if (ah_cnt > 0) {
2480 			size += sizeof (dl_capability_sub_t) +
2481 			    sizeof (dl_capab_ipsec_t);
2482 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2483 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2484 			size += ah_len;
2485 		}
2486 	}
2487 
2488 	/* Find out the number of ciphers for ESP */
2489 	if (cap_esp != NULL) {
2490 		for (i = 0; i < cap_esp->algs_size; i++) {
2491 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2492 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2493 		}
2494 		if (esp_cnt > 0) {
2495 			size += sizeof (dl_capability_sub_t) +
2496 			    sizeof (dl_capab_ipsec_t);
2497 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2498 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2499 			size += esp_len;
2500 		}
2501 	}
2502 
2503 	if (size == 0) {
2504 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2505 		    "there's nothing to reset\n"));
2506 		return;
2507 	}
2508 
2509 	mp = allocb(size, BPRI_HI);
2510 	if (mp == NULL) {
2511 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2512 		    "request to disable IPSEC Hardware Acceleration\n"));
2513 		return;
2514 	}
2515 
2516 	/*
2517 	 * Clear the capability flags for IPsec HA but retain the ill
2518 	 * capability structures since it's possible that another thread
2519 	 * is still referring to them.  The structures only get deallocated
2520 	 * when we destroy the ill.
2521 	 *
2522 	 * Various places check the flags to see if the ill is capable of
2523 	 * hardware acceleration, and by clearing them we ensure that new
2524 	 * outbound IPsec packets are sent down encrypted.
2525 	 */
2526 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2527 
2528 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2529 	if (ah_cnt > 0) {
2530 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2531 		    cap_ah, mp);
2532 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2533 	}
2534 
2535 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2536 	if (esp_cnt > 0) {
2537 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2538 		    cap_esp, mp);
2539 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2540 	}
2541 
2542 	/*
2543 	 * At this point we've composed a bunch of sub-capabilities to be
2544 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2545 	 * by the caller.  Upon receiving this reset message, the driver
2546 	 * must stop inbound decryption (by destroying all inbound SAs)
2547 	 * and let the corresponding packets come in encrypted.
2548 	 */
2549 
2550 	if (*sc_mp != NULL)
2551 		linkb(*sc_mp, mp);
2552 	else
2553 		*sc_mp = mp;
2554 }
2555 
2556 static void
2557 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2558     boolean_t encapsulated)
2559 {
2560 	boolean_t legacy = B_FALSE;
2561 
2562 	/*
2563 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2564 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2565 	 * instructed the driver to disable its advertised capabilities,
2566 	 * so there's no point in accepting any response at this moment.
2567 	 */
2568 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2569 		return;
2570 
2571 	/*
2572 	 * Note that only the following two sub-capabilities may be
2573 	 * considered as "legacy", since their original definitions
2574 	 * do not incorporate the dl_mid_t module ID token, and hence
2575 	 * may require the use of the wrapper sub-capability.
2576 	 */
2577 	switch (subp->dl_cap) {
2578 	case DL_CAPAB_IPSEC_AH:
2579 	case DL_CAPAB_IPSEC_ESP:
2580 		legacy = B_TRUE;
2581 		break;
2582 	}
2583 
2584 	/*
2585 	 * For legacy sub-capabilities which don't incorporate a queue_t
2586 	 * pointer in their structures, discard them if we detect that
2587 	 * there are intermediate modules in between IP and the driver.
2588 	 */
2589 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2590 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2591 		    "%d discarded; %d module(s) present below IP\n",
2592 		    subp->dl_cap, ill->ill_lmod_cnt));
2593 		return;
2594 	}
2595 
2596 	switch (subp->dl_cap) {
2597 	case DL_CAPAB_IPSEC_AH:
2598 	case DL_CAPAB_IPSEC_ESP:
2599 		ill_capability_ipsec_ack(ill, mp, subp);
2600 		break;
2601 	case DL_CAPAB_MDT:
2602 		ill_capability_mdt_ack(ill, mp, subp);
2603 		break;
2604 	case DL_CAPAB_HCKSUM:
2605 		ill_capability_hcksum_ack(ill, mp, subp);
2606 		break;
2607 	case DL_CAPAB_ZEROCOPY:
2608 		ill_capability_zerocopy_ack(ill, mp, subp);
2609 		break;
2610 	case DL_CAPAB_POLL:
2611 		if (!SOFT_RINGS_ENABLED())
2612 			ill_capability_dls_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_SOFT_RING:
2615 		if (SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_LSO:
2619 		ill_capability_lso_ack(ill, mp, subp);
2620 		break;
2621 	default:
2622 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2623 		    subp->dl_cap));
2624 	}
2625 }
2626 
2627 /*
2628  * As part of negotiating polling capability, the driver tells us
2629  * the default (or normal) blanking interval and packet threshold
2630  * (the receive timer fires if blanking interval is reached or
2631  * the packet threshold is reached).
2632  *
2633  * As part of manipulating the polling interval, we always use our
2634  * estimated interval (avg service time * number of packets queued
2635  * on the squeue) but we try to blank for a minimum of
2636  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2637  * packet threshold during this time. When we are not in polling mode
2638  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2639  * rr_min_blank_ratio but up the packet cnt by a ratio of
2640  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2641  * possible although for a shorter interval.
2642  */
2643 #define	RR_MAX_BLANK_RATIO	20
2644 #define	RR_MIN_BLANK_RATIO	10
2645 #define	RR_MAX_PKT_CNT_RATIO	3
2646 #define	RR_MIN_PKT_CNT_RATIO	3
2647 
2648 /*
2649  * These can be tuned via /etc/system.
2650  */
2651 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2652 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2653 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2654 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2655 
2656 static mac_resource_handle_t
2657 ill_ring_add(void *arg, mac_resource_t *mrp)
2658 {
2659 	ill_t			*ill = (ill_t *)arg;
2660 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2661 	ill_rx_ring_t		*rx_ring;
2662 	int			ip_rx_index;
2663 
2664 	ASSERT(mrp != NULL);
2665 	if (mrp->mr_type != MAC_RX_FIFO) {
2666 		return (NULL);
2667 	}
2668 	ASSERT(ill != NULL);
2669 	ASSERT(ill->ill_dls_capab != NULL);
2670 
2671 	mutex_enter(&ill->ill_lock);
2672 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2673 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2674 		ASSERT(rx_ring != NULL);
2675 
2676 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2677 			time_t normal_blank_time =
2678 			    mrfp->mrf_normal_blank_time;
2679 			uint_t normal_pkt_cnt =
2680 			    mrfp->mrf_normal_pkt_count;
2681 
2682 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2683 
2684 	rx_ring->rr_blank = mrfp->mrf_blank;
2685 	rx_ring->rr_handle = mrfp->mrf_arg;
2686 	rx_ring->rr_ill = ill;
2687 	rx_ring->rr_normal_blank_time = normal_blank_time;
2688 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2689 
2690 			rx_ring->rr_max_blank_time =
2691 			    normal_blank_time * rr_max_blank_ratio;
2692 			rx_ring->rr_min_blank_time =
2693 			    normal_blank_time * rr_min_blank_ratio;
2694 			rx_ring->rr_max_pkt_cnt =
2695 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2696 			rx_ring->rr_min_pkt_cnt =
2697 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2698 
2699 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2700 			mutex_exit(&ill->ill_lock);
2701 
2702 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2703 			    (int), ip_rx_index);
2704 			return ((mac_resource_handle_t)rx_ring);
2705 		}
2706 	}
2707 
2708 	/*
2709 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2710 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2711 	 * should be made configurable. Meanwhile it cause no panic because
2712 	 * driver will pass ip_input a NULL handle which will make
2713 	 * IP allocate the default squeue and Polling mode will not
2714 	 * be used for this ring.
2715 	 */
2716 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2717 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2718 
2719 	mutex_exit(&ill->ill_lock);
2720 	return (NULL);
2721 }
2722 
2723 static boolean_t
2724 ill_capability_dls_init(ill_t *ill)
2725 {
2726 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2727 	conn_t 			*connp;
2728 	size_t			sz;
2729 	ip_stack_t *ipst = ill->ill_ipst;
2730 
2731 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2732 		if (ill_dls == NULL) {
2733 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2734 			    "soft_ring enabled for ill=%s (%p) but data "
2735 			    "structs uninitialized\n", ill->ill_name,
2736 			    (void *)ill);
2737 		}
2738 		return (B_TRUE);
2739 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2740 		if (ill_dls == NULL) {
2741 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2742 			    "polling enabled for ill=%s (%p) but data "
2743 			    "structs uninitialized\n", ill->ill_name,
2744 			    (void *)ill);
2745 		}
2746 		return (B_TRUE);
2747 	}
2748 
2749 	if (ill_dls != NULL) {
2750 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2751 		/* Soft_Ring or polling is being re-enabled */
2752 
2753 		connp = ill_dls->ill_unbind_conn;
2754 		ASSERT(rx_ring != NULL);
2755 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2756 		bzero((void *)rx_ring,
2757 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2758 		ill_dls->ill_ring_tbl = rx_ring;
2759 		ill_dls->ill_unbind_conn = connp;
2760 		return (B_TRUE);
2761 	}
2762 
2763 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2764 	    ipst->ips_netstack)) == NULL)
2765 		return (B_FALSE);
2766 
2767 	sz = sizeof (ill_dls_capab_t);
2768 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2769 
2770 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2771 	if (ill_dls == NULL) {
2772 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2773 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2774 		    (void *)ill);
2775 		CONN_DEC_REF(connp);
2776 		return (B_FALSE);
2777 	}
2778 
2779 	/* Allocate space to hold ring table */
2780 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2781 	ill->ill_dls_capab = ill_dls;
2782 	ill_dls->ill_unbind_conn = connp;
2783 	return (B_TRUE);
2784 }
2785 
2786 /*
2787  * ill_capability_dls_disable: disable soft_ring and/or polling
2788  * capability. Since any of the rings might already be in use, need
2789  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2790  * direct calls if necessary.
2791  */
2792 static void
2793 ill_capability_dls_disable(ill_t *ill)
2794 {
2795 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2796 
2797 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2798 		ip_squeue_clean_all(ill);
2799 		ill_dls->ill_tx = NULL;
2800 		ill_dls->ill_tx_handle = NULL;
2801 		ill_dls->ill_dls_change_status = NULL;
2802 		ill_dls->ill_dls_bind = NULL;
2803 		ill_dls->ill_dls_unbind = NULL;
2804 	}
2805 
2806 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2807 }
2808 
2809 static void
2810 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2811     dl_capability_sub_t *isub)
2812 {
2813 	uint_t			size;
2814 	uchar_t			*rptr;
2815 	dl_capab_dls_t	dls, *odls;
2816 	ill_dls_capab_t	*ill_dls;
2817 	mblk_t			*nmp = NULL;
2818 	dl_capability_req_t	*ocap;
2819 	uint_t			sub_dl_cap = isub->dl_cap;
2820 
2821 	if (!ill_capability_dls_init(ill))
2822 		return;
2823 	ill_dls = ill->ill_dls_capab;
2824 
2825 	/* Copy locally to get the members aligned */
2826 	bcopy((void *)idls, (void *)&dls,
2827 	    sizeof (dl_capab_dls_t));
2828 
2829 	/* Get the tx function and handle from dld */
2830 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2831 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2832 
2833 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2834 		ill_dls->ill_dls_change_status =
2835 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2836 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2837 		ill_dls->ill_dls_unbind =
2838 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2839 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2840 	}
2841 
2842 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2843 	    isub->dl_length;
2844 
2845 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2846 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2847 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2848 		    ill->ill_name, (void *)ill);
2849 		return;
2850 	}
2851 
2852 	/* initialize dl_capability_req_t */
2853 	rptr = nmp->b_rptr;
2854 	ocap = (dl_capability_req_t *)rptr;
2855 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2856 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2857 	rptr += sizeof (dl_capability_req_t);
2858 
2859 	/* initialize dl_capability_sub_t */
2860 	bcopy(isub, rptr, sizeof (*isub));
2861 	rptr += sizeof (*isub);
2862 
2863 	odls = (dl_capab_dls_t *)rptr;
2864 	rptr += sizeof (dl_capab_dls_t);
2865 
2866 	/* initialize dl_capab_dls_t to be sent down */
2867 	dls.dls_rx_handle = (uintptr_t)ill;
2868 	dls.dls_rx = (uintptr_t)ip_input;
2869 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2870 
2871 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2872 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2873 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2874 		dls.dls_flags = SOFT_RING_ENABLE;
2875 	} else {
2876 		dls.dls_flags = POLL_ENABLE;
2877 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2878 		    "to enable polling\n", ill->ill_name));
2879 	}
2880 	bcopy((void *)&dls, (void *)odls,
2881 	    sizeof (dl_capab_dls_t));
2882 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2883 	/*
2884 	 * nmp points to a DL_CAPABILITY_REQ message to
2885 	 * enable either soft_ring or polling
2886 	 */
2887 	ill_dlpi_send(ill, nmp);
2888 }
2889 
2890 static void
2891 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2892 {
2893 	mblk_t *mp;
2894 	dl_capab_dls_t *idls;
2895 	dl_capability_sub_t *dl_subcap;
2896 	int size;
2897 
2898 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2899 		return;
2900 
2901 	ASSERT(ill->ill_dls_capab != NULL);
2902 
2903 	size = sizeof (*dl_subcap) + sizeof (*idls);
2904 
2905 	mp = allocb(size, BPRI_HI);
2906 	if (mp == NULL) {
2907 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2908 		    "request to disable soft_ring\n"));
2909 		return;
2910 	}
2911 
2912 	mp->b_wptr = mp->b_rptr + size;
2913 
2914 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2915 	dl_subcap->dl_length = sizeof (*idls);
2916 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2917 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2918 	else
2919 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2920 
2921 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2922 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2923 		idls->dls_flags = SOFT_RING_DISABLE;
2924 	else
2925 		idls->dls_flags = POLL_DISABLE;
2926 
2927 	if (*sc_mp != NULL)
2928 		linkb(*sc_mp, mp);
2929 	else
2930 		*sc_mp = mp;
2931 }
2932 
2933 /*
2934  * Process a soft_ring/poll capability negotiation ack received
2935  * from a DLS Provider.isub must point to the sub-capability
2936  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2937  */
2938 static void
2939 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2940 {
2941 	dl_capab_dls_t		*idls;
2942 	uint_t			sub_dl_cap = isub->dl_cap;
2943 	uint8_t			*capend;
2944 
2945 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2946 	    sub_dl_cap == DL_CAPAB_POLL);
2947 
2948 	if (ill->ill_isv6)
2949 		return;
2950 
2951 	/*
2952 	 * Note: range checks here are not absolutely sufficient to
2953 	 * make us robust against malformed messages sent by drivers;
2954 	 * this is in keeping with the rest of IP's dlpi handling.
2955 	 * (Remember, it's coming from something else in the kernel
2956 	 * address space)
2957 	 */
2958 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2959 	if (capend > mp->b_wptr) {
2960 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2961 		    "malformed sub-capability too long for mblk");
2962 		return;
2963 	}
2964 
2965 	/*
2966 	 * There are two types of acks we process here:
2967 	 * 1. acks in reply to a (first form) generic capability req
2968 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2969 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2970 	 *    capability req.
2971 	 */
2972 	idls = (dl_capab_dls_t *)(isub + 1);
2973 
2974 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2975 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2976 		    "capability isn't as expected; pass-thru "
2977 		    "module(s) detected, discarding capability\n"));
2978 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2979 			/*
2980 			 * This is a capability renegotitation case.
2981 			 * The interface better be unusable at this
2982 			 * point other wise bad things will happen
2983 			 * if we disable direct calls on a running
2984 			 * and up interface.
2985 			 */
2986 			ill_capability_dls_disable(ill);
2987 		}
2988 		return;
2989 	}
2990 
2991 	switch (idls->dls_flags) {
2992 	default:
2993 		/* Disable if unknown flag */
2994 	case SOFT_RING_DISABLE:
2995 	case POLL_DISABLE:
2996 		ill_capability_dls_disable(ill);
2997 		break;
2998 	case SOFT_RING_CAPABLE:
2999 	case POLL_CAPABLE:
3000 		/*
3001 		 * If the capability was already enabled, its safe
3002 		 * to disable it first to get rid of stale information
3003 		 * and then start enabling it again.
3004 		 */
3005 		ill_capability_dls_disable(ill);
3006 		ill_capability_dls_capable(ill, idls, isub);
3007 		break;
3008 	case SOFT_RING_ENABLE:
3009 	case POLL_ENABLE:
3010 		mutex_enter(&ill->ill_lock);
3011 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3012 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3013 			ASSERT(ill->ill_dls_capab != NULL);
3014 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3015 		}
3016 		if (sub_dl_cap == DL_CAPAB_POLL &&
3017 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3018 			ASSERT(ill->ill_dls_capab != NULL);
3019 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3020 			ip1dbg(("ill_capability_dls_ack: interface %s "
3021 			    "has enabled polling\n", ill->ill_name));
3022 		}
3023 		mutex_exit(&ill->ill_lock);
3024 		break;
3025 	}
3026 }
3027 
3028 /*
3029  * Process a hardware checksum offload capability negotiation ack received
3030  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3031  * of a DL_CAPABILITY_ACK message.
3032  */
3033 static void
3034 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3035 {
3036 	dl_capability_req_t	*ocap;
3037 	dl_capab_hcksum_t	*ihck, *ohck;
3038 	ill_hcksum_capab_t	**ill_hcksum;
3039 	mblk_t			*nmp = NULL;
3040 	uint_t			sub_dl_cap = isub->dl_cap;
3041 	uint8_t			*capend;
3042 
3043 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3044 
3045 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3046 
3047 	/*
3048 	 * Note: range checks here are not absolutely sufficient to
3049 	 * make us robust against malformed messages sent by drivers;
3050 	 * this is in keeping with the rest of IP's dlpi handling.
3051 	 * (Remember, it's coming from something else in the kernel
3052 	 * address space)
3053 	 */
3054 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3055 	if (capend > mp->b_wptr) {
3056 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3057 		    "malformed sub-capability too long for mblk");
3058 		return;
3059 	}
3060 
3061 	/*
3062 	 * There are two types of acks we process here:
3063 	 * 1. acks in reply to a (first form) generic capability req
3064 	 *    (no ENABLE flag set)
3065 	 * 2. acks in reply to a ENABLE capability req.
3066 	 *    (ENABLE flag set)
3067 	 */
3068 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3069 
3070 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3071 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3072 		    "unsupported hardware checksum "
3073 		    "sub-capability (version %d, expected %d)",
3074 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3075 		return;
3076 	}
3077 
3078 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3079 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3080 		    "checksum capability isn't as expected; pass-thru "
3081 		    "module(s) detected, discarding capability\n"));
3082 		return;
3083 	}
3084 
3085 #define	CURR_HCKSUM_CAPAB				\
3086 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3087 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3088 
3089 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3090 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3091 		/* do ENABLE processing */
3092 		if (*ill_hcksum == NULL) {
3093 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3094 			    KM_NOSLEEP);
3095 
3096 			if (*ill_hcksum == NULL) {
3097 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3098 				    "could not enable hcksum version %d "
3099 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3100 				    ill->ill_name);
3101 				return;
3102 			}
3103 		}
3104 
3105 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3106 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3107 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3108 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3109 		    "has enabled hardware checksumming\n ",
3110 		    ill->ill_name));
3111 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3112 		/*
3113 		 * Enabling hardware checksum offload
3114 		 * Currently IP supports {TCP,UDP}/IPv4
3115 		 * partial and full cksum offload and
3116 		 * IPv4 header checksum offload.
3117 		 * Allocate new mblk which will
3118 		 * contain a new capability request
3119 		 * to enable hardware checksum offload.
3120 		 */
3121 		uint_t	size;
3122 		uchar_t	*rptr;
3123 
3124 		size = sizeof (dl_capability_req_t) +
3125 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3126 
3127 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3128 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3129 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3130 			    ill->ill_name);
3131 			return;
3132 		}
3133 
3134 		rptr = nmp->b_rptr;
3135 		/* initialize dl_capability_req_t */
3136 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3137 		ocap->dl_sub_offset =
3138 		    sizeof (dl_capability_req_t);
3139 		ocap->dl_sub_length =
3140 		    sizeof (dl_capability_sub_t) +
3141 		    isub->dl_length;
3142 		nmp->b_rptr += sizeof (dl_capability_req_t);
3143 
3144 		/* initialize dl_capability_sub_t */
3145 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3146 		nmp->b_rptr += sizeof (*isub);
3147 
3148 		/* initialize dl_capab_hcksum_t */
3149 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3150 		bcopy(ihck, ohck, sizeof (*ihck));
3151 
3152 		nmp->b_rptr = rptr;
3153 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3154 
3155 		/* Set ENABLE flag */
3156 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3157 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3158 
3159 		/*
3160 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3161 		 * hardware checksum acceleration.
3162 		 */
3163 		ill_dlpi_send(ill, nmp);
3164 	} else {
3165 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3166 		    "advertised %x hardware checksum capability flags\n",
3167 		    ill->ill_name, ihck->hcksum_txflags));
3168 	}
3169 }
3170 
3171 static void
3172 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3173 {
3174 	mblk_t *mp;
3175 	dl_capab_hcksum_t *hck_subcap;
3176 	dl_capability_sub_t *dl_subcap;
3177 	int size;
3178 
3179 	if (!ILL_HCKSUM_CAPABLE(ill))
3180 		return;
3181 
3182 	ASSERT(ill->ill_hcksum_capab != NULL);
3183 	/*
3184 	 * Clear the capability flag for hardware checksum offload but
3185 	 * retain the ill_hcksum_capab structure since it's possible that
3186 	 * another thread is still referring to it.  The structure only
3187 	 * gets deallocated when we destroy the ill.
3188 	 */
3189 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3190 
3191 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3192 
3193 	mp = allocb(size, BPRI_HI);
3194 	if (mp == NULL) {
3195 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3196 		    "request to disable hardware checksum offload\n"));
3197 		return;
3198 	}
3199 
3200 	mp->b_wptr = mp->b_rptr + size;
3201 
3202 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3203 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3204 	dl_subcap->dl_length = sizeof (*hck_subcap);
3205 
3206 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3207 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3208 	hck_subcap->hcksum_txflags = 0;
3209 
3210 	if (*sc_mp != NULL)
3211 		linkb(*sc_mp, mp);
3212 	else
3213 		*sc_mp = mp;
3214 }
3215 
3216 static void
3217 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3218 {
3219 	mblk_t *nmp = NULL;
3220 	dl_capability_req_t *oc;
3221 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3222 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3223 	uint_t sub_dl_cap = isub->dl_cap;
3224 	uint8_t *capend;
3225 
3226 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3227 
3228 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3229 
3230 	/*
3231 	 * Note: range checks here are not absolutely sufficient to
3232 	 * make us robust against malformed messages sent by drivers;
3233 	 * this is in keeping with the rest of IP's dlpi handling.
3234 	 * (Remember, it's coming from something else in the kernel
3235 	 * address space)
3236 	 */
3237 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3238 	if (capend > mp->b_wptr) {
3239 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3240 		    "malformed sub-capability too long for mblk");
3241 		return;
3242 	}
3243 
3244 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3245 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3246 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3247 		    "unsupported ZEROCOPY sub-capability (version %d, "
3248 		    "expected %d)", zc_ic->zerocopy_version,
3249 		    ZEROCOPY_VERSION_1);
3250 		return;
3251 	}
3252 
3253 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3254 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3255 		    "capability isn't as expected; pass-thru module(s) "
3256 		    "detected, discarding capability\n"));
3257 		return;
3258 	}
3259 
3260 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3261 		if (*ill_zerocopy_capab == NULL) {
3262 			*ill_zerocopy_capab =
3263 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3264 			    KM_NOSLEEP);
3265 
3266 			if (*ill_zerocopy_capab == NULL) {
3267 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3268 				    "could not enable Zero-copy version %d "
3269 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3270 				    ill->ill_name);
3271 				return;
3272 			}
3273 		}
3274 
3275 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3276 		    "supports Zero-copy version %d\n", ill->ill_name,
3277 		    ZEROCOPY_VERSION_1));
3278 
3279 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3280 		    zc_ic->zerocopy_version;
3281 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3282 		    zc_ic->zerocopy_flags;
3283 
3284 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3285 	} else {
3286 		uint_t size;
3287 		uchar_t *rptr;
3288 
3289 		size = sizeof (dl_capability_req_t) +
3290 		    sizeof (dl_capability_sub_t) +
3291 		    sizeof (dl_capab_zerocopy_t);
3292 
3293 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3294 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3295 			    "could not enable zerocopy for %s (ENOMEM)\n",
3296 			    ill->ill_name);
3297 			return;
3298 		}
3299 
3300 		rptr = nmp->b_rptr;
3301 		/* initialize dl_capability_req_t */
3302 		oc = (dl_capability_req_t *)rptr;
3303 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3304 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3305 		    sizeof (dl_capab_zerocopy_t);
3306 		rptr += sizeof (dl_capability_req_t);
3307 
3308 		/* initialize dl_capability_sub_t */
3309 		bcopy(isub, rptr, sizeof (*isub));
3310 		rptr += sizeof (*isub);
3311 
3312 		/* initialize dl_capab_zerocopy_t */
3313 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3314 		*zc_oc = *zc_ic;
3315 
3316 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3317 		    "to enable zero-copy version %d\n", ill->ill_name,
3318 		    ZEROCOPY_VERSION_1));
3319 
3320 		/* set VMSAFE_MEM flag */
3321 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3322 
3323 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3324 		ill_dlpi_send(ill, nmp);
3325 	}
3326 }
3327 
3328 static void
3329 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3330 {
3331 	mblk_t *mp;
3332 	dl_capab_zerocopy_t *zerocopy_subcap;
3333 	dl_capability_sub_t *dl_subcap;
3334 	int size;
3335 
3336 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3337 		return;
3338 
3339 	ASSERT(ill->ill_zerocopy_capab != NULL);
3340 	/*
3341 	 * Clear the capability flag for Zero-copy but retain the
3342 	 * ill_zerocopy_capab structure since it's possible that another
3343 	 * thread is still referring to it.  The structure only gets
3344 	 * deallocated when we destroy the ill.
3345 	 */
3346 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3347 
3348 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3349 
3350 	mp = allocb(size, BPRI_HI);
3351 	if (mp == NULL) {
3352 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3353 		    "request to disable Zero-copy\n"));
3354 		return;
3355 	}
3356 
3357 	mp->b_wptr = mp->b_rptr + size;
3358 
3359 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3360 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3361 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3362 
3363 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3364 	zerocopy_subcap->zerocopy_version =
3365 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3366 	zerocopy_subcap->zerocopy_flags = 0;
3367 
3368 	if (*sc_mp != NULL)
3369 		linkb(*sc_mp, mp);
3370 	else
3371 		*sc_mp = mp;
3372 }
3373 
3374 /*
3375  * Process Large Segment Offload capability negotiation ack received from a
3376  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3377  * DL_CAPABILITY_ACK message.
3378  */
3379 static void
3380 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3381 {
3382 	mblk_t *nmp = NULL;
3383 	dl_capability_req_t *oc;
3384 	dl_capab_lso_t *lso_ic, *lso_oc;
3385 	ill_lso_capab_t **ill_lso_capab;
3386 	uint_t sub_dl_cap = isub->dl_cap;
3387 	uint8_t *capend;
3388 
3389 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3390 
3391 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3392 
3393 	/*
3394 	 * Note: range checks here are not absolutely sufficient to
3395 	 * make us robust against malformed messages sent by drivers;
3396 	 * this is in keeping with the rest of IP's dlpi handling.
3397 	 * (Remember, it's coming from something else in the kernel
3398 	 * address space)
3399 	 */
3400 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3401 	if (capend > mp->b_wptr) {
3402 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3403 		    "malformed sub-capability too long for mblk");
3404 		return;
3405 	}
3406 
3407 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3408 
3409 	if (lso_ic->lso_version != LSO_VERSION_1) {
3410 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3411 		    "unsupported LSO sub-capability (version %d, expected %d)",
3412 		    lso_ic->lso_version, LSO_VERSION_1);
3413 		return;
3414 	}
3415 
3416 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3417 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3418 		    "capability isn't as expected; pass-thru module(s) "
3419 		    "detected, discarding capability\n"));
3420 		return;
3421 	}
3422 
3423 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3424 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3425 		if (*ill_lso_capab == NULL) {
3426 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3427 			    KM_NOSLEEP);
3428 
3429 			if (*ill_lso_capab == NULL) {
3430 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3431 				    "could not enable LSO version %d "
3432 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3433 				    ill->ill_name);
3434 				return;
3435 			}
3436 		}
3437 
3438 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3439 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3440 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3441 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3442 
3443 		ip1dbg(("ill_capability_lso_ack: interface %s "
3444 		    "has enabled LSO\n ", ill->ill_name));
3445 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3446 		uint_t size;
3447 		uchar_t *rptr;
3448 
3449 		size = sizeof (dl_capability_req_t) +
3450 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3451 
3452 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3453 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3454 			    "could not enable LSO for %s (ENOMEM)\n",
3455 			    ill->ill_name);
3456 			return;
3457 		}
3458 
3459 		rptr = nmp->b_rptr;
3460 		/* initialize dl_capability_req_t */
3461 		oc = (dl_capability_req_t *)nmp->b_rptr;
3462 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3463 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3464 		    sizeof (dl_capab_lso_t);
3465 		nmp->b_rptr += sizeof (dl_capability_req_t);
3466 
3467 		/* initialize dl_capability_sub_t */
3468 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3469 		nmp->b_rptr += sizeof (*isub);
3470 
3471 		/* initialize dl_capab_lso_t */
3472 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3473 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3474 
3475 		nmp->b_rptr = rptr;
3476 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3477 
3478 		/* set ENABLE flag */
3479 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3480 
3481 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3482 		ill_dlpi_send(ill, nmp);
3483 	} else {
3484 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3485 		    "advertised %x LSO capability flags\n",
3486 		    ill->ill_name, lso_ic->lso_flags));
3487 	}
3488 }
3489 
3490 
3491 static void
3492 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3493 {
3494 	mblk_t *mp;
3495 	dl_capab_lso_t *lso_subcap;
3496 	dl_capability_sub_t *dl_subcap;
3497 	int size;
3498 
3499 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3500 		return;
3501 
3502 	ASSERT(ill->ill_lso_capab != NULL);
3503 	/*
3504 	 * Clear the capability flag for LSO but retain the
3505 	 * ill_lso_capab structure since it's possible that another
3506 	 * thread is still referring to it.  The structure only gets
3507 	 * deallocated when we destroy the ill.
3508 	 */
3509 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3510 
3511 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3512 
3513 	mp = allocb(size, BPRI_HI);
3514 	if (mp == NULL) {
3515 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3516 		    "request to disable LSO\n"));
3517 		return;
3518 	}
3519 
3520 	mp->b_wptr = mp->b_rptr + size;
3521 
3522 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3523 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3524 	dl_subcap->dl_length = sizeof (*lso_subcap);
3525 
3526 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3527 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3528 	lso_subcap->lso_flags = 0;
3529 
3530 	if (*sc_mp != NULL)
3531 		linkb(*sc_mp, mp);
3532 	else
3533 		*sc_mp = mp;
3534 }
3535 
3536 /*
3537  * Consume a new-style hardware capabilities negotiation ack.
3538  * Called from ip_rput_dlpi_writer().
3539  */
3540 void
3541 ill_capability_ack(ill_t *ill, mblk_t *mp)
3542 {
3543 	dl_capability_ack_t *capp;
3544 	dl_capability_sub_t *subp, *endp;
3545 
3546 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3547 		ill->ill_dlpi_capab_state = IDS_OK;
3548 
3549 	capp = (dl_capability_ack_t *)mp->b_rptr;
3550 
3551 	if (capp->dl_sub_length == 0)
3552 		/* no new-style capabilities */
3553 		return;
3554 
3555 	/* make sure the driver supplied correct dl_sub_length */
3556 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3557 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3558 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3559 		return;
3560 	}
3561 
3562 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3563 	/*
3564 	 * There are sub-capabilities. Process the ones we know about.
3565 	 * Loop until we don't have room for another sub-cap header..
3566 	 */
3567 	for (subp = SC(capp, capp->dl_sub_offset),
3568 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3569 	    subp <= endp;
3570 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3571 
3572 		switch (subp->dl_cap) {
3573 		case DL_CAPAB_ID_WRAPPER:
3574 			ill_capability_id_ack(ill, mp, subp);
3575 			break;
3576 		default:
3577 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3578 			break;
3579 		}
3580 	}
3581 #undef SC
3582 }
3583 
3584 /*
3585  * This routine is called to scan the fragmentation reassembly table for
3586  * the specified ILL for any packets that are starting to smell.
3587  * dead_interval is the maximum time in seconds that will be tolerated.  It
3588  * will either be the value specified in ip_g_frag_timeout, or zero if the
3589  * ILL is shutting down and it is time to blow everything off.
3590  *
3591  * It returns the number of seconds (as a time_t) that the next frag timer
3592  * should be scheduled for, 0 meaning that the timer doesn't need to be
3593  * re-started.  Note that the method of calculating next_timeout isn't
3594  * entirely accurate since time will flow between the time we grab
3595  * current_time and the time we schedule the next timeout.  This isn't a
3596  * big problem since this is the timer for sending an ICMP reassembly time
3597  * exceeded messages, and it doesn't have to be exactly accurate.
3598  *
3599  * This function is
3600  * sometimes called as writer, although this is not required.
3601  */
3602 time_t
3603 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3604 {
3605 	ipfb_t	*ipfb;
3606 	ipfb_t	*endp;
3607 	ipf_t	*ipf;
3608 	ipf_t	*ipfnext;
3609 	mblk_t	*mp;
3610 	time_t	current_time = gethrestime_sec();
3611 	time_t	next_timeout = 0;
3612 	uint32_t	hdr_length;
3613 	mblk_t	*send_icmp_head;
3614 	mblk_t	*send_icmp_head_v6;
3615 	zoneid_t zoneid;
3616 	ip_stack_t *ipst = ill->ill_ipst;
3617 
3618 	ipfb = ill->ill_frag_hash_tbl;
3619 	if (ipfb == NULL)
3620 		return (B_FALSE);
3621 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3622 	/* Walk the frag hash table. */
3623 	for (; ipfb < endp; ipfb++) {
3624 		send_icmp_head = NULL;
3625 		send_icmp_head_v6 = NULL;
3626 		mutex_enter(&ipfb->ipfb_lock);
3627 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3628 			time_t frag_time = current_time - ipf->ipf_timestamp;
3629 			time_t frag_timeout;
3630 
3631 			if (frag_time < dead_interval) {
3632 				/*
3633 				 * There are some outstanding fragments
3634 				 * that will timeout later.  Make note of
3635 				 * the time so that we can reschedule the
3636 				 * next timeout appropriately.
3637 				 */
3638 				frag_timeout = dead_interval - frag_time;
3639 				if (next_timeout == 0 ||
3640 				    frag_timeout < next_timeout) {
3641 					next_timeout = frag_timeout;
3642 				}
3643 				break;
3644 			}
3645 			/* Time's up.  Get it out of here. */
3646 			hdr_length = ipf->ipf_nf_hdr_len;
3647 			ipfnext = ipf->ipf_hash_next;
3648 			if (ipfnext)
3649 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3650 			*ipf->ipf_ptphn = ipfnext;
3651 			mp = ipf->ipf_mp->b_cont;
3652 			for (; mp; mp = mp->b_cont) {
3653 				/* Extra points for neatness. */
3654 				IP_REASS_SET_START(mp, 0);
3655 				IP_REASS_SET_END(mp, 0);
3656 			}
3657 			mp = ipf->ipf_mp->b_cont;
3658 			ill->ill_frag_count -= ipf->ipf_count;
3659 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3660 			ipfb->ipfb_count -= ipf->ipf_count;
3661 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3662 			ipfb->ipfb_frag_pkts--;
3663 			/*
3664 			 * We do not send any icmp message from here because
3665 			 * we currently are holding the ipfb_lock for this
3666 			 * hash chain. If we try and send any icmp messages
3667 			 * from here we may end up via a put back into ip
3668 			 * trying to get the same lock, causing a recursive
3669 			 * mutex panic. Instead we build a list and send all
3670 			 * the icmp messages after we have dropped the lock.
3671 			 */
3672 			if (ill->ill_isv6) {
3673 				if (hdr_length != 0) {
3674 					mp->b_next = send_icmp_head_v6;
3675 					send_icmp_head_v6 = mp;
3676 				} else {
3677 					freemsg(mp);
3678 				}
3679 			} else {
3680 				if (hdr_length != 0) {
3681 					mp->b_next = send_icmp_head;
3682 					send_icmp_head = mp;
3683 				} else {
3684 					freemsg(mp);
3685 				}
3686 			}
3687 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3688 			freeb(ipf->ipf_mp);
3689 		}
3690 		mutex_exit(&ipfb->ipfb_lock);
3691 		/*
3692 		 * Now need to send any icmp messages that we delayed from
3693 		 * above.
3694 		 */
3695 		while (send_icmp_head_v6 != NULL) {
3696 			ip6_t *ip6h;
3697 
3698 			mp = send_icmp_head_v6;
3699 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3700 			mp->b_next = NULL;
3701 			if (mp->b_datap->db_type == M_CTL)
3702 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3703 			else
3704 				ip6h = (ip6_t *)mp->b_rptr;
3705 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3706 			    ill, ipst);
3707 			if (zoneid == ALL_ZONES) {
3708 				freemsg(mp);
3709 			} else {
3710 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3711 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3712 				    B_FALSE, zoneid, ipst);
3713 			}
3714 		}
3715 		while (send_icmp_head != NULL) {
3716 			ipaddr_t dst;
3717 
3718 			mp = send_icmp_head;
3719 			send_icmp_head = send_icmp_head->b_next;
3720 			mp->b_next = NULL;
3721 
3722 			if (mp->b_datap->db_type == M_CTL)
3723 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3724 			else
3725 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3726 
3727 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3728 			if (zoneid == ALL_ZONES) {
3729 				freemsg(mp);
3730 			} else {
3731 				icmp_time_exceeded(ill->ill_wq, mp,
3732 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3733 				    ipst);
3734 			}
3735 		}
3736 	}
3737 	/*
3738 	 * A non-dying ILL will use the return value to decide whether to
3739 	 * restart the frag timer, and for how long.
3740 	 */
3741 	return (next_timeout);
3742 }
3743 
3744 /*
3745  * This routine is called when the approximate count of mblk memory used
3746  * for the specified ILL has exceeded max_count.
3747  */
3748 void
3749 ill_frag_prune(ill_t *ill, uint_t max_count)
3750 {
3751 	ipfb_t	*ipfb;
3752 	ipf_t	*ipf;
3753 	size_t	count;
3754 
3755 	/*
3756 	 * If we are here within ip_min_frag_prune_time msecs remove
3757 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3758 	 * ill_frag_free_num_pkts.
3759 	 */
3760 	mutex_enter(&ill->ill_lock);
3761 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3762 	    (ip_min_frag_prune_time != 0 ?
3763 	    ip_min_frag_prune_time : msec_per_tick)) {
3764 
3765 		ill->ill_frag_free_num_pkts++;
3766 
3767 	} else {
3768 		ill->ill_frag_free_num_pkts = 0;
3769 	}
3770 	ill->ill_last_frag_clean_time = lbolt;
3771 	mutex_exit(&ill->ill_lock);
3772 
3773 	/*
3774 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3775 	 */
3776 	if (ill->ill_frag_free_num_pkts != 0) {
3777 		int ix;
3778 
3779 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3780 			ipfb = &ill->ill_frag_hash_tbl[ix];
3781 			mutex_enter(&ipfb->ipfb_lock);
3782 			if (ipfb->ipfb_ipf != NULL) {
3783 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3784 				    ill->ill_frag_free_num_pkts);
3785 			}
3786 			mutex_exit(&ipfb->ipfb_lock);
3787 		}
3788 	}
3789 	/*
3790 	 * While the reassembly list for this ILL is too big, prune a fragment
3791 	 * queue by age, oldest first.  Note that the per ILL count is
3792 	 * approximate, while the per frag hash bucket counts are accurate.
3793 	 */
3794 	while (ill->ill_frag_count > max_count) {
3795 		int	ix;
3796 		ipfb_t	*oipfb = NULL;
3797 		uint_t	oldest = UINT_MAX;
3798 
3799 		count = 0;
3800 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3801 			ipfb = &ill->ill_frag_hash_tbl[ix];
3802 			mutex_enter(&ipfb->ipfb_lock);
3803 			ipf = ipfb->ipfb_ipf;
3804 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3805 				oldest = ipf->ipf_gen;
3806 				oipfb = ipfb;
3807 			}
3808 			count += ipfb->ipfb_count;
3809 			mutex_exit(&ipfb->ipfb_lock);
3810 		}
3811 		/* Refresh the per ILL count */
3812 		ill->ill_frag_count = count;
3813 		if (oipfb == NULL) {
3814 			ill->ill_frag_count = 0;
3815 			break;
3816 		}
3817 		if (count <= max_count)
3818 			return;	/* Somebody beat us to it, nothing to do */
3819 		mutex_enter(&oipfb->ipfb_lock);
3820 		ipf = oipfb->ipfb_ipf;
3821 		if (ipf != NULL) {
3822 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3823 		}
3824 		mutex_exit(&oipfb->ipfb_lock);
3825 	}
3826 }
3827 
3828 /*
3829  * free 'free_cnt' fragmented packets starting at ipf.
3830  */
3831 void
3832 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3833 {
3834 	size_t	count;
3835 	mblk_t	*mp;
3836 	mblk_t	*tmp;
3837 	ipf_t **ipfp = ipf->ipf_ptphn;
3838 
3839 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3840 	ASSERT(ipfp != NULL);
3841 	ASSERT(ipf != NULL);
3842 
3843 	while (ipf != NULL && free_cnt-- > 0) {
3844 		count = ipf->ipf_count;
3845 		mp = ipf->ipf_mp;
3846 		ipf = ipf->ipf_hash_next;
3847 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3848 			IP_REASS_SET_START(tmp, 0);
3849 			IP_REASS_SET_END(tmp, 0);
3850 		}
3851 		ill->ill_frag_count -= count;
3852 		ASSERT(ipfb->ipfb_count >= count);
3853 		ipfb->ipfb_count -= count;
3854 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3855 		ipfb->ipfb_frag_pkts--;
3856 		freemsg(mp);
3857 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3858 	}
3859 
3860 	if (ipf)
3861 		ipf->ipf_ptphn = ipfp;
3862 	ipfp[0] = ipf;
3863 }
3864 
3865 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3866 	"obsolete and may be removed in a future release of Solaris.  Use " \
3867 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3868 
3869 /*
3870  * For obsolete per-interface forwarding configuration;
3871  * called in response to ND_GET.
3872  */
3873 /* ARGSUSED */
3874 static int
3875 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3876 {
3877 	ill_t *ill = (ill_t *)cp;
3878 
3879 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3880 
3881 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3882 	return (0);
3883 }
3884 
3885 /*
3886  * For obsolete per-interface forwarding configuration;
3887  * called in response to ND_SET.
3888  */
3889 /* ARGSUSED */
3890 static int
3891 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3892     cred_t *ioc_cr)
3893 {
3894 	long value;
3895 	int retval;
3896 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
3897 
3898 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3899 
3900 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3901 	    value < 0 || value > 1) {
3902 		return (EINVAL);
3903 	}
3904 
3905 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3906 	retval = ill_forward_set((ill_t *)cp, (value != 0));
3907 	rw_exit(&ipst->ips_ill_g_lock);
3908 	return (retval);
3909 }
3910 
3911 /*
3912  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3913  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3914  * up RTS_IFINFO routing socket messages for each interface whose flags we
3915  * change.
3916  */
3917 int
3918 ill_forward_set(ill_t *ill, boolean_t enable)
3919 {
3920 	ill_group_t *illgrp;
3921 	ip_stack_t	*ipst = ill->ill_ipst;
3922 
3923 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
3924 
3925 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3926 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
3927 		return (0);
3928 
3929 	if (IS_LOOPBACK(ill))
3930 		return (EINVAL);
3931 
3932 	/*
3933 	 * If the ill is in an IPMP group, set the forwarding policy on all
3934 	 * members of the group to the same value.
3935 	 */
3936 	illgrp = ill->ill_group;
3937 	if (illgrp != NULL) {
3938 		ill_t *tmp_ill;
3939 
3940 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3941 		    tmp_ill = tmp_ill->ill_group_next) {
3942 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3943 			    (enable ? "Enabling" : "Disabling"),
3944 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3945 			    tmp_ill->ill_name));
3946 			mutex_enter(&tmp_ill->ill_lock);
3947 			if (enable)
3948 				tmp_ill->ill_flags |= ILLF_ROUTER;
3949 			else
3950 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3951 			mutex_exit(&tmp_ill->ill_lock);
3952 			if (tmp_ill->ill_isv6)
3953 				ill_set_nce_router_flags(tmp_ill, enable);
3954 			/* Notify routing socket listeners of this change. */
3955 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3956 		}
3957 	} else {
3958 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3959 		    (enable ? "Enabling" : "Disabling"),
3960 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3961 		mutex_enter(&ill->ill_lock);
3962 		if (enable)
3963 			ill->ill_flags |= ILLF_ROUTER;
3964 		else
3965 			ill->ill_flags &= ~ILLF_ROUTER;
3966 		mutex_exit(&ill->ill_lock);
3967 		if (ill->ill_isv6)
3968 			ill_set_nce_router_flags(ill, enable);
3969 		/* Notify routing socket listeners of this change. */
3970 		ip_rts_ifmsg(ill->ill_ipif);
3971 	}
3972 
3973 	return (0);
3974 }
3975 
3976 /*
3977  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3978  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3979  * set or clear.
3980  */
3981 static void
3982 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3983 {
3984 	ipif_t *ipif;
3985 	nce_t *nce;
3986 
3987 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3988 		nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3989 		if (nce != NULL) {
3990 			mutex_enter(&nce->nce_lock);
3991 			if (enable)
3992 				nce->nce_flags |= NCE_F_ISROUTER;
3993 			else
3994 				nce->nce_flags &= ~NCE_F_ISROUTER;
3995 			mutex_exit(&nce->nce_lock);
3996 			NCE_REFRELE(nce);
3997 		}
3998 	}
3999 }
4000 
4001 /*
4002  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
4003  * for this ill.  Make sure the v6/v4 question has been answered about this
4004  * ill.  The creation of this ndd variable is only for backwards compatibility.
4005  * The preferred way to control per-interface IP forwarding is through the
4006  * ILLF_ROUTER interface flag.
4007  */
4008 static int
4009 ill_set_ndd_name(ill_t *ill)
4010 {
4011 	char *suffix;
4012 	ip_stack_t	*ipst = ill->ill_ipst;
4013 
4014 	ASSERT(IAM_WRITER_ILL(ill));
4015 
4016 	if (ill->ill_isv6)
4017 		suffix = ipv6_forward_suffix;
4018 	else
4019 		suffix = ipv4_forward_suffix;
4020 
4021 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
4022 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
4023 	/*
4024 	 * Copies over the '\0'.
4025 	 * Note that strlen(suffix) is always bounded.
4026 	 */
4027 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
4028 	    strlen(suffix) + 1);
4029 
4030 	/*
4031 	 * Use of the nd table requires holding the reader lock.
4032 	 * Modifying the nd table thru nd_load/nd_unload requires
4033 	 * the writer lock.
4034 	 */
4035 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
4036 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
4037 	    nd_ill_forward_set, (caddr_t)ill)) {
4038 		/*
4039 		 * If the nd_load failed, it only meant that it could not
4040 		 * allocate a new bunch of room for further NDD expansion.
4041 		 * Because of that, the ill_ndd_name will be set to 0, and
4042 		 * this interface is at the mercy of the global ip_forwarding
4043 		 * variable.
4044 		 */
4045 		rw_exit(&ipst->ips_ip_g_nd_lock);
4046 		ill->ill_ndd_name = NULL;
4047 		return (ENOMEM);
4048 	}
4049 	rw_exit(&ipst->ips_ip_g_nd_lock);
4050 	return (0);
4051 }
4052 
4053 /*
4054  * Intializes the context structure and returns the first ill in the list
4055  * cuurently start_list and end_list can have values:
4056  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
4057  * IP_V4_G_HEAD		Traverse IPV4 list only.
4058  * IP_V6_G_HEAD		Traverse IPV6 list only.
4059  */
4060 
4061 /*
4062  * We don't check for CONDEMNED ills here. Caller must do that if
4063  * necessary under the ill lock.
4064  */
4065 ill_t *
4066 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
4067     ip_stack_t *ipst)
4068 {
4069 	ill_if_t *ifp;
4070 	ill_t *ill;
4071 	avl_tree_t *avl_tree;
4072 
4073 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4074 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
4075 
4076 	/*
4077 	 * setup the lists to search
4078 	 */
4079 	if (end_list != MAX_G_HEADS) {
4080 		ctx->ctx_current_list = start_list;
4081 		ctx->ctx_last_list = end_list;
4082 	} else {
4083 		ctx->ctx_last_list = MAX_G_HEADS - 1;
4084 		ctx->ctx_current_list = 0;
4085 	}
4086 
4087 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
4088 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4089 		if (ifp != (ill_if_t *)
4090 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4091 			avl_tree = &ifp->illif_avl_by_ppa;
4092 			ill = avl_first(avl_tree);
4093 			/*
4094 			 * ill is guaranteed to be non NULL or ifp should have
4095 			 * not existed.
4096 			 */
4097 			ASSERT(ill != NULL);
4098 			return (ill);
4099 		}
4100 		ctx->ctx_current_list++;
4101 	}
4102 
4103 	return (NULL);
4104 }
4105 
4106 /*
4107  * returns the next ill in the list. ill_first() must have been called
4108  * before calling ill_next() or bad things will happen.
4109  */
4110 
4111 /*
4112  * We don't check for CONDEMNED ills here. Caller must do that if
4113  * necessary under the ill lock.
4114  */
4115 ill_t *
4116 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
4117 {
4118 	ill_if_t *ifp;
4119 	ill_t *ill;
4120 	ip_stack_t	*ipst = lastill->ill_ipst;
4121 
4122 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
4123 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
4124 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
4125 	    AVL_AFTER)) != NULL) {
4126 		return (ill);
4127 	}
4128 
4129 	/* goto next ill_ifp in the list. */
4130 	ifp = lastill->ill_ifptr->illif_next;
4131 
4132 	/* make sure not at end of circular list */
4133 	while (ifp ==
4134 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
4135 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
4136 			return (NULL);
4137 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
4138 	}
4139 
4140 	return (avl_first(&ifp->illif_avl_by_ppa));
4141 }
4142 
4143 /*
4144  * Check interface name for correct format which is name+ppa.
4145  * name can contain characters and digits, the right most digits
4146  * make up the ppa number. use of octal is not allowed, name must contain
4147  * a ppa, return pointer to the start of ppa.
4148  * In case of error return NULL.
4149  */
4150 static char *
4151 ill_get_ppa_ptr(char *name)
4152 {
4153 	int namelen = mi_strlen(name);
4154 
4155 	int len = namelen;
4156 
4157 	name += len;
4158 	while (len > 0) {
4159 		name--;
4160 		if (*name < '0' || *name > '9')
4161 			break;
4162 		len--;
4163 	}
4164 
4165 	/* empty string, all digits, or no trailing digits */
4166 	if (len == 0 || len == (int)namelen)
4167 		return (NULL);
4168 
4169 	name++;
4170 	/* check for attempted use of octal */
4171 	if (*name == '0' && len != (int)namelen - 1)
4172 		return (NULL);
4173 	return (name);
4174 }
4175 
4176 /*
4177  * use avl tree to locate the ill.
4178  */
4179 static ill_t *
4180 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4181     ipsq_func_t func, int *error, ip_stack_t *ipst)
4182 {
4183 	char *ppa_ptr = NULL;
4184 	int len;
4185 	uint_t ppa;
4186 	ill_t *ill = NULL;
4187 	ill_if_t *ifp;
4188 	int list;
4189 	ipsq_t *ipsq;
4190 
4191 	if (error != NULL)
4192 		*error = 0;
4193 
4194 	/*
4195 	 * get ppa ptr
4196 	 */
4197 	if (isv6)
4198 		list = IP_V6_G_HEAD;
4199 	else
4200 		list = IP_V4_G_HEAD;
4201 
4202 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4203 		if (error != NULL)
4204 			*error = ENXIO;
4205 		return (NULL);
4206 	}
4207 
4208 	len = ppa_ptr - name + 1;
4209 
4210 	ppa = stoi(&ppa_ptr);
4211 
4212 	ifp = IP_VX_ILL_G_LIST(list, ipst);
4213 
4214 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4215 		/*
4216 		 * match is done on len - 1 as the name is not null
4217 		 * terminated it contains ppa in addition to the interface
4218 		 * name.
4219 		 */
4220 		if ((ifp->illif_name_len == len) &&
4221 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4222 			break;
4223 		} else {
4224 			ifp = ifp->illif_next;
4225 		}
4226 	}
4227 
4228 
4229 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
4230 		/*
4231 		 * Even the interface type does not exist.
4232 		 */
4233 		if (error != NULL)
4234 			*error = ENXIO;
4235 		return (NULL);
4236 	}
4237 
4238 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4239 	if (ill != NULL) {
4240 		/*
4241 		 * The block comment at the start of ipif_down
4242 		 * explains the use of the macros used below
4243 		 */
4244 		GRAB_CONN_LOCK(q);
4245 		mutex_enter(&ill->ill_lock);
4246 		if (ILL_CAN_LOOKUP(ill)) {
4247 			ill_refhold_locked(ill);
4248 			mutex_exit(&ill->ill_lock);
4249 			RELEASE_CONN_LOCK(q);
4250 			return (ill);
4251 		} else if (ILL_CAN_WAIT(ill, q)) {
4252 			ipsq = ill->ill_phyint->phyint_ipsq;
4253 			mutex_enter(&ipsq->ipsq_lock);
4254 			mutex_exit(&ill->ill_lock);
4255 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4256 			mutex_exit(&ipsq->ipsq_lock);
4257 			RELEASE_CONN_LOCK(q);
4258 			*error = EINPROGRESS;
4259 			return (NULL);
4260 		}
4261 		mutex_exit(&ill->ill_lock);
4262 		RELEASE_CONN_LOCK(q);
4263 	}
4264 	if (error != NULL)
4265 		*error = ENXIO;
4266 	return (NULL);
4267 }
4268 
4269 /*
4270  * comparison function for use with avl.
4271  */
4272 static int
4273 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4274 {
4275 	uint_t ppa;
4276 	uint_t ill_ppa;
4277 
4278 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4279 
4280 	ppa = *((uint_t *)ppa_ptr);
4281 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4282 	/*
4283 	 * We want the ill with the lowest ppa to be on the
4284 	 * top.
4285 	 */
4286 	if (ill_ppa < ppa)
4287 		return (1);
4288 	if (ill_ppa > ppa)
4289 		return (-1);
4290 	return (0);
4291 }
4292 
4293 /*
4294  * remove an interface type from the global list.
4295  */
4296 static void
4297 ill_delete_interface_type(ill_if_t *interface)
4298 {
4299 	ASSERT(interface != NULL);
4300 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4301 
4302 	avl_destroy(&interface->illif_avl_by_ppa);
4303 	if (interface->illif_ppa_arena != NULL)
4304 		vmem_destroy(interface->illif_ppa_arena);
4305 
4306 	remque(interface);
4307 
4308 	mi_free(interface);
4309 }
4310 
4311 /* Defined in ip_netinfo.c */
4312 extern ddi_taskq_t	*eventq_queue_nic;
4313 
4314 /*
4315  * remove ill from the global list.
4316  */
4317 static void
4318 ill_glist_delete(ill_t *ill)
4319 {
4320 	char *nicname;
4321 	size_t nicnamelen;
4322 	hook_nic_event_t *info;
4323 	ip_stack_t	*ipst;
4324 
4325 	if (ill == NULL)
4326 		return;
4327 	ipst = ill->ill_ipst;
4328 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4329 
4330 	if (ill->ill_name != NULL) {
4331 		nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP);
4332 		if (nicname != NULL) {
4333 			bcopy(ill->ill_name, nicname, ill->ill_name_length);
4334 			nicnamelen = ill->ill_name_length;
4335 		}
4336 	} else {
4337 		nicname = NULL;
4338 		nicnamelen = 0;
4339 	}
4340 
4341 	/*
4342 	 * If the ill was never inserted into the AVL tree
4343 	 * we skip the if branch.
4344 	 */
4345 	if (ill->ill_ifptr != NULL) {
4346 		/*
4347 		 * remove from AVL tree and free ppa number
4348 		 */
4349 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4350 
4351 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4352 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4353 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4354 		}
4355 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4356 			ill_delete_interface_type(ill->ill_ifptr);
4357 		}
4358 
4359 		/*
4360 		 * Indicate ill is no longer in the list.
4361 		 */
4362 		ill->ill_ifptr = NULL;
4363 		ill->ill_name_length = 0;
4364 		ill->ill_name[0] = '\0';
4365 		ill->ill_ppa = UINT_MAX;
4366 	}
4367 
4368 	/*
4369 	 * Run the unplumb hook after the NIC has disappeared from being
4370 	 * visible so that attempts to revalidate its existance will fail.
4371 	 *
4372 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4373 	 * that the ordering of delivered events to listeners matches the
4374 	 * order of them in the kernel.
4375 	 */
4376 	if ((info = ill->ill_nic_event_info) != NULL) {
4377 		if (info->hne_event != NE_DOWN) {
4378 			ip2dbg(("ill_glist_delete: unexpected nic event %d "
4379 			    "attached for %s\n", info->hne_event,
4380 			    ill->ill_name));
4381 			if (info->hne_data != NULL)
4382 				kmem_free(info->hne_data, info->hne_datalen);
4383 			kmem_free(info, sizeof (hook_nic_event_t));
4384 		} else {
4385 			if (ddi_taskq_dispatch(eventq_queue_nic,
4386 			    ip_ne_queue_func, (void *)info, DDI_SLEEP)
4387 			    == DDI_FAILURE) {
4388 				ip2dbg(("ill_glist_delete: ddi_taskq_dispatch "
4389 				    "failed\n"));
4390 				if (info->hne_data != NULL)
4391 					kmem_free(info->hne_data,
4392 					    info->hne_datalen);
4393 				kmem_free(info, sizeof (hook_nic_event_t));
4394 			}
4395 		}
4396 	}
4397 
4398 	/* Generate NE_UNPLUMB event for ill_name. */
4399 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
4400 	if (info != NULL) {
4401 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
4402 		info->hne_lif = 0;
4403 		info->hne_event = NE_UNPLUMB;
4404 		info->hne_data = nicname;
4405 		info->hne_datalen = nicnamelen;
4406 		info->hne_family = ill->ill_isv6 ?
4407 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
4408 	} else {
4409 		ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event "
4410 		    "information for %s (ENOMEM)\n", ill->ill_name));
4411 		if (nicname != NULL)
4412 			kmem_free(nicname, nicnamelen);
4413 	}
4414 
4415 	ill->ill_nic_event_info = info;
4416 
4417 	ill_phyint_free(ill);
4418 	rw_exit(&ipst->ips_ill_g_lock);
4419 }
4420 
4421 /*
4422  * allocate a ppa, if the number of plumbed interfaces of this type are
4423  * less than ill_no_arena do a linear search to find a unused ppa.
4424  * When the number goes beyond ill_no_arena switch to using an arena.
4425  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4426  * is the return value for an error condition, so allocation starts at one
4427  * and is decremented by one.
4428  */
4429 static int
4430 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4431 {
4432 	ill_t *tmp_ill;
4433 	uint_t start, end;
4434 	int ppa;
4435 
4436 	if (ifp->illif_ppa_arena == NULL &&
4437 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4438 		/*
4439 		 * Create an arena.
4440 		 */
4441 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4442 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4443 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4444 			/* allocate what has already been assigned */
4445 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4446 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4447 		    tmp_ill, AVL_AFTER)) {
4448 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4449 			    1,		/* size */
4450 			    1,		/* align/quantum */
4451 			    0,		/* phase */
4452 			    0,		/* nocross */
4453 			    /* minaddr */
4454 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4455 			    /* maxaddr */
4456 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4457 			    VM_NOSLEEP|VM_FIRSTFIT);
4458 			if (ppa == 0) {
4459 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4460 				    " failed while switching"));
4461 				vmem_destroy(ifp->illif_ppa_arena);
4462 				ifp->illif_ppa_arena = NULL;
4463 				break;
4464 			}
4465 		}
4466 	}
4467 
4468 	if (ifp->illif_ppa_arena != NULL) {
4469 		if (ill->ill_ppa == UINT_MAX) {
4470 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4471 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4472 			if (ppa == 0)
4473 				return (EAGAIN);
4474 			ill->ill_ppa = --ppa;
4475 		} else {
4476 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4477 			    1, 		/* size */
4478 			    1, 		/* align/quantum */
4479 			    0, 		/* phase */
4480 			    0, 		/* nocross */
4481 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4482 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4483 			    VM_NOSLEEP|VM_FIRSTFIT);
4484 			/*
4485 			 * Most likely the allocation failed because
4486 			 * the requested ppa was in use.
4487 			 */
4488 			if (ppa == 0)
4489 				return (EEXIST);
4490 		}
4491 		return (0);
4492 	}
4493 
4494 	/*
4495 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4496 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4497 	 */
4498 	if (ill->ill_ppa == UINT_MAX) {
4499 		end = UINT_MAX - 1;
4500 		start = 0;
4501 	} else {
4502 		end = start = ill->ill_ppa;
4503 	}
4504 
4505 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4506 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4507 		if (start++ >= end) {
4508 			if (ill->ill_ppa == UINT_MAX)
4509 				return (EAGAIN);
4510 			else
4511 				return (EEXIST);
4512 		}
4513 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4514 	}
4515 	ill->ill_ppa = start;
4516 	return (0);
4517 }
4518 
4519 /*
4520  * Insert ill into the list of configured ill's. Once this function completes,
4521  * the ill is globally visible and is available through lookups. More precisely
4522  * this happens after the caller drops the ill_g_lock.
4523  */
4524 static int
4525 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4526 {
4527 	ill_if_t *ill_interface;
4528 	avl_index_t where = 0;
4529 	int error;
4530 	int name_length;
4531 	int index;
4532 	boolean_t check_length = B_FALSE;
4533 	ip_stack_t	*ipst = ill->ill_ipst;
4534 
4535 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4536 
4537 	name_length = mi_strlen(name) + 1;
4538 
4539 	if (isv6)
4540 		index = IP_V6_G_HEAD;
4541 	else
4542 		index = IP_V4_G_HEAD;
4543 
4544 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4545 	/*
4546 	 * Search for interface type based on name
4547 	 */
4548 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4549 		if ((ill_interface->illif_name_len == name_length) &&
4550 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4551 			break;
4552 		}
4553 		ill_interface = ill_interface->illif_next;
4554 	}
4555 
4556 	/*
4557 	 * Interface type not found, create one.
4558 	 */
4559 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4560 
4561 		ill_g_head_t ghead;
4562 
4563 		/*
4564 		 * allocate ill_if_t structure
4565 		 */
4566 
4567 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4568 		if (ill_interface == NULL) {
4569 			return (ENOMEM);
4570 		}
4571 
4572 
4573 
4574 		(void) strcpy(ill_interface->illif_name, name);
4575 		ill_interface->illif_name_len = name_length;
4576 
4577 		avl_create(&ill_interface->illif_avl_by_ppa,
4578 		    ill_compare_ppa, sizeof (ill_t),
4579 		    offsetof(struct ill_s, ill_avl_byppa));
4580 
4581 		/*
4582 		 * link the structure in the back to maintain order
4583 		 * of configuration for ifconfig output.
4584 		 */
4585 		ghead = ipst->ips_ill_g_heads[index];
4586 		insque(ill_interface, ghead.ill_g_list_tail);
4587 
4588 	}
4589 
4590 	if (ill->ill_ppa == UINT_MAX)
4591 		check_length = B_TRUE;
4592 
4593 	error = ill_alloc_ppa(ill_interface, ill);
4594 	if (error != 0) {
4595 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4596 			ill_delete_interface_type(ill->ill_ifptr);
4597 		return (error);
4598 	}
4599 
4600 	/*
4601 	 * When the ppa is choosen by the system, check that there is
4602 	 * enough space to insert ppa. if a specific ppa was passed in this
4603 	 * check is not required as the interface name passed in will have
4604 	 * the right ppa in it.
4605 	 */
4606 	if (check_length) {
4607 		/*
4608 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4609 		 */
4610 		char buf[sizeof (uint_t) * 3];
4611 
4612 		/*
4613 		 * convert ppa to string to calculate the amount of space
4614 		 * required for it in the name.
4615 		 */
4616 		numtos(ill->ill_ppa, buf);
4617 
4618 		/* Do we have enough space to insert ppa ? */
4619 
4620 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4621 			/* Free ppa and interface type struct */
4622 			if (ill_interface->illif_ppa_arena != NULL) {
4623 				vmem_free(ill_interface->illif_ppa_arena,
4624 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4625 			}
4626 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4627 			    0) {
4628 				ill_delete_interface_type(ill->ill_ifptr);
4629 			}
4630 
4631 			return (EINVAL);
4632 		}
4633 	}
4634 
4635 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4636 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4637 
4638 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4639 	    &where);
4640 	ill->ill_ifptr = ill_interface;
4641 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4642 
4643 	ill_phyint_reinit(ill);
4644 	return (0);
4645 }
4646 
4647 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4648 static boolean_t
4649 ipsq_init(ill_t *ill)
4650 {
4651 	ipsq_t  *ipsq;
4652 
4653 	/* Init the ipsq and impicitly enter as writer */
4654 	ill->ill_phyint->phyint_ipsq =
4655 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4656 	if (ill->ill_phyint->phyint_ipsq == NULL)
4657 		return (B_FALSE);
4658 	ipsq = ill->ill_phyint->phyint_ipsq;
4659 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4660 	ill->ill_phyint->phyint_ipsq_next = NULL;
4661 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4662 	ipsq->ipsq_refs = 1;
4663 	ipsq->ipsq_writer = curthread;
4664 	ipsq->ipsq_reentry_cnt = 1;
4665 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4666 #ifdef DEBUG
4667 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4668 	    IPSQ_STACK_DEPTH);
4669 #endif
4670 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4671 	return (B_TRUE);
4672 }
4673 
4674 /*
4675  * ill_init is called by ip_open when a device control stream is opened.
4676  * It does a few initializations, and shoots a DL_INFO_REQ message down
4677  * to the driver.  The response is later picked up in ip_rput_dlpi and
4678  * used to set up default mechanisms for talking to the driver.  (Always
4679  * called as writer.)
4680  *
4681  * If this function returns error, ip_open will call ip_close which in
4682  * turn will call ill_delete to clean up any memory allocated here that
4683  * is not yet freed.
4684  */
4685 int
4686 ill_init(queue_t *q, ill_t *ill)
4687 {
4688 	int	count;
4689 	dl_info_req_t	*dlir;
4690 	mblk_t	*info_mp;
4691 	uchar_t *frag_ptr;
4692 
4693 	/*
4694 	 * The ill is initialized to zero by mi_alloc*(). In addition
4695 	 * some fields already contain valid values, initialized in
4696 	 * ip_open(), before we reach here.
4697 	 */
4698 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4699 
4700 	ill->ill_rq = q;
4701 	ill->ill_wq = WR(q);
4702 
4703 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4704 	    BPRI_HI);
4705 	if (info_mp == NULL)
4706 		return (ENOMEM);
4707 
4708 	/*
4709 	 * Allocate sufficient space to contain our fragment hash table and
4710 	 * the device name.
4711 	 */
4712 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4713 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4714 	if (frag_ptr == NULL) {
4715 		freemsg(info_mp);
4716 		return (ENOMEM);
4717 	}
4718 	ill->ill_frag_ptr = frag_ptr;
4719 	ill->ill_frag_free_num_pkts = 0;
4720 	ill->ill_last_frag_clean_time = 0;
4721 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4722 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4723 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4724 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4725 		    NULL, MUTEX_DEFAULT, NULL);
4726 	}
4727 
4728 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4729 	if (ill->ill_phyint == NULL) {
4730 		freemsg(info_mp);
4731 		mi_free(frag_ptr);
4732 		return (ENOMEM);
4733 	}
4734 
4735 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4736 	/*
4737 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4738 	 * at this point because of the following reason. If we can't
4739 	 * enter the ipsq at some point and cv_wait, the writer that
4740 	 * wakes us up tries to locate us using the list of all phyints
4741 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4742 	 * If we don't set it now, we risk a missed wakeup.
4743 	 */
4744 	ill->ill_phyint->phyint_illv4 = ill;
4745 	ill->ill_ppa = UINT_MAX;
4746 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4747 
4748 	if (!ipsq_init(ill)) {
4749 		freemsg(info_mp);
4750 		mi_free(frag_ptr);
4751 		mi_free(ill->ill_phyint);
4752 		return (ENOMEM);
4753 	}
4754 
4755 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4756 
4757 
4758 	/* Frag queue limit stuff */
4759 	ill->ill_frag_count = 0;
4760 	ill->ill_ipf_gen = 0;
4761 
4762 	ill->ill_global_timer = INFINITY;
4763 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4764 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4765 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4766 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4767 
4768 	/*
4769 	 * Initialize IPv6 configuration variables.  The IP module is always
4770 	 * opened as an IPv4 module.  Instead tracking down the cases where
4771 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4772 	 * here for convenience, this has no effect until the ill is set to do
4773 	 * IPv6.
4774 	 */
4775 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4776 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4777 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4778 	ill->ill_max_buf = ND_MAX_Q;
4779 	ill->ill_refcnt = 0;
4780 
4781 	/* Send down the Info Request to the driver. */
4782 	info_mp->b_datap->db_type = M_PCPROTO;
4783 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4784 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4785 	dlir->dl_primitive = DL_INFO_REQ;
4786 
4787 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4788 
4789 	qprocson(q);
4790 	ill_dlpi_send(ill, info_mp);
4791 
4792 	return (0);
4793 }
4794 
4795 /*
4796  * ill_dls_info
4797  * creates datalink socket info from the device.
4798  */
4799 int
4800 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4801 {
4802 	size_t	len;
4803 	ill_t	*ill = ipif->ipif_ill;
4804 
4805 	sdl->sdl_family = AF_LINK;
4806 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4807 	sdl->sdl_type = ill->ill_type;
4808 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4809 	len = strlen(sdl->sdl_data);
4810 	ASSERT(len < 256);
4811 	sdl->sdl_nlen = (uchar_t)len;
4812 	sdl->sdl_alen = ill->ill_phys_addr_length;
4813 	sdl->sdl_slen = 0;
4814 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4815 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4816 
4817 	return (sizeof (struct sockaddr_dl));
4818 }
4819 
4820 /*
4821  * ill_xarp_info
4822  * creates xarp info from the device.
4823  */
4824 static int
4825 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4826 {
4827 	sdl->sdl_family = AF_LINK;
4828 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4829 	sdl->sdl_type = ill->ill_type;
4830 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4831 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4832 	sdl->sdl_alen = ill->ill_phys_addr_length;
4833 	sdl->sdl_slen = 0;
4834 	return (sdl->sdl_nlen);
4835 }
4836 
4837 static int
4838 loopback_kstat_update(kstat_t *ksp, int rw)
4839 {
4840 	kstat_named_t *kn;
4841 	netstackid_t	stackid;
4842 	netstack_t	*ns;
4843 	ip_stack_t	*ipst;
4844 
4845 	if (ksp == NULL || ksp->ks_data == NULL)
4846 		return (EIO);
4847 
4848 	if (rw == KSTAT_WRITE)
4849 		return (EACCES);
4850 
4851 	kn = KSTAT_NAMED_PTR(ksp);
4852 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4853 
4854 	ns = netstack_find_by_stackid(stackid);
4855 	if (ns == NULL)
4856 		return (-1);
4857 
4858 	ipst = ns->netstack_ip;
4859 	if (ipst == NULL) {
4860 		netstack_rele(ns);
4861 		return (-1);
4862 	}
4863 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4864 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4865 	netstack_rele(ns);
4866 	return (0);
4867 }
4868 
4869 
4870 /*
4871  * Has ifindex been plumbed already.
4872  * Compares both phyint_ifindex and phyint_group_ifindex.
4873  */
4874 static boolean_t
4875 phyint_exists(uint_t index, ip_stack_t *ipst)
4876 {
4877 	phyint_t *phyi;
4878 
4879 	ASSERT(index != 0);
4880 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4881 	/*
4882 	 * Indexes are stored in the phyint - a common structure
4883 	 * to both IPv4 and IPv6.
4884 	 */
4885 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4886 	for (; phyi != NULL;
4887 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4888 	    phyi, AVL_AFTER)) {
4889 		if (phyi->phyint_ifindex == index ||
4890 		    phyi->phyint_group_ifindex == index)
4891 			return (B_TRUE);
4892 	}
4893 	return (B_FALSE);
4894 }
4895 
4896 /* Pick a unique ifindex */
4897 boolean_t
4898 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4899 {
4900 	uint_t starting_index;
4901 
4902 	if (!ipst->ips_ill_index_wrap) {
4903 		*indexp = ipst->ips_ill_index++;
4904 		if (ipst->ips_ill_index == 0) {
4905 			/* Reached the uint_t limit Next time wrap  */
4906 			ipst->ips_ill_index_wrap = B_TRUE;
4907 		}
4908 		return (B_TRUE);
4909 	}
4910 
4911 	/*
4912 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4913 	 * at this point and don't want to call any function that attempts
4914 	 * to get the lock again.
4915 	 */
4916 	starting_index = ipst->ips_ill_index++;
4917 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4918 		if (ipst->ips_ill_index != 0 &&
4919 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4920 			/* found unused index - use it */
4921 			*indexp = ipst->ips_ill_index;
4922 			return (B_TRUE);
4923 		}
4924 	}
4925 
4926 	/*
4927 	 * all interface indicies are inuse.
4928 	 */
4929 	return (B_FALSE);
4930 }
4931 
4932 /*
4933  * Assign a unique interface index for the phyint.
4934  */
4935 static boolean_t
4936 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4937 {
4938 	ASSERT(phyi->phyint_ifindex == 0);
4939 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4940 }
4941 
4942 /*
4943  * Return a pointer to the ill which matches the supplied name.  Note that
4944  * the ill name length includes the null termination character.  (May be
4945  * called as writer.)
4946  * If do_alloc and the interface is "lo0" it will be automatically created.
4947  * Cannot bump up reference on condemned ills. So dup detect can't be done
4948  * using this func.
4949  */
4950 ill_t *
4951 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4952     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4953     ip_stack_t *ipst)
4954 {
4955 	ill_t	*ill;
4956 	ipif_t	*ipif;
4957 	kstat_named_t	*kn;
4958 	boolean_t isloopback;
4959 	ipsq_t *old_ipsq;
4960 	in6_addr_t ov6addr;
4961 
4962 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4963 
4964 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4965 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4966 	rw_exit(&ipst->ips_ill_g_lock);
4967 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4968 		return (ill);
4969 
4970 	/*
4971 	 * Couldn't find it.  Does this happen to be a lookup for the
4972 	 * loopback device and are we allowed to allocate it?
4973 	 */
4974 	if (!isloopback || !do_alloc)
4975 		return (NULL);
4976 
4977 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4978 
4979 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4980 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4981 		rw_exit(&ipst->ips_ill_g_lock);
4982 		return (ill);
4983 	}
4984 
4985 	/* Create the loopback device on demand */
4986 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4987 	    sizeof (ipif_loopback_name), BPRI_MED));
4988 	if (ill == NULL)
4989 		goto done;
4990 
4991 	*ill = ill_null;
4992 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4993 	ill->ill_ipst = ipst;
4994 	netstack_hold(ipst->ips_netstack);
4995 	/*
4996 	 * For exclusive stacks we set the zoneid to zero
4997 	 * to make IP operate as if in the global zone.
4998 	 */
4999 	ill->ill_zoneid = GLOBAL_ZONEID;
5000 
5001 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
5002 	if (ill->ill_phyint == NULL)
5003 		goto done;
5004 
5005 	if (isv6)
5006 		ill->ill_phyint->phyint_illv6 = ill;
5007 	else
5008 		ill->ill_phyint->phyint_illv4 = ill;
5009 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
5010 	ill->ill_max_frag = IP_LOOPBACK_MTU;
5011 	/* Add room for tcp+ip headers */
5012 	if (isv6) {
5013 		ill->ill_isv6 = B_TRUE;
5014 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
5015 	} else {
5016 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
5017 	}
5018 	if (!ill_allocate_mibs(ill))
5019 		goto done;
5020 	ill->ill_max_mtu = ill->ill_max_frag;
5021 	/*
5022 	 * ipif_loopback_name can't be pointed at directly because its used
5023 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
5024 	 * from the glist, ill_glist_delete() sets the first character of
5025 	 * ill_name to '\0'.
5026 	 */
5027 	ill->ill_name = (char *)ill + sizeof (*ill);
5028 	(void) strcpy(ill->ill_name, ipif_loopback_name);
5029 	ill->ill_name_length = sizeof (ipif_loopback_name);
5030 	/* Set ill_name_set for ill_phyint_reinit to work properly */
5031 
5032 	ill->ill_global_timer = INFINITY;
5033 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
5034 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
5035 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
5036 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
5037 
5038 	/* No resolver here. */
5039 	ill->ill_net_type = IRE_LOOPBACK;
5040 
5041 	/* Initialize the ipsq */
5042 	if (!ipsq_init(ill))
5043 		goto done;
5044 
5045 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5046 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5047 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5048 #ifdef DEBUG
5049 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5050 #endif
5051 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5052 	if (ipif == NULL)
5053 		goto done;
5054 
5055 	ill->ill_flags = ILLF_MULTICAST;
5056 
5057 	ov6addr = ipif->ipif_v6lcl_addr;
5058 	/* Set up default loopback address and mask. */
5059 	if (!isv6) {
5060 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5061 
5062 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5063 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5064 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5065 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5066 		    ipif->ipif_v6subnet);
5067 		ill->ill_flags |= ILLF_IPV4;
5068 	} else {
5069 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5070 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5071 		ipif->ipif_v6net_mask = ipv6_all_ones;
5072 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5073 		    ipif->ipif_v6subnet);
5074 		ill->ill_flags |= ILLF_IPV6;
5075 	}
5076 
5077 	/*
5078 	 * Chain us in at the end of the ill list. hold the ill
5079 	 * before we make it globally visible. 1 for the lookup.
5080 	 */
5081 	ill->ill_refcnt = 0;
5082 	ill_refhold(ill);
5083 
5084 	ill->ill_frag_count = 0;
5085 	ill->ill_frag_free_num_pkts = 0;
5086 	ill->ill_last_frag_clean_time = 0;
5087 
5088 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5089 
5090 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5091 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5092 
5093 	/* Let SCTP know so that it can add this to its list */
5094 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5095 
5096 	/*
5097 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5098 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5099 	 * requires to be after ill_glist_insert() since we need the
5100 	 * ill_index set. Pass on ipv6_loopback as the old address.
5101 	 */
5102 	sctp_update_ipif_addr(ipif, ov6addr);
5103 
5104 	/*
5105 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5106 	 */
5107 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5108 		/* Loopback ills aren't in any IPMP group */
5109 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5110 		ipsq_delete(old_ipsq);
5111 	}
5112 
5113 	/*
5114 	 * Delay this till the ipif is allocated as ipif_allocate
5115 	 * de-references ill_phyint for getting the ifindex. We
5116 	 * can't do this before ipif_allocate because ill_phyint_reinit
5117 	 * -> phyint_assign_ifindex expects ipif to be present.
5118 	 */
5119 	mutex_enter(&ill->ill_phyint->phyint_lock);
5120 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5121 	mutex_exit(&ill->ill_phyint->phyint_lock);
5122 
5123 	if (ipst->ips_loopback_ksp == NULL) {
5124 		/* Export loopback interface statistics */
5125 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5126 		    ipif_loopback_name, "net",
5127 		    KSTAT_TYPE_NAMED, 2, 0,
5128 		    ipst->ips_netstack->netstack_stackid);
5129 		if (ipst->ips_loopback_ksp != NULL) {
5130 			ipst->ips_loopback_ksp->ks_update =
5131 			    loopback_kstat_update;
5132 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5133 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5134 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5135 			ipst->ips_loopback_ksp->ks_private =
5136 			    (void *)(uintptr_t)ipst->ips_netstack->
5137 			    netstack_stackid;
5138 			kstat_install(ipst->ips_loopback_ksp);
5139 		}
5140 	}
5141 
5142 	if (error != NULL)
5143 		*error = 0;
5144 	*did_alloc = B_TRUE;
5145 	rw_exit(&ipst->ips_ill_g_lock);
5146 	return (ill);
5147 done:
5148 	if (ill != NULL) {
5149 		if (ill->ill_phyint != NULL) {
5150 			ipsq_t	*ipsq;
5151 
5152 			ipsq = ill->ill_phyint->phyint_ipsq;
5153 			if (ipsq != NULL) {
5154 				ipsq->ipsq_ipst = NULL;
5155 				kmem_free(ipsq, sizeof (ipsq_t));
5156 			}
5157 			mi_free(ill->ill_phyint);
5158 		}
5159 		ill_free_mib(ill);
5160 		if (ill->ill_ipst != NULL)
5161 			netstack_rele(ill->ill_ipst->ips_netstack);
5162 		mi_free(ill);
5163 	}
5164 	rw_exit(&ipst->ips_ill_g_lock);
5165 	if (error != NULL)
5166 		*error = ENOMEM;
5167 	return (NULL);
5168 }
5169 
5170 /*
5171  * For IPP calls - use the ip_stack_t for global stack.
5172  */
5173 ill_t *
5174 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5175     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5176 {
5177 	ip_stack_t	*ipst;
5178 	ill_t		*ill;
5179 
5180 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5181 	if (ipst == NULL) {
5182 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5183 		return (NULL);
5184 	}
5185 
5186 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5187 	netstack_rele(ipst->ips_netstack);
5188 	return (ill);
5189 }
5190 
5191 /*
5192  * Return a pointer to the ill which matches the index and IP version type.
5193  */
5194 ill_t *
5195 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5196     ipsq_func_t func, int *err, ip_stack_t *ipst)
5197 {
5198 	ill_t	*ill;
5199 	ipsq_t  *ipsq;
5200 	phyint_t *phyi;
5201 
5202 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5203 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5204 
5205 	if (err != NULL)
5206 		*err = 0;
5207 
5208 	/*
5209 	 * Indexes are stored in the phyint - a common structure
5210 	 * to both IPv4 and IPv6.
5211 	 */
5212 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5213 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5214 	    (void *) &index, NULL);
5215 	if (phyi != NULL) {
5216 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5217 		if (ill != NULL) {
5218 			/*
5219 			 * The block comment at the start of ipif_down
5220 			 * explains the use of the macros used below
5221 			 */
5222 			GRAB_CONN_LOCK(q);
5223 			mutex_enter(&ill->ill_lock);
5224 			if (ILL_CAN_LOOKUP(ill)) {
5225 				ill_refhold_locked(ill);
5226 				mutex_exit(&ill->ill_lock);
5227 				RELEASE_CONN_LOCK(q);
5228 				rw_exit(&ipst->ips_ill_g_lock);
5229 				return (ill);
5230 			} else if (ILL_CAN_WAIT(ill, q)) {
5231 				ipsq = ill->ill_phyint->phyint_ipsq;
5232 				mutex_enter(&ipsq->ipsq_lock);
5233 				rw_exit(&ipst->ips_ill_g_lock);
5234 				mutex_exit(&ill->ill_lock);
5235 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5236 				mutex_exit(&ipsq->ipsq_lock);
5237 				RELEASE_CONN_LOCK(q);
5238 				*err = EINPROGRESS;
5239 				return (NULL);
5240 			}
5241 			RELEASE_CONN_LOCK(q);
5242 			mutex_exit(&ill->ill_lock);
5243 		}
5244 	}
5245 	rw_exit(&ipst->ips_ill_g_lock);
5246 	if (err != NULL)
5247 		*err = ENXIO;
5248 	return (NULL);
5249 }
5250 
5251 /*
5252  * Return the ifindex next in sequence after the passed in ifindex.
5253  * If there is no next ifindex for the given protocol, return 0.
5254  */
5255 uint_t
5256 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5257 {
5258 	phyint_t *phyi;
5259 	phyint_t *phyi_initial;
5260 	uint_t   ifindex;
5261 
5262 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5263 
5264 	if (index == 0) {
5265 		phyi = avl_first(
5266 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5267 	} else {
5268 		phyi = phyi_initial = avl_find(
5269 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5270 		    (void *) &index, NULL);
5271 	}
5272 
5273 	for (; phyi != NULL;
5274 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5275 	    phyi, AVL_AFTER)) {
5276 		/*
5277 		 * If we're not returning the first interface in the tree
5278 		 * and we still haven't moved past the phyint_t that
5279 		 * corresponds to index, avl_walk needs to be called again
5280 		 */
5281 		if (!((index != 0) && (phyi == phyi_initial))) {
5282 			if (isv6) {
5283 				if ((phyi->phyint_illv6) &&
5284 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5285 				    (phyi->phyint_illv6->ill_isv6 == 1))
5286 					break;
5287 			} else {
5288 				if ((phyi->phyint_illv4) &&
5289 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5290 				    (phyi->phyint_illv4->ill_isv6 == 0))
5291 					break;
5292 			}
5293 		}
5294 	}
5295 
5296 	rw_exit(&ipst->ips_ill_g_lock);
5297 
5298 	if (phyi != NULL)
5299 		ifindex = phyi->phyint_ifindex;
5300 	else
5301 		ifindex = 0;
5302 
5303 	return (ifindex);
5304 }
5305 
5306 
5307 /*
5308  * Return the ifindex for the named interface.
5309  * If there is no next ifindex for the interface, return 0.
5310  */
5311 uint_t
5312 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5313 {
5314 	phyint_t	*phyi;
5315 	avl_index_t	where = 0;
5316 	uint_t		ifindex;
5317 
5318 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5319 
5320 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5321 	    name, &where)) == NULL) {
5322 		rw_exit(&ipst->ips_ill_g_lock);
5323 		return (0);
5324 	}
5325 
5326 	ifindex = phyi->phyint_ifindex;
5327 
5328 	rw_exit(&ipst->ips_ill_g_lock);
5329 
5330 	return (ifindex);
5331 }
5332 
5333 
5334 /*
5335  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5336  * that gives a running thread a reference to the ill. This reference must be
5337  * released by the thread when it is done accessing the ill and related
5338  * objects. ill_refcnt can not be used to account for static references
5339  * such as other structures pointing to an ill. Callers must generally
5340  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5341  * or be sure that the ill is not being deleted or changing state before
5342  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5343  * ill won't change any of its critical state such as address, netmask etc.
5344  */
5345 void
5346 ill_refhold(ill_t *ill)
5347 {
5348 	mutex_enter(&ill->ill_lock);
5349 	ill->ill_refcnt++;
5350 	ILL_TRACE_REF(ill);
5351 	mutex_exit(&ill->ill_lock);
5352 }
5353 
5354 void
5355 ill_refhold_locked(ill_t *ill)
5356 {
5357 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5358 	ill->ill_refcnt++;
5359 	ILL_TRACE_REF(ill);
5360 }
5361 
5362 int
5363 ill_check_and_refhold(ill_t *ill)
5364 {
5365 	mutex_enter(&ill->ill_lock);
5366 	if (ILL_CAN_LOOKUP(ill)) {
5367 		ill_refhold_locked(ill);
5368 		mutex_exit(&ill->ill_lock);
5369 		return (0);
5370 	}
5371 	mutex_exit(&ill->ill_lock);
5372 	return (ILL_LOOKUP_FAILED);
5373 }
5374 
5375 /*
5376  * Must not be called while holding any locks. Otherwise if this is
5377  * the last reference to be released, there is a chance of recursive mutex
5378  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5379  * to restart an ioctl.
5380  */
5381 void
5382 ill_refrele(ill_t *ill)
5383 {
5384 	mutex_enter(&ill->ill_lock);
5385 	ASSERT(ill->ill_refcnt != 0);
5386 	ill->ill_refcnt--;
5387 	ILL_UNTRACE_REF(ill);
5388 	if (ill->ill_refcnt != 0) {
5389 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5390 		mutex_exit(&ill->ill_lock);
5391 		return;
5392 	}
5393 
5394 	/* Drops the ill_lock */
5395 	ipif_ill_refrele_tail(ill);
5396 }
5397 
5398 /*
5399  * Obtain a weak reference count on the ill. This reference ensures the
5400  * ill won't be freed, but the ill may change any of its critical state
5401  * such as netmask, address etc. Returns an error if the ill has started
5402  * closing.
5403  */
5404 boolean_t
5405 ill_waiter_inc(ill_t *ill)
5406 {
5407 	mutex_enter(&ill->ill_lock);
5408 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5409 		mutex_exit(&ill->ill_lock);
5410 		return (B_FALSE);
5411 	}
5412 	ill->ill_waiters++;
5413 	mutex_exit(&ill->ill_lock);
5414 	return (B_TRUE);
5415 }
5416 
5417 void
5418 ill_waiter_dcr(ill_t *ill)
5419 {
5420 	mutex_enter(&ill->ill_lock);
5421 	ill->ill_waiters--;
5422 	if (ill->ill_waiters == 0)
5423 		cv_broadcast(&ill->ill_cv);
5424 	mutex_exit(&ill->ill_lock);
5425 }
5426 
5427 /*
5428  * Named Dispatch routine to produce a formatted report on all ILLs.
5429  * This report is accessed by using the ndd utility to "get" ND variable
5430  * "ip_ill_status".
5431  */
5432 /* ARGSUSED */
5433 int
5434 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5435 {
5436 	ill_t		*ill;
5437 	ill_walk_context_t ctx;
5438 	ip_stack_t	*ipst;
5439 
5440 	ipst = CONNQ_TO_IPST(q);
5441 
5442 	(void) mi_mpprintf(mp,
5443 	    "ILL      " MI_COL_HDRPAD_STR
5444 	/*   01234567[89ABCDEF] */
5445 	    "rq       " MI_COL_HDRPAD_STR
5446 	/*   01234567[89ABCDEF] */
5447 	    "wq       " MI_COL_HDRPAD_STR
5448 	/*   01234567[89ABCDEF] */
5449 	    "upcnt mxfrg err name");
5450 	/*   12345 12345 123 xxxxxxxx  */
5451 
5452 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5453 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5454 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5455 		(void) mi_mpprintf(mp,
5456 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5457 		    "%05u %05u %03d %s",
5458 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5459 		    ill->ill_ipif_up_count,
5460 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5461 	}
5462 	rw_exit(&ipst->ips_ill_g_lock);
5463 
5464 	return (0);
5465 }
5466 
5467 /*
5468  * Named Dispatch routine to produce a formatted report on all IPIFs.
5469  * This report is accessed by using the ndd utility to "get" ND variable
5470  * "ip_ipif_status".
5471  */
5472 /* ARGSUSED */
5473 int
5474 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5475 {
5476 	char	buf1[INET6_ADDRSTRLEN];
5477 	char	buf2[INET6_ADDRSTRLEN];
5478 	char	buf3[INET6_ADDRSTRLEN];
5479 	char	buf4[INET6_ADDRSTRLEN];
5480 	char	buf5[INET6_ADDRSTRLEN];
5481 	char	buf6[INET6_ADDRSTRLEN];
5482 	char	buf[LIFNAMSIZ];
5483 	ill_t	*ill;
5484 	ipif_t	*ipif;
5485 	nv_t	*nvp;
5486 	uint64_t flags;
5487 	zoneid_t zoneid;
5488 	ill_walk_context_t ctx;
5489 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5490 
5491 	(void) mi_mpprintf(mp,
5492 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5493 	    "\tlocal address\n"
5494 	    "\tsrc address\n"
5495 	    "\tsubnet\n"
5496 	    "\tmask\n"
5497 	    "\tbroadcast\n"
5498 	    "\tp-p-dst");
5499 
5500 	ASSERT(q->q_next == NULL);
5501 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5502 
5503 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5504 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5505 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5506 		for (ipif = ill->ill_ipif; ipif != NULL;
5507 		    ipif = ipif->ipif_next) {
5508 			if (zoneid != GLOBAL_ZONEID &&
5509 			    zoneid != ipif->ipif_zoneid &&
5510 			    ipif->ipif_zoneid != ALL_ZONES)
5511 				continue;
5512 
5513 			ipif_get_name(ipif, buf, sizeof (buf));
5514 			(void) mi_mpprintf(mp,
5515 			    MI_COL_PTRFMT_STR
5516 			    "%04u %05u %u/%u/%u %s %d",
5517 			    (void *)ipif,
5518 			    ipif->ipif_metric, ipif->ipif_mtu,
5519 			    ipif->ipif_ib_pkt_count,
5520 			    ipif->ipif_ob_pkt_count,
5521 			    ipif->ipif_fo_pkt_count,
5522 			    buf,
5523 			    ipif->ipif_zoneid);
5524 
5525 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5526 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5527 
5528 		/* Tack on text strings for any flags. */
5529 		nvp = ipif_nv_tbl;
5530 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5531 			if (nvp->nv_value & flags)
5532 				(void) mi_mpprintf_nr(mp, " %s",
5533 				    nvp->nv_name);
5534 		}
5535 		(void) mi_mpprintf(mp,
5536 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5537 		    inet_ntop(AF_INET6,
5538 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5539 		    inet_ntop(AF_INET6,
5540 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5541 		    inet_ntop(AF_INET6,
5542 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5543 		    inet_ntop(AF_INET6,
5544 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5545 		    inet_ntop(AF_INET6,
5546 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5547 		    inet_ntop(AF_INET6,
5548 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5549 		}
5550 	}
5551 	rw_exit(&ipst->ips_ill_g_lock);
5552 	return (0);
5553 }
5554 
5555 /*
5556  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5557  * driver.  We construct best guess defaults for lower level information that
5558  * we need.  If an interface is brought up without injection of any overriding
5559  * information from outside, we have to be ready to go with these defaults.
5560  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5561  * we primarely want the dl_provider_style.
5562  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5563  * at which point we assume the other part of the information is valid.
5564  */
5565 void
5566 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5567 {
5568 	uchar_t		*brdcst_addr;
5569 	uint_t		brdcst_addr_length, phys_addr_length;
5570 	t_scalar_t	sap_length;
5571 	dl_info_ack_t	*dlia;
5572 	ip_m_t		*ipm;
5573 	dl_qos_cl_sel1_t *sel1;
5574 
5575 	ASSERT(IAM_WRITER_ILL(ill));
5576 
5577 	/*
5578 	 * Till the ill is fully up ILL_CHANGING will be set and
5579 	 * the ill is not globally visible. So no need for a lock.
5580 	 */
5581 	dlia = (dl_info_ack_t *)mp->b_rptr;
5582 	ill->ill_mactype = dlia->dl_mac_type;
5583 
5584 	ipm = ip_m_lookup(dlia->dl_mac_type);
5585 	if (ipm == NULL) {
5586 		ipm = ip_m_lookup(DL_OTHER);
5587 		ASSERT(ipm != NULL);
5588 	}
5589 	ill->ill_media = ipm;
5590 
5591 	/*
5592 	 * When the new DLPI stuff is ready we'll pull lengths
5593 	 * from dlia.
5594 	 */
5595 	if (dlia->dl_version == DL_VERSION_2) {
5596 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5597 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5598 		    brdcst_addr_length);
5599 		if (brdcst_addr == NULL) {
5600 			brdcst_addr_length = 0;
5601 		}
5602 		sap_length = dlia->dl_sap_length;
5603 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5604 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5605 		    brdcst_addr_length, sap_length, phys_addr_length));
5606 	} else {
5607 		brdcst_addr_length = 6;
5608 		brdcst_addr = ip_six_byte_all_ones;
5609 		sap_length = -2;
5610 		phys_addr_length = brdcst_addr_length;
5611 	}
5612 
5613 	ill->ill_bcast_addr_length = brdcst_addr_length;
5614 	ill->ill_phys_addr_length = phys_addr_length;
5615 	ill->ill_sap_length = sap_length;
5616 	ill->ill_max_frag = dlia->dl_max_sdu;
5617 	ill->ill_max_mtu = ill->ill_max_frag;
5618 
5619 	ill->ill_type = ipm->ip_m_type;
5620 
5621 	if (!ill->ill_dlpi_style_set) {
5622 		if (dlia->dl_provider_style == DL_STYLE2)
5623 			ill->ill_needs_attach = 1;
5624 
5625 		/*
5626 		 * Allocate the first ipif on this ill. We don't delay it
5627 		 * further as ioctl handling assumes atleast one ipif to
5628 		 * be present.
5629 		 *
5630 		 * At this point we don't know whether the ill is v4 or v6.
5631 		 * We will know this whan the SIOCSLIFNAME happens and
5632 		 * the correct value for ill_isv6 will be assigned in
5633 		 * ipif_set_values(). We need to hold the ill lock and
5634 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5635 		 * the wakeup.
5636 		 */
5637 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5638 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5639 		mutex_enter(&ill->ill_lock);
5640 		ASSERT(ill->ill_dlpi_style_set == 0);
5641 		ill->ill_dlpi_style_set = 1;
5642 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5643 		cv_broadcast(&ill->ill_cv);
5644 		mutex_exit(&ill->ill_lock);
5645 		freemsg(mp);
5646 		return;
5647 	}
5648 	ASSERT(ill->ill_ipif != NULL);
5649 	/*
5650 	 * We know whether it is IPv4 or IPv6 now, as this is the
5651 	 * second DL_INFO_ACK we are recieving in response to the
5652 	 * DL_INFO_REQ sent in ipif_set_values.
5653 	 */
5654 	if (ill->ill_isv6)
5655 		ill->ill_sap = IP6_DL_SAP;
5656 	else
5657 		ill->ill_sap = IP_DL_SAP;
5658 	/*
5659 	 * Set ipif_mtu which is used to set the IRE's
5660 	 * ire_max_frag value. The driver could have sent
5661 	 * a different mtu from what it sent last time. No
5662 	 * need to call ipif_mtu_change because IREs have
5663 	 * not yet been created.
5664 	 */
5665 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5666 	/*
5667 	 * Clear all the flags that were set based on ill_bcast_addr_length
5668 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5669 	 * changed now and we need to re-evaluate.
5670 	 */
5671 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5672 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5673 
5674 	/*
5675 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5676 	 * changed now.
5677 	 */
5678 	if (ill->ill_bcast_addr_length == 0) {
5679 		if (ill->ill_resolver_mp != NULL)
5680 			freemsg(ill->ill_resolver_mp);
5681 		if (ill->ill_bcast_mp != NULL)
5682 			freemsg(ill->ill_bcast_mp);
5683 		if (ill->ill_flags & ILLF_XRESOLV)
5684 			ill->ill_net_type = IRE_IF_RESOLVER;
5685 		else
5686 			ill->ill_net_type = IRE_IF_NORESOLVER;
5687 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5688 		    ill->ill_phys_addr_length,
5689 		    ill->ill_sap,
5690 		    ill->ill_sap_length);
5691 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5692 
5693 		if (ill->ill_isv6)
5694 			/*
5695 			 * Note: xresolv interfaces will eventually need NOARP
5696 			 * set here as well, but that will require those
5697 			 * external resolvers to have some knowledge of
5698 			 * that flag and act appropriately. Not to be changed
5699 			 * at present.
5700 			 */
5701 			ill->ill_flags |= ILLF_NONUD;
5702 		else
5703 			ill->ill_flags |= ILLF_NOARP;
5704 
5705 		if (ill->ill_phys_addr_length == 0) {
5706 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5707 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5708 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5709 			} else {
5710 				/* pt-pt supports multicast. */
5711 				ill->ill_flags |= ILLF_MULTICAST;
5712 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5713 			}
5714 		}
5715 	} else {
5716 		ill->ill_net_type = IRE_IF_RESOLVER;
5717 		if (ill->ill_bcast_mp != NULL)
5718 			freemsg(ill->ill_bcast_mp);
5719 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5720 		    ill->ill_bcast_addr_length, ill->ill_sap,
5721 		    ill->ill_sap_length);
5722 		/*
5723 		 * Later detect lack of DLPI driver multicast
5724 		 * capability by catching DL_ENABMULTI errors in
5725 		 * ip_rput_dlpi.
5726 		 */
5727 		ill->ill_flags |= ILLF_MULTICAST;
5728 		if (!ill->ill_isv6)
5729 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5730 	}
5731 	/* By default an interface does not support any CoS marking */
5732 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5733 
5734 	/*
5735 	 * If we get QoS information in DL_INFO_ACK, the device supports
5736 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5737 	 */
5738 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5739 	    dlia->dl_qos_length);
5740 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5741 		ill->ill_flags |= ILLF_COS_ENABLED;
5742 	}
5743 
5744 	/* Clear any previous error indication. */
5745 	ill->ill_error = 0;
5746 	freemsg(mp);
5747 }
5748 
5749 /*
5750  * Perform various checks to verify that an address would make sense as a
5751  * local, remote, or subnet interface address.
5752  */
5753 static boolean_t
5754 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5755 {
5756 	ipaddr_t	net_mask;
5757 
5758 	/*
5759 	 * Don't allow all zeroes, all ones or experimental address, but allow
5760 	 * all ones netmask.
5761 	 */
5762 	if ((net_mask = ip_net_mask(addr)) == 0)
5763 		return (B_FALSE);
5764 	/* A given netmask overrides the "guess" netmask */
5765 	if (subnet_mask != 0)
5766 		net_mask = subnet_mask;
5767 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5768 	    (addr == (addr | ~net_mask)))) {
5769 		return (B_FALSE);
5770 	}
5771 	if (CLASSD(addr))
5772 		return (B_FALSE);
5773 
5774 	return (B_TRUE);
5775 }
5776 
5777 #define	V6_IPIF_LINKLOCAL(p)	\
5778 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5779 
5780 /*
5781  * Compare two given ipifs and check if the second one is better than
5782  * the first one using the order of preference (not taking deprecated
5783  * into acount) specified in ipif_lookup_multicast().
5784  */
5785 static boolean_t
5786 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5787 {
5788 	/* Check the least preferred first. */
5789 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5790 		/* If both ipifs are the same, use the first one. */
5791 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5792 			return (B_FALSE);
5793 		else
5794 			return (B_TRUE);
5795 	}
5796 
5797 	/* For IPv6, check for link local address. */
5798 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5799 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5800 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5801 			/* The second one is equal or less preferred. */
5802 			return (B_FALSE);
5803 		} else {
5804 			return (B_TRUE);
5805 		}
5806 	}
5807 
5808 	/* Then check for point to point interface. */
5809 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5810 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5811 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5812 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5813 			return (B_FALSE);
5814 		} else {
5815 			return (B_TRUE);
5816 		}
5817 	}
5818 
5819 	/* old_ipif is a normal interface, so no need to use the new one. */
5820 	return (B_FALSE);
5821 }
5822 
5823 /*
5824  * Find any non-virtual, not condemned, and up multicast capable interface
5825  * given an IP instance and zoneid.  Order of preference is:
5826  *
5827  * 1. normal
5828  * 1.1 normal, but deprecated
5829  * 2. point to point
5830  * 2.1 point to point, but deprecated
5831  * 3. link local
5832  * 3.1 link local, but deprecated
5833  * 4. loopback.
5834  */
5835 ipif_t *
5836 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5837 {
5838 	ill_t			*ill;
5839 	ill_walk_context_t	ctx;
5840 	ipif_t			*ipif;
5841 	ipif_t			*saved_ipif = NULL;
5842 	ipif_t			*dep_ipif = NULL;
5843 
5844 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5845 	if (isv6)
5846 		ill = ILL_START_WALK_V6(&ctx, ipst);
5847 	else
5848 		ill = ILL_START_WALK_V4(&ctx, ipst);
5849 
5850 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5851 		mutex_enter(&ill->ill_lock);
5852 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5853 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5854 			mutex_exit(&ill->ill_lock);
5855 			continue;
5856 		}
5857 		for (ipif = ill->ill_ipif; ipif != NULL;
5858 		    ipif = ipif->ipif_next) {
5859 			if (zoneid != ipif->ipif_zoneid &&
5860 			    zoneid != ALL_ZONES &&
5861 			    ipif->ipif_zoneid != ALL_ZONES) {
5862 				continue;
5863 			}
5864 			if (!(ipif->ipif_flags & IPIF_UP) ||
5865 			    !IPIF_CAN_LOOKUP(ipif)) {
5866 				continue;
5867 			}
5868 
5869 			/*
5870 			 * Found one candidate.  If it is deprecated,
5871 			 * remember it in dep_ipif.  If it is not deprecated,
5872 			 * remember it in saved_ipif.
5873 			 */
5874 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5875 				if (dep_ipif == NULL) {
5876 					dep_ipif = ipif;
5877 				} else if (ipif_comp_multi(dep_ipif, ipif,
5878 				    isv6)) {
5879 					/*
5880 					 * If the previous dep_ipif does not
5881 					 * belong to the same ill, we've done
5882 					 * a ipif_refhold() on it.  So we need
5883 					 * to release it.
5884 					 */
5885 					if (dep_ipif->ipif_ill != ill)
5886 						ipif_refrele(dep_ipif);
5887 					dep_ipif = ipif;
5888 				}
5889 				continue;
5890 			}
5891 			if (saved_ipif == NULL) {
5892 				saved_ipif = ipif;
5893 			} else {
5894 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5895 					if (saved_ipif->ipif_ill != ill)
5896 						ipif_refrele(saved_ipif);
5897 					saved_ipif = ipif;
5898 				}
5899 			}
5900 		}
5901 		/*
5902 		 * Before going to the next ill, do a ipif_refhold() on the
5903 		 * saved ones.
5904 		 */
5905 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5906 			ipif_refhold_locked(saved_ipif);
5907 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5908 			ipif_refhold_locked(dep_ipif);
5909 		mutex_exit(&ill->ill_lock);
5910 	}
5911 	rw_exit(&ipst->ips_ill_g_lock);
5912 
5913 	/*
5914 	 * If we have only the saved_ipif, return it.  But if we have both
5915 	 * saved_ipif and dep_ipif, check to see which one is better.
5916 	 */
5917 	if (saved_ipif != NULL) {
5918 		if (dep_ipif != NULL) {
5919 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5920 				ipif_refrele(saved_ipif);
5921 				return (dep_ipif);
5922 			} else {
5923 				ipif_refrele(dep_ipif);
5924 				return (saved_ipif);
5925 			}
5926 		}
5927 		return (saved_ipif);
5928 	} else {
5929 		return (dep_ipif);
5930 	}
5931 }
5932 
5933 /*
5934  * This function is called when an application does not specify an interface
5935  * to be used for multicast traffic (joining a group/sending data).  It
5936  * calls ire_lookup_multi() to look for an interface route for the
5937  * specified multicast group.  Doing this allows the administrator to add
5938  * prefix routes for multicast to indicate which interface to be used for
5939  * multicast traffic in the above scenario.  The route could be for all
5940  * multicast (224.0/4), for a single multicast group (a /32 route) or
5941  * anything in between.  If there is no such multicast route, we just find
5942  * any multicast capable interface and return it.  The returned ipif
5943  * is refhold'ed.
5944  */
5945 ipif_t *
5946 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5947 {
5948 	ire_t			*ire;
5949 	ipif_t			*ipif;
5950 
5951 	ire = ire_lookup_multi(group, zoneid, ipst);
5952 	if (ire != NULL) {
5953 		ipif = ire->ire_ipif;
5954 		ipif_refhold(ipif);
5955 		ire_refrele(ire);
5956 		return (ipif);
5957 	}
5958 
5959 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5960 }
5961 
5962 /*
5963  * Look for an ipif with the specified interface address and destination.
5964  * The destination address is used only for matching point-to-point interfaces.
5965  */
5966 ipif_t *
5967 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5968     ipsq_func_t func, int *error, ip_stack_t *ipst)
5969 {
5970 	ipif_t	*ipif;
5971 	ill_t	*ill;
5972 	ill_walk_context_t ctx;
5973 	ipsq_t	*ipsq;
5974 
5975 	if (error != NULL)
5976 		*error = 0;
5977 
5978 	/*
5979 	 * First match all the point-to-point interfaces
5980 	 * before looking at non-point-to-point interfaces.
5981 	 * This is done to avoid returning non-point-to-point
5982 	 * ipif instead of unnumbered point-to-point ipif.
5983 	 */
5984 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5985 	ill = ILL_START_WALK_V4(&ctx, ipst);
5986 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5987 		GRAB_CONN_LOCK(q);
5988 		mutex_enter(&ill->ill_lock);
5989 		for (ipif = ill->ill_ipif; ipif != NULL;
5990 		    ipif = ipif->ipif_next) {
5991 			/* Allow the ipif to be down */
5992 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5993 			    (ipif->ipif_lcl_addr == if_addr) &&
5994 			    (ipif->ipif_pp_dst_addr == dst)) {
5995 				/*
5996 				 * The block comment at the start of ipif_down
5997 				 * explains the use of the macros used below
5998 				 */
5999 				if (IPIF_CAN_LOOKUP(ipif)) {
6000 					ipif_refhold_locked(ipif);
6001 					mutex_exit(&ill->ill_lock);
6002 					RELEASE_CONN_LOCK(q);
6003 					rw_exit(&ipst->ips_ill_g_lock);
6004 					return (ipif);
6005 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6006 					ipsq = ill->ill_phyint->phyint_ipsq;
6007 					mutex_enter(&ipsq->ipsq_lock);
6008 					mutex_exit(&ill->ill_lock);
6009 					rw_exit(&ipst->ips_ill_g_lock);
6010 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6011 					    ill);
6012 					mutex_exit(&ipsq->ipsq_lock);
6013 					RELEASE_CONN_LOCK(q);
6014 					*error = EINPROGRESS;
6015 					return (NULL);
6016 				}
6017 			}
6018 		}
6019 		mutex_exit(&ill->ill_lock);
6020 		RELEASE_CONN_LOCK(q);
6021 	}
6022 	rw_exit(&ipst->ips_ill_g_lock);
6023 
6024 	/* lookup the ipif based on interface address */
6025 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
6026 	    ipst);
6027 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
6028 	return (ipif);
6029 }
6030 
6031 /*
6032  * Look for an ipif with the specified address. For point-point links
6033  * we look for matches on either the destination address and the local
6034  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6035  * is set.
6036  * Matches on a specific ill if match_ill is set.
6037  */
6038 ipif_t *
6039 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6040     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6041 {
6042 	ipif_t  *ipif;
6043 	ill_t   *ill;
6044 	boolean_t ptp = B_FALSE;
6045 	ipsq_t	*ipsq;
6046 	ill_walk_context_t	ctx;
6047 
6048 	if (error != NULL)
6049 		*error = 0;
6050 
6051 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6052 	/*
6053 	 * Repeat twice, first based on local addresses and
6054 	 * next time for pointopoint.
6055 	 */
6056 repeat:
6057 	ill = ILL_START_WALK_V4(&ctx, ipst);
6058 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6059 		if (match_ill != NULL && ill != match_ill) {
6060 			continue;
6061 		}
6062 		GRAB_CONN_LOCK(q);
6063 		mutex_enter(&ill->ill_lock);
6064 		for (ipif = ill->ill_ipif; ipif != NULL;
6065 		    ipif = ipif->ipif_next) {
6066 			if (zoneid != ALL_ZONES &&
6067 			    zoneid != ipif->ipif_zoneid &&
6068 			    ipif->ipif_zoneid != ALL_ZONES)
6069 				continue;
6070 			/* Allow the ipif to be down */
6071 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6072 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6073 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6074 			    (ipif->ipif_pp_dst_addr == addr))) {
6075 				/*
6076 				 * The block comment at the start of ipif_down
6077 				 * explains the use of the macros used below
6078 				 */
6079 				if (IPIF_CAN_LOOKUP(ipif)) {
6080 					ipif_refhold_locked(ipif);
6081 					mutex_exit(&ill->ill_lock);
6082 					RELEASE_CONN_LOCK(q);
6083 					rw_exit(&ipst->ips_ill_g_lock);
6084 					return (ipif);
6085 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6086 					ipsq = ill->ill_phyint->phyint_ipsq;
6087 					mutex_enter(&ipsq->ipsq_lock);
6088 					mutex_exit(&ill->ill_lock);
6089 					rw_exit(&ipst->ips_ill_g_lock);
6090 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6091 					    ill);
6092 					mutex_exit(&ipsq->ipsq_lock);
6093 					RELEASE_CONN_LOCK(q);
6094 					*error = EINPROGRESS;
6095 					return (NULL);
6096 				}
6097 			}
6098 		}
6099 		mutex_exit(&ill->ill_lock);
6100 		RELEASE_CONN_LOCK(q);
6101 	}
6102 
6103 	/* If we already did the ptp case, then we are done */
6104 	if (ptp) {
6105 		rw_exit(&ipst->ips_ill_g_lock);
6106 		if (error != NULL)
6107 			*error = ENXIO;
6108 		return (NULL);
6109 	}
6110 	ptp = B_TRUE;
6111 	goto repeat;
6112 }
6113 
6114 /*
6115  * Look for an ipif with the specified address. For point-point links
6116  * we look for matches on either the destination address and the local
6117  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6118  * is set.
6119  * Matches on a specific ill if match_ill is set.
6120  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6121  */
6122 zoneid_t
6123 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6124 {
6125 	zoneid_t zoneid;
6126 	ipif_t  *ipif;
6127 	ill_t   *ill;
6128 	boolean_t ptp = B_FALSE;
6129 	ill_walk_context_t	ctx;
6130 
6131 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6132 	/*
6133 	 * Repeat twice, first based on local addresses and
6134 	 * next time for pointopoint.
6135 	 */
6136 repeat:
6137 	ill = ILL_START_WALK_V4(&ctx, ipst);
6138 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6139 		if (match_ill != NULL && ill != match_ill) {
6140 			continue;
6141 		}
6142 		mutex_enter(&ill->ill_lock);
6143 		for (ipif = ill->ill_ipif; ipif != NULL;
6144 		    ipif = ipif->ipif_next) {
6145 			/* Allow the ipif to be down */
6146 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6147 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6148 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6149 			    (ipif->ipif_pp_dst_addr == addr)) &&
6150 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6151 				zoneid = ipif->ipif_zoneid;
6152 				mutex_exit(&ill->ill_lock);
6153 				rw_exit(&ipst->ips_ill_g_lock);
6154 				/*
6155 				 * If ipif_zoneid was ALL_ZONES then we have
6156 				 * a trusted extensions shared IP address.
6157 				 * In that case GLOBAL_ZONEID works to send.
6158 				 */
6159 				if (zoneid == ALL_ZONES)
6160 					zoneid = GLOBAL_ZONEID;
6161 				return (zoneid);
6162 			}
6163 		}
6164 		mutex_exit(&ill->ill_lock);
6165 	}
6166 
6167 	/* If we already did the ptp case, then we are done */
6168 	if (ptp) {
6169 		rw_exit(&ipst->ips_ill_g_lock);
6170 		return (ALL_ZONES);
6171 	}
6172 	ptp = B_TRUE;
6173 	goto repeat;
6174 }
6175 
6176 /*
6177  * Look for an ipif that matches the specified remote address i.e. the
6178  * ipif that would receive the specified packet.
6179  * First look for directly connected interfaces and then do a recursive
6180  * IRE lookup and pick the first ipif corresponding to the source address in the
6181  * ire.
6182  * Returns: held ipif
6183  */
6184 ipif_t *
6185 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6186 {
6187 	ipif_t	*ipif;
6188 	ire_t	*ire;
6189 	ip_stack_t	*ipst = ill->ill_ipst;
6190 
6191 	ASSERT(!ill->ill_isv6);
6192 
6193 	/*
6194 	 * Someone could be changing this ipif currently or change it
6195 	 * after we return this. Thus  a few packets could use the old
6196 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6197 	 * will atomically be updated or cleaned up with the new value
6198 	 * Thus we don't need a lock to check the flags or other attrs below.
6199 	 */
6200 	mutex_enter(&ill->ill_lock);
6201 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6202 		if (!IPIF_CAN_LOOKUP(ipif))
6203 			continue;
6204 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6205 		    ipif->ipif_zoneid != ALL_ZONES)
6206 			continue;
6207 		/* Allow the ipif to be down */
6208 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6209 			if ((ipif->ipif_pp_dst_addr == addr) ||
6210 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6211 			    ipif->ipif_lcl_addr == addr)) {
6212 				ipif_refhold_locked(ipif);
6213 				mutex_exit(&ill->ill_lock);
6214 				return (ipif);
6215 			}
6216 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6217 			ipif_refhold_locked(ipif);
6218 			mutex_exit(&ill->ill_lock);
6219 			return (ipif);
6220 		}
6221 	}
6222 	mutex_exit(&ill->ill_lock);
6223 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6224 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6225 	if (ire != NULL) {
6226 		/*
6227 		 * The callers of this function wants to know the
6228 		 * interface on which they have to send the replies
6229 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6230 		 * derived from different ills, we really don't care
6231 		 * what we return here.
6232 		 */
6233 		ipif = ire->ire_ipif;
6234 		if (ipif != NULL) {
6235 			ipif_refhold(ipif);
6236 			ire_refrele(ire);
6237 			return (ipif);
6238 		}
6239 		ire_refrele(ire);
6240 	}
6241 	/* Pick the first interface */
6242 	ipif = ipif_get_next_ipif(NULL, ill);
6243 	return (ipif);
6244 }
6245 
6246 /*
6247  * This func does not prevent refcnt from increasing. But if
6248  * the caller has taken steps to that effect, then this func
6249  * can be used to determine whether the ill has become quiescent
6250  */
6251 boolean_t
6252 ill_is_quiescent(ill_t *ill)
6253 {
6254 	ipif_t	*ipif;
6255 
6256 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6257 
6258 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6259 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6260 			return (B_FALSE);
6261 		}
6262 	}
6263 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
6264 	    ill->ill_nce_cnt != 0) {
6265 		return (B_FALSE);
6266 	}
6267 	return (B_TRUE);
6268 }
6269 
6270 /*
6271  * This func does not prevent refcnt from increasing. But if
6272  * the caller has taken steps to that effect, then this func
6273  * can be used to determine whether the ipif has become quiescent
6274  */
6275 static boolean_t
6276 ipif_is_quiescent(ipif_t *ipif)
6277 {
6278 	ill_t *ill;
6279 
6280 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6281 
6282 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6283 		return (B_FALSE);
6284 	}
6285 
6286 	ill = ipif->ipif_ill;
6287 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6288 	    ill->ill_logical_down) {
6289 		return (B_TRUE);
6290 	}
6291 
6292 	/* This is the last ipif going down or being deleted on this ill */
6293 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
6294 		return (B_FALSE);
6295 	}
6296 
6297 	return (B_TRUE);
6298 }
6299 
6300 /*
6301  * This func does not prevent refcnt from increasing. But if
6302  * the caller has taken steps to that effect, then this func
6303  * can be used to determine whether the ipifs marked with IPIF_MOVING
6304  * have become quiescent and can be moved in a failover/failback.
6305  */
6306 static ipif_t *
6307 ill_quiescent_to_move(ill_t *ill)
6308 {
6309 	ipif_t  *ipif;
6310 
6311 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6312 
6313 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6314 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6315 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
6316 				return (ipif);
6317 			}
6318 		}
6319 	}
6320 	return (NULL);
6321 }
6322 
6323 /*
6324  * The ipif/ill/ire has been refreled. Do the tail processing.
6325  * Determine if the ipif or ill in question has become quiescent and if so
6326  * wakeup close and/or restart any queued pending ioctl that is waiting
6327  * for the ipif_down (or ill_down)
6328  */
6329 void
6330 ipif_ill_refrele_tail(ill_t *ill)
6331 {
6332 	mblk_t	*mp;
6333 	conn_t	*connp;
6334 	ipsq_t	*ipsq;
6335 	ipif_t	*ipif;
6336 	dl_notify_ind_t *dlindp;
6337 
6338 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6339 
6340 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6341 	    ill_is_quiescent(ill)) {
6342 		/* ill_close may be waiting */
6343 		cv_broadcast(&ill->ill_cv);
6344 	}
6345 
6346 	/* ipsq can't change because ill_lock  is held */
6347 	ipsq = ill->ill_phyint->phyint_ipsq;
6348 	if (ipsq->ipsq_waitfor == 0) {
6349 		/* Not waiting for anything, just return. */
6350 		mutex_exit(&ill->ill_lock);
6351 		return;
6352 	}
6353 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6354 	    ipsq->ipsq_pending_ipif != NULL);
6355 	/*
6356 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6357 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6358 	 * be zero for restarting an ioctl that ends up downing the ill.
6359 	 */
6360 	ipif = ipsq->ipsq_pending_ipif;
6361 	if (ipif->ipif_ill != ill) {
6362 		/* The ioctl is pending on some other ill. */
6363 		mutex_exit(&ill->ill_lock);
6364 		return;
6365 	}
6366 
6367 	switch (ipsq->ipsq_waitfor) {
6368 	case IPIF_DOWN:
6369 	case IPIF_FREE:
6370 		if (!ipif_is_quiescent(ipif)) {
6371 			mutex_exit(&ill->ill_lock);
6372 			return;
6373 		}
6374 		break;
6375 
6376 	case ILL_DOWN:
6377 	case ILL_FREE:
6378 		/*
6379 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6380 		 * waits synchronously in ip_close, and no message is queued in
6381 		 * ipsq_pending_mp at all in this case
6382 		 */
6383 		if (!ill_is_quiescent(ill)) {
6384 			mutex_exit(&ill->ill_lock);
6385 			return;
6386 		}
6387 
6388 		break;
6389 
6390 	case ILL_MOVE_OK:
6391 		if (ill_quiescent_to_move(ill) != NULL) {
6392 			mutex_exit(&ill->ill_lock);
6393 			return;
6394 		}
6395 
6396 		break;
6397 	default:
6398 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6399 		    (void *)ipsq, ipsq->ipsq_waitfor);
6400 	}
6401 
6402 	/*
6403 	 * Incr refcnt for the qwriter_ip call below which
6404 	 * does a refrele
6405 	 */
6406 	ill_refhold_locked(ill);
6407 	mutex_exit(&ill->ill_lock);
6408 
6409 	mp = ipsq_pending_mp_get(ipsq, &connp);
6410 	ASSERT(mp != NULL);
6411 
6412 	/*
6413 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6414 	 * we can only get here when the current operation decides it
6415 	 * it needs to quiesce via ipsq_pending_mp_add().
6416 	 */
6417 	switch (mp->b_datap->db_type) {
6418 	case M_PCPROTO:
6419 	case M_PROTO:
6420 		/*
6421 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6422 		 */
6423 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6424 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6425 
6426 		switch (dlindp->dl_notification) {
6427 		case DL_NOTE_PHYS_ADDR:
6428 			qwriter_ip(ill, ill->ill_rq, mp,
6429 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6430 			return;
6431 		default:
6432 			ASSERT(0);
6433 		}
6434 		break;
6435 
6436 	case M_ERROR:
6437 	case M_HANGUP:
6438 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6439 		    B_TRUE);
6440 		return;
6441 
6442 	case M_IOCTL:
6443 	case M_IOCDATA:
6444 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6445 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6446 		return;
6447 
6448 	default:
6449 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6450 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6451 	}
6452 }
6453 
6454 #ifdef DEBUG
6455 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6456 static void
6457 th_trace_rrecord(th_trace_t *th_trace)
6458 {
6459 	tr_buf_t *tr_buf;
6460 	uint_t lastref;
6461 
6462 	lastref = th_trace->th_trace_lastref;
6463 	lastref++;
6464 	if (lastref == TR_BUF_MAX)
6465 		lastref = 0;
6466 	th_trace->th_trace_lastref = lastref;
6467 	tr_buf = &th_trace->th_trbuf[lastref];
6468 	tr_buf->tr_time = lbolt;
6469 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6470 }
6471 
6472 static void
6473 th_trace_free(void *value)
6474 {
6475 	th_trace_t *th_trace = value;
6476 
6477 	ASSERT(th_trace->th_refcnt == 0);
6478 	kmem_free(th_trace, sizeof (*th_trace));
6479 }
6480 
6481 /*
6482  * Find or create the per-thread hash table used to track object references.
6483  * The ipst argument is NULL if we shouldn't allocate.
6484  *
6485  * Accesses per-thread data, so there's no need to lock here.
6486  */
6487 static mod_hash_t *
6488 th_trace_gethash(ip_stack_t *ipst)
6489 {
6490 	th_hash_t *thh;
6491 
6492 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6493 		mod_hash_t *mh;
6494 		char name[256];
6495 		size_t objsize, rshift;
6496 		int retv;
6497 
6498 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6499 			return (NULL);
6500 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6501 
6502 		/*
6503 		 * We use mod_hash_create_extended here rather than the more
6504 		 * obvious mod_hash_create_ptrhash because the latter has a
6505 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6506 		 * block.
6507 		 */
6508 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6509 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6510 		rshift = highbit(objsize);
6511 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6512 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6513 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6514 		if (mh == NULL) {
6515 			kmem_free(thh, sizeof (*thh));
6516 			return (NULL);
6517 		}
6518 		thh->thh_hash = mh;
6519 		thh->thh_ipst = ipst;
6520 		/*
6521 		 * We trace ills, ipifs, ires, and nces.  All of these are
6522 		 * per-IP-stack, so the lock on the thread list is as well.
6523 		 */
6524 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6525 		list_insert_tail(&ip_thread_list, thh);
6526 		rw_exit(&ip_thread_rwlock);
6527 		retv = tsd_set(ip_thread_data, thh);
6528 		ASSERT(retv == 0);
6529 	}
6530 	return (thh != NULL ? thh->thh_hash : NULL);
6531 }
6532 
6533 boolean_t
6534 th_trace_ref(const void *obj, ip_stack_t *ipst)
6535 {
6536 	th_trace_t *th_trace;
6537 	mod_hash_t *mh;
6538 	mod_hash_val_t val;
6539 
6540 	if ((mh = th_trace_gethash(ipst)) == NULL)
6541 		return (B_FALSE);
6542 
6543 	/*
6544 	 * Attempt to locate the trace buffer for this obj and thread.
6545 	 * If it does not exist, then allocate a new trace buffer and
6546 	 * insert into the hash.
6547 	 */
6548 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6549 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6550 		if (th_trace == NULL)
6551 			return (B_FALSE);
6552 
6553 		th_trace->th_id = curthread;
6554 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6555 		    (mod_hash_val_t)th_trace) != 0) {
6556 			kmem_free(th_trace, sizeof (th_trace_t));
6557 			return (B_FALSE);
6558 		}
6559 	} else {
6560 		th_trace = (th_trace_t *)val;
6561 	}
6562 
6563 	ASSERT(th_trace->th_refcnt >= 0 &&
6564 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6565 
6566 	th_trace->th_refcnt++;
6567 	th_trace_rrecord(th_trace);
6568 	return (B_TRUE);
6569 }
6570 
6571 /*
6572  * For the purpose of tracing a reference release, we assume that global
6573  * tracing is always on and that the same thread initiated the reference hold
6574  * is releasing.
6575  */
6576 void
6577 th_trace_unref(const void *obj)
6578 {
6579 	int retv;
6580 	mod_hash_t *mh;
6581 	th_trace_t *th_trace;
6582 	mod_hash_val_t val;
6583 
6584 	mh = th_trace_gethash(NULL);
6585 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6586 	ASSERT(retv == 0);
6587 	th_trace = (th_trace_t *)val;
6588 
6589 	ASSERT(th_trace->th_refcnt > 0);
6590 	th_trace->th_refcnt--;
6591 	th_trace_rrecord(th_trace);
6592 }
6593 
6594 /*
6595  * If tracing has been disabled, then we assume that the reference counts are
6596  * now useless, and we clear them out before destroying the entries.
6597  */
6598 void
6599 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6600 {
6601 	th_hash_t	*thh;
6602 	mod_hash_t	*mh;
6603 	mod_hash_val_t	val;
6604 	th_trace_t	*th_trace;
6605 	int		retv;
6606 
6607 	rw_enter(&ip_thread_rwlock, RW_READER);
6608 	for (thh = list_head(&ip_thread_list); thh != NULL;
6609 	    thh = list_next(&ip_thread_list, thh)) {
6610 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6611 		    &val) == 0) {
6612 			th_trace = (th_trace_t *)val;
6613 			if (trace_disable)
6614 				th_trace->th_refcnt = 0;
6615 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6616 			ASSERT(retv == 0);
6617 		}
6618 	}
6619 	rw_exit(&ip_thread_rwlock);
6620 }
6621 
6622 void
6623 ipif_trace_ref(ipif_t *ipif)
6624 {
6625 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6626 
6627 	if (ipif->ipif_trace_disable)
6628 		return;
6629 
6630 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6631 		ipif->ipif_trace_disable = B_TRUE;
6632 		ipif_trace_cleanup(ipif);
6633 	}
6634 }
6635 
6636 void
6637 ipif_untrace_ref(ipif_t *ipif)
6638 {
6639 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6640 
6641 	if (!ipif->ipif_trace_disable)
6642 		th_trace_unref(ipif);
6643 }
6644 
6645 void
6646 ill_trace_ref(ill_t *ill)
6647 {
6648 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6649 
6650 	if (ill->ill_trace_disable)
6651 		return;
6652 
6653 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6654 		ill->ill_trace_disable = B_TRUE;
6655 		ill_trace_cleanup(ill);
6656 	}
6657 }
6658 
6659 void
6660 ill_untrace_ref(ill_t *ill)
6661 {
6662 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6663 
6664 	if (!ill->ill_trace_disable)
6665 		th_trace_unref(ill);
6666 }
6667 
6668 /*
6669  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6670  * failure, ipif_trace_disable is set.
6671  */
6672 static void
6673 ipif_trace_cleanup(const ipif_t *ipif)
6674 {
6675 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6676 }
6677 
6678 /*
6679  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6680  * failure, ill_trace_disable is set.
6681  */
6682 static void
6683 ill_trace_cleanup(const ill_t *ill)
6684 {
6685 	th_trace_cleanup(ill, ill->ill_trace_disable);
6686 }
6687 #endif /* DEBUG */
6688 
6689 void
6690 ipif_refhold_locked(ipif_t *ipif)
6691 {
6692 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6693 	ipif->ipif_refcnt++;
6694 	IPIF_TRACE_REF(ipif);
6695 }
6696 
6697 void
6698 ipif_refhold(ipif_t *ipif)
6699 {
6700 	ill_t	*ill;
6701 
6702 	ill = ipif->ipif_ill;
6703 	mutex_enter(&ill->ill_lock);
6704 	ipif->ipif_refcnt++;
6705 	IPIF_TRACE_REF(ipif);
6706 	mutex_exit(&ill->ill_lock);
6707 }
6708 
6709 /*
6710  * Must not be called while holding any locks. Otherwise if this is
6711  * the last reference to be released there is a chance of recursive mutex
6712  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6713  * to restart an ioctl.
6714  */
6715 void
6716 ipif_refrele(ipif_t *ipif)
6717 {
6718 	ill_t	*ill;
6719 
6720 	ill = ipif->ipif_ill;
6721 
6722 	mutex_enter(&ill->ill_lock);
6723 	ASSERT(ipif->ipif_refcnt != 0);
6724 	ipif->ipif_refcnt--;
6725 	IPIF_UNTRACE_REF(ipif);
6726 	if (ipif->ipif_refcnt != 0) {
6727 		mutex_exit(&ill->ill_lock);
6728 		return;
6729 	}
6730 
6731 	/* Drops the ill_lock */
6732 	ipif_ill_refrele_tail(ill);
6733 }
6734 
6735 ipif_t *
6736 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6737 {
6738 	ipif_t	*ipif;
6739 
6740 	mutex_enter(&ill->ill_lock);
6741 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6742 	    ipif != NULL; ipif = ipif->ipif_next) {
6743 		if (!IPIF_CAN_LOOKUP(ipif))
6744 			continue;
6745 		ipif_refhold_locked(ipif);
6746 		mutex_exit(&ill->ill_lock);
6747 		return (ipif);
6748 	}
6749 	mutex_exit(&ill->ill_lock);
6750 	return (NULL);
6751 }
6752 
6753 /*
6754  * TODO: make this table extendible at run time
6755  * Return a pointer to the mac type info for 'mac_type'
6756  */
6757 static ip_m_t *
6758 ip_m_lookup(t_uscalar_t mac_type)
6759 {
6760 	ip_m_t	*ipm;
6761 
6762 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6763 		if (ipm->ip_m_mac_type == mac_type)
6764 			return (ipm);
6765 	return (NULL);
6766 }
6767 
6768 /*
6769  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6770  * ipif_arg is passed in to associate it with the correct interface.
6771  * We may need to restart this operation if the ipif cannot be looked up
6772  * due to an exclusive operation that is currently in progress. The restart
6773  * entry point is specified by 'func'
6774  */
6775 int
6776 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6777     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6778     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6779     struct rtsa_s *sp, ip_stack_t *ipst)
6780 {
6781 	ire_t	*ire;
6782 	ire_t	*gw_ire = NULL;
6783 	ipif_t	*ipif = NULL;
6784 	boolean_t ipif_refheld = B_FALSE;
6785 	uint_t	type;
6786 	int	match_flags = MATCH_IRE_TYPE;
6787 	int	error;
6788 	tsol_gc_t *gc = NULL;
6789 	tsol_gcgrp_t *gcgrp = NULL;
6790 	boolean_t gcgrp_xtraref = B_FALSE;
6791 
6792 	ip1dbg(("ip_rt_add:"));
6793 
6794 	if (ire_arg != NULL)
6795 		*ire_arg = NULL;
6796 
6797 	/*
6798 	 * If this is the case of RTF_HOST being set, then we set the netmask
6799 	 * to all ones (regardless if one was supplied).
6800 	 */
6801 	if (flags & RTF_HOST)
6802 		mask = IP_HOST_MASK;
6803 
6804 	/*
6805 	 * Prevent routes with a zero gateway from being created (since
6806 	 * interfaces can currently be plumbed and brought up no assigned
6807 	 * address).
6808 	 */
6809 	if (gw_addr == 0)
6810 		return (ENETUNREACH);
6811 	/*
6812 	 * Get the ipif, if any, corresponding to the gw_addr
6813 	 */
6814 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6815 	    ipst);
6816 	if (ipif != NULL) {
6817 		if (IS_VNI(ipif->ipif_ill)) {
6818 			ipif_refrele(ipif);
6819 			return (EINVAL);
6820 		}
6821 		ipif_refheld = B_TRUE;
6822 	} else if (error == EINPROGRESS) {
6823 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6824 		return (EINPROGRESS);
6825 	} else {
6826 		error = 0;
6827 	}
6828 
6829 	if (ipif != NULL) {
6830 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6831 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6832 	} else {
6833 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6834 	}
6835 
6836 	/*
6837 	 * GateD will attempt to create routes with a loopback interface
6838 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6839 	 * these routes to be added, but create them as interface routes
6840 	 * since the gateway is an interface address.
6841 	 */
6842 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6843 		flags &= ~RTF_GATEWAY;
6844 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6845 		    mask == IP_HOST_MASK) {
6846 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6847 			    ALL_ZONES, NULL, match_flags, ipst);
6848 			if (ire != NULL) {
6849 				ire_refrele(ire);
6850 				if (ipif_refheld)
6851 					ipif_refrele(ipif);
6852 				return (EEXIST);
6853 			}
6854 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6855 			    "for 0x%x\n", (void *)ipif,
6856 			    ipif->ipif_ire_type,
6857 			    ntohl(ipif->ipif_lcl_addr)));
6858 			ire = ire_create(
6859 			    (uchar_t *)&dst_addr,	/* dest address */
6860 			    (uchar_t *)&mask,		/* mask */
6861 			    (uchar_t *)&ipif->ipif_src_addr,
6862 			    NULL,			/* no gateway */
6863 			    &ipif->ipif_mtu,
6864 			    NULL,
6865 			    ipif->ipif_rq,		/* recv-from queue */
6866 			    NULL,			/* no send-to queue */
6867 			    ipif->ipif_ire_type,	/* LOOPBACK */
6868 			    ipif,
6869 			    0,
6870 			    0,
6871 			    0,
6872 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6873 			    RTF_PRIVATE : 0,
6874 			    &ire_uinfo_null,
6875 			    NULL,
6876 			    NULL,
6877 			    ipst);
6878 
6879 			if (ire == NULL) {
6880 				if (ipif_refheld)
6881 					ipif_refrele(ipif);
6882 				return (ENOMEM);
6883 			}
6884 			error = ire_add(&ire, q, mp, func, B_FALSE);
6885 			if (error == 0)
6886 				goto save_ire;
6887 			if (ipif_refheld)
6888 				ipif_refrele(ipif);
6889 			return (error);
6890 
6891 		}
6892 	}
6893 
6894 	/*
6895 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6896 	 * and the gateway address provided is one of the system's interface
6897 	 * addresses.  By using the routing socket interface and supplying an
6898 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6899 	 * specifying an interface route to be created is available which uses
6900 	 * the interface index that specifies the outgoing interface rather than
6901 	 * the address of an outgoing interface (which may not be able to
6902 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6903 	 * flag, routes can be specified which not only specify the next-hop to
6904 	 * be used when routing to a certain prefix, but also which outgoing
6905 	 * interface should be used.
6906 	 *
6907 	 * Previously, interfaces would have unique addresses assigned to them
6908 	 * and so the address assigned to a particular interface could be used
6909 	 * to identify a particular interface.  One exception to this was the
6910 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6911 	 *
6912 	 * With the advent of IPv6 and its link-local addresses, this
6913 	 * restriction was relaxed and interfaces could share addresses between
6914 	 * themselves.  In fact, typically all of the link-local interfaces on
6915 	 * an IPv6 node or router will have the same link-local address.  In
6916 	 * order to differentiate between these interfaces, the use of an
6917 	 * interface index is necessary and this index can be carried inside a
6918 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6919 	 * of using the interface index, however, is that all of the ipif's that
6920 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6921 	 * cannot be used to differentiate between ipif's (or logical
6922 	 * interfaces) that belong to the same ill (physical interface).
6923 	 *
6924 	 * For example, in the following case involving IPv4 interfaces and
6925 	 * logical interfaces
6926 	 *
6927 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6928 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6929 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6930 	 *
6931 	 * the ipif's corresponding to each of these interface routes can be
6932 	 * uniquely identified by the "gateway" (actually interface address).
6933 	 *
6934 	 * In this case involving multiple IPv6 default routes to a particular
6935 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6936 	 * default route is of interest:
6937 	 *
6938 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6939 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6940 	 */
6941 
6942 	/* RTF_GATEWAY not set */
6943 	if (!(flags & RTF_GATEWAY)) {
6944 		queue_t	*stq;
6945 
6946 		if (sp != NULL) {
6947 			ip2dbg(("ip_rt_add: gateway security attributes "
6948 			    "cannot be set with interface route\n"));
6949 			if (ipif_refheld)
6950 				ipif_refrele(ipif);
6951 			return (EINVAL);
6952 		}
6953 
6954 		/*
6955 		 * As the interface index specified with the RTA_IFP sockaddr is
6956 		 * the same for all ipif's off of an ill, the matching logic
6957 		 * below uses MATCH_IRE_ILL if such an index was specified.
6958 		 * This means that routes sharing the same prefix when added
6959 		 * using a RTA_IFP sockaddr must have distinct interface
6960 		 * indices (namely, they must be on distinct ill's).
6961 		 *
6962 		 * On the other hand, since the gateway address will usually be
6963 		 * different for each ipif on the system, the matching logic
6964 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6965 		 * route.  This means that interface routes for the same prefix
6966 		 * can be created if they belong to distinct ipif's and if a
6967 		 * RTA_IFP sockaddr is not present.
6968 		 */
6969 		if (ipif_arg != NULL) {
6970 			if (ipif_refheld)  {
6971 				ipif_refrele(ipif);
6972 				ipif_refheld = B_FALSE;
6973 			}
6974 			ipif = ipif_arg;
6975 			match_flags |= MATCH_IRE_ILL;
6976 		} else {
6977 			/*
6978 			 * Check the ipif corresponding to the gw_addr
6979 			 */
6980 			if (ipif == NULL)
6981 				return (ENETUNREACH);
6982 			match_flags |= MATCH_IRE_IPIF;
6983 		}
6984 		ASSERT(ipif != NULL);
6985 
6986 		/*
6987 		 * We check for an existing entry at this point.
6988 		 *
6989 		 * Since a netmask isn't passed in via the ioctl interface
6990 		 * (SIOCADDRT), we don't check for a matching netmask in that
6991 		 * case.
6992 		 */
6993 		if (!ioctl_msg)
6994 			match_flags |= MATCH_IRE_MASK;
6995 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
6996 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
6997 		if (ire != NULL) {
6998 			ire_refrele(ire);
6999 			if (ipif_refheld)
7000 				ipif_refrele(ipif);
7001 			return (EEXIST);
7002 		}
7003 
7004 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7005 		    ? ipif->ipif_rq : ipif->ipif_wq;
7006 
7007 		/*
7008 		 * Create a copy of the IRE_LOOPBACK,
7009 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7010 		 * the modified address and netmask.
7011 		 */
7012 		ire = ire_create(
7013 		    (uchar_t *)&dst_addr,
7014 		    (uint8_t *)&mask,
7015 		    (uint8_t *)&ipif->ipif_src_addr,
7016 		    NULL,
7017 		    &ipif->ipif_mtu,
7018 		    NULL,
7019 		    NULL,
7020 		    stq,
7021 		    ipif->ipif_net_type,
7022 		    ipif,
7023 		    0,
7024 		    0,
7025 		    0,
7026 		    flags,
7027 		    &ire_uinfo_null,
7028 		    NULL,
7029 		    NULL,
7030 		    ipst);
7031 		if (ire == NULL) {
7032 			if (ipif_refheld)
7033 				ipif_refrele(ipif);
7034 			return (ENOMEM);
7035 		}
7036 
7037 		/*
7038 		 * Some software (for example, GateD and Sun Cluster) attempts
7039 		 * to create (what amount to) IRE_PREFIX routes with the
7040 		 * loopback address as the gateway.  This is primarily done to
7041 		 * set up prefixes with the RTF_REJECT flag set (for example,
7042 		 * when generating aggregate routes.)
7043 		 *
7044 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7045 		 * IRE_LOOPBACK, then we map the request into a
7046 		 * IRE_IF_NORESOLVER.
7047 		 *
7048 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7049 		 * routine, but rather using ire_create() directly.
7050 		 *
7051 		 */
7052 		if (ipif->ipif_net_type == IRE_LOOPBACK)
7053 			ire->ire_type = IRE_IF_NORESOLVER;
7054 
7055 		error = ire_add(&ire, q, mp, func, B_FALSE);
7056 		if (error == 0)
7057 			goto save_ire;
7058 
7059 		/*
7060 		 * In the result of failure, ire_add() will have already
7061 		 * deleted the ire in question, so there is no need to
7062 		 * do that here.
7063 		 */
7064 		if (ipif_refheld)
7065 			ipif_refrele(ipif);
7066 		return (error);
7067 	}
7068 	if (ipif_refheld) {
7069 		ipif_refrele(ipif);
7070 		ipif_refheld = B_FALSE;
7071 	}
7072 
7073 	/*
7074 	 * Get an interface IRE for the specified gateway.
7075 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7076 	 * gateway, it is currently unreachable and we fail the request
7077 	 * accordingly.
7078 	 */
7079 	ipif = ipif_arg;
7080 	if (ipif_arg != NULL)
7081 		match_flags |= MATCH_IRE_ILL;
7082 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7083 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7084 	if (gw_ire == NULL)
7085 		return (ENETUNREACH);
7086 
7087 	/*
7088 	 * We create one of three types of IREs as a result of this request
7089 	 * based on the netmask.  A netmask of all ones (which is automatically
7090 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7091 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7092 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7093 	 * destination prefix.
7094 	 */
7095 	if (mask == IP_HOST_MASK)
7096 		type = IRE_HOST;
7097 	else if (mask == 0)
7098 		type = IRE_DEFAULT;
7099 	else
7100 		type = IRE_PREFIX;
7101 
7102 	/* check for a duplicate entry */
7103 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7104 	    NULL, ALL_ZONES, 0, NULL,
7105 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7106 	if (ire != NULL) {
7107 		ire_refrele(gw_ire);
7108 		ire_refrele(ire);
7109 		return (EEXIST);
7110 	}
7111 
7112 	/* Security attribute exists */
7113 	if (sp != NULL) {
7114 		tsol_gcgrp_addr_t ga;
7115 
7116 		/* find or create the gateway credentials group */
7117 		ga.ga_af = AF_INET;
7118 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7119 
7120 		/* we hold reference to it upon success */
7121 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7122 		if (gcgrp == NULL) {
7123 			ire_refrele(gw_ire);
7124 			return (ENOMEM);
7125 		}
7126 
7127 		/*
7128 		 * Create and add the security attribute to the group; a
7129 		 * reference to the group is made upon allocating a new
7130 		 * entry successfully.  If it finds an already-existing
7131 		 * entry for the security attribute in the group, it simply
7132 		 * returns it and no new reference is made to the group.
7133 		 */
7134 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7135 		if (gc == NULL) {
7136 			/* release reference held by gcgrp_lookup */
7137 			GCGRP_REFRELE(gcgrp);
7138 			ire_refrele(gw_ire);
7139 			return (ENOMEM);
7140 		}
7141 	}
7142 
7143 	/* Create the IRE. */
7144 	ire = ire_create(
7145 	    (uchar_t *)&dst_addr,		/* dest address */
7146 	    (uchar_t *)&mask,			/* mask */
7147 	    /* src address assigned by the caller? */
7148 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7149 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7150 	    (uchar_t *)&gw_addr,		/* gateway address */
7151 	    &gw_ire->ire_max_frag,
7152 	    NULL,				/* no src nce */
7153 	    NULL,				/* no recv-from queue */
7154 	    NULL,				/* no send-to queue */
7155 	    (ushort_t)type,			/* IRE type */
7156 	    ipif_arg,
7157 	    0,
7158 	    0,
7159 	    0,
7160 	    flags,
7161 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7162 	    gc,					/* security attribute */
7163 	    NULL,
7164 	    ipst);
7165 
7166 	/*
7167 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7168 	 * reference to the 'gcgrp'. We can now release the extra reference
7169 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7170 	 */
7171 	if (gcgrp_xtraref)
7172 		GCGRP_REFRELE(gcgrp);
7173 	if (ire == NULL) {
7174 		if (gc != NULL)
7175 			GC_REFRELE(gc);
7176 		ire_refrele(gw_ire);
7177 		return (ENOMEM);
7178 	}
7179 
7180 	/*
7181 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7182 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7183 	 */
7184 
7185 	/* Add the new IRE. */
7186 	error = ire_add(&ire, q, mp, func, B_FALSE);
7187 	if (error != 0) {
7188 		/*
7189 		 * In the result of failure, ire_add() will have already
7190 		 * deleted the ire in question, so there is no need to
7191 		 * do that here.
7192 		 */
7193 		ire_refrele(gw_ire);
7194 		return (error);
7195 	}
7196 
7197 	if (flags & RTF_MULTIRT) {
7198 		/*
7199 		 * Invoke the CGTP (multirouting) filtering module
7200 		 * to add the dst address in the filtering database.
7201 		 * Replicated inbound packets coming from that address
7202 		 * will be filtered to discard the duplicates.
7203 		 * It is not necessary to call the CGTP filter hook
7204 		 * when the dst address is a broadcast or multicast,
7205 		 * because an IP source address cannot be a broadcast
7206 		 * or a multicast.
7207 		 */
7208 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7209 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7210 		if (ire_dst != NULL) {
7211 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7212 			ire_refrele(ire_dst);
7213 			goto save_ire;
7214 		}
7215 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7216 		    !CLASSD(ire->ire_addr)) {
7217 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7218 			    ipst->ips_netstack->netstack_stackid,
7219 			    ire->ire_addr,
7220 			    ire->ire_gateway_addr,
7221 			    ire->ire_src_addr,
7222 			    gw_ire->ire_src_addr);
7223 			if (res != 0) {
7224 				ire_refrele(gw_ire);
7225 				ire_delete(ire);
7226 				return (res);
7227 			}
7228 		}
7229 	}
7230 
7231 	/*
7232 	 * Now that the prefix IRE entry has been created, delete any
7233 	 * existing gateway IRE cache entries as well as any IRE caches
7234 	 * using the gateway, and force them to be created through
7235 	 * ip_newroute.
7236 	 */
7237 	if (gc != NULL) {
7238 		ASSERT(gcgrp != NULL);
7239 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7240 	}
7241 
7242 save_ire:
7243 	if (gw_ire != NULL) {
7244 		ire_refrele(gw_ire);
7245 	}
7246 	if (ipif != NULL) {
7247 		/*
7248 		 * Save enough information so that we can recreate the IRE if
7249 		 * the interface goes down and then up.  The metrics associated
7250 		 * with the route will be saved as well when rts_setmetrics() is
7251 		 * called after the IRE has been created.  In the case where
7252 		 * memory cannot be allocated, none of this information will be
7253 		 * saved.
7254 		 */
7255 		ipif_save_ire(ipif, ire);
7256 	}
7257 	if (ioctl_msg)
7258 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7259 	if (ire_arg != NULL) {
7260 		/*
7261 		 * Store the ire that was successfully added into where ire_arg
7262 		 * points to so that callers don't have to look it up
7263 		 * themselves (but they are responsible for ire_refrele()ing
7264 		 * the ire when they are finished with it).
7265 		 */
7266 		*ire_arg = ire;
7267 	} else {
7268 		ire_refrele(ire);		/* Held in ire_add */
7269 	}
7270 	if (ipif_refheld)
7271 		ipif_refrele(ipif);
7272 	return (0);
7273 }
7274 
7275 /*
7276  * ip_rt_delete is called to delete an IPv4 route.
7277  * ipif_arg is passed in to associate it with the correct interface.
7278  * We may need to restart this operation if the ipif cannot be looked up
7279  * due to an exclusive operation that is currently in progress. The restart
7280  * entry point is specified by 'func'
7281  */
7282 /* ARGSUSED4 */
7283 int
7284 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7285     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7286     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7287 {
7288 	ire_t	*ire = NULL;
7289 	ipif_t	*ipif;
7290 	boolean_t ipif_refheld = B_FALSE;
7291 	uint_t	type;
7292 	uint_t	match_flags = MATCH_IRE_TYPE;
7293 	int	err = 0;
7294 
7295 	ip1dbg(("ip_rt_delete:"));
7296 	/*
7297 	 * If this is the case of RTF_HOST being set, then we set the netmask
7298 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7299 	 */
7300 	if (flags & RTF_HOST) {
7301 		mask = IP_HOST_MASK;
7302 		match_flags |= MATCH_IRE_MASK;
7303 	} else if (rtm_addrs & RTA_NETMASK) {
7304 		match_flags |= MATCH_IRE_MASK;
7305 	}
7306 
7307 	/*
7308 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7309 	 * we check if the gateway address is one of our interfaces first,
7310 	 * and fall back on RTF_GATEWAY routes.
7311 	 *
7312 	 * This makes it possible to delete an original
7313 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7314 	 *
7315 	 * As the interface index specified with the RTA_IFP sockaddr is the
7316 	 * same for all ipif's off of an ill, the matching logic below uses
7317 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7318 	 * sharing the same prefix and interface index as the the route
7319 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7320 	 * is specified in the request.
7321 	 *
7322 	 * On the other hand, since the gateway address will usually be
7323 	 * different for each ipif on the system, the matching logic
7324 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7325 	 * route.  This means that interface routes for the same prefix can be
7326 	 * uniquely identified if they belong to distinct ipif's and if a
7327 	 * RTA_IFP sockaddr is not present.
7328 	 *
7329 	 * For more detail on specifying routes by gateway address and by
7330 	 * interface index, see the comments in ip_rt_add().
7331 	 */
7332 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7333 	    ipst);
7334 	if (ipif != NULL)
7335 		ipif_refheld = B_TRUE;
7336 	else if (err == EINPROGRESS)
7337 		return (err);
7338 	else
7339 		err = 0;
7340 	if (ipif != NULL) {
7341 		if (ipif_arg != NULL) {
7342 			if (ipif_refheld) {
7343 				ipif_refrele(ipif);
7344 				ipif_refheld = B_FALSE;
7345 			}
7346 			ipif = ipif_arg;
7347 			match_flags |= MATCH_IRE_ILL;
7348 		} else {
7349 			match_flags |= MATCH_IRE_IPIF;
7350 		}
7351 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7352 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7353 			    ALL_ZONES, NULL, match_flags, ipst);
7354 		}
7355 		if (ire == NULL) {
7356 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7357 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7358 			    match_flags, ipst);
7359 		}
7360 	}
7361 
7362 	if (ire == NULL) {
7363 		/*
7364 		 * At this point, the gateway address is not one of our own
7365 		 * addresses or a matching interface route was not found.  We
7366 		 * set the IRE type to lookup based on whether
7367 		 * this is a host route, a default route or just a prefix.
7368 		 *
7369 		 * If an ipif_arg was passed in, then the lookup is based on an
7370 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7371 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7372 		 * set as the route being looked up is not a traditional
7373 		 * interface route.
7374 		 */
7375 		match_flags &= ~MATCH_IRE_IPIF;
7376 		match_flags |= MATCH_IRE_GW;
7377 		if (ipif_arg != NULL)
7378 			match_flags |= MATCH_IRE_ILL;
7379 		if (mask == IP_HOST_MASK)
7380 			type = IRE_HOST;
7381 		else if (mask == 0)
7382 			type = IRE_DEFAULT;
7383 		else
7384 			type = IRE_PREFIX;
7385 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7386 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7387 	}
7388 
7389 	if (ipif_refheld)
7390 		ipif_refrele(ipif);
7391 
7392 	/* ipif is not refheld anymore */
7393 	if (ire == NULL)
7394 		return (ESRCH);
7395 
7396 	if (ire->ire_flags & RTF_MULTIRT) {
7397 		/*
7398 		 * Invoke the CGTP (multirouting) filtering module
7399 		 * to remove the dst address from the filtering database.
7400 		 * Packets coming from that address will no longer be
7401 		 * filtered to remove duplicates.
7402 		 */
7403 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7404 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7405 			    ipst->ips_netstack->netstack_stackid,
7406 			    ire->ire_addr, ire->ire_gateway_addr);
7407 		}
7408 		ip_cgtp_bcast_delete(ire, ipst);
7409 	}
7410 
7411 	ipif = ire->ire_ipif;
7412 	if (ipif != NULL)
7413 		ipif_remove_ire(ipif, ire);
7414 	if (ioctl_msg)
7415 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7416 	ire_delete(ire);
7417 	ire_refrele(ire);
7418 	return (err);
7419 }
7420 
7421 /*
7422  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7423  */
7424 /* ARGSUSED */
7425 int
7426 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7427     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7428 {
7429 	ipaddr_t dst_addr;
7430 	ipaddr_t gw_addr;
7431 	ipaddr_t mask;
7432 	int error = 0;
7433 	mblk_t *mp1;
7434 	struct rtentry *rt;
7435 	ipif_t *ipif = NULL;
7436 	ip_stack_t	*ipst;
7437 
7438 	ASSERT(q->q_next == NULL);
7439 	ipst = CONNQ_TO_IPST(q);
7440 
7441 	ip1dbg(("ip_siocaddrt:"));
7442 	/* Existence of mp1 verified in ip_wput_nondata */
7443 	mp1 = mp->b_cont->b_cont;
7444 	rt = (struct rtentry *)mp1->b_rptr;
7445 
7446 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7447 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7448 
7449 	/*
7450 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7451 	 * to a particular host address.  In this case, we set the netmask to
7452 	 * all ones for the particular destination address.  Otherwise,
7453 	 * determine the netmask to be used based on dst_addr and the interfaces
7454 	 * in use.
7455 	 */
7456 	if (rt->rt_flags & RTF_HOST) {
7457 		mask = IP_HOST_MASK;
7458 	} else {
7459 		/*
7460 		 * Note that ip_subnet_mask returns a zero mask in the case of
7461 		 * default (an all-zeroes address).
7462 		 */
7463 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7464 	}
7465 
7466 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7467 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7468 	if (ipif != NULL)
7469 		ipif_refrele(ipif);
7470 	return (error);
7471 }
7472 
7473 /*
7474  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7475  */
7476 /* ARGSUSED */
7477 int
7478 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7479     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7480 {
7481 	ipaddr_t dst_addr;
7482 	ipaddr_t gw_addr;
7483 	ipaddr_t mask;
7484 	int error;
7485 	mblk_t *mp1;
7486 	struct rtentry *rt;
7487 	ipif_t *ipif = NULL;
7488 	ip_stack_t	*ipst;
7489 
7490 	ASSERT(q->q_next == NULL);
7491 	ipst = CONNQ_TO_IPST(q);
7492 
7493 	ip1dbg(("ip_siocdelrt:"));
7494 	/* Existence of mp1 verified in ip_wput_nondata */
7495 	mp1 = mp->b_cont->b_cont;
7496 	rt = (struct rtentry *)mp1->b_rptr;
7497 
7498 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7499 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7500 
7501 	/*
7502 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7503 	 * to a particular host address.  In this case, we set the netmask to
7504 	 * all ones for the particular destination address.  Otherwise,
7505 	 * determine the netmask to be used based on dst_addr and the interfaces
7506 	 * in use.
7507 	 */
7508 	if (rt->rt_flags & RTF_HOST) {
7509 		mask = IP_HOST_MASK;
7510 	} else {
7511 		/*
7512 		 * Note that ip_subnet_mask returns a zero mask in the case of
7513 		 * default (an all-zeroes address).
7514 		 */
7515 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7516 	}
7517 
7518 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7519 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7520 	    mp, ip_process_ioctl, ipst);
7521 	if (ipif != NULL)
7522 		ipif_refrele(ipif);
7523 	return (error);
7524 }
7525 
7526 /*
7527  * Enqueue the mp onto the ipsq, chained by b_next.
7528  * b_prev stores the function to be executed later, and b_queue the queue
7529  * where this mp originated.
7530  */
7531 void
7532 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7533     ill_t *pending_ill)
7534 {
7535 	conn_t	*connp = NULL;
7536 
7537 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7538 	ASSERT(func != NULL);
7539 
7540 	mp->b_queue = q;
7541 	mp->b_prev = (void *)func;
7542 	mp->b_next = NULL;
7543 
7544 	switch (type) {
7545 	case CUR_OP:
7546 		if (ipsq->ipsq_mptail != NULL) {
7547 			ASSERT(ipsq->ipsq_mphead != NULL);
7548 			ipsq->ipsq_mptail->b_next = mp;
7549 		} else {
7550 			ASSERT(ipsq->ipsq_mphead == NULL);
7551 			ipsq->ipsq_mphead = mp;
7552 		}
7553 		ipsq->ipsq_mptail = mp;
7554 		break;
7555 
7556 	case NEW_OP:
7557 		if (ipsq->ipsq_xopq_mptail != NULL) {
7558 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7559 			ipsq->ipsq_xopq_mptail->b_next = mp;
7560 		} else {
7561 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7562 			ipsq->ipsq_xopq_mphead = mp;
7563 		}
7564 		ipsq->ipsq_xopq_mptail = mp;
7565 		break;
7566 	default:
7567 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7568 	}
7569 
7570 	if (CONN_Q(q) && pending_ill != NULL) {
7571 		connp = Q_TO_CONN(q);
7572 
7573 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7574 		connp->conn_oper_pending_ill = pending_ill;
7575 	}
7576 }
7577 
7578 /*
7579  * Return the mp at the head of the ipsq. After emptying the ipsq
7580  * look at the next ioctl, if this ioctl is complete. Otherwise
7581  * return, we will resume when we complete the current ioctl.
7582  * The current ioctl will wait till it gets a response from the
7583  * driver below.
7584  */
7585 static mblk_t *
7586 ipsq_dq(ipsq_t *ipsq)
7587 {
7588 	mblk_t	*mp;
7589 
7590 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7591 
7592 	mp = ipsq->ipsq_mphead;
7593 	if (mp != NULL) {
7594 		ipsq->ipsq_mphead = mp->b_next;
7595 		if (ipsq->ipsq_mphead == NULL)
7596 			ipsq->ipsq_mptail = NULL;
7597 		mp->b_next = NULL;
7598 		return (mp);
7599 	}
7600 	if (ipsq->ipsq_current_ipif != NULL)
7601 		return (NULL);
7602 	mp = ipsq->ipsq_xopq_mphead;
7603 	if (mp != NULL) {
7604 		ipsq->ipsq_xopq_mphead = mp->b_next;
7605 		if (ipsq->ipsq_xopq_mphead == NULL)
7606 			ipsq->ipsq_xopq_mptail = NULL;
7607 		mp->b_next = NULL;
7608 		return (mp);
7609 	}
7610 	return (NULL);
7611 }
7612 
7613 /*
7614  * Enter the ipsq corresponding to ill, by waiting synchronously till
7615  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7616  * will have to drain completely before ipsq_enter returns success.
7617  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7618  * and the ipsq_exit logic will start the next enqueued ioctl after
7619  * completion of the current ioctl. If 'force' is used, we don't wait
7620  * for the enqueued ioctls. This is needed when a conn_close wants to
7621  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7622  * of an ill can also use this option. But we dont' use it currently.
7623  */
7624 #define	ENTER_SQ_WAIT_TICKS 100
7625 boolean_t
7626 ipsq_enter(ill_t *ill, boolean_t force)
7627 {
7628 	ipsq_t	*ipsq;
7629 	boolean_t waited_enough = B_FALSE;
7630 
7631 	/*
7632 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7633 	 * Since the <ill-ipsq> assocs could change while we wait for the
7634 	 * writer, it is easier to wait on a fixed global rather than try to
7635 	 * cv_wait on a changing ipsq.
7636 	 */
7637 	mutex_enter(&ill->ill_lock);
7638 	for (;;) {
7639 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7640 			mutex_exit(&ill->ill_lock);
7641 			return (B_FALSE);
7642 		}
7643 
7644 		ipsq = ill->ill_phyint->phyint_ipsq;
7645 		mutex_enter(&ipsq->ipsq_lock);
7646 		if (ipsq->ipsq_writer == NULL &&
7647 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7648 			break;
7649 		} else if (ipsq->ipsq_writer != NULL) {
7650 			mutex_exit(&ipsq->ipsq_lock);
7651 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7652 		} else {
7653 			mutex_exit(&ipsq->ipsq_lock);
7654 			if (force) {
7655 				(void) cv_timedwait(&ill->ill_cv,
7656 				    &ill->ill_lock,
7657 				    lbolt + ENTER_SQ_WAIT_TICKS);
7658 				waited_enough = B_TRUE;
7659 				continue;
7660 			} else {
7661 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7662 			}
7663 		}
7664 	}
7665 
7666 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7667 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7668 	ipsq->ipsq_writer = curthread;
7669 	ipsq->ipsq_reentry_cnt++;
7670 #ifdef DEBUG
7671 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7672 #endif
7673 	mutex_exit(&ipsq->ipsq_lock);
7674 	mutex_exit(&ill->ill_lock);
7675 	return (B_TRUE);
7676 }
7677 
7678 /*
7679  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7680  * certain critical operations like plumbing (i.e. most set ioctls),
7681  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7682  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7683  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7684  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7685  * threads executing in the ipsq. Responses from the driver pertain to the
7686  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7687  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7688  *
7689  * If a thread does not want to reenter the ipsq when it is already writer,
7690  * it must make sure that the specified reentry point to be called later
7691  * when the ipsq is empty, nor any code path starting from the specified reentry
7692  * point must never ever try to enter the ipsq again. Otherwise it can lead
7693  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7694  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7695  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7696  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7697  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7698  * ioctl if the current ioctl has completed. If the current ioctl is still
7699  * in progress it simply returns. The current ioctl could be waiting for
7700  * a response from another module (arp_ or the driver or could be waiting for
7701  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7702  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7703  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7704  * ipsq_current_ipif is clear which happens only on ioctl completion.
7705  */
7706 
7707 /*
7708  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7709  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7710  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7711  * completion.
7712  */
7713 ipsq_t *
7714 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7715     ipsq_func_t func, int type, boolean_t reentry_ok)
7716 {
7717 	ipsq_t	*ipsq;
7718 
7719 	/* Only 1 of ipif or ill can be specified */
7720 	ASSERT((ipif != NULL) ^ (ill != NULL));
7721 	if (ipif != NULL)
7722 		ill = ipif->ipif_ill;
7723 
7724 	/*
7725 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7726 	 * ipsq of an ill can't change when ill_lock is held.
7727 	 */
7728 	GRAB_CONN_LOCK(q);
7729 	mutex_enter(&ill->ill_lock);
7730 	ipsq = ill->ill_phyint->phyint_ipsq;
7731 	mutex_enter(&ipsq->ipsq_lock);
7732 
7733 	/*
7734 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7735 	 *    (Note: If the caller does not specify reentry_ok then neither
7736 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7737 	 *    again. Otherwise it can lead to an infinite loop
7738 	 * 2. Enter the ipsq if there is no current writer and this attempted
7739 	 *    entry is part of the current ioctl or operation
7740 	 * 3. Enter the ipsq if there is no current writer and this is a new
7741 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7742 	 *    empty and there is no ioctl (or operation) currently in progress
7743 	 */
7744 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7745 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7746 	    ipsq->ipsq_current_ipif == NULL))) ||
7747 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7748 		/* Success. */
7749 		ipsq->ipsq_reentry_cnt++;
7750 		ipsq->ipsq_writer = curthread;
7751 		mutex_exit(&ipsq->ipsq_lock);
7752 		mutex_exit(&ill->ill_lock);
7753 		RELEASE_CONN_LOCK(q);
7754 #ifdef DEBUG
7755 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7756 		    IPSQ_STACK_DEPTH);
7757 #endif
7758 		return (ipsq);
7759 	}
7760 
7761 	ipsq_enq(ipsq, q, mp, func, type, ill);
7762 
7763 	mutex_exit(&ipsq->ipsq_lock);
7764 	mutex_exit(&ill->ill_lock);
7765 	RELEASE_CONN_LOCK(q);
7766 	return (NULL);
7767 }
7768 
7769 /*
7770  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7771  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7772  * cannot be entered, the mp is queued for completion.
7773  */
7774 void
7775 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7776     boolean_t reentry_ok)
7777 {
7778 	ipsq_t	*ipsq;
7779 
7780 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7781 
7782 	/*
7783 	 * Drop the caller's refhold on the ill.  This is safe since we either
7784 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7785 	 * IPSQ, in which case we return without accessing ill anymore.  This
7786 	 * is needed because func needs to see the correct refcount.
7787 	 * e.g. removeif can work only then.
7788 	 */
7789 	ill_refrele(ill);
7790 	if (ipsq != NULL) {
7791 		(*func)(ipsq, q, mp, NULL);
7792 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7793 	}
7794 }
7795 
7796 /*
7797  * If there are more than ILL_GRP_CNT ills in a group,
7798  * we use kmem alloc'd buffers, else use the stack
7799  */
7800 #define	ILL_GRP_CNT	14
7801 /*
7802  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7803  * Called by a thread that is currently exclusive on this ipsq.
7804  */
7805 void
7806 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7807 {
7808 	queue_t	*q;
7809 	mblk_t	*mp;
7810 	ipsq_func_t	func;
7811 	int	next;
7812 	ill_t	**ill_list = NULL;
7813 	size_t	ill_list_size = 0;
7814 	int	cnt = 0;
7815 	boolean_t need_ipsq_free = B_FALSE;
7816 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7817 
7818 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7819 	mutex_enter(&ipsq->ipsq_lock);
7820 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7821 	if (ipsq->ipsq_reentry_cnt != 1) {
7822 		ipsq->ipsq_reentry_cnt--;
7823 		mutex_exit(&ipsq->ipsq_lock);
7824 		return;
7825 	}
7826 
7827 	mp = ipsq_dq(ipsq);
7828 	while (mp != NULL) {
7829 again:
7830 		mutex_exit(&ipsq->ipsq_lock);
7831 		func = (ipsq_func_t)mp->b_prev;
7832 		q = (queue_t *)mp->b_queue;
7833 		mp->b_prev = NULL;
7834 		mp->b_queue = NULL;
7835 
7836 		/*
7837 		 * If 'q' is an conn queue, it is valid, since we did a
7838 		 * a refhold on the connp, at the start of the ioctl.
7839 		 * If 'q' is an ill queue, it is valid, since close of an
7840 		 * ill will clean up the 'ipsq'.
7841 		 */
7842 		(*func)(ipsq, q, mp, NULL);
7843 
7844 		mutex_enter(&ipsq->ipsq_lock);
7845 		mp = ipsq_dq(ipsq);
7846 	}
7847 
7848 	mutex_exit(&ipsq->ipsq_lock);
7849 
7850 	/*
7851 	 * Need to grab the locks in the right order. Need to
7852 	 * atomically check (under ipsq_lock) that there are no
7853 	 * messages before relinquishing the ipsq. Also need to
7854 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7855 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7856 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7857 	 * to grab ill_g_lock as writer.
7858 	 */
7859 	rw_enter(&ipst->ips_ill_g_lock,
7860 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7861 
7862 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7863 	if (ipsq->ipsq_refs != 0) {
7864 		/* At most 2 ills v4/v6 per phyint */
7865 		cnt = ipsq->ipsq_refs << 1;
7866 		ill_list_size = cnt * sizeof (ill_t *);
7867 		/*
7868 		 * If memory allocation fails, we will do the split
7869 		 * the next time ipsq_exit is called for whatever reason.
7870 		 * As long as the ipsq_split flag is set the need to
7871 		 * split is remembered.
7872 		 */
7873 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7874 		if (ill_list != NULL)
7875 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7876 	}
7877 	mutex_enter(&ipsq->ipsq_lock);
7878 	mp = ipsq_dq(ipsq);
7879 	if (mp != NULL) {
7880 		/* oops, some message has landed up, we can't get out */
7881 		if (ill_list != NULL)
7882 			ill_unlock_ills(ill_list, cnt);
7883 		rw_exit(&ipst->ips_ill_g_lock);
7884 		if (ill_list != NULL)
7885 			kmem_free(ill_list, ill_list_size);
7886 		ill_list = NULL;
7887 		ill_list_size = 0;
7888 		cnt = 0;
7889 		goto again;
7890 	}
7891 
7892 	/*
7893 	 * Split only if no ioctl is pending and if memory alloc succeeded
7894 	 * above.
7895 	 */
7896 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7897 	    ill_list != NULL) {
7898 		/*
7899 		 * No new ill can join this ipsq since we are holding the
7900 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7901 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7902 		 * If so we will retry on the next ipsq_exit.
7903 		 */
7904 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7905 	}
7906 
7907 	/*
7908 	 * We are holding the ipsq lock, hence no new messages can
7909 	 * land up on the ipsq, and there are no messages currently.
7910 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7911 	 * atomically while holding ill locks.
7912 	 */
7913 	ipsq->ipsq_writer = NULL;
7914 	ipsq->ipsq_reentry_cnt--;
7915 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7916 #ifdef DEBUG
7917 	ipsq->ipsq_depth = 0;
7918 #endif
7919 	mutex_exit(&ipsq->ipsq_lock);
7920 	/*
7921 	 * For IPMP this should wake up all ills in this ipsq.
7922 	 * We need to hold the ill_lock while waking up waiters to
7923 	 * avoid missed wakeups. But there is no need to acquire all
7924 	 * the ill locks and then wakeup. If we have not acquired all
7925 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7926 	 * wakes up ills one at a time after getting the right ill_lock
7927 	 */
7928 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7929 	if (ill_list != NULL)
7930 		ill_unlock_ills(ill_list, cnt);
7931 	if (ipsq->ipsq_refs == 0)
7932 		need_ipsq_free = B_TRUE;
7933 	rw_exit(&ipst->ips_ill_g_lock);
7934 	if (ill_list != 0)
7935 		kmem_free(ill_list, ill_list_size);
7936 
7937 	if (need_ipsq_free) {
7938 		/*
7939 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7940 		 * looked up. ipsq can be looked up only thru ill or phyint
7941 		 * and there are no ills/phyint on this ipsq.
7942 		 */
7943 		ipsq_delete(ipsq);
7944 	}
7945 	/*
7946 	 * Now start any igmp or mld timers that could not be started
7947 	 * while inside the ipsq. The timers can't be started while inside
7948 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7949 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7950 	 * there could be a deadlock since the timeout handlers
7951 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7952 	 * wait in ipsq_enter() trying to get the ipsq.
7953 	 *
7954 	 * However there is one exception to the above. If this thread is
7955 	 * itself the igmp/mld timeout handler thread, then we don't want
7956 	 * to start any new timer until the current handler is done. The
7957 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7958 	 * all others pass B_TRUE.
7959 	 */
7960 	if (start_igmp_timer) {
7961 		mutex_enter(&ipst->ips_igmp_timer_lock);
7962 		next = ipst->ips_igmp_deferred_next;
7963 		ipst->ips_igmp_deferred_next = INFINITY;
7964 		mutex_exit(&ipst->ips_igmp_timer_lock);
7965 
7966 		if (next != INFINITY)
7967 			igmp_start_timers(next, ipst);
7968 	}
7969 
7970 	if (start_mld_timer) {
7971 		mutex_enter(&ipst->ips_mld_timer_lock);
7972 		next = ipst->ips_mld_deferred_next;
7973 		ipst->ips_mld_deferred_next = INFINITY;
7974 		mutex_exit(&ipst->ips_mld_timer_lock);
7975 
7976 		if (next != INFINITY)
7977 			mld_start_timers(next, ipst);
7978 	}
7979 }
7980 
7981 /*
7982  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
7983  * and `ioccmd'.
7984  */
7985 void
7986 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
7987 {
7988 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7989 
7990 	mutex_enter(&ipsq->ipsq_lock);
7991 	ASSERT(ipsq->ipsq_current_ipif == NULL);
7992 	ASSERT(ipsq->ipsq_current_ioctl == 0);
7993 	ipsq->ipsq_current_ipif = ipif;
7994 	ipsq->ipsq_current_ioctl = ioccmd;
7995 	mutex_exit(&ipsq->ipsq_lock);
7996 }
7997 
7998 /*
7999  * Finish the current exclusive operation on `ipsq'.  Note that other
8000  * operations will not be able to proceed until an ipsq_exit() is done.
8001  */
8002 void
8003 ipsq_current_finish(ipsq_t *ipsq)
8004 {
8005 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8006 
8007 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8008 
8009 	/*
8010 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8011 	 * (but we're careful to never set IPIF_CHANGING in that case).
8012 	 */
8013 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8014 		mutex_enter(&ipif->ipif_ill->ill_lock);
8015 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8016 
8017 		/* Send any queued event */
8018 		ill_nic_info_dispatch(ipif->ipif_ill);
8019 		mutex_exit(&ipif->ipif_ill->ill_lock);
8020 	}
8021 
8022 	mutex_enter(&ipsq->ipsq_lock);
8023 	ASSERT(ipsq->ipsq_current_ipif != NULL);
8024 	ipsq->ipsq_current_ipif = NULL;
8025 	ipsq->ipsq_current_ioctl = 0;
8026 	mutex_exit(&ipsq->ipsq_lock);
8027 }
8028 
8029 /*
8030  * The ill is closing. Flush all messages on the ipsq that originated
8031  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8032  * for this ill since ipsq_enter could not have entered until then.
8033  * New messages can't be queued since the CONDEMNED flag is set.
8034  */
8035 static void
8036 ipsq_flush(ill_t *ill)
8037 {
8038 	queue_t	*q;
8039 	mblk_t	*prev;
8040 	mblk_t	*mp;
8041 	mblk_t	*mp_next;
8042 	ipsq_t	*ipsq;
8043 
8044 	ASSERT(IAM_WRITER_ILL(ill));
8045 	ipsq = ill->ill_phyint->phyint_ipsq;
8046 	/*
8047 	 * Flush any messages sent up by the driver.
8048 	 */
8049 	mutex_enter(&ipsq->ipsq_lock);
8050 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8051 		mp_next = mp->b_next;
8052 		q = mp->b_queue;
8053 		if (q == ill->ill_rq || q == ill->ill_wq) {
8054 			/* Remove the mp from the ipsq */
8055 			if (prev == NULL)
8056 				ipsq->ipsq_mphead = mp->b_next;
8057 			else
8058 				prev->b_next = mp->b_next;
8059 			if (ipsq->ipsq_mptail == mp) {
8060 				ASSERT(mp_next == NULL);
8061 				ipsq->ipsq_mptail = prev;
8062 			}
8063 			inet_freemsg(mp);
8064 		} else {
8065 			prev = mp;
8066 		}
8067 	}
8068 	mutex_exit(&ipsq->ipsq_lock);
8069 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8070 	ipsq_xopq_mp_cleanup(ill, NULL);
8071 	ill_pending_mp_cleanup(ill);
8072 }
8073 
8074 /* ARGSUSED */
8075 int
8076 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8077     ip_ioctl_cmd_t *ipip, void *ifreq)
8078 {
8079 	ill_t	*ill;
8080 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8081 	boolean_t isv6;
8082 	conn_t	*connp;
8083 	ip_stack_t	*ipst;
8084 
8085 	connp = Q_TO_CONN(q);
8086 	ipst = connp->conn_netstack->netstack_ip;
8087 	isv6 = connp->conn_af_isv6;
8088 	/*
8089 	 * Set original index.
8090 	 * Failover and failback move logical interfaces
8091 	 * from one physical interface to another.  The
8092 	 * original index indicates the parent of a logical
8093 	 * interface, in other words, the physical interface
8094 	 * the logical interface will be moved back to on
8095 	 * failback.
8096 	 */
8097 
8098 	/*
8099 	 * Don't allow the original index to be changed
8100 	 * for non-failover addresses, autoconfigured
8101 	 * addresses, or IPv6 link local addresses.
8102 	 */
8103 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8104 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8105 		return (EINVAL);
8106 	}
8107 	/*
8108 	 * The new original index must be in use by some
8109 	 * physical interface.
8110 	 */
8111 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8112 	    NULL, NULL, ipst);
8113 	if (ill == NULL)
8114 		return (ENXIO);
8115 	ill_refrele(ill);
8116 
8117 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8118 	/*
8119 	 * When this ipif gets failed back, don't
8120 	 * preserve the original id, as it is no
8121 	 * longer applicable.
8122 	 */
8123 	ipif->ipif_orig_ipifid = 0;
8124 	/*
8125 	 * For IPv4, change the original index of any
8126 	 * multicast addresses associated with the
8127 	 * ipif to the new value.
8128 	 */
8129 	if (!isv6) {
8130 		ilm_t *ilm;
8131 
8132 		mutex_enter(&ipif->ipif_ill->ill_lock);
8133 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8134 		    ilm = ilm->ilm_next) {
8135 			if (ilm->ilm_ipif == ipif) {
8136 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8137 			}
8138 		}
8139 		mutex_exit(&ipif->ipif_ill->ill_lock);
8140 	}
8141 	return (0);
8142 }
8143 
8144 /* ARGSUSED */
8145 int
8146 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8147     ip_ioctl_cmd_t *ipip, void *ifreq)
8148 {
8149 	struct lifreq *lifr = (struct lifreq *)ifreq;
8150 
8151 	/*
8152 	 * Get the original interface index i.e the one
8153 	 * before FAILOVER if it ever happened.
8154 	 */
8155 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8156 	return (0);
8157 }
8158 
8159 /*
8160  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8161  * refhold and return the associated ipif
8162  */
8163 /* ARGSUSED */
8164 int
8165 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8166     cmd_info_t *ci, ipsq_func_t func)
8167 {
8168 	boolean_t exists;
8169 	struct iftun_req *ta;
8170 	ipif_t	*ipif;
8171 	ill_t	*ill;
8172 	boolean_t isv6;
8173 	mblk_t	*mp1;
8174 	int	error;
8175 	conn_t	*connp;
8176 	ip_stack_t	*ipst;
8177 
8178 	/* Existence verified in ip_wput_nondata */
8179 	mp1 = mp->b_cont->b_cont;
8180 	ta = (struct iftun_req *)mp1->b_rptr;
8181 	/*
8182 	 * Null terminate the string to protect against buffer
8183 	 * overrun. String was generated by user code and may not
8184 	 * be trusted.
8185 	 */
8186 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8187 
8188 	connp = Q_TO_CONN(q);
8189 	isv6 = connp->conn_af_isv6;
8190 	ipst = connp->conn_netstack->netstack_ip;
8191 
8192 	/* Disallows implicit create */
8193 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8194 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8195 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8196 	if (ipif == NULL)
8197 		return (error);
8198 
8199 	if (ipif->ipif_id != 0) {
8200 		/*
8201 		 * We really don't want to set/get tunnel parameters
8202 		 * on virtual tunnel interfaces.  Only allow the
8203 		 * base tunnel to do these.
8204 		 */
8205 		ipif_refrele(ipif);
8206 		return (EINVAL);
8207 	}
8208 
8209 	/*
8210 	 * Send down to tunnel mod for ioctl processing.
8211 	 * Will finish ioctl in ip_rput_other().
8212 	 */
8213 	ill = ipif->ipif_ill;
8214 	if (ill->ill_net_type == IRE_LOOPBACK) {
8215 		ipif_refrele(ipif);
8216 		return (EOPNOTSUPP);
8217 	}
8218 
8219 	if (ill->ill_wq == NULL) {
8220 		ipif_refrele(ipif);
8221 		return (ENXIO);
8222 	}
8223 	/*
8224 	 * Mark the ioctl as coming from an IPv6 interface for
8225 	 * tun's convenience.
8226 	 */
8227 	if (ill->ill_isv6)
8228 		ta->ifta_flags |= 0x80000000;
8229 	ci->ci_ipif = ipif;
8230 	return (0);
8231 }
8232 
8233 /*
8234  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8235  * and return the associated ipif.
8236  * Return value:
8237  *	Non zero: An error has occurred. ci may not be filled out.
8238  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8239  *	a held ipif in ci.ci_ipif.
8240  */
8241 int
8242 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8243     cmd_info_t *ci, ipsq_func_t func)
8244 {
8245 	sin_t		*sin;
8246 	sin6_t		*sin6;
8247 	char		*name;
8248 	struct ifreq    *ifr;
8249 	struct lifreq    *lifr;
8250 	ipif_t		*ipif = NULL;
8251 	ill_t		*ill;
8252 	conn_t		*connp;
8253 	boolean_t	isv6;
8254 	boolean_t	exists;
8255 	int		err;
8256 	mblk_t		*mp1;
8257 	zoneid_t	zoneid;
8258 	ip_stack_t	*ipst;
8259 
8260 	if (q->q_next != NULL) {
8261 		ill = (ill_t *)q->q_ptr;
8262 		isv6 = ill->ill_isv6;
8263 		connp = NULL;
8264 		zoneid = ALL_ZONES;
8265 		ipst = ill->ill_ipst;
8266 	} else {
8267 		ill = NULL;
8268 		connp = Q_TO_CONN(q);
8269 		isv6 = connp->conn_af_isv6;
8270 		zoneid = connp->conn_zoneid;
8271 		if (zoneid == GLOBAL_ZONEID) {
8272 			/* global zone can access ipifs in all zones */
8273 			zoneid = ALL_ZONES;
8274 		}
8275 		ipst = connp->conn_netstack->netstack_ip;
8276 	}
8277 
8278 	/* Has been checked in ip_wput_nondata */
8279 	mp1 = mp->b_cont->b_cont;
8280 
8281 	if (ipip->ipi_cmd_type == IF_CMD) {
8282 		/* This a old style SIOC[GS]IF* command */
8283 		ifr = (struct ifreq *)mp1->b_rptr;
8284 		/*
8285 		 * Null terminate the string to protect against buffer
8286 		 * overrun. String was generated by user code and may not
8287 		 * be trusted.
8288 		 */
8289 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8290 		sin = (sin_t *)&ifr->ifr_addr;
8291 		name = ifr->ifr_name;
8292 		ci->ci_sin = sin;
8293 		ci->ci_sin6 = NULL;
8294 		ci->ci_lifr = (struct lifreq *)ifr;
8295 	} else {
8296 		/* This a new style SIOC[GS]LIF* command */
8297 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8298 		lifr = (struct lifreq *)mp1->b_rptr;
8299 		/*
8300 		 * Null terminate the string to protect against buffer
8301 		 * overrun. String was generated by user code and may not
8302 		 * be trusted.
8303 		 */
8304 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8305 		name = lifr->lifr_name;
8306 		sin = (sin_t *)&lifr->lifr_addr;
8307 		sin6 = (sin6_t *)&lifr->lifr_addr;
8308 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8309 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8310 			    LIFNAMSIZ);
8311 		}
8312 		ci->ci_sin = sin;
8313 		ci->ci_sin6 = sin6;
8314 		ci->ci_lifr = lifr;
8315 	}
8316 
8317 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8318 		/*
8319 		 * The ioctl will be failed if the ioctl comes down
8320 		 * an conn stream
8321 		 */
8322 		if (ill == NULL) {
8323 			/*
8324 			 * Not an ill queue, return EINVAL same as the
8325 			 * old error code.
8326 			 */
8327 			return (ENXIO);
8328 		}
8329 		ipif = ill->ill_ipif;
8330 		ipif_refhold(ipif);
8331 	} else {
8332 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8333 		    &exists, isv6, zoneid,
8334 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8335 		    ipst);
8336 		if (ipif == NULL) {
8337 			if (err == EINPROGRESS)
8338 				return (err);
8339 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8340 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8341 				/*
8342 				 * Need to try both v4 and v6 since this
8343 				 * ioctl can come down either v4 or v6
8344 				 * socket. The lifreq.lifr_family passed
8345 				 * down by this ioctl is AF_UNSPEC.
8346 				 */
8347 				ipif = ipif_lookup_on_name(name,
8348 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8349 				    zoneid, (connp == NULL) ? q :
8350 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8351 				if (err == EINPROGRESS)
8352 					return (err);
8353 			}
8354 			err = 0;	/* Ensure we don't use it below */
8355 		}
8356 	}
8357 
8358 	/*
8359 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8360 	 */
8361 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8362 		ipif_refrele(ipif);
8363 		return (ENXIO);
8364 	}
8365 
8366 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8367 	    name[0] == '\0') {
8368 		/*
8369 		 * Handle a or a SIOC?IF* with a null name
8370 		 * during plumb (on the ill queue before the I_PLINK).
8371 		 */
8372 		ipif = ill->ill_ipif;
8373 		ipif_refhold(ipif);
8374 	}
8375 
8376 	if (ipif == NULL)
8377 		return (ENXIO);
8378 
8379 	/*
8380 	 * Allow only GET operations if this ipif has been created
8381 	 * temporarily due to a MOVE operation.
8382 	 */
8383 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8384 		ipif_refrele(ipif);
8385 		return (EINVAL);
8386 	}
8387 
8388 	ci->ci_ipif = ipif;
8389 	return (0);
8390 }
8391 
8392 /*
8393  * Return the total number of ipifs.
8394  */
8395 static uint_t
8396 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8397 {
8398 	uint_t numifs = 0;
8399 	ill_t	*ill;
8400 	ill_walk_context_t	ctx;
8401 	ipif_t	*ipif;
8402 
8403 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8404 	ill = ILL_START_WALK_V4(&ctx, ipst);
8405 
8406 	while (ill != NULL) {
8407 		for (ipif = ill->ill_ipif; ipif != NULL;
8408 		    ipif = ipif->ipif_next) {
8409 			if (ipif->ipif_zoneid == zoneid ||
8410 			    ipif->ipif_zoneid == ALL_ZONES)
8411 				numifs++;
8412 		}
8413 		ill = ill_next(&ctx, ill);
8414 	}
8415 	rw_exit(&ipst->ips_ill_g_lock);
8416 	return (numifs);
8417 }
8418 
8419 /*
8420  * Return the total number of ipifs.
8421  */
8422 static uint_t
8423 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8424 {
8425 	uint_t numifs = 0;
8426 	ill_t	*ill;
8427 	ipif_t	*ipif;
8428 	ill_walk_context_t	ctx;
8429 
8430 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8431 
8432 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8433 	if (family == AF_INET)
8434 		ill = ILL_START_WALK_V4(&ctx, ipst);
8435 	else if (family == AF_INET6)
8436 		ill = ILL_START_WALK_V6(&ctx, ipst);
8437 	else
8438 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8439 
8440 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8441 		for (ipif = ill->ill_ipif; ipif != NULL;
8442 		    ipif = ipif->ipif_next) {
8443 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8444 			    !(lifn_flags & LIFC_NOXMIT))
8445 				continue;
8446 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8447 			    !(lifn_flags & LIFC_TEMPORARY))
8448 				continue;
8449 			if (((ipif->ipif_flags &
8450 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8451 			    IPIF_DEPRECATED)) ||
8452 			    IS_LOOPBACK(ill) ||
8453 			    !(ipif->ipif_flags & IPIF_UP)) &&
8454 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8455 				continue;
8456 
8457 			if (zoneid != ipif->ipif_zoneid &&
8458 			    ipif->ipif_zoneid != ALL_ZONES &&
8459 			    (zoneid != GLOBAL_ZONEID ||
8460 			    !(lifn_flags & LIFC_ALLZONES)))
8461 				continue;
8462 
8463 			numifs++;
8464 		}
8465 	}
8466 	rw_exit(&ipst->ips_ill_g_lock);
8467 	return (numifs);
8468 }
8469 
8470 uint_t
8471 ip_get_lifsrcofnum(ill_t *ill)
8472 {
8473 	uint_t numifs = 0;
8474 	ill_t	*ill_head = ill;
8475 	ip_stack_t	*ipst = ill->ill_ipst;
8476 
8477 	/*
8478 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8479 	 * other thread may be trying to relink the ILLs in this usesrc group
8480 	 * and adjusting the ill_usesrc_grp_next pointers
8481 	 */
8482 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8483 	if ((ill->ill_usesrc_ifindex == 0) &&
8484 	    (ill->ill_usesrc_grp_next != NULL)) {
8485 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8486 		    ill = ill->ill_usesrc_grp_next)
8487 			numifs++;
8488 	}
8489 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8490 
8491 	return (numifs);
8492 }
8493 
8494 /* Null values are passed in for ipif, sin, and ifreq */
8495 /* ARGSUSED */
8496 int
8497 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8498     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8499 {
8500 	int *nump;
8501 	conn_t *connp = Q_TO_CONN(q);
8502 
8503 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8504 
8505 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8506 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8507 
8508 	*nump = ip_get_numifs(connp->conn_zoneid,
8509 	    connp->conn_netstack->netstack_ip);
8510 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8511 	return (0);
8512 }
8513 
8514 /* Null values are passed in for ipif, sin, and ifreq */
8515 /* ARGSUSED */
8516 int
8517 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8518     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8519 {
8520 	struct lifnum *lifn;
8521 	mblk_t	*mp1;
8522 	conn_t *connp = Q_TO_CONN(q);
8523 
8524 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8525 
8526 	/* Existence checked in ip_wput_nondata */
8527 	mp1 = mp->b_cont->b_cont;
8528 
8529 	lifn = (struct lifnum *)mp1->b_rptr;
8530 	switch (lifn->lifn_family) {
8531 	case AF_UNSPEC:
8532 	case AF_INET:
8533 	case AF_INET6:
8534 		break;
8535 	default:
8536 		return (EAFNOSUPPORT);
8537 	}
8538 
8539 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8540 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8541 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8542 	return (0);
8543 }
8544 
8545 /* ARGSUSED */
8546 int
8547 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8548     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8549 {
8550 	STRUCT_HANDLE(ifconf, ifc);
8551 	mblk_t *mp1;
8552 	struct iocblk *iocp;
8553 	struct ifreq *ifr;
8554 	ill_walk_context_t	ctx;
8555 	ill_t	*ill;
8556 	ipif_t	*ipif;
8557 	struct sockaddr_in *sin;
8558 	int32_t	ifclen;
8559 	zoneid_t zoneid;
8560 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8561 
8562 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8563 
8564 	ip1dbg(("ip_sioctl_get_ifconf"));
8565 	/* Existence verified in ip_wput_nondata */
8566 	mp1 = mp->b_cont->b_cont;
8567 	iocp = (struct iocblk *)mp->b_rptr;
8568 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8569 
8570 	/*
8571 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8572 	 * the user buffer address and length into which the list of struct
8573 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8574 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8575 	 * the SIOCGIFCONF operation was redefined to simply provide
8576 	 * a large output buffer into which we are supposed to jam the ifreq
8577 	 * array.  The same ioctl command code was used, despite the fact that
8578 	 * both the applications and the kernel code had to change, thus making
8579 	 * it impossible to support both interfaces.
8580 	 *
8581 	 * For reasons not good enough to try to explain, the following
8582 	 * algorithm is used for deciding what to do with one of these:
8583 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8584 	 * form with the output buffer coming down as the continuation message.
8585 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8586 	 * and we have to copy in the ifconf structure to find out how big the
8587 	 * output buffer is and where to copy out to.  Sure no problem...
8588 	 *
8589 	 */
8590 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8591 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8592 		int numifs = 0;
8593 		size_t ifc_bufsize;
8594 
8595 		/*
8596 		 * Must be (better be!) continuation of a TRANSPARENT
8597 		 * IOCTL.  We just copied in the ifconf structure.
8598 		 */
8599 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8600 		    (struct ifconf *)mp1->b_rptr);
8601 
8602 		/*
8603 		 * Allocate a buffer to hold requested information.
8604 		 *
8605 		 * If ifc_len is larger than what is needed, we only
8606 		 * allocate what we will use.
8607 		 *
8608 		 * If ifc_len is smaller than what is needed, return
8609 		 * EINVAL.
8610 		 *
8611 		 * XXX: the ill_t structure can hava 2 counters, for
8612 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8613 		 * number of interfaces for a device, so we don't need
8614 		 * to count them here...
8615 		 */
8616 		numifs = ip_get_numifs(zoneid, ipst);
8617 
8618 		ifclen = STRUCT_FGET(ifc, ifc_len);
8619 		ifc_bufsize = numifs * sizeof (struct ifreq);
8620 		if (ifc_bufsize > ifclen) {
8621 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8622 				/* old behaviour */
8623 				return (EINVAL);
8624 			} else {
8625 				ifc_bufsize = ifclen;
8626 			}
8627 		}
8628 
8629 		mp1 = mi_copyout_alloc(q, mp,
8630 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8631 		if (mp1 == NULL)
8632 			return (ENOMEM);
8633 
8634 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8635 	}
8636 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8637 	/*
8638 	 * the SIOCGIFCONF ioctl only knows about
8639 	 * IPv4 addresses, so don't try to tell
8640 	 * it about interfaces with IPv6-only
8641 	 * addresses. (Last parm 'isv6' is B_FALSE)
8642 	 */
8643 
8644 	ifr = (struct ifreq *)mp1->b_rptr;
8645 
8646 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8647 	ill = ILL_START_WALK_V4(&ctx, ipst);
8648 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8649 		for (ipif = ill->ill_ipif; ipif != NULL;
8650 		    ipif = ipif->ipif_next) {
8651 			if (zoneid != ipif->ipif_zoneid &&
8652 			    ipif->ipif_zoneid != ALL_ZONES)
8653 				continue;
8654 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8655 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8656 					/* old behaviour */
8657 					rw_exit(&ipst->ips_ill_g_lock);
8658 					return (EINVAL);
8659 				} else {
8660 					goto if_copydone;
8661 				}
8662 			}
8663 			ipif_get_name(ipif, ifr->ifr_name,
8664 			    sizeof (ifr->ifr_name));
8665 			sin = (sin_t *)&ifr->ifr_addr;
8666 			*sin = sin_null;
8667 			sin->sin_family = AF_INET;
8668 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8669 			ifr++;
8670 		}
8671 	}
8672 if_copydone:
8673 	rw_exit(&ipst->ips_ill_g_lock);
8674 	mp1->b_wptr = (uchar_t *)ifr;
8675 
8676 	if (STRUCT_BUF(ifc) != NULL) {
8677 		STRUCT_FSET(ifc, ifc_len,
8678 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8679 	}
8680 	return (0);
8681 }
8682 
8683 /*
8684  * Get the interfaces using the address hosted on the interface passed in,
8685  * as a source adddress
8686  */
8687 /* ARGSUSED */
8688 int
8689 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8690     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8691 {
8692 	mblk_t *mp1;
8693 	ill_t	*ill, *ill_head;
8694 	ipif_t	*ipif, *orig_ipif;
8695 	int	numlifs = 0;
8696 	size_t	lifs_bufsize, lifsmaxlen;
8697 	struct	lifreq *lifr;
8698 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8699 	uint_t	ifindex;
8700 	zoneid_t zoneid;
8701 	int err = 0;
8702 	boolean_t isv6 = B_FALSE;
8703 	struct	sockaddr_in	*sin;
8704 	struct	sockaddr_in6	*sin6;
8705 	STRUCT_HANDLE(lifsrcof, lifs);
8706 	ip_stack_t		*ipst;
8707 
8708 	ipst = CONNQ_TO_IPST(q);
8709 
8710 	ASSERT(q->q_next == NULL);
8711 
8712 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8713 
8714 	/* Existence verified in ip_wput_nondata */
8715 	mp1 = mp->b_cont->b_cont;
8716 
8717 	/*
8718 	 * Must be (better be!) continuation of a TRANSPARENT
8719 	 * IOCTL.  We just copied in the lifsrcof structure.
8720 	 */
8721 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8722 	    (struct lifsrcof *)mp1->b_rptr);
8723 
8724 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8725 		return (EINVAL);
8726 
8727 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8728 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8729 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8730 	    ip_process_ioctl, &err, ipst);
8731 	if (ipif == NULL) {
8732 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8733 		    ifindex));
8734 		return (err);
8735 	}
8736 
8737 
8738 	/* Allocate a buffer to hold requested information */
8739 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8740 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8741 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8742 	/* The actual size needed is always returned in lifs_len */
8743 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8744 
8745 	/* If the amount we need is more than what is passed in, abort */
8746 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8747 		ipif_refrele(ipif);
8748 		return (0);
8749 	}
8750 
8751 	mp1 = mi_copyout_alloc(q, mp,
8752 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8753 	if (mp1 == NULL) {
8754 		ipif_refrele(ipif);
8755 		return (ENOMEM);
8756 	}
8757 
8758 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8759 	bzero(mp1->b_rptr, lifs_bufsize);
8760 
8761 	lifr = (struct lifreq *)mp1->b_rptr;
8762 
8763 	ill = ill_head = ipif->ipif_ill;
8764 	orig_ipif = ipif;
8765 
8766 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8767 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8768 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8769 
8770 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8771 	for (; (ill != NULL) && (ill != ill_head);
8772 	    ill = ill->ill_usesrc_grp_next) {
8773 
8774 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8775 			break;
8776 
8777 		ipif = ill->ill_ipif;
8778 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8779 		if (ipif->ipif_isv6) {
8780 			sin6 = (sin6_t *)&lifr->lifr_addr;
8781 			*sin6 = sin6_null;
8782 			sin6->sin6_family = AF_INET6;
8783 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8784 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8785 			    &ipif->ipif_v6net_mask);
8786 		} else {
8787 			sin = (sin_t *)&lifr->lifr_addr;
8788 			*sin = sin_null;
8789 			sin->sin_family = AF_INET;
8790 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8791 			lifr->lifr_addrlen = ip_mask_to_plen(
8792 			    ipif->ipif_net_mask);
8793 		}
8794 		lifr++;
8795 	}
8796 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8797 	rw_exit(&ipst->ips_ill_g_lock);
8798 	ipif_refrele(orig_ipif);
8799 	mp1->b_wptr = (uchar_t *)lifr;
8800 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8801 
8802 	return (0);
8803 }
8804 
8805 /* ARGSUSED */
8806 int
8807 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8808     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8809 {
8810 	mblk_t *mp1;
8811 	int	list;
8812 	ill_t	*ill;
8813 	ipif_t	*ipif;
8814 	int	flags;
8815 	int	numlifs = 0;
8816 	size_t	lifc_bufsize;
8817 	struct	lifreq *lifr;
8818 	sa_family_t	family;
8819 	struct	sockaddr_in	*sin;
8820 	struct	sockaddr_in6	*sin6;
8821 	ill_walk_context_t	ctx;
8822 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8823 	int32_t	lifclen;
8824 	zoneid_t zoneid;
8825 	STRUCT_HANDLE(lifconf, lifc);
8826 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8827 
8828 	ip1dbg(("ip_sioctl_get_lifconf"));
8829 
8830 	ASSERT(q->q_next == NULL);
8831 
8832 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8833 
8834 	/* Existence verified in ip_wput_nondata */
8835 	mp1 = mp->b_cont->b_cont;
8836 
8837 	/*
8838 	 * An extended version of SIOCGIFCONF that takes an
8839 	 * additional address family and flags field.
8840 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8841 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8842 	 * interfaces are omitted.
8843 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8844 	 * unless LIFC_TEMPORARY is specified.
8845 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8846 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8847 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8848 	 * has priority over LIFC_NOXMIT.
8849 	 */
8850 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8851 
8852 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8853 		return (EINVAL);
8854 
8855 	/*
8856 	 * Must be (better be!) continuation of a TRANSPARENT
8857 	 * IOCTL.  We just copied in the lifconf structure.
8858 	 */
8859 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8860 
8861 	family = STRUCT_FGET(lifc, lifc_family);
8862 	flags = STRUCT_FGET(lifc, lifc_flags);
8863 
8864 	switch (family) {
8865 	case AF_UNSPEC:
8866 		/*
8867 		 * walk all ILL's.
8868 		 */
8869 		list = MAX_G_HEADS;
8870 		break;
8871 	case AF_INET:
8872 		/*
8873 		 * walk only IPV4 ILL's.
8874 		 */
8875 		list = IP_V4_G_HEAD;
8876 		break;
8877 	case AF_INET6:
8878 		/*
8879 		 * walk only IPV6 ILL's.
8880 		 */
8881 		list = IP_V6_G_HEAD;
8882 		break;
8883 	default:
8884 		return (EAFNOSUPPORT);
8885 	}
8886 
8887 	/*
8888 	 * Allocate a buffer to hold requested information.
8889 	 *
8890 	 * If lifc_len is larger than what is needed, we only
8891 	 * allocate what we will use.
8892 	 *
8893 	 * If lifc_len is smaller than what is needed, return
8894 	 * EINVAL.
8895 	 */
8896 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8897 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8898 	lifclen = STRUCT_FGET(lifc, lifc_len);
8899 	if (lifc_bufsize > lifclen) {
8900 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8901 			return (EINVAL);
8902 		else
8903 			lifc_bufsize = lifclen;
8904 	}
8905 
8906 	mp1 = mi_copyout_alloc(q, mp,
8907 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8908 	if (mp1 == NULL)
8909 		return (ENOMEM);
8910 
8911 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8912 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8913 
8914 	lifr = (struct lifreq *)mp1->b_rptr;
8915 
8916 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8917 	ill = ill_first(list, list, &ctx, ipst);
8918 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8919 		for (ipif = ill->ill_ipif; ipif != NULL;
8920 		    ipif = ipif->ipif_next) {
8921 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8922 			    !(flags & LIFC_NOXMIT))
8923 				continue;
8924 
8925 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8926 			    !(flags & LIFC_TEMPORARY))
8927 				continue;
8928 
8929 			if (((ipif->ipif_flags &
8930 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8931 			    IPIF_DEPRECATED)) ||
8932 			    IS_LOOPBACK(ill) ||
8933 			    !(ipif->ipif_flags & IPIF_UP)) &&
8934 			    (flags & LIFC_EXTERNAL_SOURCE))
8935 				continue;
8936 
8937 			if (zoneid != ipif->ipif_zoneid &&
8938 			    ipif->ipif_zoneid != ALL_ZONES &&
8939 			    (zoneid != GLOBAL_ZONEID ||
8940 			    !(flags & LIFC_ALLZONES)))
8941 				continue;
8942 
8943 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8944 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8945 					rw_exit(&ipst->ips_ill_g_lock);
8946 					return (EINVAL);
8947 				} else {
8948 					goto lif_copydone;
8949 				}
8950 			}
8951 
8952 			ipif_get_name(ipif, lifr->lifr_name,
8953 			    sizeof (lifr->lifr_name));
8954 			if (ipif->ipif_isv6) {
8955 				sin6 = (sin6_t *)&lifr->lifr_addr;
8956 				*sin6 = sin6_null;
8957 				sin6->sin6_family = AF_INET6;
8958 				sin6->sin6_addr =
8959 				    ipif->ipif_v6lcl_addr;
8960 				lifr->lifr_addrlen =
8961 				    ip_mask_to_plen_v6(
8962 				    &ipif->ipif_v6net_mask);
8963 			} else {
8964 				sin = (sin_t *)&lifr->lifr_addr;
8965 				*sin = sin_null;
8966 				sin->sin_family = AF_INET;
8967 				sin->sin_addr.s_addr =
8968 				    ipif->ipif_lcl_addr;
8969 				lifr->lifr_addrlen =
8970 				    ip_mask_to_plen(
8971 				    ipif->ipif_net_mask);
8972 			}
8973 			lifr++;
8974 		}
8975 	}
8976 lif_copydone:
8977 	rw_exit(&ipst->ips_ill_g_lock);
8978 
8979 	mp1->b_wptr = (uchar_t *)lifr;
8980 	if (STRUCT_BUF(lifc) != NULL) {
8981 		STRUCT_FSET(lifc, lifc_len,
8982 		    (int)((uchar_t *)lifr - mp1->b_rptr));
8983 	}
8984 	return (0);
8985 }
8986 
8987 /* ARGSUSED */
8988 int
8989 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8990     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8991 {
8992 	ip_stack_t	*ipst;
8993 
8994 	if (q->q_next == NULL)
8995 		ipst = CONNQ_TO_IPST(q);
8996 	else
8997 		ipst = ILLQ_TO_IPST(q);
8998 
8999 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9000 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9001 	return (0);
9002 }
9003 
9004 static void
9005 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9006 {
9007 	ip6_asp_t *table;
9008 	size_t table_size;
9009 	mblk_t *data_mp;
9010 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9011 	ip_stack_t	*ipst;
9012 
9013 	if (q->q_next == NULL)
9014 		ipst = CONNQ_TO_IPST(q);
9015 	else
9016 		ipst = ILLQ_TO_IPST(q);
9017 
9018 	/* These two ioctls are I_STR only */
9019 	if (iocp->ioc_count == TRANSPARENT) {
9020 		miocnak(q, mp, 0, EINVAL);
9021 		return;
9022 	}
9023 
9024 	data_mp = mp->b_cont;
9025 	if (data_mp == NULL) {
9026 		/* The user passed us a NULL argument */
9027 		table = NULL;
9028 		table_size = iocp->ioc_count;
9029 	} else {
9030 		/*
9031 		 * The user provided a table.  The stream head
9032 		 * may have copied in the user data in chunks,
9033 		 * so make sure everything is pulled up
9034 		 * properly.
9035 		 */
9036 		if (MBLKL(data_mp) < iocp->ioc_count) {
9037 			mblk_t *new_data_mp;
9038 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9039 			    NULL) {
9040 				miocnak(q, mp, 0, ENOMEM);
9041 				return;
9042 			}
9043 			freemsg(data_mp);
9044 			data_mp = new_data_mp;
9045 			mp->b_cont = data_mp;
9046 		}
9047 		table = (ip6_asp_t *)data_mp->b_rptr;
9048 		table_size = iocp->ioc_count;
9049 	}
9050 
9051 	switch (iocp->ioc_cmd) {
9052 	case SIOCGIP6ADDRPOLICY:
9053 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9054 		if (iocp->ioc_rval == -1)
9055 			iocp->ioc_error = EINVAL;
9056 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9057 		else if (table != NULL &&
9058 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9059 			ip6_asp_t *src = table;
9060 			ip6_asp32_t *dst = (void *)table;
9061 			int count = table_size / sizeof (ip6_asp_t);
9062 			int i;
9063 
9064 			/*
9065 			 * We need to do an in-place shrink of the array
9066 			 * to match the alignment attributes of the
9067 			 * 32-bit ABI looking at it.
9068 			 */
9069 			/* LINTED: logical expression always true: op "||" */
9070 			ASSERT(sizeof (*src) > sizeof (*dst));
9071 			for (i = 1; i < count; i++)
9072 				bcopy(src + i, dst + i, sizeof (*dst));
9073 		}
9074 #endif
9075 		break;
9076 
9077 	case SIOCSIP6ADDRPOLICY:
9078 		ASSERT(mp->b_prev == NULL);
9079 		mp->b_prev = (void *)q;
9080 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9081 		/*
9082 		 * We pass in the datamodel here so that the ip6_asp_replace()
9083 		 * routine can handle converting from 32-bit to native formats
9084 		 * where necessary.
9085 		 *
9086 		 * A better way to handle this might be to convert the inbound
9087 		 * data structure here, and hang it off a new 'mp'; thus the
9088 		 * ip6_asp_replace() logic would always be dealing with native
9089 		 * format data structures..
9090 		 *
9091 		 * (An even simpler way to handle these ioctls is to just
9092 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9093 		 * and just recompile everything that depends on it.)
9094 		 */
9095 #endif
9096 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9097 		    iocp->ioc_flag & IOC_MODELS);
9098 		return;
9099 	}
9100 
9101 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9102 	qreply(q, mp);
9103 }
9104 
9105 static void
9106 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9107 {
9108 	mblk_t 		*data_mp;
9109 	struct dstinforeq	*dir;
9110 	uint8_t		*end, *cur;
9111 	in6_addr_t	*daddr, *saddr;
9112 	ipaddr_t	v4daddr;
9113 	ire_t		*ire;
9114 	char		*slabel, *dlabel;
9115 	boolean_t	isipv4;
9116 	int		match_ire;
9117 	ill_t		*dst_ill;
9118 	ipif_t		*src_ipif, *ire_ipif;
9119 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9120 	zoneid_t	zoneid;
9121 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9122 
9123 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9124 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9125 
9126 	/*
9127 	 * This ioctl is I_STR only, and must have a
9128 	 * data mblk following the M_IOCTL mblk.
9129 	 */
9130 	data_mp = mp->b_cont;
9131 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9132 		miocnak(q, mp, 0, EINVAL);
9133 		return;
9134 	}
9135 
9136 	if (MBLKL(data_mp) < iocp->ioc_count) {
9137 		mblk_t *new_data_mp;
9138 
9139 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9140 			miocnak(q, mp, 0, ENOMEM);
9141 			return;
9142 		}
9143 		freemsg(data_mp);
9144 		data_mp = new_data_mp;
9145 		mp->b_cont = data_mp;
9146 	}
9147 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9148 
9149 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9150 	    end - cur >= sizeof (struct dstinforeq);
9151 	    cur += sizeof (struct dstinforeq)) {
9152 		dir = (struct dstinforeq *)cur;
9153 		daddr = &dir->dir_daddr;
9154 		saddr = &dir->dir_saddr;
9155 
9156 		/*
9157 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9158 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9159 		 * and ipif_select_source[_v6]() do not.
9160 		 */
9161 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9162 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9163 
9164 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9165 		if (isipv4) {
9166 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9167 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9168 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9169 		} else {
9170 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9171 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9172 		}
9173 		if (ire == NULL) {
9174 			dir->dir_dreachable = 0;
9175 
9176 			/* move on to next dst addr */
9177 			continue;
9178 		}
9179 		dir->dir_dreachable = 1;
9180 
9181 		ire_ipif = ire->ire_ipif;
9182 		if (ire_ipif == NULL)
9183 			goto next_dst;
9184 
9185 		/*
9186 		 * We expect to get back an interface ire or a
9187 		 * gateway ire cache entry.  For both types, the
9188 		 * output interface is ire_ipif->ipif_ill.
9189 		 */
9190 		dst_ill = ire_ipif->ipif_ill;
9191 		dir->dir_dmactype = dst_ill->ill_mactype;
9192 
9193 		if (isipv4) {
9194 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9195 		} else {
9196 			src_ipif = ipif_select_source_v6(dst_ill,
9197 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9198 			    zoneid);
9199 		}
9200 		if (src_ipif == NULL)
9201 			goto next_dst;
9202 
9203 		*saddr = src_ipif->ipif_v6lcl_addr;
9204 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9205 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9206 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9207 		dir->dir_sdeprecated =
9208 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9209 		ipif_refrele(src_ipif);
9210 next_dst:
9211 		ire_refrele(ire);
9212 	}
9213 	miocack(q, mp, iocp->ioc_count, 0);
9214 }
9215 
9216 
9217 /*
9218  * Check if this is an address assigned to this machine.
9219  * Skips interfaces that are down by using ire checks.
9220  * Translates mapped addresses to v4 addresses and then
9221  * treats them as such, returning true if the v4 address
9222  * associated with this mapped address is configured.
9223  * Note: Applications will have to be careful what they do
9224  * with the response; use of mapped addresses limits
9225  * what can be done with the socket, especially with
9226  * respect to socket options and ioctls - neither IPv4
9227  * options nor IPv6 sticky options/ancillary data options
9228  * may be used.
9229  */
9230 /* ARGSUSED */
9231 int
9232 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9233     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9234 {
9235 	struct sioc_addrreq *sia;
9236 	sin_t *sin;
9237 	ire_t *ire;
9238 	mblk_t *mp1;
9239 	zoneid_t zoneid;
9240 	ip_stack_t	*ipst;
9241 
9242 	ip1dbg(("ip_sioctl_tmyaddr"));
9243 
9244 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9245 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9246 	ipst = CONNQ_TO_IPST(q);
9247 
9248 	/* Existence verified in ip_wput_nondata */
9249 	mp1 = mp->b_cont->b_cont;
9250 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9251 	sin = (sin_t *)&sia->sa_addr;
9252 	switch (sin->sin_family) {
9253 	case AF_INET6: {
9254 		sin6_t *sin6 = (sin6_t *)sin;
9255 
9256 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9257 			ipaddr_t v4_addr;
9258 
9259 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9260 			    v4_addr);
9261 			ire = ire_ctable_lookup(v4_addr, 0,
9262 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9263 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9264 		} else {
9265 			in6_addr_t v6addr;
9266 
9267 			v6addr = sin6->sin6_addr;
9268 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9269 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9270 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9271 		}
9272 		break;
9273 	}
9274 	case AF_INET: {
9275 		ipaddr_t v4addr;
9276 
9277 		v4addr = sin->sin_addr.s_addr;
9278 		ire = ire_ctable_lookup(v4addr, 0,
9279 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9280 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9281 		break;
9282 	}
9283 	default:
9284 		return (EAFNOSUPPORT);
9285 	}
9286 	if (ire != NULL) {
9287 		sia->sa_res = 1;
9288 		ire_refrele(ire);
9289 	} else {
9290 		sia->sa_res = 0;
9291 	}
9292 	return (0);
9293 }
9294 
9295 /*
9296  * Check if this is an address assigned on-link i.e. neighbor,
9297  * and makes sure it's reachable from the current zone.
9298  * Returns true for my addresses as well.
9299  * Translates mapped addresses to v4 addresses and then
9300  * treats them as such, returning true if the v4 address
9301  * associated with this mapped address is configured.
9302  * Note: Applications will have to be careful what they do
9303  * with the response; use of mapped addresses limits
9304  * what can be done with the socket, especially with
9305  * respect to socket options and ioctls - neither IPv4
9306  * options nor IPv6 sticky options/ancillary data options
9307  * may be used.
9308  */
9309 /* ARGSUSED */
9310 int
9311 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9312     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9313 {
9314 	struct sioc_addrreq *sia;
9315 	sin_t *sin;
9316 	mblk_t	*mp1;
9317 	ire_t *ire = NULL;
9318 	zoneid_t zoneid;
9319 	ip_stack_t	*ipst;
9320 
9321 	ip1dbg(("ip_sioctl_tonlink"));
9322 
9323 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9324 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9325 	ipst = CONNQ_TO_IPST(q);
9326 
9327 	/* Existence verified in ip_wput_nondata */
9328 	mp1 = mp->b_cont->b_cont;
9329 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9330 	sin = (sin_t *)&sia->sa_addr;
9331 
9332 	/*
9333 	 * Match addresses with a zero gateway field to avoid
9334 	 * routes going through a router.
9335 	 * Exclude broadcast and multicast addresses.
9336 	 */
9337 	switch (sin->sin_family) {
9338 	case AF_INET6: {
9339 		sin6_t *sin6 = (sin6_t *)sin;
9340 
9341 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9342 			ipaddr_t v4_addr;
9343 
9344 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9345 			    v4_addr);
9346 			if (!CLASSD(v4_addr)) {
9347 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9348 				    NULL, NULL, zoneid, NULL,
9349 				    MATCH_IRE_GW, ipst);
9350 			}
9351 		} else {
9352 			in6_addr_t v6addr;
9353 			in6_addr_t v6gw;
9354 
9355 			v6addr = sin6->sin6_addr;
9356 			v6gw = ipv6_all_zeros;
9357 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9358 				ire = ire_route_lookup_v6(&v6addr, 0,
9359 				    &v6gw, 0, NULL, NULL, zoneid,
9360 				    NULL, MATCH_IRE_GW, ipst);
9361 			}
9362 		}
9363 		break;
9364 	}
9365 	case AF_INET: {
9366 		ipaddr_t v4addr;
9367 
9368 		v4addr = sin->sin_addr.s_addr;
9369 		if (!CLASSD(v4addr)) {
9370 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9371 			    NULL, NULL, zoneid, NULL,
9372 			    MATCH_IRE_GW, ipst);
9373 		}
9374 		break;
9375 	}
9376 	default:
9377 		return (EAFNOSUPPORT);
9378 	}
9379 	sia->sa_res = 0;
9380 	if (ire != NULL) {
9381 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9382 		    IRE_LOCAL|IRE_LOOPBACK)) {
9383 			sia->sa_res = 1;
9384 		}
9385 		ire_refrele(ire);
9386 	}
9387 	return (0);
9388 }
9389 
9390 /*
9391  * TBD: implement when kernel maintaines a list of site prefixes.
9392  */
9393 /* ARGSUSED */
9394 int
9395 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9396     ip_ioctl_cmd_t *ipip, void *ifreq)
9397 {
9398 	return (ENXIO);
9399 }
9400 
9401 /* ARGSUSED */
9402 int
9403 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9404     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9405 {
9406 	ill_t  		*ill;
9407 	mblk_t		*mp1;
9408 	conn_t		*connp;
9409 	boolean_t	success;
9410 
9411 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9412 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9413 	/* ioctl comes down on an conn */
9414 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9415 	connp = Q_TO_CONN(q);
9416 
9417 	mp->b_datap->db_type = M_IOCTL;
9418 
9419 	/*
9420 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9421 	 * The original mp contains contaminated b_next values due to 'mi',
9422 	 * which is needed to do the mi_copy_done. Unfortunately if we
9423 	 * send down the original mblk itself and if we are popped due to an
9424 	 * an unplumb before the response comes back from tunnel,
9425 	 * the streamhead (which does a freemsg) will see this contaminated
9426 	 * message and the assertion in freemsg about non-null b_next/b_prev
9427 	 * will panic a DEBUG kernel.
9428 	 */
9429 	mp1 = copymsg(mp);
9430 	if (mp1 == NULL)
9431 		return (ENOMEM);
9432 
9433 	ill = ipif->ipif_ill;
9434 	mutex_enter(&connp->conn_lock);
9435 	mutex_enter(&ill->ill_lock);
9436 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9437 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9438 		    mp, 0);
9439 	} else {
9440 		success = ill_pending_mp_add(ill, connp, mp);
9441 	}
9442 	mutex_exit(&ill->ill_lock);
9443 	mutex_exit(&connp->conn_lock);
9444 
9445 	if (success) {
9446 		ip1dbg(("sending down tunparam request "));
9447 		putnext(ill->ill_wq, mp1);
9448 		return (EINPROGRESS);
9449 	} else {
9450 		/* The conn has started closing */
9451 		freemsg(mp1);
9452 		return (EINTR);
9453 	}
9454 }
9455 
9456 /*
9457  * ARP IOCTLs.
9458  * How does IP get in the business of fronting ARP configuration/queries?
9459  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9460  * are by tradition passed in through a datagram socket.  That lands in IP.
9461  * As it happens, this is just as well since the interface is quite crude in
9462  * that it passes in no information about protocol or hardware types, or
9463  * interface association.  After making the protocol assumption, IP is in
9464  * the position to look up the name of the ILL, which ARP will need, and
9465  * format a request that can be handled by ARP.  The request is passed up
9466  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9467  * back a response.  ARP supports its own set of more general IOCTLs, in
9468  * case anyone is interested.
9469  */
9470 /* ARGSUSED */
9471 int
9472 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9473     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9474 {
9475 	mblk_t *mp1;
9476 	mblk_t *mp2;
9477 	mblk_t *pending_mp;
9478 	ipaddr_t ipaddr;
9479 	area_t *area;
9480 	struct iocblk *iocp;
9481 	conn_t *connp;
9482 	struct arpreq *ar;
9483 	struct xarpreq *xar;
9484 	int flags, alength;
9485 	char *lladdr;
9486 	ip_stack_t	*ipst;
9487 	ill_t *ill = ipif->ipif_ill;
9488 	boolean_t if_arp_ioctl = B_FALSE;
9489 
9490 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9491 	connp = Q_TO_CONN(q);
9492 	ipst = connp->conn_netstack->netstack_ip;
9493 
9494 	if (ipip->ipi_cmd_type == XARP_CMD) {
9495 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9496 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9497 		ar = NULL;
9498 
9499 		flags = xar->xarp_flags;
9500 		lladdr = LLADDR(&xar->xarp_ha);
9501 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9502 		/*
9503 		 * Validate against user's link layer address length
9504 		 * input and name and addr length limits.
9505 		 */
9506 		alength = ill->ill_phys_addr_length;
9507 		if (ipip->ipi_cmd == SIOCSXARP) {
9508 			if (alength != xar->xarp_ha.sdl_alen ||
9509 			    (alength + xar->xarp_ha.sdl_nlen >
9510 			    sizeof (xar->xarp_ha.sdl_data)))
9511 				return (EINVAL);
9512 		}
9513 	} else {
9514 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9515 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9516 		xar = NULL;
9517 
9518 		flags = ar->arp_flags;
9519 		lladdr = ar->arp_ha.sa_data;
9520 		/*
9521 		 * Theoretically, the sa_family could tell us what link
9522 		 * layer type this operation is trying to deal with. By
9523 		 * common usage AF_UNSPEC means ethernet. We'll assume
9524 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9525 		 * for now. Our new SIOC*XARP ioctls can be used more
9526 		 * generally.
9527 		 *
9528 		 * If the underlying media happens to have a non 6 byte
9529 		 * address, arp module will fail set/get, but the del
9530 		 * operation will succeed.
9531 		 */
9532 		alength = 6;
9533 		if ((ipip->ipi_cmd != SIOCDARP) &&
9534 		    (alength != ill->ill_phys_addr_length)) {
9535 			return (EINVAL);
9536 		}
9537 	}
9538 
9539 	/*
9540 	 * We are going to pass up to ARP a packet chain that looks
9541 	 * like:
9542 	 *
9543 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9544 	 *
9545 	 * Get a copy of the original IOCTL mblk to head the chain,
9546 	 * to be sent up (in mp1). Also get another copy to store
9547 	 * in the ill_pending_mp list, for matching the response
9548 	 * when it comes back from ARP.
9549 	 */
9550 	mp1 = copyb(mp);
9551 	pending_mp = copymsg(mp);
9552 	if (mp1 == NULL || pending_mp == NULL) {
9553 		if (mp1 != NULL)
9554 			freeb(mp1);
9555 		if (pending_mp != NULL)
9556 			inet_freemsg(pending_mp);
9557 		return (ENOMEM);
9558 	}
9559 
9560 	ipaddr = sin->sin_addr.s_addr;
9561 
9562 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9563 	    (caddr_t)&ipaddr);
9564 	if (mp2 == NULL) {
9565 		freeb(mp1);
9566 		inet_freemsg(pending_mp);
9567 		return (ENOMEM);
9568 	}
9569 	/* Put together the chain. */
9570 	mp1->b_cont = mp2;
9571 	mp1->b_datap->db_type = M_IOCTL;
9572 	mp2->b_cont = mp;
9573 	mp2->b_datap->db_type = M_DATA;
9574 
9575 	iocp = (struct iocblk *)mp1->b_rptr;
9576 
9577 	/*
9578 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9579 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9580 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9581 	 * ioc_count field; set ioc_count to be correct.
9582 	 */
9583 	iocp->ioc_count = MBLKL(mp1->b_cont);
9584 
9585 	/*
9586 	 * Set the proper command in the ARP message.
9587 	 * Convert the SIOC{G|S|D}ARP calls into our
9588 	 * AR_ENTRY_xxx calls.
9589 	 */
9590 	area = (area_t *)mp2->b_rptr;
9591 	switch (iocp->ioc_cmd) {
9592 	case SIOCDARP:
9593 	case SIOCDXARP:
9594 		/*
9595 		 * We defer deleting the corresponding IRE until
9596 		 * we return from arp.
9597 		 */
9598 		area->area_cmd = AR_ENTRY_DELETE;
9599 		area->area_proto_mask_offset = 0;
9600 		break;
9601 	case SIOCGARP:
9602 	case SIOCGXARP:
9603 		area->area_cmd = AR_ENTRY_SQUERY;
9604 		area->area_proto_mask_offset = 0;
9605 		break;
9606 	case SIOCSARP:
9607 	case SIOCSXARP:
9608 		/*
9609 		 * Delete the corresponding ire to make sure IP will
9610 		 * pick up any change from arp.
9611 		 */
9612 		if (!if_arp_ioctl) {
9613 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9614 		} else {
9615 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9616 			if (ipif != NULL) {
9617 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9618 				    ipst);
9619 				ipif_refrele(ipif);
9620 			}
9621 		}
9622 		break;
9623 	}
9624 	iocp->ioc_cmd = area->area_cmd;
9625 
9626 	/*
9627 	 * Fill in the rest of the ARP operation fields.
9628 	 */
9629 	area->area_hw_addr_length = alength;
9630 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9631 
9632 	/* Translate the flags. */
9633 	if (flags & ATF_PERM)
9634 		area->area_flags |= ACE_F_PERMANENT;
9635 	if (flags & ATF_PUBL)
9636 		area->area_flags |= ACE_F_PUBLISH;
9637 	if (flags & ATF_AUTHORITY)
9638 		area->area_flags |= ACE_F_AUTHORITY;
9639 
9640 	/*
9641 	 * Before sending 'mp' to ARP, we have to clear the b_next
9642 	 * and b_prev. Otherwise if STREAMS encounters such a message
9643 	 * in freemsg(), (because ARP can close any time) it can cause
9644 	 * a panic. But mi code needs the b_next and b_prev values of
9645 	 * mp->b_cont, to complete the ioctl. So we store it here
9646 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9647 	 * when the response comes down from ARP.
9648 	 */
9649 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9650 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9651 	mp->b_cont->b_next = NULL;
9652 	mp->b_cont->b_prev = NULL;
9653 
9654 	mutex_enter(&connp->conn_lock);
9655 	mutex_enter(&ill->ill_lock);
9656 	/* conn has not yet started closing, hence this can't fail */
9657 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9658 	mutex_exit(&ill->ill_lock);
9659 	mutex_exit(&connp->conn_lock);
9660 
9661 	/*
9662 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9663 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9664 	 */
9665 	putnext(ill->ill_rq, mp1);
9666 	return (EINPROGRESS);
9667 }
9668 
9669 /*
9670  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9671  * the associated sin and refhold and return the associated ipif via `ci'.
9672  */
9673 int
9674 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9675     cmd_info_t *ci, ipsq_func_t func)
9676 {
9677 	mblk_t	*mp1;
9678 	int	err;
9679 	sin_t	*sin;
9680 	conn_t	*connp;
9681 	ipif_t	*ipif;
9682 	ire_t	*ire = NULL;
9683 	ill_t	*ill = NULL;
9684 	boolean_t exists;
9685 	ip_stack_t *ipst;
9686 	struct arpreq *ar;
9687 	struct xarpreq *xar;
9688 	struct sockaddr_dl *sdl;
9689 
9690 	/* ioctl comes down on a conn */
9691 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9692 	connp = Q_TO_CONN(q);
9693 	if (connp->conn_af_isv6)
9694 		return (ENXIO);
9695 
9696 	ipst = connp->conn_netstack->netstack_ip;
9697 
9698 	/* Verified in ip_wput_nondata */
9699 	mp1 = mp->b_cont->b_cont;
9700 
9701 	if (ipip->ipi_cmd_type == XARP_CMD) {
9702 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9703 		xar = (struct xarpreq *)mp1->b_rptr;
9704 		sin = (sin_t *)&xar->xarp_pa;
9705 		sdl = &xar->xarp_ha;
9706 
9707 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9708 			return (ENXIO);
9709 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9710 			return (EINVAL);
9711 	} else {
9712 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9713 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9714 		ar = (struct arpreq *)mp1->b_rptr;
9715 		sin = (sin_t *)&ar->arp_pa;
9716 	}
9717 
9718 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9719 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9720 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9721 		    mp, func, &err, ipst);
9722 		if (ipif == NULL)
9723 			return (err);
9724 		if (ipif->ipif_id != 0 ||
9725 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9726 			ipif_refrele(ipif);
9727 			return (ENXIO);
9728 		}
9729 	} else {
9730 		/*
9731 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9732 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9733 		 * case, a simple forwarding table lookup will return the
9734 		 * IRE_IF_RESOLVER for the first interface in the group, which
9735 		 * might not be the interface on which the requested IP
9736 		 * address was resolved due to the ill selection algorithm
9737 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9738 		 * lookup first: if the IRE cache entry for the IP address is
9739 		 * still there, it will contain the ill pointer for the right
9740 		 * interface, so we use that. If the cache entry has been
9741 		 * flushed, we fall back to the forwarding table lookup. This
9742 		 * should be rare enough since IRE cache entries have a longer
9743 		 * life expectancy than ARP cache entries.
9744 		 */
9745 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9746 		    ipst);
9747 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9748 		    ((ill = ire_to_ill(ire)) == NULL) ||
9749 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9750 			if (ire != NULL)
9751 				ire_refrele(ire);
9752 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9753 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9754 			    NULL, MATCH_IRE_TYPE, ipst);
9755 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9756 
9757 				if (ire != NULL)
9758 					ire_refrele(ire);
9759 				return (ENXIO);
9760 			}
9761 		}
9762 		ASSERT(ire != NULL && ill != NULL);
9763 		ipif = ill->ill_ipif;
9764 		ipif_refhold(ipif);
9765 		ire_refrele(ire);
9766 	}
9767 	ci->ci_sin = sin;
9768 	ci->ci_ipif = ipif;
9769 	return (0);
9770 }
9771 
9772 /*
9773  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9774  * atomically set/clear the muxids. Also complete the ioctl by acking or
9775  * naking it.  Note that the code is structured such that the link type,
9776  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9777  * its clones use the persistent link, while pppd(1M) and perhaps many
9778  * other daemons may use non-persistent link.  When combined with some
9779  * ill_t states, linking and unlinking lower streams may be used as
9780  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9781  */
9782 /* ARGSUSED */
9783 void
9784 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9785 {
9786 	mblk_t		*mp1, *mp2;
9787 	struct linkblk	*li;
9788 	struct ipmx_s	*ipmxp;
9789 	ill_t		*ill;
9790 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9791 	int		err = 0;
9792 	boolean_t	entered_ipsq = B_FALSE;
9793 	boolean_t	islink;
9794 	ip_stack_t	*ipst;
9795 
9796 	if (CONN_Q(q))
9797 		ipst = CONNQ_TO_IPST(q);
9798 	else
9799 		ipst = ILLQ_TO_IPST(q);
9800 
9801 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9802 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9803 
9804 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9805 
9806 	mp1 = mp->b_cont;	/* This is the linkblk info */
9807 	li = (struct linkblk *)mp1->b_rptr;
9808 
9809 	/*
9810 	 * ARP has added this special mblk, and the utility is asking us
9811 	 * to perform consistency checks, and also atomically set the
9812 	 * muxid. Ifconfig is an example.  It achieves this by using
9813 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9814 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9815 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9816 	 * and other comments in this routine for more details.
9817 	 */
9818 	mp2 = mp1->b_cont;	/* This is added by ARP */
9819 
9820 	/*
9821 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9822 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9823 	 * get the special mblk above.  For backward compatibility, we
9824 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9825 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9826 	 * not atomic, and can leave the streams unplumbable if the utility
9827 	 * is interrupted before it does the SIOCSLIFMUXID.
9828 	 */
9829 	if (mp2 == NULL) {
9830 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9831 		if (err == EINPROGRESS)
9832 			return;
9833 		goto done;
9834 	}
9835 
9836 	/*
9837 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9838 	 * ARP has appended this last mblk to tell us whether the lower stream
9839 	 * is an arp-dev stream or an IP module stream.
9840 	 */
9841 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9842 	if (ipmxp->ipmx_arpdev_stream) {
9843 		/*
9844 		 * The lower stream is the arp-dev stream.
9845 		 */
9846 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9847 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9848 		if (ill == NULL) {
9849 			if (err == EINPROGRESS)
9850 				return;
9851 			err = EINVAL;
9852 			goto done;
9853 		}
9854 
9855 		if (ipsq == NULL) {
9856 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9857 			    NEW_OP, B_TRUE);
9858 			if (ipsq == NULL) {
9859 				ill_refrele(ill);
9860 				return;
9861 			}
9862 			entered_ipsq = B_TRUE;
9863 		}
9864 		ASSERT(IAM_WRITER_ILL(ill));
9865 		ill_refrele(ill);
9866 
9867 		/*
9868 		 * To ensure consistency between IP and ARP, the following
9869 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9870 		 * This is because the muxid's are stored in the IP stream on
9871 		 * the ill.
9872 		 *
9873 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9874 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9875 		 * not yet plinked, and it also checks that the corresponding
9876 		 * IP stream is already plinked.
9877 		 *
9878 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9879 		 * punlinking the IP stream. IP does not allow punlink of the
9880 		 * IP stream unless the arp stream has been punlinked.
9881 		 */
9882 		if ((islink &&
9883 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9884 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9885 			err = EINVAL;
9886 			goto done;
9887 		}
9888 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9889 	} else {
9890 		/*
9891 		 * The lower stream is probably an IP module stream.  Do
9892 		 * consistency checking.
9893 		 */
9894 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9895 		if (err == EINPROGRESS)
9896 			return;
9897 	}
9898 done:
9899 	if (err == 0)
9900 		miocack(q, mp, 0, 0);
9901 	else
9902 		miocnak(q, mp, 0, err);
9903 
9904 	/* Conn was refheld in ip_sioctl_copyin_setup */
9905 	if (CONN_Q(q))
9906 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9907 	if (entered_ipsq)
9908 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9909 }
9910 
9911 /*
9912  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9913  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9914  * module stream).  If `doconsist' is set, then do the extended consistency
9915  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9916  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9917  * an error code on failure.
9918  */
9919 static int
9920 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9921     struct linkblk *li, boolean_t doconsist)
9922 {
9923 	ill_t  		*ill;
9924 	queue_t		*ipwq, *dwq;
9925 	const char	*name;
9926 	struct qinit	*qinfo;
9927 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9928 	boolean_t	entered_ipsq = B_FALSE;
9929 
9930 	/*
9931 	 * Walk the lower stream to verify it's the IP module stream.
9932 	 * The IP module is identified by its name, wput function,
9933 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9934 	 * (li->l_qbot) will not vanish until this ioctl completes.
9935 	 */
9936 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9937 		qinfo = ipwq->q_qinfo;
9938 		name = qinfo->qi_minfo->mi_idname;
9939 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9940 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9941 			break;
9942 		}
9943 	}
9944 
9945 	/*
9946 	 * If this isn't an IP module stream, bail.
9947 	 */
9948 	if (ipwq == NULL)
9949 		return (0);
9950 
9951 	ill = ipwq->q_ptr;
9952 	ASSERT(ill != NULL);
9953 
9954 	if (ipsq == NULL) {
9955 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9956 		    NEW_OP, B_TRUE);
9957 		if (ipsq == NULL)
9958 			return (EINPROGRESS);
9959 		entered_ipsq = B_TRUE;
9960 	}
9961 	ASSERT(IAM_WRITER_ILL(ill));
9962 
9963 	if (doconsist) {
9964 		/*
9965 		 * Consistency checking requires that I_{P}LINK occurs
9966 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
9967 		 * occurs prior to clearing ill_arp_muxid.
9968 		 */
9969 		if ((islink && ill->ill_ip_muxid != 0) ||
9970 		    (!islink && ill->ill_arp_muxid != 0)) {
9971 			if (entered_ipsq)
9972 				ipsq_exit(ipsq, B_TRUE, B_TRUE);
9973 			return (EINVAL);
9974 		}
9975 	}
9976 
9977 	/*
9978 	 * As part of I_{P}LINKing, stash the number of downstream modules and
9979 	 * the read queue of the module immediately below IP in the ill.
9980 	 * These are used during the capability negotiation below.
9981 	 */
9982 	ill->ill_lmod_rq = NULL;
9983 	ill->ill_lmod_cnt = 0;
9984 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
9985 		ill->ill_lmod_rq = RD(dwq);
9986 		for (; dwq != NULL; dwq = dwq->q_next)
9987 			ill->ill_lmod_cnt++;
9988 	}
9989 
9990 	if (doconsist)
9991 		ill->ill_ip_muxid = islink ? li->l_index : 0;
9992 
9993 	/*
9994 	 * If there's at least one up ipif on this ill, then we're bound to
9995 	 * the underlying driver via DLPI.  In that case, renegotiate
9996 	 * capabilities to account for any possible change in modules
9997 	 * interposed between IP and the driver.
9998 	 */
9999 	if (ill->ill_ipif_up_count > 0) {
10000 		if (islink)
10001 			ill_capability_probe(ill);
10002 		else
10003 			ill_capability_reset(ill);
10004 	}
10005 
10006 	if (entered_ipsq)
10007 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
10008 
10009 	return (0);
10010 }
10011 
10012 /*
10013  * Search the ioctl command in the ioctl tables and return a pointer
10014  * to the ioctl command information. The ioctl command tables are
10015  * static and fully populated at compile time.
10016  */
10017 ip_ioctl_cmd_t *
10018 ip_sioctl_lookup(int ioc_cmd)
10019 {
10020 	int index;
10021 	ip_ioctl_cmd_t *ipip;
10022 	ip_ioctl_cmd_t *ipip_end;
10023 
10024 	if (ioc_cmd == IPI_DONTCARE)
10025 		return (NULL);
10026 
10027 	/*
10028 	 * Do a 2 step search. First search the indexed table
10029 	 * based on the least significant byte of the ioctl cmd.
10030 	 * If we don't find a match, then search the misc table
10031 	 * serially.
10032 	 */
10033 	index = ioc_cmd & 0xFF;
10034 	if (index < ip_ndx_ioctl_count) {
10035 		ipip = &ip_ndx_ioctl_table[index];
10036 		if (ipip->ipi_cmd == ioc_cmd) {
10037 			/* Found a match in the ndx table */
10038 			return (ipip);
10039 		}
10040 	}
10041 
10042 	/* Search the misc table */
10043 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10044 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10045 		if (ipip->ipi_cmd == ioc_cmd)
10046 			/* Found a match in the misc table */
10047 			return (ipip);
10048 	}
10049 
10050 	return (NULL);
10051 }
10052 
10053 /*
10054  * Wrapper function for resuming deferred ioctl processing
10055  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10056  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10057  */
10058 /* ARGSUSED */
10059 void
10060 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10061     void *dummy_arg)
10062 {
10063 	ip_sioctl_copyin_setup(q, mp);
10064 }
10065 
10066 /*
10067  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10068  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10069  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10070  * We establish here the size of the block to be copied in.  mi_copyin
10071  * arranges for this to happen, an processing continues in ip_wput with
10072  * an M_IOCDATA message.
10073  */
10074 void
10075 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10076 {
10077 	int	copyin_size;
10078 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10079 	ip_ioctl_cmd_t *ipip;
10080 	cred_t *cr;
10081 	ip_stack_t	*ipst;
10082 
10083 	if (CONN_Q(q))
10084 		ipst = CONNQ_TO_IPST(q);
10085 	else
10086 		ipst = ILLQ_TO_IPST(q);
10087 
10088 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10089 	if (ipip == NULL) {
10090 		/*
10091 		 * The ioctl is not one we understand or own.
10092 		 * Pass it along to be processed down stream,
10093 		 * if this is a module instance of IP, else nak
10094 		 * the ioctl.
10095 		 */
10096 		if (q->q_next == NULL) {
10097 			goto nak;
10098 		} else {
10099 			putnext(q, mp);
10100 			return;
10101 		}
10102 	}
10103 
10104 	/*
10105 	 * If this is deferred, then we will do all the checks when we
10106 	 * come back.
10107 	 */
10108 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10109 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10110 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10111 		return;
10112 	}
10113 
10114 	/*
10115 	 * Only allow a very small subset of IP ioctls on this stream if
10116 	 * IP is a module and not a driver. Allowing ioctls to be processed
10117 	 * in this case may cause assert failures or data corruption.
10118 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10119 	 * ioctls allowed on an IP module stream, after which this stream
10120 	 * normally becomes a multiplexor (at which time the stream head
10121 	 * will fail all ioctls).
10122 	 */
10123 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10124 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10125 			/*
10126 			 * Pass common Streams ioctls which the IP
10127 			 * module does not own or consume along to
10128 			 * be processed down stream.
10129 			 */
10130 			putnext(q, mp);
10131 			return;
10132 		} else {
10133 			goto nak;
10134 		}
10135 	}
10136 
10137 	/* Make sure we have ioctl data to process. */
10138 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10139 		goto nak;
10140 
10141 	/*
10142 	 * Prefer dblk credential over ioctl credential; some synthesized
10143 	 * ioctls have kcred set because there's no way to crhold()
10144 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10145 	 * the framework; the caller of ioctl needs to hold the reference
10146 	 * for the duration of the call).
10147 	 */
10148 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10149 
10150 	/* Make sure normal users don't send down privileged ioctls */
10151 	if ((ipip->ipi_flags & IPI_PRIV) &&
10152 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10153 		/* We checked the privilege earlier but log it here */
10154 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10155 		return;
10156 	}
10157 
10158 	/*
10159 	 * The ioctl command tables can only encode fixed length
10160 	 * ioctl data. If the length is variable, the table will
10161 	 * encode the length as zero. Such special cases are handled
10162 	 * below in the switch.
10163 	 */
10164 	if (ipip->ipi_copyin_size != 0) {
10165 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10166 		return;
10167 	}
10168 
10169 	switch (iocp->ioc_cmd) {
10170 	case O_SIOCGIFCONF:
10171 	case SIOCGIFCONF:
10172 		/*
10173 		 * This IOCTL is hilarious.  See comments in
10174 		 * ip_sioctl_get_ifconf for the story.
10175 		 */
10176 		if (iocp->ioc_count == TRANSPARENT)
10177 			copyin_size = SIZEOF_STRUCT(ifconf,
10178 			    iocp->ioc_flag);
10179 		else
10180 			copyin_size = iocp->ioc_count;
10181 		mi_copyin(q, mp, NULL, copyin_size);
10182 		return;
10183 
10184 	case O_SIOCGLIFCONF:
10185 	case SIOCGLIFCONF:
10186 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10187 		mi_copyin(q, mp, NULL, copyin_size);
10188 		return;
10189 
10190 	case SIOCGLIFSRCOF:
10191 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10192 		mi_copyin(q, mp, NULL, copyin_size);
10193 		return;
10194 	case SIOCGIP6ADDRPOLICY:
10195 		ip_sioctl_ip6addrpolicy(q, mp);
10196 		ip6_asp_table_refrele(ipst);
10197 		return;
10198 
10199 	case SIOCSIP6ADDRPOLICY:
10200 		ip_sioctl_ip6addrpolicy(q, mp);
10201 		return;
10202 
10203 	case SIOCGDSTINFO:
10204 		ip_sioctl_dstinfo(q, mp);
10205 		ip6_asp_table_refrele(ipst);
10206 		return;
10207 
10208 	case I_PLINK:
10209 	case I_PUNLINK:
10210 	case I_LINK:
10211 	case I_UNLINK:
10212 		/*
10213 		 * We treat non-persistent link similarly as the persistent
10214 		 * link case, in terms of plumbing/unplumbing, as well as
10215 		 * dynamic re-plumbing events indicator.  See comments
10216 		 * in ip_sioctl_plink() for more.
10217 		 *
10218 		 * Request can be enqueued in the 'ipsq' while waiting
10219 		 * to become exclusive. So bump up the conn ref.
10220 		 */
10221 		if (CONN_Q(q))
10222 			CONN_INC_REF(Q_TO_CONN(q));
10223 		ip_sioctl_plink(NULL, q, mp, NULL);
10224 		return;
10225 
10226 	case ND_GET:
10227 	case ND_SET:
10228 		/*
10229 		 * Use of the nd table requires holding the reader lock.
10230 		 * Modifying the nd table thru nd_load/nd_unload requires
10231 		 * the writer lock.
10232 		 */
10233 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10234 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10235 			rw_exit(&ipst->ips_ip_g_nd_lock);
10236 
10237 			if (iocp->ioc_error)
10238 				iocp->ioc_count = 0;
10239 			mp->b_datap->db_type = M_IOCACK;
10240 			qreply(q, mp);
10241 			return;
10242 		}
10243 		rw_exit(&ipst->ips_ip_g_nd_lock);
10244 		/*
10245 		 * We don't understand this subioctl of ND_GET / ND_SET.
10246 		 * Maybe intended for some driver / module below us
10247 		 */
10248 		if (q->q_next) {
10249 			putnext(q, mp);
10250 		} else {
10251 			iocp->ioc_error = ENOENT;
10252 			mp->b_datap->db_type = M_IOCNAK;
10253 			iocp->ioc_count = 0;
10254 			qreply(q, mp);
10255 		}
10256 		return;
10257 
10258 	case IP_IOCTL:
10259 		ip_wput_ioctl(q, mp);
10260 		return;
10261 	default:
10262 		cmn_err(CE_PANIC, "should not happen ");
10263 	}
10264 nak:
10265 	if (mp->b_cont != NULL) {
10266 		freemsg(mp->b_cont);
10267 		mp->b_cont = NULL;
10268 	}
10269 	iocp->ioc_error = EINVAL;
10270 	mp->b_datap->db_type = M_IOCNAK;
10271 	iocp->ioc_count = 0;
10272 	qreply(q, mp);
10273 }
10274 
10275 /* ip_wput hands off ARP IOCTL responses to us */
10276 void
10277 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10278 {
10279 	struct arpreq *ar;
10280 	struct xarpreq *xar;
10281 	area_t	*area;
10282 	mblk_t	*area_mp;
10283 	struct iocblk *iocp;
10284 	mblk_t	*orig_ioc_mp, *tmp;
10285 	struct iocblk	*orig_iocp;
10286 	ill_t *ill;
10287 	conn_t *connp = NULL;
10288 	uint_t ioc_id;
10289 	mblk_t *pending_mp;
10290 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10291 	int *flagsp;
10292 	char *storage = NULL;
10293 	sin_t *sin;
10294 	ipaddr_t addr;
10295 	int err;
10296 	ip_stack_t *ipst;
10297 
10298 	ill = q->q_ptr;
10299 	ASSERT(ill != NULL);
10300 	ipst = ill->ill_ipst;
10301 
10302 	/*
10303 	 * We should get back from ARP a packet chain that looks like:
10304 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10305 	 */
10306 	if (!(area_mp = mp->b_cont) ||
10307 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10308 	    !(orig_ioc_mp = area_mp->b_cont) ||
10309 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10310 		freemsg(mp);
10311 		return;
10312 	}
10313 
10314 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10315 
10316 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10317 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10318 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10319 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10320 		x_arp_ioctl = B_TRUE;
10321 		xar = (struct xarpreq *)tmp->b_rptr;
10322 		sin = (sin_t *)&xar->xarp_pa;
10323 		flagsp = &xar->xarp_flags;
10324 		storage = xar->xarp_ha.sdl_data;
10325 		if (xar->xarp_ha.sdl_nlen != 0)
10326 			ifx_arp_ioctl = B_TRUE;
10327 	} else {
10328 		ar = (struct arpreq *)tmp->b_rptr;
10329 		sin = (sin_t *)&ar->arp_pa;
10330 		flagsp = &ar->arp_flags;
10331 		storage = ar->arp_ha.sa_data;
10332 	}
10333 
10334 	iocp = (struct iocblk *)mp->b_rptr;
10335 
10336 	/*
10337 	 * Pick out the originating queue based on the ioc_id.
10338 	 */
10339 	ioc_id = iocp->ioc_id;
10340 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10341 	if (pending_mp == NULL) {
10342 		ASSERT(connp == NULL);
10343 		inet_freemsg(mp);
10344 		return;
10345 	}
10346 	ASSERT(connp != NULL);
10347 	q = CONNP_TO_WQ(connp);
10348 
10349 	/* Uncouple the internally generated IOCTL from the original one */
10350 	area = (area_t *)area_mp->b_rptr;
10351 	area_mp->b_cont = NULL;
10352 
10353 	/*
10354 	 * Restore the b_next and b_prev used by mi code. This is needed
10355 	 * to complete the ioctl using mi* functions. We stored them in
10356 	 * the pending mp prior to sending the request to ARP.
10357 	 */
10358 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10359 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10360 	inet_freemsg(pending_mp);
10361 
10362 	/*
10363 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10364 	 * Catch the case where there is an IRE_CACHE by no entry in the
10365 	 * arp table.
10366 	 */
10367 	addr = sin->sin_addr.s_addr;
10368 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10369 		ire_t			*ire;
10370 		dl_unitdata_req_t	*dlup;
10371 		mblk_t			*llmp;
10372 		int			addr_len;
10373 		ill_t			*ipsqill = NULL;
10374 
10375 		if (ifx_arp_ioctl) {
10376 			/*
10377 			 * There's no need to lookup the ill, since
10378 			 * we've already done that when we started
10379 			 * processing the ioctl and sent the message
10380 			 * to ARP on that ill.  So use the ill that
10381 			 * is stored in q->q_ptr.
10382 			 */
10383 			ipsqill = ill;
10384 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10385 			    ipsqill->ill_ipif, ALL_ZONES,
10386 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10387 		} else {
10388 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10389 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10390 			if (ire != NULL)
10391 				ipsqill = ire_to_ill(ire);
10392 		}
10393 
10394 		if ((x_arp_ioctl) && (ipsqill != NULL))
10395 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10396 
10397 		if (ire != NULL) {
10398 			/*
10399 			 * Since the ire obtained from cachetable is used for
10400 			 * mac addr copying below, treat an incomplete ire as if
10401 			 * as if we never found it.
10402 			 */
10403 			if (ire->ire_nce != NULL &&
10404 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10405 				ire_refrele(ire);
10406 				ire = NULL;
10407 				ipsqill = NULL;
10408 				goto errack;
10409 			}
10410 			*flagsp = ATF_INUSE;
10411 			llmp = (ire->ire_nce != NULL ?
10412 			    ire->ire_nce->nce_res_mp : NULL);
10413 			if (llmp != NULL && ipsqill != NULL) {
10414 				uchar_t *macaddr;
10415 
10416 				addr_len = ipsqill->ill_phys_addr_length;
10417 				if (x_arp_ioctl && ((addr_len +
10418 				    ipsqill->ill_name_length) >
10419 				    sizeof (xar->xarp_ha.sdl_data))) {
10420 					ire_refrele(ire);
10421 					freemsg(mp);
10422 					ip_ioctl_finish(q, orig_ioc_mp,
10423 					    EINVAL, NO_COPYOUT, NULL);
10424 					return;
10425 				}
10426 				*flagsp |= ATF_COM;
10427 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10428 				if (ipsqill->ill_sap_length < 0)
10429 					macaddr = llmp->b_rptr +
10430 					    dlup->dl_dest_addr_offset;
10431 				else
10432 					macaddr = llmp->b_rptr +
10433 					    dlup->dl_dest_addr_offset +
10434 					    ipsqill->ill_sap_length;
10435 				/*
10436 				 * For SIOCGARP, MAC address length
10437 				 * validation has already been done
10438 				 * before the ioctl was issued to ARP to
10439 				 * allow it to progress only on 6 byte
10440 				 * addressable (ethernet like) media. Thus
10441 				 * the mac address copying can not overwrite
10442 				 * the sa_data area below.
10443 				 */
10444 				bcopy(macaddr, storage, addr_len);
10445 			}
10446 			/* Ditch the internal IOCTL. */
10447 			freemsg(mp);
10448 			ire_refrele(ire);
10449 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10450 			return;
10451 		}
10452 	}
10453 
10454 	/*
10455 	 * Delete the coresponding IRE_CACHE if any.
10456 	 * Reset the error if there was one (in case there was no entry
10457 	 * in arp.)
10458 	 */
10459 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10460 		ipif_t *ipintf = NULL;
10461 
10462 		if (ifx_arp_ioctl) {
10463 			/*
10464 			 * There's no need to lookup the ill, since
10465 			 * we've already done that when we started
10466 			 * processing the ioctl and sent the message
10467 			 * to ARP on that ill.  So use the ill that
10468 			 * is stored in q->q_ptr.
10469 			 */
10470 			ipintf = ill->ill_ipif;
10471 		}
10472 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10473 			/*
10474 			 * The address in "addr" may be an entry for a
10475 			 * router. If that's true, then any off-net
10476 			 * IRE_CACHE entries that go through the router
10477 			 * with address "addr" must be clobbered. Use
10478 			 * ire_walk to achieve this goal.
10479 			 */
10480 			if (ifx_arp_ioctl)
10481 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10482 				    ire_delete_cache_gw, (char *)&addr, ill);
10483 			else
10484 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10485 				    ALL_ZONES, ipst);
10486 			iocp->ioc_error = 0;
10487 		}
10488 	}
10489 errack:
10490 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10491 		err = iocp->ioc_error;
10492 		freemsg(mp);
10493 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10494 		return;
10495 	}
10496 
10497 	/*
10498 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10499 	 * the area_t into the struct {x}arpreq.
10500 	 */
10501 	if (x_arp_ioctl) {
10502 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10503 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10504 		    sizeof (xar->xarp_ha.sdl_data)) {
10505 			freemsg(mp);
10506 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10507 			    NULL);
10508 			return;
10509 		}
10510 	}
10511 	*flagsp = ATF_INUSE;
10512 	if (area->area_flags & ACE_F_PERMANENT)
10513 		*flagsp |= ATF_PERM;
10514 	if (area->area_flags & ACE_F_PUBLISH)
10515 		*flagsp |= ATF_PUBL;
10516 	if (area->area_flags & ACE_F_AUTHORITY)
10517 		*flagsp |= ATF_AUTHORITY;
10518 	if (area->area_hw_addr_length != 0) {
10519 		*flagsp |= ATF_COM;
10520 		/*
10521 		 * For SIOCGARP, MAC address length validation has
10522 		 * already been done before the ioctl was issued to ARP
10523 		 * to allow it to progress only on 6 byte addressable
10524 		 * (ethernet like) media. Thus the mac address copying
10525 		 * can not overwrite the sa_data area below.
10526 		 */
10527 		bcopy((char *)area + area->area_hw_addr_offset,
10528 		    storage, area->area_hw_addr_length);
10529 	}
10530 
10531 	/* Ditch the internal IOCTL. */
10532 	freemsg(mp);
10533 	/* Complete the original. */
10534 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10535 }
10536 
10537 /*
10538  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10539  * interface) create the next available logical interface for this
10540  * physical interface.
10541  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10542  * ipif with the specified name.
10543  *
10544  * If the address family is not AF_UNSPEC then set the address as well.
10545  *
10546  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10547  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10548  *
10549  * Executed as a writer on the ill or ill group.
10550  * So no lock is needed to traverse the ipif chain, or examine the
10551  * phyint flags.
10552  */
10553 /* ARGSUSED */
10554 int
10555 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10556     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10557 {
10558 	mblk_t	*mp1;
10559 	struct lifreq *lifr;
10560 	boolean_t	isv6;
10561 	boolean_t	exists;
10562 	char 	*name;
10563 	char	*endp;
10564 	char	*cp;
10565 	int	namelen;
10566 	ipif_t	*ipif;
10567 	long	id;
10568 	ipsq_t	*ipsq;
10569 	ill_t	*ill;
10570 	sin_t	*sin;
10571 	int	err = 0;
10572 	boolean_t found_sep = B_FALSE;
10573 	conn_t	*connp;
10574 	zoneid_t zoneid;
10575 	int	orig_ifindex = 0;
10576 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10577 
10578 	ASSERT(q->q_next == NULL);
10579 	ip1dbg(("ip_sioctl_addif\n"));
10580 	/* Existence of mp1 has been checked in ip_wput_nondata */
10581 	mp1 = mp->b_cont->b_cont;
10582 	/*
10583 	 * Null terminate the string to protect against buffer
10584 	 * overrun. String was generated by user code and may not
10585 	 * be trusted.
10586 	 */
10587 	lifr = (struct lifreq *)mp1->b_rptr;
10588 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10589 	name = lifr->lifr_name;
10590 	ASSERT(CONN_Q(q));
10591 	connp = Q_TO_CONN(q);
10592 	isv6 = connp->conn_af_isv6;
10593 	zoneid = connp->conn_zoneid;
10594 	namelen = mi_strlen(name);
10595 	if (namelen == 0)
10596 		return (EINVAL);
10597 
10598 	exists = B_FALSE;
10599 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10600 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10601 		/*
10602 		 * Allow creating lo0 using SIOCLIFADDIF.
10603 		 * can't be any other writer thread. So can pass null below
10604 		 * for the last 4 args to ipif_lookup_name.
10605 		 */
10606 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10607 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10608 		/* Prevent any further action */
10609 		if (ipif == NULL) {
10610 			return (ENOBUFS);
10611 		} else if (!exists) {
10612 			/* We created the ipif now and as writer */
10613 			ipif_refrele(ipif);
10614 			return (0);
10615 		} else {
10616 			ill = ipif->ipif_ill;
10617 			ill_refhold(ill);
10618 			ipif_refrele(ipif);
10619 		}
10620 	} else {
10621 		/* Look for a colon in the name. */
10622 		endp = &name[namelen];
10623 		for (cp = endp; --cp > name; ) {
10624 			if (*cp == IPIF_SEPARATOR_CHAR) {
10625 				found_sep = B_TRUE;
10626 				/*
10627 				 * Reject any non-decimal aliases for plumbing
10628 				 * of logical interfaces. Aliases with leading
10629 				 * zeroes are also rejected as they introduce
10630 				 * ambiguity in the naming of the interfaces.
10631 				 * Comparing with "0" takes care of all such
10632 				 * cases.
10633 				 */
10634 				if ((strncmp("0", cp+1, 1)) == 0)
10635 					return (EINVAL);
10636 
10637 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10638 				    id <= 0 || *endp != '\0') {
10639 					return (EINVAL);
10640 				}
10641 				*cp = '\0';
10642 				break;
10643 			}
10644 		}
10645 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10646 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10647 		if (found_sep)
10648 			*cp = IPIF_SEPARATOR_CHAR;
10649 		if (ill == NULL)
10650 			return (err);
10651 	}
10652 
10653 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10654 	    B_TRUE);
10655 
10656 	/*
10657 	 * Release the refhold due to the lookup, now that we are excl
10658 	 * or we are just returning
10659 	 */
10660 	ill_refrele(ill);
10661 
10662 	if (ipsq == NULL)
10663 		return (EINPROGRESS);
10664 
10665 	/*
10666 	 * If the interface is failed, inactive or offlined, look for a working
10667 	 * interface in the ill group and create the ipif there. If we can't
10668 	 * find a good interface, create the ipif anyway so that in.mpathd can
10669 	 * move it to the first repaired interface.
10670 	 */
10671 	if ((ill->ill_phyint->phyint_flags &
10672 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10673 	    ill->ill_phyint->phyint_groupname_len != 0) {
10674 		phyint_t *phyi;
10675 		char *groupname = ill->ill_phyint->phyint_groupname;
10676 
10677 		/*
10678 		 * We're looking for a working interface, but it doesn't matter
10679 		 * if it's up or down; so instead of following the group lists,
10680 		 * we look at each physical interface and compare the groupname.
10681 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10682 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10683 		 * Otherwise we create the ipif on the failed interface.
10684 		 */
10685 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10686 		phyi = avl_first(&ipst->ips_phyint_g_list->
10687 		    phyint_list_avl_by_index);
10688 		for (; phyi != NULL;
10689 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10690 		    phyint_list_avl_by_index,
10691 		    phyi, AVL_AFTER)) {
10692 			if (phyi->phyint_groupname_len == 0)
10693 				continue;
10694 			ASSERT(phyi->phyint_groupname != NULL);
10695 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10696 			    !(phyi->phyint_flags &
10697 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10698 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10699 			    (phyi->phyint_illv4 != NULL))) {
10700 				break;
10701 			}
10702 		}
10703 		rw_exit(&ipst->ips_ill_g_lock);
10704 
10705 		if (phyi != NULL) {
10706 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10707 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10708 			    phyi->phyint_illv4);
10709 		}
10710 	}
10711 
10712 	/*
10713 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10714 	 * before or after us.
10715 	 */
10716 	ASSERT(IAM_WRITER_ILL(ill));
10717 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10718 
10719 	if (found_sep && orig_ifindex == 0) {
10720 		/* Now see if there is an IPIF with this unit number. */
10721 		for (ipif = ill->ill_ipif; ipif != NULL;
10722 		    ipif = ipif->ipif_next) {
10723 			if (ipif->ipif_id == id) {
10724 				err = EEXIST;
10725 				goto done;
10726 			}
10727 		}
10728 	}
10729 
10730 	/*
10731 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10732 	 * of lo0. We never come here when we plumb lo0:0. It
10733 	 * happens in ipif_lookup_on_name.
10734 	 * The specified unit number is ignored when we create the ipif on a
10735 	 * different interface. However, we save it in ipif_orig_ipifid below so
10736 	 * that the ipif fails back to the right position.
10737 	 */
10738 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10739 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10740 		err = ENOBUFS;
10741 		goto done;
10742 	}
10743 
10744 	/* Return created name with ioctl */
10745 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10746 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10747 	ip1dbg(("created %s\n", lifr->lifr_name));
10748 
10749 	/* Set address */
10750 	sin = (sin_t *)&lifr->lifr_addr;
10751 	if (sin->sin_family != AF_UNSPEC) {
10752 		err = ip_sioctl_addr(ipif, sin, q, mp,
10753 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10754 	}
10755 
10756 	/* Set ifindex and unit number for failback */
10757 	if (err == 0 && orig_ifindex != 0) {
10758 		ipif->ipif_orig_ifindex = orig_ifindex;
10759 		if (found_sep) {
10760 			ipif->ipif_orig_ipifid = id;
10761 		}
10762 	}
10763 
10764 done:
10765 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10766 	return (err);
10767 }
10768 
10769 /*
10770  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10771  * interface) delete it based on the IP address (on this physical interface).
10772  * Otherwise delete it based on the ipif_id.
10773  * Also, special handling to allow a removeif of lo0.
10774  */
10775 /* ARGSUSED */
10776 int
10777 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10778     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10779 {
10780 	conn_t		*connp;
10781 	ill_t		*ill = ipif->ipif_ill;
10782 	boolean_t	 success;
10783 	ip_stack_t	*ipst;
10784 
10785 	ipst = CONNQ_TO_IPST(q);
10786 
10787 	ASSERT(q->q_next == NULL);
10788 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10789 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10790 	ASSERT(IAM_WRITER_IPIF(ipif));
10791 
10792 	connp = Q_TO_CONN(q);
10793 	/*
10794 	 * Special case for unplumbing lo0 (the loopback physical interface).
10795 	 * If unplumbing lo0, the incoming address structure has been
10796 	 * initialized to all zeros. When unplumbing lo0, all its logical
10797 	 * interfaces must be removed too.
10798 	 *
10799 	 * Note that this interface may be called to remove a specific
10800 	 * loopback logical interface (eg, lo0:1). But in that case
10801 	 * ipif->ipif_id != 0 so that the code path for that case is the
10802 	 * same as any other interface (meaning it skips the code directly
10803 	 * below).
10804 	 */
10805 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10806 		if (sin->sin_family == AF_UNSPEC &&
10807 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10808 			/*
10809 			 * Mark it condemned. No new ref. will be made to ill.
10810 			 */
10811 			mutex_enter(&ill->ill_lock);
10812 			ill->ill_state_flags |= ILL_CONDEMNED;
10813 			for (ipif = ill->ill_ipif; ipif != NULL;
10814 			    ipif = ipif->ipif_next) {
10815 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10816 			}
10817 			mutex_exit(&ill->ill_lock);
10818 
10819 			ipif = ill->ill_ipif;
10820 			/* unplumb the loopback interface */
10821 			ill_delete(ill);
10822 			mutex_enter(&connp->conn_lock);
10823 			mutex_enter(&ill->ill_lock);
10824 			ASSERT(ill->ill_group == NULL);
10825 
10826 			/* Are any references to this ill active */
10827 			if (ill_is_quiescent(ill)) {
10828 				mutex_exit(&ill->ill_lock);
10829 				mutex_exit(&connp->conn_lock);
10830 				ill_delete_tail(ill);
10831 				mi_free(ill);
10832 				return (0);
10833 			}
10834 			success = ipsq_pending_mp_add(connp, ipif,
10835 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10836 			mutex_exit(&connp->conn_lock);
10837 			mutex_exit(&ill->ill_lock);
10838 			if (success)
10839 				return (EINPROGRESS);
10840 			else
10841 				return (EINTR);
10842 		}
10843 	}
10844 
10845 	/*
10846 	 * We are exclusive on the ipsq, so an ill move will be serialized
10847 	 * before or after us.
10848 	 */
10849 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10850 
10851 	if (ipif->ipif_id == 0) {
10852 		/* Find based on address */
10853 		if (ipif->ipif_isv6) {
10854 			sin6_t *sin6;
10855 
10856 			if (sin->sin_family != AF_INET6)
10857 				return (EAFNOSUPPORT);
10858 
10859 			sin6 = (sin6_t *)sin;
10860 			/* We are a writer, so we should be able to lookup */
10861 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10862 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10863 			if (ipif == NULL) {
10864 				/*
10865 				 * Maybe the address in on another interface in
10866 				 * the same IPMP group? We check this below.
10867 				 */
10868 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10869 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10870 				    ipst);
10871 			}
10872 		} else {
10873 			ipaddr_t addr;
10874 
10875 			if (sin->sin_family != AF_INET)
10876 				return (EAFNOSUPPORT);
10877 
10878 			addr = sin->sin_addr.s_addr;
10879 			/* We are a writer, so we should be able to lookup */
10880 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10881 			    NULL, NULL, NULL, ipst);
10882 			if (ipif == NULL) {
10883 				/*
10884 				 * Maybe the address in on another interface in
10885 				 * the same IPMP group? We check this below.
10886 				 */
10887 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10888 				    NULL, NULL, NULL, NULL, ipst);
10889 			}
10890 		}
10891 		if (ipif == NULL) {
10892 			return (EADDRNOTAVAIL);
10893 		}
10894 		/*
10895 		 * When the address to be removed is hosted on a different
10896 		 * interface, we check if the interface is in the same IPMP
10897 		 * group as the specified one; if so we proceed with the
10898 		 * removal.
10899 		 * ill->ill_group is NULL when the ill is down, so we have to
10900 		 * compare the group names instead.
10901 		 */
10902 		if (ipif->ipif_ill != ill &&
10903 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10904 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10905 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10906 		    ill->ill_phyint->phyint_groupname) != 0)) {
10907 			ipif_refrele(ipif);
10908 			return (EADDRNOTAVAIL);
10909 		}
10910 
10911 		/* This is a writer */
10912 		ipif_refrele(ipif);
10913 	}
10914 
10915 	/*
10916 	 * Can not delete instance zero since it is tied to the ill.
10917 	 */
10918 	if (ipif->ipif_id == 0)
10919 		return (EBUSY);
10920 
10921 	mutex_enter(&ill->ill_lock);
10922 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10923 	mutex_exit(&ill->ill_lock);
10924 
10925 	ipif_free(ipif);
10926 
10927 	mutex_enter(&connp->conn_lock);
10928 	mutex_enter(&ill->ill_lock);
10929 
10930 	/* Are any references to this ipif active */
10931 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10932 		mutex_exit(&ill->ill_lock);
10933 		mutex_exit(&connp->conn_lock);
10934 		ipif_non_duplicate(ipif);
10935 		ipif_down_tail(ipif);
10936 		ipif_free_tail(ipif);
10937 		return (0);
10938 	}
10939 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10940 	    IPIF_FREE);
10941 	mutex_exit(&ill->ill_lock);
10942 	mutex_exit(&connp->conn_lock);
10943 	if (success)
10944 		return (EINPROGRESS);
10945 	else
10946 		return (EINTR);
10947 }
10948 
10949 /*
10950  * Restart the removeif ioctl. The refcnt has gone down to 0.
10951  * The ipif is already condemned. So can't find it thru lookups.
10952  */
10953 /* ARGSUSED */
10954 int
10955 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10956     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10957 {
10958 	ill_t *ill;
10959 
10960 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10961 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10962 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10963 		ill = ipif->ipif_ill;
10964 		ASSERT(IAM_WRITER_ILL(ill));
10965 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10966 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10967 		ill_delete_tail(ill);
10968 		mi_free(ill);
10969 		return (0);
10970 	}
10971 
10972 	ill = ipif->ipif_ill;
10973 	ASSERT(IAM_WRITER_IPIF(ipif));
10974 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10975 
10976 	ipif_non_duplicate(ipif);
10977 	ipif_down_tail(ipif);
10978 	ipif_free_tail(ipif);
10979 
10980 	ILL_UNMARK_CHANGING(ill);
10981 	return (0);
10982 }
10983 
10984 /*
10985  * Set the local interface address.
10986  * Allow an address of all zero when the interface is down.
10987  */
10988 /* ARGSUSED */
10989 int
10990 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10991     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10992 {
10993 	int err = 0;
10994 	in6_addr_t v6addr;
10995 	boolean_t need_up = B_FALSE;
10996 
10997 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10998 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10999 
11000 	ASSERT(IAM_WRITER_IPIF(ipif));
11001 
11002 	if (ipif->ipif_isv6) {
11003 		sin6_t *sin6;
11004 		ill_t *ill;
11005 		phyint_t *phyi;
11006 
11007 		if (sin->sin_family != AF_INET6)
11008 			return (EAFNOSUPPORT);
11009 
11010 		sin6 = (sin6_t *)sin;
11011 		v6addr = sin6->sin6_addr;
11012 		ill = ipif->ipif_ill;
11013 		phyi = ill->ill_phyint;
11014 
11015 		/*
11016 		 * Enforce that true multicast interfaces have a link-local
11017 		 * address for logical unit 0.
11018 		 */
11019 		if (ipif->ipif_id == 0 &&
11020 		    (ill->ill_flags & ILLF_MULTICAST) &&
11021 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11022 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11023 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11024 			return (EADDRNOTAVAIL);
11025 		}
11026 
11027 		/*
11028 		 * up interfaces shouldn't have the unspecified address
11029 		 * unless they also have the IPIF_NOLOCAL flags set and
11030 		 * have a subnet assigned.
11031 		 */
11032 		if ((ipif->ipif_flags & IPIF_UP) &&
11033 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11034 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11035 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11036 			return (EADDRNOTAVAIL);
11037 		}
11038 
11039 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11040 			return (EADDRNOTAVAIL);
11041 	} else {
11042 		ipaddr_t addr;
11043 
11044 		if (sin->sin_family != AF_INET)
11045 			return (EAFNOSUPPORT);
11046 
11047 		addr = sin->sin_addr.s_addr;
11048 
11049 		/* Allow 0 as the local address. */
11050 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11051 			return (EADDRNOTAVAIL);
11052 
11053 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11054 	}
11055 
11056 
11057 	/*
11058 	 * Even if there is no change we redo things just to rerun
11059 	 * ipif_set_default.
11060 	 */
11061 	if (ipif->ipif_flags & IPIF_UP) {
11062 		/*
11063 		 * Setting a new local address, make sure
11064 		 * we have net and subnet bcast ire's for
11065 		 * the old address if we need them.
11066 		 */
11067 		if (!ipif->ipif_isv6)
11068 			ipif_check_bcast_ires(ipif);
11069 		/*
11070 		 * If the interface is already marked up,
11071 		 * we call ipif_down which will take care
11072 		 * of ditching any IREs that have been set
11073 		 * up based on the old interface address.
11074 		 */
11075 		err = ipif_logical_down(ipif, q, mp);
11076 		if (err == EINPROGRESS)
11077 			return (err);
11078 		ipif_down_tail(ipif);
11079 		need_up = 1;
11080 	}
11081 
11082 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11083 	return (err);
11084 }
11085 
11086 int
11087 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11088     boolean_t need_up)
11089 {
11090 	in6_addr_t v6addr;
11091 	in6_addr_t ov6addr;
11092 	ipaddr_t addr;
11093 	sin6_t	*sin6;
11094 	int	sinlen;
11095 	int	err = 0;
11096 	ill_t	*ill = ipif->ipif_ill;
11097 	boolean_t need_dl_down;
11098 	boolean_t need_arp_down;
11099 	struct iocblk *iocp;
11100 
11101 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11102 
11103 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11104 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11105 	ASSERT(IAM_WRITER_IPIF(ipif));
11106 
11107 	/* Must cancel any pending timer before taking the ill_lock */
11108 	if (ipif->ipif_recovery_id != 0)
11109 		(void) untimeout(ipif->ipif_recovery_id);
11110 	ipif->ipif_recovery_id = 0;
11111 
11112 	if (ipif->ipif_isv6) {
11113 		sin6 = (sin6_t *)sin;
11114 		v6addr = sin6->sin6_addr;
11115 		sinlen = sizeof (struct sockaddr_in6);
11116 	} else {
11117 		addr = sin->sin_addr.s_addr;
11118 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11119 		sinlen = sizeof (struct sockaddr_in);
11120 	}
11121 	mutex_enter(&ill->ill_lock);
11122 	ov6addr = ipif->ipif_v6lcl_addr;
11123 	ipif->ipif_v6lcl_addr = v6addr;
11124 	sctp_update_ipif_addr(ipif, ov6addr);
11125 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11126 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11127 	} else {
11128 		ipif->ipif_v6src_addr = v6addr;
11129 	}
11130 	ipif->ipif_addr_ready = 0;
11131 
11132 	/*
11133 	 * If the interface was previously marked as a duplicate, then since
11134 	 * we've now got a "new" address, it should no longer be considered a
11135 	 * duplicate -- even if the "new" address is the same as the old one.
11136 	 * Note that if all ipifs are down, we may have a pending ARP down
11137 	 * event to handle.  This is because we want to recover from duplicates
11138 	 * and thus delay tearing down ARP until the duplicates have been
11139 	 * removed or disabled.
11140 	 */
11141 	need_dl_down = need_arp_down = B_FALSE;
11142 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11143 		need_arp_down = !need_up;
11144 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11145 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11146 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11147 			need_dl_down = B_TRUE;
11148 		}
11149 	}
11150 
11151 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11152 	    !ill->ill_is_6to4tun) {
11153 		queue_t *wqp = ill->ill_wq;
11154 
11155 		/*
11156 		 * The local address of this interface is a 6to4 address,
11157 		 * check if this interface is in fact a 6to4 tunnel or just
11158 		 * an interface configured with a 6to4 address.  We are only
11159 		 * interested in the former.
11160 		 */
11161 		if (wqp != NULL) {
11162 			while ((wqp->q_next != NULL) &&
11163 			    (wqp->q_next->q_qinfo != NULL) &&
11164 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11165 
11166 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11167 				    == TUN6TO4_MODID) {
11168 					/* set for use in IP */
11169 					ill->ill_is_6to4tun = 1;
11170 					break;
11171 				}
11172 				wqp = wqp->q_next;
11173 			}
11174 		}
11175 	}
11176 
11177 	ipif_set_default(ipif);
11178 
11179 	/*
11180 	 * When publishing an interface address change event, we only notify
11181 	 * the event listeners of the new address.  It is assumed that if they
11182 	 * actively care about the addresses assigned that they will have
11183 	 * already discovered the previous address assigned (if there was one.)
11184 	 *
11185 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11186 	 */
11187 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11188 		hook_nic_event_t *info;
11189 		if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) {
11190 			ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d "
11191 			    "attached for %s\n", info->hne_event,
11192 			    ill->ill_name));
11193 			if (info->hne_data != NULL)
11194 				kmem_free(info->hne_data, info->hne_datalen);
11195 			kmem_free(info, sizeof (hook_nic_event_t));
11196 		}
11197 
11198 		info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
11199 		if (info != NULL) {
11200 			ip_stack_t	*ipst = ill->ill_ipst;
11201 
11202 			info->hne_nic =
11203 			    ipif->ipif_ill->ill_phyint->phyint_hook_ifindex;
11204 			info->hne_lif = MAP_IPIF_ID(ipif->ipif_id);
11205 			info->hne_event = NE_ADDRESS_CHANGE;
11206 			info->hne_family = ipif->ipif_isv6 ?
11207 			    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
11208 			info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP);
11209 			if (info->hne_data != NULL) {
11210 				info->hne_datalen = sinlen;
11211 				bcopy(sin, info->hne_data, sinlen);
11212 			} else {
11213 				ip2dbg(("ip_sioctl_addr_tail: could not attach "
11214 				    "address information for ADDRESS_CHANGE nic"
11215 				    " event of %s (ENOMEM)\n",
11216 				    ipif->ipif_ill->ill_name));
11217 				kmem_free(info, sizeof (hook_nic_event_t));
11218 			}
11219 		} else
11220 			ip2dbg(("ip_sioctl_addr_tail: could not attach "
11221 			    "ADDRESS_CHANGE nic event information for %s "
11222 			    "(ENOMEM)\n", ipif->ipif_ill->ill_name));
11223 
11224 		ipif->ipif_ill->ill_nic_event_info = info;
11225 	}
11226 
11227 	mutex_exit(&ill->ill_lock);
11228 
11229 	if (need_up) {
11230 		/*
11231 		 * Now bring the interface back up.  If this
11232 		 * is the only IPIF for the ILL, ipif_up
11233 		 * will have to re-bind to the device, so
11234 		 * we may get back EINPROGRESS, in which
11235 		 * case, this IOCTL will get completed in
11236 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11237 		 */
11238 		err = ipif_up(ipif, q, mp);
11239 	}
11240 
11241 	if (need_dl_down)
11242 		ill_dl_down(ill);
11243 	if (need_arp_down)
11244 		ipif_arp_down(ipif);
11245 
11246 	return (err);
11247 }
11248 
11249 
11250 /*
11251  * Restart entry point to restart the address set operation after the
11252  * refcounts have dropped to zero.
11253  */
11254 /* ARGSUSED */
11255 int
11256 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11257     ip_ioctl_cmd_t *ipip, void *ifreq)
11258 {
11259 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11260 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11261 	ASSERT(IAM_WRITER_IPIF(ipif));
11262 	ipif_down_tail(ipif);
11263 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11264 }
11265 
11266 /* ARGSUSED */
11267 int
11268 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11269     ip_ioctl_cmd_t *ipip, void *if_req)
11270 {
11271 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11272 	struct lifreq *lifr = (struct lifreq *)if_req;
11273 
11274 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11275 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11276 	/*
11277 	 * The net mask and address can't change since we have a
11278 	 * reference to the ipif. So no lock is necessary.
11279 	 */
11280 	if (ipif->ipif_isv6) {
11281 		*sin6 = sin6_null;
11282 		sin6->sin6_family = AF_INET6;
11283 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11284 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11285 		lifr->lifr_addrlen =
11286 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11287 	} else {
11288 		*sin = sin_null;
11289 		sin->sin_family = AF_INET;
11290 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11291 		if (ipip->ipi_cmd_type == LIF_CMD) {
11292 			lifr->lifr_addrlen =
11293 			    ip_mask_to_plen(ipif->ipif_net_mask);
11294 		}
11295 	}
11296 	return (0);
11297 }
11298 
11299 /*
11300  * Set the destination address for a pt-pt interface.
11301  */
11302 /* ARGSUSED */
11303 int
11304 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11305     ip_ioctl_cmd_t *ipip, void *if_req)
11306 {
11307 	int err = 0;
11308 	in6_addr_t v6addr;
11309 	boolean_t need_up = B_FALSE;
11310 
11311 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11312 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11313 	ASSERT(IAM_WRITER_IPIF(ipif));
11314 
11315 	if (ipif->ipif_isv6) {
11316 		sin6_t *sin6;
11317 
11318 		if (sin->sin_family != AF_INET6)
11319 			return (EAFNOSUPPORT);
11320 
11321 		sin6 = (sin6_t *)sin;
11322 		v6addr = sin6->sin6_addr;
11323 
11324 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11325 			return (EADDRNOTAVAIL);
11326 	} else {
11327 		ipaddr_t addr;
11328 
11329 		if (sin->sin_family != AF_INET)
11330 			return (EAFNOSUPPORT);
11331 
11332 		addr = sin->sin_addr.s_addr;
11333 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11334 			return (EADDRNOTAVAIL);
11335 
11336 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11337 	}
11338 
11339 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11340 		return (0);	/* No change */
11341 
11342 	if (ipif->ipif_flags & IPIF_UP) {
11343 		/*
11344 		 * If the interface is already marked up,
11345 		 * we call ipif_down which will take care
11346 		 * of ditching any IREs that have been set
11347 		 * up based on the old pp dst address.
11348 		 */
11349 		err = ipif_logical_down(ipif, q, mp);
11350 		if (err == EINPROGRESS)
11351 			return (err);
11352 		ipif_down_tail(ipif);
11353 		need_up = B_TRUE;
11354 	}
11355 	/*
11356 	 * could return EINPROGRESS. If so ioctl will complete in
11357 	 * ip_rput_dlpi_writer
11358 	 */
11359 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11360 	return (err);
11361 }
11362 
11363 static int
11364 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11365     boolean_t need_up)
11366 {
11367 	in6_addr_t v6addr;
11368 	ill_t	*ill = ipif->ipif_ill;
11369 	int	err = 0;
11370 	boolean_t need_dl_down;
11371 	boolean_t need_arp_down;
11372 
11373 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11374 	    ipif->ipif_id, (void *)ipif));
11375 
11376 	/* Must cancel any pending timer before taking the ill_lock */
11377 	if (ipif->ipif_recovery_id != 0)
11378 		(void) untimeout(ipif->ipif_recovery_id);
11379 	ipif->ipif_recovery_id = 0;
11380 
11381 	if (ipif->ipif_isv6) {
11382 		sin6_t *sin6;
11383 
11384 		sin6 = (sin6_t *)sin;
11385 		v6addr = sin6->sin6_addr;
11386 	} else {
11387 		ipaddr_t addr;
11388 
11389 		addr = sin->sin_addr.s_addr;
11390 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11391 	}
11392 	mutex_enter(&ill->ill_lock);
11393 	/* Set point to point destination address. */
11394 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11395 		/*
11396 		 * Allow this as a means of creating logical
11397 		 * pt-pt interfaces on top of e.g. an Ethernet.
11398 		 * XXX Undocumented HACK for testing.
11399 		 * pt-pt interfaces are created with NUD disabled.
11400 		 */
11401 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11402 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11403 		if (ipif->ipif_isv6)
11404 			ill->ill_flags |= ILLF_NONUD;
11405 	}
11406 
11407 	/*
11408 	 * If the interface was previously marked as a duplicate, then since
11409 	 * we've now got a "new" address, it should no longer be considered a
11410 	 * duplicate -- even if the "new" address is the same as the old one.
11411 	 * Note that if all ipifs are down, we may have a pending ARP down
11412 	 * event to handle.
11413 	 */
11414 	need_dl_down = need_arp_down = B_FALSE;
11415 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11416 		need_arp_down = !need_up;
11417 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11418 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11419 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11420 			need_dl_down = B_TRUE;
11421 		}
11422 	}
11423 
11424 	/* Set the new address. */
11425 	ipif->ipif_v6pp_dst_addr = v6addr;
11426 	/* Make sure subnet tracks pp_dst */
11427 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11428 	mutex_exit(&ill->ill_lock);
11429 
11430 	if (need_up) {
11431 		/*
11432 		 * Now bring the interface back up.  If this
11433 		 * is the only IPIF for the ILL, ipif_up
11434 		 * will have to re-bind to the device, so
11435 		 * we may get back EINPROGRESS, in which
11436 		 * case, this IOCTL will get completed in
11437 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11438 		 */
11439 		err = ipif_up(ipif, q, mp);
11440 	}
11441 
11442 	if (need_dl_down)
11443 		ill_dl_down(ill);
11444 
11445 	if (need_arp_down)
11446 		ipif_arp_down(ipif);
11447 	return (err);
11448 }
11449 
11450 /*
11451  * Restart entry point to restart the dstaddress set operation after the
11452  * refcounts have dropped to zero.
11453  */
11454 /* ARGSUSED */
11455 int
11456 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11457     ip_ioctl_cmd_t *ipip, void *ifreq)
11458 {
11459 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11460 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11461 	ipif_down_tail(ipif);
11462 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11463 }
11464 
11465 /* ARGSUSED */
11466 int
11467 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11468     ip_ioctl_cmd_t *ipip, void *if_req)
11469 {
11470 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11471 
11472 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11473 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11474 	/*
11475 	 * Get point to point destination address. The addresses can't
11476 	 * change since we hold a reference to the ipif.
11477 	 */
11478 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11479 		return (EADDRNOTAVAIL);
11480 
11481 	if (ipif->ipif_isv6) {
11482 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11483 		*sin6 = sin6_null;
11484 		sin6->sin6_family = AF_INET6;
11485 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11486 	} else {
11487 		*sin = sin_null;
11488 		sin->sin_family = AF_INET;
11489 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11490 	}
11491 	return (0);
11492 }
11493 
11494 /*
11495  * part of ipmp, make this func return the active/inactive state and
11496  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11497  */
11498 /*
11499  * This function either sets or clears the IFF_INACTIVE flag.
11500  *
11501  * As long as there are some addresses or multicast memberships on the
11502  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11503  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11504  * will be used for outbound packets.
11505  *
11506  * Caller needs to verify the validity of setting IFF_INACTIVE.
11507  */
11508 static void
11509 phyint_inactive(phyint_t *phyi)
11510 {
11511 	ill_t *ill_v4;
11512 	ill_t *ill_v6;
11513 	ipif_t *ipif;
11514 	ilm_t *ilm;
11515 
11516 	ill_v4 = phyi->phyint_illv4;
11517 	ill_v6 = phyi->phyint_illv6;
11518 
11519 	/*
11520 	 * No need for a lock while traversing the list since iam
11521 	 * a writer
11522 	 */
11523 	if (ill_v4 != NULL) {
11524 		ASSERT(IAM_WRITER_ILL(ill_v4));
11525 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11526 		    ipif = ipif->ipif_next) {
11527 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11528 				mutex_enter(&phyi->phyint_lock);
11529 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11530 				mutex_exit(&phyi->phyint_lock);
11531 				return;
11532 			}
11533 		}
11534 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11535 		    ilm = ilm->ilm_next) {
11536 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11537 				mutex_enter(&phyi->phyint_lock);
11538 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11539 				mutex_exit(&phyi->phyint_lock);
11540 				return;
11541 			}
11542 		}
11543 	}
11544 	if (ill_v6 != NULL) {
11545 		ill_v6 = phyi->phyint_illv6;
11546 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11547 		    ipif = ipif->ipif_next) {
11548 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11549 				mutex_enter(&phyi->phyint_lock);
11550 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11551 				mutex_exit(&phyi->phyint_lock);
11552 				return;
11553 			}
11554 		}
11555 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11556 		    ilm = ilm->ilm_next) {
11557 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11558 				mutex_enter(&phyi->phyint_lock);
11559 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11560 				mutex_exit(&phyi->phyint_lock);
11561 				return;
11562 			}
11563 		}
11564 	}
11565 	mutex_enter(&phyi->phyint_lock);
11566 	phyi->phyint_flags |= PHYI_INACTIVE;
11567 	mutex_exit(&phyi->phyint_lock);
11568 }
11569 
11570 /*
11571  * This function is called only when the phyint flags change. Currently
11572  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11573  * that we can select a good ill.
11574  */
11575 static void
11576 ip_redo_nomination(phyint_t *phyi)
11577 {
11578 	ill_t *ill_v4;
11579 
11580 	ill_v4 = phyi->phyint_illv4;
11581 
11582 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11583 		ASSERT(IAM_WRITER_ILL(ill_v4));
11584 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11585 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11586 	}
11587 }
11588 
11589 /*
11590  * Heuristic to check if ill is INACTIVE.
11591  * Checks if ill has an ipif with an usable ip address.
11592  *
11593  * Return values:
11594  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11595  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11596  */
11597 static boolean_t
11598 ill_is_inactive(ill_t *ill)
11599 {
11600 	ipif_t *ipif;
11601 
11602 	/* Check whether it is in an IPMP group */
11603 	if (ill->ill_phyint->phyint_groupname == NULL)
11604 		return (B_FALSE);
11605 
11606 	if (ill->ill_ipif_up_count == 0)
11607 		return (B_TRUE);
11608 
11609 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11610 		uint64_t flags = ipif->ipif_flags;
11611 
11612 		/*
11613 		 * This ipif is usable if it is IPIF_UP and not a
11614 		 * dedicated test address.  A dedicated test address
11615 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11616 		 * (note in particular that V6 test addresses are
11617 		 * link-local data addresses and thus are marked
11618 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11619 		 */
11620 		if ((flags & IPIF_UP) &&
11621 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11622 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11623 			return (B_FALSE);
11624 	}
11625 	return (B_TRUE);
11626 }
11627 
11628 /*
11629  * Set interface flags.
11630  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11631  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11632  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11633  *
11634  * NOTE : We really don't enforce that ipif_id zero should be used
11635  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11636  *	  is because applications generally does SICGLIFFLAGS and
11637  *	  ORs in the new flags (that affects the logical) and does a
11638  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11639  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11640  *	  flags that will be turned on is correct with respect to
11641  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11642  */
11643 /* ARGSUSED */
11644 int
11645 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11646     ip_ioctl_cmd_t *ipip, void *if_req)
11647 {
11648 	uint64_t turn_on;
11649 	uint64_t turn_off;
11650 	int	err;
11651 	boolean_t need_up = B_FALSE;
11652 	phyint_t *phyi;
11653 	ill_t *ill;
11654 	uint64_t intf_flags;
11655 	boolean_t phyint_flags_modified = B_FALSE;
11656 	uint64_t flags;
11657 	struct ifreq *ifr;
11658 	struct lifreq *lifr;
11659 	boolean_t set_linklocal = B_FALSE;
11660 	boolean_t zero_source = B_FALSE;
11661 	ip_stack_t *ipst;
11662 
11663 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11664 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11665 
11666 	ASSERT(IAM_WRITER_IPIF(ipif));
11667 
11668 	ill = ipif->ipif_ill;
11669 	phyi = ill->ill_phyint;
11670 	ipst = ill->ill_ipst;
11671 
11672 	if (ipip->ipi_cmd_type == IF_CMD) {
11673 		ifr = (struct ifreq *)if_req;
11674 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11675 	} else {
11676 		lifr = (struct lifreq *)if_req;
11677 		flags = lifr->lifr_flags;
11678 	}
11679 
11680 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11681 
11682 	/*
11683 	 * Has the flags been set correctly till now ?
11684 	 */
11685 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11686 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11687 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11688 	/*
11689 	 * Compare the new flags to the old, and partition
11690 	 * into those coming on and those going off.
11691 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11692 	 */
11693 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11694 		flags |= intf_flags & ~0xFFFF;
11695 
11696 	/*
11697 	 * First check which bits will change and then which will
11698 	 * go on and off
11699 	 */
11700 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11701 	if (!turn_on)
11702 		return (0);	/* No change */
11703 
11704 	turn_off = intf_flags & turn_on;
11705 	turn_on ^= turn_off;
11706 	err = 0;
11707 
11708 	/*
11709 	 * Don't allow any bits belonging to the logical interface
11710 	 * to be set or cleared on the replacement ipif that was
11711 	 * created temporarily during a MOVE.
11712 	 */
11713 	if (ipif->ipif_replace_zero &&
11714 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11715 		return (EINVAL);
11716 	}
11717 
11718 	/*
11719 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11720 	 * IPv6 interfaces.
11721 	 */
11722 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11723 		return (EINVAL);
11724 
11725 	/*
11726 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11727 	 */
11728 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11729 		return (EINVAL);
11730 
11731 	/*
11732 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11733 	 * interfaces.  It makes no sense in that context.
11734 	 */
11735 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11736 		return (EINVAL);
11737 
11738 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11739 		zero_source = B_TRUE;
11740 
11741 	/*
11742 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11743 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11744 	 * If the link local address isn't set, and can be set, it will get
11745 	 * set later on in this function.
11746 	 */
11747 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11748 	    (flags & IFF_UP) && !zero_source &&
11749 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11750 		if (ipif_cant_setlinklocal(ipif))
11751 			return (EINVAL);
11752 		set_linklocal = B_TRUE;
11753 	}
11754 
11755 	/*
11756 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11757 	 * same time. No need to grab ill_g_usesrc_lock here, see
11758 	 * synchronization notes in ip.c
11759 	 */
11760 	if (turn_on & PHYI_STANDBY &&
11761 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11762 		return (EINVAL);
11763 	}
11764 
11765 	/*
11766 	 * If we modify physical interface flags, we'll potentially need to
11767 	 * send up two routing socket messages for the changes (one for the
11768 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11769 	 */
11770 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11771 		phyint_flags_modified = B_TRUE;
11772 
11773 	/*
11774 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11775 	 * we need to flush the IRE_CACHES belonging to this ill.
11776 	 * We handle this case here without doing the DOWN/UP dance
11777 	 * like it is done for other flags. If some other flags are
11778 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11779 	 * below will handle it by bringing it down and then
11780 	 * bringing it UP.
11781 	 */
11782 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11783 		ill_t *ill_v4, *ill_v6;
11784 
11785 		ill_v4 = phyi->phyint_illv4;
11786 		ill_v6 = phyi->phyint_illv6;
11787 
11788 		/*
11789 		 * First set the INACTIVE flag if needed. Then delete the ires.
11790 		 * ire_add will atomically prevent creating new IRE_CACHEs
11791 		 * unless hidden flag is set.
11792 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11793 		 */
11794 		if ((turn_on & PHYI_FAILED) &&
11795 		    ((intf_flags & PHYI_STANDBY) ||
11796 		    !ipst->ips_ipmp_enable_failback)) {
11797 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11798 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11799 		}
11800 		if ((turn_off & PHYI_FAILED) &&
11801 		    ((intf_flags & PHYI_STANDBY) ||
11802 		    (!ipst->ips_ipmp_enable_failback &&
11803 		    ill_is_inactive(ill)))) {
11804 			phyint_inactive(phyi);
11805 		}
11806 
11807 		if (turn_on & PHYI_STANDBY) {
11808 			/*
11809 			 * We implicitly set INACTIVE only when STANDBY is set.
11810 			 * INACTIVE is also set on non-STANDBY phyint when user
11811 			 * disables FAILBACK using configuration file.
11812 			 * Do not allow STANDBY to be set on such INACTIVE
11813 			 * phyint
11814 			 */
11815 			if (phyi->phyint_flags & PHYI_INACTIVE)
11816 				return (EINVAL);
11817 			if (!(phyi->phyint_flags & PHYI_FAILED))
11818 				phyint_inactive(phyi);
11819 		}
11820 		if (turn_off & PHYI_STANDBY) {
11821 			if (ipst->ips_ipmp_enable_failback) {
11822 				/*
11823 				 * Reset PHYI_INACTIVE.
11824 				 */
11825 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11826 			} else if (ill_is_inactive(ill) &&
11827 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11828 				/*
11829 				 * Need to set INACTIVE, when user sets
11830 				 * STANDBY on a non-STANDBY phyint and
11831 				 * later resets STANDBY
11832 				 */
11833 				phyint_inactive(phyi);
11834 			}
11835 		}
11836 		/*
11837 		 * We should always send up a message so that the
11838 		 * daemons come to know of it. Note that the zeroth
11839 		 * interface can be down and the check below for IPIF_UP
11840 		 * will not make sense as we are actually setting
11841 		 * a phyint flag here. We assume that the ipif used
11842 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11843 		 * send up any message for non-zero ipifs).
11844 		 */
11845 		phyint_flags_modified = B_TRUE;
11846 
11847 		if (ill_v4 != NULL) {
11848 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11849 			    IRE_CACHE, ill_stq_cache_delete,
11850 			    (char *)ill_v4, ill_v4);
11851 			illgrp_reset_schednext(ill_v4);
11852 		}
11853 		if (ill_v6 != NULL) {
11854 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11855 			    IRE_CACHE, ill_stq_cache_delete,
11856 			    (char *)ill_v6, ill_v6);
11857 			illgrp_reset_schednext(ill_v6);
11858 		}
11859 	}
11860 
11861 	/*
11862 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11863 	 * status of the interface and, if the interface is part of an IPMP
11864 	 * group, all other interfaces that are part of the same IPMP
11865 	 * group.
11866 	 */
11867 	if ((turn_on | turn_off) & ILLF_ROUTER)
11868 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11869 
11870 	/*
11871 	 * If the interface is not UP and we are not going to
11872 	 * bring it UP, record the flags and return. When the
11873 	 * interface comes UP later, the right actions will be
11874 	 * taken.
11875 	 */
11876 	if (!(ipif->ipif_flags & IPIF_UP) &&
11877 	    !(turn_on & IPIF_UP)) {
11878 		/* Record new flags in their respective places. */
11879 		mutex_enter(&ill->ill_lock);
11880 		mutex_enter(&ill->ill_phyint->phyint_lock);
11881 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11882 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11883 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11884 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11885 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11886 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11887 		mutex_exit(&ill->ill_lock);
11888 		mutex_exit(&ill->ill_phyint->phyint_lock);
11889 
11890 		/*
11891 		 * We do the broadcast and nomination here rather
11892 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11893 		 * the case of FAILBACK from INACTIVE standby to the
11894 		 * interface that has been repaired, PHYI_FAILED has not
11895 		 * been cleared yet. If there are only two interfaces in
11896 		 * that group, all we have is a FAILED and INACTIVE
11897 		 * interface. If we do the nomination soon after a failback,
11898 		 * the broadcast nomination code would select the
11899 		 * INACTIVE interface for receiving broadcasts as FAILED is
11900 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11901 		 * receive broadcast packets, we need to redo nomination
11902 		 * when the FAILED is cleared here. Thus, in general we
11903 		 * always do the nomination here for FAILED, STANDBY
11904 		 * and OFFLINE.
11905 		 */
11906 		if (((turn_on | turn_off) &
11907 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11908 			ip_redo_nomination(phyi);
11909 		}
11910 		if (phyint_flags_modified) {
11911 			if (phyi->phyint_illv4 != NULL) {
11912 				ip_rts_ifmsg(phyi->phyint_illv4->
11913 				    ill_ipif);
11914 			}
11915 			if (phyi->phyint_illv6 != NULL) {
11916 				ip_rts_ifmsg(phyi->phyint_illv6->
11917 				    ill_ipif);
11918 			}
11919 		}
11920 		return (0);
11921 	} else if (set_linklocal || zero_source) {
11922 		mutex_enter(&ill->ill_lock);
11923 		if (set_linklocal)
11924 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11925 		if (zero_source)
11926 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11927 		mutex_exit(&ill->ill_lock);
11928 	}
11929 
11930 	/*
11931 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11932 	 * or point-to-point interfaces with an unspecified destination. We do
11933 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11934 	 * have a subnet assigned, which is how in.ndpd currently manages its
11935 	 * onlink prefix list when no addresses are configured with those
11936 	 * prefixes.
11937 	 */
11938 	if (ipif->ipif_isv6 &&
11939 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11940 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11941 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11942 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11943 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11944 		return (EINVAL);
11945 	}
11946 
11947 	/*
11948 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11949 	 * from being brought up.
11950 	 */
11951 	if (!ipif->ipif_isv6 &&
11952 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11953 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11954 		return (EINVAL);
11955 	}
11956 
11957 	/*
11958 	 * The only flag changes that we currently take specific action on
11959 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11960 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11961 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11962 	 * the flags and bringing it back up again.
11963 	 */
11964 	if ((turn_on|turn_off) &
11965 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11966 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11967 		/*
11968 		 * Taking this ipif down, make sure we have
11969 		 * valid net and subnet bcast ire's for other
11970 		 * logical interfaces, if we need them.
11971 		 */
11972 		if (!ipif->ipif_isv6)
11973 			ipif_check_bcast_ires(ipif);
11974 
11975 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11976 		    !(turn_off & IPIF_UP)) {
11977 			need_up = B_TRUE;
11978 			if (ipif->ipif_flags & IPIF_UP)
11979 				ill->ill_logical_down = 1;
11980 			turn_on &= ~IPIF_UP;
11981 		}
11982 		err = ipif_down(ipif, q, mp);
11983 		ip1dbg(("ipif_down returns %d err ", err));
11984 		if (err == EINPROGRESS)
11985 			return (err);
11986 		ipif_down_tail(ipif);
11987 	}
11988 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11989 }
11990 
11991 static int
11992 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11993     boolean_t need_up)
11994 {
11995 	ill_t	*ill;
11996 	phyint_t *phyi;
11997 	uint64_t turn_on;
11998 	uint64_t turn_off;
11999 	uint64_t intf_flags;
12000 	boolean_t phyint_flags_modified = B_FALSE;
12001 	int	err = 0;
12002 	boolean_t set_linklocal = B_FALSE;
12003 	boolean_t zero_source = B_FALSE;
12004 
12005 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12006 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12007 
12008 	ASSERT(IAM_WRITER_IPIF(ipif));
12009 
12010 	ill = ipif->ipif_ill;
12011 	phyi = ill->ill_phyint;
12012 
12013 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12014 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12015 
12016 	turn_off = intf_flags & turn_on;
12017 	turn_on ^= turn_off;
12018 
12019 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12020 		phyint_flags_modified = B_TRUE;
12021 
12022 	/*
12023 	 * Now we change the flags. Track current value of
12024 	 * other flags in their respective places.
12025 	 */
12026 	mutex_enter(&ill->ill_lock);
12027 	mutex_enter(&phyi->phyint_lock);
12028 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12029 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12030 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12031 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12032 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12033 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12034 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12035 		set_linklocal = B_TRUE;
12036 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12037 	}
12038 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12039 		zero_source = B_TRUE;
12040 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12041 	}
12042 	mutex_exit(&ill->ill_lock);
12043 	mutex_exit(&phyi->phyint_lock);
12044 
12045 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12046 		ip_redo_nomination(phyi);
12047 
12048 	if (set_linklocal)
12049 		(void) ipif_setlinklocal(ipif);
12050 
12051 	if (zero_source)
12052 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12053 	else
12054 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12055 
12056 	if (need_up) {
12057 		/*
12058 		 * XXX ipif_up really does not know whether a phyint flags
12059 		 * was modified or not. So, it sends up information on
12060 		 * only one routing sockets message. As we don't bring up
12061 		 * the interface and also set STANDBY/FAILED simultaneously
12062 		 * it should be okay.
12063 		 */
12064 		err = ipif_up(ipif, q, mp);
12065 	} else {
12066 		/*
12067 		 * Make sure routing socket sees all changes to the flags.
12068 		 * ipif_up_done* handles this when we use ipif_up.
12069 		 */
12070 		if (phyint_flags_modified) {
12071 			if (phyi->phyint_illv4 != NULL) {
12072 				ip_rts_ifmsg(phyi->phyint_illv4->
12073 				    ill_ipif);
12074 			}
12075 			if (phyi->phyint_illv6 != NULL) {
12076 				ip_rts_ifmsg(phyi->phyint_illv6->
12077 				    ill_ipif);
12078 			}
12079 		} else {
12080 			ip_rts_ifmsg(ipif);
12081 		}
12082 		/*
12083 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12084 		 * this in need_up case.
12085 		 */
12086 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12087 	}
12088 	return (err);
12089 }
12090 
12091 /*
12092  * Restart entry point to restart the flags restart operation after the
12093  * refcounts have dropped to zero.
12094  */
12095 /* ARGSUSED */
12096 int
12097 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12098     ip_ioctl_cmd_t *ipip, void *if_req)
12099 {
12100 	int	err;
12101 	struct ifreq *ifr = (struct ifreq *)if_req;
12102 	struct lifreq *lifr = (struct lifreq *)if_req;
12103 
12104 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12105 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12106 
12107 	ipif_down_tail(ipif);
12108 	if (ipip->ipi_cmd_type == IF_CMD) {
12109 		/*
12110 		 * Since ip_sioctl_flags expects an int and ifr_flags
12111 		 * is a short we need to cast ifr_flags into an int
12112 		 * to avoid having sign extension cause bits to get
12113 		 * set that should not be.
12114 		 */
12115 		err = ip_sioctl_flags_tail(ipif,
12116 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12117 		    q, mp, B_TRUE);
12118 	} else {
12119 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12120 		    q, mp, B_TRUE);
12121 	}
12122 	return (err);
12123 }
12124 
12125 /*
12126  * Can operate on either a module or a driver queue.
12127  */
12128 /* ARGSUSED */
12129 int
12130 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12131     ip_ioctl_cmd_t *ipip, void *if_req)
12132 {
12133 	/*
12134 	 * Has the flags been set correctly till now ?
12135 	 */
12136 	ill_t *ill = ipif->ipif_ill;
12137 	phyint_t *phyi = ill->ill_phyint;
12138 
12139 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12140 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12141 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12142 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12143 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12144 
12145 	/*
12146 	 * Need a lock since some flags can be set even when there are
12147 	 * references to the ipif.
12148 	 */
12149 	mutex_enter(&ill->ill_lock);
12150 	if (ipip->ipi_cmd_type == IF_CMD) {
12151 		struct ifreq *ifr = (struct ifreq *)if_req;
12152 
12153 		/* Get interface flags (low 16 only). */
12154 		ifr->ifr_flags = ((ipif->ipif_flags |
12155 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12156 	} else {
12157 		struct lifreq *lifr = (struct lifreq *)if_req;
12158 
12159 		/* Get interface flags. */
12160 		lifr->lifr_flags = ipif->ipif_flags |
12161 		    ill->ill_flags | phyi->phyint_flags;
12162 	}
12163 	mutex_exit(&ill->ill_lock);
12164 	return (0);
12165 }
12166 
12167 /* ARGSUSED */
12168 int
12169 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12170     ip_ioctl_cmd_t *ipip, void *if_req)
12171 {
12172 	int mtu;
12173 	int ip_min_mtu;
12174 	struct ifreq	*ifr;
12175 	struct lifreq *lifr;
12176 	ire_t	*ire;
12177 	ip_stack_t *ipst;
12178 
12179 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12180 	    ipif->ipif_id, (void *)ipif));
12181 	if (ipip->ipi_cmd_type == IF_CMD) {
12182 		ifr = (struct ifreq *)if_req;
12183 		mtu = ifr->ifr_metric;
12184 	} else {
12185 		lifr = (struct lifreq *)if_req;
12186 		mtu = lifr->lifr_mtu;
12187 	}
12188 
12189 	if (ipif->ipif_isv6)
12190 		ip_min_mtu = IPV6_MIN_MTU;
12191 	else
12192 		ip_min_mtu = IP_MIN_MTU;
12193 
12194 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12195 		return (EINVAL);
12196 
12197 	/*
12198 	 * Change the MTU size in all relevant ire's.
12199 	 * Mtu change Vs. new ire creation - protocol below.
12200 	 * First change ipif_mtu and the ire_max_frag of the
12201 	 * interface ire. Then do an ire walk and change the
12202 	 * ire_max_frag of all affected ires. During ire_add
12203 	 * under the bucket lock, set the ire_max_frag of the
12204 	 * new ire being created from the ipif/ire from which
12205 	 * it is being derived. If an mtu change happens after
12206 	 * the ire is added, the new ire will be cleaned up.
12207 	 * Conversely if the mtu change happens before the ire
12208 	 * is added, ire_add will see the new value of the mtu.
12209 	 */
12210 	ipif->ipif_mtu = mtu;
12211 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12212 
12213 	if (ipif->ipif_isv6)
12214 		ire = ipif_to_ire_v6(ipif);
12215 	else
12216 		ire = ipif_to_ire(ipif);
12217 	if (ire != NULL) {
12218 		ire->ire_max_frag = ipif->ipif_mtu;
12219 		ire_refrele(ire);
12220 	}
12221 	ipst = ipif->ipif_ill->ill_ipst;
12222 	if (ipif->ipif_flags & IPIF_UP) {
12223 		if (ipif->ipif_isv6)
12224 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12225 			    ipst);
12226 		else
12227 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12228 			    ipst);
12229 	}
12230 	/* Update the MTU in SCTP's list */
12231 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12232 	return (0);
12233 }
12234 
12235 /* Get interface MTU. */
12236 /* ARGSUSED */
12237 int
12238 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12239 	ip_ioctl_cmd_t *ipip, void *if_req)
12240 {
12241 	struct ifreq	*ifr;
12242 	struct lifreq	*lifr;
12243 
12244 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12245 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12246 	if (ipip->ipi_cmd_type == IF_CMD) {
12247 		ifr = (struct ifreq *)if_req;
12248 		ifr->ifr_metric = ipif->ipif_mtu;
12249 	} else {
12250 		lifr = (struct lifreq *)if_req;
12251 		lifr->lifr_mtu = ipif->ipif_mtu;
12252 	}
12253 	return (0);
12254 }
12255 
12256 /* Set interface broadcast address. */
12257 /* ARGSUSED2 */
12258 int
12259 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12260 	ip_ioctl_cmd_t *ipip, void *if_req)
12261 {
12262 	ipaddr_t addr;
12263 	ire_t	*ire;
12264 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12265 
12266 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12267 	    ipif->ipif_id));
12268 
12269 	ASSERT(IAM_WRITER_IPIF(ipif));
12270 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12271 		return (EADDRNOTAVAIL);
12272 
12273 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12274 
12275 	if (sin->sin_family != AF_INET)
12276 		return (EAFNOSUPPORT);
12277 
12278 	addr = sin->sin_addr.s_addr;
12279 	if (ipif->ipif_flags & IPIF_UP) {
12280 		/*
12281 		 * If we are already up, make sure the new
12282 		 * broadcast address makes sense.  If it does,
12283 		 * there should be an IRE for it already.
12284 		 * Don't match on ipif, only on the ill
12285 		 * since we are sharing these now. Don't use
12286 		 * MATCH_IRE_ILL_GROUP as we are looking for
12287 		 * the broadcast ire on this ill and each ill
12288 		 * in the group has its own broadcast ire.
12289 		 */
12290 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12291 		    ipif, ALL_ZONES, NULL,
12292 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12293 		if (ire == NULL) {
12294 			return (EINVAL);
12295 		} else {
12296 			ire_refrele(ire);
12297 		}
12298 	}
12299 	/*
12300 	 * Changing the broadcast addr for this ipif.
12301 	 * Make sure we have valid net and subnet bcast
12302 	 * ire's for other logical interfaces, if needed.
12303 	 */
12304 	if (addr != ipif->ipif_brd_addr)
12305 		ipif_check_bcast_ires(ipif);
12306 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12307 	return (0);
12308 }
12309 
12310 /* Get interface broadcast address. */
12311 /* ARGSUSED */
12312 int
12313 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12314     ip_ioctl_cmd_t *ipip, void *if_req)
12315 {
12316 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12317 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12318 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12319 		return (EADDRNOTAVAIL);
12320 
12321 	/* IPIF_BROADCAST not possible with IPv6 */
12322 	ASSERT(!ipif->ipif_isv6);
12323 	*sin = sin_null;
12324 	sin->sin_family = AF_INET;
12325 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12326 	return (0);
12327 }
12328 
12329 /*
12330  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12331  */
12332 /* ARGSUSED */
12333 int
12334 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12335     ip_ioctl_cmd_t *ipip, void *if_req)
12336 {
12337 	int err = 0;
12338 	in6_addr_t v6mask;
12339 
12340 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12341 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12342 
12343 	ASSERT(IAM_WRITER_IPIF(ipif));
12344 
12345 	if (ipif->ipif_isv6) {
12346 		sin6_t *sin6;
12347 
12348 		if (sin->sin_family != AF_INET6)
12349 			return (EAFNOSUPPORT);
12350 
12351 		sin6 = (sin6_t *)sin;
12352 		v6mask = sin6->sin6_addr;
12353 	} else {
12354 		ipaddr_t mask;
12355 
12356 		if (sin->sin_family != AF_INET)
12357 			return (EAFNOSUPPORT);
12358 
12359 		mask = sin->sin_addr.s_addr;
12360 		V4MASK_TO_V6(mask, v6mask);
12361 	}
12362 
12363 	/*
12364 	 * No big deal if the interface isn't already up, or the mask
12365 	 * isn't really changing, or this is pt-pt.
12366 	 */
12367 	if (!(ipif->ipif_flags & IPIF_UP) ||
12368 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12369 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12370 		ipif->ipif_v6net_mask = v6mask;
12371 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12372 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12373 			    ipif->ipif_v6net_mask,
12374 			    ipif->ipif_v6subnet);
12375 		}
12376 		return (0);
12377 	}
12378 	/*
12379 	 * Make sure we have valid net and subnet broadcast ire's
12380 	 * for the old netmask, if needed by other logical interfaces.
12381 	 */
12382 	if (!ipif->ipif_isv6)
12383 		ipif_check_bcast_ires(ipif);
12384 
12385 	err = ipif_logical_down(ipif, q, mp);
12386 	if (err == EINPROGRESS)
12387 		return (err);
12388 	ipif_down_tail(ipif);
12389 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12390 	return (err);
12391 }
12392 
12393 static int
12394 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12395 {
12396 	in6_addr_t v6mask;
12397 	int err = 0;
12398 
12399 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12400 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12401 
12402 	if (ipif->ipif_isv6) {
12403 		sin6_t *sin6;
12404 
12405 		sin6 = (sin6_t *)sin;
12406 		v6mask = sin6->sin6_addr;
12407 	} else {
12408 		ipaddr_t mask;
12409 
12410 		mask = sin->sin_addr.s_addr;
12411 		V4MASK_TO_V6(mask, v6mask);
12412 	}
12413 
12414 	ipif->ipif_v6net_mask = v6mask;
12415 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12416 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12417 		    ipif->ipif_v6subnet);
12418 	}
12419 	err = ipif_up(ipif, q, mp);
12420 
12421 	if (err == 0 || err == EINPROGRESS) {
12422 		/*
12423 		 * The interface must be DL_BOUND if this packet has to
12424 		 * go out on the wire. Since we only go through a logical
12425 		 * down and are bound with the driver during an internal
12426 		 * down/up that is satisfied.
12427 		 */
12428 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12429 			/* Potentially broadcast an address mask reply. */
12430 			ipif_mask_reply(ipif);
12431 		}
12432 	}
12433 	return (err);
12434 }
12435 
12436 /* ARGSUSED */
12437 int
12438 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12439     ip_ioctl_cmd_t *ipip, void *if_req)
12440 {
12441 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12442 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12443 	ipif_down_tail(ipif);
12444 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12445 }
12446 
12447 /* Get interface net mask. */
12448 /* ARGSUSED */
12449 int
12450 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12451     ip_ioctl_cmd_t *ipip, void *if_req)
12452 {
12453 	struct lifreq *lifr = (struct lifreq *)if_req;
12454 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12455 
12456 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12457 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12458 
12459 	/*
12460 	 * net mask can't change since we have a reference to the ipif.
12461 	 */
12462 	if (ipif->ipif_isv6) {
12463 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12464 		*sin6 = sin6_null;
12465 		sin6->sin6_family = AF_INET6;
12466 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12467 		lifr->lifr_addrlen =
12468 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12469 	} else {
12470 		*sin = sin_null;
12471 		sin->sin_family = AF_INET;
12472 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12473 		if (ipip->ipi_cmd_type == LIF_CMD) {
12474 			lifr->lifr_addrlen =
12475 			    ip_mask_to_plen(ipif->ipif_net_mask);
12476 		}
12477 	}
12478 	return (0);
12479 }
12480 
12481 /* ARGSUSED */
12482 int
12483 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12484     ip_ioctl_cmd_t *ipip, void *if_req)
12485 {
12486 
12487 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12488 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12489 	/*
12490 	 * Set interface metric.  We don't use this for
12491 	 * anything but we keep track of it in case it is
12492 	 * important to routing applications or such.
12493 	 */
12494 	if (ipip->ipi_cmd_type == IF_CMD) {
12495 		struct ifreq    *ifr;
12496 
12497 		ifr = (struct ifreq *)if_req;
12498 		ipif->ipif_metric = ifr->ifr_metric;
12499 	} else {
12500 		struct lifreq   *lifr;
12501 
12502 		lifr = (struct lifreq *)if_req;
12503 		ipif->ipif_metric = lifr->lifr_metric;
12504 	}
12505 	return (0);
12506 }
12507 
12508 
12509 /* ARGSUSED */
12510 int
12511 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12512     ip_ioctl_cmd_t *ipip, void *if_req)
12513 {
12514 
12515 	/* Get interface metric. */
12516 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12517 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12518 	if (ipip->ipi_cmd_type == IF_CMD) {
12519 		struct ifreq    *ifr;
12520 
12521 		ifr = (struct ifreq *)if_req;
12522 		ifr->ifr_metric = ipif->ipif_metric;
12523 	} else {
12524 		struct lifreq   *lifr;
12525 
12526 		lifr = (struct lifreq *)if_req;
12527 		lifr->lifr_metric = ipif->ipif_metric;
12528 	}
12529 
12530 	return (0);
12531 }
12532 
12533 /* ARGSUSED */
12534 int
12535 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12536     ip_ioctl_cmd_t *ipip, void *if_req)
12537 {
12538 
12539 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12540 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12541 	/*
12542 	 * Set the muxid returned from I_PLINK.
12543 	 */
12544 	if (ipip->ipi_cmd_type == IF_CMD) {
12545 		struct ifreq *ifr = (struct ifreq *)if_req;
12546 
12547 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12548 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12549 	} else {
12550 		struct lifreq *lifr = (struct lifreq *)if_req;
12551 
12552 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12553 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12554 	}
12555 	return (0);
12556 }
12557 
12558 /* ARGSUSED */
12559 int
12560 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12561     ip_ioctl_cmd_t *ipip, void *if_req)
12562 {
12563 
12564 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12565 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12566 	/*
12567 	 * Get the muxid saved in ill for I_PUNLINK.
12568 	 */
12569 	if (ipip->ipi_cmd_type == IF_CMD) {
12570 		struct ifreq *ifr = (struct ifreq *)if_req;
12571 
12572 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12573 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12574 	} else {
12575 		struct lifreq *lifr = (struct lifreq *)if_req;
12576 
12577 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12578 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12579 	}
12580 	return (0);
12581 }
12582 
12583 /*
12584  * Set the subnet prefix. Does not modify the broadcast address.
12585  */
12586 /* ARGSUSED */
12587 int
12588 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12589     ip_ioctl_cmd_t *ipip, void *if_req)
12590 {
12591 	int err = 0;
12592 	in6_addr_t v6addr;
12593 	in6_addr_t v6mask;
12594 	boolean_t need_up = B_FALSE;
12595 	int addrlen;
12596 
12597 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12598 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12599 
12600 	ASSERT(IAM_WRITER_IPIF(ipif));
12601 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12602 
12603 	if (ipif->ipif_isv6) {
12604 		sin6_t *sin6;
12605 
12606 		if (sin->sin_family != AF_INET6)
12607 			return (EAFNOSUPPORT);
12608 
12609 		sin6 = (sin6_t *)sin;
12610 		v6addr = sin6->sin6_addr;
12611 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12612 			return (EADDRNOTAVAIL);
12613 	} else {
12614 		ipaddr_t addr;
12615 
12616 		if (sin->sin_family != AF_INET)
12617 			return (EAFNOSUPPORT);
12618 
12619 		addr = sin->sin_addr.s_addr;
12620 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12621 			return (EADDRNOTAVAIL);
12622 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12623 		/* Add 96 bits */
12624 		addrlen += IPV6_ABITS - IP_ABITS;
12625 	}
12626 
12627 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12628 		return (EINVAL);
12629 
12630 	/* Check if bits in the address is set past the mask */
12631 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12632 		return (EINVAL);
12633 
12634 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12635 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12636 		return (0);	/* No change */
12637 
12638 	if (ipif->ipif_flags & IPIF_UP) {
12639 		/*
12640 		 * If the interface is already marked up,
12641 		 * we call ipif_down which will take care
12642 		 * of ditching any IREs that have been set
12643 		 * up based on the old interface address.
12644 		 */
12645 		err = ipif_logical_down(ipif, q, mp);
12646 		if (err == EINPROGRESS)
12647 			return (err);
12648 		ipif_down_tail(ipif);
12649 		need_up = B_TRUE;
12650 	}
12651 
12652 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12653 	return (err);
12654 }
12655 
12656 static int
12657 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12658     queue_t *q, mblk_t *mp, boolean_t need_up)
12659 {
12660 	ill_t	*ill = ipif->ipif_ill;
12661 	int	err = 0;
12662 
12663 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12664 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12665 
12666 	/* Set the new address. */
12667 	mutex_enter(&ill->ill_lock);
12668 	ipif->ipif_v6net_mask = v6mask;
12669 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12670 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12671 		    ipif->ipif_v6subnet);
12672 	}
12673 	mutex_exit(&ill->ill_lock);
12674 
12675 	if (need_up) {
12676 		/*
12677 		 * Now bring the interface back up.  If this
12678 		 * is the only IPIF for the ILL, ipif_up
12679 		 * will have to re-bind to the device, so
12680 		 * we may get back EINPROGRESS, in which
12681 		 * case, this IOCTL will get completed in
12682 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12683 		 */
12684 		err = ipif_up(ipif, q, mp);
12685 		if (err == EINPROGRESS)
12686 			return (err);
12687 	}
12688 	return (err);
12689 }
12690 
12691 /* ARGSUSED */
12692 int
12693 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12694     ip_ioctl_cmd_t *ipip, void *if_req)
12695 {
12696 	int	addrlen;
12697 	in6_addr_t v6addr;
12698 	in6_addr_t v6mask;
12699 	struct lifreq *lifr = (struct lifreq *)if_req;
12700 
12701 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12702 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12703 	ipif_down_tail(ipif);
12704 
12705 	addrlen = lifr->lifr_addrlen;
12706 	if (ipif->ipif_isv6) {
12707 		sin6_t *sin6;
12708 
12709 		sin6 = (sin6_t *)sin;
12710 		v6addr = sin6->sin6_addr;
12711 	} else {
12712 		ipaddr_t addr;
12713 
12714 		addr = sin->sin_addr.s_addr;
12715 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12716 		addrlen += IPV6_ABITS - IP_ABITS;
12717 	}
12718 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12719 
12720 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12721 }
12722 
12723 /* ARGSUSED */
12724 int
12725 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12726     ip_ioctl_cmd_t *ipip, void *if_req)
12727 {
12728 	struct lifreq *lifr = (struct lifreq *)if_req;
12729 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12730 
12731 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12732 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12733 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12734 
12735 	if (ipif->ipif_isv6) {
12736 		*sin6 = sin6_null;
12737 		sin6->sin6_family = AF_INET6;
12738 		sin6->sin6_addr = ipif->ipif_v6subnet;
12739 		lifr->lifr_addrlen =
12740 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12741 	} else {
12742 		*sin = sin_null;
12743 		sin->sin_family = AF_INET;
12744 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12745 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12746 	}
12747 	return (0);
12748 }
12749 
12750 /*
12751  * Set the IPv6 address token.
12752  */
12753 /* ARGSUSED */
12754 int
12755 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12756     ip_ioctl_cmd_t *ipi, void *if_req)
12757 {
12758 	ill_t *ill = ipif->ipif_ill;
12759 	int err;
12760 	in6_addr_t v6addr;
12761 	in6_addr_t v6mask;
12762 	boolean_t need_up = B_FALSE;
12763 	int i;
12764 	sin6_t *sin6 = (sin6_t *)sin;
12765 	struct lifreq *lifr = (struct lifreq *)if_req;
12766 	int addrlen;
12767 
12768 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12769 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12770 	ASSERT(IAM_WRITER_IPIF(ipif));
12771 
12772 	addrlen = lifr->lifr_addrlen;
12773 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12774 	if (ipif->ipif_id != 0)
12775 		return (EINVAL);
12776 
12777 	if (!ipif->ipif_isv6)
12778 		return (EINVAL);
12779 
12780 	if (addrlen > IPV6_ABITS)
12781 		return (EINVAL);
12782 
12783 	v6addr = sin6->sin6_addr;
12784 
12785 	/*
12786 	 * The length of the token is the length from the end.  To get
12787 	 * the proper mask for this, compute the mask of the bits not
12788 	 * in the token; ie. the prefix, and then xor to get the mask.
12789 	 */
12790 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12791 		return (EINVAL);
12792 	for (i = 0; i < 4; i++) {
12793 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12794 	}
12795 
12796 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12797 	    ill->ill_token_length == addrlen)
12798 		return (0);	/* No change */
12799 
12800 	if (ipif->ipif_flags & IPIF_UP) {
12801 		err = ipif_logical_down(ipif, q, mp);
12802 		if (err == EINPROGRESS)
12803 			return (err);
12804 		ipif_down_tail(ipif);
12805 		need_up = B_TRUE;
12806 	}
12807 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12808 	return (err);
12809 }
12810 
12811 static int
12812 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12813     mblk_t *mp, boolean_t need_up)
12814 {
12815 	in6_addr_t v6addr;
12816 	in6_addr_t v6mask;
12817 	ill_t	*ill = ipif->ipif_ill;
12818 	int	i;
12819 	int	err = 0;
12820 
12821 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12822 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12823 	v6addr = sin6->sin6_addr;
12824 	/*
12825 	 * The length of the token is the length from the end.  To get
12826 	 * the proper mask for this, compute the mask of the bits not
12827 	 * in the token; ie. the prefix, and then xor to get the mask.
12828 	 */
12829 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12830 	for (i = 0; i < 4; i++)
12831 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12832 
12833 	mutex_enter(&ill->ill_lock);
12834 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12835 	ill->ill_token_length = addrlen;
12836 	mutex_exit(&ill->ill_lock);
12837 
12838 	if (need_up) {
12839 		/*
12840 		 * Now bring the interface back up.  If this
12841 		 * is the only IPIF for the ILL, ipif_up
12842 		 * will have to re-bind to the device, so
12843 		 * we may get back EINPROGRESS, in which
12844 		 * case, this IOCTL will get completed in
12845 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12846 		 */
12847 		err = ipif_up(ipif, q, mp);
12848 		if (err == EINPROGRESS)
12849 			return (err);
12850 	}
12851 	return (err);
12852 }
12853 
12854 /* ARGSUSED */
12855 int
12856 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12857     ip_ioctl_cmd_t *ipi, void *if_req)
12858 {
12859 	ill_t *ill;
12860 	sin6_t *sin6 = (sin6_t *)sin;
12861 	struct lifreq *lifr = (struct lifreq *)if_req;
12862 
12863 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12864 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12865 	if (ipif->ipif_id != 0)
12866 		return (EINVAL);
12867 
12868 	ill = ipif->ipif_ill;
12869 	if (!ill->ill_isv6)
12870 		return (ENXIO);
12871 
12872 	*sin6 = sin6_null;
12873 	sin6->sin6_family = AF_INET6;
12874 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12875 	sin6->sin6_addr = ill->ill_token;
12876 	lifr->lifr_addrlen = ill->ill_token_length;
12877 	return (0);
12878 }
12879 
12880 /*
12881  * Set (hardware) link specific information that might override
12882  * what was acquired through the DL_INFO_ACK.
12883  * The logic is as follows.
12884  *
12885  * become exclusive
12886  * set CHANGING flag
12887  * change mtu on affected IREs
12888  * clear CHANGING flag
12889  *
12890  * An ire add that occurs before the CHANGING flag is set will have its mtu
12891  * changed by the ip_sioctl_lnkinfo.
12892  *
12893  * During the time the CHANGING flag is set, no new ires will be added to the
12894  * bucket, and ire add will fail (due the CHANGING flag).
12895  *
12896  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12897  * before it is added to the bucket.
12898  *
12899  * Obviously only 1 thread can set the CHANGING flag and we need to become
12900  * exclusive to set the flag.
12901  */
12902 /* ARGSUSED */
12903 int
12904 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12905     ip_ioctl_cmd_t *ipi, void *if_req)
12906 {
12907 	ill_t		*ill = ipif->ipif_ill;
12908 	ipif_t		*nipif;
12909 	int		ip_min_mtu;
12910 	boolean_t	mtu_walk = B_FALSE;
12911 	struct lifreq	*lifr = (struct lifreq *)if_req;
12912 	lif_ifinfo_req_t *lir;
12913 	ire_t		*ire;
12914 
12915 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12916 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12917 	lir = &lifr->lifr_ifinfo;
12918 	ASSERT(IAM_WRITER_IPIF(ipif));
12919 
12920 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12921 	if (ipif->ipif_id != 0)
12922 		return (EINVAL);
12923 
12924 	/* Set interface MTU. */
12925 	if (ipif->ipif_isv6)
12926 		ip_min_mtu = IPV6_MIN_MTU;
12927 	else
12928 		ip_min_mtu = IP_MIN_MTU;
12929 
12930 	/*
12931 	 * Verify values before we set anything. Allow zero to
12932 	 * mean unspecified.
12933 	 */
12934 	if (lir->lir_maxmtu != 0 &&
12935 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12936 	    lir->lir_maxmtu < ip_min_mtu))
12937 		return (EINVAL);
12938 	if (lir->lir_reachtime != 0 &&
12939 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12940 		return (EINVAL);
12941 	if (lir->lir_reachretrans != 0 &&
12942 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12943 		return (EINVAL);
12944 
12945 	mutex_enter(&ill->ill_lock);
12946 	ill->ill_state_flags |= ILL_CHANGING;
12947 	for (nipif = ill->ill_ipif; nipif != NULL;
12948 	    nipif = nipif->ipif_next) {
12949 		nipif->ipif_state_flags |= IPIF_CHANGING;
12950 	}
12951 
12952 	mutex_exit(&ill->ill_lock);
12953 
12954 	if (lir->lir_maxmtu != 0) {
12955 		ill->ill_max_mtu = lir->lir_maxmtu;
12956 		ill->ill_mtu_userspecified = 1;
12957 		mtu_walk = B_TRUE;
12958 	}
12959 
12960 	if (lir->lir_reachtime != 0)
12961 		ill->ill_reachable_time = lir->lir_reachtime;
12962 
12963 	if (lir->lir_reachretrans != 0)
12964 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12965 
12966 	ill->ill_max_hops = lir->lir_maxhops;
12967 
12968 	ill->ill_max_buf = ND_MAX_Q;
12969 
12970 	if (mtu_walk) {
12971 		/*
12972 		 * Set the MTU on all ipifs associated with this ill except
12973 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12974 		 */
12975 		for (nipif = ill->ill_ipif; nipif != NULL;
12976 		    nipif = nipif->ipif_next) {
12977 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12978 				continue;
12979 
12980 			nipif->ipif_mtu = ill->ill_max_mtu;
12981 
12982 			if (!(nipif->ipif_flags & IPIF_UP))
12983 				continue;
12984 
12985 			if (nipif->ipif_isv6)
12986 				ire = ipif_to_ire_v6(nipif);
12987 			else
12988 				ire = ipif_to_ire(nipif);
12989 			if (ire != NULL) {
12990 				ire->ire_max_frag = ipif->ipif_mtu;
12991 				ire_refrele(ire);
12992 			}
12993 			if (ill->ill_isv6) {
12994 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12995 				    ipif_mtu_change, (char *)nipif,
12996 				    ill);
12997 			} else {
12998 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12999 				    ipif_mtu_change, (char *)nipif,
13000 				    ill);
13001 			}
13002 		}
13003 	}
13004 
13005 	mutex_enter(&ill->ill_lock);
13006 	for (nipif = ill->ill_ipif; nipif != NULL;
13007 	    nipif = nipif->ipif_next) {
13008 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13009 	}
13010 	ILL_UNMARK_CHANGING(ill);
13011 	mutex_exit(&ill->ill_lock);
13012 
13013 	return (0);
13014 }
13015 
13016 /* ARGSUSED */
13017 int
13018 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13019     ip_ioctl_cmd_t *ipi, void *if_req)
13020 {
13021 	struct lif_ifinfo_req *lir;
13022 	ill_t *ill = ipif->ipif_ill;
13023 
13024 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13025 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13026 	if (ipif->ipif_id != 0)
13027 		return (EINVAL);
13028 
13029 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13030 	lir->lir_maxhops = ill->ill_max_hops;
13031 	lir->lir_reachtime = ill->ill_reachable_time;
13032 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13033 	lir->lir_maxmtu = ill->ill_max_mtu;
13034 
13035 	return (0);
13036 }
13037 
13038 /*
13039  * Return best guess as to the subnet mask for the specified address.
13040  * Based on the subnet masks for all the configured interfaces.
13041  *
13042  * We end up returning a zero mask in the case of default, multicast or
13043  * experimental.
13044  */
13045 static ipaddr_t
13046 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13047 {
13048 	ipaddr_t net_mask;
13049 	ill_t	*ill;
13050 	ipif_t	*ipif;
13051 	ill_walk_context_t ctx;
13052 	ipif_t	*fallback_ipif = NULL;
13053 
13054 	net_mask = ip_net_mask(addr);
13055 	if (net_mask == 0) {
13056 		*ipifp = NULL;
13057 		return (0);
13058 	}
13059 
13060 	/* Let's check to see if this is maybe a local subnet route. */
13061 	/* this function only applies to IPv4 interfaces */
13062 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13063 	ill = ILL_START_WALK_V4(&ctx, ipst);
13064 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13065 		mutex_enter(&ill->ill_lock);
13066 		for (ipif = ill->ill_ipif; ipif != NULL;
13067 		    ipif = ipif->ipif_next) {
13068 			if (!IPIF_CAN_LOOKUP(ipif))
13069 				continue;
13070 			if (!(ipif->ipif_flags & IPIF_UP))
13071 				continue;
13072 			if ((ipif->ipif_subnet & net_mask) ==
13073 			    (addr & net_mask)) {
13074 				/*
13075 				 * Don't trust pt-pt interfaces if there are
13076 				 * other interfaces.
13077 				 */
13078 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13079 					if (fallback_ipif == NULL) {
13080 						ipif_refhold_locked(ipif);
13081 						fallback_ipif = ipif;
13082 					}
13083 					continue;
13084 				}
13085 
13086 				/*
13087 				 * Fine. Just assume the same net mask as the
13088 				 * directly attached subnet interface is using.
13089 				 */
13090 				ipif_refhold_locked(ipif);
13091 				mutex_exit(&ill->ill_lock);
13092 				rw_exit(&ipst->ips_ill_g_lock);
13093 				if (fallback_ipif != NULL)
13094 					ipif_refrele(fallback_ipif);
13095 				*ipifp = ipif;
13096 				return (ipif->ipif_net_mask);
13097 			}
13098 		}
13099 		mutex_exit(&ill->ill_lock);
13100 	}
13101 	rw_exit(&ipst->ips_ill_g_lock);
13102 
13103 	*ipifp = fallback_ipif;
13104 	return ((fallback_ipif != NULL) ?
13105 	    fallback_ipif->ipif_net_mask : net_mask);
13106 }
13107 
13108 /*
13109  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13110  */
13111 static void
13112 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13113 {
13114 	IOCP	iocp;
13115 	ipft_t	*ipft;
13116 	ipllc_t	*ipllc;
13117 	mblk_t	*mp1;
13118 	cred_t	*cr;
13119 	int	error = 0;
13120 	conn_t	*connp;
13121 
13122 	ip1dbg(("ip_wput_ioctl"));
13123 	iocp = (IOCP)mp->b_rptr;
13124 	mp1 = mp->b_cont;
13125 	if (mp1 == NULL) {
13126 		iocp->ioc_error = EINVAL;
13127 		mp->b_datap->db_type = M_IOCNAK;
13128 		iocp->ioc_count = 0;
13129 		qreply(q, mp);
13130 		return;
13131 	}
13132 
13133 	/*
13134 	 * These IOCTLs provide various control capabilities to
13135 	 * upstream agents such as ULPs and processes.	There
13136 	 * are currently two such IOCTLs implemented.  They
13137 	 * are used by TCP to provide update information for
13138 	 * existing IREs and to forcibly delete an IRE for a
13139 	 * host that is not responding, thereby forcing an
13140 	 * attempt at a new route.
13141 	 */
13142 	iocp->ioc_error = EINVAL;
13143 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13144 		goto done;
13145 
13146 	ipllc = (ipllc_t *)mp1->b_rptr;
13147 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13148 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13149 			break;
13150 	}
13151 	/*
13152 	 * prefer credential from mblk over ioctl;
13153 	 * see ip_sioctl_copyin_setup
13154 	 */
13155 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13156 
13157 	/*
13158 	 * Refhold the conn in case the request gets queued up in some lookup
13159 	 */
13160 	ASSERT(CONN_Q(q));
13161 	connp = Q_TO_CONN(q);
13162 	CONN_INC_REF(connp);
13163 	if (ipft->ipft_pfi &&
13164 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13165 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13166 		error = (*ipft->ipft_pfi)(q,
13167 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13168 	}
13169 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13170 		/*
13171 		 * CONN_OPER_PENDING_DONE happens in the function called
13172 		 * through ipft_pfi above.
13173 		 */
13174 		return;
13175 	}
13176 
13177 	CONN_OPER_PENDING_DONE(connp);
13178 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13179 		freemsg(mp);
13180 		return;
13181 	}
13182 	iocp->ioc_error = error;
13183 
13184 done:
13185 	mp->b_datap->db_type = M_IOCACK;
13186 	if (iocp->ioc_error)
13187 		iocp->ioc_count = 0;
13188 	qreply(q, mp);
13189 }
13190 
13191 /*
13192  * Lookup an ipif using the sequence id (ipif_seqid)
13193  */
13194 ipif_t *
13195 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13196 {
13197 	ipif_t *ipif;
13198 
13199 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13200 
13201 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13202 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13203 			return (ipif);
13204 	}
13205 	return (NULL);
13206 }
13207 
13208 /*
13209  * Assign a unique id for the ipif. This is used later when we send
13210  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13211  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13212  * IRE is added, we verify that ipif has not disappeared.
13213  */
13214 
13215 static void
13216 ipif_assign_seqid(ipif_t *ipif)
13217 {
13218 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13219 
13220 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13221 }
13222 
13223 /*
13224  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13225  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13226  * be inserted into the first space available in the list. The value of
13227  * ipif_id will then be set to the appropriate value for its position.
13228  */
13229 static int
13230 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13231 {
13232 	ill_t *ill;
13233 	ipif_t *tipif;
13234 	ipif_t **tipifp;
13235 	int id;
13236 	ip_stack_t	*ipst;
13237 
13238 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13239 	    IAM_WRITER_IPIF(ipif));
13240 
13241 	ill = ipif->ipif_ill;
13242 	ASSERT(ill != NULL);
13243 	ipst = ill->ill_ipst;
13244 
13245 	/*
13246 	 * In the case of lo0:0 we already hold the ill_g_lock.
13247 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13248 	 * ipif_insert. Another such caller is ipif_move.
13249 	 */
13250 	if (acquire_g_lock)
13251 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13252 	if (acquire_ill_lock)
13253 		mutex_enter(&ill->ill_lock);
13254 	id = ipif->ipif_id;
13255 	tipifp = &(ill->ill_ipif);
13256 	if (id == -1) {	/* need to find a real id */
13257 		id = 0;
13258 		while ((tipif = *tipifp) != NULL) {
13259 			ASSERT(tipif->ipif_id >= id);
13260 			if (tipif->ipif_id != id)
13261 				break; /* non-consecutive id */
13262 			id++;
13263 			tipifp = &(tipif->ipif_next);
13264 		}
13265 		/* limit number of logical interfaces */
13266 		if (id >= ipst->ips_ip_addrs_per_if) {
13267 			if (acquire_ill_lock)
13268 				mutex_exit(&ill->ill_lock);
13269 			if (acquire_g_lock)
13270 				rw_exit(&ipst->ips_ill_g_lock);
13271 			return (-1);
13272 		}
13273 		ipif->ipif_id = id; /* assign new id */
13274 	} else if (id < ipst->ips_ip_addrs_per_if) {
13275 		/* we have a real id; insert ipif in the right place */
13276 		while ((tipif = *tipifp) != NULL) {
13277 			ASSERT(tipif->ipif_id != id);
13278 			if (tipif->ipif_id > id)
13279 				break; /* found correct location */
13280 			tipifp = &(tipif->ipif_next);
13281 		}
13282 	} else {
13283 		if (acquire_ill_lock)
13284 			mutex_exit(&ill->ill_lock);
13285 		if (acquire_g_lock)
13286 			rw_exit(&ipst->ips_ill_g_lock);
13287 		return (-1);
13288 	}
13289 
13290 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13291 
13292 	ipif->ipif_next = tipif;
13293 	*tipifp = ipif;
13294 	if (acquire_ill_lock)
13295 		mutex_exit(&ill->ill_lock);
13296 	if (acquire_g_lock)
13297 		rw_exit(&ipst->ips_ill_g_lock);
13298 	return (0);
13299 }
13300 
13301 static void
13302 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13303 {
13304 	ipif_t	**ipifp;
13305 	ill_t	*ill = ipif->ipif_ill;
13306 
13307 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13308 	if (acquire_ill_lock)
13309 		mutex_enter(&ill->ill_lock);
13310 	else
13311 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13312 
13313 	ipifp = &ill->ill_ipif;
13314 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13315 		if (*ipifp == ipif) {
13316 			*ipifp = ipif->ipif_next;
13317 			break;
13318 		}
13319 	}
13320 
13321 	if (acquire_ill_lock)
13322 		mutex_exit(&ill->ill_lock);
13323 }
13324 
13325 /*
13326  * Allocate and initialize a new interface control structure.  (Always
13327  * called as writer.)
13328  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13329  * is not part of the global linked list of ills. ipif_seqid is unique
13330  * in the system and to preserve the uniqueness, it is assigned only
13331  * when ill becomes part of the global list. At that point ill will
13332  * have a name. If it doesn't get assigned here, it will get assigned
13333  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13334  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13335  * the interface flags or any other information from the DL_INFO_ACK for
13336  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13337  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13338  * second DL_INFO_ACK comes in from the driver.
13339  */
13340 static ipif_t *
13341 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13342 {
13343 	ipif_t	*ipif;
13344 	phyint_t *phyi;
13345 
13346 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13347 	    ill->ill_name, id, (void *)ill));
13348 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13349 
13350 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13351 		return (NULL);
13352 	*ipif = ipif_zero;	/* start clean */
13353 
13354 	ipif->ipif_ill = ill;
13355 	ipif->ipif_id = id;	/* could be -1 */
13356 	/*
13357 	 * Inherit the zoneid from the ill; for the shared stack instance
13358 	 * this is always the global zone
13359 	 */
13360 	ipif->ipif_zoneid = ill->ill_zoneid;
13361 
13362 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13363 
13364 	ipif->ipif_refcnt = 0;
13365 	ipif->ipif_saved_ire_cnt = 0;
13366 
13367 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13368 		mi_free(ipif);
13369 		return (NULL);
13370 	}
13371 	/* -1 id should have been replaced by real id */
13372 	id = ipif->ipif_id;
13373 	ASSERT(id >= 0);
13374 
13375 	if (ill->ill_name[0] != '\0')
13376 		ipif_assign_seqid(ipif);
13377 
13378 	/*
13379 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13380 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13381 	 * ioctl sets ipif_orig_ipifid to zero.
13382 	 */
13383 	ipif->ipif_orig_ipifid = id;
13384 
13385 	/*
13386 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13387 	 * The ipif is still not up and can't be looked up until the
13388 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13389 	 */
13390 	mutex_enter(&ill->ill_lock);
13391 	mutex_enter(&ill->ill_phyint->phyint_lock);
13392 	/*
13393 	 * Set the running flag when logical interface zero is created.
13394 	 * For subsequent logical interfaces, a DLPI link down
13395 	 * notification message may have cleared the running flag to
13396 	 * indicate the link is down, so we shouldn't just blindly set it.
13397 	 */
13398 	if (id == 0)
13399 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13400 	ipif->ipif_ire_type = ire_type;
13401 	phyi = ill->ill_phyint;
13402 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13403 
13404 	if (ipif->ipif_isv6) {
13405 		ill->ill_flags |= ILLF_IPV6;
13406 	} else {
13407 		ipaddr_t inaddr_any = INADDR_ANY;
13408 
13409 		ill->ill_flags |= ILLF_IPV4;
13410 
13411 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13412 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13413 		    &ipif->ipif_v6lcl_addr);
13414 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13415 		    &ipif->ipif_v6src_addr);
13416 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13417 		    &ipif->ipif_v6subnet);
13418 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13419 		    &ipif->ipif_v6net_mask);
13420 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13421 		    &ipif->ipif_v6brd_addr);
13422 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13423 		    &ipif->ipif_v6pp_dst_addr);
13424 	}
13425 
13426 	/*
13427 	 * Don't set the interface flags etc. now, will do it in
13428 	 * ip_ll_subnet_defaults.
13429 	 */
13430 	if (!initialize) {
13431 		mutex_exit(&ill->ill_lock);
13432 		mutex_exit(&ill->ill_phyint->phyint_lock);
13433 		return (ipif);
13434 	}
13435 	ipif->ipif_mtu = ill->ill_max_mtu;
13436 
13437 	if (ill->ill_bcast_addr_length != 0) {
13438 		/*
13439 		 * Later detect lack of DLPI driver multicast
13440 		 * capability by catching DL_ENABMULTI errors in
13441 		 * ip_rput_dlpi.
13442 		 */
13443 		ill->ill_flags |= ILLF_MULTICAST;
13444 		if (!ipif->ipif_isv6)
13445 			ipif->ipif_flags |= IPIF_BROADCAST;
13446 	} else {
13447 		if (ill->ill_net_type != IRE_LOOPBACK) {
13448 			if (ipif->ipif_isv6)
13449 				/*
13450 				 * Note: xresolv interfaces will eventually need
13451 				 * NOARP set here as well, but that will require
13452 				 * those external resolvers to have some
13453 				 * knowledge of that flag and act appropriately.
13454 				 * Not to be changed at present.
13455 				 */
13456 				ill->ill_flags |= ILLF_NONUD;
13457 			else
13458 				ill->ill_flags |= ILLF_NOARP;
13459 		}
13460 		if (ill->ill_phys_addr_length == 0) {
13461 			if (ill->ill_media &&
13462 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13463 				ipif->ipif_flags |= IPIF_NOXMIT;
13464 				phyi->phyint_flags |= PHYI_VIRTUAL;
13465 			} else {
13466 				/* pt-pt supports multicast. */
13467 				ill->ill_flags |= ILLF_MULTICAST;
13468 				if (ill->ill_net_type == IRE_LOOPBACK) {
13469 					phyi->phyint_flags |=
13470 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13471 				} else {
13472 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13473 				}
13474 			}
13475 		}
13476 	}
13477 	mutex_exit(&ill->ill_lock);
13478 	mutex_exit(&ill->ill_phyint->phyint_lock);
13479 	return (ipif);
13480 }
13481 
13482 /*
13483  * If appropriate, send a message up to the resolver delete the entry
13484  * for the address of this interface which is going out of business.
13485  * (Always called as writer).
13486  *
13487  * NOTE : We need to check for NULL mps as some of the fields are
13488  *	  initialized only for some interface types. See ipif_resolver_up()
13489  *	  for details.
13490  */
13491 void
13492 ipif_arp_down(ipif_t *ipif)
13493 {
13494 	mblk_t	*mp;
13495 	ill_t	*ill = ipif->ipif_ill;
13496 
13497 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13498 	ASSERT(IAM_WRITER_IPIF(ipif));
13499 
13500 	/* Delete the mapping for the local address */
13501 	mp = ipif->ipif_arp_del_mp;
13502 	if (mp != NULL) {
13503 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13504 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13505 		putnext(ill->ill_rq, mp);
13506 		ipif->ipif_arp_del_mp = NULL;
13507 	}
13508 
13509 	/*
13510 	 * If this is the last ipif that is going down and there are no
13511 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13512 	 * clean up ARP completely.
13513 	 */
13514 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13515 
13516 		/* Send up AR_INTERFACE_DOWN message */
13517 		mp = ill->ill_arp_down_mp;
13518 		if (mp != NULL) {
13519 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13520 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13521 			    ipif->ipif_id));
13522 			putnext(ill->ill_rq, mp);
13523 			ill->ill_arp_down_mp = NULL;
13524 		}
13525 
13526 		/* Tell ARP to delete the multicast mappings */
13527 		mp = ill->ill_arp_del_mapping_mp;
13528 		if (mp != NULL) {
13529 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13530 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13531 			    ipif->ipif_id));
13532 			putnext(ill->ill_rq, mp);
13533 			ill->ill_arp_del_mapping_mp = NULL;
13534 		}
13535 	}
13536 }
13537 
13538 /*
13539  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13540  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13541  * that it wants the add_mp allocated in this function to be returned
13542  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13543  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13544  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13545  * as it does a ipif_arp_down after calling this function - which will
13546  * remove what we add here.
13547  *
13548  * Returns -1 on failures and 0 on success.
13549  */
13550 int
13551 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13552 {
13553 	mblk_t	*del_mp = NULL;
13554 	mblk_t *add_mp = NULL;
13555 	mblk_t *mp;
13556 	ill_t	*ill = ipif->ipif_ill;
13557 	phyint_t *phyi = ill->ill_phyint;
13558 	ipaddr_t addr, mask, extract_mask = 0;
13559 	arma_t	*arma;
13560 	uint8_t *maddr, *bphys_addr;
13561 	uint32_t hw_start;
13562 	dl_unitdata_req_t *dlur;
13563 
13564 	ASSERT(IAM_WRITER_IPIF(ipif));
13565 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13566 		return (0);
13567 
13568 	/*
13569 	 * Delete the existing mapping from ARP. Normally ipif_down
13570 	 * -> ipif_arp_down should send this up to ARP. The only
13571 	 * reason we would find this when we are switching from
13572 	 * Multicast to Broadcast where we did not do a down.
13573 	 */
13574 	mp = ill->ill_arp_del_mapping_mp;
13575 	if (mp != NULL) {
13576 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13577 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13578 		putnext(ill->ill_rq, mp);
13579 		ill->ill_arp_del_mapping_mp = NULL;
13580 	}
13581 
13582 	if (arp_add_mapping_mp != NULL)
13583 		*arp_add_mapping_mp = NULL;
13584 
13585 	/*
13586 	 * Check that the address is not to long for the constant
13587 	 * length reserved in the template arma_t.
13588 	 */
13589 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13590 		return (-1);
13591 
13592 	/* Add mapping mblk */
13593 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13594 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13595 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13596 	    (caddr_t)&addr);
13597 	if (add_mp == NULL)
13598 		return (-1);
13599 	arma = (arma_t *)add_mp->b_rptr;
13600 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13601 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13602 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13603 
13604 	/*
13605 	 * Determine the broadcast address.
13606 	 */
13607 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13608 	if (ill->ill_sap_length < 0)
13609 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13610 	else
13611 		bphys_addr = (uchar_t *)dlur +
13612 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13613 	/*
13614 	 * Check PHYI_MULTI_BCAST and length of physical
13615 	 * address to determine if we use the mapping or the
13616 	 * broadcast address.
13617 	 */
13618 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13619 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13620 		    bphys_addr, maddr, &hw_start, &extract_mask))
13621 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13622 
13623 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13624 	    (ill->ill_flags & ILLF_MULTICAST)) {
13625 		/* Make sure this will not match the "exact" entry. */
13626 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13627 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13628 		    (caddr_t)&addr);
13629 		if (del_mp == NULL) {
13630 			freemsg(add_mp);
13631 			return (-1);
13632 		}
13633 		bcopy(&extract_mask, (char *)arma +
13634 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13635 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13636 			/* Use link-layer broadcast address for MULTI_BCAST */
13637 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13638 			ip2dbg(("ipif_arp_setup_multicast: adding"
13639 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13640 		} else {
13641 			arma->arma_hw_mapping_start = hw_start;
13642 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13643 			    " ARP setup for %s\n", ill->ill_name));
13644 		}
13645 	} else {
13646 		freemsg(add_mp);
13647 		ASSERT(del_mp == NULL);
13648 		/* It is neither MULTICAST nor MULTI_BCAST */
13649 		return (0);
13650 	}
13651 	ASSERT(add_mp != NULL && del_mp != NULL);
13652 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13653 	ill->ill_arp_del_mapping_mp = del_mp;
13654 	if (arp_add_mapping_mp != NULL) {
13655 		/* The caller just wants the mblks allocated */
13656 		*arp_add_mapping_mp = add_mp;
13657 	} else {
13658 		/* The caller wants us to send it to arp */
13659 		putnext(ill->ill_rq, add_mp);
13660 	}
13661 	return (0);
13662 }
13663 
13664 /*
13665  * Get the resolver set up for a new interface address.
13666  * (Always called as writer.)
13667  * Called both for IPv4 and IPv6 interfaces,
13668  * though it only sets up the resolver for v6
13669  * if it's an xresolv interface (one using an external resolver).
13670  * Honors ILLF_NOARP.
13671  * The enumerated value res_act is used to tune the behavior.
13672  * If set to Res_act_initial, then we set up all the resolver
13673  * structures for a new interface.  If set to Res_act_move, then
13674  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13675  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13676  * asynchronous hardware address change notification.  If set to
13677  * Res_act_defend, then we tell ARP that it needs to send a single
13678  * gratuitous message in defense of the address.
13679  * Returns error on failure.
13680  */
13681 int
13682 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13683 {
13684 	caddr_t	addr;
13685 	mblk_t	*arp_up_mp = NULL;
13686 	mblk_t	*arp_down_mp = NULL;
13687 	mblk_t	*arp_add_mp = NULL;
13688 	mblk_t	*arp_del_mp = NULL;
13689 	mblk_t	*arp_add_mapping_mp = NULL;
13690 	mblk_t	*arp_del_mapping_mp = NULL;
13691 	ill_t	*ill = ipif->ipif_ill;
13692 	uchar_t	*area_p = NULL;
13693 	uchar_t	*ared_p = NULL;
13694 	int	err = ENOMEM;
13695 	boolean_t was_dup;
13696 
13697 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13698 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13699 	ASSERT(IAM_WRITER_IPIF(ipif));
13700 
13701 	was_dup = B_FALSE;
13702 	if (res_act == Res_act_initial) {
13703 		ipif->ipif_addr_ready = 0;
13704 		/*
13705 		 * We're bringing an interface up here.  There's no way that we
13706 		 * should need to shut down ARP now.
13707 		 */
13708 		mutex_enter(&ill->ill_lock);
13709 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13710 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13711 			ill->ill_ipif_dup_count--;
13712 			was_dup = B_TRUE;
13713 		}
13714 		mutex_exit(&ill->ill_lock);
13715 	}
13716 	if (ipif->ipif_recovery_id != 0)
13717 		(void) untimeout(ipif->ipif_recovery_id);
13718 	ipif->ipif_recovery_id = 0;
13719 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13720 		ipif->ipif_addr_ready = 1;
13721 		return (0);
13722 	}
13723 	/* NDP will set the ipif_addr_ready flag when it's ready */
13724 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13725 		return (0);
13726 
13727 	if (ill->ill_isv6) {
13728 		/*
13729 		 * External resolver for IPv6
13730 		 */
13731 		ASSERT(res_act == Res_act_initial);
13732 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13733 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13734 			area_p = (uchar_t *)&ip6_area_template;
13735 			ared_p = (uchar_t *)&ip6_ared_template;
13736 		}
13737 	} else {
13738 		/*
13739 		 * IPv4 arp case. If the ARP stream has already started
13740 		 * closing, fail this request for ARP bringup. Else
13741 		 * record the fact that an ARP bringup is pending.
13742 		 */
13743 		mutex_enter(&ill->ill_lock);
13744 		if (ill->ill_arp_closing) {
13745 			mutex_exit(&ill->ill_lock);
13746 			err = EINVAL;
13747 			goto failed;
13748 		} else {
13749 			if (ill->ill_ipif_up_count == 0 &&
13750 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13751 				ill->ill_arp_bringup_pending = 1;
13752 			mutex_exit(&ill->ill_lock);
13753 		}
13754 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13755 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13756 			area_p = (uchar_t *)&ip_area_template;
13757 			ared_p = (uchar_t *)&ip_ared_template;
13758 		}
13759 	}
13760 
13761 	/*
13762 	 * Add an entry for the local address in ARP only if it
13763 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13764 	 */
13765 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13766 		area_t *area;
13767 
13768 		/* Now ask ARP to publish our address. */
13769 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13770 		if (arp_add_mp == NULL)
13771 			goto failed;
13772 		area = (area_t *)arp_add_mp->b_rptr;
13773 		if (res_act != Res_act_initial) {
13774 			/*
13775 			 * Copy the new hardware address and length into
13776 			 * arp_add_mp to be sent to ARP.
13777 			 */
13778 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13779 			bcopy(ill->ill_phys_addr,
13780 			    ((char *)area + area->area_hw_addr_offset),
13781 			    area->area_hw_addr_length);
13782 		}
13783 
13784 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13785 		    ACE_F_MYADDR;
13786 
13787 		if (res_act == Res_act_defend) {
13788 			area->area_flags |= ACE_F_DEFEND;
13789 			/*
13790 			 * If we're just defending our address now, then
13791 			 * there's no need to set up ARP multicast mappings.
13792 			 * The publish command is enough.
13793 			 */
13794 			goto done;
13795 		}
13796 
13797 		if (res_act != Res_act_initial)
13798 			goto arp_setup_multicast;
13799 
13800 		/*
13801 		 * Allocate an ARP deletion message so we know we can tell ARP
13802 		 * when the interface goes down.
13803 		 */
13804 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13805 		if (arp_del_mp == NULL)
13806 			goto failed;
13807 
13808 	} else {
13809 		if (res_act != Res_act_initial)
13810 			goto done;
13811 	}
13812 	/*
13813 	 * Need to bring up ARP or setup multicast mapping only
13814 	 * when the first interface is coming UP.
13815 	 */
13816 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13817 	    was_dup) {
13818 		goto done;
13819 	}
13820 
13821 	/*
13822 	 * Allocate an ARP down message (to be saved) and an ARP up
13823 	 * message.
13824 	 */
13825 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13826 	if (arp_down_mp == NULL)
13827 		goto failed;
13828 
13829 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13830 	if (arp_up_mp == NULL)
13831 		goto failed;
13832 
13833 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13834 		goto done;
13835 
13836 arp_setup_multicast:
13837 	/*
13838 	 * Setup the multicast mappings. This function initializes
13839 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13840 	 * IPv6.
13841 	 */
13842 	if (!ill->ill_isv6) {
13843 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13844 		if (err != 0)
13845 			goto failed;
13846 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13847 		ASSERT(arp_add_mapping_mp != NULL);
13848 	}
13849 
13850 done:
13851 	if (arp_del_mp != NULL) {
13852 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13853 		ipif->ipif_arp_del_mp = arp_del_mp;
13854 	}
13855 	if (arp_down_mp != NULL) {
13856 		ASSERT(ill->ill_arp_down_mp == NULL);
13857 		ill->ill_arp_down_mp = arp_down_mp;
13858 	}
13859 	if (arp_del_mapping_mp != NULL) {
13860 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13861 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13862 	}
13863 	if (arp_up_mp != NULL) {
13864 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13865 		    ill->ill_name, ipif->ipif_id));
13866 		putnext(ill->ill_rq, arp_up_mp);
13867 	}
13868 	if (arp_add_mp != NULL) {
13869 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13870 		    ill->ill_name, ipif->ipif_id));
13871 		/*
13872 		 * If it's an extended ARP implementation, then we'll wait to
13873 		 * hear that DAD has finished before using the interface.
13874 		 */
13875 		if (!ill->ill_arp_extend)
13876 			ipif->ipif_addr_ready = 1;
13877 		putnext(ill->ill_rq, arp_add_mp);
13878 	} else {
13879 		ipif->ipif_addr_ready = 1;
13880 	}
13881 	if (arp_add_mapping_mp != NULL) {
13882 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13883 		    ill->ill_name, ipif->ipif_id));
13884 		putnext(ill->ill_rq, arp_add_mapping_mp);
13885 	}
13886 	if (res_act != Res_act_initial)
13887 		return (0);
13888 
13889 	if (ill->ill_flags & ILLF_NOARP)
13890 		err = ill_arp_off(ill);
13891 	else
13892 		err = ill_arp_on(ill);
13893 	if (err != 0) {
13894 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13895 		freemsg(ipif->ipif_arp_del_mp);
13896 		freemsg(ill->ill_arp_down_mp);
13897 		freemsg(ill->ill_arp_del_mapping_mp);
13898 		ipif->ipif_arp_del_mp = NULL;
13899 		ill->ill_arp_down_mp = NULL;
13900 		ill->ill_arp_del_mapping_mp = NULL;
13901 		return (err);
13902 	}
13903 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13904 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13905 
13906 failed:
13907 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13908 	freemsg(arp_add_mp);
13909 	freemsg(arp_del_mp);
13910 	freemsg(arp_add_mapping_mp);
13911 	freemsg(arp_up_mp);
13912 	freemsg(arp_down_mp);
13913 	ill->ill_arp_bringup_pending = 0;
13914 	return (err);
13915 }
13916 
13917 /*
13918  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13919  * just gone back up.
13920  */
13921 static void
13922 ipif_arp_start_dad(ipif_t *ipif)
13923 {
13924 	ill_t *ill = ipif->ipif_ill;
13925 	mblk_t *arp_add_mp;
13926 	area_t *area;
13927 
13928 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13929 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13930 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13931 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13932 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13933 		/*
13934 		 * If we can't contact ARP for some reason, that's not really a
13935 		 * problem.  Just send out the routing socket notification that
13936 		 * DAD completion would have done, and continue.
13937 		 */
13938 		ipif_mask_reply(ipif);
13939 		ip_rts_ifmsg(ipif);
13940 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13941 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13942 		ipif->ipif_addr_ready = 1;
13943 		return;
13944 	}
13945 
13946 	/* Setting the 'unverified' flag restarts DAD */
13947 	area = (area_t *)arp_add_mp->b_rptr;
13948 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13949 	    ACE_F_UNVERIFIED;
13950 	putnext(ill->ill_rq, arp_add_mp);
13951 }
13952 
13953 static void
13954 ipif_ndp_start_dad(ipif_t *ipif)
13955 {
13956 	nce_t *nce;
13957 
13958 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13959 	if (nce == NULL)
13960 		return;
13961 
13962 	if (!ndp_restart_dad(nce)) {
13963 		/*
13964 		 * If we can't restart DAD for some reason, that's not really a
13965 		 * problem.  Just send out the routing socket notification that
13966 		 * DAD completion would have done, and continue.
13967 		 */
13968 		ip_rts_ifmsg(ipif);
13969 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13970 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13971 		ipif->ipif_addr_ready = 1;
13972 	}
13973 	NCE_REFRELE(nce);
13974 }
13975 
13976 /*
13977  * Restart duplicate address detection on all interfaces on the given ill.
13978  *
13979  * This is called when an interface transitions from down to up
13980  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
13981  *
13982  * Note that since the underlying physical link has transitioned, we must cause
13983  * at least one routing socket message to be sent here, either via DAD
13984  * completion or just by default on the first ipif.  (If we don't do this, then
13985  * in.mpathd will see long delays when doing link-based failure recovery.)
13986  */
13987 void
13988 ill_restart_dad(ill_t *ill, boolean_t went_up)
13989 {
13990 	ipif_t *ipif;
13991 
13992 	if (ill == NULL)
13993 		return;
13994 
13995 	/*
13996 	 * If layer two doesn't support duplicate address detection, then just
13997 	 * send the routing socket message now and be done with it.
13998 	 */
13999 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14000 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14001 		ip_rts_ifmsg(ill->ill_ipif);
14002 		return;
14003 	}
14004 
14005 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14006 		if (went_up) {
14007 			if (ipif->ipif_flags & IPIF_UP) {
14008 				if (ill->ill_isv6)
14009 					ipif_ndp_start_dad(ipif);
14010 				else
14011 					ipif_arp_start_dad(ipif);
14012 			} else if (ill->ill_isv6 &&
14013 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14014 				/*
14015 				 * For IPv4, the ARP module itself will
14016 				 * automatically start the DAD process when it
14017 				 * sees DL_NOTE_LINK_UP.  We respond to the
14018 				 * AR_CN_READY at the completion of that task.
14019 				 * For IPv6, we must kick off the bring-up
14020 				 * process now.
14021 				 */
14022 				ndp_do_recovery(ipif);
14023 			} else {
14024 				/*
14025 				 * Unfortunately, the first ipif is "special"
14026 				 * and represents the underlying ill in the
14027 				 * routing socket messages.  Thus, when this
14028 				 * one ipif is down, we must still notify so
14029 				 * that the user knows the IFF_RUNNING status
14030 				 * change.  (If the first ipif is up, then
14031 				 * we'll handle eventual routing socket
14032 				 * notification via DAD completion.)
14033 				 */
14034 				if (ipif == ill->ill_ipif)
14035 					ip_rts_ifmsg(ill->ill_ipif);
14036 			}
14037 		} else {
14038 			/*
14039 			 * After link down, we'll need to send a new routing
14040 			 * message when the link comes back, so clear
14041 			 * ipif_addr_ready.
14042 			 */
14043 			ipif->ipif_addr_ready = 0;
14044 		}
14045 	}
14046 
14047 	/*
14048 	 * If we've torn down links, then notify the user right away.
14049 	 */
14050 	if (!went_up)
14051 		ip_rts_ifmsg(ill->ill_ipif);
14052 }
14053 
14054 /*
14055  * Wakeup all threads waiting to enter the ipsq, and sleeping
14056  * on any of the ills in this ipsq. The ill_lock of the ill
14057  * must be held so that waiters don't miss wakeups
14058  */
14059 static void
14060 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14061 {
14062 	phyint_t *phyint;
14063 
14064 	phyint = ipsq->ipsq_phyint_list;
14065 	while (phyint != NULL) {
14066 		if (phyint->phyint_illv4) {
14067 			if (!caller_holds_lock)
14068 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14069 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14070 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14071 			if (!caller_holds_lock)
14072 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14073 		}
14074 		if (phyint->phyint_illv6) {
14075 			if (!caller_holds_lock)
14076 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14077 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14078 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14079 			if (!caller_holds_lock)
14080 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14081 		}
14082 		phyint = phyint->phyint_ipsq_next;
14083 	}
14084 }
14085 
14086 static ipsq_t *
14087 ipsq_create(char *groupname, ip_stack_t *ipst)
14088 {
14089 	ipsq_t	*ipsq;
14090 
14091 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14092 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14093 	if (ipsq == NULL) {
14094 		return (NULL);
14095 	}
14096 
14097 	if (groupname != NULL)
14098 		(void) strcpy(ipsq->ipsq_name, groupname);
14099 	else
14100 		ipsq->ipsq_name[0] = '\0';
14101 
14102 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14103 	ipsq->ipsq_flags |= IPSQ_GROUP;
14104 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14105 	ipst->ips_ipsq_g_head = ipsq;
14106 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14107 	return (ipsq);
14108 }
14109 
14110 /*
14111  * Return an ipsq correspoding to the groupname. If 'create' is true
14112  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14113  * uniquely with an IPMP group. However during IPMP groupname operations,
14114  * multiple IPMP groups may be associated with a single ipsq. But no
14115  * IPMP group can be associated with more than 1 ipsq at any time.
14116  * For example
14117  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14118  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14119  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14120  *
14121  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14122  * status shown below during the execution of the above command.
14123  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14124  *
14125  * After the completion of the above groupname command we return to the stable
14126  * state shown below.
14127  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14128  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14129  *
14130  * Because of the above, we don't search based on the ipsq_name since that
14131  * would miss the correct ipsq during certain windows as shown above.
14132  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14133  * natural state.
14134  */
14135 static ipsq_t *
14136 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14137     ip_stack_t *ipst)
14138 {
14139 	ipsq_t	*ipsq;
14140 	int	group_len;
14141 	phyint_t *phyint;
14142 
14143 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14144 
14145 	group_len = strlen(groupname);
14146 	ASSERT(group_len != 0);
14147 	group_len++;
14148 
14149 	for (ipsq = ipst->ips_ipsq_g_head;
14150 	    ipsq != NULL;
14151 	    ipsq = ipsq->ipsq_next) {
14152 		/*
14153 		 * When an ipsq is being split, and ill_split_ipsq
14154 		 * calls this function, we exclude it from being considered.
14155 		 */
14156 		if (ipsq == exclude_ipsq)
14157 			continue;
14158 
14159 		/*
14160 		 * Compare against the ipsq_name. The groupname change happens
14161 		 * in 2 phases. The 1st phase merges the from group into
14162 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14163 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14164 		 * ipsq_name. At this point the phyint_groupname has not been
14165 		 * updated.
14166 		 */
14167 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14168 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14169 			/*
14170 			 * Verify that an ipmp groupname is exactly
14171 			 * part of 1 ipsq and is not found in any other
14172 			 * ipsq.
14173 			 */
14174 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14175 			    NULL);
14176 			return (ipsq);
14177 		}
14178 
14179 		/*
14180 		 * Comparison against ipsq_name alone is not sufficient.
14181 		 * In the case when groups are currently being
14182 		 * merged, the ipsq could hold other IPMP groups temporarily.
14183 		 * so we walk the phyint list and compare against the
14184 		 * phyint_groupname as well.
14185 		 */
14186 		phyint = ipsq->ipsq_phyint_list;
14187 		while (phyint != NULL) {
14188 			if ((group_len == phyint->phyint_groupname_len) &&
14189 			    (bcmp(phyint->phyint_groupname, groupname,
14190 			    group_len) == 0)) {
14191 				/*
14192 				 * Verify that an ipmp groupname is exactly
14193 				 * part of 1 ipsq and is not found in any other
14194 				 * ipsq.
14195 				 */
14196 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14197 				    ipst) == NULL);
14198 				return (ipsq);
14199 			}
14200 			phyint = phyint->phyint_ipsq_next;
14201 		}
14202 	}
14203 	if (create)
14204 		ipsq = ipsq_create(groupname, ipst);
14205 	return (ipsq);
14206 }
14207 
14208 static void
14209 ipsq_delete(ipsq_t *ipsq)
14210 {
14211 	ipsq_t *nipsq;
14212 	ipsq_t *pipsq = NULL;
14213 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14214 
14215 	/*
14216 	 * We don't hold the ipsq lock, but we are sure no new
14217 	 * messages can land up, since the ipsq_refs is zero.
14218 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14219 	 * is associated with this ipsq. (Lookups are based on ill_name
14220 	 * or phyint_groupname)
14221 	 */
14222 	ASSERT(ipsq->ipsq_refs == 0);
14223 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14224 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14225 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14226 		/*
14227 		 * This is not the ipsq of an IPMP group.
14228 		 */
14229 		ipsq->ipsq_ipst = NULL;
14230 		kmem_free(ipsq, sizeof (ipsq_t));
14231 		return;
14232 	}
14233 
14234 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14235 
14236 	/*
14237 	 * Locate the ipsq  before we can remove it from
14238 	 * the singly linked list of ipsq's.
14239 	 */
14240 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14241 	    nipsq = nipsq->ipsq_next) {
14242 		if (nipsq == ipsq) {
14243 			break;
14244 		}
14245 		pipsq = nipsq;
14246 	}
14247 
14248 	ASSERT(nipsq == ipsq);
14249 
14250 	/* unlink ipsq from the list */
14251 	if (pipsq != NULL)
14252 		pipsq->ipsq_next = ipsq->ipsq_next;
14253 	else
14254 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14255 	ipsq->ipsq_ipst = NULL;
14256 	kmem_free(ipsq, sizeof (ipsq_t));
14257 	rw_exit(&ipst->ips_ill_g_lock);
14258 }
14259 
14260 static void
14261 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14262     queue_t *q)
14263 {
14264 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14265 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14266 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14267 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14268 	ASSERT(current_mp != NULL);
14269 
14270 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14271 	    NEW_OP, NULL);
14272 
14273 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14274 	    new_ipsq->ipsq_xopq_mphead != NULL);
14275 
14276 	/*
14277 	 * move from old ipsq to the new ipsq.
14278 	 */
14279 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14280 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14281 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14282 
14283 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14284 }
14285 
14286 void
14287 ill_group_cleanup(ill_t *ill)
14288 {
14289 	ill_t *ill_v4;
14290 	ill_t *ill_v6;
14291 	ipif_t *ipif;
14292 
14293 	ill_v4 = ill->ill_phyint->phyint_illv4;
14294 	ill_v6 = ill->ill_phyint->phyint_illv6;
14295 
14296 	if (ill_v4 != NULL) {
14297 		mutex_enter(&ill_v4->ill_lock);
14298 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14299 		    ipif = ipif->ipif_next) {
14300 			IPIF_UNMARK_MOVING(ipif);
14301 		}
14302 		ill_v4->ill_up_ipifs = B_FALSE;
14303 		mutex_exit(&ill_v4->ill_lock);
14304 	}
14305 
14306 	if (ill_v6 != NULL) {
14307 		mutex_enter(&ill_v6->ill_lock);
14308 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14309 		    ipif = ipif->ipif_next) {
14310 			IPIF_UNMARK_MOVING(ipif);
14311 		}
14312 		ill_v6->ill_up_ipifs = B_FALSE;
14313 		mutex_exit(&ill_v6->ill_lock);
14314 	}
14315 }
14316 /*
14317  * This function is called when an ill has had a change in its group status
14318  * to bring up all the ipifs that were up before the change.
14319  */
14320 int
14321 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14322 {
14323 	ipif_t *ipif;
14324 	ill_t *ill_v4;
14325 	ill_t *ill_v6;
14326 	ill_t *from_ill;
14327 	int err = 0;
14328 
14329 
14330 	ASSERT(IAM_WRITER_ILL(ill));
14331 
14332 	/*
14333 	 * Except for ipif_state_flags and ill_state_flags the other
14334 	 * fields of the ipif/ill that are modified below are protected
14335 	 * implicitly since we are a writer. We would have tried to down
14336 	 * even an ipif that was already down, in ill_down_ipifs. So we
14337 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14338 	 */
14339 	ill_v4 = ill->ill_phyint->phyint_illv4;
14340 	ill_v6 = ill->ill_phyint->phyint_illv6;
14341 	if (ill_v4 != NULL) {
14342 		ill_v4->ill_up_ipifs = B_TRUE;
14343 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14344 		    ipif = ipif->ipif_next) {
14345 			mutex_enter(&ill_v4->ill_lock);
14346 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14347 			IPIF_UNMARK_MOVING(ipif);
14348 			mutex_exit(&ill_v4->ill_lock);
14349 			if (ipif->ipif_was_up) {
14350 				if (!(ipif->ipif_flags & IPIF_UP))
14351 					err = ipif_up(ipif, q, mp);
14352 				ipif->ipif_was_up = B_FALSE;
14353 				if (err != 0) {
14354 					/*
14355 					 * Can there be any other error ?
14356 					 */
14357 					ASSERT(err == EINPROGRESS);
14358 					return (err);
14359 				}
14360 			}
14361 		}
14362 		mutex_enter(&ill_v4->ill_lock);
14363 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14364 		mutex_exit(&ill_v4->ill_lock);
14365 		ill_v4->ill_up_ipifs = B_FALSE;
14366 		if (ill_v4->ill_move_in_progress) {
14367 			ASSERT(ill_v4->ill_move_peer != NULL);
14368 			ill_v4->ill_move_in_progress = B_FALSE;
14369 			from_ill = ill_v4->ill_move_peer;
14370 			from_ill->ill_move_in_progress = B_FALSE;
14371 			from_ill->ill_move_peer = NULL;
14372 			mutex_enter(&from_ill->ill_lock);
14373 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14374 			mutex_exit(&from_ill->ill_lock);
14375 			if (ill_v6 == NULL) {
14376 				if (from_ill->ill_phyint->phyint_flags &
14377 				    PHYI_STANDBY) {
14378 					phyint_inactive(from_ill->ill_phyint);
14379 				}
14380 				if (ill_v4->ill_phyint->phyint_flags &
14381 				    PHYI_STANDBY) {
14382 					phyint_inactive(ill_v4->ill_phyint);
14383 				}
14384 			}
14385 			ill_v4->ill_move_peer = NULL;
14386 		}
14387 	}
14388 
14389 	if (ill_v6 != NULL) {
14390 		ill_v6->ill_up_ipifs = B_TRUE;
14391 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14392 		    ipif = ipif->ipif_next) {
14393 			mutex_enter(&ill_v6->ill_lock);
14394 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14395 			IPIF_UNMARK_MOVING(ipif);
14396 			mutex_exit(&ill_v6->ill_lock);
14397 			if (ipif->ipif_was_up) {
14398 				if (!(ipif->ipif_flags & IPIF_UP))
14399 					err = ipif_up(ipif, q, mp);
14400 				ipif->ipif_was_up = B_FALSE;
14401 				if (err != 0) {
14402 					/*
14403 					 * Can there be any other error ?
14404 					 */
14405 					ASSERT(err == EINPROGRESS);
14406 					return (err);
14407 				}
14408 			}
14409 		}
14410 		mutex_enter(&ill_v6->ill_lock);
14411 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14412 		mutex_exit(&ill_v6->ill_lock);
14413 		ill_v6->ill_up_ipifs = B_FALSE;
14414 		if (ill_v6->ill_move_in_progress) {
14415 			ASSERT(ill_v6->ill_move_peer != NULL);
14416 			ill_v6->ill_move_in_progress = B_FALSE;
14417 			from_ill = ill_v6->ill_move_peer;
14418 			from_ill->ill_move_in_progress = B_FALSE;
14419 			from_ill->ill_move_peer = NULL;
14420 			mutex_enter(&from_ill->ill_lock);
14421 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14422 			mutex_exit(&from_ill->ill_lock);
14423 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14424 				phyint_inactive(from_ill->ill_phyint);
14425 			}
14426 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14427 				phyint_inactive(ill_v6->ill_phyint);
14428 			}
14429 			ill_v6->ill_move_peer = NULL;
14430 		}
14431 	}
14432 	return (0);
14433 }
14434 
14435 /*
14436  * bring down all the approriate ipifs.
14437  */
14438 /* ARGSUSED */
14439 static void
14440 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14441 {
14442 	ipif_t *ipif;
14443 
14444 	ASSERT(IAM_WRITER_ILL(ill));
14445 
14446 	/*
14447 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14448 	 * are modified below are protected implicitly since we are a writer
14449 	 */
14450 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14451 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14452 			continue;
14453 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14454 			/*
14455 			 * We go through the ipif_down logic even if the ipif
14456 			 * is already down, since routes can be added based
14457 			 * on down ipifs. Going through ipif_down once again
14458 			 * will delete any IREs created based on these routes.
14459 			 */
14460 			if (ipif->ipif_flags & IPIF_UP)
14461 				ipif->ipif_was_up = B_TRUE;
14462 			/*
14463 			 * If called with chk_nofailover true ipif is moving.
14464 			 */
14465 			mutex_enter(&ill->ill_lock);
14466 			if (chk_nofailover) {
14467 				ipif->ipif_state_flags |=
14468 				    IPIF_MOVING | IPIF_CHANGING;
14469 			} else {
14470 				ipif->ipif_state_flags |= IPIF_CHANGING;
14471 			}
14472 			mutex_exit(&ill->ill_lock);
14473 			/*
14474 			 * Need to re-create net/subnet bcast ires if
14475 			 * they are dependent on ipif.
14476 			 */
14477 			if (!ipif->ipif_isv6)
14478 				ipif_check_bcast_ires(ipif);
14479 			(void) ipif_logical_down(ipif, NULL, NULL);
14480 			ipif_non_duplicate(ipif);
14481 			ipif_down_tail(ipif);
14482 		}
14483 	}
14484 }
14485 
14486 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14487 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14488 	(ipsq)->ipsq_refs++;				\
14489 }
14490 
14491 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14492 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14493 	(ipsq)->ipsq_refs--;				\
14494 	if ((ipsq)->ipsq_refs == 0)				\
14495 		(ipsq)->ipsq_name[0] = '\0'; 		\
14496 }
14497 
14498 /*
14499  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14500  * new_ipsq.
14501  */
14502 static void
14503 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14504 {
14505 	phyint_t *phyint;
14506 	phyint_t *next_phyint;
14507 
14508 	/*
14509 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14510 	 * writer and the ill_lock of the ill in question. Also the dest
14511 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14512 	 */
14513 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14514 
14515 	phyint = cur_ipsq->ipsq_phyint_list;
14516 	cur_ipsq->ipsq_phyint_list = NULL;
14517 	while (phyint != NULL) {
14518 		next_phyint = phyint->phyint_ipsq_next;
14519 		IPSQ_DEC_REF(cur_ipsq, ipst);
14520 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14521 		new_ipsq->ipsq_phyint_list = phyint;
14522 		IPSQ_INC_REF(new_ipsq, ipst);
14523 		phyint->phyint_ipsq = new_ipsq;
14524 		phyint = next_phyint;
14525 	}
14526 }
14527 
14528 #define	SPLIT_SUCCESS		0
14529 #define	SPLIT_NOT_NEEDED	1
14530 #define	SPLIT_FAILED		2
14531 
14532 int
14533 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14534     ip_stack_t *ipst)
14535 {
14536 	ipsq_t *newipsq = NULL;
14537 
14538 	/*
14539 	 * Assertions denote pre-requisites for changing the ipsq of
14540 	 * a phyint
14541 	 */
14542 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14543 	/*
14544 	 * <ill-phyint> assocs can't change while ill_g_lock
14545 	 * is held as writer. See ill_phyint_reinit()
14546 	 */
14547 	ASSERT(phyint->phyint_illv4 == NULL ||
14548 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14549 	ASSERT(phyint->phyint_illv6 == NULL ||
14550 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14551 
14552 	if ((phyint->phyint_groupname_len !=
14553 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14554 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14555 	    phyint->phyint_groupname_len) != 0)) {
14556 		/*
14557 		 * Once we fail in creating a new ipsq due to memory shortage,
14558 		 * don't attempt to create new ipsq again, based on another
14559 		 * phyint, since we want all phyints belonging to an IPMP group
14560 		 * to be in the same ipsq even in the event of mem alloc fails.
14561 		 */
14562 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14563 		    cur_ipsq, ipst);
14564 		if (newipsq == NULL) {
14565 			/* Memory allocation failure */
14566 			return (SPLIT_FAILED);
14567 		} else {
14568 			/* ipsq_refs protected by ill_g_lock (writer) */
14569 			IPSQ_DEC_REF(cur_ipsq, ipst);
14570 			phyint->phyint_ipsq = newipsq;
14571 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14572 			newipsq->ipsq_phyint_list = phyint;
14573 			IPSQ_INC_REF(newipsq, ipst);
14574 			return (SPLIT_SUCCESS);
14575 		}
14576 	}
14577 	return (SPLIT_NOT_NEEDED);
14578 }
14579 
14580 /*
14581  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14582  * to do this split
14583  */
14584 static int
14585 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14586 {
14587 	ipsq_t *newipsq;
14588 
14589 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14590 	/*
14591 	 * <ill-phyint> assocs can't change while ill_g_lock
14592 	 * is held as writer. See ill_phyint_reinit()
14593 	 */
14594 
14595 	ASSERT(phyint->phyint_illv4 == NULL ||
14596 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14597 	ASSERT(phyint->phyint_illv6 == NULL ||
14598 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14599 
14600 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14601 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14602 		/*
14603 		 * ipsq_init failed due to no memory
14604 		 * caller will use the same ipsq
14605 		 */
14606 		return (SPLIT_FAILED);
14607 	}
14608 
14609 	/* ipsq_ref is protected by ill_g_lock (writer) */
14610 	IPSQ_DEC_REF(cur_ipsq, ipst);
14611 
14612 	/*
14613 	 * This is a new ipsq that is unknown to the world.
14614 	 * So we don't need to hold ipsq_lock,
14615 	 */
14616 	newipsq = phyint->phyint_ipsq;
14617 	newipsq->ipsq_writer = NULL;
14618 	newipsq->ipsq_reentry_cnt--;
14619 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14620 #ifdef DEBUG
14621 	newipsq->ipsq_depth = 0;
14622 #endif
14623 
14624 	return (SPLIT_SUCCESS);
14625 }
14626 
14627 /*
14628  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14629  * ipsq's representing their individual groups or themselves. Return
14630  * whether split needs to be retried again later.
14631  */
14632 static boolean_t
14633 ill_split_ipsq(ipsq_t *cur_ipsq)
14634 {
14635 	phyint_t *phyint;
14636 	phyint_t *next_phyint;
14637 	int	error;
14638 	boolean_t need_retry = B_FALSE;
14639 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14640 
14641 	phyint = cur_ipsq->ipsq_phyint_list;
14642 	cur_ipsq->ipsq_phyint_list = NULL;
14643 	while (phyint != NULL) {
14644 		next_phyint = phyint->phyint_ipsq_next;
14645 		/*
14646 		 * 'created' will tell us whether the callee actually
14647 		 * created an ipsq. Lack of memory may force the callee
14648 		 * to return without creating an ipsq.
14649 		 */
14650 		if (phyint->phyint_groupname == NULL) {
14651 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14652 		} else {
14653 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14654 			    need_retry, ipst);
14655 		}
14656 
14657 		switch (error) {
14658 		case SPLIT_FAILED:
14659 			need_retry = B_TRUE;
14660 			/* FALLTHRU */
14661 		case SPLIT_NOT_NEEDED:
14662 			/*
14663 			 * Keep it on the list.
14664 			 */
14665 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14666 			cur_ipsq->ipsq_phyint_list = phyint;
14667 			break;
14668 		case SPLIT_SUCCESS:
14669 			break;
14670 		default:
14671 			ASSERT(0);
14672 		}
14673 
14674 		phyint = next_phyint;
14675 	}
14676 	return (need_retry);
14677 }
14678 
14679 /*
14680  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14681  * and return the ills in the list. This list will be
14682  * needed to unlock all the ills later on by the caller.
14683  * The <ill-ipsq> associations could change between the
14684  * lock and unlock. Hence the unlock can't traverse the
14685  * ipsq to get the list of ills.
14686  */
14687 static int
14688 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14689 {
14690 	int	cnt = 0;
14691 	phyint_t	*phyint;
14692 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14693 
14694 	/*
14695 	 * The caller holds ill_g_lock to ensure that the ill memberships
14696 	 * of the ipsq don't change
14697 	 */
14698 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14699 
14700 	phyint = ipsq->ipsq_phyint_list;
14701 	while (phyint != NULL) {
14702 		if (phyint->phyint_illv4 != NULL) {
14703 			ASSERT(cnt < list_max);
14704 			list[cnt++] = phyint->phyint_illv4;
14705 		}
14706 		if (phyint->phyint_illv6 != NULL) {
14707 			ASSERT(cnt < list_max);
14708 			list[cnt++] = phyint->phyint_illv6;
14709 		}
14710 		phyint = phyint->phyint_ipsq_next;
14711 	}
14712 	ill_lock_ills(list, cnt);
14713 	return (cnt);
14714 }
14715 
14716 void
14717 ill_lock_ills(ill_t **list, int cnt)
14718 {
14719 	int	i;
14720 
14721 	if (cnt > 1) {
14722 		boolean_t try_again;
14723 		do {
14724 			try_again = B_FALSE;
14725 			for (i = 0; i < cnt - 1; i++) {
14726 				if (list[i] < list[i + 1]) {
14727 					ill_t	*tmp;
14728 
14729 					/* swap the elements */
14730 					tmp = list[i];
14731 					list[i] = list[i + 1];
14732 					list[i + 1] = tmp;
14733 					try_again = B_TRUE;
14734 				}
14735 			}
14736 		} while (try_again);
14737 	}
14738 
14739 	for (i = 0; i < cnt; i++) {
14740 		if (i == 0) {
14741 			if (list[i] != NULL)
14742 				mutex_enter(&list[i]->ill_lock);
14743 			else
14744 				return;
14745 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14746 			mutex_enter(&list[i]->ill_lock);
14747 		}
14748 	}
14749 }
14750 
14751 void
14752 ill_unlock_ills(ill_t **list, int cnt)
14753 {
14754 	int	i;
14755 
14756 	for (i = 0; i < cnt; i++) {
14757 		if ((i == 0) && (list[i] != NULL)) {
14758 			mutex_exit(&list[i]->ill_lock);
14759 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14760 			mutex_exit(&list[i]->ill_lock);
14761 		}
14762 	}
14763 }
14764 
14765 /*
14766  * Merge all the ills from 1 ipsq group into another ipsq group.
14767  * The source ipsq group is specified by the ipsq associated with
14768  * 'from_ill'. The destination ipsq group is specified by the ipsq
14769  * associated with 'to_ill' or 'groupname' respectively.
14770  * Note that ipsq itself does not have a reference count mechanism
14771  * and functions don't look up an ipsq and pass it around. Instead
14772  * functions pass around an ill or groupname, and the ipsq is looked
14773  * up from the ill or groupname and the required operation performed
14774  * atomically with the lookup on the ipsq.
14775  */
14776 static int
14777 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14778     queue_t *q)
14779 {
14780 	ipsq_t *old_ipsq;
14781 	ipsq_t *new_ipsq;
14782 	ill_t	**ill_list;
14783 	int	cnt;
14784 	size_t	ill_list_size;
14785 	boolean_t became_writer_on_new_sq = B_FALSE;
14786 	ip_stack_t	*ipst = from_ill->ill_ipst;
14787 
14788 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14789 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14790 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14791 
14792 	/*
14793 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14794 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14795 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14796 	 */
14797 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14798 
14799 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14800 	if (groupname != NULL)
14801 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14802 	else {
14803 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14804 	}
14805 
14806 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14807 
14808 	/*
14809 	 * both groups are on the same ipsq.
14810 	 */
14811 	if (old_ipsq == new_ipsq) {
14812 		rw_exit(&ipst->ips_ill_g_lock);
14813 		return (0);
14814 	}
14815 
14816 	cnt = old_ipsq->ipsq_refs << 1;
14817 	ill_list_size = cnt * sizeof (ill_t *);
14818 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14819 	if (ill_list == NULL) {
14820 		rw_exit(&ipst->ips_ill_g_lock);
14821 		return (ENOMEM);
14822 	}
14823 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14824 
14825 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14826 	mutex_enter(&new_ipsq->ipsq_lock);
14827 	if ((new_ipsq->ipsq_writer == NULL &&
14828 	    new_ipsq->ipsq_current_ipif == NULL) ||
14829 	    (new_ipsq->ipsq_writer == curthread)) {
14830 		new_ipsq->ipsq_writer = curthread;
14831 		new_ipsq->ipsq_reentry_cnt++;
14832 		became_writer_on_new_sq = B_TRUE;
14833 	}
14834 
14835 	/*
14836 	 * We are holding ill_g_lock as writer and all the ill locks of
14837 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14838 	 * message can land up on the old ipsq even though we don't hold the
14839 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14840 	 */
14841 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14842 
14843 	/*
14844 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14845 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14846 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14847 	 */
14848 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14849 
14850 	/*
14851 	 * Mark the new ipsq as needing a split since it is currently
14852 	 * being shared by more than 1 IPMP group. The split will
14853 	 * occur at the end of ipsq_exit
14854 	 */
14855 	new_ipsq->ipsq_split = B_TRUE;
14856 
14857 	/* Now release all the locks */
14858 	mutex_exit(&new_ipsq->ipsq_lock);
14859 	ill_unlock_ills(ill_list, cnt);
14860 	rw_exit(&ipst->ips_ill_g_lock);
14861 
14862 	kmem_free(ill_list, ill_list_size);
14863 
14864 	/*
14865 	 * If we succeeded in becoming writer on the new ipsq, then
14866 	 * drain the new ipsq and start processing  all enqueued messages
14867 	 * including the current ioctl we are processing which is either
14868 	 * a set groupname or failover/failback.
14869 	 */
14870 	if (became_writer_on_new_sq)
14871 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14872 
14873 	/*
14874 	 * syncq has been changed and all the messages have been moved.
14875 	 */
14876 	mutex_enter(&old_ipsq->ipsq_lock);
14877 	old_ipsq->ipsq_current_ipif = NULL;
14878 	old_ipsq->ipsq_current_ioctl = 0;
14879 	mutex_exit(&old_ipsq->ipsq_lock);
14880 	return (EINPROGRESS);
14881 }
14882 
14883 /*
14884  * Delete and add the loopback copy and non-loopback copy of
14885  * the BROADCAST ire corresponding to ill and addr. Used to
14886  * group broadcast ires together when ill becomes part of
14887  * a group.
14888  *
14889  * This function is also called when ill is leaving the group
14890  * so that the ires belonging to the group gets re-grouped.
14891  */
14892 static void
14893 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14894 {
14895 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14896 	ire_t **ire_ptpn = &ire_head;
14897 	ip_stack_t	*ipst = ill->ill_ipst;
14898 
14899 	/*
14900 	 * The loopback and non-loopback IREs are inserted in the order in which
14901 	 * they're found, on the basis that they are correctly ordered (loopback
14902 	 * first).
14903 	 */
14904 	for (;;) {
14905 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14906 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14907 		if (ire == NULL)
14908 			break;
14909 
14910 		/*
14911 		 * we are passing in KM_SLEEP because it is not easy to
14912 		 * go back to a sane state in case of memory failure.
14913 		 */
14914 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14915 		ASSERT(nire != NULL);
14916 		bzero(nire, sizeof (ire_t));
14917 		/*
14918 		 * Don't use ire_max_frag directly since we don't
14919 		 * hold on to 'ire' until we add the new ire 'nire' and
14920 		 * we don't want the new ire to have a dangling reference
14921 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14922 		 * be in sync with the ipif_mtu of the associate ipif.
14923 		 * For eg. this happens as a result of SIOCSLIFNAME,
14924 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14925 		 * the driver. A change in ire_max_frag triggered as
14926 		 * as a result of path mtu discovery, or due to an
14927 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14928 		 * route change -mtu command does not apply to broadcast ires.
14929 		 *
14930 		 * XXX We need a recovery strategy here if ire_init fails
14931 		 */
14932 		if (ire_init(nire,
14933 		    (uchar_t *)&ire->ire_addr,
14934 		    (uchar_t *)&ire->ire_mask,
14935 		    (uchar_t *)&ire->ire_src_addr,
14936 		    (uchar_t *)&ire->ire_gateway_addr,
14937 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14938 		    &ire->ire_ipif->ipif_mtu,
14939 		    ire->ire_nce,
14940 		    ire->ire_rfq,
14941 		    ire->ire_stq,
14942 		    ire->ire_type,
14943 		    ire->ire_ipif,
14944 		    ire->ire_cmask,
14945 		    ire->ire_phandle,
14946 		    ire->ire_ihandle,
14947 		    ire->ire_flags,
14948 		    &ire->ire_uinfo,
14949 		    NULL,
14950 		    NULL,
14951 		    ipst) == NULL) {
14952 			cmn_err(CE_PANIC, "ire_init() failed");
14953 		}
14954 		ire_delete(ire);
14955 		ire_refrele(ire);
14956 
14957 		/*
14958 		 * The newly created IREs are inserted at the tail of the list
14959 		 * starting with ire_head. As we've just allocated them no one
14960 		 * knows about them so it's safe.
14961 		 */
14962 		*ire_ptpn = nire;
14963 		ire_ptpn = &nire->ire_next;
14964 	}
14965 
14966 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14967 		int error;
14968 		ire_t *oire;
14969 		/* unlink the IRE from our list before calling ire_add() */
14970 		nire_next = nire->ire_next;
14971 		nire->ire_next = NULL;
14972 
14973 		/* ire_add adds the ire at the right place in the list */
14974 		oire = nire;
14975 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
14976 		ASSERT(error == 0);
14977 		ASSERT(oire == nire);
14978 		ire_refrele(nire);	/* Held in ire_add */
14979 	}
14980 }
14981 
14982 /*
14983  * This function is usually called when an ill is inserted in
14984  * a group and all the ipifs are already UP. As all the ipifs
14985  * are already UP, the broadcast ires have already been created
14986  * and been inserted. But, ire_add_v4 would not have grouped properly.
14987  * We need to re-group for the benefit of ip_wput_ire which
14988  * expects BROADCAST ires to be grouped properly to avoid sending
14989  * more than one copy of the broadcast packet per group.
14990  *
14991  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14992  *	  because when ipif_up_done ends up calling this, ires have
14993  *        already been added before illgrp_insert i.e before ill_group
14994  *	  has been initialized.
14995  */
14996 static void
14997 ill_group_bcast_for_xmit(ill_t *ill)
14998 {
14999 	ill_group_t *illgrp;
15000 	ipif_t *ipif;
15001 	ipaddr_t addr;
15002 	ipaddr_t net_mask;
15003 	ipaddr_t subnet_netmask;
15004 
15005 	illgrp = ill->ill_group;
15006 
15007 	/*
15008 	 * This function is called even when an ill is deleted from
15009 	 * the group. Hence, illgrp could be null.
15010 	 */
15011 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15012 		return;
15013 
15014 	/*
15015 	 * Delete all the BROADCAST ires matching this ill and add
15016 	 * them back. This time, ire_add_v4 should take care of
15017 	 * grouping them with others because ill is part of the
15018 	 * group.
15019 	 */
15020 	ill_bcast_delete_and_add(ill, 0);
15021 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15022 
15023 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15024 
15025 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15026 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15027 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15028 		} else {
15029 			net_mask = htonl(IN_CLASSA_NET);
15030 		}
15031 		addr = net_mask & ipif->ipif_subnet;
15032 		ill_bcast_delete_and_add(ill, addr);
15033 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15034 
15035 		subnet_netmask = ipif->ipif_net_mask;
15036 		addr = ipif->ipif_subnet;
15037 		ill_bcast_delete_and_add(ill, addr);
15038 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15039 	}
15040 }
15041 
15042 /*
15043  * This function is called from illgrp_delete when ill is being deleted
15044  * from the group.
15045  *
15046  * As ill is not there in the group anymore, any address belonging
15047  * to this ill should be cleared of IRE_MARK_NORECV.
15048  */
15049 static void
15050 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15051 {
15052 	ire_t *ire;
15053 	irb_t *irb;
15054 	ip_stack_t	*ipst = ill->ill_ipst;
15055 
15056 	ASSERT(ill->ill_group == NULL);
15057 
15058 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15059 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15060 
15061 	if (ire != NULL) {
15062 		/*
15063 		 * IPMP and plumbing operations are serialized on the ipsq, so
15064 		 * no one will insert or delete a broadcast ire under our feet.
15065 		 */
15066 		irb = ire->ire_bucket;
15067 		rw_enter(&irb->irb_lock, RW_READER);
15068 		ire_refrele(ire);
15069 
15070 		for (; ire != NULL; ire = ire->ire_next) {
15071 			if (ire->ire_addr != addr)
15072 				break;
15073 			if (ire_to_ill(ire) != ill)
15074 				continue;
15075 
15076 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15077 			ire->ire_marks &= ~IRE_MARK_NORECV;
15078 		}
15079 		rw_exit(&irb->irb_lock);
15080 	}
15081 }
15082 
15083 /*
15084  * This function must be called only after the broadcast ires
15085  * have been grouped together. For a given address addr, nominate
15086  * only one of the ires whose interface is not FAILED or OFFLINE.
15087  *
15088  * This is also called when an ipif goes down, so that we can nominate
15089  * a different ire with the same address for receiving.
15090  */
15091 static void
15092 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15093 {
15094 	irb_t *irb;
15095 	ire_t *ire;
15096 	ire_t *ire1;
15097 	ire_t *save_ire;
15098 	ire_t **irep = NULL;
15099 	boolean_t first = B_TRUE;
15100 	ire_t *clear_ire = NULL;
15101 	ire_t *start_ire = NULL;
15102 	ire_t	*new_lb_ire;
15103 	ire_t	*new_nlb_ire;
15104 	boolean_t new_lb_ire_used = B_FALSE;
15105 	boolean_t new_nlb_ire_used = B_FALSE;
15106 	uint64_t match_flags;
15107 	uint64_t phyi_flags;
15108 	boolean_t fallback = B_FALSE;
15109 	uint_t	max_frag;
15110 
15111 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15112 	    NULL, MATCH_IRE_TYPE, ipst);
15113 	/*
15114 	 * We may not be able to find some ires if a previous
15115 	 * ire_create failed. This happens when an ipif goes
15116 	 * down and we are unable to create BROADCAST ires due
15117 	 * to memory failure. Thus, we have to check for NULL
15118 	 * below. This should handle the case for LOOPBACK,
15119 	 * POINTOPOINT and interfaces with some POINTOPOINT
15120 	 * logicals for which there are no BROADCAST ires.
15121 	 */
15122 	if (ire == NULL)
15123 		return;
15124 	/*
15125 	 * Currently IRE_BROADCASTS are deleted when an ipif
15126 	 * goes down which runs exclusively. Thus, setting
15127 	 * IRE_MARK_RCVD should not race with ire_delete marking
15128 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15129 	 * be consistent with other parts of the code that walks
15130 	 * a given bucket.
15131 	 */
15132 	save_ire = ire;
15133 	irb = ire->ire_bucket;
15134 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15135 	if (new_lb_ire == NULL) {
15136 		ire_refrele(ire);
15137 		return;
15138 	}
15139 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15140 	if (new_nlb_ire == NULL) {
15141 		ire_refrele(ire);
15142 		kmem_cache_free(ire_cache, new_lb_ire);
15143 		return;
15144 	}
15145 	IRB_REFHOLD(irb);
15146 	rw_enter(&irb->irb_lock, RW_WRITER);
15147 	/*
15148 	 * Get to the first ire matching the address and the
15149 	 * group. If the address does not match we are done
15150 	 * as we could not find the IRE. If the address matches
15151 	 * we should get to the first one matching the group.
15152 	 */
15153 	while (ire != NULL) {
15154 		if (ire->ire_addr != addr ||
15155 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15156 			break;
15157 		}
15158 		ire = ire->ire_next;
15159 	}
15160 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15161 	start_ire = ire;
15162 redo:
15163 	while (ire != NULL && ire->ire_addr == addr &&
15164 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15165 		/*
15166 		 * The first ire for any address within a group
15167 		 * should always be the one with IRE_MARK_NORECV cleared
15168 		 * so that ip_wput_ire can avoid searching for one.
15169 		 * Note down the insertion point which will be used
15170 		 * later.
15171 		 */
15172 		if (first && (irep == NULL))
15173 			irep = ire->ire_ptpn;
15174 		/*
15175 		 * PHYI_FAILED is set when the interface fails.
15176 		 * This interface might have become good, but the
15177 		 * daemon has not yet detected. We should still
15178 		 * not receive on this. PHYI_OFFLINE should never
15179 		 * be picked as this has been offlined and soon
15180 		 * be removed.
15181 		 */
15182 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15183 		if (phyi_flags & PHYI_OFFLINE) {
15184 			ire->ire_marks |= IRE_MARK_NORECV;
15185 			ire = ire->ire_next;
15186 			continue;
15187 		}
15188 		if (phyi_flags & match_flags) {
15189 			ire->ire_marks |= IRE_MARK_NORECV;
15190 			ire = ire->ire_next;
15191 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15192 			    PHYI_INACTIVE) {
15193 				fallback = B_TRUE;
15194 			}
15195 			continue;
15196 		}
15197 		if (first) {
15198 			/*
15199 			 * We will move this to the front of the list later
15200 			 * on.
15201 			 */
15202 			clear_ire = ire;
15203 			ire->ire_marks &= ~IRE_MARK_NORECV;
15204 		} else {
15205 			ire->ire_marks |= IRE_MARK_NORECV;
15206 		}
15207 		first = B_FALSE;
15208 		ire = ire->ire_next;
15209 	}
15210 	/*
15211 	 * If we never nominated anybody, try nominating at least
15212 	 * an INACTIVE, if we found one. Do it only once though.
15213 	 */
15214 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15215 	    fallback) {
15216 		match_flags = PHYI_FAILED;
15217 		ire = start_ire;
15218 		irep = NULL;
15219 		goto redo;
15220 	}
15221 	ire_refrele(save_ire);
15222 
15223 	/*
15224 	 * irep non-NULL indicates that we entered the while loop
15225 	 * above. If clear_ire is at the insertion point, we don't
15226 	 * have to do anything. clear_ire will be NULL if all the
15227 	 * interfaces are failed.
15228 	 *
15229 	 * We cannot unlink and reinsert the ire at the right place
15230 	 * in the list since there can be other walkers of this bucket.
15231 	 * Instead we delete and recreate the ire
15232 	 */
15233 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15234 		ire_t *clear_ire_stq = NULL;
15235 
15236 		bzero(new_lb_ire, sizeof (ire_t));
15237 		/* XXX We need a recovery strategy here. */
15238 		if (ire_init(new_lb_ire,
15239 		    (uchar_t *)&clear_ire->ire_addr,
15240 		    (uchar_t *)&clear_ire->ire_mask,
15241 		    (uchar_t *)&clear_ire->ire_src_addr,
15242 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15243 		    &clear_ire->ire_max_frag,
15244 		    NULL, /* let ire_nce_init derive the resolver info */
15245 		    clear_ire->ire_rfq,
15246 		    clear_ire->ire_stq,
15247 		    clear_ire->ire_type,
15248 		    clear_ire->ire_ipif,
15249 		    clear_ire->ire_cmask,
15250 		    clear_ire->ire_phandle,
15251 		    clear_ire->ire_ihandle,
15252 		    clear_ire->ire_flags,
15253 		    &clear_ire->ire_uinfo,
15254 		    NULL,
15255 		    NULL,
15256 		    ipst) == NULL)
15257 			cmn_err(CE_PANIC, "ire_init() failed");
15258 		if (clear_ire->ire_stq == NULL) {
15259 			ire_t *ire_next = clear_ire->ire_next;
15260 			if (ire_next != NULL &&
15261 			    ire_next->ire_stq != NULL &&
15262 			    ire_next->ire_addr == clear_ire->ire_addr &&
15263 			    ire_next->ire_ipif->ipif_ill ==
15264 			    clear_ire->ire_ipif->ipif_ill) {
15265 				clear_ire_stq = ire_next;
15266 
15267 				bzero(new_nlb_ire, sizeof (ire_t));
15268 				/* XXX We need a recovery strategy here. */
15269 				if (ire_init(new_nlb_ire,
15270 				    (uchar_t *)&clear_ire_stq->ire_addr,
15271 				    (uchar_t *)&clear_ire_stq->ire_mask,
15272 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15273 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15274 				    &clear_ire_stq->ire_max_frag,
15275 				    NULL,
15276 				    clear_ire_stq->ire_rfq,
15277 				    clear_ire_stq->ire_stq,
15278 				    clear_ire_stq->ire_type,
15279 				    clear_ire_stq->ire_ipif,
15280 				    clear_ire_stq->ire_cmask,
15281 				    clear_ire_stq->ire_phandle,
15282 				    clear_ire_stq->ire_ihandle,
15283 				    clear_ire_stq->ire_flags,
15284 				    &clear_ire_stq->ire_uinfo,
15285 				    NULL,
15286 				    NULL,
15287 				    ipst) == NULL)
15288 					cmn_err(CE_PANIC, "ire_init() failed");
15289 			}
15290 		}
15291 
15292 		/*
15293 		 * Delete the ire. We can't call ire_delete() since
15294 		 * we are holding the bucket lock. We can't release the
15295 		 * bucket lock since we can't allow irep to change. So just
15296 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15297 		 * ire from the list and do the refrele.
15298 		 */
15299 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15300 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15301 
15302 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15303 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15304 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15305 		}
15306 
15307 		/*
15308 		 * Also take care of otherfields like ib/ob pkt count
15309 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15310 		 */
15311 
15312 		/* Set the max_frag before adding the ire */
15313 		max_frag = *new_lb_ire->ire_max_fragp;
15314 		new_lb_ire->ire_max_fragp = NULL;
15315 		new_lb_ire->ire_max_frag = max_frag;
15316 
15317 		/* Add the new ire's. Insert at *irep */
15318 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15319 		ire1 = *irep;
15320 		if (ire1 != NULL)
15321 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15322 		new_lb_ire->ire_next = ire1;
15323 		/* Link the new one in. */
15324 		new_lb_ire->ire_ptpn = irep;
15325 		membar_producer();
15326 		*irep = new_lb_ire;
15327 		new_lb_ire_used = B_TRUE;
15328 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15329 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15330 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15331 
15332 		if (clear_ire_stq != NULL) {
15333 			/* Set the max_frag before adding the ire */
15334 			max_frag = *new_nlb_ire->ire_max_fragp;
15335 			new_nlb_ire->ire_max_fragp = NULL;
15336 			new_nlb_ire->ire_max_frag = max_frag;
15337 
15338 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15339 			irep = &new_lb_ire->ire_next;
15340 			/* Add the new ire. Insert at *irep */
15341 			ire1 = *irep;
15342 			if (ire1 != NULL)
15343 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15344 			new_nlb_ire->ire_next = ire1;
15345 			/* Link the new one in. */
15346 			new_nlb_ire->ire_ptpn = irep;
15347 			membar_producer();
15348 			*irep = new_nlb_ire;
15349 			new_nlb_ire_used = B_TRUE;
15350 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15351 			    ire_stats_inserted);
15352 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15353 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15354 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
15355 		}
15356 	}
15357 	rw_exit(&irb->irb_lock);
15358 	if (!new_lb_ire_used)
15359 		kmem_cache_free(ire_cache, new_lb_ire);
15360 	if (!new_nlb_ire_used)
15361 		kmem_cache_free(ire_cache, new_nlb_ire);
15362 	IRB_REFRELE(irb);
15363 }
15364 
15365 /*
15366  * Whenever an ipif goes down we have to renominate a different
15367  * broadcast ire to receive. Whenever an ipif comes up, we need
15368  * to make sure that we have only one nominated to receive.
15369  */
15370 static void
15371 ipif_renominate_bcast(ipif_t *ipif)
15372 {
15373 	ill_t *ill = ipif->ipif_ill;
15374 	ipaddr_t subnet_addr;
15375 	ipaddr_t net_addr;
15376 	ipaddr_t net_mask = 0;
15377 	ipaddr_t subnet_netmask;
15378 	ipaddr_t addr;
15379 	ill_group_t *illgrp;
15380 	ip_stack_t	*ipst = ill->ill_ipst;
15381 
15382 	illgrp = ill->ill_group;
15383 	/*
15384 	 * If this is the last ipif going down, it might take
15385 	 * the ill out of the group. In that case ipif_down ->
15386 	 * illgrp_delete takes care of doing the nomination.
15387 	 * ipif_down does not call for this case.
15388 	 */
15389 	ASSERT(illgrp != NULL);
15390 
15391 	/* There could not have been any ires associated with this */
15392 	if (ipif->ipif_subnet == 0)
15393 		return;
15394 
15395 	ill_mark_bcast(illgrp, 0, ipst);
15396 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15397 
15398 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15399 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15400 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15401 	} else {
15402 		net_mask = htonl(IN_CLASSA_NET);
15403 	}
15404 	addr = net_mask & ipif->ipif_subnet;
15405 	ill_mark_bcast(illgrp, addr, ipst);
15406 
15407 	net_addr = ~net_mask | addr;
15408 	ill_mark_bcast(illgrp, net_addr, ipst);
15409 
15410 	subnet_netmask = ipif->ipif_net_mask;
15411 	addr = ipif->ipif_subnet;
15412 	ill_mark_bcast(illgrp, addr, ipst);
15413 
15414 	subnet_addr = ~subnet_netmask | addr;
15415 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15416 }
15417 
15418 /*
15419  * Whenever we form or delete ill groups, we need to nominate one set of
15420  * BROADCAST ires for receiving in the group.
15421  *
15422  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15423  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15424  *    for ill_ipif_up_count to be non-zero. This is the only case where
15425  *    ill_ipif_up_count is zero and we would still find the ires.
15426  *
15427  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15428  *    ipif is UP and we just have to do the nomination.
15429  *
15430  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15431  *    from the group. So, we have to do the nomination.
15432  *
15433  * Because of (3), there could be just one ill in the group. But we have
15434  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15435  * Thus, this function does not optimize when there is only one ill as
15436  * it is not correct for (3).
15437  */
15438 static void
15439 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15440 {
15441 	ill_t *ill;
15442 	ipif_t *ipif;
15443 	ipaddr_t subnet_addr;
15444 	ipaddr_t prev_subnet_addr = 0;
15445 	ipaddr_t net_addr;
15446 	ipaddr_t prev_net_addr = 0;
15447 	ipaddr_t net_mask = 0;
15448 	ipaddr_t subnet_netmask;
15449 	ipaddr_t addr;
15450 	ip_stack_t	*ipst;
15451 
15452 	/*
15453 	 * When the last memeber is leaving, there is nothing to
15454 	 * nominate.
15455 	 */
15456 	if (illgrp->illgrp_ill_count == 0) {
15457 		ASSERT(illgrp->illgrp_ill == NULL);
15458 		return;
15459 	}
15460 
15461 	ill = illgrp->illgrp_ill;
15462 	ASSERT(!ill->ill_isv6);
15463 	ipst = ill->ill_ipst;
15464 	/*
15465 	 * We assume that ires with same address and belonging to the
15466 	 * same group, has been grouped together. Nominating a *single*
15467 	 * ill in the group for sending and receiving broadcast is done
15468 	 * by making sure that the first BROADCAST ire (which will be
15469 	 * the one returned by ire_ctable_lookup for ip_rput and the
15470 	 * one that will be used in ip_wput_ire) will be the one that
15471 	 * will not have IRE_MARK_NORECV set.
15472 	 *
15473 	 * 1) ip_rput checks and discards packets received on ires marked
15474 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15475 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15476 	 *    first ire in the group for every broadcast address in the group.
15477 	 *    ip_rput will accept packets only on the first ire i.e only
15478 	 *    one copy of the ill.
15479 	 *
15480 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15481 	 *    packet for the whole group. It needs to send out on the ill
15482 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15483 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15484 	 *    the copy echoed back on other port where the ire is not marked
15485 	 *    with IRE_MARK_NORECV.
15486 	 *
15487 	 * Note that we just need to have the first IRE either loopback or
15488 	 * non-loopback (either of them may not exist if ire_create failed
15489 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15490 	 * always hit the first one and hence will always accept one copy.
15491 	 *
15492 	 * We have a broadcast ire per ill for all the unique prefixes
15493 	 * hosted on that ill. As we don't have a way of knowing the
15494 	 * unique prefixes on a given ill and hence in the whole group,
15495 	 * we just call ill_mark_bcast on all the prefixes that exist
15496 	 * in the group. For the common case of one prefix, the code
15497 	 * below optimizes by remebering the last address used for
15498 	 * markng. In the case of multiple prefixes, this will still
15499 	 * optimize depending the order of prefixes.
15500 	 *
15501 	 * The only unique address across the whole group is 0.0.0.0 and
15502 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15503 	 * the first ire in the bucket for receiving and disables the
15504 	 * others.
15505 	 */
15506 	ill_mark_bcast(illgrp, 0, ipst);
15507 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15508 	for (; ill != NULL; ill = ill->ill_group_next) {
15509 
15510 		for (ipif = ill->ill_ipif; ipif != NULL;
15511 		    ipif = ipif->ipif_next) {
15512 
15513 			if (!(ipif->ipif_flags & IPIF_UP) ||
15514 			    ipif->ipif_subnet == 0) {
15515 				continue;
15516 			}
15517 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15518 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15519 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15520 			} else {
15521 				net_mask = htonl(IN_CLASSA_NET);
15522 			}
15523 			addr = net_mask & ipif->ipif_subnet;
15524 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15525 				ill_mark_bcast(illgrp, addr, ipst);
15526 				net_addr = ~net_mask | addr;
15527 				ill_mark_bcast(illgrp, net_addr, ipst);
15528 			}
15529 			prev_net_addr = addr;
15530 
15531 			subnet_netmask = ipif->ipif_net_mask;
15532 			addr = ipif->ipif_subnet;
15533 			if (prev_subnet_addr == 0 ||
15534 			    prev_subnet_addr != addr) {
15535 				ill_mark_bcast(illgrp, addr, ipst);
15536 				subnet_addr = ~subnet_netmask | addr;
15537 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15538 			}
15539 			prev_subnet_addr = addr;
15540 		}
15541 	}
15542 }
15543 
15544 /*
15545  * This function is called while forming ill groups.
15546  *
15547  * Currently, we handle only allmulti groups. We want to join
15548  * allmulti on only one of the ills in the groups. In future,
15549  * when we have link aggregation, we may have to join normal
15550  * multicast groups on multiple ills as switch does inbound load
15551  * balancing. Following are the functions that calls this
15552  * function :
15553  *
15554  * 1) ill_recover_multicast : Interface is coming back UP.
15555  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15556  *    will call ill_recover_multicast to recover all the multicast
15557  *    groups. We need to make sure that only one member is joined
15558  *    in the ill group.
15559  *
15560  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15561  *    Somebody is joining allmulti. We need to make sure that only one
15562  *    member is joined in the group.
15563  *
15564  * 3) illgrp_insert : If allmulti has already joined, we need to make
15565  *    sure that only one member is joined in the group.
15566  *
15567  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15568  *    allmulti who we have nominated. We need to pick someother ill.
15569  *
15570  * 5) illgrp_delete : The ill we nominated is leaving the group,
15571  *    we need to pick a new ill to join the group.
15572  *
15573  * For (1), (2), (5) - we just have to check whether there is
15574  * a good ill joined in the group. If we could not find any ills
15575  * joined the group, we should join.
15576  *
15577  * For (4), the one that was nominated to receive, left the group.
15578  * There could be nobody joined in the group when this function is
15579  * called.
15580  *
15581  * For (3) - we need to explicitly check whether there are multiple
15582  * ills joined in the group.
15583  *
15584  * For simplicity, we don't differentiate any of the above cases. We
15585  * just leave the group if it is joined on any of them and join on
15586  * the first good ill.
15587  */
15588 int
15589 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15590 {
15591 	ilm_t *ilm;
15592 	ill_t *ill;
15593 	ill_t *fallback_inactive_ill = NULL;
15594 	ill_t *fallback_failed_ill = NULL;
15595 	int ret = 0;
15596 
15597 	/*
15598 	 * Leave the allmulti on all the ills and start fresh.
15599 	 */
15600 	for (ill = illgrp->illgrp_ill; ill != NULL;
15601 	    ill = ill->ill_group_next) {
15602 		if (ill->ill_join_allmulti)
15603 			(void) ip_leave_allmulti(ill->ill_ipif);
15604 	}
15605 
15606 	/*
15607 	 * Choose a good ill. Fallback to inactive or failed if
15608 	 * none available. We need to fallback to FAILED in the
15609 	 * case where we have 2 interfaces in a group - where
15610 	 * one of them is failed and another is a good one and
15611 	 * the good one (not marked inactive) is leaving the group.
15612 	 */
15613 	ret = 0;
15614 	for (ill = illgrp->illgrp_ill; ill != NULL;
15615 	    ill = ill->ill_group_next) {
15616 		/* Never pick an offline interface */
15617 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15618 			continue;
15619 
15620 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15621 			fallback_failed_ill = ill;
15622 			continue;
15623 		}
15624 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15625 			fallback_inactive_ill = ill;
15626 			continue;
15627 		}
15628 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15629 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15630 				ret = ip_join_allmulti(ill->ill_ipif);
15631 				/*
15632 				 * ip_join_allmulti can fail because of memory
15633 				 * failures. So, make sure we join at least
15634 				 * on one ill.
15635 				 */
15636 				if (ill->ill_join_allmulti)
15637 					return (0);
15638 			}
15639 		}
15640 	}
15641 	if (ret != 0) {
15642 		/*
15643 		 * If we tried nominating above and failed to do so,
15644 		 * return error. We might have tried multiple times.
15645 		 * But, return the latest error.
15646 		 */
15647 		return (ret);
15648 	}
15649 	if ((ill = fallback_inactive_ill) != NULL) {
15650 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15651 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15652 				ret = ip_join_allmulti(ill->ill_ipif);
15653 				return (ret);
15654 			}
15655 		}
15656 	} else if ((ill = fallback_failed_ill) != NULL) {
15657 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15658 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15659 				ret = ip_join_allmulti(ill->ill_ipif);
15660 				return (ret);
15661 			}
15662 		}
15663 	}
15664 	return (0);
15665 }
15666 
15667 /*
15668  * This function is called from illgrp_delete after it is
15669  * deleted from the group to reschedule responsibilities
15670  * to a different ill.
15671  */
15672 static void
15673 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15674 {
15675 	ilm_t	*ilm;
15676 	ipif_t	*ipif;
15677 	ipaddr_t subnet_addr;
15678 	ipaddr_t net_addr;
15679 	ipaddr_t net_mask = 0;
15680 	ipaddr_t subnet_netmask;
15681 	ipaddr_t addr;
15682 	ip_stack_t *ipst = ill->ill_ipst;
15683 
15684 	ASSERT(ill->ill_group == NULL);
15685 	/*
15686 	 * Broadcast Responsibility:
15687 	 *
15688 	 * 1. If this ill has been nominated for receiving broadcast
15689 	 * packets, we need to find a new one. Before we find a new
15690 	 * one, we need to re-group the ires that are part of this new
15691 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15692 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15693 	 * thing for us.
15694 	 *
15695 	 * 2. If this ill was not nominated for receiving broadcast
15696 	 * packets, we need to clear the IRE_MARK_NORECV flag
15697 	 * so that we continue to send up broadcast packets.
15698 	 */
15699 	if (!ill->ill_isv6) {
15700 		/*
15701 		 * Case 1 above : No optimization here. Just redo the
15702 		 * nomination.
15703 		 */
15704 		ill_group_bcast_for_xmit(ill);
15705 		ill_nominate_bcast_rcv(illgrp);
15706 
15707 		/*
15708 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15709 		 */
15710 		ill_clear_bcast_mark(ill, 0);
15711 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15712 
15713 		for (ipif = ill->ill_ipif; ipif != NULL;
15714 		    ipif = ipif->ipif_next) {
15715 
15716 			if (!(ipif->ipif_flags & IPIF_UP) ||
15717 			    ipif->ipif_subnet == 0) {
15718 				continue;
15719 			}
15720 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15721 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15722 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15723 			} else {
15724 				net_mask = htonl(IN_CLASSA_NET);
15725 			}
15726 			addr = net_mask & ipif->ipif_subnet;
15727 			ill_clear_bcast_mark(ill, addr);
15728 
15729 			net_addr = ~net_mask | addr;
15730 			ill_clear_bcast_mark(ill, net_addr);
15731 
15732 			subnet_netmask = ipif->ipif_net_mask;
15733 			addr = ipif->ipif_subnet;
15734 			ill_clear_bcast_mark(ill, addr);
15735 
15736 			subnet_addr = ~subnet_netmask | addr;
15737 			ill_clear_bcast_mark(ill, subnet_addr);
15738 		}
15739 	}
15740 
15741 	/*
15742 	 * Multicast Responsibility.
15743 	 *
15744 	 * If we have joined allmulti on this one, find a new member
15745 	 * in the group to join allmulti. As this ill is already part
15746 	 * of allmulti, we don't have to join on this one.
15747 	 *
15748 	 * If we have not joined allmulti on this one, there is no
15749 	 * responsibility to handoff. But we need to take new
15750 	 * responsibility i.e, join allmulti on this one if we need
15751 	 * to.
15752 	 */
15753 	if (ill->ill_join_allmulti) {
15754 		(void) ill_nominate_mcast_rcv(illgrp);
15755 	} else {
15756 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15757 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15758 				(void) ip_join_allmulti(ill->ill_ipif);
15759 				break;
15760 			}
15761 		}
15762 	}
15763 
15764 	/*
15765 	 * We intentionally do the flushing of IRE_CACHES only matching
15766 	 * on the ill and not on groups. Note that we are already deleted
15767 	 * from the group.
15768 	 *
15769 	 * This will make sure that all IRE_CACHES whose stq is pointing
15770 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15771 	 * deleted and IRE_CACHES that are not pointing at this ill will
15772 	 * be left alone.
15773 	 */
15774 	if (ill->ill_isv6) {
15775 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15776 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15777 	} else {
15778 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15779 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15780 	}
15781 
15782 	/*
15783 	 * Some conn may have cached one of the IREs deleted above. By removing
15784 	 * the ire reference, we clean up the extra reference to the ill held in
15785 	 * ire->ire_stq.
15786 	 */
15787 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15788 
15789 	/*
15790 	 * Re-do source address selection for all the members in the
15791 	 * group, if they borrowed source address from one of the ipifs
15792 	 * in this ill.
15793 	 */
15794 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15795 		if (ill->ill_isv6) {
15796 			ipif_update_other_ipifs_v6(ipif, illgrp);
15797 		} else {
15798 			ipif_update_other_ipifs(ipif, illgrp);
15799 		}
15800 	}
15801 }
15802 
15803 /*
15804  * Delete the ill from the group. The caller makes sure that it is
15805  * in a group and it okay to delete from the group. So, we always
15806  * delete here.
15807  */
15808 static void
15809 illgrp_delete(ill_t *ill)
15810 {
15811 	ill_group_t *illgrp;
15812 	ill_group_t *tmpg;
15813 	ill_t *tmp_ill;
15814 	ip_stack_t	*ipst = ill->ill_ipst;
15815 
15816 	/*
15817 	 * Reset illgrp_ill_schednext if it was pointing at us.
15818 	 * We need to do this before we set ill_group to NULL.
15819 	 */
15820 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15821 	mutex_enter(&ill->ill_lock);
15822 
15823 	illgrp_reset_schednext(ill);
15824 
15825 	illgrp = ill->ill_group;
15826 
15827 	/* Delete the ill from illgrp. */
15828 	if (illgrp->illgrp_ill == ill) {
15829 		illgrp->illgrp_ill = ill->ill_group_next;
15830 	} else {
15831 		tmp_ill = illgrp->illgrp_ill;
15832 		while (tmp_ill->ill_group_next != ill) {
15833 			tmp_ill = tmp_ill->ill_group_next;
15834 			ASSERT(tmp_ill != NULL);
15835 		}
15836 		tmp_ill->ill_group_next = ill->ill_group_next;
15837 	}
15838 	ill->ill_group = NULL;
15839 	ill->ill_group_next = NULL;
15840 
15841 	illgrp->illgrp_ill_count--;
15842 	mutex_exit(&ill->ill_lock);
15843 	rw_exit(&ipst->ips_ill_g_lock);
15844 
15845 	/*
15846 	 * As this ill is leaving the group, we need to hand off
15847 	 * the responsibilities to the other ills in the group, if
15848 	 * this ill had some responsibilities.
15849 	 */
15850 
15851 	ill_handoff_responsibility(ill, illgrp);
15852 
15853 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15854 
15855 	if (illgrp->illgrp_ill_count == 0) {
15856 
15857 		ASSERT(illgrp->illgrp_ill == NULL);
15858 		if (ill->ill_isv6) {
15859 			if (illgrp == ipst->ips_illgrp_head_v6) {
15860 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15861 			} else {
15862 				tmpg = ipst->ips_illgrp_head_v6;
15863 				while (tmpg->illgrp_next != illgrp) {
15864 					tmpg = tmpg->illgrp_next;
15865 					ASSERT(tmpg != NULL);
15866 				}
15867 				tmpg->illgrp_next = illgrp->illgrp_next;
15868 			}
15869 		} else {
15870 			if (illgrp == ipst->ips_illgrp_head_v4) {
15871 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15872 			} else {
15873 				tmpg = ipst->ips_illgrp_head_v4;
15874 				while (tmpg->illgrp_next != illgrp) {
15875 					tmpg = tmpg->illgrp_next;
15876 					ASSERT(tmpg != NULL);
15877 				}
15878 				tmpg->illgrp_next = illgrp->illgrp_next;
15879 			}
15880 		}
15881 		mutex_destroy(&illgrp->illgrp_lock);
15882 		mi_free(illgrp);
15883 	}
15884 	rw_exit(&ipst->ips_ill_g_lock);
15885 
15886 	/*
15887 	 * Even though the ill is out of the group its not necessary
15888 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15889 	 * We will split the ipsq when phyint_groupname is set to NULL.
15890 	 */
15891 
15892 	/*
15893 	 * Send a routing sockets message if we are deleting from
15894 	 * groups with names.
15895 	 */
15896 	if (ill->ill_phyint->phyint_groupname_len != 0)
15897 		ip_rts_ifmsg(ill->ill_ipif);
15898 }
15899 
15900 /*
15901  * Re-do source address selection. This is normally called when
15902  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15903  * ipif comes up.
15904  */
15905 void
15906 ill_update_source_selection(ill_t *ill)
15907 {
15908 	ipif_t *ipif;
15909 
15910 	ASSERT(IAM_WRITER_ILL(ill));
15911 
15912 	if (ill->ill_group != NULL)
15913 		ill = ill->ill_group->illgrp_ill;
15914 
15915 	for (; ill != NULL; ill = ill->ill_group_next) {
15916 		for (ipif = ill->ill_ipif; ipif != NULL;
15917 		    ipif = ipif->ipif_next) {
15918 			if (ill->ill_isv6)
15919 				ipif_recreate_interface_routes_v6(NULL, ipif);
15920 			else
15921 				ipif_recreate_interface_routes(NULL, ipif);
15922 		}
15923 	}
15924 }
15925 
15926 /*
15927  * Insert ill in a group headed by illgrp_head. The caller can either
15928  * pass a groupname in which case we search for a group with the
15929  * same name to insert in or pass a group to insert in. This function
15930  * would only search groups with names.
15931  *
15932  * NOTE : The caller should make sure that there is at least one ipif
15933  *	  UP on this ill so that illgrp_scheduler can pick this ill
15934  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15935  *	  already sent a DL_UNBIND to the driver and we don't want to
15936  *	  send anymore packets. We don't assert for ipif_up_count
15937  *	  to be greater than zero, because ipif_up_done wants to call
15938  *	  this function before bumping up the ipif_up_count. See
15939  *	  ipif_up_done() for details.
15940  */
15941 int
15942 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15943     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15944 {
15945 	ill_group_t *illgrp;
15946 	ill_t *prev_ill;
15947 	phyint_t *phyi;
15948 	ip_stack_t	*ipst = ill->ill_ipst;
15949 
15950 	ASSERT(ill->ill_group == NULL);
15951 
15952 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15953 	mutex_enter(&ill->ill_lock);
15954 
15955 	if (groupname != NULL) {
15956 		/*
15957 		 * Look for a group with a matching groupname to insert.
15958 		 */
15959 		for (illgrp = *illgrp_head; illgrp != NULL;
15960 		    illgrp = illgrp->illgrp_next) {
15961 
15962 			ill_t *tmp_ill;
15963 
15964 			/*
15965 			 * If we have an ill_group_t in the list which has
15966 			 * no ill_t assigned then we must be in the process of
15967 			 * removing this group. We skip this as illgrp_delete()
15968 			 * will remove it from the list.
15969 			 */
15970 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15971 				ASSERT(illgrp->illgrp_ill_count == 0);
15972 				continue;
15973 			}
15974 
15975 			ASSERT(tmp_ill->ill_phyint != NULL);
15976 			phyi = tmp_ill->ill_phyint;
15977 			/*
15978 			 * Look at groups which has names only.
15979 			 */
15980 			if (phyi->phyint_groupname_len == 0)
15981 				continue;
15982 			/*
15983 			 * Names are stored in the phyint common to both
15984 			 * IPv4 and IPv6.
15985 			 */
15986 			if (mi_strcmp(phyi->phyint_groupname,
15987 			    groupname) == 0) {
15988 				break;
15989 			}
15990 		}
15991 	} else {
15992 		/*
15993 		 * If the caller passes in a NULL "grp_to_insert", we
15994 		 * allocate one below and insert this singleton.
15995 		 */
15996 		illgrp = grp_to_insert;
15997 	}
15998 
15999 	ill->ill_group_next = NULL;
16000 
16001 	if (illgrp == NULL) {
16002 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16003 		if (illgrp == NULL) {
16004 			return (ENOMEM);
16005 		}
16006 		illgrp->illgrp_next = *illgrp_head;
16007 		*illgrp_head = illgrp;
16008 		illgrp->illgrp_ill = ill;
16009 		illgrp->illgrp_ill_count = 1;
16010 		ill->ill_group = illgrp;
16011 		/*
16012 		 * Used in illgrp_scheduler to protect multiple threads
16013 		 * from traversing the list.
16014 		 */
16015 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16016 	} else {
16017 		ASSERT(ill->ill_net_type ==
16018 		    illgrp->illgrp_ill->ill_net_type);
16019 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16020 
16021 		/* Insert ill at tail of this group */
16022 		prev_ill = illgrp->illgrp_ill;
16023 		while (prev_ill->ill_group_next != NULL)
16024 			prev_ill = prev_ill->ill_group_next;
16025 		prev_ill->ill_group_next = ill;
16026 		ill->ill_group = illgrp;
16027 		illgrp->illgrp_ill_count++;
16028 		/*
16029 		 * Inherit group properties. Currently only forwarding
16030 		 * is the property we try to keep the same with all the
16031 		 * ills. When there are more, we will abstract this into
16032 		 * a function.
16033 		 */
16034 		ill->ill_flags &= ~ILLF_ROUTER;
16035 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16036 	}
16037 	mutex_exit(&ill->ill_lock);
16038 	rw_exit(&ipst->ips_ill_g_lock);
16039 
16040 	/*
16041 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16042 	 *    may be zero as it has not yet been bumped. But the ires
16043 	 *    have already been added. So, we do the nomination here
16044 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16045 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16046 	 *    ill_ipif_up_count here while nominating broadcast ires for
16047 	 *    receive.
16048 	 *
16049 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16050 	 *    to group them properly as ire_add() has already happened
16051 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16052 	 *    case, we need to do it here anyway.
16053 	 */
16054 	if (!ill->ill_isv6) {
16055 		ill_group_bcast_for_xmit(ill);
16056 		ill_nominate_bcast_rcv(illgrp);
16057 	}
16058 
16059 	if (!ipif_is_coming_up) {
16060 		/*
16061 		 * When ipif_up_done() calls this function, the multicast
16062 		 * groups have not been joined yet. So, there is no point in
16063 		 * nomination. ip_join_allmulti will handle groups when
16064 		 * ill_recover_multicast is called from ipif_up_done() later.
16065 		 */
16066 		(void) ill_nominate_mcast_rcv(illgrp);
16067 		/*
16068 		 * ipif_up_done calls ill_update_source_selection
16069 		 * anyway. Moreover, we don't want to re-create
16070 		 * interface routes while ipif_up_done() still has reference
16071 		 * to them. Refer to ipif_up_done() for more details.
16072 		 */
16073 		ill_update_source_selection(ill);
16074 	}
16075 
16076 	/*
16077 	 * Send a routing sockets message if we are inserting into
16078 	 * groups with names.
16079 	 */
16080 	if (groupname != NULL)
16081 		ip_rts_ifmsg(ill->ill_ipif);
16082 	return (0);
16083 }
16084 
16085 /*
16086  * Return the first phyint matching the groupname. There could
16087  * be more than one when there are ill groups.
16088  *
16089  * If 'usable' is set, then we exclude ones that are marked with any of
16090  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16091  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16092  * emulation of ipmp.
16093  */
16094 phyint_t *
16095 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16096 {
16097 	phyint_t *phyi;
16098 
16099 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16100 	/*
16101 	 * Group names are stored in the phyint - a common structure
16102 	 * to both IPv4 and IPv6.
16103 	 */
16104 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16105 	for (; phyi != NULL;
16106 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16107 	    phyi, AVL_AFTER)) {
16108 		if (phyi->phyint_groupname_len == 0)
16109 			continue;
16110 		/*
16111 		 * Skip the ones that should not be used since the callers
16112 		 * sometime use this for sending packets.
16113 		 */
16114 		if (usable && (phyi->phyint_flags &
16115 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16116 			continue;
16117 
16118 		ASSERT(phyi->phyint_groupname != NULL);
16119 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16120 			return (phyi);
16121 	}
16122 	return (NULL);
16123 }
16124 
16125 
16126 /*
16127  * Return the first usable phyint matching the group index. By 'usable'
16128  * we exclude ones that are marked ununsable with any of
16129  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16130  *
16131  * Used only for the ipmp/netinfo emulation of ipmp.
16132  */
16133 phyint_t *
16134 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16135 {
16136 	phyint_t *phyi;
16137 
16138 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16139 
16140 	if (!ipst->ips_ipmp_hook_emulation)
16141 		return (NULL);
16142 
16143 	/*
16144 	 * Group indicies are stored in the phyint - a common structure
16145 	 * to both IPv4 and IPv6.
16146 	 */
16147 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16148 	for (; phyi != NULL;
16149 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16150 	    phyi, AVL_AFTER)) {
16151 		/* Ignore the ones that do not have a group */
16152 		if (phyi->phyint_groupname_len == 0)
16153 			continue;
16154 
16155 		ASSERT(phyi->phyint_group_ifindex != 0);
16156 		/*
16157 		 * Skip the ones that should not be used since the callers
16158 		 * sometime use this for sending packets.
16159 		 */
16160 		if (phyi->phyint_flags &
16161 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16162 			continue;
16163 		if (phyi->phyint_group_ifindex == group_ifindex)
16164 			return (phyi);
16165 	}
16166 	return (NULL);
16167 }
16168 
16169 
16170 /*
16171  * MT notes on creation and deletion of IPMP groups
16172  *
16173  * Creation and deletion of IPMP groups introduce the need to merge or
16174  * split the associated serialization objects i.e the ipsq's. Normally all
16175  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16176  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16177  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16178  * is a need to change the <ill-ipsq> association and we have to operate on both
16179  * the source and destination IPMP groups. For eg. attempting to set the
16180  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16181  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16182  * source or destination IPMP group are mapped to a single ipsq for executing
16183  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16184  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16185  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16186  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16187  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16188  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16189  *
16190  * In the above example the ioctl handling code locates the current ipsq of hme0
16191  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16192  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16193  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16194  * the destination ipsq. If the destination ipsq is not busy, it also enters
16195  * the destination ipsq exclusively. Now the actual groupname setting operation
16196  * can proceed. If the destination ipsq is busy, the operation is enqueued
16197  * on the destination (merged) ipsq and will be handled in the unwind from
16198  * ipsq_exit.
16199  *
16200  * To prevent other threads accessing the ill while the group name change is
16201  * in progres, we bring down the ipifs which also removes the ill from the
16202  * group. The group is changed in phyint and when the first ipif on the ill
16203  * is brought up, the ill is inserted into the right IPMP group by
16204  * illgrp_insert.
16205  */
16206 /* ARGSUSED */
16207 int
16208 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16209     ip_ioctl_cmd_t *ipip, void *ifreq)
16210 {
16211 	int i;
16212 	char *tmp;
16213 	int namelen;
16214 	ill_t *ill = ipif->ipif_ill;
16215 	ill_t *ill_v4, *ill_v6;
16216 	int err = 0;
16217 	phyint_t *phyi;
16218 	phyint_t *phyi_tmp;
16219 	struct lifreq *lifr;
16220 	mblk_t	*mp1;
16221 	char *groupname;
16222 	ipsq_t *ipsq;
16223 	ip_stack_t	*ipst = ill->ill_ipst;
16224 
16225 	ASSERT(IAM_WRITER_IPIF(ipif));
16226 
16227 	/* Existance verified in ip_wput_nondata */
16228 	mp1 = mp->b_cont->b_cont;
16229 	lifr = (struct lifreq *)mp1->b_rptr;
16230 	groupname = lifr->lifr_groupname;
16231 
16232 	if (ipif->ipif_id != 0)
16233 		return (EINVAL);
16234 
16235 	phyi = ill->ill_phyint;
16236 	ASSERT(phyi != NULL);
16237 
16238 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16239 		return (EINVAL);
16240 
16241 	tmp = groupname;
16242 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16243 		;
16244 
16245 	if (i == LIFNAMSIZ) {
16246 		/* no null termination */
16247 		return (EINVAL);
16248 	}
16249 
16250 	/*
16251 	 * Calculate the namelen exclusive of the null
16252 	 * termination character.
16253 	 */
16254 	namelen = tmp - groupname;
16255 
16256 	ill_v4 = phyi->phyint_illv4;
16257 	ill_v6 = phyi->phyint_illv6;
16258 
16259 	/*
16260 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16261 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16262 	 * synchronization notes in ip.c
16263 	 */
16264 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16265 		return (EINVAL);
16266 	}
16267 
16268 	/*
16269 	 * mark the ill as changing.
16270 	 * this should queue all new requests on the syncq.
16271 	 */
16272 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16273 
16274 	if (ill_v4 != NULL)
16275 		ill_v4->ill_state_flags |= ILL_CHANGING;
16276 	if (ill_v6 != NULL)
16277 		ill_v6->ill_state_flags |= ILL_CHANGING;
16278 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16279 
16280 	if (namelen == 0) {
16281 		/*
16282 		 * Null string means remove this interface from the
16283 		 * existing group.
16284 		 */
16285 		if (phyi->phyint_groupname_len == 0) {
16286 			/*
16287 			 * Never was in a group.
16288 			 */
16289 			err = 0;
16290 			goto done;
16291 		}
16292 
16293 		/*
16294 		 * IPv4 or IPv6 may be temporarily out of the group when all
16295 		 * the ipifs are down. Thus, we need to check for ill_group to
16296 		 * be non-NULL.
16297 		 */
16298 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16299 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16300 			mutex_enter(&ill_v4->ill_lock);
16301 			if (!ill_is_quiescent(ill_v4)) {
16302 				/*
16303 				 * ipsq_pending_mp_add will not fail since
16304 				 * connp is NULL
16305 				 */
16306 				(void) ipsq_pending_mp_add(NULL,
16307 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16308 				mutex_exit(&ill_v4->ill_lock);
16309 				err = EINPROGRESS;
16310 				goto done;
16311 			}
16312 			mutex_exit(&ill_v4->ill_lock);
16313 		}
16314 
16315 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16316 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16317 			mutex_enter(&ill_v6->ill_lock);
16318 			if (!ill_is_quiescent(ill_v6)) {
16319 				(void) ipsq_pending_mp_add(NULL,
16320 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16321 				mutex_exit(&ill_v6->ill_lock);
16322 				err = EINPROGRESS;
16323 				goto done;
16324 			}
16325 			mutex_exit(&ill_v6->ill_lock);
16326 		}
16327 
16328 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16329 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16330 		mutex_enter(&phyi->phyint_lock);
16331 		ASSERT(phyi->phyint_groupname != NULL);
16332 		mi_free(phyi->phyint_groupname);
16333 		phyi->phyint_groupname = NULL;
16334 		phyi->phyint_groupname_len = 0;
16335 
16336 		/* Restore the ifindex used to be the per interface one */
16337 		phyi->phyint_group_ifindex = 0;
16338 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16339 		mutex_exit(&phyi->phyint_lock);
16340 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16341 		rw_exit(&ipst->ips_ill_g_lock);
16342 		err = ill_up_ipifs(ill, q, mp);
16343 
16344 		/*
16345 		 * set the split flag so that the ipsq can be split
16346 		 */
16347 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16348 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16349 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16350 
16351 	} else {
16352 		if (phyi->phyint_groupname_len != 0) {
16353 			ASSERT(phyi->phyint_groupname != NULL);
16354 			/* Are we inserting in the same group ? */
16355 			if (mi_strcmp(groupname,
16356 			    phyi->phyint_groupname) == 0) {
16357 				err = 0;
16358 				goto done;
16359 			}
16360 		}
16361 
16362 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16363 		/*
16364 		 * Merge ipsq for the group's.
16365 		 * This check is here as multiple groups/ills might be
16366 		 * sharing the same ipsq.
16367 		 * If we have to merege than the operation is restarted
16368 		 * on the new ipsq.
16369 		 */
16370 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16371 		if (phyi->phyint_ipsq != ipsq) {
16372 			rw_exit(&ipst->ips_ill_g_lock);
16373 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16374 			goto done;
16375 		}
16376 		/*
16377 		 * Running exclusive on new ipsq.
16378 		 */
16379 
16380 		ASSERT(ipsq != NULL);
16381 		ASSERT(ipsq->ipsq_writer == curthread);
16382 
16383 		/*
16384 		 * Check whether the ill_type and ill_net_type matches before
16385 		 * we allocate any memory so that the cleanup is easier.
16386 		 *
16387 		 * We can't group dissimilar ones as we can't load spread
16388 		 * packets across the group because of potential link-level
16389 		 * header differences.
16390 		 */
16391 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16392 		if (phyi_tmp != NULL) {
16393 			if ((ill_v4 != NULL &&
16394 			    phyi_tmp->phyint_illv4 != NULL) &&
16395 			    ((ill_v4->ill_net_type !=
16396 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16397 			    (ill_v4->ill_type !=
16398 			    phyi_tmp->phyint_illv4->ill_type))) {
16399 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16400 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16401 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16402 				rw_exit(&ipst->ips_ill_g_lock);
16403 				return (EINVAL);
16404 			}
16405 			if ((ill_v6 != NULL &&
16406 			    phyi_tmp->phyint_illv6 != NULL) &&
16407 			    ((ill_v6->ill_net_type !=
16408 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16409 			    (ill_v6->ill_type !=
16410 			    phyi_tmp->phyint_illv6->ill_type))) {
16411 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16412 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16413 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16414 				rw_exit(&ipst->ips_ill_g_lock);
16415 				return (EINVAL);
16416 			}
16417 		}
16418 
16419 		rw_exit(&ipst->ips_ill_g_lock);
16420 
16421 		/*
16422 		 * bring down all v4 ipifs.
16423 		 */
16424 		if (ill_v4 != NULL) {
16425 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16426 		}
16427 
16428 		/*
16429 		 * bring down all v6 ipifs.
16430 		 */
16431 		if (ill_v6 != NULL) {
16432 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16433 		}
16434 
16435 		/*
16436 		 * make sure all ipifs are down and there are no active
16437 		 * references. Call to ipsq_pending_mp_add will not fail
16438 		 * since connp is NULL.
16439 		 */
16440 		if (ill_v4 != NULL) {
16441 			mutex_enter(&ill_v4->ill_lock);
16442 			if (!ill_is_quiescent(ill_v4)) {
16443 				(void) ipsq_pending_mp_add(NULL,
16444 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16445 				mutex_exit(&ill_v4->ill_lock);
16446 				err = EINPROGRESS;
16447 				goto done;
16448 			}
16449 			mutex_exit(&ill_v4->ill_lock);
16450 		}
16451 
16452 		if (ill_v6 != NULL) {
16453 			mutex_enter(&ill_v6->ill_lock);
16454 			if (!ill_is_quiescent(ill_v6)) {
16455 				(void) ipsq_pending_mp_add(NULL,
16456 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16457 				mutex_exit(&ill_v6->ill_lock);
16458 				err = EINPROGRESS;
16459 				goto done;
16460 			}
16461 			mutex_exit(&ill_v6->ill_lock);
16462 		}
16463 
16464 		/*
16465 		 * allocate including space for null terminator
16466 		 * before we insert.
16467 		 */
16468 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16469 		if (tmp == NULL)
16470 			return (ENOMEM);
16471 
16472 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16473 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16474 		mutex_enter(&phyi->phyint_lock);
16475 		if (phyi->phyint_groupname_len != 0) {
16476 			ASSERT(phyi->phyint_groupname != NULL);
16477 			mi_free(phyi->phyint_groupname);
16478 		}
16479 
16480 		/*
16481 		 * setup the new group name.
16482 		 */
16483 		phyi->phyint_groupname = tmp;
16484 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16485 		phyi->phyint_groupname_len = namelen + 1;
16486 
16487 		if (ipst->ips_ipmp_hook_emulation) {
16488 			/*
16489 			 * If the group already exists we use the existing
16490 			 * group_ifindex, otherwise we pick a new index here.
16491 			 */
16492 			if (phyi_tmp != NULL) {
16493 				phyi->phyint_group_ifindex =
16494 				    phyi_tmp->phyint_group_ifindex;
16495 			} else {
16496 				/* XXX We need a recovery strategy here. */
16497 				if (!ip_assign_ifindex(
16498 				    &phyi->phyint_group_ifindex, ipst))
16499 					cmn_err(CE_PANIC,
16500 					    "ip_assign_ifindex() failed");
16501 			}
16502 		}
16503 		/*
16504 		 * Select whether the netinfo and hook use the per-interface
16505 		 * or per-group ifindex.
16506 		 */
16507 		if (ipst->ips_ipmp_hook_emulation)
16508 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16509 		else
16510 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16511 
16512 		if (ipst->ips_ipmp_hook_emulation &&
16513 		    phyi_tmp != NULL) {
16514 			/* First phyint in group - group PLUMB event */
16515 			ill_nic_info_plumb(ill, B_TRUE);
16516 		}
16517 		mutex_exit(&phyi->phyint_lock);
16518 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16519 		rw_exit(&ipst->ips_ill_g_lock);
16520 
16521 		err = ill_up_ipifs(ill, q, mp);
16522 	}
16523 
16524 done:
16525 	/*
16526 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16527 	 */
16528 	if (err != EINPROGRESS) {
16529 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16530 		if (ill_v4 != NULL)
16531 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16532 		if (ill_v6 != NULL)
16533 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16534 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16535 	}
16536 	return (err);
16537 }
16538 
16539 /* ARGSUSED */
16540 int
16541 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16542     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16543 {
16544 	ill_t *ill;
16545 	phyint_t *phyi;
16546 	struct lifreq *lifr;
16547 	mblk_t	*mp1;
16548 
16549 	/* Existence verified in ip_wput_nondata */
16550 	mp1 = mp->b_cont->b_cont;
16551 	lifr = (struct lifreq *)mp1->b_rptr;
16552 	ill = ipif->ipif_ill;
16553 	phyi = ill->ill_phyint;
16554 
16555 	lifr->lifr_groupname[0] = '\0';
16556 	/*
16557 	 * ill_group may be null if all the interfaces
16558 	 * are down. But still, the phyint should always
16559 	 * hold the name.
16560 	 */
16561 	if (phyi->phyint_groupname_len != 0) {
16562 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16563 		    phyi->phyint_groupname_len);
16564 	}
16565 
16566 	return (0);
16567 }
16568 
16569 
16570 typedef struct conn_move_s {
16571 	ill_t	*cm_from_ill;
16572 	ill_t	*cm_to_ill;
16573 	int	cm_ifindex;
16574 } conn_move_t;
16575 
16576 /*
16577  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16578  */
16579 static void
16580 conn_move(conn_t *connp, caddr_t arg)
16581 {
16582 	conn_move_t *connm;
16583 	int ifindex;
16584 	int i;
16585 	ill_t *from_ill;
16586 	ill_t *to_ill;
16587 	ilg_t *ilg;
16588 	ilm_t *ret_ilm;
16589 
16590 	connm = (conn_move_t *)arg;
16591 	ifindex = connm->cm_ifindex;
16592 	from_ill = connm->cm_from_ill;
16593 	to_ill = connm->cm_to_ill;
16594 
16595 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16596 
16597 	/* All multicast fields protected by conn_lock */
16598 	mutex_enter(&connp->conn_lock);
16599 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16600 	if ((connp->conn_outgoing_ill == from_ill) &&
16601 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16602 		connp->conn_outgoing_ill = to_ill;
16603 		connp->conn_incoming_ill = to_ill;
16604 	}
16605 
16606 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16607 
16608 	if ((connp->conn_multicast_ill == from_ill) &&
16609 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16610 		connp->conn_multicast_ill = connm->cm_to_ill;
16611 	}
16612 
16613 	/* Change IP_XMIT_IF associations */
16614 	if ((connp->conn_xmit_if_ill == from_ill) &&
16615 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
16616 		connp->conn_xmit_if_ill = to_ill;
16617 	}
16618 	/*
16619 	 * Change the ilg_ill to point to the new one. This assumes
16620 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16621 	 * has been told to receive packets on this interface.
16622 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16623 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16624 	 * some ilms may not have moved. We check to see whether
16625 	 * the ilms have moved to to_ill. We can't check on from_ill
16626 	 * as in the process of moving, we could have split an ilm
16627 	 * in to two - which has the same orig_ifindex and v6group.
16628 	 *
16629 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16630 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16631 	 */
16632 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16633 		ilg = &connp->conn_ilg[i];
16634 		if ((ilg->ilg_ill == from_ill) &&
16635 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16636 			/* ifindex != 0 indicates failback */
16637 			if (ifindex != 0) {
16638 				connp->conn_ilg[i].ilg_ill = to_ill;
16639 				continue;
16640 			}
16641 
16642 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16643 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16644 			    connp->conn_zoneid);
16645 
16646 			if (ret_ilm != NULL)
16647 				connp->conn_ilg[i].ilg_ill = to_ill;
16648 		}
16649 	}
16650 	mutex_exit(&connp->conn_lock);
16651 }
16652 
16653 static void
16654 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16655 {
16656 	conn_move_t connm;
16657 	ip_stack_t	*ipst = from_ill->ill_ipst;
16658 
16659 	connm.cm_from_ill = from_ill;
16660 	connm.cm_to_ill = to_ill;
16661 	connm.cm_ifindex = ifindex;
16662 
16663 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16664 }
16665 
16666 /*
16667  * ilm has been moved from from_ill to to_ill.
16668  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16669  * appropriately.
16670  *
16671  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16672  *	  the code there de-references ipif_ill to get the ill to
16673  *	  send multicast requests. It does not work as ipif is on its
16674  *	  move and already moved when this function is called.
16675  *	  Thus, we need to use from_ill and to_ill send down multicast
16676  *	  requests.
16677  */
16678 static void
16679 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16680 {
16681 	ipif_t *ipif;
16682 	ilm_t *ilm;
16683 
16684 	/*
16685 	 * See whether we need to send down DL_ENABMULTI_REQ on
16686 	 * to_ill as ilm has just been added.
16687 	 */
16688 	ASSERT(IAM_WRITER_ILL(to_ill));
16689 	ASSERT(IAM_WRITER_ILL(from_ill));
16690 
16691 	ILM_WALKER_HOLD(to_ill);
16692 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16693 
16694 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16695 			continue;
16696 		/*
16697 		 * no locks held, ill/ipif cannot dissappear as long
16698 		 * as we are writer.
16699 		 */
16700 		ipif = to_ill->ill_ipif;
16701 		/*
16702 		 * No need to hold any lock as we are the writer and this
16703 		 * can only be changed by a writer.
16704 		 */
16705 		ilm->ilm_is_new = B_FALSE;
16706 
16707 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16708 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16709 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16710 			    "resolver\n"));
16711 			continue;		/* Must be IRE_IF_NORESOLVER */
16712 		}
16713 
16714 
16715 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16716 			ip1dbg(("ilm_send_multicast_reqs: "
16717 			    "to_ill MULTI_BCAST\n"));
16718 			goto from;
16719 		}
16720 
16721 		if (to_ill->ill_isv6)
16722 			mld_joingroup(ilm);
16723 		else
16724 			igmp_joingroup(ilm);
16725 
16726 		if (to_ill->ill_ipif_up_count == 0) {
16727 			/*
16728 			 * Nobody there. All multicast addresses will be
16729 			 * re-joined when we get the DL_BIND_ACK bringing the
16730 			 * interface up.
16731 			 */
16732 			ilm->ilm_notify_driver = B_FALSE;
16733 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16734 			goto from;
16735 		}
16736 
16737 		/*
16738 		 * For allmulti address, we want to join on only one interface.
16739 		 * Checking for ilm_numentries_v6 is not correct as you may
16740 		 * find an ilm with zero address on to_ill, but we may not
16741 		 * have nominated to_ill for receiving. Thus, if we have
16742 		 * nominated from_ill (ill_join_allmulti is set), nominate
16743 		 * only if to_ill is not already nominated (to_ill normally
16744 		 * should not have been nominated if "from_ill" has already
16745 		 * been nominated. As we don't prevent failovers from happening
16746 		 * across groups, we don't assert).
16747 		 */
16748 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16749 			/*
16750 			 * There is no need to hold ill locks as we are
16751 			 * writer on both ills and when ill_join_allmulti
16752 			 * is changed the thread is always a writer.
16753 			 */
16754 			if (from_ill->ill_join_allmulti &&
16755 			    !to_ill->ill_join_allmulti) {
16756 				(void) ip_join_allmulti(to_ill->ill_ipif);
16757 			}
16758 		} else if (ilm->ilm_notify_driver) {
16759 
16760 			/*
16761 			 * This is a newly moved ilm so we need to tell the
16762 			 * driver about the new group. There can be more than
16763 			 * one ilm's for the same group in the list each with a
16764 			 * different orig_ifindex. We have to inform the driver
16765 			 * once. In ilm_move_v[4,6] we only set the flag
16766 			 * ilm_notify_driver for the first ilm.
16767 			 */
16768 
16769 			(void) ip_ll_send_enabmulti_req(to_ill,
16770 			    &ilm->ilm_v6addr);
16771 		}
16772 
16773 		ilm->ilm_notify_driver = B_FALSE;
16774 
16775 		/*
16776 		 * See whether we need to send down DL_DISABMULTI_REQ on
16777 		 * from_ill as ilm has just been removed.
16778 		 */
16779 from:
16780 		ipif = from_ill->ill_ipif;
16781 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16782 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16783 			ip1dbg(("ilm_send_multicast_reqs: "
16784 			    "from_ill not resolver\n"));
16785 			continue;		/* Must be IRE_IF_NORESOLVER */
16786 		}
16787 
16788 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16789 			ip1dbg(("ilm_send_multicast_reqs: "
16790 			    "from_ill MULTI_BCAST\n"));
16791 			continue;
16792 		}
16793 
16794 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16795 			if (from_ill->ill_join_allmulti)
16796 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16797 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16798 			(void) ip_ll_send_disabmulti_req(from_ill,
16799 			    &ilm->ilm_v6addr);
16800 		}
16801 	}
16802 	ILM_WALKER_RELE(to_ill);
16803 }
16804 
16805 /*
16806  * This function is called when all multicast memberships needs
16807  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16808  * called only once unlike the IPv4 counterpart where it is called after
16809  * every logical interface is moved. The reason is due to multicast
16810  * memberships are joined using an interface address in IPv4 while in
16811  * IPv6, interface index is used.
16812  */
16813 static void
16814 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16815 {
16816 	ilm_t	*ilm;
16817 	ilm_t	*ilm_next;
16818 	ilm_t	*new_ilm;
16819 	ilm_t	**ilmp;
16820 	int	count;
16821 	char buf[INET6_ADDRSTRLEN];
16822 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16823 	ip_stack_t	*ipst = from_ill->ill_ipst;
16824 
16825 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16826 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16827 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16828 
16829 	if (ifindex == 0) {
16830 		/*
16831 		 * Form the solicited node mcast address which is used later.
16832 		 */
16833 		ipif_t *ipif;
16834 
16835 		ipif = from_ill->ill_ipif;
16836 		ASSERT(ipif->ipif_id == 0);
16837 
16838 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16839 	}
16840 
16841 	ilmp = &from_ill->ill_ilm;
16842 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16843 		ilm_next = ilm->ilm_next;
16844 
16845 		if (ilm->ilm_flags & ILM_DELETED) {
16846 			ilmp = &ilm->ilm_next;
16847 			continue;
16848 		}
16849 
16850 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16851 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16852 		ASSERT(ilm->ilm_orig_ifindex != 0);
16853 		if (ilm->ilm_orig_ifindex == ifindex) {
16854 			/*
16855 			 * We are failing back multicast memberships.
16856 			 * If the same ilm exists in to_ill, it means somebody
16857 			 * has joined the same group there e.g. ff02::1
16858 			 * is joined within the kernel when the interfaces
16859 			 * came UP.
16860 			 */
16861 			ASSERT(ilm->ilm_ipif == NULL);
16862 			if (new_ilm != NULL) {
16863 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16864 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16865 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16866 					new_ilm->ilm_is_new = B_TRUE;
16867 				}
16868 			} else {
16869 				/*
16870 				 * check if we can just move the ilm
16871 				 */
16872 				if (from_ill->ill_ilm_walker_cnt != 0) {
16873 					/*
16874 					 * We have walkers we cannot move
16875 					 * the ilm, so allocate a new ilm,
16876 					 * this (old) ilm will be marked
16877 					 * ILM_DELETED at the end of the loop
16878 					 * and will be freed when the
16879 					 * last walker exits.
16880 					 */
16881 					new_ilm = (ilm_t *)mi_zalloc
16882 					    (sizeof (ilm_t));
16883 					if (new_ilm == NULL) {
16884 						ip0dbg(("ilm_move_v6: "
16885 						    "FAILBACK of IPv6"
16886 						    " multicast address %s : "
16887 						    "from %s to"
16888 						    " %s failed : ENOMEM \n",
16889 						    inet_ntop(AF_INET6,
16890 						    &ilm->ilm_v6addr, buf,
16891 						    sizeof (buf)),
16892 						    from_ill->ill_name,
16893 						    to_ill->ill_name));
16894 
16895 							ilmp = &ilm->ilm_next;
16896 							continue;
16897 					}
16898 					*new_ilm = *ilm;
16899 					/*
16900 					 * we don't want new_ilm linked to
16901 					 * ilm's filter list.
16902 					 */
16903 					new_ilm->ilm_filter = NULL;
16904 				} else {
16905 					/*
16906 					 * No walkers we can move the ilm.
16907 					 * lets take it out of the list.
16908 					 */
16909 					*ilmp = ilm->ilm_next;
16910 					ilm->ilm_next = NULL;
16911 					new_ilm = ilm;
16912 				}
16913 
16914 				/*
16915 				 * if this is the first ilm for the group
16916 				 * set ilm_notify_driver so that we notify the
16917 				 * driver in ilm_send_multicast_reqs.
16918 				 */
16919 				if (ilm_lookup_ill_v6(to_ill,
16920 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16921 					new_ilm->ilm_notify_driver = B_TRUE;
16922 
16923 				new_ilm->ilm_ill = to_ill;
16924 				/* Add to the to_ill's list */
16925 				new_ilm->ilm_next = to_ill->ill_ilm;
16926 				to_ill->ill_ilm = new_ilm;
16927 				/*
16928 				 * set the flag so that mld_joingroup is
16929 				 * called in ilm_send_multicast_reqs().
16930 				 */
16931 				new_ilm->ilm_is_new = B_TRUE;
16932 			}
16933 			goto bottom;
16934 		} else if (ifindex != 0) {
16935 			/*
16936 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16937 			 * has not matched above, look at the next ilm.
16938 			 */
16939 			ilmp = &ilm->ilm_next;
16940 			continue;
16941 		}
16942 		/*
16943 		 * If we are here, it means ifindex is 0. Failover
16944 		 * everything.
16945 		 *
16946 		 * We need to handle solicited node mcast address
16947 		 * and all_nodes mcast address differently as they
16948 		 * are joined witin the kenrel (ipif_multicast_up)
16949 		 * and potentially from the userland. We are called
16950 		 * after the ipifs of from_ill has been moved.
16951 		 * If we still find ilms on ill with solicited node
16952 		 * mcast address or all_nodes mcast address, it must
16953 		 * belong to the UP interface that has not moved e.g.
16954 		 * ipif_id 0 with the link local prefix does not move.
16955 		 * We join this on the new ill accounting for all the
16956 		 * userland memberships so that applications don't
16957 		 * see any failure.
16958 		 *
16959 		 * We need to make sure that we account only for the
16960 		 * solicited node and all node multicast addresses
16961 		 * that was brought UP on these. In the case of
16962 		 * a failover from A to B, we might have ilms belonging
16963 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16964 		 * for the membership from the userland. If we are failing
16965 		 * over from B to C now, we will find the ones belonging
16966 		 * to A on B. These don't account for the ill_ipif_up_count.
16967 		 * They just move from B to C. The check below on
16968 		 * ilm_orig_ifindex ensures that.
16969 		 */
16970 		if ((ilm->ilm_orig_ifindex ==
16971 		    from_ill->ill_phyint->phyint_ifindex) &&
16972 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16973 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16974 		    &ilm->ilm_v6addr))) {
16975 			ASSERT(ilm->ilm_refcnt > 0);
16976 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16977 			/*
16978 			 * For indentation reasons, we are not using a
16979 			 * "else" here.
16980 			 */
16981 			if (count == 0) {
16982 				ilmp = &ilm->ilm_next;
16983 				continue;
16984 			}
16985 			ilm->ilm_refcnt -= count;
16986 			if (new_ilm != NULL) {
16987 				/*
16988 				 * Can find one with the same
16989 				 * ilm_orig_ifindex, if we are failing
16990 				 * over to a STANDBY. This happens
16991 				 * when somebody wants to join a group
16992 				 * on a STANDBY interface and we
16993 				 * internally join on a different one.
16994 				 * If we had joined on from_ill then, a
16995 				 * failover now will find a new ilm
16996 				 * with this index.
16997 				 */
16998 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16999 				    " new ilm on %s, group address %s\n",
17000 				    to_ill->ill_name,
17001 				    inet_ntop(AF_INET6,
17002 				    &ilm->ilm_v6addr, buf,
17003 				    sizeof (buf))));
17004 				new_ilm->ilm_refcnt += count;
17005 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17006 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17007 					new_ilm->ilm_is_new = B_TRUE;
17008 				}
17009 			} else {
17010 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17011 				if (new_ilm == NULL) {
17012 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17013 					    " multicast address %s : from %s to"
17014 					    " %s failed : ENOMEM \n",
17015 					    inet_ntop(AF_INET6,
17016 					    &ilm->ilm_v6addr, buf,
17017 					    sizeof (buf)), from_ill->ill_name,
17018 					    to_ill->ill_name));
17019 					ilmp = &ilm->ilm_next;
17020 					continue;
17021 				}
17022 				*new_ilm = *ilm;
17023 				new_ilm->ilm_filter = NULL;
17024 				new_ilm->ilm_refcnt = count;
17025 				new_ilm->ilm_timer = INFINITY;
17026 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17027 				new_ilm->ilm_is_new = B_TRUE;
17028 				/*
17029 				 * If the to_ill has not joined this
17030 				 * group we need to tell the driver in
17031 				 * ill_send_multicast_reqs.
17032 				 */
17033 				if (ilm_lookup_ill_v6(to_ill,
17034 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17035 					new_ilm->ilm_notify_driver = B_TRUE;
17036 
17037 				new_ilm->ilm_ill = to_ill;
17038 				/* Add to the to_ill's list */
17039 				new_ilm->ilm_next = to_ill->ill_ilm;
17040 				to_ill->ill_ilm = new_ilm;
17041 				ASSERT(new_ilm->ilm_ipif == NULL);
17042 			}
17043 			if (ilm->ilm_refcnt == 0) {
17044 				goto bottom;
17045 			} else {
17046 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17047 				CLEAR_SLIST(new_ilm->ilm_filter);
17048 				ilmp = &ilm->ilm_next;
17049 			}
17050 			continue;
17051 		} else {
17052 			/*
17053 			 * ifindex = 0 means, move everything pointing at
17054 			 * from_ill. We are doing this becuase ill has
17055 			 * either FAILED or became INACTIVE.
17056 			 *
17057 			 * As we would like to move things later back to
17058 			 * from_ill, we want to retain the identity of this
17059 			 * ilm. Thus, we don't blindly increment the reference
17060 			 * count on the ilms matching the address alone. We
17061 			 * need to match on the ilm_orig_index also. new_ilm
17062 			 * was obtained by matching ilm_orig_index also.
17063 			 */
17064 			if (new_ilm != NULL) {
17065 				/*
17066 				 * This is possible only if a previous restore
17067 				 * was incomplete i.e restore to
17068 				 * ilm_orig_ifindex left some ilms because
17069 				 * of some failures. Thus when we are failing
17070 				 * again, we might find our old friends there.
17071 				 */
17072 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17073 				    " on %s, group address %s\n",
17074 				    to_ill->ill_name,
17075 				    inet_ntop(AF_INET6,
17076 				    &ilm->ilm_v6addr, buf,
17077 				    sizeof (buf))));
17078 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17079 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17080 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17081 					new_ilm->ilm_is_new = B_TRUE;
17082 				}
17083 			} else {
17084 				if (from_ill->ill_ilm_walker_cnt != 0) {
17085 					new_ilm = (ilm_t *)
17086 					    mi_zalloc(sizeof (ilm_t));
17087 					if (new_ilm == NULL) {
17088 						ip0dbg(("ilm_move_v6: "
17089 						    "FAILOVER of IPv6"
17090 						    " multicast address %s : "
17091 						    "from %s to"
17092 						    " %s failed : ENOMEM \n",
17093 						    inet_ntop(AF_INET6,
17094 						    &ilm->ilm_v6addr, buf,
17095 						    sizeof (buf)),
17096 						    from_ill->ill_name,
17097 						    to_ill->ill_name));
17098 
17099 							ilmp = &ilm->ilm_next;
17100 							continue;
17101 					}
17102 					*new_ilm = *ilm;
17103 					new_ilm->ilm_filter = NULL;
17104 				} else {
17105 					*ilmp = ilm->ilm_next;
17106 					new_ilm = ilm;
17107 				}
17108 				/*
17109 				 * If the to_ill has not joined this
17110 				 * group we need to tell the driver in
17111 				 * ill_send_multicast_reqs.
17112 				 */
17113 				if (ilm_lookup_ill_v6(to_ill,
17114 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17115 					new_ilm->ilm_notify_driver = B_TRUE;
17116 
17117 				/* Add to the to_ill's list */
17118 				new_ilm->ilm_next = to_ill->ill_ilm;
17119 				to_ill->ill_ilm = new_ilm;
17120 				ASSERT(ilm->ilm_ipif == NULL);
17121 				new_ilm->ilm_ill = to_ill;
17122 				new_ilm->ilm_is_new = B_TRUE;
17123 			}
17124 
17125 		}
17126 
17127 bottom:
17128 		/*
17129 		 * Revert multicast filter state to (EXCLUDE, NULL).
17130 		 * new_ilm->ilm_is_new should already be set if needed.
17131 		 */
17132 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17133 		CLEAR_SLIST(new_ilm->ilm_filter);
17134 		/*
17135 		 * We allocated/got a new ilm, free the old one.
17136 		 */
17137 		if (new_ilm != ilm) {
17138 			if (from_ill->ill_ilm_walker_cnt == 0) {
17139 				*ilmp = ilm->ilm_next;
17140 				ilm->ilm_next = NULL;
17141 				FREE_SLIST(ilm->ilm_filter);
17142 				FREE_SLIST(ilm->ilm_pendsrcs);
17143 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17144 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17145 				mi_free((char *)ilm);
17146 			} else {
17147 				ilm->ilm_flags |= ILM_DELETED;
17148 				from_ill->ill_ilm_cleanup_reqd = 1;
17149 				ilmp = &ilm->ilm_next;
17150 			}
17151 		}
17152 	}
17153 }
17154 
17155 /*
17156  * Move all the multicast memberships to to_ill. Called when
17157  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17158  * different from IPv6 counterpart as multicast memberships are associated
17159  * with ills in IPv6. This function is called after every ipif is moved
17160  * unlike IPv6, where it is moved only once.
17161  */
17162 static void
17163 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17164 {
17165 	ilm_t	*ilm;
17166 	ilm_t	*ilm_next;
17167 	ilm_t	*new_ilm;
17168 	ilm_t	**ilmp;
17169 	ip_stack_t	*ipst = from_ill->ill_ipst;
17170 
17171 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17172 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17173 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17174 
17175 	ilmp = &from_ill->ill_ilm;
17176 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17177 		ilm_next = ilm->ilm_next;
17178 
17179 		if (ilm->ilm_flags & ILM_DELETED) {
17180 			ilmp = &ilm->ilm_next;
17181 			continue;
17182 		}
17183 
17184 		ASSERT(ilm->ilm_ipif != NULL);
17185 
17186 		if (ilm->ilm_ipif != ipif) {
17187 			ilmp = &ilm->ilm_next;
17188 			continue;
17189 		}
17190 
17191 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17192 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17193 			new_ilm = ilm_lookup_ipif(ipif,
17194 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17195 			if (new_ilm != NULL) {
17196 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17197 				/*
17198 				 * We still need to deal with the from_ill.
17199 				 */
17200 				new_ilm->ilm_is_new = B_TRUE;
17201 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17202 				CLEAR_SLIST(new_ilm->ilm_filter);
17203 				goto delete_ilm;
17204 			}
17205 			/*
17206 			 * If we could not find one e.g. ipif is
17207 			 * still down on to_ill, we add this ilm
17208 			 * on ill_new to preserve the reference
17209 			 * count.
17210 			 */
17211 		}
17212 		/*
17213 		 * When ipifs move, ilms always move with it
17214 		 * to the NEW ill. Thus we should never be
17215 		 * able to find ilm till we really move it here.
17216 		 */
17217 		ASSERT(ilm_lookup_ipif(ipif,
17218 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17219 
17220 		if (from_ill->ill_ilm_walker_cnt != 0) {
17221 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17222 			if (new_ilm == NULL) {
17223 				char buf[INET6_ADDRSTRLEN];
17224 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17225 				    " multicast address %s : "
17226 				    "from %s to"
17227 				    " %s failed : ENOMEM \n",
17228 				    inet_ntop(AF_INET,
17229 				    &ilm->ilm_v6addr, buf,
17230 				    sizeof (buf)),
17231 				    from_ill->ill_name,
17232 				    to_ill->ill_name));
17233 
17234 				ilmp = &ilm->ilm_next;
17235 				continue;
17236 			}
17237 			*new_ilm = *ilm;
17238 			/* We don't want new_ilm linked to ilm's filter list */
17239 			new_ilm->ilm_filter = NULL;
17240 		} else {
17241 			/* Remove from the list */
17242 			*ilmp = ilm->ilm_next;
17243 			new_ilm = ilm;
17244 		}
17245 
17246 		/*
17247 		 * If we have never joined this group on the to_ill
17248 		 * make sure we tell the driver.
17249 		 */
17250 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17251 		    ALL_ZONES) == NULL)
17252 			new_ilm->ilm_notify_driver = B_TRUE;
17253 
17254 		/* Add to the to_ill's list */
17255 		new_ilm->ilm_next = to_ill->ill_ilm;
17256 		to_ill->ill_ilm = new_ilm;
17257 		new_ilm->ilm_is_new = B_TRUE;
17258 
17259 		/*
17260 		 * Revert multicast filter state to (EXCLUDE, NULL)
17261 		 */
17262 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17263 		CLEAR_SLIST(new_ilm->ilm_filter);
17264 
17265 		/*
17266 		 * Delete only if we have allocated a new ilm.
17267 		 */
17268 		if (new_ilm != ilm) {
17269 delete_ilm:
17270 			if (from_ill->ill_ilm_walker_cnt == 0) {
17271 				/* Remove from the list */
17272 				*ilmp = ilm->ilm_next;
17273 				ilm->ilm_next = NULL;
17274 				FREE_SLIST(ilm->ilm_filter);
17275 				FREE_SLIST(ilm->ilm_pendsrcs);
17276 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
17277 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
17278 				mi_free((char *)ilm);
17279 			} else {
17280 				ilm->ilm_flags |= ILM_DELETED;
17281 				from_ill->ill_ilm_cleanup_reqd = 1;
17282 				ilmp = &ilm->ilm_next;
17283 			}
17284 		}
17285 	}
17286 }
17287 
17288 static uint_t
17289 ipif_get_id(ill_t *ill, uint_t id)
17290 {
17291 	uint_t	unit;
17292 	ipif_t	*tipif;
17293 	boolean_t found = B_FALSE;
17294 	ip_stack_t	*ipst = ill->ill_ipst;
17295 
17296 	/*
17297 	 * During failback, we want to go back to the same id
17298 	 * instead of the smallest id so that the original
17299 	 * configuration is maintained. id is non-zero in that
17300 	 * case.
17301 	 */
17302 	if (id != 0) {
17303 		/*
17304 		 * While failing back, if we still have an ipif with
17305 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17306 		 * as soon as we return from this function. It was
17307 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17308 		 * we can choose the smallest id. Thus we return zero
17309 		 * in that case ignoring the hint.
17310 		 */
17311 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17312 			return (0);
17313 		for (tipif = ill->ill_ipif; tipif != NULL;
17314 		    tipif = tipif->ipif_next) {
17315 			if (tipif->ipif_id == id) {
17316 				found = B_TRUE;
17317 				break;
17318 			}
17319 		}
17320 		/*
17321 		 * If somebody already plumbed another logical
17322 		 * with the same id, we won't be able to find it.
17323 		 */
17324 		if (!found)
17325 			return (id);
17326 	}
17327 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17328 		found = B_FALSE;
17329 		for (tipif = ill->ill_ipif; tipif != NULL;
17330 		    tipif = tipif->ipif_next) {
17331 			if (tipif->ipif_id == unit) {
17332 				found = B_TRUE;
17333 				break;
17334 			}
17335 		}
17336 		if (!found)
17337 			break;
17338 	}
17339 	return (unit);
17340 }
17341 
17342 /* ARGSUSED */
17343 static int
17344 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17345     ipif_t **rep_ipif_ptr)
17346 {
17347 	ill_t	*from_ill;
17348 	ipif_t	*rep_ipif;
17349 	uint_t	unit;
17350 	int err = 0;
17351 	ipif_t	*to_ipif;
17352 	struct iocblk	*iocp;
17353 	boolean_t failback_cmd;
17354 	boolean_t remove_ipif;
17355 	int	rc;
17356 	ip_stack_t	*ipst;
17357 
17358 	ASSERT(IAM_WRITER_ILL(to_ill));
17359 	ASSERT(IAM_WRITER_IPIF(ipif));
17360 
17361 	iocp = (struct iocblk *)mp->b_rptr;
17362 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17363 	remove_ipif = B_FALSE;
17364 
17365 	from_ill = ipif->ipif_ill;
17366 	ipst = from_ill->ill_ipst;
17367 
17368 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17369 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17370 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17371 
17372 	/*
17373 	 * Don't move LINK LOCAL addresses as they are tied to
17374 	 * physical interface.
17375 	 */
17376 	if (from_ill->ill_isv6 &&
17377 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17378 		ipif->ipif_was_up = B_FALSE;
17379 		IPIF_UNMARK_MOVING(ipif);
17380 		return (0);
17381 	}
17382 
17383 	/*
17384 	 * We set the ipif_id to maximum so that the search for
17385 	 * ipif_id will pick the lowest number i.e 0 in the
17386 	 * following 2 cases :
17387 	 *
17388 	 * 1) We have a replacement ipif at the head of to_ill.
17389 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17390 	 *    on to_ill and hence the MOVE might fail. We want to
17391 	 *    remove it only if we could move the ipif. Thus, by
17392 	 *    setting it to the MAX value, we make the search in
17393 	 *    ipif_get_id return the zeroth id.
17394 	 *
17395 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17396 	 *    we might just have a zero address plumbed on the ipif
17397 	 *    with zero id in the case of IPv4. We remove that while
17398 	 *    doing the failback. We want to remove it only if we
17399 	 *    could move the ipif. Thus, by setting it to the MAX
17400 	 *    value, we make the search in ipif_get_id return the
17401 	 *    zeroth id.
17402 	 *
17403 	 * Both (1) and (2) are done only when when we are moving
17404 	 * an ipif (either due to failover/failback) which originally
17405 	 * belonged to this interface i.e the ipif_orig_ifindex is
17406 	 * the same as to_ill's ifindex. This is needed so that
17407 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17408 	 * from B -> A (B is being removed from the group) and
17409 	 * FAILBACK from A -> B restores the original configuration.
17410 	 * Without the check for orig_ifindex, the second FAILOVER
17411 	 * could make the ipif belonging to B replace the A's zeroth
17412 	 * ipif and the subsequent failback re-creating the replacement
17413 	 * ipif again.
17414 	 *
17415 	 * NOTE : We created the replacement ipif when we did a
17416 	 * FAILOVER (See below). We could check for FAILBACK and
17417 	 * then look for replacement ipif to be removed. But we don't
17418 	 * want to do that because we wan't to allow the possibility
17419 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17420 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17421 	 * from B -> A.
17422 	 */
17423 	to_ipif = to_ill->ill_ipif;
17424 	if ((to_ill->ill_phyint->phyint_ifindex ==
17425 	    ipif->ipif_orig_ifindex) &&
17426 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17427 		ASSERT(to_ipif->ipif_id == 0);
17428 		remove_ipif = B_TRUE;
17429 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17430 	}
17431 	/*
17432 	 * Find the lowest logical unit number on the to_ill.
17433 	 * If we are failing back, try to get the original id
17434 	 * rather than the lowest one so that the original
17435 	 * configuration is maintained.
17436 	 *
17437 	 * XXX need a better scheme for this.
17438 	 */
17439 	if (failback_cmd) {
17440 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17441 	} else {
17442 		unit = ipif_get_id(to_ill, 0);
17443 	}
17444 
17445 	/* Reset back to zero in case we fail below */
17446 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17447 		to_ipif->ipif_id = 0;
17448 
17449 	if (unit == ipst->ips_ip_addrs_per_if) {
17450 		ipif->ipif_was_up = B_FALSE;
17451 		IPIF_UNMARK_MOVING(ipif);
17452 		return (EINVAL);
17453 	}
17454 
17455 	/*
17456 	 * ipif is ready to move from "from_ill" to "to_ill".
17457 	 *
17458 	 * 1) If we are moving ipif with id zero, create a
17459 	 *    replacement ipif for this ipif on from_ill. If this fails
17460 	 *    fail the MOVE operation.
17461 	 *
17462 	 * 2) Remove the replacement ipif on to_ill if any.
17463 	 *    We could remove the replacement ipif when we are moving
17464 	 *    the ipif with id zero. But what if somebody already
17465 	 *    unplumbed it ? Thus we always remove it if it is present.
17466 	 *    We want to do it only if we are sure we are going to
17467 	 *    move the ipif to to_ill which is why there are no
17468 	 *    returns due to error till ipif is linked to to_ill.
17469 	 *    Note that the first ipif that we failback will always
17470 	 *    be zero if it is present.
17471 	 */
17472 	if (ipif->ipif_id == 0) {
17473 		ipaddr_t inaddr_any = INADDR_ANY;
17474 
17475 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17476 		if (rep_ipif == NULL) {
17477 			ipif->ipif_was_up = B_FALSE;
17478 			IPIF_UNMARK_MOVING(ipif);
17479 			return (ENOMEM);
17480 		}
17481 		*rep_ipif = ipif_zero;
17482 		/*
17483 		 * Before we put the ipif on the list, store the addresses
17484 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17485 		 * assumes so. This logic is not any different from what
17486 		 * ipif_allocate does.
17487 		 */
17488 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17489 		    &rep_ipif->ipif_v6lcl_addr);
17490 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17491 		    &rep_ipif->ipif_v6src_addr);
17492 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17493 		    &rep_ipif->ipif_v6subnet);
17494 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17495 		    &rep_ipif->ipif_v6net_mask);
17496 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17497 		    &rep_ipif->ipif_v6brd_addr);
17498 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17499 		    &rep_ipif->ipif_v6pp_dst_addr);
17500 		/*
17501 		 * We mark IPIF_NOFAILOVER so that this can never
17502 		 * move.
17503 		 */
17504 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17505 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17506 		rep_ipif->ipif_replace_zero = B_TRUE;
17507 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17508 		    MUTEX_DEFAULT, NULL);
17509 		rep_ipif->ipif_id = 0;
17510 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17511 		rep_ipif->ipif_ill = from_ill;
17512 		rep_ipif->ipif_orig_ifindex =
17513 		    from_ill->ill_phyint->phyint_ifindex;
17514 		/* Insert at head */
17515 		rep_ipif->ipif_next = from_ill->ill_ipif;
17516 		from_ill->ill_ipif = rep_ipif;
17517 		/*
17518 		 * We don't really care to let apps know about
17519 		 * this interface.
17520 		 */
17521 	}
17522 
17523 	if (remove_ipif) {
17524 		/*
17525 		 * We set to a max value above for this case to get
17526 		 * id zero. ASSERT that we did get one.
17527 		 */
17528 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17529 		rep_ipif = to_ipif;
17530 		to_ill->ill_ipif = rep_ipif->ipif_next;
17531 		rep_ipif->ipif_next = NULL;
17532 		/*
17533 		 * If some apps scanned and find this interface,
17534 		 * it is time to let them know, so that they can
17535 		 * delete it.
17536 		 */
17537 
17538 		*rep_ipif_ptr = rep_ipif;
17539 	}
17540 
17541 	/* Get it out of the ILL interface list. */
17542 	ipif_remove(ipif, B_FALSE);
17543 
17544 	/* Assign the new ill */
17545 	ipif->ipif_ill = to_ill;
17546 	ipif->ipif_id = unit;
17547 	/* id has already been checked */
17548 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17549 	ASSERT(rc == 0);
17550 	/* Let SCTP update its list */
17551 	sctp_move_ipif(ipif, from_ill, to_ill);
17552 	/*
17553 	 * Handle the failover and failback of ipif_t between
17554 	 * ill_t that have differing maximum mtu values.
17555 	 */
17556 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17557 		if (ipif->ipif_saved_mtu == 0) {
17558 			/*
17559 			 * As this ipif_t is moving to an ill_t
17560 			 * that has a lower ill_max_mtu, its
17561 			 * ipif_mtu needs to be saved so it can
17562 			 * be restored during failback or during
17563 			 * failover to an ill_t which has a
17564 			 * higher ill_max_mtu.
17565 			 */
17566 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17567 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17568 		} else {
17569 			/*
17570 			 * The ipif_t is, once again, moving to
17571 			 * an ill_t that has a lower maximum mtu
17572 			 * value.
17573 			 */
17574 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17575 		}
17576 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17577 	    ipif->ipif_saved_mtu != 0) {
17578 		/*
17579 		 * The mtu of this ipif_t had to be reduced
17580 		 * during an earlier failover; this is an
17581 		 * opportunity for it to be increased (either as
17582 		 * part of another failover or a failback).
17583 		 */
17584 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17585 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17586 			ipif->ipif_saved_mtu = 0;
17587 		} else {
17588 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17589 		}
17590 	}
17591 
17592 	/*
17593 	 * We preserve all the other fields of the ipif including
17594 	 * ipif_saved_ire_mp. The routes that are saved here will
17595 	 * be recreated on the new interface and back on the old
17596 	 * interface when we move back.
17597 	 */
17598 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17599 
17600 	return (err);
17601 }
17602 
17603 static int
17604 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17605     int ifindex, ipif_t **rep_ipif_ptr)
17606 {
17607 	ipif_t *mipif;
17608 	ipif_t *ipif_next;
17609 	int err;
17610 
17611 	/*
17612 	 * We don't really try to MOVE back things if some of the
17613 	 * operations fail. The daemon will take care of moving again
17614 	 * later on.
17615 	 */
17616 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17617 		ipif_next = mipif->ipif_next;
17618 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17619 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17620 
17621 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17622 
17623 			/*
17624 			 * When the MOVE fails, it is the job of the
17625 			 * application to take care of this properly
17626 			 * i.e try again if it is ENOMEM.
17627 			 */
17628 			if (mipif->ipif_ill != from_ill) {
17629 				/*
17630 				 * ipif has moved.
17631 				 *
17632 				 * Move the multicast memberships associated
17633 				 * with this ipif to the new ill. For IPv6, we
17634 				 * do it once after all the ipifs are moved
17635 				 * (in ill_move) as they are not associated
17636 				 * with ipifs.
17637 				 *
17638 				 * We need to move the ilms as the ipif has
17639 				 * already been moved to a new ill even
17640 				 * in the case of errors. Neither
17641 				 * ilm_free(ipif) will find the ilm
17642 				 * when somebody unplumbs this ipif nor
17643 				 * ilm_delete(ilm) will be able to find the
17644 				 * ilm, if we don't move now.
17645 				 */
17646 				if (!from_ill->ill_isv6)
17647 					ilm_move_v4(from_ill, to_ill, mipif);
17648 			}
17649 
17650 			if (err != 0)
17651 				return (err);
17652 		}
17653 	}
17654 	return (0);
17655 }
17656 
17657 static int
17658 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17659 {
17660 	int ifindex;
17661 	int err;
17662 	struct iocblk	*iocp;
17663 	ipif_t	*ipif;
17664 	ipif_t *rep_ipif_ptr = NULL;
17665 	ipif_t	*from_ipif = NULL;
17666 	boolean_t check_rep_if = B_FALSE;
17667 	ip_stack_t	*ipst = from_ill->ill_ipst;
17668 
17669 	iocp = (struct iocblk *)mp->b_rptr;
17670 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17671 		/*
17672 		 * Move everything pointing at from_ill to to_ill.
17673 		 * We acheive this by passing in 0 as ifindex.
17674 		 */
17675 		ifindex = 0;
17676 	} else {
17677 		/*
17678 		 * Move everything pointing at from_ill whose original
17679 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17680 		 * We acheive this by passing in ifindex rather than 0.
17681 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17682 		 */
17683 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17684 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17685 	}
17686 
17687 	/*
17688 	 * Determine if there is at least one ipif that would move from
17689 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17690 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17691 	 * the move, in which case we need to quiesce the replacement ipif also.
17692 	 */
17693 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17694 	    from_ipif = from_ipif->ipif_next) {
17695 		if (((ifindex == 0) ||
17696 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17697 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17698 			check_rep_if = B_TRUE;
17699 			break;
17700 		}
17701 	}
17702 
17703 
17704 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17705 
17706 	GRAB_ILL_LOCKS(from_ill, to_ill);
17707 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17708 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17709 		    mp, ILL_MOVE_OK);
17710 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17711 		return (EINPROGRESS);
17712 	}
17713 
17714 	/* Check if the replacement ipif is quiescent to delete */
17715 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17716 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17717 		to_ill->ill_ipif->ipif_state_flags |=
17718 		    IPIF_MOVING | IPIF_CHANGING;
17719 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17720 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17721 			    mp, ILL_MOVE_OK);
17722 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17723 			return (EINPROGRESS);
17724 		}
17725 	}
17726 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17727 
17728 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17729 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17730 	GRAB_ILL_LOCKS(from_ill, to_ill);
17731 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17732 
17733 	/* ilm_move is done inside ipif_move for IPv4 */
17734 	if (err == 0 && from_ill->ill_isv6)
17735 		ilm_move_v6(from_ill, to_ill, ifindex);
17736 
17737 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17738 	rw_exit(&ipst->ips_ill_g_lock);
17739 
17740 	/*
17741 	 * send rts messages and multicast messages.
17742 	 */
17743 	if (rep_ipif_ptr != NULL) {
17744 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17745 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17746 			rep_ipif_ptr->ipif_recovery_id = 0;
17747 		}
17748 		ip_rts_ifmsg(rep_ipif_ptr);
17749 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17750 #ifdef DEBUG
17751 		ipif_trace_cleanup(rep_ipif_ptr);
17752 #endif
17753 		mi_free(rep_ipif_ptr);
17754 	}
17755 
17756 	conn_move_ill(from_ill, to_ill, ifindex);
17757 
17758 	return (err);
17759 }
17760 
17761 /*
17762  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17763  * Also checks for the validity of the arguments.
17764  * Note: We are already exclusive inside the from group.
17765  * It is upto the caller to release refcnt on the to_ill's.
17766  */
17767 static int
17768 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17769     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17770 {
17771 	int dst_index;
17772 	ipif_t *ipif_v4, *ipif_v6;
17773 	struct lifreq *lifr;
17774 	mblk_t *mp1;
17775 	boolean_t exists;
17776 	sin_t	*sin;
17777 	int	err = 0;
17778 	ip_stack_t	*ipst;
17779 
17780 	if (CONN_Q(q))
17781 		ipst = CONNQ_TO_IPST(q);
17782 	else
17783 		ipst = ILLQ_TO_IPST(q);
17784 
17785 
17786 	if ((mp1 = mp->b_cont) == NULL)
17787 		return (EPROTO);
17788 
17789 	if ((mp1 = mp1->b_cont) == NULL)
17790 		return (EPROTO);
17791 
17792 	lifr = (struct lifreq *)mp1->b_rptr;
17793 	sin = (sin_t *)&lifr->lifr_addr;
17794 
17795 	/*
17796 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17797 	 * specific operations.
17798 	 */
17799 	if (sin->sin_family != AF_UNSPEC)
17800 		return (EINVAL);
17801 
17802 	/*
17803 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17804 	 * NULLs for the last 4 args and we know the lookup won't fail
17805 	 * with EINPROGRESS.
17806 	 */
17807 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17808 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17809 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17810 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17811 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17812 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17813 
17814 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17815 		return (ENXIO);
17816 
17817 	if (ipif_v4 != NULL) {
17818 		ASSERT(ipif_v4->ipif_refcnt != 0);
17819 		if (ipif_v4->ipif_id != 0) {
17820 			err = EINVAL;
17821 			goto done;
17822 		}
17823 
17824 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17825 		*ill_from_v4 = ipif_v4->ipif_ill;
17826 	}
17827 
17828 	if (ipif_v6 != NULL) {
17829 		ASSERT(ipif_v6->ipif_refcnt != 0);
17830 		if (ipif_v6->ipif_id != 0) {
17831 			err = EINVAL;
17832 			goto done;
17833 		}
17834 
17835 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17836 		*ill_from_v6 = ipif_v6->ipif_ill;
17837 	}
17838 
17839 	err = 0;
17840 	dst_index = lifr->lifr_movetoindex;
17841 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17842 	    q, mp, ip_process_ioctl, &err, ipst);
17843 	if (err != 0) {
17844 		/*
17845 		 * There could be only v6.
17846 		 */
17847 		if (err != ENXIO)
17848 			goto done;
17849 		err = 0;
17850 	}
17851 
17852 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17853 	    q, mp, ip_process_ioctl, &err, ipst);
17854 	if (err != 0) {
17855 		if (err != ENXIO)
17856 			goto done;
17857 		if (*ill_to_v4 == NULL) {
17858 			err = ENXIO;
17859 			goto done;
17860 		}
17861 		err = 0;
17862 	}
17863 
17864 	/*
17865 	 * If we have something to MOVE i.e "from" not NULL,
17866 	 * "to" should be non-NULL.
17867 	 */
17868 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17869 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17870 		err = EINVAL;
17871 	}
17872 
17873 done:
17874 	if (ipif_v4 != NULL)
17875 		ipif_refrele(ipif_v4);
17876 	if (ipif_v6 != NULL)
17877 		ipif_refrele(ipif_v6);
17878 	return (err);
17879 }
17880 
17881 /*
17882  * FAILOVER and FAILBACK are modelled as MOVE operations.
17883  *
17884  * We don't check whether the MOVE is within the same group or
17885  * not, because this ioctl can be used as a generic mechanism
17886  * to failover from interface A to B, though things will function
17887  * only if they are really part of the same group. Moreover,
17888  * all ipifs may be down and hence temporarily out of the group.
17889  *
17890  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17891  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17892  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17893  * have been deleted and there are no active references. Once quiescent the
17894  * ipif's are moved and brought up on the new ill.
17895  *
17896  * Normally the source ill and destination ill belong to the same IPMP group
17897  * and hence the same ipsq_t. In the event they don't belong to the same
17898  * same group the two ipsq's are first merged into one ipsq - that of the
17899  * to_ill. The multicast memberships on the source and destination ill cannot
17900  * change during the move operation since multicast joins/leaves also have to
17901  * execute on the same ipsq and are hence serialized.
17902  */
17903 /* ARGSUSED */
17904 int
17905 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17906     ip_ioctl_cmd_t *ipip, void *ifreq)
17907 {
17908 	ill_t *ill_to_v4 = NULL;
17909 	ill_t *ill_to_v6 = NULL;
17910 	ill_t *ill_from_v4 = NULL;
17911 	ill_t *ill_from_v6 = NULL;
17912 	int err = 0;
17913 
17914 	/*
17915 	 * setup from and to ill's, we can get EINPROGRESS only for
17916 	 * to_ill's.
17917 	 */
17918 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17919 	    &ill_to_v4, &ill_to_v6);
17920 
17921 	if (err != 0) {
17922 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17923 		goto done;
17924 	}
17925 
17926 	/*
17927 	 * nothing to do.
17928 	 */
17929 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17930 		goto done;
17931 	}
17932 
17933 	/*
17934 	 * nothing to do.
17935 	 */
17936 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17937 		goto done;
17938 	}
17939 
17940 	/*
17941 	 * Mark the ill as changing.
17942 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17943 	 * in ill_up_ipifs in case of error they are cleared below.
17944 	 */
17945 
17946 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17947 	if (ill_from_v4 != NULL)
17948 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17949 	if (ill_from_v6 != NULL)
17950 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17951 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17952 
17953 	/*
17954 	 * Make sure that both src and dst are
17955 	 * in the same syncq group. If not make it happen.
17956 	 * We are not holding any locks because we are the writer
17957 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17958 	 * to protect to_ipsq against changing.
17959 	 */
17960 	if (ill_from_v4 != NULL) {
17961 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17962 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17963 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17964 			    NULL, mp, q);
17965 			goto err_ret;
17966 
17967 		}
17968 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17969 	} else {
17970 
17971 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17972 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17973 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17974 			    NULL, mp, q);
17975 			goto err_ret;
17976 
17977 		}
17978 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17979 	}
17980 
17981 	/*
17982 	 * Now that the ipsq's have been merged and we are the writer
17983 	 * lets mark to_ill as changing as well.
17984 	 */
17985 
17986 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17987 	if (ill_to_v4 != NULL)
17988 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17989 	if (ill_to_v6 != NULL)
17990 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17991 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17992 
17993 	/*
17994 	 * Its ok for us to proceed with the move even if
17995 	 * ill_pending_mp is non null on one of the from ill's as the reply
17996 	 * should not be looking at the ipif, it should only care about the
17997 	 * ill itself.
17998 	 */
17999 
18000 	/*
18001 	 * lets move ipv4 first.
18002 	 */
18003 	if (ill_from_v4 != NULL) {
18004 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18005 		ill_from_v4->ill_move_in_progress = B_TRUE;
18006 		ill_to_v4->ill_move_in_progress = B_TRUE;
18007 		ill_to_v4->ill_move_peer = ill_from_v4;
18008 		ill_from_v4->ill_move_peer = ill_to_v4;
18009 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18010 	}
18011 
18012 	/*
18013 	 * Now lets move ipv6.
18014 	 */
18015 	if (err == 0 && ill_from_v6 != NULL) {
18016 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18017 		ill_from_v6->ill_move_in_progress = B_TRUE;
18018 		ill_to_v6->ill_move_in_progress = B_TRUE;
18019 		ill_to_v6->ill_move_peer = ill_from_v6;
18020 		ill_from_v6->ill_move_peer = ill_to_v6;
18021 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18022 	}
18023 
18024 err_ret:
18025 	/*
18026 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18027 	 * moved to become quiescent.
18028 	 */
18029 	if (err == EINPROGRESS) {
18030 		goto done;
18031 	}
18032 
18033 	/*
18034 	 * if err is set ill_up_ipifs will not be called
18035 	 * lets clear the flags.
18036 	 */
18037 
18038 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18039 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18040 	/*
18041 	 * Some of the clearing may be redundant. But it is simple
18042 	 * not making any extra checks.
18043 	 */
18044 	if (ill_from_v6 != NULL) {
18045 		ill_from_v6->ill_move_in_progress = B_FALSE;
18046 		ill_from_v6->ill_move_peer = NULL;
18047 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18048 	}
18049 	if (ill_from_v4 != NULL) {
18050 		ill_from_v4->ill_move_in_progress = B_FALSE;
18051 		ill_from_v4->ill_move_peer = NULL;
18052 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18053 	}
18054 	if (ill_to_v6 != NULL) {
18055 		ill_to_v6->ill_move_in_progress = B_FALSE;
18056 		ill_to_v6->ill_move_peer = NULL;
18057 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18058 	}
18059 	if (ill_to_v4 != NULL) {
18060 		ill_to_v4->ill_move_in_progress = B_FALSE;
18061 		ill_to_v4->ill_move_peer = NULL;
18062 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18063 	}
18064 
18065 	/*
18066 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18067 	 * Do this always to maintain proper state i.e even in case of errors.
18068 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18069 	 * we need not call on both v4 and v6 interfaces.
18070 	 */
18071 	if (ill_from_v4 != NULL) {
18072 		if ((ill_from_v4->ill_phyint->phyint_flags &
18073 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18074 			phyint_inactive(ill_from_v4->ill_phyint);
18075 		}
18076 	} else if (ill_from_v6 != NULL) {
18077 		if ((ill_from_v6->ill_phyint->phyint_flags &
18078 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18079 			phyint_inactive(ill_from_v6->ill_phyint);
18080 		}
18081 	}
18082 
18083 	if (ill_to_v4 != NULL) {
18084 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18085 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18086 		}
18087 	} else if (ill_to_v6 != NULL) {
18088 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18089 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18090 		}
18091 	}
18092 
18093 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18094 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18095 
18096 no_err:
18097 	/*
18098 	 * lets bring the interfaces up on the to_ill.
18099 	 */
18100 	if (err == 0) {
18101 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18102 		    q, mp);
18103 	}
18104 
18105 	if (err == 0) {
18106 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18107 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18108 
18109 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18110 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18111 	}
18112 done:
18113 
18114 	if (ill_to_v4 != NULL) {
18115 		ill_refrele(ill_to_v4);
18116 	}
18117 	if (ill_to_v6 != NULL) {
18118 		ill_refrele(ill_to_v6);
18119 	}
18120 
18121 	return (err);
18122 }
18123 
18124 static void
18125 ill_dl_down(ill_t *ill)
18126 {
18127 	/*
18128 	 * The ill is down; unbind but stay attached since we're still
18129 	 * associated with a PPA. If we have negotiated DLPI capabilites
18130 	 * with the data link service provider (IDS_OK) then reset them.
18131 	 * The interval between unbinding and rebinding is potentially
18132 	 * unbounded hence we cannot assume things will be the same.
18133 	 * The DLPI capabilities will be probed again when the data link
18134 	 * is brought up.
18135 	 */
18136 	mblk_t	*mp = ill->ill_unbind_mp;
18137 	hook_nic_event_t *info;
18138 
18139 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18140 
18141 	ill->ill_unbind_mp = NULL;
18142 	if (mp != NULL) {
18143 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18144 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18145 		    ill->ill_name));
18146 		mutex_enter(&ill->ill_lock);
18147 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18148 		mutex_exit(&ill->ill_lock);
18149 		if (ill->ill_dlpi_capab_state == IDS_OK)
18150 			ill_capability_reset(ill);
18151 		ill_dlpi_send(ill, mp);
18152 	}
18153 
18154 	/*
18155 	 * Toss all of our multicast memberships.  We could keep them, but
18156 	 * then we'd have to do bookkeeping of any joins and leaves performed
18157 	 * by the application while the the interface is down (we can't just
18158 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18159 	 * on a downed interface).
18160 	 */
18161 	ill_leave_multicast(ill);
18162 
18163 	mutex_enter(&ill->ill_lock);
18164 
18165 	ill->ill_dl_up = 0;
18166 
18167 	if ((info = ill->ill_nic_event_info) != NULL) {
18168 		ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n",
18169 		    info->hne_event, ill->ill_name));
18170 		if (info->hne_data != NULL)
18171 			kmem_free(info->hne_data, info->hne_datalen);
18172 		kmem_free(info, sizeof (hook_nic_event_t));
18173 	}
18174 
18175 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
18176 	if (info != NULL) {
18177 		ip_stack_t	*ipst = ill->ill_ipst;
18178 
18179 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
18180 		info->hne_lif = 0;
18181 		info->hne_event = NE_DOWN;
18182 		info->hne_data = NULL;
18183 		info->hne_datalen = 0;
18184 		info->hne_family = ill->ill_isv6 ?
18185 		    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18186 	} else
18187 		ip2dbg(("ill_dl_down: could not attach DOWN nic event "
18188 		    "information for %s (ENOMEM)\n", ill->ill_name));
18189 
18190 	ill->ill_nic_event_info = info;
18191 
18192 	mutex_exit(&ill->ill_lock);
18193 }
18194 
18195 static void
18196 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18197 {
18198 	union DL_primitives *dlp;
18199 	t_uscalar_t prim;
18200 
18201 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18202 
18203 	dlp = (union DL_primitives *)mp->b_rptr;
18204 	prim = dlp->dl_primitive;
18205 
18206 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18207 	    dlpi_prim_str(prim), prim, ill->ill_name));
18208 
18209 	switch (prim) {
18210 	case DL_PHYS_ADDR_REQ:
18211 	{
18212 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18213 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18214 		break;
18215 	}
18216 	case DL_BIND_REQ:
18217 		mutex_enter(&ill->ill_lock);
18218 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18219 		mutex_exit(&ill->ill_lock);
18220 		break;
18221 	}
18222 
18223 	/*
18224 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18225 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18226 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18227 	 */
18228 	mutex_enter(&ill->ill_lock);
18229 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18230 	    (prim == DL_UNBIND_REQ)) {
18231 		ill->ill_dlpi_pending = prim;
18232 	}
18233 	mutex_exit(&ill->ill_lock);
18234 
18235 	putnext(ill->ill_wq, mp);
18236 }
18237 
18238 /*
18239  * Helper function for ill_dlpi_send().
18240  */
18241 /* ARGSUSED */
18242 static void
18243 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18244 {
18245 	ill_dlpi_send((ill_t *)q->q_ptr, mp);
18246 }
18247 
18248 /*
18249  * Send a DLPI control message to the driver but make sure there
18250  * is only one outstanding message. Uses ill_dlpi_pending to tell
18251  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18252  * when an ACK or a NAK is received to process the next queued message.
18253  */
18254 void
18255 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18256 {
18257 	mblk_t **mpp;
18258 
18259 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18260 
18261 	/*
18262 	 * To ensure that any DLPI requests for current exclusive operation
18263 	 * are always completely sent before any DLPI messages for other
18264 	 * operations, require writer access before enqueuing.
18265 	 */
18266 	if (!IAM_WRITER_ILL(ill)) {
18267 		ill_refhold(ill);
18268 		/* qwriter_ip() does the ill_refrele() */
18269 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18270 		    NEW_OP, B_TRUE);
18271 		return;
18272 	}
18273 
18274 	mutex_enter(&ill->ill_lock);
18275 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18276 		/* Must queue message. Tail insertion */
18277 		mpp = &ill->ill_dlpi_deferred;
18278 		while (*mpp != NULL)
18279 			mpp = &((*mpp)->b_next);
18280 
18281 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18282 		    ill->ill_name));
18283 
18284 		*mpp = mp;
18285 		mutex_exit(&ill->ill_lock);
18286 		return;
18287 	}
18288 	mutex_exit(&ill->ill_lock);
18289 	ill_dlpi_dispatch(ill, mp);
18290 }
18291 
18292 /*
18293  * Send all deferred DLPI messages without waiting for their ACKs.
18294  */
18295 void
18296 ill_dlpi_send_deferred(ill_t *ill)
18297 {
18298 	mblk_t *mp, *nextmp;
18299 
18300 	/*
18301 	 * Clear ill_dlpi_pending so that the message is not queued in
18302 	 * ill_dlpi_send().
18303 	 */
18304 	mutex_enter(&ill->ill_lock);
18305 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18306 	mp = ill->ill_dlpi_deferred;
18307 	ill->ill_dlpi_deferred = NULL;
18308 	mutex_exit(&ill->ill_lock);
18309 
18310 	for (; mp != NULL; mp = nextmp) {
18311 		nextmp = mp->b_next;
18312 		mp->b_next = NULL;
18313 		ill_dlpi_send(ill, mp);
18314 	}
18315 }
18316 
18317 /*
18318  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18319  */
18320 boolean_t
18321 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18322 {
18323 	t_uscalar_t prim_pending;
18324 
18325 	mutex_enter(&ill->ill_lock);
18326 	prim_pending = ill->ill_dlpi_pending;
18327 	mutex_exit(&ill->ill_lock);
18328 
18329 	/*
18330 	 * During teardown, ill_dlpi_send_deferred() will send requests
18331 	 * without waiting; don't bother printing any warnings in that case.
18332 	 */
18333 	if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) {
18334 		if (prim_pending == DL_PRIM_INVAL) {
18335 			(void) mi_strlog(ill->ill_rq, 1,
18336 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18337 			    "unsolicited ack for %s on %s\n",
18338 			    dlpi_prim_str(prim), ill->ill_name);
18339 		} else {
18340 			(void) mi_strlog(ill->ill_rq, 1,
18341 			    SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received "
18342 			    "unexpected ack for %s on %s (expecting %s)\n",
18343 			    dlpi_prim_str(prim), ill->ill_name,
18344 			    dlpi_prim_str(prim_pending));
18345 		}
18346 	}
18347 	return (prim_pending == prim);
18348 }
18349 
18350 /*
18351  * Called when an DLPI control message has been acked or nacked to
18352  * send down the next queued message (if any).
18353  */
18354 void
18355 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18356 {
18357 	mblk_t *mp;
18358 
18359 	ASSERT(IAM_WRITER_ILL(ill));
18360 	mutex_enter(&ill->ill_lock);
18361 
18362 	ASSERT(prim != DL_PRIM_INVAL);
18363 	ASSERT(ill->ill_dlpi_pending == prim);
18364 
18365 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18366 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18367 
18368 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18369 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18370 		cv_signal(&ill->ill_cv);
18371 		mutex_exit(&ill->ill_lock);
18372 		return;
18373 	}
18374 
18375 	ill->ill_dlpi_deferred = mp->b_next;
18376 	mp->b_next = NULL;
18377 	mutex_exit(&ill->ill_lock);
18378 
18379 	ill_dlpi_dispatch(ill, mp);
18380 }
18381 
18382 void
18383 conn_delete_ire(conn_t *connp, caddr_t arg)
18384 {
18385 	ipif_t	*ipif = (ipif_t *)arg;
18386 	ire_t	*ire;
18387 
18388 	/*
18389 	 * Look at the cached ires on conns which has pointers to ipifs.
18390 	 * We just call ire_refrele which clears up the reference
18391 	 * to ire. Called when a conn closes. Also called from ipif_free
18392 	 * to cleanup indirect references to the stale ipif via the cached ire.
18393 	 */
18394 	mutex_enter(&connp->conn_lock);
18395 	ire = connp->conn_ire_cache;
18396 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18397 		connp->conn_ire_cache = NULL;
18398 		mutex_exit(&connp->conn_lock);
18399 		IRE_REFRELE_NOTR(ire);
18400 		return;
18401 	}
18402 	mutex_exit(&connp->conn_lock);
18403 
18404 }
18405 
18406 /*
18407  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18408  * of IREs. Those IREs may have been previously cached in the conn structure.
18409  * This ipcl_walk() walker function releases all references to such IREs based
18410  * on the condemned flag.
18411  */
18412 /* ARGSUSED */
18413 void
18414 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18415 {
18416 	ire_t	*ire;
18417 
18418 	mutex_enter(&connp->conn_lock);
18419 	ire = connp->conn_ire_cache;
18420 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18421 		connp->conn_ire_cache = NULL;
18422 		mutex_exit(&connp->conn_lock);
18423 		IRE_REFRELE_NOTR(ire);
18424 		return;
18425 	}
18426 	mutex_exit(&connp->conn_lock);
18427 }
18428 
18429 /*
18430  * Take down a specific interface, but don't lose any information about it.
18431  * Also delete interface from its interface group (ifgrp).
18432  * (Always called as writer.)
18433  * This function goes through the down sequence even if the interface is
18434  * already down. There are 2 reasons.
18435  * a. Currently we permit interface routes that depend on down interfaces
18436  *    to be added. This behaviour itself is questionable. However it appears
18437  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18438  *    time. We go thru the cleanup in order to remove these routes.
18439  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18440  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18441  *    down, but we need to cleanup i.e. do ill_dl_down and
18442  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18443  *
18444  * IP-MT notes:
18445  *
18446  * Model of reference to interfaces.
18447  *
18448  * The following members in ipif_t track references to the ipif.
18449  *	int     ipif_refcnt;    Active reference count
18450  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18451  * The following members in ill_t track references to the ill.
18452  *	int             ill_refcnt;     active refcnt
18453  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18454  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18455  *
18456  * Reference to an ipif or ill can be obtained in any of the following ways.
18457  *
18458  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18459  * Pointers to ipif / ill from other data structures viz ire and conn.
18460  * Implicit reference to the ipif / ill by holding a reference to the ire.
18461  *
18462  * The ipif/ill lookup functions return a reference held ipif / ill.
18463  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18464  * This is a purely dynamic reference count associated with threads holding
18465  * references to the ipif / ill. Pointers from other structures do not
18466  * count towards this reference count.
18467  *
18468  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
18469  * ipif/ill. This is incremented whenever a new ire is created referencing the
18470  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
18471  * actually added to the ire hash table. The count is decremented in
18472  * ire_inactive where the ire is destroyed.
18473  *
18474  * nce's reference ill's thru nce_ill and the count of nce's associated with
18475  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18476  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18477  * table. Similarly it is decremented in ndp_inactive() where the nce
18478  * is destroyed.
18479  *
18480  * Flow of ioctls involving interface down/up
18481  *
18482  * The following is the sequence of an attempt to set some critical flags on an
18483  * up interface.
18484  * ip_sioctl_flags
18485  * ipif_down
18486  * wait for ipif to be quiescent
18487  * ipif_down_tail
18488  * ip_sioctl_flags_tail
18489  *
18490  * All set ioctls that involve down/up sequence would have a skeleton similar
18491  * to the above. All the *tail functions are called after the refcounts have
18492  * dropped to the appropriate values.
18493  *
18494  * The mechanism to quiesce an ipif is as follows.
18495  *
18496  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18497  * on the ipif. Callers either pass a flag requesting wait or the lookup
18498  *  functions will return NULL.
18499  *
18500  * Delete all ires referencing this ipif
18501  *
18502  * Any thread attempting to do an ipif_refhold on an ipif that has been
18503  * obtained thru a cached pointer will first make sure that
18504  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18505  * increment the refcount.
18506  *
18507  * The above guarantees that the ipif refcount will eventually come down to
18508  * zero and the ipif will quiesce, once all threads that currently hold a
18509  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18510  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18511  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
18512  * drop to zero.
18513  *
18514  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18515  *
18516  * Threads trying to lookup an ipif or ill can pass a flag requesting
18517  * wait and restart if the ipif / ill cannot be looked up currently.
18518  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18519  * failure if the ipif is currently undergoing an exclusive operation, and
18520  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18521  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18522  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18523  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18524  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18525  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18526  * until we release the ipsq_lock, even though the the ill/ipif state flags
18527  * can change after we drop the ill_lock.
18528  *
18529  * An attempt to send out a packet using an ipif that is currently
18530  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18531  * operation and restart it later when the exclusive condition on the ipif ends.
18532  * This is an example of not passing the wait flag to the lookup functions. For
18533  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18534  * out a multicast packet on that ipif will fail while the ipif is
18535  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18536  * currently IPIF_CHANGING will also fail.
18537  */
18538 int
18539 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18540 {
18541 	ill_t		*ill = ipif->ipif_ill;
18542 	phyint_t	*phyi;
18543 	conn_t		*connp;
18544 	boolean_t	success;
18545 	boolean_t	ipif_was_up = B_FALSE;
18546 	ip_stack_t	*ipst = ill->ill_ipst;
18547 
18548 	ASSERT(IAM_WRITER_IPIF(ipif));
18549 
18550 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18551 
18552 	if (ipif->ipif_flags & IPIF_UP) {
18553 		mutex_enter(&ill->ill_lock);
18554 		ipif->ipif_flags &= ~IPIF_UP;
18555 		ASSERT(ill->ill_ipif_up_count > 0);
18556 		--ill->ill_ipif_up_count;
18557 		mutex_exit(&ill->ill_lock);
18558 		ipif_was_up = B_TRUE;
18559 		/* Update status in SCTP's list */
18560 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18561 	}
18562 
18563 	/*
18564 	 * Blow away memberships we established in ipif_multicast_up().
18565 	 */
18566 	ipif_multicast_down(ipif);
18567 
18568 	/*
18569 	 * Remove from the mapping for __sin6_src_id. We insert only
18570 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18571 	 * stored as mapped addresses, we need to check for mapped
18572 	 * INADDR_ANY also.
18573 	 */
18574 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18575 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18576 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18577 		int err;
18578 
18579 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18580 		    ipif->ipif_zoneid, ipst);
18581 		if (err != 0) {
18582 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18583 		}
18584 	}
18585 
18586 	/*
18587 	 * Before we delete the ill from the group (if any), we need
18588 	 * to make sure that we delete all the routes dependent on
18589 	 * this and also any ipifs dependent on this ipif for
18590 	 * source address. We need to do before we delete from
18591 	 * the group because
18592 	 *
18593 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18594 	 *
18595 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18596 	 *    for re-doing source address selection. Note that
18597 	 *    ipif_select_source[_v6] called from
18598 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18599 	 *    because we have already marked down here i.e cleared
18600 	 *    IPIF_UP.
18601 	 */
18602 	if (ipif->ipif_isv6) {
18603 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18604 		    ipst);
18605 	} else {
18606 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18607 		    ipst);
18608 	}
18609 
18610 	/*
18611 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18612 	 * ires have been deleted above. Otherwise a thread could end up
18613 	 * caching an ire in a conn after we have finished the cleanup of the
18614 	 * conn. The caching is done after making sure that the ire is not yet
18615 	 * condemned. Also documented in the block comment above ip_output
18616 	 */
18617 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18618 	/* Also, delete the ires cached in SCTP */
18619 	sctp_ire_cache_flush(ipif);
18620 
18621 	/*
18622 	 * Update any other ipifs which have used "our" local address as
18623 	 * a source address. This entails removing and recreating IRE_INTERFACE
18624 	 * entries for such ipifs.
18625 	 */
18626 	if (ipif->ipif_isv6)
18627 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18628 	else
18629 		ipif_update_other_ipifs(ipif, ill->ill_group);
18630 
18631 	if (ipif_was_up) {
18632 		/*
18633 		 * Check whether it is last ipif to leave this group.
18634 		 * If this is the last ipif to leave, we should remove
18635 		 * this ill from the group as ipif_select_source will not
18636 		 * be able to find any useful ipifs if this ill is selected
18637 		 * for load balancing.
18638 		 *
18639 		 * For nameless groups, we should call ifgrp_delete if this
18640 		 * belongs to some group. As this ipif is going down, we may
18641 		 * need to reconstruct groups.
18642 		 */
18643 		phyi = ill->ill_phyint;
18644 		/*
18645 		 * If the phyint_groupname_len is 0, it may or may not
18646 		 * be in the nameless group. If the phyint_groupname_len is
18647 		 * not 0, then this ill should be part of some group.
18648 		 * As we always insert this ill in the group if
18649 		 * phyint_groupname_len is not zero when the first ipif
18650 		 * comes up (in ipif_up_done), it should be in a group
18651 		 * when the namelen is not 0.
18652 		 *
18653 		 * NOTE : When we delete the ill from the group,it will
18654 		 * blow away all the IRE_CACHES pointing either at this ipif or
18655 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18656 		 * should be pointing at this ill.
18657 		 */
18658 		ASSERT(phyi->phyint_groupname_len == 0 ||
18659 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18660 
18661 		if (phyi->phyint_groupname_len != 0) {
18662 			if (ill->ill_ipif_up_count == 0)
18663 				illgrp_delete(ill);
18664 		}
18665 
18666 		/*
18667 		 * If we have deleted some of the broadcast ires associated
18668 		 * with this ipif, we need to re-nominate somebody else if
18669 		 * the ires that we deleted were the nominated ones.
18670 		 */
18671 		if (ill->ill_group != NULL && !ill->ill_isv6)
18672 			ipif_renominate_bcast(ipif);
18673 	}
18674 
18675 	/*
18676 	 * neighbor-discovery or arp entries for this interface.
18677 	 */
18678 	ipif_ndp_down(ipif);
18679 
18680 	/*
18681 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18682 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18683 	 * and ill_delete -> ipif_free -> ipif_down
18684 	 */
18685 	if (mp == NULL) {
18686 		ASSERT(q == NULL);
18687 		return (0);
18688 	}
18689 
18690 	if (CONN_Q(q)) {
18691 		connp = Q_TO_CONN(q);
18692 		mutex_enter(&connp->conn_lock);
18693 	} else {
18694 		connp = NULL;
18695 	}
18696 	mutex_enter(&ill->ill_lock);
18697 	/*
18698 	 * Are there any ire's pointing to this ipif that are still active ?
18699 	 * If this is the last ipif going down, are there any ire's pointing
18700 	 * to this ill that are still active ?
18701 	 */
18702 	if (ipif_is_quiescent(ipif)) {
18703 		mutex_exit(&ill->ill_lock);
18704 		if (connp != NULL)
18705 			mutex_exit(&connp->conn_lock);
18706 		return (0);
18707 	}
18708 
18709 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18710 	    ill->ill_name, (void *)ill));
18711 	/*
18712 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18713 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18714 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18715 	 */
18716 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18717 	if (!success) {
18718 		/* The conn is closing. So just return */
18719 		ASSERT(connp != NULL);
18720 		mutex_exit(&ill->ill_lock);
18721 		mutex_exit(&connp->conn_lock);
18722 		return (EINTR);
18723 	}
18724 
18725 	mutex_exit(&ill->ill_lock);
18726 	if (connp != NULL)
18727 		mutex_exit(&connp->conn_lock);
18728 	return (EINPROGRESS);
18729 }
18730 
18731 void
18732 ipif_down_tail(ipif_t *ipif)
18733 {
18734 	ill_t	*ill = ipif->ipif_ill;
18735 
18736 	/*
18737 	 * Skip any loopback interface (null wq).
18738 	 * If this is the last logical interface on the ill
18739 	 * have ill_dl_down tell the driver we are gone (unbind)
18740 	 * Note that lun 0 can ipif_down even though
18741 	 * there are other logical units that are up.
18742 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18743 	 */
18744 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18745 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18746 	    ill->ill_dl_up) {
18747 		ill_dl_down(ill);
18748 	}
18749 	ill->ill_logical_down = 0;
18750 
18751 	/*
18752 	 * Have to be after removing the routes in ipif_down_delete_ire.
18753 	 */
18754 	if (ipif->ipif_isv6) {
18755 		if (ill->ill_flags & ILLF_XRESOLV)
18756 			ipif_arp_down(ipif);
18757 	} else {
18758 		ipif_arp_down(ipif);
18759 	}
18760 
18761 	ip_rts_ifmsg(ipif);
18762 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18763 }
18764 
18765 /*
18766  * Bring interface logically down without bringing the physical interface
18767  * down e.g. when the netmask is changed. This avoids long lasting link
18768  * negotiations between an ethernet interface and a certain switches.
18769  */
18770 static int
18771 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18772 {
18773 	/*
18774 	 * The ill_logical_down flag is a transient flag. It is set here
18775 	 * and is cleared once the down has completed in ipif_down_tail.
18776 	 * This flag does not indicate whether the ill stream is in the
18777 	 * DL_BOUND state with the driver. Instead this flag is used by
18778 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18779 	 * the driver. The state of the ill stream i.e. whether it is
18780 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18781 	 */
18782 	ipif->ipif_ill->ill_logical_down = 1;
18783 	return (ipif_down(ipif, q, mp));
18784 }
18785 
18786 /*
18787  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18788  * If the usesrc client ILL is already part of a usesrc group or not,
18789  * in either case a ire_stq with the matching usesrc client ILL will
18790  * locate the IRE's that need to be deleted. We want IREs to be created
18791  * with the new source address.
18792  */
18793 static void
18794 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18795 {
18796 	ill_t	*ucill = (ill_t *)ill_arg;
18797 
18798 	ASSERT(IAM_WRITER_ILL(ucill));
18799 
18800 	if (ire->ire_stq == NULL)
18801 		return;
18802 
18803 	if ((ire->ire_type == IRE_CACHE) &&
18804 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18805 		ire_delete(ire);
18806 }
18807 
18808 /*
18809  * ire_walk routine to delete every IRE dependent on the interface
18810  * address that is going down.	(Always called as writer.)
18811  * Works for both v4 and v6.
18812  * In addition for checking for ire_ipif matches it also checks for
18813  * IRE_CACHE entries which have the same source address as the
18814  * disappearing ipif since ipif_select_source might have picked
18815  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18816  * care of any IRE_INTERFACE with the disappearing source address.
18817  */
18818 static void
18819 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18820 {
18821 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18822 	ill_t *ire_ill;
18823 	ill_t *ipif_ill;
18824 
18825 	ASSERT(IAM_WRITER_IPIF(ipif));
18826 	if (ire->ire_ipif == NULL)
18827 		return;
18828 
18829 	/*
18830 	 * For IPv4, we derive source addresses for an IRE from ipif's
18831 	 * belonging to the same IPMP group as the IRE's outgoing
18832 	 * interface.  If an IRE's outgoing interface isn't in the
18833 	 * same IPMP group as a particular ipif, then that ipif
18834 	 * couldn't have been used as a source address for this IRE.
18835 	 *
18836 	 * For IPv6, source addresses are only restricted to the IPMP group
18837 	 * if the IRE is for a link-local address or a multicast address.
18838 	 * Otherwise, source addresses for an IRE can be chosen from
18839 	 * interfaces other than the the outgoing interface for that IRE.
18840 	 *
18841 	 * For source address selection details, see ipif_select_source()
18842 	 * and ipif_select_source_v6().
18843 	 */
18844 	if (ire->ire_ipversion == IPV4_VERSION ||
18845 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18846 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18847 		ire_ill = ire->ire_ipif->ipif_ill;
18848 		ipif_ill = ipif->ipif_ill;
18849 
18850 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18851 			return;
18852 		}
18853 	}
18854 
18855 
18856 	if (ire->ire_ipif != ipif) {
18857 		/*
18858 		 * Look for a matching source address.
18859 		 */
18860 		if (ire->ire_type != IRE_CACHE)
18861 			return;
18862 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18863 			return;
18864 
18865 		if (ire->ire_ipversion == IPV4_VERSION) {
18866 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18867 				return;
18868 		} else {
18869 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18870 			    &ipif->ipif_v6lcl_addr))
18871 				return;
18872 		}
18873 		ire_delete(ire);
18874 		return;
18875 	}
18876 	/*
18877 	 * ire_delete() will do an ire_flush_cache which will delete
18878 	 * all ire_ipif matches
18879 	 */
18880 	ire_delete(ire);
18881 }
18882 
18883 /*
18884  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18885  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18886  * 2) when an interface is brought up or down (on that ill).
18887  * This ensures that the IRE_CACHE entries don't retain stale source
18888  * address selection results.
18889  */
18890 void
18891 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18892 {
18893 	ill_t	*ill = (ill_t *)ill_arg;
18894 	ill_t	*ipif_ill;
18895 
18896 	ASSERT(IAM_WRITER_ILL(ill));
18897 	/*
18898 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18899 	 * Hence this should be IRE_CACHE.
18900 	 */
18901 	ASSERT(ire->ire_type == IRE_CACHE);
18902 
18903 	/*
18904 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18905 	 * We are only interested in IRE_CACHES that has borrowed
18906 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18907 	 * for which we need to look at ire_ipif->ipif_ill match
18908 	 * with ill.
18909 	 */
18910 	ASSERT(ire->ire_ipif != NULL);
18911 	ipif_ill = ire->ire_ipif->ipif_ill;
18912 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18913 	    ipif_ill->ill_group == ill->ill_group)) {
18914 		ire_delete(ire);
18915 	}
18916 }
18917 
18918 /*
18919  * Delete all the ire whose stq references ill_arg.
18920  */
18921 static void
18922 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18923 {
18924 	ill_t	*ill = (ill_t *)ill_arg;
18925 	ill_t	*ire_ill;
18926 
18927 	ASSERT(IAM_WRITER_ILL(ill));
18928 	/*
18929 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18930 	 * Hence this should be IRE_CACHE.
18931 	 */
18932 	ASSERT(ire->ire_type == IRE_CACHE);
18933 
18934 	/*
18935 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18936 	 * matches ill. We are only interested in IRE_CACHES that
18937 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
18938 	 * filtering here.
18939 	 */
18940 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
18941 
18942 	if (ire_ill == ill)
18943 		ire_delete(ire);
18944 }
18945 
18946 /*
18947  * This is called when an ill leaves the group. We want to delete
18948  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
18949  * pointing at ill.
18950  */
18951 static void
18952 illgrp_cache_delete(ire_t *ire, char *ill_arg)
18953 {
18954 	ill_t	*ill = (ill_t *)ill_arg;
18955 
18956 	ASSERT(IAM_WRITER_ILL(ill));
18957 	ASSERT(ill->ill_group == NULL);
18958 	/*
18959 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18960 	 * Hence this should be IRE_CACHE.
18961 	 */
18962 	ASSERT(ire->ire_type == IRE_CACHE);
18963 	/*
18964 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
18965 	 * matches ill. We are interested in both.
18966 	 */
18967 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
18968 	    (ire->ire_ipif->ipif_ill == ill));
18969 
18970 	ire_delete(ire);
18971 }
18972 
18973 /*
18974  * Initiate deallocate of an IPIF. Always called as writer. Called by
18975  * ill_delete or ip_sioctl_removeif.
18976  */
18977 static void
18978 ipif_free(ipif_t *ipif)
18979 {
18980 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
18981 
18982 	ASSERT(IAM_WRITER_IPIF(ipif));
18983 
18984 	if (ipif->ipif_recovery_id != 0)
18985 		(void) untimeout(ipif->ipif_recovery_id);
18986 	ipif->ipif_recovery_id = 0;
18987 
18988 	/* Remove conn references */
18989 	reset_conn_ipif(ipif);
18990 
18991 	/*
18992 	 * Make sure we have valid net and subnet broadcast ire's for the
18993 	 * other ipif's which share them with this ipif.
18994 	 */
18995 	if (!ipif->ipif_isv6)
18996 		ipif_check_bcast_ires(ipif);
18997 
18998 	/*
18999 	 * Take down the interface. We can be called either from ill_delete
19000 	 * or from ip_sioctl_removeif.
19001 	 */
19002 	(void) ipif_down(ipif, NULL, NULL);
19003 
19004 	/*
19005 	 * Now that the interface is down, there's no chance it can still
19006 	 * become a duplicate.  Cancel any timer that may have been set while
19007 	 * tearing down.
19008 	 */
19009 	if (ipif->ipif_recovery_id != 0)
19010 		(void) untimeout(ipif->ipif_recovery_id);
19011 	ipif->ipif_recovery_id = 0;
19012 
19013 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19014 	/* Remove pointers to this ill in the multicast routing tables */
19015 	reset_mrt_vif_ipif(ipif);
19016 	rw_exit(&ipst->ips_ill_g_lock);
19017 }
19018 
19019 /*
19020  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19021  * also ill_move().
19022  */
19023 static void
19024 ipif_free_tail(ipif_t *ipif)
19025 {
19026 	mblk_t	*mp;
19027 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19028 
19029 	/*
19030 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19031 	 */
19032 	mutex_enter(&ipif->ipif_saved_ire_lock);
19033 	mp = ipif->ipif_saved_ire_mp;
19034 	ipif->ipif_saved_ire_mp = NULL;
19035 	mutex_exit(&ipif->ipif_saved_ire_lock);
19036 	freemsg(mp);
19037 
19038 	/*
19039 	 * Need to hold both ill_g_lock and ill_lock while
19040 	 * inserting or removing an ipif from the linked list
19041 	 * of ipifs hanging off the ill.
19042 	 */
19043 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19044 	/*
19045 	 * Remove all IPv4 multicast memberships on the interface now.
19046 	 * IPv6 is not handled here as the multicast memberships are
19047 	 * tied to the ill rather than the ipif.
19048 	 */
19049 	ilm_free(ipif);
19050 
19051 	/*
19052 	 * Since we held the ill_g_lock while doing the ilm_free above,
19053 	 * we can assert the ilms were really deleted and not just marked
19054 	 * ILM_DELETED.
19055 	 */
19056 	ASSERT(ilm_walk_ipif(ipif) == 0);
19057 
19058 #ifdef DEBUG
19059 	ipif_trace_cleanup(ipif);
19060 #endif
19061 
19062 	/* Ask SCTP to take it out of it list */
19063 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19064 
19065 	/* Get it out of the ILL interface list. */
19066 	ipif_remove(ipif, B_TRUE);
19067 	rw_exit(&ipst->ips_ill_g_lock);
19068 
19069 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19070 
19071 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19072 	ASSERT(ipif->ipif_recovery_id == 0);
19073 
19074 	/* Free the memory. */
19075 	mi_free(ipif);
19076 }
19077 
19078 /*
19079  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19080  * is zero.
19081  */
19082 void
19083 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19084 {
19085 	char	lbuf[LIFNAMSIZ];
19086 	char	*name;
19087 	size_t	name_len;
19088 
19089 	buf[0] = '\0';
19090 	name = ipif->ipif_ill->ill_name;
19091 	name_len = ipif->ipif_ill->ill_name_length;
19092 	if (ipif->ipif_id != 0) {
19093 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19094 		    ipif->ipif_id);
19095 		name = lbuf;
19096 		name_len = mi_strlen(name) + 1;
19097 	}
19098 	len -= 1;
19099 	buf[len] = '\0';
19100 	len = MIN(len, name_len);
19101 	bcopy(name, buf, len);
19102 }
19103 
19104 /*
19105  * Find an IPIF based on the name passed in.  Names can be of the
19106  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19107  * The <phys> string can have forms like <dev><#> (e.g., le0),
19108  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19109  * When there is no colon, the implied unit id is zero. <phys> must
19110  * correspond to the name of an ILL.  (May be called as writer.)
19111  */
19112 static ipif_t *
19113 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19114     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19115     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19116 {
19117 	char	*cp;
19118 	char	*endp;
19119 	long	id;
19120 	ill_t	*ill;
19121 	ipif_t	*ipif;
19122 	uint_t	ire_type;
19123 	boolean_t did_alloc = B_FALSE;
19124 	ipsq_t	*ipsq;
19125 
19126 	if (error != NULL)
19127 		*error = 0;
19128 
19129 	/*
19130 	 * If the caller wants to us to create the ipif, make sure we have a
19131 	 * valid zoneid
19132 	 */
19133 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19134 
19135 	if (namelen == 0) {
19136 		if (error != NULL)
19137 			*error = ENXIO;
19138 		return (NULL);
19139 	}
19140 
19141 	*exists = B_FALSE;
19142 	/* Look for a colon in the name. */
19143 	endp = &name[namelen];
19144 	for (cp = endp; --cp > name; ) {
19145 		if (*cp == IPIF_SEPARATOR_CHAR)
19146 			break;
19147 	}
19148 
19149 	if (*cp == IPIF_SEPARATOR_CHAR) {
19150 		/*
19151 		 * Reject any non-decimal aliases for logical
19152 		 * interfaces. Aliases with leading zeroes
19153 		 * are also rejected as they introduce ambiguity
19154 		 * in the naming of the interfaces.
19155 		 * In order to confirm with existing semantics,
19156 		 * and to not break any programs/script relying
19157 		 * on that behaviour, if<0>:0 is considered to be
19158 		 * a valid interface.
19159 		 *
19160 		 * If alias has two or more digits and the first
19161 		 * is zero, fail.
19162 		 */
19163 		if (&cp[2] < endp && cp[1] == '0')
19164 			return (NULL);
19165 	}
19166 
19167 	if (cp <= name) {
19168 		cp = endp;
19169 	} else {
19170 		*cp = '\0';
19171 	}
19172 
19173 	/*
19174 	 * Look up the ILL, based on the portion of the name
19175 	 * before the slash. ill_lookup_on_name returns a held ill.
19176 	 * Temporary to check whether ill exists already. If so
19177 	 * ill_lookup_on_name will clear it.
19178 	 */
19179 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19180 	    q, mp, func, error, &did_alloc, ipst);
19181 	if (cp != endp)
19182 		*cp = IPIF_SEPARATOR_CHAR;
19183 	if (ill == NULL)
19184 		return (NULL);
19185 
19186 	/* Establish the unit number in the name. */
19187 	id = 0;
19188 	if (cp < endp && *endp == '\0') {
19189 		/* If there was a colon, the unit number follows. */
19190 		cp++;
19191 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19192 			ill_refrele(ill);
19193 			if (error != NULL)
19194 				*error = ENXIO;
19195 			return (NULL);
19196 		}
19197 	}
19198 
19199 	GRAB_CONN_LOCK(q);
19200 	mutex_enter(&ill->ill_lock);
19201 	/* Now see if there is an IPIF with this unit number. */
19202 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19203 		if (ipif->ipif_id == id) {
19204 			if (zoneid != ALL_ZONES &&
19205 			    zoneid != ipif->ipif_zoneid &&
19206 			    ipif->ipif_zoneid != ALL_ZONES) {
19207 				mutex_exit(&ill->ill_lock);
19208 				RELEASE_CONN_LOCK(q);
19209 				ill_refrele(ill);
19210 				if (error != NULL)
19211 					*error = ENXIO;
19212 				return (NULL);
19213 			}
19214 			/*
19215 			 * The block comment at the start of ipif_down
19216 			 * explains the use of the macros used below
19217 			 */
19218 			if (IPIF_CAN_LOOKUP(ipif)) {
19219 				ipif_refhold_locked(ipif);
19220 				mutex_exit(&ill->ill_lock);
19221 				if (!did_alloc)
19222 					*exists = B_TRUE;
19223 				/*
19224 				 * Drop locks before calling ill_refrele
19225 				 * since it can potentially call into
19226 				 * ipif_ill_refrele_tail which can end up
19227 				 * in trying to acquire any lock.
19228 				 */
19229 				RELEASE_CONN_LOCK(q);
19230 				ill_refrele(ill);
19231 				return (ipif);
19232 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19233 				ipsq = ill->ill_phyint->phyint_ipsq;
19234 				mutex_enter(&ipsq->ipsq_lock);
19235 				mutex_exit(&ill->ill_lock);
19236 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19237 				mutex_exit(&ipsq->ipsq_lock);
19238 				RELEASE_CONN_LOCK(q);
19239 				ill_refrele(ill);
19240 				*error = EINPROGRESS;
19241 				return (NULL);
19242 			}
19243 		}
19244 	}
19245 	RELEASE_CONN_LOCK(q);
19246 
19247 	if (!do_alloc) {
19248 		mutex_exit(&ill->ill_lock);
19249 		ill_refrele(ill);
19250 		if (error != NULL)
19251 			*error = ENXIO;
19252 		return (NULL);
19253 	}
19254 
19255 	/*
19256 	 * If none found, atomically allocate and return a new one.
19257 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19258 	 * to support "receive only" use of lo0:1 etc. as is still done
19259 	 * below as an initial guess.
19260 	 * However, this is now likely to be overriden later in ipif_up_done()
19261 	 * when we know for sure what address has been configured on the
19262 	 * interface, since we might have more than one loopback interface
19263 	 * with a loopback address, e.g. in the case of zones, and all the
19264 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19265 	 */
19266 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19267 		ire_type = IRE_LOOPBACK;
19268 	else
19269 		ire_type = IRE_LOCAL;
19270 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19271 	if (ipif != NULL)
19272 		ipif_refhold_locked(ipif);
19273 	else if (error != NULL)
19274 		*error = ENOMEM;
19275 	mutex_exit(&ill->ill_lock);
19276 	ill_refrele(ill);
19277 	return (ipif);
19278 }
19279 
19280 /*
19281  * This routine is called whenever a new address comes up on an ipif.  If
19282  * we are configured to respond to address mask requests, then we are supposed
19283  * to broadcast an address mask reply at this time.  This routine is also
19284  * called if we are already up, but a netmask change is made.  This is legal
19285  * but might not make the system manager very popular.	(May be called
19286  * as writer.)
19287  */
19288 void
19289 ipif_mask_reply(ipif_t *ipif)
19290 {
19291 	icmph_t	*icmph;
19292 	ipha_t	*ipha;
19293 	mblk_t	*mp;
19294 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19295 
19296 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19297 
19298 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19299 		return;
19300 
19301 	/* ICMP mask reply is IPv4 only */
19302 	ASSERT(!ipif->ipif_isv6);
19303 	/* ICMP mask reply is not for a loopback interface */
19304 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19305 
19306 	mp = allocb(REPLY_LEN, BPRI_HI);
19307 	if (mp == NULL)
19308 		return;
19309 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19310 
19311 	ipha = (ipha_t *)mp->b_rptr;
19312 	bzero(ipha, REPLY_LEN);
19313 	*ipha = icmp_ipha;
19314 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19315 	ipha->ipha_src = ipif->ipif_src_addr;
19316 	ipha->ipha_dst = ipif->ipif_brd_addr;
19317 	ipha->ipha_length = htons(REPLY_LEN);
19318 	ipha->ipha_ident = 0;
19319 
19320 	icmph = (icmph_t *)&ipha[1];
19321 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19322 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19323 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19324 
19325 	put(ipif->ipif_wq, mp);
19326 
19327 #undef	REPLY_LEN
19328 }
19329 
19330 /*
19331  * When the mtu in the ipif changes, we call this routine through ire_walk
19332  * to update all the relevant IREs.
19333  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19334  */
19335 static void
19336 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19337 {
19338 	ipif_t *ipif = (ipif_t *)ipif_arg;
19339 
19340 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19341 		return;
19342 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19343 }
19344 
19345 /*
19346  * When the mtu in the ill changes, we call this routine through ire_walk
19347  * to update all the relevant IREs.
19348  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19349  */
19350 void
19351 ill_mtu_change(ire_t *ire, char *ill_arg)
19352 {
19353 	ill_t	*ill = (ill_t *)ill_arg;
19354 
19355 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19356 		return;
19357 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19358 }
19359 
19360 /*
19361  * Join the ipif specific multicast groups.
19362  * Must be called after a mapping has been set up in the resolver.  (Always
19363  * called as writer.)
19364  */
19365 void
19366 ipif_multicast_up(ipif_t *ipif)
19367 {
19368 	int err, index;
19369 	ill_t *ill;
19370 
19371 	ASSERT(IAM_WRITER_IPIF(ipif));
19372 
19373 	ill = ipif->ipif_ill;
19374 	index = ill->ill_phyint->phyint_ifindex;
19375 
19376 	ip1dbg(("ipif_multicast_up\n"));
19377 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19378 		return;
19379 
19380 	if (ipif->ipif_isv6) {
19381 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19382 			return;
19383 
19384 		/* Join the all hosts multicast address */
19385 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19386 		/*
19387 		 * Passing B_TRUE means we have to join the multicast
19388 		 * membership on this interface even though this is
19389 		 * FAILED. If we join on a different one in the group,
19390 		 * we will not be able to delete the membership later
19391 		 * as we currently don't track where we join when we
19392 		 * join within the kernel unlike applications where
19393 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19394 		 * for more on this.
19395 		 */
19396 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19397 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19398 		if (err != 0) {
19399 			ip0dbg(("ipif_multicast_up: "
19400 			    "all_hosts_mcast failed %d\n",
19401 			    err));
19402 			return;
19403 		}
19404 		/*
19405 		 * Enable multicast for the solicited node multicast address
19406 		 */
19407 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19408 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19409 
19410 			ipv6_multi.s6_addr32[3] |=
19411 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19412 
19413 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19414 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19415 			    NULL);
19416 			if (err != 0) {
19417 				ip0dbg(("ipif_multicast_up: solicited MC"
19418 				    " failed %d\n", err));
19419 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19420 				    ill, ill->ill_phyint->phyint_ifindex,
19421 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19422 				return;
19423 			}
19424 		}
19425 	} else {
19426 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19427 			return;
19428 
19429 		/* Join the all hosts multicast address */
19430 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19431 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19432 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19433 		if (err) {
19434 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19435 			return;
19436 		}
19437 	}
19438 	ipif->ipif_multicast_up = 1;
19439 }
19440 
19441 /*
19442  * Blow away any multicast groups that we joined in ipif_multicast_up().
19443  * (Explicit memberships are blown away in ill_leave_multicast() when the
19444  * ill is brought down.)
19445  */
19446 static void
19447 ipif_multicast_down(ipif_t *ipif)
19448 {
19449 	int err;
19450 
19451 	ASSERT(IAM_WRITER_IPIF(ipif));
19452 
19453 	ip1dbg(("ipif_multicast_down\n"));
19454 	if (!ipif->ipif_multicast_up)
19455 		return;
19456 
19457 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19458 
19459 	if (!ipif->ipif_isv6) {
19460 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19461 		    B_TRUE);
19462 		if (err != 0)
19463 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19464 
19465 		ipif->ipif_multicast_up = 0;
19466 		return;
19467 	}
19468 
19469 	/*
19470 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19471 	 * we should look for ilms on this ill rather than the ones that have
19472 	 * been failed over here.  They are here temporarily. As
19473 	 * ipif_multicast_up has joined on this ill, we should delete only
19474 	 * from this ill.
19475 	 */
19476 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19477 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19478 	    B_TRUE, B_TRUE);
19479 	if (err != 0) {
19480 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19481 		    err));
19482 	}
19483 	/*
19484 	 * Disable multicast for the solicited node multicast address
19485 	 */
19486 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19487 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19488 
19489 		ipv6_multi.s6_addr32[3] |=
19490 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19491 
19492 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19493 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19494 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19495 
19496 		if (err != 0) {
19497 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19498 			    err));
19499 		}
19500 	}
19501 
19502 	ipif->ipif_multicast_up = 0;
19503 }
19504 
19505 /*
19506  * Used when an interface comes up to recreate any extra routes on this
19507  * interface.
19508  */
19509 static ire_t **
19510 ipif_recover_ire(ipif_t *ipif)
19511 {
19512 	mblk_t	*mp;
19513 	ire_t	**ipif_saved_irep;
19514 	ire_t	**irep;
19515 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19516 
19517 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19518 	    ipif->ipif_id));
19519 
19520 	mutex_enter(&ipif->ipif_saved_ire_lock);
19521 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19522 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19523 	if (ipif_saved_irep == NULL) {
19524 		mutex_exit(&ipif->ipif_saved_ire_lock);
19525 		return (NULL);
19526 	}
19527 
19528 	irep = ipif_saved_irep;
19529 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19530 		ire_t		*ire;
19531 		queue_t		*rfq;
19532 		queue_t		*stq;
19533 		ifrt_t		*ifrt;
19534 		uchar_t		*src_addr;
19535 		uchar_t		*gateway_addr;
19536 		ushort_t	type;
19537 
19538 		/*
19539 		 * When the ire was initially created and then added in
19540 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19541 		 * in the case of a traditional interface route, or as one of
19542 		 * the IRE_OFFSUBNET types (with the exception of
19543 		 * IRE_HOST types ire which is created by icmp_redirect() and
19544 		 * which we don't need to save or recover).  In the case where
19545 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19546 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19547 		 * to satisfy software like GateD and Sun Cluster which creates
19548 		 * routes using the the loopback interface's address as a
19549 		 * gateway.
19550 		 *
19551 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19552 		 * ire_create() will be called in the same way here as
19553 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19554 		 * the route looks like a traditional interface route (where
19555 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19556 		 * the saved ifrt->ifrt_type.  This means that in the case where
19557 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19558 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19559 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19560 		 */
19561 		ifrt = (ifrt_t *)mp->b_rptr;
19562 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19563 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19564 			rfq = NULL;
19565 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19566 			    ? ipif->ipif_rq : ipif->ipif_wq;
19567 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19568 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19569 			    : (uint8_t *)&ipif->ipif_src_addr;
19570 			gateway_addr = NULL;
19571 			type = ipif->ipif_net_type;
19572 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19573 			/* Recover multiroute broadcast IRE. */
19574 			rfq = ipif->ipif_rq;
19575 			stq = ipif->ipif_wq;
19576 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19577 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19578 			    : (uint8_t *)&ipif->ipif_src_addr;
19579 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19580 			type = ifrt->ifrt_type;
19581 		} else {
19582 			rfq = NULL;
19583 			stq = NULL;
19584 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19585 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19586 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19587 			type = ifrt->ifrt_type;
19588 		}
19589 
19590 		/*
19591 		 * Create a copy of the IRE with the saved address and netmask.
19592 		 */
19593 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19594 		    "0x%x/0x%x\n",
19595 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19596 		    ntohl(ifrt->ifrt_addr),
19597 		    ntohl(ifrt->ifrt_mask)));
19598 		ire = ire_create(
19599 		    (uint8_t *)&ifrt->ifrt_addr,
19600 		    (uint8_t *)&ifrt->ifrt_mask,
19601 		    src_addr,
19602 		    gateway_addr,
19603 		    &ifrt->ifrt_max_frag,
19604 		    NULL,
19605 		    rfq,
19606 		    stq,
19607 		    type,
19608 		    ipif,
19609 		    0,
19610 		    0,
19611 		    0,
19612 		    ifrt->ifrt_flags,
19613 		    &ifrt->ifrt_iulp_info,
19614 		    NULL,
19615 		    NULL,
19616 		    ipst);
19617 
19618 		if (ire == NULL) {
19619 			mutex_exit(&ipif->ipif_saved_ire_lock);
19620 			kmem_free(ipif_saved_irep,
19621 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19622 			return (NULL);
19623 		}
19624 
19625 		/*
19626 		 * Some software (for example, GateD and Sun Cluster) attempts
19627 		 * to create (what amount to) IRE_PREFIX routes with the
19628 		 * loopback address as the gateway.  This is primarily done to
19629 		 * set up prefixes with the RTF_REJECT flag set (for example,
19630 		 * when generating aggregate routes.)
19631 		 *
19632 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19633 		 * IRE_LOOPBACK, then we map the request into a
19634 		 * IRE_IF_NORESOLVER.
19635 		 */
19636 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19637 			ire->ire_type = IRE_IF_NORESOLVER;
19638 		/*
19639 		 * ire held by ire_add, will be refreled' towards the
19640 		 * the end of ipif_up_done
19641 		 */
19642 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19643 		*irep = ire;
19644 		irep++;
19645 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19646 	}
19647 	mutex_exit(&ipif->ipif_saved_ire_lock);
19648 	return (ipif_saved_irep);
19649 }
19650 
19651 /*
19652  * Used to set the netmask and broadcast address to default values when the
19653  * interface is brought up.  (Always called as writer.)
19654  */
19655 static void
19656 ipif_set_default(ipif_t *ipif)
19657 {
19658 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19659 
19660 	if (!ipif->ipif_isv6) {
19661 		/*
19662 		 * Interface holds an IPv4 address. Default
19663 		 * mask is the natural netmask.
19664 		 */
19665 		if (!ipif->ipif_net_mask) {
19666 			ipaddr_t	v4mask;
19667 
19668 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19669 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19670 		}
19671 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19672 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19673 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19674 		} else {
19675 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19676 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19677 		}
19678 		/*
19679 		 * NOTE: SunOS 4.X does this even if the broadcast address
19680 		 * has been already set thus we do the same here.
19681 		 */
19682 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19683 			ipaddr_t	v4addr;
19684 
19685 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19686 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19687 		}
19688 	} else {
19689 		/*
19690 		 * Interface holds an IPv6-only address.  Default
19691 		 * mask is all-ones.
19692 		 */
19693 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19694 			ipif->ipif_v6net_mask = ipv6_all_ones;
19695 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19696 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19697 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19698 		} else {
19699 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19700 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19701 		}
19702 	}
19703 }
19704 
19705 /*
19706  * Return 0 if this address can be used as local address without causing
19707  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19708  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19709  * Special checks are needed to allow the same IPv6 link-local address
19710  * on different ills.
19711  * TODO: allowing the same site-local address on different ill's.
19712  */
19713 int
19714 ip_addr_availability_check(ipif_t *new_ipif)
19715 {
19716 	in6_addr_t our_v6addr;
19717 	ill_t *ill;
19718 	ipif_t *ipif;
19719 	ill_walk_context_t ctx;
19720 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19721 
19722 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19723 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19724 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19725 
19726 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19727 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19728 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19729 		return (0);
19730 
19731 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19732 
19733 	if (new_ipif->ipif_isv6)
19734 		ill = ILL_START_WALK_V6(&ctx, ipst);
19735 	else
19736 		ill = ILL_START_WALK_V4(&ctx, ipst);
19737 
19738 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19739 		for (ipif = ill->ill_ipif; ipif != NULL;
19740 		    ipif = ipif->ipif_next) {
19741 			if ((ipif == new_ipif) ||
19742 			    !(ipif->ipif_flags & IPIF_UP) ||
19743 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19744 				continue;
19745 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19746 			    &our_v6addr)) {
19747 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19748 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19749 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19750 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19751 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19752 				    new_ipif->ipif_ill != ill)
19753 					continue;
19754 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19755 				    new_ipif->ipif_ill != ill)
19756 					continue;
19757 				else if (new_ipif->ipif_zoneid !=
19758 				    ipif->ipif_zoneid &&
19759 				    ipif->ipif_zoneid != ALL_ZONES &&
19760 				    IS_LOOPBACK(ill))
19761 					continue;
19762 				else if (new_ipif->ipif_ill == ill)
19763 					return (EADDRINUSE);
19764 				else
19765 					return (EADDRNOTAVAIL);
19766 			}
19767 		}
19768 	}
19769 
19770 	return (0);
19771 }
19772 
19773 /*
19774  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19775  * IREs for the ipif.
19776  * When the routine returns EINPROGRESS then mp has been consumed and
19777  * the ioctl will be acked from ip_rput_dlpi.
19778  */
19779 static int
19780 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19781 {
19782 	ill_t	*ill = ipif->ipif_ill;
19783 	boolean_t isv6 = ipif->ipif_isv6;
19784 	int	err = 0;
19785 	boolean_t success;
19786 
19787 	ASSERT(IAM_WRITER_IPIF(ipif));
19788 
19789 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19790 
19791 	/* Shouldn't get here if it is already up. */
19792 	if (ipif->ipif_flags & IPIF_UP)
19793 		return (EALREADY);
19794 
19795 	/* Skip arp/ndp for any loopback interface. */
19796 	if (ill->ill_wq != NULL) {
19797 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19798 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19799 
19800 		if (!ill->ill_dl_up) {
19801 			/*
19802 			 * ill_dl_up is not yet set. i.e. we are yet to
19803 			 * DL_BIND with the driver and this is the first
19804 			 * logical interface on the ill to become "up".
19805 			 * Tell the driver to get going (via DL_BIND_REQ).
19806 			 * Note that changing "significant" IFF_ flags
19807 			 * address/netmask etc cause a down/up dance, but
19808 			 * does not cause an unbind (DL_UNBIND) with the driver
19809 			 */
19810 			return (ill_dl_up(ill, ipif, mp, q));
19811 		}
19812 
19813 		/*
19814 		 * ipif_resolver_up may end up sending an
19815 		 * AR_INTERFACE_UP message to ARP, which would, in
19816 		 * turn send a DLPI message to the driver. ioctls are
19817 		 * serialized and so we cannot send more than one
19818 		 * interface up message at a time. If ipif_resolver_up
19819 		 * does send an interface up message to ARP, we get
19820 		 * EINPROGRESS and we will complete in ip_arp_done.
19821 		 */
19822 
19823 		ASSERT(connp != NULL || !CONN_Q(q));
19824 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19825 		if (connp != NULL)
19826 			mutex_enter(&connp->conn_lock);
19827 		mutex_enter(&ill->ill_lock);
19828 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19829 		mutex_exit(&ill->ill_lock);
19830 		if (connp != NULL)
19831 			mutex_exit(&connp->conn_lock);
19832 		if (!success)
19833 			return (EINTR);
19834 
19835 		/*
19836 		 * Crank up IPv6 neighbor discovery
19837 		 * Unlike ARP, this should complete when
19838 		 * ipif_ndp_up returns. However, for
19839 		 * ILLF_XRESOLV interfaces we also send a
19840 		 * AR_INTERFACE_UP to the external resolver.
19841 		 * That ioctl will complete in ip_rput.
19842 		 */
19843 		if (isv6) {
19844 			err = ipif_ndp_up(ipif);
19845 			if (err != 0) {
19846 				if (err != EINPROGRESS)
19847 					mp = ipsq_pending_mp_get(ipsq, &connp);
19848 				return (err);
19849 			}
19850 		}
19851 		/* Now, ARP */
19852 		err = ipif_resolver_up(ipif, Res_act_initial);
19853 		if (err == EINPROGRESS) {
19854 			/* We will complete it in ip_arp_done */
19855 			return (err);
19856 		}
19857 		mp = ipsq_pending_mp_get(ipsq, &connp);
19858 		ASSERT(mp != NULL);
19859 		if (err != 0)
19860 			return (err);
19861 	} else {
19862 		/*
19863 		 * Interfaces without underlying hardware don't do duplicate
19864 		 * address detection.
19865 		 */
19866 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19867 		ipif->ipif_addr_ready = 1;
19868 	}
19869 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19870 }
19871 
19872 /*
19873  * Perform a bind for the physical device.
19874  * When the routine returns EINPROGRESS then mp has been consumed and
19875  * the ioctl will be acked from ip_rput_dlpi.
19876  * Allocate an unbind message and save it until ipif_down.
19877  */
19878 static int
19879 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19880 {
19881 	areq_t	*areq;
19882 	mblk_t	*areq_mp = NULL;
19883 	mblk_t	*bind_mp = NULL;
19884 	mblk_t	*unbind_mp = NULL;
19885 	conn_t	*connp;
19886 	boolean_t success;
19887 	uint16_t sap_addr;
19888 
19889 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19890 	ASSERT(IAM_WRITER_ILL(ill));
19891 	ASSERT(mp != NULL);
19892 
19893 	/* Create a resolver cookie for ARP */
19894 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19895 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19896 		if (areq_mp == NULL)
19897 			return (ENOMEM);
19898 
19899 		freemsg(ill->ill_resolver_mp);
19900 		ill->ill_resolver_mp = areq_mp;
19901 		areq = (areq_t *)areq_mp->b_rptr;
19902 		sap_addr = ill->ill_sap;
19903 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19904 	}
19905 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19906 	    DL_BIND_REQ);
19907 	if (bind_mp == NULL)
19908 		goto bad;
19909 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19910 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19911 
19912 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19913 	if (unbind_mp == NULL)
19914 		goto bad;
19915 
19916 	/*
19917 	 * Record state needed to complete this operation when the
19918 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19919 	 */
19920 	ASSERT(WR(q)->q_next == NULL);
19921 	connp = Q_TO_CONN(q);
19922 
19923 	mutex_enter(&connp->conn_lock);
19924 	mutex_enter(&ipif->ipif_ill->ill_lock);
19925 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19926 	mutex_exit(&ipif->ipif_ill->ill_lock);
19927 	mutex_exit(&connp->conn_lock);
19928 	if (!success)
19929 		goto bad;
19930 
19931 	/*
19932 	 * Save the unbind message for ill_dl_down(); it will be consumed when
19933 	 * the interface goes down.
19934 	 */
19935 	ASSERT(ill->ill_unbind_mp == NULL);
19936 	ill->ill_unbind_mp = unbind_mp;
19937 
19938 	ill_dlpi_send(ill, bind_mp);
19939 	/* Send down link-layer capabilities probe if not already done. */
19940 	ill_capability_probe(ill);
19941 
19942 	/*
19943 	 * Sysid used to rely on the fact that netboots set domainname
19944 	 * and the like. Now that miniroot boots aren't strictly netboots
19945 	 * and miniroot network configuration is driven from userland
19946 	 * these things still need to be set. This situation can be detected
19947 	 * by comparing the interface being configured here to the one
19948 	 * dhcack was set to reference by the boot loader. Once sysid is
19949 	 * converted to use dhcp_ipc_getinfo() this call can go away.
19950 	 */
19951 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
19952 	    (strcmp(ill->ill_name, dhcack) == 0) &&
19953 	    (strlen(srpc_domain) == 0)) {
19954 		if (dhcpinit() != 0)
19955 			cmn_err(CE_WARN, "no cached dhcp response");
19956 	}
19957 
19958 	/*
19959 	 * This operation will complete in ip_rput_dlpi with either
19960 	 * a DL_BIND_ACK or DL_ERROR_ACK.
19961 	 */
19962 	return (EINPROGRESS);
19963 bad:
19964 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
19965 	/*
19966 	 * We don't have to check for possible removal from illgrp
19967 	 * as we have not yet inserted in illgrp. For groups
19968 	 * without names, this ipif is still not UP and hence
19969 	 * this could not have possibly had any influence in forming
19970 	 * groups.
19971 	 */
19972 
19973 	freemsg(bind_mp);
19974 	freemsg(unbind_mp);
19975 	return (ENOMEM);
19976 }
19977 
19978 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
19979 
19980 /*
19981  * DLPI and ARP is up.
19982  * Create all the IREs associated with an interface bring up multicast.
19983  * Set the interface flag and finish other initialization
19984  * that potentially had to be differed to after DL_BIND_ACK.
19985  */
19986 int
19987 ipif_up_done(ipif_t *ipif)
19988 {
19989 	ire_t	*ire_array[20];
19990 	ire_t	**irep = ire_array;
19991 	ire_t	**irep1;
19992 	ipaddr_t net_mask = 0;
19993 	ipaddr_t subnet_mask, route_mask;
19994 	ill_t	*ill = ipif->ipif_ill;
19995 	queue_t	*stq;
19996 	ipif_t	 *src_ipif;
19997 	ipif_t   *tmp_ipif;
19998 	boolean_t	flush_ire_cache = B_TRUE;
19999 	int	err = 0;
20000 	phyint_t *phyi;
20001 	ire_t	**ipif_saved_irep = NULL;
20002 	int ipif_saved_ire_cnt;
20003 	int	cnt;
20004 	boolean_t	src_ipif_held = B_FALSE;
20005 	boolean_t	ire_added = B_FALSE;
20006 	boolean_t	loopback = B_FALSE;
20007 	ip_stack_t	*ipst = ill->ill_ipst;
20008 
20009 	ip1dbg(("ipif_up_done(%s:%u)\n",
20010 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20011 	/* Check if this is a loopback interface */
20012 	if (ipif->ipif_ill->ill_wq == NULL)
20013 		loopback = B_TRUE;
20014 
20015 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20016 	/*
20017 	 * If all other interfaces for this ill are down or DEPRECATED,
20018 	 * or otherwise unsuitable for source address selection, remove
20019 	 * any IRE_CACHE entries for this ill to make sure source
20020 	 * address selection gets to take this new ipif into account.
20021 	 * No need to hold ill_lock while traversing the ipif list since
20022 	 * we are writer
20023 	 */
20024 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20025 	    tmp_ipif = tmp_ipif->ipif_next) {
20026 		if (((tmp_ipif->ipif_flags &
20027 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20028 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20029 		    (tmp_ipif == ipif))
20030 			continue;
20031 		/* first useable pre-existing interface */
20032 		flush_ire_cache = B_FALSE;
20033 		break;
20034 	}
20035 	if (flush_ire_cache)
20036 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20037 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20038 
20039 	/*
20040 	 * Figure out which way the send-to queue should go.  Only
20041 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20042 	 * should show up here.
20043 	 */
20044 	switch (ill->ill_net_type) {
20045 	case IRE_IF_RESOLVER:
20046 		stq = ill->ill_rq;
20047 		break;
20048 	case IRE_IF_NORESOLVER:
20049 	case IRE_LOOPBACK:
20050 		stq = ill->ill_wq;
20051 		break;
20052 	default:
20053 		return (EINVAL);
20054 	}
20055 
20056 	if (IS_LOOPBACK(ill)) {
20057 		/*
20058 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20059 		 * ipif_lookup_on_name(), but in the case of zones we can have
20060 		 * several loopback addresses on lo0. So all the interfaces with
20061 		 * loopback addresses need to be marked IRE_LOOPBACK.
20062 		 */
20063 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20064 		    htonl(INADDR_LOOPBACK))
20065 			ipif->ipif_ire_type = IRE_LOOPBACK;
20066 		else
20067 			ipif->ipif_ire_type = IRE_LOCAL;
20068 	}
20069 
20070 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20071 		/*
20072 		 * Can't use our source address. Select a different
20073 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20074 		 */
20075 		src_ipif = ipif_select_source(ipif->ipif_ill,
20076 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20077 		if (src_ipif == NULL)
20078 			src_ipif = ipif;	/* Last resort */
20079 		else
20080 			src_ipif_held = B_TRUE;
20081 	} else {
20082 		src_ipif = ipif;
20083 	}
20084 
20085 	/* Create all the IREs associated with this interface */
20086 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20087 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20088 
20089 		/*
20090 		 * If we're on a labeled system then make sure that zone-
20091 		 * private addresses have proper remote host database entries.
20092 		 */
20093 		if (is_system_labeled() &&
20094 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20095 		    !tsol_check_interface_address(ipif))
20096 			return (EINVAL);
20097 
20098 		/* Register the source address for __sin6_src_id */
20099 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20100 		    ipif->ipif_zoneid, ipst);
20101 		if (err != 0) {
20102 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20103 			return (err);
20104 		}
20105 
20106 		/* If the interface address is set, create the local IRE. */
20107 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20108 		    (void *)ipif,
20109 		    ipif->ipif_ire_type,
20110 		    ntohl(ipif->ipif_lcl_addr)));
20111 		*irep++ = ire_create(
20112 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20113 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20114 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20115 		    NULL,				/* no gateway */
20116 		    &ip_loopback_mtuplus,		/* max frag size */
20117 		    NULL,
20118 		    ipif->ipif_rq,			/* recv-from queue */
20119 		    NULL,				/* no send-to queue */
20120 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20121 		    ipif,
20122 		    0,
20123 		    0,
20124 		    0,
20125 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20126 		    RTF_PRIVATE : 0,
20127 		    &ire_uinfo_null,
20128 		    NULL,
20129 		    NULL,
20130 		    ipst);
20131 	} else {
20132 		ip1dbg((
20133 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20134 		    ipif->ipif_ire_type,
20135 		    ntohl(ipif->ipif_lcl_addr),
20136 		    (uint_t)ipif->ipif_flags));
20137 	}
20138 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20139 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20140 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20141 	} else {
20142 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20143 	}
20144 
20145 	subnet_mask = ipif->ipif_net_mask;
20146 
20147 	/*
20148 	 * If mask was not specified, use natural netmask of
20149 	 * interface address. Also, store this mask back into the
20150 	 * ipif struct.
20151 	 */
20152 	if (subnet_mask == 0) {
20153 		subnet_mask = net_mask;
20154 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20155 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20156 		    ipif->ipif_v6subnet);
20157 	}
20158 
20159 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20160 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20161 	    ipif->ipif_subnet != INADDR_ANY) {
20162 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20163 
20164 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20165 			route_mask = IP_HOST_MASK;
20166 		} else {
20167 			route_mask = subnet_mask;
20168 		}
20169 
20170 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20171 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20172 		    (void *)ipif, (void *)ill,
20173 		    ill->ill_net_type,
20174 		    ntohl(ipif->ipif_subnet)));
20175 		*irep++ = ire_create(
20176 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20177 		    (uchar_t *)&route_mask,		/* mask */
20178 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20179 		    NULL,				/* no gateway */
20180 		    &ipif->ipif_mtu,			/* max frag */
20181 		    NULL,
20182 		    NULL,				/* no recv queue */
20183 		    stq,				/* send-to queue */
20184 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20185 		    ipif,
20186 		    0,
20187 		    0,
20188 		    0,
20189 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20190 		    &ire_uinfo_null,
20191 		    NULL,
20192 		    NULL,
20193 		    ipst);
20194 	}
20195 
20196 	/*
20197 	 * Create any necessary broadcast IREs.
20198 	 */
20199 	if ((ipif->ipif_subnet != INADDR_ANY) &&
20200 	    (ipif->ipif_flags & IPIF_BROADCAST))
20201 		irep = ipif_create_bcast_ires(ipif, irep);
20202 
20203 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20204 
20205 	/* If an earlier ire_create failed, get out now */
20206 	for (irep1 = irep; irep1 > ire_array; ) {
20207 		irep1--;
20208 		if (*irep1 == NULL) {
20209 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20210 			err = ENOMEM;
20211 			goto bad;
20212 		}
20213 	}
20214 
20215 	/*
20216 	 * Need to atomically check for ip_addr_availablity_check
20217 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20218 	 * from group also.The ill_g_lock is grabbed as reader
20219 	 * just to make sure no new ills or new ipifs are being added
20220 	 * to the system while we are checking the uniqueness of addresses.
20221 	 */
20222 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20223 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20224 	/* Mark it up, and increment counters. */
20225 	ipif->ipif_flags |= IPIF_UP;
20226 	ill->ill_ipif_up_count++;
20227 	err = ip_addr_availability_check(ipif);
20228 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20229 	rw_exit(&ipst->ips_ill_g_lock);
20230 
20231 	if (err != 0) {
20232 		/*
20233 		 * Our address may already be up on the same ill. In this case,
20234 		 * the ARP entry for our ipif replaced the one for the other
20235 		 * ipif. So we don't want to delete it (otherwise the other ipif
20236 		 * would be unable to send packets).
20237 		 * ip_addr_availability_check() identifies this case for us and
20238 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20239 		 * which is the expected error code.
20240 		 */
20241 		if (err == EADDRINUSE) {
20242 			freemsg(ipif->ipif_arp_del_mp);
20243 			ipif->ipif_arp_del_mp = NULL;
20244 			err = EADDRNOTAVAIL;
20245 		}
20246 		ill->ill_ipif_up_count--;
20247 		ipif->ipif_flags &= ~IPIF_UP;
20248 		goto bad;
20249 	}
20250 
20251 	/*
20252 	 * Add in all newly created IREs.  ire_create_bcast() has
20253 	 * already checked for duplicates of the IRE_BROADCAST type.
20254 	 * We want to add before we call ifgrp_insert which wants
20255 	 * to know whether IRE_IF_RESOLVER exists or not.
20256 	 *
20257 	 * NOTE : We refrele the ire though we may branch to "bad"
20258 	 *	  later on where we do ire_delete. This is okay
20259 	 *	  because nobody can delete it as we are running
20260 	 *	  exclusively.
20261 	 */
20262 	for (irep1 = irep; irep1 > ire_array; ) {
20263 		irep1--;
20264 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20265 		/*
20266 		 * refheld by ire_add. refele towards the end of the func
20267 		 */
20268 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20269 	}
20270 	ire_added = B_TRUE;
20271 	/*
20272 	 * Form groups if possible.
20273 	 *
20274 	 * If we are supposed to be in a ill_group with a name, insert it
20275 	 * now as we know that at least one ipif is UP. Otherwise form
20276 	 * nameless groups.
20277 	 *
20278 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20279 	 * this ipif into the appropriate interface group, or create a
20280 	 * new one. If this is already in a nameless group, we try to form
20281 	 * a bigger group looking at other ills potentially sharing this
20282 	 * ipif's prefix.
20283 	 */
20284 	phyi = ill->ill_phyint;
20285 	if (phyi->phyint_groupname_len != 0) {
20286 		ASSERT(phyi->phyint_groupname != NULL);
20287 		if (ill->ill_ipif_up_count == 1) {
20288 			ASSERT(ill->ill_group == NULL);
20289 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20290 			    phyi->phyint_groupname, NULL, B_TRUE);
20291 			if (err != 0) {
20292 				ip1dbg(("ipif_up_done: illgrp allocation "
20293 				    "failed, error %d\n", err));
20294 				goto bad;
20295 			}
20296 		}
20297 		ASSERT(ill->ill_group != NULL);
20298 	}
20299 
20300 	/*
20301 	 * When this is part of group, we need to make sure that
20302 	 * any broadcast ires created because of this ipif coming
20303 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20304 	 * so that we don't receive duplicate broadcast packets.
20305 	 */
20306 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20307 		ipif_renominate_bcast(ipif);
20308 
20309 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20310 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20311 	ipif_saved_irep = ipif_recover_ire(ipif);
20312 
20313 	if (!loopback) {
20314 		/*
20315 		 * If the broadcast address has been set, make sure it makes
20316 		 * sense based on the interface address.
20317 		 * Only match on ill since we are sharing broadcast addresses.
20318 		 */
20319 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20320 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20321 			ire_t	*ire;
20322 
20323 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20324 			    IRE_BROADCAST, ipif, ALL_ZONES,
20325 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20326 
20327 			if (ire == NULL) {
20328 				/*
20329 				 * If there isn't a matching broadcast IRE,
20330 				 * revert to the default for this netmask.
20331 				 */
20332 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20333 				mutex_enter(&ipif->ipif_ill->ill_lock);
20334 				ipif_set_default(ipif);
20335 				mutex_exit(&ipif->ipif_ill->ill_lock);
20336 			} else {
20337 				ire_refrele(ire);
20338 			}
20339 		}
20340 
20341 	}
20342 
20343 	/* This is the first interface on this ill */
20344 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20345 		/*
20346 		 * Need to recover all multicast memberships in the driver.
20347 		 * This had to be deferred until we had attached.
20348 		 */
20349 		ill_recover_multicast(ill);
20350 	}
20351 	/* Join the allhosts multicast address */
20352 	ipif_multicast_up(ipif);
20353 
20354 	if (!loopback) {
20355 		/*
20356 		 * See whether anybody else would benefit from the
20357 		 * new ipif that we added. We call this always rather
20358 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20359 		 * ipif is for the benefit of illgrp_insert (done above)
20360 		 * which does not do source address selection as it does
20361 		 * not want to re-create interface routes that we are
20362 		 * having reference to it here.
20363 		 */
20364 		ill_update_source_selection(ill);
20365 	}
20366 
20367 	for (irep1 = irep; irep1 > ire_array; ) {
20368 		irep1--;
20369 		if (*irep1 != NULL) {
20370 			/* was held in ire_add */
20371 			ire_refrele(*irep1);
20372 		}
20373 	}
20374 
20375 	cnt = ipif_saved_ire_cnt;
20376 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20377 		if (*irep1 != NULL) {
20378 			/* was held in ire_add */
20379 			ire_refrele(*irep1);
20380 		}
20381 	}
20382 
20383 	if (!loopback && ipif->ipif_addr_ready) {
20384 		/* Broadcast an address mask reply. */
20385 		ipif_mask_reply(ipif);
20386 	}
20387 	if (ipif_saved_irep != NULL) {
20388 		kmem_free(ipif_saved_irep,
20389 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20390 	}
20391 	if (src_ipif_held)
20392 		ipif_refrele(src_ipif);
20393 
20394 	/*
20395 	 * This had to be deferred until we had bound.  Tell routing sockets and
20396 	 * others that this interface is up if it looks like the address has
20397 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20398 	 * duplicate address detection to do its thing.
20399 	 */
20400 	if (ipif->ipif_addr_ready) {
20401 		ip_rts_ifmsg(ipif);
20402 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20403 		/* Let SCTP update the status for this ipif */
20404 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20405 	}
20406 	return (0);
20407 
20408 bad:
20409 	ip1dbg(("ipif_up_done: FAILED \n"));
20410 	/*
20411 	 * We don't have to bother removing from ill groups because
20412 	 *
20413 	 * 1) For groups with names, we insert only when the first ipif
20414 	 *    comes up. In that case if it fails, it will not be in any
20415 	 *    group. So, we need not try to remove for that case.
20416 	 *
20417 	 * 2) For groups without names, either we tried to insert ipif_ill
20418 	 *    in a group as singleton or found some other group to become
20419 	 *    a bigger group. For the former, if it fails we don't have
20420 	 *    anything to do as ipif_ill is not in the group and for the
20421 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20422 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20423 	 */
20424 	while (irep > ire_array) {
20425 		irep--;
20426 		if (*irep != NULL) {
20427 			ire_delete(*irep);
20428 			if (ire_added)
20429 				ire_refrele(*irep);
20430 		}
20431 	}
20432 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20433 
20434 	if (ipif_saved_irep != NULL) {
20435 		kmem_free(ipif_saved_irep,
20436 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20437 	}
20438 	if (src_ipif_held)
20439 		ipif_refrele(src_ipif);
20440 
20441 	ipif_arp_down(ipif);
20442 	return (err);
20443 }
20444 
20445 /*
20446  * Turn off the ARP with the ILLF_NOARP flag.
20447  */
20448 static int
20449 ill_arp_off(ill_t *ill)
20450 {
20451 	mblk_t	*arp_off_mp = NULL;
20452 	mblk_t	*arp_on_mp = NULL;
20453 
20454 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20455 
20456 	ASSERT(IAM_WRITER_ILL(ill));
20457 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20458 
20459 	/*
20460 	 * If the on message is still around we've already done
20461 	 * an arp_off without doing an arp_on thus there is no
20462 	 * work needed.
20463 	 */
20464 	if (ill->ill_arp_on_mp != NULL)
20465 		return (0);
20466 
20467 	/*
20468 	 * Allocate an ARP on message (to be saved) and an ARP off message
20469 	 */
20470 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20471 	if (!arp_off_mp)
20472 		return (ENOMEM);
20473 
20474 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20475 	if (!arp_on_mp)
20476 		goto failed;
20477 
20478 	ASSERT(ill->ill_arp_on_mp == NULL);
20479 	ill->ill_arp_on_mp = arp_on_mp;
20480 
20481 	/* Send an AR_INTERFACE_OFF request */
20482 	putnext(ill->ill_rq, arp_off_mp);
20483 	return (0);
20484 failed:
20485 
20486 	if (arp_off_mp)
20487 		freemsg(arp_off_mp);
20488 	return (ENOMEM);
20489 }
20490 
20491 /*
20492  * Turn on ARP by turning off the ILLF_NOARP flag.
20493  */
20494 static int
20495 ill_arp_on(ill_t *ill)
20496 {
20497 	mblk_t	*mp;
20498 
20499 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20500 
20501 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20502 
20503 	ASSERT(IAM_WRITER_ILL(ill));
20504 	/*
20505 	 * Send an AR_INTERFACE_ON request if we have already done
20506 	 * an arp_off (which allocated the message).
20507 	 */
20508 	if (ill->ill_arp_on_mp != NULL) {
20509 		mp = ill->ill_arp_on_mp;
20510 		ill->ill_arp_on_mp = NULL;
20511 		putnext(ill->ill_rq, mp);
20512 	}
20513 	return (0);
20514 }
20515 
20516 /*
20517  * Called after either deleting ill from the group or when setting
20518  * FAILED or STANDBY on the interface.
20519  */
20520 static void
20521 illgrp_reset_schednext(ill_t *ill)
20522 {
20523 	ill_group_t *illgrp;
20524 	ill_t *save_ill;
20525 
20526 	ASSERT(IAM_WRITER_ILL(ill));
20527 	/*
20528 	 * When called from illgrp_delete, ill_group will be non-NULL.
20529 	 * But when called from ip_sioctl_flags, it could be NULL if
20530 	 * somebody is setting FAILED/INACTIVE on some interface which
20531 	 * is not part of a group.
20532 	 */
20533 	illgrp = ill->ill_group;
20534 	if (illgrp == NULL)
20535 		return;
20536 	if (illgrp->illgrp_ill_schednext != ill)
20537 		return;
20538 
20539 	illgrp->illgrp_ill_schednext = NULL;
20540 	save_ill = ill;
20541 	/*
20542 	 * Choose a good ill to be the next one for
20543 	 * outbound traffic. As the flags FAILED/STANDBY is
20544 	 * not yet marked when called from ip_sioctl_flags,
20545 	 * we check for ill separately.
20546 	 */
20547 	for (ill = illgrp->illgrp_ill; ill != NULL;
20548 	    ill = ill->ill_group_next) {
20549 		if ((ill != save_ill) &&
20550 		    !(ill->ill_phyint->phyint_flags &
20551 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20552 			illgrp->illgrp_ill_schednext = ill;
20553 			return;
20554 		}
20555 	}
20556 }
20557 
20558 /*
20559  * Given an ill, find the next ill in the group to be scheduled.
20560  * (This should be called by ip_newroute() before ire_create().)
20561  * The passed in ill may be pulled out of the group, after we have picked
20562  * up a different outgoing ill from the same group. However ire add will
20563  * atomically check this.
20564  */
20565 ill_t *
20566 illgrp_scheduler(ill_t *ill)
20567 {
20568 	ill_t *retill;
20569 	ill_group_t *illgrp;
20570 	int illcnt;
20571 	int i;
20572 	uint64_t flags;
20573 	ip_stack_t	*ipst = ill->ill_ipst;
20574 
20575 	/*
20576 	 * We don't use a lock to check for the ill_group. If this ill
20577 	 * is currently being inserted we may end up just returning this
20578 	 * ill itself. That is ok.
20579 	 */
20580 	if (ill->ill_group == NULL) {
20581 		ill_refhold(ill);
20582 		return (ill);
20583 	}
20584 
20585 	/*
20586 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20587 	 * a set of stable ills. No ill can be added or deleted or change
20588 	 * group while we hold the reader lock.
20589 	 */
20590 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20591 	if ((illgrp = ill->ill_group) == NULL) {
20592 		rw_exit(&ipst->ips_ill_g_lock);
20593 		ill_refhold(ill);
20594 		return (ill);
20595 	}
20596 
20597 	illcnt = illgrp->illgrp_ill_count;
20598 	mutex_enter(&illgrp->illgrp_lock);
20599 	retill = illgrp->illgrp_ill_schednext;
20600 
20601 	if (retill == NULL)
20602 		retill = illgrp->illgrp_ill;
20603 
20604 	/*
20605 	 * We do a circular search beginning at illgrp_ill_schednext
20606 	 * or illgrp_ill. We don't check the flags against the ill lock
20607 	 * since it can change anytime. The ire creation will be atomic
20608 	 * and will fail if the ill is FAILED or OFFLINE.
20609 	 */
20610 	for (i = 0; i < illcnt; i++) {
20611 		flags = retill->ill_phyint->phyint_flags;
20612 
20613 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20614 		    ILL_CAN_LOOKUP(retill)) {
20615 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20616 			ill_refhold(retill);
20617 			break;
20618 		}
20619 		retill = retill->ill_group_next;
20620 		if (retill == NULL)
20621 			retill = illgrp->illgrp_ill;
20622 	}
20623 	mutex_exit(&illgrp->illgrp_lock);
20624 	rw_exit(&ipst->ips_ill_g_lock);
20625 
20626 	return (i == illcnt ? NULL : retill);
20627 }
20628 
20629 /*
20630  * Checks for availbility of a usable source address (if there is one) when the
20631  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20632  * this selection is done regardless of the destination.
20633  */
20634 boolean_t
20635 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20636 {
20637 	uint_t	ifindex;
20638 	ipif_t	*ipif = NULL;
20639 	ill_t	*uill;
20640 	boolean_t isv6;
20641 	ip_stack_t	*ipst = ill->ill_ipst;
20642 
20643 	ASSERT(ill != NULL);
20644 
20645 	isv6 = ill->ill_isv6;
20646 	ifindex = ill->ill_usesrc_ifindex;
20647 	if (ifindex != 0) {
20648 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20649 		    NULL, ipst);
20650 		if (uill == NULL)
20651 			return (NULL);
20652 		mutex_enter(&uill->ill_lock);
20653 		for (ipif = uill->ill_ipif; ipif != NULL;
20654 		    ipif = ipif->ipif_next) {
20655 			if (!IPIF_CAN_LOOKUP(ipif))
20656 				continue;
20657 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20658 				continue;
20659 			if (!(ipif->ipif_flags & IPIF_UP))
20660 				continue;
20661 			if (ipif->ipif_zoneid != zoneid)
20662 				continue;
20663 			if ((isv6 &&
20664 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20665 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20666 				continue;
20667 			mutex_exit(&uill->ill_lock);
20668 			ill_refrele(uill);
20669 			return (B_TRUE);
20670 		}
20671 		mutex_exit(&uill->ill_lock);
20672 		ill_refrele(uill);
20673 	}
20674 	return (B_FALSE);
20675 }
20676 
20677 /*
20678  * Determine the best source address given a destination address and an ill.
20679  * Prefers non-deprecated over deprecated but will return a deprecated
20680  * address if there is no other choice. If there is a usable source address
20681  * on the interface pointed to by ill_usesrc_ifindex then that is given
20682  * first preference.
20683  *
20684  * Returns NULL if there is no suitable source address for the ill.
20685  * This only occurs when there is no valid source address for the ill.
20686  */
20687 ipif_t *
20688 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20689 {
20690 	ipif_t *ipif;
20691 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20692 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20693 	int index = 0;
20694 	boolean_t wrapped = B_FALSE;
20695 	boolean_t same_subnet_only = B_FALSE;
20696 	boolean_t ipif_same_found, ipif_other_found;
20697 	boolean_t specific_found;
20698 	ill_t	*till, *usill = NULL;
20699 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20700 	ip_stack_t	*ipst = ill->ill_ipst;
20701 
20702 	if (ill->ill_usesrc_ifindex != 0) {
20703 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20704 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20705 		if (usill != NULL)
20706 			ill = usill;	/* Select source from usesrc ILL */
20707 		else
20708 			return (NULL);
20709 	}
20710 
20711 	/*
20712 	 * If we're dealing with an unlabeled destination on a labeled system,
20713 	 * make sure that we ignore source addresses that are incompatible with
20714 	 * the destination's default label.  That destination's default label
20715 	 * must dominate the minimum label on the source address.
20716 	 */
20717 	dst_rhtp = NULL;
20718 	if (is_system_labeled()) {
20719 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20720 		if (dst_rhtp == NULL)
20721 			return (NULL);
20722 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20723 			TPC_RELE(dst_rhtp);
20724 			dst_rhtp = NULL;
20725 		}
20726 	}
20727 
20728 	/*
20729 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20730 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20731 	 * After selecting the right ipif, under ill_lock make sure ipif is
20732 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20733 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20734 	 * but not under a lock.
20735 	 */
20736 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20737 
20738 retry:
20739 	till = ill;
20740 	ipif_arr[0] = NULL;
20741 
20742 	if (till->ill_group != NULL)
20743 		till = till->ill_group->illgrp_ill;
20744 
20745 	/*
20746 	 * Choose one good source address from each ill across the group.
20747 	 * If possible choose a source address in the same subnet as
20748 	 * the destination address.
20749 	 *
20750 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20751 	 * This is okay because of the following.
20752 	 *
20753 	 *    If PHYI_FAILED is set and we still have non-deprecated
20754 	 *    addresses, it means the addresses have not yet been
20755 	 *    failed over to a different interface. We potentially
20756 	 *    select them to create IRE_CACHES, which will be later
20757 	 *    flushed when the addresses move over.
20758 	 *
20759 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20760 	 *    addresses, it means either the user has configured them
20761 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20762 	 *    been moved over. For the former, in.mpathd does a failover
20763 	 *    when the interface becomes INACTIVE and hence we should
20764 	 *    not find them. Once INACTIVE is set, we don't allow them
20765 	 *    to create logical interfaces anymore. For the latter, a
20766 	 *    flush will happen when INACTIVE is cleared which will
20767 	 *    flush the IRE_CACHES.
20768 	 *
20769 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20770 	 *    over soon. We potentially select them to create IRE_CACHEs,
20771 	 *    which will be later flushed when the addresses move over.
20772 	 *
20773 	 * NOTE : As ipif_select_source is called to borrow source address
20774 	 * for an ipif that is part of a group, source address selection
20775 	 * will be re-done whenever the group changes i.e either an
20776 	 * insertion/deletion in the group.
20777 	 *
20778 	 * Fill ipif_arr[] with source addresses, using these rules:
20779 	 *
20780 	 *	1. At most one source address from a given ill ends up
20781 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20782 	 *	   associated with a given ill ends up in ipif_arr[].
20783 	 *
20784 	 *	2. If there is at least one non-deprecated ipif in the
20785 	 *	   IPMP group with a source address on the same subnet as
20786 	 *	   our destination, then fill ipif_arr[] only with
20787 	 *	   source addresses on the same subnet as our destination.
20788 	 *	   Note that because of (1), only the first
20789 	 *	   non-deprecated ipif found with a source address
20790 	 *	   matching the destination ends up in ipif_arr[].
20791 	 *
20792 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20793 	 *	   addresses not in the same subnet as our destination.
20794 	 *	   Again, because of (1), only the first off-subnet source
20795 	 *	   address will be chosen.
20796 	 *
20797 	 *	4. If there are no non-deprecated ipifs, then just use
20798 	 *	   the source address associated with the last deprecated
20799 	 *	   one we find that happens to be on the same subnet,
20800 	 *	   otherwise the first one not in the same subnet.
20801 	 */
20802 	specific_found = B_FALSE;
20803 	for (; till != NULL; till = till->ill_group_next) {
20804 		ipif_same_found = B_FALSE;
20805 		ipif_other_found = B_FALSE;
20806 		for (ipif = till->ill_ipif; ipif != NULL;
20807 		    ipif = ipif->ipif_next) {
20808 			if (!IPIF_CAN_LOOKUP(ipif))
20809 				continue;
20810 			/* Always skip NOLOCAL and ANYCAST interfaces */
20811 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20812 				continue;
20813 			if (!(ipif->ipif_flags & IPIF_UP) ||
20814 			    !ipif->ipif_addr_ready)
20815 				continue;
20816 			if (ipif->ipif_zoneid != zoneid &&
20817 			    ipif->ipif_zoneid != ALL_ZONES)
20818 				continue;
20819 			/*
20820 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20821 			 * but are not valid as source addresses.
20822 			 */
20823 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20824 				continue;
20825 
20826 			/*
20827 			 * Check compatibility of local address for
20828 			 * destination's default label if we're on a labeled
20829 			 * system.  Incompatible addresses can't be used at
20830 			 * all.
20831 			 */
20832 			if (dst_rhtp != NULL) {
20833 				boolean_t incompat;
20834 
20835 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20836 				    IPV4_VERSION, B_FALSE);
20837 				if (src_rhtp == NULL)
20838 					continue;
20839 				incompat =
20840 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20841 				    src_rhtp->tpc_tp.tp_doi !=
20842 				    dst_rhtp->tpc_tp.tp_doi ||
20843 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20844 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20845 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20846 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20847 				TPC_RELE(src_rhtp);
20848 				if (incompat)
20849 					continue;
20850 			}
20851 
20852 			/*
20853 			 * We prefer not to use all all-zones addresses, if we
20854 			 * can avoid it, as they pose problems with unlabeled
20855 			 * destinations.
20856 			 */
20857 			if (ipif->ipif_zoneid != ALL_ZONES) {
20858 				if (!specific_found &&
20859 				    (!same_subnet_only ||
20860 				    (ipif->ipif_net_mask & dst) ==
20861 				    ipif->ipif_subnet)) {
20862 					index = 0;
20863 					specific_found = B_TRUE;
20864 					ipif_other_found = B_FALSE;
20865 				}
20866 			} else {
20867 				if (specific_found)
20868 					continue;
20869 			}
20870 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20871 				if (ipif_dep == NULL ||
20872 				    (ipif->ipif_net_mask & dst) ==
20873 				    ipif->ipif_subnet)
20874 					ipif_dep = ipif;
20875 				continue;
20876 			}
20877 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20878 				/* found a source address in the same subnet */
20879 				if (!same_subnet_only) {
20880 					same_subnet_only = B_TRUE;
20881 					index = 0;
20882 				}
20883 				ipif_same_found = B_TRUE;
20884 			} else {
20885 				if (same_subnet_only || ipif_other_found)
20886 					continue;
20887 				ipif_other_found = B_TRUE;
20888 			}
20889 			ipif_arr[index++] = ipif;
20890 			if (index == MAX_IPIF_SELECT_SOURCE) {
20891 				wrapped = B_TRUE;
20892 				index = 0;
20893 			}
20894 			if (ipif_same_found)
20895 				break;
20896 		}
20897 	}
20898 
20899 	if (ipif_arr[0] == NULL) {
20900 		ipif = ipif_dep;
20901 	} else {
20902 		if (wrapped)
20903 			index = MAX_IPIF_SELECT_SOURCE;
20904 		ipif = ipif_arr[ipif_rand(ipst) % index];
20905 		ASSERT(ipif != NULL);
20906 	}
20907 
20908 	if (ipif != NULL) {
20909 		mutex_enter(&ipif->ipif_ill->ill_lock);
20910 		if (!IPIF_CAN_LOOKUP(ipif)) {
20911 			mutex_exit(&ipif->ipif_ill->ill_lock);
20912 			goto retry;
20913 		}
20914 		ipif_refhold_locked(ipif);
20915 		mutex_exit(&ipif->ipif_ill->ill_lock);
20916 	}
20917 
20918 	rw_exit(&ipst->ips_ill_g_lock);
20919 	if (usill != NULL)
20920 		ill_refrele(usill);
20921 	if (dst_rhtp != NULL)
20922 		TPC_RELE(dst_rhtp);
20923 
20924 #ifdef DEBUG
20925 	if (ipif == NULL) {
20926 		char buf1[INET6_ADDRSTRLEN];
20927 
20928 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20929 		    ill->ill_name,
20930 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20931 	} else {
20932 		char buf1[INET6_ADDRSTRLEN];
20933 		char buf2[INET6_ADDRSTRLEN];
20934 
20935 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20936 		    ipif->ipif_ill->ill_name,
20937 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20938 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20939 		    buf2, sizeof (buf2))));
20940 	}
20941 #endif /* DEBUG */
20942 	return (ipif);
20943 }
20944 
20945 
20946 /*
20947  * If old_ipif is not NULL, see if ipif was derived from old
20948  * ipif and if so, recreate the interface route by re-doing
20949  * source address selection. This happens when ipif_down ->
20950  * ipif_update_other_ipifs calls us.
20951  *
20952  * If old_ipif is NULL, just redo the source address selection
20953  * if needed. This happens when illgrp_insert or ipif_up_done
20954  * calls us.
20955  */
20956 static void
20957 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20958 {
20959 	ire_t *ire;
20960 	ire_t *ipif_ire;
20961 	queue_t *stq;
20962 	ipif_t *nipif;
20963 	ill_t *ill;
20964 	boolean_t need_rele = B_FALSE;
20965 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
20966 
20967 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20968 	ASSERT(IAM_WRITER_IPIF(ipif));
20969 
20970 	ill = ipif->ipif_ill;
20971 	if (!(ipif->ipif_flags &
20972 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20973 		/*
20974 		 * Can't possibly have borrowed the source
20975 		 * from old_ipif.
20976 		 */
20977 		return;
20978 	}
20979 
20980 	/*
20981 	 * Is there any work to be done? No work if the address
20982 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20983 	 * ipif_select_source() does not borrow addresses from
20984 	 * NOLOCAL and ANYCAST interfaces).
20985 	 */
20986 	if ((old_ipif != NULL) &&
20987 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20988 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20989 	    (old_ipif->ipif_flags &
20990 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20991 		return;
20992 	}
20993 
20994 	/*
20995 	 * Perform the same checks as when creating the
20996 	 * IRE_INTERFACE in ipif_up_done.
20997 	 */
20998 	if (!(ipif->ipif_flags & IPIF_UP))
20999 		return;
21000 
21001 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21002 	    (ipif->ipif_subnet == INADDR_ANY))
21003 		return;
21004 
21005 	ipif_ire = ipif_to_ire(ipif);
21006 	if (ipif_ire == NULL)
21007 		return;
21008 
21009 	/*
21010 	 * We know that ipif uses some other source for its
21011 	 * IRE_INTERFACE. Is it using the source of this
21012 	 * old_ipif?
21013 	 */
21014 	if (old_ipif != NULL &&
21015 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21016 		ire_refrele(ipif_ire);
21017 		return;
21018 	}
21019 	if (ip_debug > 2) {
21020 		/* ip1dbg */
21021 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21022 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21023 	}
21024 
21025 	stq = ipif_ire->ire_stq;
21026 
21027 	/*
21028 	 * Can't use our source address. Select a different
21029 	 * source address for the IRE_INTERFACE.
21030 	 */
21031 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21032 	if (nipif == NULL) {
21033 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21034 		nipif = ipif;
21035 	} else {
21036 		need_rele = B_TRUE;
21037 	}
21038 
21039 	ire = ire_create(
21040 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21041 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21042 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21043 	    NULL,				/* no gateway */
21044 	    &ipif->ipif_mtu,			/* max frag */
21045 	    NULL,				/* no src nce */
21046 	    NULL,				/* no recv from queue */
21047 	    stq,				/* send-to queue */
21048 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21049 	    ipif,
21050 	    0,
21051 	    0,
21052 	    0,
21053 	    0,
21054 	    &ire_uinfo_null,
21055 	    NULL,
21056 	    NULL,
21057 	    ipst);
21058 
21059 	if (ire != NULL) {
21060 		ire_t *ret_ire;
21061 		int error;
21062 
21063 		/*
21064 		 * We don't need ipif_ire anymore. We need to delete
21065 		 * before we add so that ire_add does not detect
21066 		 * duplicates.
21067 		 */
21068 		ire_delete(ipif_ire);
21069 		ret_ire = ire;
21070 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21071 		ASSERT(error == 0);
21072 		ASSERT(ire == ret_ire);
21073 		/* Held in ire_add */
21074 		ire_refrele(ret_ire);
21075 	}
21076 	/*
21077 	 * Either we are falling through from above or could not
21078 	 * allocate a replacement.
21079 	 */
21080 	ire_refrele(ipif_ire);
21081 	if (need_rele)
21082 		ipif_refrele(nipif);
21083 }
21084 
21085 /*
21086  * This old_ipif is going away.
21087  *
21088  * Determine if any other ipif's is using our address as
21089  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21090  * IPIF_DEPRECATED).
21091  * Find the IRE_INTERFACE for such ipifs and recreate them
21092  * to use an different source address following the rules in
21093  * ipif_up_done.
21094  *
21095  * This function takes an illgrp as an argument so that illgrp_delete
21096  * can call this to update source address even after deleting the
21097  * old_ipif->ipif_ill from the ill group.
21098  */
21099 static void
21100 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21101 {
21102 	ipif_t *ipif;
21103 	ill_t *ill;
21104 	char	buf[INET6_ADDRSTRLEN];
21105 
21106 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21107 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21108 
21109 	ill = old_ipif->ipif_ill;
21110 
21111 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21112 	    ill->ill_name,
21113 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21114 	    buf, sizeof (buf))));
21115 	/*
21116 	 * If this part of a group, look at all ills as ipif_select_source
21117 	 * borrows source address across all the ills in the group.
21118 	 */
21119 	if (illgrp != NULL)
21120 		ill = illgrp->illgrp_ill;
21121 
21122 	for (; ill != NULL; ill = ill->ill_group_next) {
21123 		for (ipif = ill->ill_ipif; ipif != NULL;
21124 		    ipif = ipif->ipif_next) {
21125 
21126 			if (ipif == old_ipif)
21127 				continue;
21128 
21129 			ipif_recreate_interface_routes(old_ipif, ipif);
21130 		}
21131 	}
21132 }
21133 
21134 /* ARGSUSED */
21135 int
21136 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21137 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21138 {
21139 	/*
21140 	 * ill_phyint_reinit merged the v4 and v6 into a single
21141 	 * ipsq. Could also have become part of a ipmp group in the
21142 	 * process, and we might not have been able to complete the
21143 	 * operation in ipif_set_values, if we could not become
21144 	 * exclusive.  If so restart it here.
21145 	 */
21146 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21147 }
21148 
21149 
21150 /*
21151  * Can operate on either a module or a driver queue.
21152  * Returns an error if not a module queue.
21153  */
21154 /* ARGSUSED */
21155 int
21156 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21157     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21158 {
21159 	queue_t		*q1 = q;
21160 	char 		*cp;
21161 	char		interf_name[LIFNAMSIZ];
21162 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21163 
21164 	if (q->q_next == NULL) {
21165 		ip1dbg((
21166 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21167 		return (EINVAL);
21168 	}
21169 
21170 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21171 		return (EALREADY);
21172 
21173 	do {
21174 		q1 = q1->q_next;
21175 	} while (q1->q_next);
21176 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21177 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21178 
21179 	/*
21180 	 * Here we are not going to delay the ioack until after
21181 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21182 	 * original ioctl message before sending the requests.
21183 	 */
21184 	return (ipif_set_values(q, mp, interf_name, &ppa));
21185 }
21186 
21187 /* ARGSUSED */
21188 int
21189 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21190     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21191 {
21192 	return (ENXIO);
21193 }
21194 
21195 /*
21196  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21197  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21198  * ire_check_and_create_bcast()).
21199  */
21200 static ire_t **
21201 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21202 {
21203 	ipaddr_t addr;
21204 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21205 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21206 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21207 
21208 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21209 
21210 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21211 
21212 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21213 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21214 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21215 
21216 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21217 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21218 
21219 	/*
21220 	 * For backward compatibility, we create net broadcast IREs based on
21221 	 * the old "IP address class system", since some old machines only
21222 	 * respond to these class derived net broadcast.  However, we must not
21223 	 * create these net broadcast IREs if the subnetmask is shorter than
21224 	 * the IP address class based derived netmask.  Otherwise, we may
21225 	 * create a net broadcast address which is the same as an IP address
21226 	 * on the subnet -- and then TCP will refuse to talk to that address.
21227 	 */
21228 	if (netmask < subnetmask) {
21229 		addr = netmask & ipif->ipif_subnet;
21230 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21231 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21232 		    flags);
21233 	}
21234 
21235 	/*
21236 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21237 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21238 	 * created.  Creating these broadcast IREs will only create confusion
21239 	 * as `addr' will be the same as the IP address.
21240 	 */
21241 	if (subnetmask != 0xFFFFFFFF) {
21242 		addr = ipif->ipif_subnet;
21243 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21244 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21245 		    irep, flags);
21246 	}
21247 
21248 	return (irep);
21249 }
21250 
21251 /*
21252  * Broadcast IRE info structure used in the functions below.  Since we
21253  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21254  */
21255 typedef struct bcast_ireinfo {
21256 	uchar_t		bi_type;	/* BCAST_* value from below */
21257 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21258 			bi_needrep:1,	/* do we need to replace it? */
21259 			bi_haverep:1,	/* have we replaced it? */
21260 			bi_pad:5;
21261 	ipaddr_t	bi_addr;	/* IRE address */
21262 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21263 } bcast_ireinfo_t;
21264 
21265 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21266 
21267 /*
21268  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21269  * return B_TRUE if it should immediately be used to recreate the IRE.
21270  */
21271 static boolean_t
21272 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21273 {
21274 	ipaddr_t addr;
21275 
21276 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21277 
21278 	switch (bireinfop->bi_type) {
21279 	case BCAST_NET:
21280 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21281 		if (addr != bireinfop->bi_addr)
21282 			return (B_FALSE);
21283 		break;
21284 	case BCAST_SUBNET:
21285 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21286 			return (B_FALSE);
21287 		break;
21288 	}
21289 
21290 	bireinfop->bi_needrep = 1;
21291 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21292 		if (bireinfop->bi_backup == NULL)
21293 			bireinfop->bi_backup = ipif;
21294 		return (B_FALSE);
21295 	}
21296 	return (B_TRUE);
21297 }
21298 
21299 /*
21300  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21301  * them ala ire_check_and_create_bcast().
21302  */
21303 static ire_t **
21304 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21305 {
21306 	ipaddr_t mask, addr;
21307 
21308 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21309 
21310 	addr = bireinfop->bi_addr;
21311 	irep = ire_create_bcast(ipif, addr, irep);
21312 
21313 	switch (bireinfop->bi_type) {
21314 	case BCAST_NET:
21315 		mask = ip_net_mask(ipif->ipif_subnet);
21316 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21317 		break;
21318 	case BCAST_SUBNET:
21319 		mask = ipif->ipif_net_mask;
21320 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21321 		break;
21322 	}
21323 
21324 	bireinfop->bi_haverep = 1;
21325 	return (irep);
21326 }
21327 
21328 /*
21329  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21330  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21331  * that are going away are still needed.  If so, have ipif_create_bcast()
21332  * recreate them (except for the deprecated case, as explained below).
21333  */
21334 static ire_t **
21335 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21336     ire_t **irep)
21337 {
21338 	int i;
21339 	ipif_t *ipif;
21340 
21341 	ASSERT(!ill->ill_isv6);
21342 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21343 		/*
21344 		 * Skip this ipif if it's (a) the one being taken down, (b)
21345 		 * not in the same zone, or (c) has no valid local address.
21346 		 */
21347 		if (ipif == test_ipif ||
21348 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21349 		    ipif->ipif_subnet == 0 ||
21350 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21351 		    (IPIF_UP|IPIF_BROADCAST))
21352 			continue;
21353 
21354 		/*
21355 		 * For each dying IRE that hasn't yet been replaced, see if
21356 		 * `ipif' needs it and whether the IRE should be recreated on
21357 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21358 		 * will return B_FALSE even if `ipif' needs the IRE on the
21359 		 * hopes that we'll later find a needy non-deprecated ipif.
21360 		 * However, the ipif is recorded in bi_backup for possible
21361 		 * subsequent use by ipif_check_bcast_ires().
21362 		 */
21363 		for (i = 0; i < BCAST_COUNT; i++) {
21364 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21365 				continue;
21366 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21367 				continue;
21368 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21369 		}
21370 
21371 		/*
21372 		 * If we've replaced all of the broadcast IREs that are going
21373 		 * to be taken down, we know we're done.
21374 		 */
21375 		for (i = 0; i < BCAST_COUNT; i++) {
21376 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21377 				break;
21378 		}
21379 		if (i == BCAST_COUNT)
21380 			break;
21381 	}
21382 	return (irep);
21383 }
21384 
21385 /*
21386  * Check if `test_ipif' (which is going away) is associated with any existing
21387  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21388  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21389  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21390  *
21391  * This is necessary because broadcast IREs are shared.  In particular, a
21392  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21393  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21394  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21395  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21396  * same zone, they will share the same set of broadcast IREs.
21397  *
21398  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21399  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21400  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21401  */
21402 static void
21403 ipif_check_bcast_ires(ipif_t *test_ipif)
21404 {
21405 	ill_t		*ill = test_ipif->ipif_ill;
21406 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21407 	ire_t		**irep1, **irep = &ire_array[0];
21408 	uint_t 		i, willdie;
21409 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21410 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21411 
21412 	ASSERT(!test_ipif->ipif_isv6);
21413 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21414 
21415 	/*
21416 	 * No broadcast IREs for the LOOPBACK interface
21417 	 * or others such as point to point and IPIF_NOXMIT.
21418 	 */
21419 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21420 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21421 		return;
21422 
21423 	bzero(bireinfo, sizeof (bireinfo));
21424 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21425 	bireinfo[0].bi_addr = 0;
21426 
21427 	bireinfo[1].bi_type = BCAST_ALLONES;
21428 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21429 
21430 	bireinfo[2].bi_type = BCAST_NET;
21431 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21432 
21433 	if (test_ipif->ipif_net_mask != 0)
21434 		mask = test_ipif->ipif_net_mask;
21435 	bireinfo[3].bi_type = BCAST_SUBNET;
21436 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21437 
21438 	/*
21439 	 * Figure out what (if any) broadcast IREs will die as a result of
21440 	 * `test_ipif' going away.  If none will die, we're done.
21441 	 */
21442 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21443 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21444 		    test_ipif, ALL_ZONES, NULL,
21445 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21446 		if (ire != NULL) {
21447 			willdie++;
21448 			bireinfo[i].bi_willdie = 1;
21449 			ire_refrele(ire);
21450 		}
21451 	}
21452 
21453 	if (willdie == 0)
21454 		return;
21455 
21456 	/*
21457 	 * Walk through all the ipifs that will be affected by the dying IREs,
21458 	 * and recreate the IREs as necessary.
21459 	 */
21460 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21461 
21462 	/*
21463 	 * Scan through the set of broadcast IREs and see if there are any
21464 	 * that we need to replace that have not yet been replaced.  If so,
21465 	 * replace them using the appropriate backup ipif.
21466 	 */
21467 	for (i = 0; i < BCAST_COUNT; i++) {
21468 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21469 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21470 			    &bireinfo[i], irep);
21471 	}
21472 
21473 	/*
21474 	 * If we can't create all of them, don't add any of them.  (Code in
21475 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21476 	 * non-loopback copy and loopback copy for a given address.)
21477 	 */
21478 	for (irep1 = irep; irep1 > ire_array; ) {
21479 		irep1--;
21480 		if (*irep1 == NULL) {
21481 			ip0dbg(("ipif_check_bcast_ires: can't create "
21482 			    "IRE_BROADCAST, memory allocation failure\n"));
21483 			while (irep > ire_array) {
21484 				irep--;
21485 				if (*irep != NULL)
21486 					ire_delete(*irep);
21487 			}
21488 			return;
21489 		}
21490 	}
21491 
21492 	for (irep1 = irep; irep1 > ire_array; ) {
21493 		irep1--;
21494 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21495 			ire_refrele(*irep1);		/* Held in ire_add */
21496 	}
21497 }
21498 
21499 /*
21500  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21501  * from lifr_flags and the name from lifr_name.
21502  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21503  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21504  * Returns EINPROGRESS when mp has been consumed by queueing it on
21505  * ill_pending_mp and the ioctl will complete in ip_rput.
21506  *
21507  * Can operate on either a module or a driver queue.
21508  * Returns an error if not a module queue.
21509  */
21510 /* ARGSUSED */
21511 int
21512 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21513     ip_ioctl_cmd_t *ipip, void *if_req)
21514 {
21515 	int	err;
21516 	ill_t	*ill;
21517 	struct lifreq *lifr = (struct lifreq *)if_req;
21518 
21519 	ASSERT(ipif != NULL);
21520 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21521 
21522 	if (q->q_next == NULL) {
21523 		ip1dbg((
21524 		    "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21525 		return (EINVAL);
21526 	}
21527 
21528 	ill = (ill_t *)q->q_ptr;
21529 	/*
21530 	 * If we are not writer on 'q' then this interface exists already
21531 	 * and previous lookups (ipif_extract_lifreq()) found this ipif.
21532 	 * So return EALREADY
21533 	 */
21534 	if (ill != ipif->ipif_ill)
21535 		return (EALREADY);
21536 
21537 	if (ill->ill_name[0] != '\0')
21538 		return (EALREADY);
21539 
21540 	/*
21541 	 * Set all the flags. Allows all kinds of override. Provide some
21542 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21543 	 * unless there is either multicast/broadcast support in the driver
21544 	 * or it is a pt-pt link.
21545 	 */
21546 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21547 		/* Meaningless to IP thus don't allow them to be set. */
21548 		ip1dbg(("ip_setname: EINVAL 1\n"));
21549 		return (EINVAL);
21550 	}
21551 	/*
21552 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21553 	 * ill_bcast_addr_length info.
21554 	 */
21555 	if (!ill->ill_needs_attach &&
21556 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21557 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21558 	    ill->ill_bcast_addr_length == 0)) {
21559 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21560 		ip1dbg(("ip_setname: EINVAL 2\n"));
21561 		return (EINVAL);
21562 	}
21563 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21564 	    ((lifr->lifr_flags & IFF_IPV6) ||
21565 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21566 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21567 		ip1dbg(("ip_setname: EINVAL 3\n"));
21568 		return (EINVAL);
21569 	}
21570 	if (lifr->lifr_flags & IFF_UP) {
21571 		/* Can only be set with SIOCSLIFFLAGS */
21572 		ip1dbg(("ip_setname: EINVAL 4\n"));
21573 		return (EINVAL);
21574 	}
21575 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21576 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21577 		ip1dbg(("ip_setname: EINVAL 5\n"));
21578 		return (EINVAL);
21579 	}
21580 	/*
21581 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21582 	 */
21583 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21584 	    !(lifr->lifr_flags & IFF_IPV6) &&
21585 	    !(ipif->ipif_isv6)) {
21586 		ip1dbg(("ip_setname: EINVAL 6\n"));
21587 		return (EINVAL);
21588 	}
21589 
21590 	/*
21591 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21592 	 * we have all the flags here. So, we assign rather than we OR.
21593 	 * We can't OR the flags here because we don't want to set
21594 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21595 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21596 	 * on lifr_flags value here.
21597 	 */
21598 	/*
21599 	 * This ill has not been inserted into the global list.
21600 	 * So we are still single threaded and don't need any lock
21601 	 */
21602 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS &
21603 	    ~IFF_DUPLICATE;
21604 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21605 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21606 
21607 	/* We started off as V4. */
21608 	if (ill->ill_flags & ILLF_IPV6) {
21609 		ill->ill_phyint->phyint_illv6 = ill;
21610 		ill->ill_phyint->phyint_illv4 = NULL;
21611 	}
21612 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
21613 	return (err);
21614 }
21615 
21616 /* ARGSUSED */
21617 int
21618 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21619     ip_ioctl_cmd_t *ipip, void *if_req)
21620 {
21621 	/*
21622 	 * ill_phyint_reinit merged the v4 and v6 into a single
21623 	 * ipsq. Could also have become part of a ipmp group in the
21624 	 * process, and we might not have been able to complete the
21625 	 * slifname in ipif_set_values, if we could not become
21626 	 * exclusive.  If so restart it here
21627 	 */
21628 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21629 }
21630 
21631 /*
21632  * Return a pointer to the ipif which matches the index, IP version type and
21633  * zoneid.
21634  */
21635 ipif_t *
21636 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21637     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21638 {
21639 	ill_t	*ill;
21640 	ipsq_t  *ipsq;
21641 	phyint_t *phyi;
21642 	ipif_t	*ipif;
21643 
21644 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21645 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21646 
21647 	if (err != NULL)
21648 		*err = 0;
21649 
21650 	/*
21651 	 * Indexes are stored in the phyint - a common structure
21652 	 * to both IPv4 and IPv6.
21653 	 */
21654 
21655 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21656 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
21657 	    (void *) &index, NULL);
21658 	if (phyi != NULL) {
21659 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
21660 		if (ill == NULL) {
21661 			rw_exit(&ipst->ips_ill_g_lock);
21662 			if (err != NULL)
21663 				*err = ENXIO;
21664 			return (NULL);
21665 		}
21666 		GRAB_CONN_LOCK(q);
21667 		mutex_enter(&ill->ill_lock);
21668 		if (ILL_CAN_LOOKUP(ill)) {
21669 			for (ipif = ill->ill_ipif; ipif != NULL;
21670 			    ipif = ipif->ipif_next) {
21671 				if (IPIF_CAN_LOOKUP(ipif) &&
21672 				    (zoneid == ALL_ZONES ||
21673 				    zoneid == ipif->ipif_zoneid ||
21674 				    ipif->ipif_zoneid == ALL_ZONES)) {
21675 					ipif_refhold_locked(ipif);
21676 					mutex_exit(&ill->ill_lock);
21677 					RELEASE_CONN_LOCK(q);
21678 					rw_exit(&ipst->ips_ill_g_lock);
21679 					return (ipif);
21680 				}
21681 			}
21682 		} else if (ILL_CAN_WAIT(ill, q)) {
21683 			ipsq = ill->ill_phyint->phyint_ipsq;
21684 			mutex_enter(&ipsq->ipsq_lock);
21685 			rw_exit(&ipst->ips_ill_g_lock);
21686 			mutex_exit(&ill->ill_lock);
21687 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
21688 			mutex_exit(&ipsq->ipsq_lock);
21689 			RELEASE_CONN_LOCK(q);
21690 			*err = EINPROGRESS;
21691 			return (NULL);
21692 		}
21693 		mutex_exit(&ill->ill_lock);
21694 		RELEASE_CONN_LOCK(q);
21695 	}
21696 	rw_exit(&ipst->ips_ill_g_lock);
21697 	if (err != NULL)
21698 		*err = ENXIO;
21699 	return (NULL);
21700 }
21701 
21702 typedef struct conn_change_s {
21703 	uint_t cc_old_ifindex;
21704 	uint_t cc_new_ifindex;
21705 } conn_change_t;
21706 
21707 /*
21708  * ipcl_walk function for changing interface index.
21709  */
21710 static void
21711 conn_change_ifindex(conn_t *connp, caddr_t arg)
21712 {
21713 	conn_change_t *connc;
21714 	uint_t old_ifindex;
21715 	uint_t new_ifindex;
21716 	int i;
21717 	ilg_t *ilg;
21718 
21719 	connc = (conn_change_t *)arg;
21720 	old_ifindex = connc->cc_old_ifindex;
21721 	new_ifindex = connc->cc_new_ifindex;
21722 
21723 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21724 		connp->conn_orig_bound_ifindex = new_ifindex;
21725 
21726 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21727 		connp->conn_orig_multicast_ifindex = new_ifindex;
21728 
21729 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
21730 		connp->conn_orig_xmit_ifindex = new_ifindex;
21731 
21732 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21733 		ilg = &connp->conn_ilg[i];
21734 		if (ilg->ilg_orig_ifindex == old_ifindex)
21735 			ilg->ilg_orig_ifindex = new_ifindex;
21736 	}
21737 }
21738 
21739 /*
21740  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21741  * to new_index if it matches the old_index.
21742  *
21743  * Failovers typically happen within a group of ills. But somebody
21744  * can remove an ill from the group after a failover happened. If
21745  * we are setting the ifindex after this, we potentially need to
21746  * look at all the ills rather than just the ones in the group.
21747  * We cut down the work by looking at matching ill_net_types
21748  * and ill_types as we could not possibly grouped them together.
21749  */
21750 static void
21751 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21752 {
21753 	ill_t *ill;
21754 	ipif_t *ipif;
21755 	uint_t old_ifindex;
21756 	uint_t new_ifindex;
21757 	ilm_t *ilm;
21758 	ill_walk_context_t ctx;
21759 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21760 
21761 	old_ifindex = connc->cc_old_ifindex;
21762 	new_ifindex = connc->cc_new_ifindex;
21763 
21764 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21765 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21766 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21767 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21768 		    (ill_orig->ill_type != ill->ill_type)) {
21769 			continue;
21770 		}
21771 		for (ipif = ill->ill_ipif; ipif != NULL;
21772 		    ipif = ipif->ipif_next) {
21773 			if (ipif->ipif_orig_ifindex == old_ifindex)
21774 				ipif->ipif_orig_ifindex = new_ifindex;
21775 		}
21776 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21777 			if (ilm->ilm_orig_ifindex == old_ifindex)
21778 				ilm->ilm_orig_ifindex = new_ifindex;
21779 		}
21780 	}
21781 	rw_exit(&ipst->ips_ill_g_lock);
21782 }
21783 
21784 /*
21785  * We first need to ensure that the new index is unique, and
21786  * then carry the change across both v4 and v6 ill representation
21787  * of the physical interface.
21788  */
21789 /* ARGSUSED */
21790 int
21791 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21792     ip_ioctl_cmd_t *ipip, void *ifreq)
21793 {
21794 	ill_t		*ill;
21795 	ill_t		*ill_other;
21796 	phyint_t	*phyi;
21797 	int		old_index;
21798 	conn_change_t	connc;
21799 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21800 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21801 	uint_t	index;
21802 	ill_t	*ill_v4;
21803 	ill_t	*ill_v6;
21804 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21805 
21806 	if (ipip->ipi_cmd_type == IF_CMD)
21807 		index = ifr->ifr_index;
21808 	else
21809 		index = lifr->lifr_index;
21810 
21811 	/*
21812 	 * Only allow on physical interface. Also, index zero is illegal.
21813 	 *
21814 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21815 	 *
21816 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21817 	 *    implies a possible failback might have to happen. As failback
21818 	 *    depends on the old index, we should fail setting the index.
21819 	 *
21820 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21821 	 *    any addresses or multicast memberships are failed over to
21822 	 *    a non-STANDBY interface. As failback depends on the old
21823 	 *    index, we should fail setting the index for this case also.
21824 	 *
21825 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21826 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21827 	 */
21828 	ill = ipif->ipif_ill;
21829 	phyi = ill->ill_phyint;
21830 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21831 	    ipif->ipif_id != 0 || index == 0) {
21832 		return (EINVAL);
21833 	}
21834 	old_index = phyi->phyint_ifindex;
21835 
21836 	/* If the index is not changing, no work to do */
21837 	if (old_index == index)
21838 		return (0);
21839 
21840 	/*
21841 	 * Use ill_lookup_on_ifindex to determine if the
21842 	 * new index is unused and if so allow the change.
21843 	 */
21844 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21845 	    ipst);
21846 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21847 	    ipst);
21848 	if (ill_v6 != NULL || ill_v4 != NULL) {
21849 		if (ill_v4 != NULL)
21850 			ill_refrele(ill_v4);
21851 		if (ill_v6 != NULL)
21852 			ill_refrele(ill_v6);
21853 		return (EBUSY);
21854 	}
21855 
21856 	/*
21857 	 * The new index is unused. Set it in the phyint.
21858 	 * Locate the other ill so that we can send a routing
21859 	 * sockets message.
21860 	 */
21861 	if (ill->ill_isv6) {
21862 		ill_other = phyi->phyint_illv4;
21863 	} else {
21864 		ill_other = phyi->phyint_illv6;
21865 	}
21866 
21867 	phyi->phyint_ifindex = index;
21868 
21869 	/* Update SCTP's ILL list */
21870 	sctp_ill_reindex(ill, old_index);
21871 
21872 	connc.cc_old_ifindex = old_index;
21873 	connc.cc_new_ifindex = index;
21874 	ip_change_ifindex(ill, &connc);
21875 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21876 
21877 	/* Send the routing sockets message */
21878 	ip_rts_ifmsg(ipif);
21879 	if (ill_other != NULL)
21880 		ip_rts_ifmsg(ill_other->ill_ipif);
21881 
21882 	return (0);
21883 }
21884 
21885 /* ARGSUSED */
21886 int
21887 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21888     ip_ioctl_cmd_t *ipip, void *ifreq)
21889 {
21890 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21891 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21892 
21893 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21894 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21895 	/* Get the interface index */
21896 	if (ipip->ipi_cmd_type == IF_CMD) {
21897 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21898 	} else {
21899 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21900 	}
21901 	return (0);
21902 }
21903 
21904 /* ARGSUSED */
21905 int
21906 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21907     ip_ioctl_cmd_t *ipip, void *ifreq)
21908 {
21909 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21910 
21911 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21912 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21913 	/* Get the interface zone */
21914 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21915 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21916 	return (0);
21917 }
21918 
21919 /*
21920  * Set the zoneid of an interface.
21921  */
21922 /* ARGSUSED */
21923 int
21924 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21925     ip_ioctl_cmd_t *ipip, void *ifreq)
21926 {
21927 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21928 	int err = 0;
21929 	boolean_t need_up = B_FALSE;
21930 	zone_t *zptr;
21931 	zone_status_t status;
21932 	zoneid_t zoneid;
21933 
21934 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21935 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21936 		if (!is_system_labeled())
21937 			return (ENOTSUP);
21938 		zoneid = GLOBAL_ZONEID;
21939 	}
21940 
21941 	/* cannot assign instance zero to a non-global zone */
21942 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21943 		return (ENOTSUP);
21944 
21945 	/*
21946 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21947 	 * the event of a race with the zone shutdown processing, since IP
21948 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
21949 	 * interface will be cleaned up even if the zone is shut down
21950 	 * immediately after the status check. If the interface can't be brought
21951 	 * down right away, and the zone is shut down before the restart
21952 	 * function is called, we resolve the possible races by rechecking the
21953 	 * zone status in the restart function.
21954 	 */
21955 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21956 		return (EINVAL);
21957 	status = zone_status_get(zptr);
21958 	zone_rele(zptr);
21959 
21960 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21961 		return (EINVAL);
21962 
21963 	if (ipif->ipif_flags & IPIF_UP) {
21964 		/*
21965 		 * If the interface is already marked up,
21966 		 * we call ipif_down which will take care
21967 		 * of ditching any IREs that have been set
21968 		 * up based on the old interface address.
21969 		 */
21970 		err = ipif_logical_down(ipif, q, mp);
21971 		if (err == EINPROGRESS)
21972 			return (err);
21973 		ipif_down_tail(ipif);
21974 		need_up = B_TRUE;
21975 	}
21976 
21977 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21978 	return (err);
21979 }
21980 
21981 static int
21982 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21983     queue_t *q, mblk_t *mp, boolean_t need_up)
21984 {
21985 	int	err = 0;
21986 	ip_stack_t	*ipst;
21987 
21988 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21989 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21990 
21991 	if (CONN_Q(q))
21992 		ipst = CONNQ_TO_IPST(q);
21993 	else
21994 		ipst = ILLQ_TO_IPST(q);
21995 
21996 	/*
21997 	 * For exclusive stacks we don't allow a different zoneid than
21998 	 * global.
21999 	 */
22000 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22001 	    zoneid != GLOBAL_ZONEID)
22002 		return (EINVAL);
22003 
22004 	/* Set the new zone id. */
22005 	ipif->ipif_zoneid = zoneid;
22006 
22007 	/* Update sctp list */
22008 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22009 
22010 	if (need_up) {
22011 		/*
22012 		 * Now bring the interface back up.  If this
22013 		 * is the only IPIF for the ILL, ipif_up
22014 		 * will have to re-bind to the device, so
22015 		 * we may get back EINPROGRESS, in which
22016 		 * case, this IOCTL will get completed in
22017 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22018 		 */
22019 		err = ipif_up(ipif, q, mp);
22020 	}
22021 	return (err);
22022 }
22023 
22024 /* ARGSUSED */
22025 int
22026 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22027     ip_ioctl_cmd_t *ipip, void *if_req)
22028 {
22029 	struct lifreq *lifr = (struct lifreq *)if_req;
22030 	zoneid_t zoneid;
22031 	zone_t *zptr;
22032 	zone_status_t status;
22033 
22034 	ASSERT(ipif->ipif_id != 0);
22035 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22036 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22037 		zoneid = GLOBAL_ZONEID;
22038 
22039 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22040 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22041 
22042 	/*
22043 	 * We recheck the zone status to resolve the following race condition:
22044 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22045 	 * 2) hme0:1 is up and can't be brought down right away;
22046 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22047 	 * 3) zone "myzone" is halted; the zone status switches to
22048 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22049 	 * the interfaces to remove - hme0:1 is not returned because it's not
22050 	 * yet in "myzone", so it won't be removed;
22051 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22052 	 * status check here, we would have hme0:1 in "myzone" after it's been
22053 	 * destroyed.
22054 	 * Note that if the status check fails, we need to bring the interface
22055 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22056 	 * ipif_up_done[_v6]().
22057 	 */
22058 	status = ZONE_IS_UNINITIALIZED;
22059 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22060 		status = zone_status_get(zptr);
22061 		zone_rele(zptr);
22062 	}
22063 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22064 		if (ipif->ipif_isv6) {
22065 			(void) ipif_up_done_v6(ipif);
22066 		} else {
22067 			(void) ipif_up_done(ipif);
22068 		}
22069 		return (EINVAL);
22070 	}
22071 
22072 	ipif_down_tail(ipif);
22073 
22074 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22075 	    B_TRUE));
22076 }
22077 
22078 /* ARGSUSED */
22079 int
22080 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22081 	ip_ioctl_cmd_t *ipip, void *ifreq)
22082 {
22083 	struct lifreq	*lifr = ifreq;
22084 
22085 	ASSERT(q->q_next == NULL);
22086 	ASSERT(CONN_Q(q));
22087 
22088 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22089 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22090 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22091 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22092 
22093 	return (0);
22094 }
22095 
22096 
22097 /* Find the previous ILL in this usesrc group */
22098 static ill_t *
22099 ill_prev_usesrc(ill_t *uill)
22100 {
22101 	ill_t *ill;
22102 
22103 	for (ill = uill->ill_usesrc_grp_next;
22104 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22105 	    ill = ill->ill_usesrc_grp_next)
22106 		/* do nothing */;
22107 	return (ill);
22108 }
22109 
22110 /*
22111  * Release all members of the usesrc group. This routine is called
22112  * from ill_delete when the interface being unplumbed is the
22113  * group head.
22114  */
22115 static void
22116 ill_disband_usesrc_group(ill_t *uill)
22117 {
22118 	ill_t *next_ill, *tmp_ill;
22119 	ip_stack_t	*ipst = uill->ill_ipst;
22120 
22121 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22122 	next_ill = uill->ill_usesrc_grp_next;
22123 
22124 	do {
22125 		ASSERT(next_ill != NULL);
22126 		tmp_ill = next_ill->ill_usesrc_grp_next;
22127 		ASSERT(tmp_ill != NULL);
22128 		next_ill->ill_usesrc_grp_next = NULL;
22129 		next_ill->ill_usesrc_ifindex = 0;
22130 		next_ill = tmp_ill;
22131 	} while (next_ill->ill_usesrc_ifindex != 0);
22132 	uill->ill_usesrc_grp_next = NULL;
22133 }
22134 
22135 /*
22136  * Remove the client usesrc ILL from the list and relink to a new list
22137  */
22138 int
22139 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22140 {
22141 	ill_t *ill, *tmp_ill;
22142 	ip_stack_t	*ipst = ucill->ill_ipst;
22143 
22144 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22145 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22146 
22147 	/*
22148 	 * Check if the usesrc client ILL passed in is not already
22149 	 * in use as a usesrc ILL i.e one whose source address is
22150 	 * in use OR a usesrc ILL is not already in use as a usesrc
22151 	 * client ILL
22152 	 */
22153 	if ((ucill->ill_usesrc_ifindex == 0) ||
22154 	    (uill->ill_usesrc_ifindex != 0)) {
22155 		return (-1);
22156 	}
22157 
22158 	ill = ill_prev_usesrc(ucill);
22159 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22160 
22161 	/* Remove from the current list */
22162 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22163 		/* Only two elements in the list */
22164 		ASSERT(ill->ill_usesrc_ifindex == 0);
22165 		ill->ill_usesrc_grp_next = NULL;
22166 	} else {
22167 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22168 	}
22169 
22170 	if (ifindex == 0) {
22171 		ucill->ill_usesrc_ifindex = 0;
22172 		ucill->ill_usesrc_grp_next = NULL;
22173 		return (0);
22174 	}
22175 
22176 	ucill->ill_usesrc_ifindex = ifindex;
22177 	tmp_ill = uill->ill_usesrc_grp_next;
22178 	uill->ill_usesrc_grp_next = ucill;
22179 	ucill->ill_usesrc_grp_next =
22180 	    (tmp_ill != NULL) ? tmp_ill : uill;
22181 	return (0);
22182 }
22183 
22184 /*
22185  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22186  * ip.c for locking details.
22187  */
22188 /* ARGSUSED */
22189 int
22190 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22191     ip_ioctl_cmd_t *ipip, void *ifreq)
22192 {
22193 	struct lifreq *lifr = (struct lifreq *)ifreq;
22194 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22195 	    ill_flag_changed = B_FALSE;
22196 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22197 	int err = 0, ret;
22198 	uint_t ifindex;
22199 	phyint_t *us_phyint, *us_cli_phyint;
22200 	ipsq_t *ipsq = NULL;
22201 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22202 
22203 	ASSERT(IAM_WRITER_IPIF(ipif));
22204 	ASSERT(q->q_next == NULL);
22205 	ASSERT(CONN_Q(q));
22206 
22207 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22208 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22209 
22210 	ASSERT(us_cli_phyint != NULL);
22211 
22212 	/*
22213 	 * If the client ILL is being used for IPMP, abort.
22214 	 * Note, this can be done before ipsq_try_enter since we are already
22215 	 * exclusive on this ILL
22216 	 */
22217 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22218 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22219 		return (EINVAL);
22220 	}
22221 
22222 	ifindex = lifr->lifr_index;
22223 	if (ifindex == 0) {
22224 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22225 			/* non usesrc group interface, nothing to reset */
22226 			return (0);
22227 		}
22228 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22229 		/* valid reset request */
22230 		reset_flg = B_TRUE;
22231 	}
22232 
22233 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22234 	    ip_process_ioctl, &err, ipst);
22235 
22236 	if (usesrc_ill == NULL) {
22237 		return (err);
22238 	}
22239 
22240 	/*
22241 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22242 	 * group nor can either of the interfaces be used for standy. So
22243 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22244 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22245 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22246 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22247 	 * the usesrc_cli_ill
22248 	 */
22249 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22250 	    NEW_OP, B_TRUE);
22251 	if (ipsq == NULL) {
22252 		err = EINPROGRESS;
22253 		/* Operation enqueued on the ipsq of the usesrc ILL */
22254 		goto done;
22255 	}
22256 
22257 	/* Check if the usesrc_ill is used for IPMP */
22258 	us_phyint = usesrc_ill->ill_phyint;
22259 	if ((us_phyint->phyint_groupname != NULL) ||
22260 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22261 		err = EINVAL;
22262 		goto done;
22263 	}
22264 
22265 	/*
22266 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22267 	 * already a client then return EINVAL
22268 	 */
22269 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22270 		err = EINVAL;
22271 		goto done;
22272 	}
22273 
22274 	/*
22275 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22276 	 * be then this is a duplicate operation.
22277 	 */
22278 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22279 		err = 0;
22280 		goto done;
22281 	}
22282 
22283 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22284 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22285 	    usesrc_ill->ill_isv6));
22286 
22287 	/*
22288 	 * The next step ensures that no new ires will be created referencing
22289 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22290 	 * we go through an ire walk deleting all ire caches that reference
22291 	 * the client ill. New ires referencing the client ill that are added
22292 	 * to the ire table before the ILL_CHANGING flag is set, will be
22293 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22294 	 * the client ill while the ILL_CHANGING flag is set will be failed
22295 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22296 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22297 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22298 	 * belong to the same usesrc group.
22299 	 */
22300 	mutex_enter(&usesrc_cli_ill->ill_lock);
22301 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22302 	mutex_exit(&usesrc_cli_ill->ill_lock);
22303 	ill_flag_changed = B_TRUE;
22304 
22305 	if (ipif->ipif_isv6)
22306 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22307 		    ALL_ZONES, ipst);
22308 	else
22309 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22310 		    ALL_ZONES, ipst);
22311 
22312 	/*
22313 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22314 	 * and the ill_usesrc_ifindex fields
22315 	 */
22316 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22317 
22318 	if (reset_flg) {
22319 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22320 		if (ret != 0) {
22321 			err = EINVAL;
22322 		}
22323 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22324 		goto done;
22325 	}
22326 
22327 	/*
22328 	 * Four possibilities to consider:
22329 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22330 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22331 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22332 	 * 4. Both are part of their respective usesrc groups
22333 	 */
22334 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22335 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22336 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22337 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22338 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22339 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22340 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22341 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22342 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22343 		/* Insert at head of list */
22344 		usesrc_cli_ill->ill_usesrc_grp_next =
22345 		    usesrc_ill->ill_usesrc_grp_next;
22346 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22347 	} else {
22348 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22349 		    ifindex);
22350 		if (ret != 0)
22351 			err = EINVAL;
22352 	}
22353 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22354 
22355 done:
22356 	if (ill_flag_changed) {
22357 		mutex_enter(&usesrc_cli_ill->ill_lock);
22358 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22359 		mutex_exit(&usesrc_cli_ill->ill_lock);
22360 	}
22361 	if (ipsq != NULL)
22362 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
22363 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22364 	ill_refrele(usesrc_ill);
22365 	return (err);
22366 }
22367 
22368 /*
22369  * comparison function used by avl.
22370  */
22371 static int
22372 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22373 {
22374 
22375 	uint_t index;
22376 
22377 	ASSERT(phyip != NULL && index_ptr != NULL);
22378 
22379 	index = *((uint_t *)index_ptr);
22380 	/*
22381 	 * let the phyint with the lowest index be on top.
22382 	 */
22383 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22384 		return (1);
22385 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22386 		return (-1);
22387 	return (0);
22388 }
22389 
22390 /*
22391  * comparison function used by avl.
22392  */
22393 static int
22394 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22395 {
22396 	ill_t *ill;
22397 	int res = 0;
22398 
22399 	ASSERT(phyip != NULL && name_ptr != NULL);
22400 
22401 	if (((phyint_t *)phyip)->phyint_illv4)
22402 		ill = ((phyint_t *)phyip)->phyint_illv4;
22403 	else
22404 		ill = ((phyint_t *)phyip)->phyint_illv6;
22405 	ASSERT(ill != NULL);
22406 
22407 	res = strcmp(ill->ill_name, (char *)name_ptr);
22408 	if (res > 0)
22409 		return (1);
22410 	else if (res < 0)
22411 		return (-1);
22412 	return (0);
22413 }
22414 /*
22415  * This function is called from ill_delete when the ill is being
22416  * unplumbed. We remove the reference from the phyint and we also
22417  * free the phyint when there are no more references to it.
22418  */
22419 static void
22420 ill_phyint_free(ill_t *ill)
22421 {
22422 	phyint_t *phyi;
22423 	phyint_t *next_phyint;
22424 	ipsq_t *cur_ipsq;
22425 	ip_stack_t	*ipst = ill->ill_ipst;
22426 
22427 	ASSERT(ill->ill_phyint != NULL);
22428 
22429 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22430 	phyi = ill->ill_phyint;
22431 	ill->ill_phyint = NULL;
22432 	/*
22433 	 * ill_init allocates a phyint always to store the copy
22434 	 * of flags relevant to phyint. At that point in time, we could
22435 	 * not assign the name and hence phyint_illv4/v6 could not be
22436 	 * initialized. Later in ipif_set_values, we assign the name to
22437 	 * the ill, at which point in time we assign phyint_illv4/v6.
22438 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22439 	 */
22440 	if (ill->ill_flags & ILLF_IPV6) {
22441 		phyi->phyint_illv6 = NULL;
22442 	} else {
22443 		phyi->phyint_illv4 = NULL;
22444 	}
22445 	/*
22446 	 * ipif_down removes it from the group when the last ipif goes
22447 	 * down.
22448 	 */
22449 	ASSERT(ill->ill_group == NULL);
22450 
22451 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22452 		return;
22453 
22454 	/*
22455 	 * Make sure this phyint was put in the list.
22456 	 */
22457 	if (phyi->phyint_ifindex > 0) {
22458 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22459 		    phyi);
22460 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22461 		    phyi);
22462 	}
22463 	/*
22464 	 * remove phyint from the ipsq list.
22465 	 */
22466 	cur_ipsq = phyi->phyint_ipsq;
22467 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22468 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22469 	} else {
22470 		next_phyint = cur_ipsq->ipsq_phyint_list;
22471 		while (next_phyint != NULL) {
22472 			if (next_phyint->phyint_ipsq_next == phyi) {
22473 				next_phyint->phyint_ipsq_next =
22474 				    phyi->phyint_ipsq_next;
22475 				break;
22476 			}
22477 			next_phyint = next_phyint->phyint_ipsq_next;
22478 		}
22479 		ASSERT(next_phyint != NULL);
22480 	}
22481 	IPSQ_DEC_REF(cur_ipsq, ipst);
22482 
22483 	if (phyi->phyint_groupname_len != 0) {
22484 		ASSERT(phyi->phyint_groupname != NULL);
22485 		mi_free(phyi->phyint_groupname);
22486 	}
22487 	mi_free(phyi);
22488 }
22489 
22490 /*
22491  * Attach the ill to the phyint structure which can be shared by both
22492  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22493  * function is called from ipif_set_values and ill_lookup_on_name (for
22494  * loopback) where we know the name of the ill. We lookup the ill and if
22495  * there is one present already with the name use that phyint. Otherwise
22496  * reuse the one allocated by ill_init.
22497  */
22498 static void
22499 ill_phyint_reinit(ill_t *ill)
22500 {
22501 	boolean_t isv6 = ill->ill_isv6;
22502 	phyint_t *phyi_old;
22503 	phyint_t *phyi;
22504 	avl_index_t where = 0;
22505 	ill_t	*ill_other = NULL;
22506 	ipsq_t	*ipsq;
22507 	ip_stack_t	*ipst = ill->ill_ipst;
22508 
22509 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22510 
22511 	phyi_old = ill->ill_phyint;
22512 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22513 	    phyi_old->phyint_illv6 == NULL));
22514 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22515 	    phyi_old->phyint_illv4 == NULL));
22516 	ASSERT(phyi_old->phyint_ifindex == 0);
22517 
22518 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22519 	    ill->ill_name, &where);
22520 
22521 	/*
22522 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22523 	 *    the global list of ills. So no other thread could have located
22524 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22525 	 * 2. Now locate the other protocol instance of this ill.
22526 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22527 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22528 	 *    of neither ill can change.
22529 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22530 	 *    other ill.
22531 	 * 5. Release all locks.
22532 	 */
22533 
22534 	/*
22535 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22536 	 * we are initializing IPv4.
22537 	 */
22538 	if (phyi != NULL) {
22539 		ill_other = (isv6) ? phyi->phyint_illv4 :
22540 		    phyi->phyint_illv6;
22541 		ASSERT(ill_other->ill_phyint != NULL);
22542 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22543 		    (!isv6 && ill_other->ill_isv6));
22544 		GRAB_ILL_LOCKS(ill, ill_other);
22545 		/*
22546 		 * We are potentially throwing away phyint_flags which
22547 		 * could be different from the one that we obtain from
22548 		 * ill_other->ill_phyint. But it is okay as we are assuming
22549 		 * that the state maintained within IP is correct.
22550 		 */
22551 		mutex_enter(&phyi->phyint_lock);
22552 		if (isv6) {
22553 			ASSERT(phyi->phyint_illv6 == NULL);
22554 			phyi->phyint_illv6 = ill;
22555 		} else {
22556 			ASSERT(phyi->phyint_illv4 == NULL);
22557 			phyi->phyint_illv4 = ill;
22558 		}
22559 		/*
22560 		 * This is a new ill, currently undergoing SLIFNAME
22561 		 * So we could not have joined an IPMP group until now.
22562 		 */
22563 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22564 		    phyi_old->phyint_groupname == NULL);
22565 
22566 		/*
22567 		 * This phyi_old is going away. Decref ipsq_refs and
22568 		 * assert it is zero. The ipsq itself will be freed in
22569 		 * ipsq_exit
22570 		 */
22571 		ipsq = phyi_old->phyint_ipsq;
22572 		IPSQ_DEC_REF(ipsq, ipst);
22573 		ASSERT(ipsq->ipsq_refs == 0);
22574 		/* Get the singleton phyint out of the ipsq list */
22575 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22576 		ipsq->ipsq_phyint_list = NULL;
22577 		phyi_old->phyint_illv4 = NULL;
22578 		phyi_old->phyint_illv6 = NULL;
22579 		mi_free(phyi_old);
22580 	} else {
22581 		mutex_enter(&ill->ill_lock);
22582 		/*
22583 		 * We don't need to acquire any lock, since
22584 		 * the ill is not yet visible globally  and we
22585 		 * have not yet released the ill_g_lock.
22586 		 */
22587 		phyi = phyi_old;
22588 		mutex_enter(&phyi->phyint_lock);
22589 		/* XXX We need a recovery strategy here. */
22590 		if (!phyint_assign_ifindex(phyi, ipst))
22591 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22592 
22593 		/* No IPMP group yet, thus the hook uses the ifindex */
22594 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22595 
22596 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22597 		    (void *)phyi, where);
22598 
22599 		(void) avl_find(&ipst->ips_phyint_g_list->
22600 		    phyint_list_avl_by_index,
22601 		    &phyi->phyint_ifindex, &where);
22602 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22603 		    (void *)phyi, where);
22604 	}
22605 
22606 	/*
22607 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22608 	 * pending mp is not affected because that is per ill basis.
22609 	 */
22610 	ill->ill_phyint = phyi;
22611 
22612 	/*
22613 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22614 	 * We do this here as when the first ipif was allocated,
22615 	 * ipif_allocate does not know the right interface index.
22616 	 */
22617 
22618 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22619 	/*
22620 	 * Now that the phyint's ifindex has been assigned, complete the
22621 	 * remaining
22622 	 */
22623 
22624 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22625 	if (ill->ill_isv6) {
22626 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22627 		    ill->ill_phyint->phyint_ifindex;
22628 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22629 	} else {
22630 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22631 	}
22632 
22633 	/*
22634 	 * Generate an event within the hooks framework to indicate that
22635 	 * a new interface has just been added to IP.  For this event to
22636 	 * be generated, the network interface must, at least, have an
22637 	 * ifindex assigned to it.
22638 	 *
22639 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22640 	 * that the ordering of delivered events to listeners matches the
22641 	 * order of them in the kernel.
22642 	 *
22643 	 * This function could be called from ill_lookup_on_name. In that case
22644 	 * the interface is loopback "lo", which will not generate a NIC event.
22645 	 */
22646 	if (ill->ill_name_length <= 2 ||
22647 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22648 		/*
22649 		 * Generate nic plumb event for ill_name even if
22650 		 * ipmp_hook_emulation is set. That avoids generating events
22651 		 * for the ill_names should ipmp_hook_emulation be turned on
22652 		 * later.
22653 		 */
22654 		ill_nic_info_plumb(ill, B_FALSE);
22655 	}
22656 	RELEASE_ILL_LOCKS(ill, ill_other);
22657 	mutex_exit(&phyi->phyint_lock);
22658 }
22659 
22660 /*
22661  * Allocate a NE_PLUMB nic info event and store in the ill.
22662  * If 'group' is set we do it for the group name, otherwise the ill name.
22663  * It will be sent when we leave the ipsq.
22664  */
22665 void
22666 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22667 {
22668 	phyint_t	*phyi = ill->ill_phyint;
22669 	ip_stack_t	*ipst = ill->ill_ipst;
22670 	hook_nic_event_t *info;
22671 	char		*name;
22672 	int		namelen;
22673 
22674 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22675 
22676 	if ((info = ill->ill_nic_event_info) != NULL) {
22677 		ip2dbg(("ill_nic_info_plumb: unexpected nic event %d "
22678 		    "attached for %s\n", info->hne_event,
22679 		    ill->ill_name));
22680 		if (info->hne_data != NULL)
22681 			kmem_free(info->hne_data, info->hne_datalen);
22682 		kmem_free(info, sizeof (hook_nic_event_t));
22683 		ill->ill_nic_event_info = NULL;
22684 	}
22685 
22686 	info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP);
22687 	if (info == NULL) {
22688 		ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic "
22689 		    "event information for %s (ENOMEM)\n",
22690 		    ill->ill_name));
22691 		return;
22692 	}
22693 
22694 	if (group) {
22695 		ASSERT(phyi->phyint_groupname_len != 0);
22696 		namelen = phyi->phyint_groupname_len;
22697 		name = phyi->phyint_groupname;
22698 	} else {
22699 		namelen = ill->ill_name_length;
22700 		name = ill->ill_name;
22701 	}
22702 
22703 	info->hne_nic = phyi->phyint_hook_ifindex;
22704 	info->hne_lif = 0;
22705 	info->hne_event = NE_PLUMB;
22706 	info->hne_family = ill->ill_isv6 ?
22707 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
22708 
22709 	info->hne_data = kmem_alloc(namelen, KM_NOSLEEP);
22710 	if (info->hne_data != NULL) {
22711 		info->hne_datalen = namelen;
22712 		bcopy(name, info->hne_data, info->hne_datalen);
22713 	} else {
22714 		ip2dbg(("ill_nic_info_plumb: could not attach "
22715 		    "name information for PLUMB nic event "
22716 		    "of %s (ENOMEM)\n", name));
22717 		kmem_free(info, sizeof (hook_nic_event_t));
22718 		info = NULL;
22719 	}
22720 	ill->ill_nic_event_info = info;
22721 }
22722 
22723 /*
22724  * Unhook the nic event message from the ill and enqueue it
22725  * into the nic event taskq.
22726  */
22727 void
22728 ill_nic_info_dispatch(ill_t *ill)
22729 {
22730 	hook_nic_event_t *info;
22731 
22732 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22733 
22734 	if ((info = ill->ill_nic_event_info) != NULL) {
22735 		if (ddi_taskq_dispatch(eventq_queue_nic,
22736 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22737 			ip2dbg(("ill_nic_info_dispatch: "
22738 			    "ddi_taskq_dispatch failed\n"));
22739 			if (info->hne_data != NULL)
22740 				kmem_free(info->hne_data, info->hne_datalen);
22741 			kmem_free(info, sizeof (hook_nic_event_t));
22742 		}
22743 		ill->ill_nic_event_info = NULL;
22744 	}
22745 }
22746 
22747 /*
22748  * Notify any downstream modules of the name of this interface.
22749  * An M_IOCTL is used even though we don't expect a successful reply.
22750  * Any reply message from the driver (presumably an M_IOCNAK) will
22751  * eventually get discarded somewhere upstream.  The message format is
22752  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22753  * to IP.
22754  */
22755 static void
22756 ip_ifname_notify(ill_t *ill, queue_t *q)
22757 {
22758 	mblk_t *mp1, *mp2;
22759 	struct iocblk *iocp;
22760 	struct lifreq *lifr;
22761 
22762 	mp1 = mkiocb(SIOCSLIFNAME);
22763 	if (mp1 == NULL)
22764 		return;
22765 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22766 	if (mp2 == NULL) {
22767 		freeb(mp1);
22768 		return;
22769 	}
22770 
22771 	mp1->b_cont = mp2;
22772 	iocp = (struct iocblk *)mp1->b_rptr;
22773 	iocp->ioc_count = sizeof (struct lifreq);
22774 
22775 	lifr = (struct lifreq *)mp2->b_rptr;
22776 	mp2->b_wptr += sizeof (struct lifreq);
22777 	bzero(lifr, sizeof (struct lifreq));
22778 
22779 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22780 	lifr->lifr_ppa = ill->ill_ppa;
22781 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22782 
22783 	putnext(q, mp1);
22784 }
22785 
22786 static int
22787 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22788 {
22789 	int err;
22790 	ip_stack_t	*ipst = ill->ill_ipst;
22791 
22792 	/* Set the obsolete NDD per-interface forwarding name. */
22793 	err = ill_set_ndd_name(ill);
22794 	if (err != 0) {
22795 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22796 		    err);
22797 	}
22798 
22799 	/* Tell downstream modules where they are. */
22800 	ip_ifname_notify(ill, q);
22801 
22802 	/*
22803 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22804 	 * Error cases are ENOMEM ...
22805 	 */
22806 	err = ill_dl_phys(ill, ipif, mp, q);
22807 
22808 	/*
22809 	 * If there is no IRE expiration timer running, get one started.
22810 	 * igmp and mld timers will be triggered by the first multicast
22811 	 */
22812 	if (ipst->ips_ip_ire_expire_id == 0) {
22813 		/*
22814 		 * acquire the lock and check again.
22815 		 */
22816 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22817 		if (ipst->ips_ip_ire_expire_id == 0) {
22818 			ipst->ips_ip_ire_expire_id = timeout(
22819 			    ip_trash_timer_expire, ipst,
22820 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22821 		}
22822 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22823 	}
22824 
22825 	if (ill->ill_isv6) {
22826 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22827 		if (ipst->ips_mld_slowtimeout_id == 0) {
22828 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22829 			    (void *)ipst,
22830 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22831 		}
22832 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22833 	} else {
22834 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22835 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22836 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22837 			    (void *)ipst,
22838 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22839 		}
22840 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22841 	}
22842 
22843 	return (err);
22844 }
22845 
22846 /*
22847  * Common routine for ppa and ifname setting. Should be called exclusive.
22848  *
22849  * Returns EINPROGRESS when mp has been consumed by queueing it on
22850  * ill_pending_mp and the ioctl will complete in ip_rput.
22851  *
22852  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22853  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22854  * For SLIFNAME, we pass these values back to the userland.
22855  */
22856 static int
22857 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22858 {
22859 	ill_t	*ill;
22860 	ipif_t	*ipif;
22861 	ipsq_t	*ipsq;
22862 	char	*ppa_ptr;
22863 	char	*old_ptr;
22864 	char	old_char;
22865 	int	error;
22866 	ip_stack_t	*ipst;
22867 
22868 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22869 	ASSERT(q->q_next != NULL);
22870 	ASSERT(interf_name != NULL);
22871 
22872 	ill = (ill_t *)q->q_ptr;
22873 	ipst = ill->ill_ipst;
22874 
22875 	ASSERT(ill->ill_ipst != NULL);
22876 	ASSERT(ill->ill_name[0] == '\0');
22877 	ASSERT(IAM_WRITER_ILL(ill));
22878 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22879 	ASSERT(ill->ill_ppa == UINT_MAX);
22880 
22881 	/* The ppa is sent down by ifconfig or is chosen */
22882 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22883 		return (EINVAL);
22884 	}
22885 
22886 	/*
22887 	 * make sure ppa passed in is same as ppa in the name.
22888 	 * This check is not made when ppa == UINT_MAX in that case ppa
22889 	 * in the name could be anything. System will choose a ppa and
22890 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22891 	 */
22892 	if (*new_ppa_ptr != UINT_MAX) {
22893 		/* stoi changes the pointer */
22894 		old_ptr = ppa_ptr;
22895 		/*
22896 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22897 		 * (they don't have an externally visible ppa).  We assign one
22898 		 * here so that we can manage the interface.  Note that in
22899 		 * the past this value was always 0 for DLPI 1 drivers.
22900 		 */
22901 		if (*new_ppa_ptr == 0)
22902 			*new_ppa_ptr = stoi(&old_ptr);
22903 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22904 			return (EINVAL);
22905 	}
22906 	/*
22907 	 * terminate string before ppa
22908 	 * save char at that location.
22909 	 */
22910 	old_char = ppa_ptr[0];
22911 	ppa_ptr[0] = '\0';
22912 
22913 	ill->ill_ppa = *new_ppa_ptr;
22914 	/*
22915 	 * Finish as much work now as possible before calling ill_glist_insert
22916 	 * which makes the ill globally visible and also merges it with the
22917 	 * other protocol instance of this phyint. The remaining work is
22918 	 * done after entering the ipsq which may happen sometime later.
22919 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22920 	 */
22921 	ipif = ill->ill_ipif;
22922 
22923 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22924 	ipif_assign_seqid(ipif);
22925 
22926 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22927 		ill->ill_flags |= ILLF_IPV4;
22928 
22929 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22930 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22931 
22932 	if (ill->ill_flags & ILLF_IPV6) {
22933 
22934 		ill->ill_isv6 = B_TRUE;
22935 		if (ill->ill_rq != NULL) {
22936 			ill->ill_rq->q_qinfo = &rinit_ipv6;
22937 			ill->ill_wq->q_qinfo = &winit_ipv6;
22938 		}
22939 
22940 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22941 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22942 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22943 		ipif->ipif_v6subnet = ipv6_all_zeros;
22944 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22945 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22946 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22947 		/*
22948 		 * point-to-point or Non-mulicast capable
22949 		 * interfaces won't do NUD unless explicitly
22950 		 * configured to do so.
22951 		 */
22952 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22953 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22954 			ill->ill_flags |= ILLF_NONUD;
22955 		}
22956 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22957 		if (ill->ill_flags & ILLF_NOARP) {
22958 			/*
22959 			 * Note: xresolv interfaces will eventually need
22960 			 * NOARP set here as well, but that will require
22961 			 * those external resolvers to have some
22962 			 * knowledge of that flag and act appropriately.
22963 			 * Not to be changed at present.
22964 			 */
22965 			ill->ill_flags &= ~ILLF_NOARP;
22966 		}
22967 		/*
22968 		 * Set the ILLF_ROUTER flag according to the global
22969 		 * IPv6 forwarding policy.
22970 		 */
22971 		if (ipst->ips_ipv6_forward != 0)
22972 			ill->ill_flags |= ILLF_ROUTER;
22973 	} else if (ill->ill_flags & ILLF_IPV4) {
22974 		ill->ill_isv6 = B_FALSE;
22975 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22976 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22977 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22978 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22979 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22980 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22981 		/*
22982 		 * Set the ILLF_ROUTER flag according to the global
22983 		 * IPv4 forwarding policy.
22984 		 */
22985 		if (ipst->ips_ip_g_forward != 0)
22986 			ill->ill_flags |= ILLF_ROUTER;
22987 	}
22988 
22989 	ASSERT(ill->ill_phyint != NULL);
22990 
22991 	/*
22992 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
22993 	 * be completed in ill_glist_insert -> ill_phyint_reinit
22994 	 */
22995 	if (!ill_allocate_mibs(ill))
22996 		return (ENOMEM);
22997 
22998 	/*
22999 	 * Pick a default sap until we get the DL_INFO_ACK back from
23000 	 * the driver.
23001 	 */
23002 	if (ill->ill_sap == 0) {
23003 		if (ill->ill_isv6)
23004 			ill->ill_sap  = IP6_DL_SAP;
23005 		else
23006 			ill->ill_sap  = IP_DL_SAP;
23007 	}
23008 
23009 	ill->ill_ifname_pending = 1;
23010 	ill->ill_ifname_pending_err = 0;
23011 
23012 	ill_refhold(ill);
23013 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23014 	if ((error = ill_glist_insert(ill, interf_name,
23015 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23016 		ill->ill_ppa = UINT_MAX;
23017 		ill->ill_name[0] = '\0';
23018 		/*
23019 		 * undo null termination done above.
23020 		 */
23021 		ppa_ptr[0] = old_char;
23022 		rw_exit(&ipst->ips_ill_g_lock);
23023 		ill_refrele(ill);
23024 		return (error);
23025 	}
23026 
23027 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23028 
23029 	/*
23030 	 * When we return the buffer pointed to by interf_name should contain
23031 	 * the same name as in ill_name.
23032 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23033 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23034 	 * so copy full name and update the ppa ptr.
23035 	 * When ppa passed in != UINT_MAX all values are correct just undo
23036 	 * null termination, this saves a bcopy.
23037 	 */
23038 	if (*new_ppa_ptr == UINT_MAX) {
23039 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23040 		*new_ppa_ptr = ill->ill_ppa;
23041 	} else {
23042 		/*
23043 		 * undo null termination done above.
23044 		 */
23045 		ppa_ptr[0] = old_char;
23046 	}
23047 
23048 	/* Let SCTP know about this ILL */
23049 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23050 
23051 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23052 	    B_TRUE);
23053 
23054 	rw_exit(&ipst->ips_ill_g_lock);
23055 	ill_refrele(ill);
23056 	if (ipsq == NULL)
23057 		return (EINPROGRESS);
23058 
23059 	/*
23060 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23061 	 */
23062 	if (ipsq->ipsq_current_ipif == NULL)
23063 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23064 	else
23065 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23066 
23067 	error = ipif_set_values_tail(ill, ipif, mp, q);
23068 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
23069 	if (error != 0 && error != EINPROGRESS) {
23070 		/*
23071 		 * restore previous values
23072 		 */
23073 		ill->ill_isv6 = B_FALSE;
23074 	}
23075 	return (error);
23076 }
23077 
23078 
23079 void
23080 ipif_init(ip_stack_t *ipst)
23081 {
23082 	hrtime_t hrt;
23083 	int i;
23084 
23085 	/*
23086 	 * Can't call drv_getparm here as it is too early in the boot.
23087 	 * As we use ipif_src_random just for picking a different
23088 	 * source address everytime, this need not be really random.
23089 	 */
23090 	hrt = gethrtime();
23091 	ipst->ips_ipif_src_random =
23092 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23093 
23094 	for (i = 0; i < MAX_G_HEADS; i++) {
23095 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23096 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23097 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23098 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23099 	}
23100 
23101 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23102 	    ill_phyint_compare_index,
23103 	    sizeof (phyint_t),
23104 	    offsetof(struct phyint, phyint_avl_by_index));
23105 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23106 	    ill_phyint_compare_name,
23107 	    sizeof (phyint_t),
23108 	    offsetof(struct phyint, phyint_avl_by_name));
23109 }
23110 
23111 /*
23112  * Lookup the ipif corresponding to the onlink destination address. For
23113  * point-to-point interfaces, it matches with remote endpoint destination
23114  * address. For point-to-multipoint interfaces it only tries to match the
23115  * destination with the interface's subnet address. The longest, most specific
23116  * match is found to take care of such rare network configurations like -
23117  * le0: 129.146.1.1/16
23118  * le1: 129.146.2.2/24
23119  * It is used only by SO_DONTROUTE at the moment.
23120  */
23121 ipif_t *
23122 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23123 {
23124 	ipif_t	*ipif, *best_ipif;
23125 	ill_t	*ill;
23126 	ill_walk_context_t ctx;
23127 
23128 	ASSERT(zoneid != ALL_ZONES);
23129 	best_ipif = NULL;
23130 
23131 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23132 	ill = ILL_START_WALK_V4(&ctx, ipst);
23133 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23134 		mutex_enter(&ill->ill_lock);
23135 		for (ipif = ill->ill_ipif; ipif != NULL;
23136 		    ipif = ipif->ipif_next) {
23137 			if (!IPIF_CAN_LOOKUP(ipif))
23138 				continue;
23139 			if (ipif->ipif_zoneid != zoneid &&
23140 			    ipif->ipif_zoneid != ALL_ZONES)
23141 				continue;
23142 			/*
23143 			 * Point-to-point case. Look for exact match with
23144 			 * destination address.
23145 			 */
23146 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23147 				if (ipif->ipif_pp_dst_addr == addr) {
23148 					ipif_refhold_locked(ipif);
23149 					mutex_exit(&ill->ill_lock);
23150 					rw_exit(&ipst->ips_ill_g_lock);
23151 					if (best_ipif != NULL)
23152 						ipif_refrele(best_ipif);
23153 					return (ipif);
23154 				}
23155 			} else if (ipif->ipif_subnet == (addr &
23156 			    ipif->ipif_net_mask)) {
23157 				/*
23158 				 * Point-to-multipoint case. Looping through to
23159 				 * find the most specific match. If there are
23160 				 * multiple best match ipif's then prefer ipif's
23161 				 * that are UP. If there is only one best match
23162 				 * ipif and it is DOWN we must still return it.
23163 				 */
23164 				if ((best_ipif == NULL) ||
23165 				    (ipif->ipif_net_mask >
23166 				    best_ipif->ipif_net_mask) ||
23167 				    ((ipif->ipif_net_mask ==
23168 				    best_ipif->ipif_net_mask) &&
23169 				    ((ipif->ipif_flags & IPIF_UP) &&
23170 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23171 					ipif_refhold_locked(ipif);
23172 					mutex_exit(&ill->ill_lock);
23173 					rw_exit(&ipst->ips_ill_g_lock);
23174 					if (best_ipif != NULL)
23175 						ipif_refrele(best_ipif);
23176 					best_ipif = ipif;
23177 					rw_enter(&ipst->ips_ill_g_lock,
23178 					    RW_READER);
23179 					mutex_enter(&ill->ill_lock);
23180 				}
23181 			}
23182 		}
23183 		mutex_exit(&ill->ill_lock);
23184 	}
23185 	rw_exit(&ipst->ips_ill_g_lock);
23186 	return (best_ipif);
23187 }
23188 
23189 
23190 /*
23191  * Save enough information so that we can recreate the IRE if
23192  * the interface goes down and then up.
23193  */
23194 static void
23195 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23196 {
23197 	mblk_t	*save_mp;
23198 
23199 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23200 	if (save_mp != NULL) {
23201 		ifrt_t	*ifrt;
23202 
23203 		save_mp->b_wptr += sizeof (ifrt_t);
23204 		ifrt = (ifrt_t *)save_mp->b_rptr;
23205 		bzero(ifrt, sizeof (ifrt_t));
23206 		ifrt->ifrt_type = ire->ire_type;
23207 		ifrt->ifrt_addr = ire->ire_addr;
23208 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23209 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23210 		ifrt->ifrt_mask = ire->ire_mask;
23211 		ifrt->ifrt_flags = ire->ire_flags;
23212 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23213 		mutex_enter(&ipif->ipif_saved_ire_lock);
23214 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23215 		ipif->ipif_saved_ire_mp = save_mp;
23216 		ipif->ipif_saved_ire_cnt++;
23217 		mutex_exit(&ipif->ipif_saved_ire_lock);
23218 	}
23219 }
23220 
23221 
23222 static void
23223 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23224 {
23225 	mblk_t	**mpp;
23226 	mblk_t	*mp;
23227 	ifrt_t	*ifrt;
23228 
23229 	/* Remove from ipif_saved_ire_mp list if it is there */
23230 	mutex_enter(&ipif->ipif_saved_ire_lock);
23231 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23232 	    mpp = &(*mpp)->b_cont) {
23233 		/*
23234 		 * On a given ipif, the triple of address, gateway and
23235 		 * mask is unique for each saved IRE (in the case of
23236 		 * ordinary interface routes, the gateway address is
23237 		 * all-zeroes).
23238 		 */
23239 		mp = *mpp;
23240 		ifrt = (ifrt_t *)mp->b_rptr;
23241 		if (ifrt->ifrt_addr == ire->ire_addr &&
23242 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23243 		    ifrt->ifrt_mask == ire->ire_mask) {
23244 			*mpp = mp->b_cont;
23245 			ipif->ipif_saved_ire_cnt--;
23246 			freeb(mp);
23247 			break;
23248 		}
23249 	}
23250 	mutex_exit(&ipif->ipif_saved_ire_lock);
23251 }
23252 
23253 
23254 /*
23255  * IP multirouting broadcast routes handling
23256  * Append CGTP broadcast IREs to regular ones created
23257  * at ifconfig time.
23258  */
23259 static void
23260 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23261 {
23262 	ire_t *ire_prim;
23263 
23264 	ASSERT(ire != NULL);
23265 	ASSERT(ire_dst != NULL);
23266 
23267 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23268 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23269 	if (ire_prim != NULL) {
23270 		/*
23271 		 * We are in the special case of broadcasts for
23272 		 * CGTP. We add an IRE_BROADCAST that holds
23273 		 * the RTF_MULTIRT flag, the destination
23274 		 * address of ire_dst and the low level
23275 		 * info of ire_prim. In other words, CGTP
23276 		 * broadcast is added to the redundant ipif.
23277 		 */
23278 		ipif_t *ipif_prim;
23279 		ire_t  *bcast_ire;
23280 
23281 		ipif_prim = ire_prim->ire_ipif;
23282 
23283 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23284 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23285 		    (void *)ire_dst, (void *)ire_prim,
23286 		    (void *)ipif_prim));
23287 
23288 		bcast_ire = ire_create(
23289 		    (uchar_t *)&ire->ire_addr,
23290 		    (uchar_t *)&ip_g_all_ones,
23291 		    (uchar_t *)&ire_dst->ire_src_addr,
23292 		    (uchar_t *)&ire->ire_gateway_addr,
23293 		    &ipif_prim->ipif_mtu,
23294 		    NULL,
23295 		    ipif_prim->ipif_rq,
23296 		    ipif_prim->ipif_wq,
23297 		    IRE_BROADCAST,
23298 		    ipif_prim,
23299 		    0,
23300 		    0,
23301 		    0,
23302 		    ire->ire_flags,
23303 		    &ire_uinfo_null,
23304 		    NULL,
23305 		    NULL,
23306 		    ipst);
23307 
23308 		if (bcast_ire != NULL) {
23309 
23310 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23311 			    B_FALSE) == 0) {
23312 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23313 				    "added bcast_ire %p\n",
23314 				    (void *)bcast_ire));
23315 
23316 				ipif_save_ire(bcast_ire->ire_ipif,
23317 				    bcast_ire);
23318 				ire_refrele(bcast_ire);
23319 			}
23320 		}
23321 		ire_refrele(ire_prim);
23322 	}
23323 }
23324 
23325 
23326 /*
23327  * IP multirouting broadcast routes handling
23328  * Remove the broadcast ire
23329  */
23330 static void
23331 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23332 {
23333 	ire_t *ire_dst;
23334 
23335 	ASSERT(ire != NULL);
23336 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23337 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23338 	if (ire_dst != NULL) {
23339 		ire_t *ire_prim;
23340 
23341 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23342 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23343 		if (ire_prim != NULL) {
23344 			ipif_t *ipif_prim;
23345 			ire_t  *bcast_ire;
23346 
23347 			ipif_prim = ire_prim->ire_ipif;
23348 
23349 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23350 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23351 			    (void *)ire_dst, (void *)ire_prim,
23352 			    (void *)ipif_prim));
23353 
23354 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23355 			    ire->ire_gateway_addr,
23356 			    IRE_BROADCAST,
23357 			    ipif_prim, ALL_ZONES,
23358 			    NULL,
23359 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23360 			    MATCH_IRE_MASK, ipst);
23361 
23362 			if (bcast_ire != NULL) {
23363 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23364 				    "looked up bcast_ire %p\n",
23365 				    (void *)bcast_ire));
23366 				ipif_remove_ire(bcast_ire->ire_ipif,
23367 				    bcast_ire);
23368 				ire_delete(bcast_ire);
23369 			}
23370 			ire_refrele(ire_prim);
23371 		}
23372 		ire_refrele(ire_dst);
23373 	}
23374 }
23375 
23376 /*
23377  * IPsec hardware acceleration capabilities related functions.
23378  */
23379 
23380 /*
23381  * Free a per-ill IPsec capabilities structure.
23382  */
23383 static void
23384 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23385 {
23386 	if (capab->auth_hw_algs != NULL)
23387 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23388 	if (capab->encr_hw_algs != NULL)
23389 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23390 	if (capab->encr_algparm != NULL)
23391 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23392 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23393 }
23394 
23395 /*
23396  * Allocate a new per-ill IPsec capabilities structure. This structure
23397  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23398  * an array which specifies, for each algorithm, whether this algorithm
23399  * is supported by the ill or not.
23400  */
23401 static ill_ipsec_capab_t *
23402 ill_ipsec_capab_alloc(void)
23403 {
23404 	ill_ipsec_capab_t *capab;
23405 	uint_t nelems;
23406 
23407 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23408 	if (capab == NULL)
23409 		return (NULL);
23410 
23411 	/* we need one bit per algorithm */
23412 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23413 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23414 
23415 	/* allocate memory to store algorithm flags */
23416 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23417 	if (capab->encr_hw_algs == NULL)
23418 		goto nomem;
23419 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23420 	if (capab->auth_hw_algs == NULL)
23421 		goto nomem;
23422 	/*
23423 	 * Leave encr_algparm NULL for now since we won't need it half
23424 	 * the time
23425 	 */
23426 	return (capab);
23427 
23428 nomem:
23429 	ill_ipsec_capab_free(capab);
23430 	return (NULL);
23431 }
23432 
23433 /*
23434  * Resize capability array.  Since we're exclusive, this is OK.
23435  */
23436 static boolean_t
23437 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23438 {
23439 	ipsec_capab_algparm_t *nalp, *oalp;
23440 	uint32_t olen, nlen;
23441 
23442 	oalp = capab->encr_algparm;
23443 	olen = capab->encr_algparm_size;
23444 
23445 	if (oalp != NULL) {
23446 		if (algid < capab->encr_algparm_end)
23447 			return (B_TRUE);
23448 	}
23449 
23450 	nlen = (algid + 1) * sizeof (*nalp);
23451 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23452 	if (nalp == NULL)
23453 		return (B_FALSE);
23454 
23455 	if (oalp != NULL) {
23456 		bcopy(oalp, nalp, olen);
23457 		kmem_free(oalp, olen);
23458 	}
23459 	capab->encr_algparm = nalp;
23460 	capab->encr_algparm_size = nlen;
23461 	capab->encr_algparm_end = algid + 1;
23462 
23463 	return (B_TRUE);
23464 }
23465 
23466 /*
23467  * Compare the capabilities of the specified ill with the protocol
23468  * and algorithms specified by the SA passed as argument.
23469  * If they match, returns B_TRUE, B_FALSE if they do not match.
23470  *
23471  * The ill can be passed as a pointer to it, or by specifying its index
23472  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23473  *
23474  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23475  * packet is eligible for hardware acceleration, and by
23476  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23477  * to a particular ill.
23478  */
23479 boolean_t
23480 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23481     ipsa_t *sa, netstack_t *ns)
23482 {
23483 	boolean_t sa_isv6;
23484 	uint_t algid;
23485 	struct ill_ipsec_capab_s *cpp;
23486 	boolean_t need_refrele = B_FALSE;
23487 	ip_stack_t	*ipst = ns->netstack_ip;
23488 
23489 	if (ill == NULL) {
23490 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23491 		    NULL, NULL, NULL, ipst);
23492 		if (ill == NULL) {
23493 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23494 			return (B_FALSE);
23495 		}
23496 		need_refrele = B_TRUE;
23497 	}
23498 
23499 	/*
23500 	 * Use the address length specified by the SA to determine
23501 	 * if it corresponds to a IPv6 address, and fail the matching
23502 	 * if the isv6 flag passed as argument does not match.
23503 	 * Note: this check is used for SADB capability checking before
23504 	 * sending SA information to an ill.
23505 	 */
23506 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23507 	if (sa_isv6 != ill_isv6)
23508 		/* protocol mismatch */
23509 		goto done;
23510 
23511 	/*
23512 	 * Check if the ill supports the protocol, algorithm(s) and
23513 	 * key size(s) specified by the SA, and get the pointers to
23514 	 * the algorithms supported by the ill.
23515 	 */
23516 	switch (sa->ipsa_type) {
23517 
23518 	case SADB_SATYPE_ESP:
23519 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23520 			/* ill does not support ESP acceleration */
23521 			goto done;
23522 		cpp = ill->ill_ipsec_capab_esp;
23523 		algid = sa->ipsa_auth_alg;
23524 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23525 			goto done;
23526 		algid = sa->ipsa_encr_alg;
23527 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23528 			goto done;
23529 		if (algid < cpp->encr_algparm_end) {
23530 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23531 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23532 				goto done;
23533 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23534 				goto done;
23535 		}
23536 		break;
23537 
23538 	case SADB_SATYPE_AH:
23539 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23540 			/* ill does not support AH acceleration */
23541 			goto done;
23542 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23543 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23544 			goto done;
23545 		break;
23546 	}
23547 
23548 	if (need_refrele)
23549 		ill_refrele(ill);
23550 	return (B_TRUE);
23551 done:
23552 	if (need_refrele)
23553 		ill_refrele(ill);
23554 	return (B_FALSE);
23555 }
23556 
23557 
23558 /*
23559  * Add a new ill to the list of IPsec capable ills.
23560  * Called from ill_capability_ipsec_ack() when an ACK was received
23561  * indicating that IPsec hardware processing was enabled for an ill.
23562  *
23563  * ill must point to the ill for which acceleration was enabled.
23564  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23565  */
23566 static void
23567 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23568 {
23569 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23570 	uint_t sa_type;
23571 	uint_t ipproto;
23572 	ip_stack_t	*ipst = ill->ill_ipst;
23573 
23574 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23575 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23576 
23577 	switch (dl_cap) {
23578 	case DL_CAPAB_IPSEC_AH:
23579 		sa_type = SADB_SATYPE_AH;
23580 		ills = &ipst->ips_ipsec_capab_ills_ah;
23581 		ipproto = IPPROTO_AH;
23582 		break;
23583 	case DL_CAPAB_IPSEC_ESP:
23584 		sa_type = SADB_SATYPE_ESP;
23585 		ills = &ipst->ips_ipsec_capab_ills_esp;
23586 		ipproto = IPPROTO_ESP;
23587 		break;
23588 	}
23589 
23590 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23591 
23592 	/*
23593 	 * Add ill index to list of hardware accelerators. If
23594 	 * already in list, do nothing.
23595 	 */
23596 	for (cur_ill = *ills; cur_ill != NULL &&
23597 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23598 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23599 		;
23600 
23601 	if (cur_ill == NULL) {
23602 		/* if this is a new entry for this ill */
23603 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23604 		if (new_ill == NULL) {
23605 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23606 			return;
23607 		}
23608 
23609 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23610 		new_ill->ill_isv6 = ill->ill_isv6;
23611 		new_ill->next = *ills;
23612 		*ills = new_ill;
23613 	} else if (!sadb_resync) {
23614 		/* not resync'ing SADB and an entry exists for this ill */
23615 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23616 		return;
23617 	}
23618 
23619 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23620 
23621 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23622 		/*
23623 		 * IPsec module for protocol loaded, initiate dump
23624 		 * of the SADB to this ill.
23625 		 */
23626 		sadb_ill_download(ill, sa_type);
23627 }
23628 
23629 /*
23630  * Remove an ill from the list of IPsec capable ills.
23631  */
23632 static void
23633 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23634 {
23635 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23636 	ip_stack_t	*ipst = ill->ill_ipst;
23637 
23638 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23639 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23640 
23641 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23642 	    &ipst->ips_ipsec_capab_ills_esp;
23643 
23644 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23645 
23646 	prev_ill = NULL;
23647 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23648 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23649 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23650 		;
23651 	if (cur_ill == NULL) {
23652 		/* entry not found */
23653 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23654 		return;
23655 	}
23656 	if (prev_ill == NULL) {
23657 		/* entry at front of list */
23658 		*ills = NULL;
23659 	} else {
23660 		prev_ill->next = cur_ill->next;
23661 	}
23662 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23663 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23664 }
23665 
23666 /*
23667  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23668  * supporting the specified IPsec protocol acceleration.
23669  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23670  * We free the mblk and, if sa is non-null, release the held referece.
23671  */
23672 void
23673 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23674     netstack_t *ns)
23675 {
23676 	ipsec_capab_ill_t *ici, *cur_ici;
23677 	ill_t *ill;
23678 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23679 	ip_stack_t	*ipst = ns->netstack_ip;
23680 
23681 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23682 	    ipst->ips_ipsec_capab_ills_esp;
23683 
23684 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23685 
23686 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23687 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23688 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23689 
23690 		/*
23691 		 * Handle the case where the ill goes away while the SADB is
23692 		 * attempting to send messages.  If it's going away, it's
23693 		 * nuking its shadow SADB, so we don't care..
23694 		 */
23695 
23696 		if (ill == NULL)
23697 			continue;
23698 
23699 		if (sa != NULL) {
23700 			/*
23701 			 * Make sure capabilities match before
23702 			 * sending SA to ill.
23703 			 */
23704 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23705 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23706 				ill_refrele(ill);
23707 				continue;
23708 			}
23709 
23710 			mutex_enter(&sa->ipsa_lock);
23711 			sa->ipsa_flags |= IPSA_F_HW;
23712 			mutex_exit(&sa->ipsa_lock);
23713 		}
23714 
23715 		/*
23716 		 * Copy template message, and add it to the front
23717 		 * of the mblk ship list. We want to avoid holding
23718 		 * the ipsec_capab_ills_lock while sending the
23719 		 * message to the ills.
23720 		 *
23721 		 * The b_next and b_prev are temporarily used
23722 		 * to build a list of mblks to be sent down, and to
23723 		 * save the ill to which they must be sent.
23724 		 */
23725 		nmp = copymsg(mp);
23726 		if (nmp == NULL) {
23727 			ill_refrele(ill);
23728 			continue;
23729 		}
23730 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23731 		nmp->b_next = mp_ship_list;
23732 		mp_ship_list = nmp;
23733 		nmp->b_prev = (mblk_t *)ill;
23734 	}
23735 
23736 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23737 
23738 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23739 		/* restore the mblk to a sane state */
23740 		next_mp = nmp->b_next;
23741 		nmp->b_next = NULL;
23742 		ill = (ill_t *)nmp->b_prev;
23743 		nmp->b_prev = NULL;
23744 
23745 		ill_dlpi_send(ill, nmp);
23746 		ill_refrele(ill);
23747 	}
23748 
23749 	if (sa != NULL)
23750 		IPSA_REFRELE(sa);
23751 	freemsg(mp);
23752 }
23753 
23754 /*
23755  * Derive an interface id from the link layer address.
23756  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23757  */
23758 static boolean_t
23759 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23760 {
23761 	char		*addr;
23762 
23763 	if (phys_length != ETHERADDRL)
23764 		return (B_FALSE);
23765 
23766 	/* Form EUI-64 like address */
23767 	addr = (char *)&v6addr->s6_addr32[2];
23768 	bcopy((char *)phys_addr, addr, 3);
23769 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23770 	addr[3] = (char)0xff;
23771 	addr[4] = (char)0xfe;
23772 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23773 	return (B_TRUE);
23774 }
23775 
23776 /* ARGSUSED */
23777 static boolean_t
23778 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23779 {
23780 	return (B_FALSE);
23781 }
23782 
23783 /* ARGSUSED */
23784 static boolean_t
23785 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23786     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23787 {
23788 	/*
23789 	 * Multicast address mappings used over Ethernet/802.X.
23790 	 * This address is used as a base for mappings.
23791 	 */
23792 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23793 	    0x00, 0x00, 0x00};
23794 
23795 	/*
23796 	 * Extract low order 32 bits from IPv6 multicast address.
23797 	 * Or that into the link layer address, starting from the
23798 	 * second byte.
23799 	 */
23800 	*hw_start = 2;
23801 	v6_extract_mask->s6_addr32[0] = 0;
23802 	v6_extract_mask->s6_addr32[1] = 0;
23803 	v6_extract_mask->s6_addr32[2] = 0;
23804 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23805 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23806 	return (B_TRUE);
23807 }
23808 
23809 /*
23810  * Indicate by return value whether multicast is supported. If not,
23811  * this code should not touch/change any parameters.
23812  */
23813 /* ARGSUSED */
23814 static boolean_t
23815 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23816     uint32_t *hw_start, ipaddr_t *extract_mask)
23817 {
23818 	/*
23819 	 * Multicast address mappings used over Ethernet/802.X.
23820 	 * This address is used as a base for mappings.
23821 	 */
23822 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23823 	    0x00, 0x00, 0x00 };
23824 
23825 	if (phys_length != ETHERADDRL)
23826 		return (B_FALSE);
23827 
23828 	*extract_mask = htonl(0x007fffff);
23829 	*hw_start = 2;
23830 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23831 	return (B_TRUE);
23832 }
23833 
23834 /*
23835  * Derive IPoIB interface id from the link layer address.
23836  */
23837 static boolean_t
23838 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23839 {
23840 	char		*addr;
23841 
23842 	if (phys_length != 20)
23843 		return (B_FALSE);
23844 	addr = (char *)&v6addr->s6_addr32[2];
23845 	bcopy(phys_addr + 12, addr, 8);
23846 	/*
23847 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23848 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23849 	 * rules. In these cases, the IBA considers these GUIDs to be in
23850 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23851 	 * required; vendors are required not to assign global EUI-64's
23852 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23853 	 * of the interface identifier. Whether the GUID is in modified
23854 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23855 	 * bit set to 1.
23856 	 */
23857 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23858 	return (B_TRUE);
23859 }
23860 
23861 /*
23862  * Note on mapping from multicast IP addresses to IPoIB multicast link
23863  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23864  * The format of an IPoIB multicast address is:
23865  *
23866  *  4 byte QPN      Scope Sign.  Pkey
23867  * +--------------------------------------------+
23868  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23869  * +--------------------------------------------+
23870  *
23871  * The Scope and Pkey components are properties of the IBA port and
23872  * network interface. They can be ascertained from the broadcast address.
23873  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23874  */
23875 
23876 static boolean_t
23877 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23878     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23879 {
23880 	/*
23881 	 * Base IPoIB IPv6 multicast address used for mappings.
23882 	 * Does not contain the IBA scope/Pkey values.
23883 	 */
23884 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23885 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23886 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23887 
23888 	/*
23889 	 * Extract low order 80 bits from IPv6 multicast address.
23890 	 * Or that into the link layer address, starting from the
23891 	 * sixth byte.
23892 	 */
23893 	*hw_start = 6;
23894 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23895 
23896 	/*
23897 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23898 	 */
23899 	*(maddr + 5) = *(bphys_addr + 5);
23900 	*(maddr + 8) = *(bphys_addr + 8);
23901 	*(maddr + 9) = *(bphys_addr + 9);
23902 
23903 	v6_extract_mask->s6_addr32[0] = 0;
23904 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23905 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23906 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23907 	return (B_TRUE);
23908 }
23909 
23910 static boolean_t
23911 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23912     uint32_t *hw_start, ipaddr_t *extract_mask)
23913 {
23914 	/*
23915 	 * Base IPoIB IPv4 multicast address used for mappings.
23916 	 * Does not contain the IBA scope/Pkey values.
23917 	 */
23918 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23919 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23920 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23921 
23922 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23923 		return (B_FALSE);
23924 
23925 	/*
23926 	 * Extract low order 28 bits from IPv4 multicast address.
23927 	 * Or that into the link layer address, starting from the
23928 	 * sixteenth byte.
23929 	 */
23930 	*extract_mask = htonl(0x0fffffff);
23931 	*hw_start = 16;
23932 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23933 
23934 	/*
23935 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23936 	 */
23937 	*(maddr + 5) = *(bphys_addr + 5);
23938 	*(maddr + 8) = *(bphys_addr + 8);
23939 	*(maddr + 9) = *(bphys_addr + 9);
23940 	return (B_TRUE);
23941 }
23942 
23943 /*
23944  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23945  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23946  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23947  * the link-local address is preferred.
23948  */
23949 boolean_t
23950 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23951 {
23952 	ipif_t	*ipif;
23953 	ipif_t	*maybe_ipif = NULL;
23954 
23955 	mutex_enter(&ill->ill_lock);
23956 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23957 		mutex_exit(&ill->ill_lock);
23958 		if (ipifp != NULL)
23959 			*ipifp = NULL;
23960 		return (B_FALSE);
23961 	}
23962 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23963 		if (!IPIF_CAN_LOOKUP(ipif))
23964 			continue;
23965 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23966 		    ipif->ipif_zoneid != ALL_ZONES)
23967 			continue;
23968 		if ((ipif->ipif_flags & flags) != flags)
23969 			continue;
23970 
23971 		if (ipifp == NULL) {
23972 			mutex_exit(&ill->ill_lock);
23973 			ASSERT(maybe_ipif == NULL);
23974 			return (B_TRUE);
23975 		}
23976 		if (!ill->ill_isv6 ||
23977 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23978 			ipif_refhold_locked(ipif);
23979 			mutex_exit(&ill->ill_lock);
23980 			*ipifp = ipif;
23981 			return (B_TRUE);
23982 		}
23983 		if (maybe_ipif == NULL)
23984 			maybe_ipif = ipif;
23985 	}
23986 	if (ipifp != NULL) {
23987 		if (maybe_ipif != NULL)
23988 			ipif_refhold_locked(maybe_ipif);
23989 		*ipifp = maybe_ipif;
23990 	}
23991 	mutex_exit(&ill->ill_lock);
23992 	return (maybe_ipif != NULL);
23993 }
23994 
23995 /*
23996  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23997  */
23998 boolean_t
23999 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24000 {
24001 	ill_t *illg;
24002 	ip_stack_t	*ipst = ill->ill_ipst;
24003 
24004 	/*
24005 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24006 	 */
24007 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24008 		return (B_TRUE);
24009 	}
24010 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24011 	if (ill->ill_group == NULL) {
24012 		/* ill not in a group */
24013 		rw_exit(&ipst->ips_ill_g_lock);
24014 		return (B_FALSE);
24015 	}
24016 
24017 	/*
24018 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24019 	 * group. We need to look for an ipif in the zone on all the ills in the
24020 	 * group.
24021 	 */
24022 	illg = ill->ill_group->illgrp_ill;
24023 	do {
24024 		/*
24025 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24026 		 * that it's not there.
24027 		 */
24028 		if (illg != ill &&
24029 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24030 			break;
24031 		}
24032 	} while ((illg = illg->ill_group_next) != NULL);
24033 	rw_exit(&ipst->ips_ill_g_lock);
24034 	return (illg != NULL);
24035 }
24036 
24037 /*
24038  * Check if this ill is only being used to send ICMP probes for IPMP
24039  */
24040 boolean_t
24041 ill_is_probeonly(ill_t *ill)
24042 {
24043 	/*
24044 	 * Check if the interface is FAILED, or INACTIVE
24045 	 */
24046 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24047 		return (B_TRUE);
24048 
24049 	return (B_FALSE);
24050 }
24051 
24052 /*
24053  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24054  * If a pointer to an ipif_t is returned then the caller will need to do
24055  * an ill_refrele().
24056  *
24057  * If there is no real interface which matches the ifindex, then it looks
24058  * for a group that has a matching index. In the case of a group match the
24059  * lifidx must be zero. We don't need emulate the logical interfaces
24060  * since IP Filter's use of netinfo doesn't use that.
24061  */
24062 ipif_t *
24063 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24064     ip_stack_t *ipst)
24065 {
24066 	ipif_t *ipif;
24067 	ill_t *ill;
24068 
24069 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24070 	    ipst);
24071 
24072 	if (ill == NULL) {
24073 		/* Fallback to group names only if hook_emulation set */
24074 		if (!ipst->ips_ipmp_hook_emulation)
24075 			return (NULL);
24076 
24077 		if (lifidx != 0)
24078 			return (NULL);
24079 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24080 		if (ill == NULL)
24081 			return (NULL);
24082 	}
24083 
24084 	mutex_enter(&ill->ill_lock);
24085 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24086 		mutex_exit(&ill->ill_lock);
24087 		ill_refrele(ill);
24088 		return (NULL);
24089 	}
24090 
24091 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24092 		if (!IPIF_CAN_LOOKUP(ipif))
24093 			continue;
24094 		if (lifidx == ipif->ipif_id) {
24095 			ipif_refhold_locked(ipif);
24096 			break;
24097 		}
24098 	}
24099 
24100 	mutex_exit(&ill->ill_lock);
24101 	ill_refrele(ill);
24102 	return (ipif);
24103 }
24104 
24105 /*
24106  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24107  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24108  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24109  * for details.
24110  */
24111 void
24112 ill_fastpath_flush(ill_t *ill)
24113 {
24114 	ip_stack_t *ipst = ill->ill_ipst;
24115 
24116 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24117 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24118 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24119 }
24120 
24121 /*
24122  * Set the physical address information for `ill' to the contents of the
24123  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24124  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24125  * EINPROGRESS will be returned.
24126  */
24127 int
24128 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24129 {
24130 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24131 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24132 
24133 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24134 
24135 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24136 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24137 		/* Changing DL_IPV6_TOKEN is not yet supported */
24138 		return (0);
24139 	}
24140 
24141 	/*
24142 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24143 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24144 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24145 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24146 	 */
24147 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24148 		freemsg(mp);
24149 		return (ENOMEM);
24150 	}
24151 
24152 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24153 
24154 	/*
24155 	 * If we can quiesce the ill, then set the address.  If not, then
24156 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24157 	 */
24158 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24159 	mutex_enter(&ill->ill_lock);
24160 	if (!ill_is_quiescent(ill)) {
24161 		/* call cannot fail since `conn_t *' argument is NULL */
24162 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24163 		    mp, ILL_DOWN);
24164 		mutex_exit(&ill->ill_lock);
24165 		return (EINPROGRESS);
24166 	}
24167 	mutex_exit(&ill->ill_lock);
24168 
24169 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24170 	return (0);
24171 }
24172 
24173 /*
24174  * Once the ill associated with `q' has quiesced, set its physical address
24175  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24176  * are passed (linked by b_cont), since we sometimes need to save two distinct
24177  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24178  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24179  * is quiesced, we know any stale IREs with the old address information have
24180  * already been removed, so we don't need to call ill_fastpath_flush().
24181  */
24182 /* ARGSUSED */
24183 static void
24184 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24185 {
24186 	ill_t		*ill = q->q_ptr;
24187 	mblk_t		*addrmp2 = unlinkb(addrmp);
24188 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24189 	uint_t		addrlen, addroff;
24190 
24191 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24192 
24193 	addroff	= dlindp->dl_addr_offset;
24194 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24195 
24196 	switch (dlindp->dl_data) {
24197 	case DL_IPV6_LINK_LAYER_ADDR:
24198 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24199 		freemsg(addrmp2);
24200 		break;
24201 
24202 	case DL_CURR_PHYS_ADDR:
24203 		freemsg(ill->ill_phys_addr_mp);
24204 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24205 		ill->ill_phys_addr_mp = addrmp;
24206 		ill->ill_phys_addr_length = addrlen;
24207 
24208 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24209 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24210 		else
24211 			freemsg(addrmp2);
24212 		break;
24213 	default:
24214 		ASSERT(0);
24215 	}
24216 
24217 	/*
24218 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24219 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24220 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24221 	 * brought up.
24222 	 */
24223 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24224 		ipsq_current_finish(ipsq);
24225 }
24226 
24227 /*
24228  * Helper routine for setting the ill_nd_lla fields.
24229  */
24230 void
24231 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24232 {
24233 	freemsg(ill->ill_nd_lla_mp);
24234 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24235 	ill->ill_nd_lla_mp = ndmp;
24236 	ill->ill_nd_lla_len = addrlen;
24237 }
24238 
24239 major_t IP_MAJ;
24240 #define	IP	"ip"
24241 
24242 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24243 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24244 
24245 /*
24246  * Issue REMOVEIF ioctls to have the loopback interfaces
24247  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24248  * the former going away when the user-level processes in the zone
24249  * are killed  * and the latter are cleaned up by the stream head
24250  * str_stack_shutdown callback that undoes all I_PLINKs.
24251  */
24252 void
24253 ip_loopback_cleanup(ip_stack_t *ipst)
24254 {
24255 	int error;
24256 	ldi_handle_t	lh = NULL;
24257 	ldi_ident_t	li = NULL;
24258 	int		rval;
24259 	cred_t		*cr;
24260 	struct strioctl iocb;
24261 	struct lifreq	lifreq;
24262 
24263 	IP_MAJ = ddi_name_to_major(IP);
24264 
24265 #ifdef NS_DEBUG
24266 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24267 	    ipst->ips_netstack->netstack_stackid);
24268 #endif
24269 
24270 	bzero(&lifreq, sizeof (lifreq));
24271 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24272 
24273 	error = ldi_ident_from_major(IP_MAJ, &li);
24274 	if (error) {
24275 #ifdef DEBUG
24276 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24277 		    error);
24278 #endif
24279 		return;
24280 	}
24281 
24282 	cr = zone_get_kcred(netstackid_to_zoneid(
24283 	    ipst->ips_netstack->netstack_stackid));
24284 	ASSERT(cr != NULL);
24285 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24286 	if (error) {
24287 #ifdef DEBUG
24288 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24289 		    error);
24290 #endif
24291 		goto out;
24292 	}
24293 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24294 	iocb.ic_timout = 15;
24295 	iocb.ic_len = sizeof (lifreq);
24296 	iocb.ic_dp = (char *)&lifreq;
24297 
24298 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24299 	/* LINTED - statement has no consequent */
24300 	if (error) {
24301 #ifdef NS_DEBUG
24302 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24303 		    "UDP6 error %d\n", error);
24304 #endif
24305 	}
24306 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24307 	lh = NULL;
24308 
24309 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24310 	if (error) {
24311 #ifdef NS_DEBUG
24312 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24313 		    error);
24314 #endif
24315 		goto out;
24316 	}
24317 
24318 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24319 	iocb.ic_timout = 15;
24320 	iocb.ic_len = sizeof (lifreq);
24321 	iocb.ic_dp = (char *)&lifreq;
24322 
24323 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24324 	/* LINTED - statement has no consequent */
24325 	if (error) {
24326 #ifdef NS_DEBUG
24327 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24328 		    "UDP error %d\n", error);
24329 #endif
24330 	}
24331 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24332 	lh = NULL;
24333 
24334 out:
24335 	/* Close layered handles */
24336 	if (lh)
24337 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24338 	if (li)
24339 		ldi_ident_release(li);
24340 
24341 	crfree(cr);
24342 }
24343