xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision 051aabe6136ff13e81542a427e9693ffe1503525)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strlog.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/kstat.h>
44 #include <sys/debug.h>
45 #include <sys/zone.h>
46 #include <sys/sunldi.h>
47 #include <sys/file.h>
48 #include <sys/bitmap.h>
49 
50 #include <sys/kmem.h>
51 #include <sys/systm.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/isa_defs.h>
55 #include <net/if.h>
56 #include <net/if_arp.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <sys/sockio.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet/igmp_var.h>
65 #include <sys/strsun.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 
69 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
70 #include <inet/mi.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/mib2.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 #include <inet/ip6_asp.h>
77 #include <inet/tcp.h>
78 #include <inet/ip_multi.h>
79 #include <inet/ip_ire.h>
80 #include <inet/ip_ftable.h>
81 #include <inet/ip_rts.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_if.h>
84 #include <inet/ip_impl.h>
85 #include <inet/tun.h>
86 #include <inet/sctp_ip.h>
87 #include <inet/ip_netinfo.h>
88 #include <inet/mib2.h>
89 
90 #include <net/pfkeyv2.h>
91 #include <inet/ipsec_info.h>
92 #include <inet/sadb.h>
93 #include <inet/ipsec_impl.h>
94 #include <sys/iphada.h>
95 
96 
97 #include <netinet/igmp.h>
98 #include <inet/ip_listutils.h>
99 #include <inet/ipclassifier.h>
100 #include <sys/mac.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 /* The character which tells where the ill_name ends */
109 #define	IPIF_SEPARATOR_CHAR	':'
110 
111 /* IP ioctl function table entry */
112 typedef struct ipft_s {
113 	int	ipft_cmd;
114 	pfi_t	ipft_pfi;
115 	int	ipft_min_size;
116 	int	ipft_flags;
117 } ipft_t;
118 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
119 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
120 
121 typedef struct ip_sock_ar_s {
122 	union {
123 		area_t	ip_sock_area;
124 		ared_t	ip_sock_ared;
125 		areq_t	ip_sock_areq;
126 	} ip_sock_ar_u;
127 	queue_t	*ip_sock_ar_q;
128 } ip_sock_ar_t;
129 
130 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
131 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
132 		    char *value, caddr_t cp, cred_t *ioc_cr);
133 
134 static boolean_t ill_is_quiescent(ill_t *);
135 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
136 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
137 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
138     mblk_t *mp, boolean_t need_up);
139 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp, boolean_t need_up);
141 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
144     mblk_t *mp, boolean_t need_up);
145 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
146     mblk_t *mp);
147 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
148     queue_t *q, mblk_t *mp, boolean_t need_up);
149 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
150     int ioccmd, struct linkblk *li, boolean_t doconsist);
151 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
152 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
153 static void	ipsq_flush(ill_t *ill);
154 
155 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
156     queue_t *q, mblk_t *mp, boolean_t need_up);
157 static void	ipsq_delete(ipsq_t *);
158 
159 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
160 		    boolean_t initialize);
161 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
162 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
163 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
164 		    boolean_t isv6);
165 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
166 static void	ipif_delete_cache_ire(ire_t *, char *);
167 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
168 static void	ipif_free(ipif_t *ipif);
169 static void	ipif_free_tail(ipif_t *ipif);
170 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
171 static void	ipif_multicast_down(ipif_t *ipif);
172 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
173 static void	ipif_set_default(ipif_t *ipif);
174 static int	ipif_set_values(queue_t *q, mblk_t *mp,
175     char *interf_name, uint_t *ppa);
176 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
177     queue_t *q);
178 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
179     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
180     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *);
181 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
182 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
183 
184 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
185 static int	ill_arp_off(ill_t *ill);
186 static int	ill_arp_on(ill_t *ill);
187 static void	ill_delete_interface_type(ill_if_t *);
188 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
189 static void	ill_dl_down(ill_t *ill);
190 static void	ill_down(ill_t *ill);
191 static void	ill_downi(ire_t *ire, char *ill_arg);
192 static void	ill_free_mib(ill_t *ill);
193 static void	ill_glist_delete(ill_t *);
194 static boolean_t ill_has_usable_ipif(ill_t *);
195 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
196 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
197 static void	ill_phyint_free(ill_t *ill);
198 static void	ill_phyint_reinit(ill_t *ill);
199 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
200 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
201 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
202 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
203 static void	ill_stq_cache_delete(ire_t *, char *);
204 
205 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
206 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
207 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     in6_addr_t *);
209 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
210     ipaddr_t *);
211 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
212 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
213     in6_addr_t *);
214 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
215     ipaddr_t *);
216 
217 static void	ipif_save_ire(ipif_t *, ire_t *);
218 static void	ipif_remove_ire(ipif_t *, ire_t *);
219 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *);
220 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
221 
222 /*
223  * Per-ill IPsec capabilities management.
224  */
225 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
226 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
227 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
228 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
229 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
230 static void ill_capability_proto(ill_t *, int, mblk_t *);
231 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
232     boolean_t);
233 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
234 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
235 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
236 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
237 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
238 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
239 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
240 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
241     dl_capability_sub_t *);
242 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
243 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
244 static void ill_capability_lso_reset(ill_t *, mblk_t **);
245 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
246 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
247 static void	ill_capability_dls_reset(ill_t *, mblk_t **);
248 static void	ill_capability_dls_disable(ill_t *);
249 
250 static void	illgrp_cache_delete(ire_t *, char *);
251 static void	illgrp_delete(ill_t *ill);
252 static void	illgrp_reset_schednext(ill_t *ill);
253 
254 static ill_t	*ill_prev_usesrc(ill_t *);
255 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
256 static void	ill_disband_usesrc_group(ill_t *);
257 
258 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
259 
260 #ifdef DEBUG
261 static	void	ill_trace_cleanup(const ill_t *);
262 static	void	ipif_trace_cleanup(const ipif_t *);
263 #endif
264 
265 /*
266  * if we go over the memory footprint limit more than once in this msec
267  * interval, we'll start pruning aggressively.
268  */
269 int ip_min_frag_prune_time = 0;
270 
271 /*
272  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
273  * and the IPsec DOI
274  */
275 #define	MAX_IPSEC_ALGS	256
276 
277 #define	BITSPERBYTE	8
278 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
279 
280 #define	IPSEC_ALG_ENABLE(algs, algid) \
281 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
282 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
283 
284 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
285 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
286 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
287 
288 typedef uint8_t ipsec_capab_elem_t;
289 
290 /*
291  * Per-algorithm parameters.  Note that at present, only encryption
292  * algorithms have variable keysize (IKE does not provide a way to negotiate
293  * auth algorithm keysize).
294  *
295  * All sizes here are in bits.
296  */
297 typedef struct
298 {
299 	uint16_t	minkeylen;
300 	uint16_t	maxkeylen;
301 } ipsec_capab_algparm_t;
302 
303 /*
304  * Per-ill capabilities.
305  */
306 struct ill_ipsec_capab_s {
307 	ipsec_capab_elem_t *encr_hw_algs;
308 	ipsec_capab_elem_t *auth_hw_algs;
309 	uint32_t algs_size;	/* size of _hw_algs in bytes */
310 	/* algorithm key lengths */
311 	ipsec_capab_algparm_t *encr_algparm;
312 	uint32_t encr_algparm_size;
313 	uint32_t encr_algparm_end;
314 };
315 
316 /*
317  * The field values are larger than strictly necessary for simple
318  * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls.
319  */
320 static area_t	ip_area_template = {
321 	AR_ENTRY_ADD,			/* area_cmd */
322 	sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl),
323 					/* area_name_offset */
324 	/* area_name_length temporarily holds this structure length */
325 	sizeof (area_t),			/* area_name_length */
326 	IP_ARP_PROTO_TYPE,		/* area_proto */
327 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
328 	IP_ADDR_LEN,			/* area_proto_addr_length */
329 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN,
330 					/* area_proto_mask_offset */
331 	0,				/* area_flags */
332 	sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN,
333 					/* area_hw_addr_offset */
334 	/* Zero length hw_addr_length means 'use your idea of the address' */
335 	0				/* area_hw_addr_length */
336 };
337 
338 /*
339  * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver
340  * support
341  */
342 static area_t	ip6_area_template = {
343 	AR_ENTRY_ADD,			/* area_cmd */
344 	sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t),
345 					/* area_name_offset */
346 	/* area_name_length temporarily holds this structure length */
347 	sizeof (area_t),			/* area_name_length */
348 	IP_ARP_PROTO_TYPE,		/* area_proto */
349 	sizeof (ip_sock_ar_t),		/* area_proto_addr_offset */
350 	IPV6_ADDR_LEN,			/* area_proto_addr_length */
351 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN,
352 					/* area_proto_mask_offset */
353 	0,				/* area_flags */
354 	sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN,
355 					/* area_hw_addr_offset */
356 	/* Zero length hw_addr_length means 'use your idea of the address' */
357 	0				/* area_hw_addr_length */
358 };
359 
360 static ared_t	ip_ared_template = {
361 	AR_ENTRY_DELETE,
362 	sizeof (ared_t) + IP_ADDR_LEN,
363 	sizeof (ared_t),
364 	IP_ARP_PROTO_TYPE,
365 	sizeof (ared_t),
366 	IP_ADDR_LEN
367 };
368 
369 static ared_t	ip6_ared_template = {
370 	AR_ENTRY_DELETE,
371 	sizeof (ared_t) + IPV6_ADDR_LEN,
372 	sizeof (ared_t),
373 	IP_ARP_PROTO_TYPE,
374 	sizeof (ared_t),
375 	IPV6_ADDR_LEN
376 };
377 
378 /*
379  * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as
380  * as the areq doesn't include an IP address in ill_dl_up() (the only place a
381  * areq is used).
382  */
383 static areq_t	ip_areq_template = {
384 	AR_ENTRY_QUERY,			/* cmd */
385 	sizeof (areq_t)+(2*IP_ADDR_LEN),	/* name offset */
386 	sizeof (areq_t),	/* name len (filled by ill_arp_alloc) */
387 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
388 	sizeof (areq_t),			/* target addr offset */
389 	IP_ADDR_LEN,			/* target addr_length */
390 	0,				/* flags */
391 	sizeof (areq_t) + IP_ADDR_LEN,	/* sender addr offset */
392 	IP_ADDR_LEN,			/* sender addr length */
393 	AR_EQ_DEFAULT_XMIT_COUNT,	/* xmit_count */
394 	AR_EQ_DEFAULT_XMIT_INTERVAL,	/* (re)xmit_interval in milliseconds */
395 	AR_EQ_DEFAULT_MAX_BUFFERED	/* max # of requests to buffer */
396 	/* anything else filled in by the code */
397 };
398 
399 static arc_t	ip_aru_template = {
400 	AR_INTERFACE_UP,
401 	sizeof (arc_t),		/* Name offset */
402 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
403 };
404 
405 static arc_t	ip_ard_template = {
406 	AR_INTERFACE_DOWN,
407 	sizeof (arc_t),		/* Name offset */
408 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
409 };
410 
411 static arc_t	ip_aron_template = {
412 	AR_INTERFACE_ON,
413 	sizeof (arc_t),		/* Name offset */
414 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
415 };
416 
417 static arc_t	ip_aroff_template = {
418 	AR_INTERFACE_OFF,
419 	sizeof (arc_t),		/* Name offset */
420 	sizeof (arc_t)		/* Name length (set by ill_arp_alloc) */
421 };
422 
423 
424 static arma_t	ip_arma_multi_template = {
425 	AR_MAPPING_ADD,
426 	sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN,
427 				/* Name offset */
428 	sizeof (arma_t),	/* Name length (set by ill_arp_alloc) */
429 	IP_ARP_PROTO_TYPE,
430 	sizeof (arma_t),			/* proto_addr_offset */
431 	IP_ADDR_LEN,				/* proto_addr_length */
432 	sizeof (arma_t) + IP_ADDR_LEN,		/* proto_mask_offset */
433 	sizeof (arma_t) + 2*IP_ADDR_LEN,	/* proto_extract_mask_offset */
434 	ACE_F_PERMANENT | ACE_F_MAPPING,	/* flags */
435 	sizeof (arma_t) + 3*IP_ADDR_LEN,	/* hw_addr_offset */
436 	IP_MAX_HW_LEN,				/* hw_addr_length */
437 	0,					/* hw_mapping_start */
438 };
439 
440 static ipft_t	ip_ioctl_ftbl[] = {
441 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
442 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
443 		IPFT_F_NO_REPLY },
444 	{ IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t),
445 		IPFT_F_NO_REPLY },
446 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
447 	{ 0 }
448 };
449 
450 /* Simple ICMP IP Header Template */
451 static ipha_t icmp_ipha = {
452 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
453 };
454 
455 /* Flag descriptors for ip_ipif_report */
456 static nv_t	ipif_nv_tbl[] = {
457 	{ IPIF_UP,		"UP" },
458 	{ IPIF_BROADCAST,	"BROADCAST" },
459 	{ ILLF_DEBUG,		"DEBUG" },
460 	{ PHYI_LOOPBACK,	"LOOPBACK" },
461 	{ IPIF_POINTOPOINT,	"POINTOPOINT" },
462 	{ ILLF_NOTRAILERS,	"NOTRAILERS" },
463 	{ PHYI_RUNNING,		"RUNNING" },
464 	{ ILLF_NOARP,		"NOARP" },
465 	{ PHYI_PROMISC,		"PROMISC" },
466 	{ PHYI_ALLMULTI,	"ALLMULTI" },
467 	{ PHYI_INTELLIGENT,	"INTELLIGENT" },
468 	{ ILLF_MULTICAST,	"MULTICAST" },
469 	{ PHYI_MULTI_BCAST,	"MULTI_BCAST" },
470 	{ IPIF_UNNUMBERED,	"UNNUMBERED" },
471 	{ IPIF_DHCPRUNNING,	"DHCP" },
472 	{ IPIF_PRIVATE,		"PRIVATE" },
473 	{ IPIF_NOXMIT,		"NOXMIT" },
474 	{ IPIF_NOLOCAL,		"NOLOCAL" },
475 	{ IPIF_DEPRECATED,	"DEPRECATED" },
476 	{ IPIF_PREFERRED,	"PREFERRED" },
477 	{ IPIF_TEMPORARY,	"TEMPORARY" },
478 	{ IPIF_ADDRCONF,	"ADDRCONF" },
479 	{ PHYI_VIRTUAL,		"VIRTUAL" },
480 	{ ILLF_ROUTER,		"ROUTER" },
481 	{ ILLF_NONUD,		"NONUD" },
482 	{ IPIF_ANYCAST,		"ANYCAST" },
483 	{ ILLF_NORTEXCH,	"NORTEXCH" },
484 	{ ILLF_IPV4,		"IPV4" },
485 	{ ILLF_IPV6,		"IPV6" },
486 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
487 	{ PHYI_FAILED,		"FAILED" },
488 	{ PHYI_STANDBY,		"STANDBY" },
489 	{ PHYI_INACTIVE,	"INACTIVE" },
490 	{ PHYI_OFFLINE,		"OFFLINE" },
491 };
492 
493 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
494 
495 static ip_m_t	ip_m_tbl[] = {
496 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
497 	    ip_ether_v6intfid },
498 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
499 	    ip_nodef_v6intfid },
500 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
501 	    ip_nodef_v6intfid },
502 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
503 	    ip_nodef_v6intfid },
504 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
505 	    ip_ether_v6intfid },
506 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
507 	    ip_ib_v6intfid },
508 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
509 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
510 	    ip_nodef_v6intfid }
511 };
512 
513 static ill_t	ill_null;		/* Empty ILL for init. */
514 char	ipif_loopback_name[] = "lo0";
515 static char *ipv4_forward_suffix = ":ip_forwarding";
516 static char *ipv6_forward_suffix = ":ip6_forwarding";
517 static	sin6_t	sin6_null;	/* Zero address for quick clears */
518 static	sin_t	sin_null;	/* Zero address for quick clears */
519 
520 /* When set search for unused ipif_seqid */
521 static ipif_t	ipif_zero;
522 
523 /*
524  * ppa arena is created after these many
525  * interfaces have been plumbed.
526  */
527 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
528 
529 /*
530  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
531  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
532  * set through platform specific code (Niagara/Ontario).
533  */
534 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
535 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
536 
537 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
538 
539 static uint_t
540 ipif_rand(ip_stack_t *ipst)
541 {
542 	ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 +
543 	    12345;
544 	return ((ipst->ips_ipif_src_random >> 16) & 0x7fff);
545 }
546 
547 /*
548  * Allocate per-interface mibs.
549  * Returns true if ok. False otherwise.
550  *  ipsq  may not yet be allocated (loopback case ).
551  */
552 static boolean_t
553 ill_allocate_mibs(ill_t *ill)
554 {
555 	/* Already allocated? */
556 	if (ill->ill_ip_mib != NULL) {
557 		if (ill->ill_isv6)
558 			ASSERT(ill->ill_icmp6_mib != NULL);
559 		return (B_TRUE);
560 	}
561 
562 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
563 	    KM_NOSLEEP);
564 	if (ill->ill_ip_mib == NULL) {
565 		return (B_FALSE);
566 	}
567 
568 	/* Setup static information */
569 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
570 	    sizeof (mib2_ipIfStatsEntry_t));
571 	if (ill->ill_isv6) {
572 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
573 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
574 		    sizeof (mib2_ipv6AddrEntry_t));
575 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
576 		    sizeof (mib2_ipv6RouteEntry_t));
577 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
578 		    sizeof (mib2_ipv6NetToMediaEntry_t));
579 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
580 		    sizeof (ipv6_member_t));
581 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
582 		    sizeof (ipv6_grpsrc_t));
583 	} else {
584 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
585 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
586 		    sizeof (mib2_ipAddrEntry_t));
587 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
588 		    sizeof (mib2_ipRouteEntry_t));
589 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
590 		    sizeof (mib2_ipNetToMediaEntry_t));
591 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
592 		    sizeof (ip_member_t));
593 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
594 		    sizeof (ip_grpsrc_t));
595 
596 		/*
597 		 * For a v4 ill, we are done at this point, because per ill
598 		 * icmp mibs are only used for v6.
599 		 */
600 		return (B_TRUE);
601 	}
602 
603 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
604 	    KM_NOSLEEP);
605 	if (ill->ill_icmp6_mib == NULL) {
606 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
607 		ill->ill_ip_mib = NULL;
608 		return (B_FALSE);
609 	}
610 	/* static icmp info */
611 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
612 	    sizeof (mib2_ipv6IfIcmpEntry_t);
613 	/*
614 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
615 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
616 	 * -> ill_phyint_reinit
617 	 */
618 	return (B_TRUE);
619 }
620 
621 /*
622  * Common code for preparation of ARP commands.  Two points to remember:
623  * 	1) The ill_name is tacked on at the end of the allocated space so
624  *	   the templates name_offset field must contain the total space
625  *	   to allocate less the name length.
626  *
627  *	2) The templates name_length field should contain the *template*
628  *	   length.  We use it as a parameter to bcopy() and then write
629  *	   the real ill_name_length into the name_length field of the copy.
630  * (Always called as writer.)
631  */
632 mblk_t *
633 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
634 {
635 	arc_t	*arc = (arc_t *)template;
636 	char	*cp;
637 	int	len;
638 	mblk_t	*mp;
639 	uint_t	name_length = ill->ill_name_length;
640 	uint_t	template_len = arc->arc_name_length;
641 
642 	len = arc->arc_name_offset + name_length;
643 	mp = allocb(len, BPRI_HI);
644 	if (mp == NULL)
645 		return (NULL);
646 	cp = (char *)mp->b_rptr;
647 	mp->b_wptr = (uchar_t *)&cp[len];
648 	if (template_len)
649 		bcopy(template, cp, template_len);
650 	if (len > template_len)
651 		bzero(&cp[template_len], len - template_len);
652 	mp->b_datap->db_type = M_PROTO;
653 
654 	arc = (arc_t *)cp;
655 	arc->arc_name_length = name_length;
656 	cp = (char *)arc + arc->arc_name_offset;
657 	bcopy(ill->ill_name, cp, name_length);
658 
659 	if (addr) {
660 		area_t	*area = (area_t *)mp->b_rptr;
661 
662 		cp = (char *)area + area->area_proto_addr_offset;
663 		bcopy(addr, cp, area->area_proto_addr_length);
664 		if (area->area_cmd == AR_ENTRY_ADD) {
665 			cp = (char *)area;
666 			len = area->area_proto_addr_length;
667 			if (area->area_proto_mask_offset)
668 				cp += area->area_proto_mask_offset;
669 			else
670 				cp += area->area_proto_addr_offset + len;
671 			while (len-- > 0)
672 				*cp++ = (char)~0;
673 		}
674 	}
675 	return (mp);
676 }
677 
678 mblk_t *
679 ipif_area_alloc(ipif_t *ipif)
680 {
681 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template,
682 	    (char *)&ipif->ipif_lcl_addr));
683 }
684 
685 mblk_t *
686 ipif_ared_alloc(ipif_t *ipif)
687 {
688 	return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template,
689 	    (char *)&ipif->ipif_lcl_addr));
690 }
691 
692 mblk_t *
693 ill_ared_alloc(ill_t *ill, ipaddr_t addr)
694 {
695 	return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
696 	    (char *)&addr));
697 }
698 
699 /*
700  * Completely vaporize a lower level tap and all associated interfaces.
701  * ill_delete is called only out of ip_close when the device control
702  * stream is being closed.
703  */
704 void
705 ill_delete(ill_t *ill)
706 {
707 	ipif_t	*ipif;
708 	ill_t	*prev_ill;
709 	ip_stack_t	*ipst = ill->ill_ipst;
710 
711 	/*
712 	 * ill_delete may be forcibly entering the ipsq. The previous
713 	 * ioctl may not have completed and may need to be aborted.
714 	 * ipsq_flush takes care of it. If we don't need to enter the
715 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
716 	 * ill_delete_tail is sufficient.
717 	 */
718 	ipsq_flush(ill);
719 
720 	/*
721 	 * Nuke all interfaces.  ipif_free will take down the interface,
722 	 * remove it from the list, and free the data structure.
723 	 * Walk down the ipif list and remove the logical interfaces
724 	 * first before removing the main ipif. We can't unplumb
725 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
726 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
727 	 * POINTOPOINT.
728 	 *
729 	 * If ill_ipif was not properly initialized (i.e low on memory),
730 	 * then no interfaces to clean up. In this case just clean up the
731 	 * ill.
732 	 */
733 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
734 		ipif_free(ipif);
735 
736 	/*
737 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
738 	 * So nobody can be using this mp now. Free the mp allocated for
739 	 * honoring ILLF_NOARP
740 	 */
741 	freemsg(ill->ill_arp_on_mp);
742 	ill->ill_arp_on_mp = NULL;
743 
744 	/* Clean up msgs on pending upcalls for mrouted */
745 	reset_mrt_ill(ill);
746 
747 	/*
748 	 * ipif_free -> reset_conn_ipif will remove all multicast
749 	 * references for IPv4. For IPv6, we need to do it here as
750 	 * it points only at ills.
751 	 */
752 	reset_conn_ill(ill);
753 
754 	/*
755 	 * ill_down will arrange to blow off any IRE's dependent on this
756 	 * ILL, and shut down fragmentation reassembly.
757 	 */
758 	ill_down(ill);
759 
760 	/* Let SCTP know, so that it can remove this from its list. */
761 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
762 
763 	/*
764 	 * If an address on this ILL is being used as a source address then
765 	 * clear out the pointers in other ILLs that point to this ILL.
766 	 */
767 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
768 	if (ill->ill_usesrc_grp_next != NULL) {
769 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
770 			ill_disband_usesrc_group(ill);
771 		} else {	/* consumer of the usesrc ILL */
772 			prev_ill = ill_prev_usesrc(ill);
773 			prev_ill->ill_usesrc_grp_next =
774 			    ill->ill_usesrc_grp_next;
775 		}
776 	}
777 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
778 }
779 
780 static void
781 ipif_non_duplicate(ipif_t *ipif)
782 {
783 	ill_t *ill = ipif->ipif_ill;
784 	mutex_enter(&ill->ill_lock);
785 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
786 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
787 		ASSERT(ill->ill_ipif_dup_count > 0);
788 		ill->ill_ipif_dup_count--;
789 	}
790 	mutex_exit(&ill->ill_lock);
791 }
792 
793 /*
794  * ill_delete_tail is called from ip_modclose after all references
795  * to the closing ill are gone. The wait is done in ip_modclose
796  */
797 void
798 ill_delete_tail(ill_t *ill)
799 {
800 	mblk_t	**mpp;
801 	ipif_t	*ipif;
802 	ip_stack_t	*ipst = ill->ill_ipst;
803 
804 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
805 		ipif_non_duplicate(ipif);
806 		ipif_down_tail(ipif);
807 	}
808 
809 	ASSERT(ill->ill_ipif_dup_count == 0 &&
810 	    ill->ill_arp_down_mp == NULL &&
811 	    ill->ill_arp_del_mapping_mp == NULL);
812 
813 	/*
814 	 * If polling capability is enabled (which signifies direct
815 	 * upcall into IP and driver has ill saved as a handle),
816 	 * we need to make sure that unbind has completed before we
817 	 * let the ill disappear and driver no longer has any reference
818 	 * to this ill.
819 	 */
820 	mutex_enter(&ill->ill_lock);
821 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
822 		cv_wait(&ill->ill_cv, &ill->ill_lock);
823 	mutex_exit(&ill->ill_lock);
824 
825 	/*
826 	 * Clean up polling and soft ring capabilities
827 	 */
828 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
829 		ill_capability_dls_disable(ill);
830 
831 	if (ill->ill_net_type != IRE_LOOPBACK)
832 		qprocsoff(ill->ill_rq);
833 
834 	/*
835 	 * We do an ipsq_flush once again now. New messages could have
836 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
837 	 * could also have landed up if an ioctl thread had looked up
838 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
839 	 * enqueued the ioctl when we did the ipsq_flush last time.
840 	 */
841 	ipsq_flush(ill);
842 
843 	/*
844 	 * Free capabilities.
845 	 */
846 	if (ill->ill_ipsec_capab_ah != NULL) {
847 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
848 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
849 		ill->ill_ipsec_capab_ah = NULL;
850 	}
851 
852 	if (ill->ill_ipsec_capab_esp != NULL) {
853 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
854 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
855 		ill->ill_ipsec_capab_esp = NULL;
856 	}
857 
858 	if (ill->ill_mdt_capab != NULL) {
859 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
860 		ill->ill_mdt_capab = NULL;
861 	}
862 
863 	if (ill->ill_hcksum_capab != NULL) {
864 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
865 		ill->ill_hcksum_capab = NULL;
866 	}
867 
868 	if (ill->ill_zerocopy_capab != NULL) {
869 		kmem_free(ill->ill_zerocopy_capab,
870 		    sizeof (ill_zerocopy_capab_t));
871 		ill->ill_zerocopy_capab = NULL;
872 	}
873 
874 	if (ill->ill_lso_capab != NULL) {
875 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
876 		ill->ill_lso_capab = NULL;
877 	}
878 
879 	if (ill->ill_dls_capab != NULL) {
880 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
881 		ill->ill_dls_capab->ill_unbind_conn = NULL;
882 		kmem_free(ill->ill_dls_capab,
883 		    sizeof (ill_dls_capab_t) +
884 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
885 		ill->ill_dls_capab = NULL;
886 	}
887 
888 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
889 
890 	while (ill->ill_ipif != NULL)
891 		ipif_free_tail(ill->ill_ipif);
892 
893 	/*
894 	 * We have removed all references to ilm from conn and the ones joined
895 	 * within the kernel.
896 	 *
897 	 * We don't walk conns, mrts and ires because
898 	 *
899 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
900 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
901 	 *    ill references.
902 	 */
903 	ASSERT(ilm_walk_ill(ill) == 0);
904 	/*
905 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
906 	 * could free the phyint. No more reference to the phyint after this
907 	 * point.
908 	 */
909 	(void) ill_glist_delete(ill);
910 
911 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
912 	if (ill->ill_ndd_name != NULL)
913 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
914 	rw_exit(&ipst->ips_ip_g_nd_lock);
915 
916 
917 	if (ill->ill_frag_ptr != NULL) {
918 		uint_t count;
919 
920 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
921 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
922 		}
923 		mi_free(ill->ill_frag_ptr);
924 		ill->ill_frag_ptr = NULL;
925 		ill->ill_frag_hash_tbl = NULL;
926 	}
927 
928 	freemsg(ill->ill_nd_lla_mp);
929 	/* Free all retained control messages. */
930 	mpp = &ill->ill_first_mp_to_free;
931 	do {
932 		while (mpp[0]) {
933 			mblk_t  *mp;
934 			mblk_t  *mp1;
935 
936 			mp = mpp[0];
937 			mpp[0] = mp->b_next;
938 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
939 				mp1->b_next = NULL;
940 				mp1->b_prev = NULL;
941 			}
942 			freemsg(mp);
943 		}
944 	} while (mpp++ != &ill->ill_last_mp_to_free);
945 
946 	ill_free_mib(ill);
947 
948 #ifdef DEBUG
949 	ill_trace_cleanup(ill);
950 #endif
951 
952 	/* Drop refcnt here */
953 	netstack_rele(ill->ill_ipst->ips_netstack);
954 	ill->ill_ipst = NULL;
955 }
956 
957 static void
958 ill_free_mib(ill_t *ill)
959 {
960 	ip_stack_t *ipst = ill->ill_ipst;
961 
962 	/*
963 	 * MIB statistics must not be lost, so when an interface
964 	 * goes away the counter values will be added to the global
965 	 * MIBs.
966 	 */
967 	if (ill->ill_ip_mib != NULL) {
968 		if (ill->ill_isv6) {
969 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
970 			    ill->ill_ip_mib);
971 		} else {
972 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
973 			    ill->ill_ip_mib);
974 		}
975 
976 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
977 		ill->ill_ip_mib = NULL;
978 	}
979 	if (ill->ill_icmp6_mib != NULL) {
980 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
981 		    ill->ill_icmp6_mib);
982 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
983 		ill->ill_icmp6_mib = NULL;
984 	}
985 }
986 
987 /*
988  * Concatenate together a physical address and a sap.
989  *
990  * Sap_lengths are interpreted as follows:
991  *   sap_length == 0	==>	no sap
992  *   sap_length > 0	==>	sap is at the head of the dlpi address
993  *   sap_length < 0	==>	sap is at the tail of the dlpi address
994  */
995 static void
996 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
997     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
998 {
999 	uint16_t sap_addr = (uint16_t)sap_src;
1000 
1001 	if (sap_length == 0) {
1002 		if (phys_src == NULL)
1003 			bzero(dst, phys_length);
1004 		else
1005 			bcopy(phys_src, dst, phys_length);
1006 	} else if (sap_length < 0) {
1007 		if (phys_src == NULL)
1008 			bzero(dst, phys_length);
1009 		else
1010 			bcopy(phys_src, dst, phys_length);
1011 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
1012 	} else {
1013 		bcopy(&sap_addr, dst, sizeof (sap_addr));
1014 		if (phys_src == NULL)
1015 			bzero((char *)dst + sap_length, phys_length);
1016 		else
1017 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
1018 	}
1019 }
1020 
1021 /*
1022  * Generate a dl_unitdata_req mblk for the device and address given.
1023  * addr_length is the length of the physical portion of the address.
1024  * If addr is NULL include an all zero address of the specified length.
1025  * TRUE? In any case, addr_length is taken to be the entire length of the
1026  * dlpi address, including the absolute value of sap_length.
1027  */
1028 mblk_t *
1029 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
1030 		t_scalar_t sap_length)
1031 {
1032 	dl_unitdata_req_t *dlur;
1033 	mblk_t	*mp;
1034 	t_scalar_t	abs_sap_length;		/* absolute value */
1035 
1036 	abs_sap_length = ABS(sap_length);
1037 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
1038 	    DL_UNITDATA_REQ);
1039 	if (mp == NULL)
1040 		return (NULL);
1041 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
1042 	/* HACK: accomodate incompatible DLPI drivers */
1043 	if (addr_length == 8)
1044 		addr_length = 6;
1045 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
1046 	dlur->dl_dest_addr_offset = sizeof (*dlur);
1047 	dlur->dl_priority.dl_min = 0;
1048 	dlur->dl_priority.dl_max = 0;
1049 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
1050 	    (uchar_t *)&dlur[1]);
1051 	return (mp);
1052 }
1053 
1054 /*
1055  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
1056  * Return an error if we already have 1 or more ioctls in progress.
1057  * This is used only for non-exclusive ioctls. Currently this is used
1058  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
1059  * and thus need to use ipsq_pending_mp_add.
1060  */
1061 boolean_t
1062 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
1063 {
1064 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1065 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1066 	/*
1067 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1068 	 */
1069 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1070 	    (add_mp->b_datap->db_type == M_IOCTL));
1071 
1072 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1073 	/*
1074 	 * Return error if the conn has started closing. The conn
1075 	 * could have finished cleaning up the pending mp list,
1076 	 * If so we should not add another mp to the list negating
1077 	 * the cleanup.
1078 	 */
1079 	if (connp->conn_state_flags & CONN_CLOSING)
1080 		return (B_FALSE);
1081 	/*
1082 	 * Add the pending mp to the head of the list, chained by b_next.
1083 	 * Note down the conn on which the ioctl request came, in b_prev.
1084 	 * This will be used to later get the conn, when we get a response
1085 	 * on the ill queue, from some other module (typically arp)
1086 	 */
1087 	add_mp->b_next = (void *)ill->ill_pending_mp;
1088 	add_mp->b_queue = CONNP_TO_WQ(connp);
1089 	ill->ill_pending_mp = add_mp;
1090 	if (connp != NULL)
1091 		connp->conn_oper_pending_ill = ill;
1092 	return (B_TRUE);
1093 }
1094 
1095 /*
1096  * Retrieve the ill_pending_mp and return it. We have to walk the list
1097  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1098  */
1099 mblk_t *
1100 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1101 {
1102 	mblk_t	*prev = NULL;
1103 	mblk_t	*curr = NULL;
1104 	uint_t	id;
1105 	conn_t	*connp;
1106 
1107 	/*
1108 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1109 	 * up the pending mp, but it does not know the ioc_id and
1110 	 * passes in a zero for it.
1111 	 */
1112 	mutex_enter(&ill->ill_lock);
1113 	if (ioc_id != 0)
1114 		*connpp = NULL;
1115 
1116 	/* Search the list for the appropriate ioctl based on ioc_id */
1117 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1118 	    prev = curr, curr = curr->b_next) {
1119 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1120 		connp = Q_TO_CONN(curr->b_queue);
1121 		/* Match based on the ioc_id or based on the conn */
1122 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1123 			break;
1124 	}
1125 
1126 	if (curr != NULL) {
1127 		/* Unlink the mblk from the pending mp list */
1128 		if (prev != NULL) {
1129 			prev->b_next = curr->b_next;
1130 		} else {
1131 			ASSERT(ill->ill_pending_mp == curr);
1132 			ill->ill_pending_mp = curr->b_next;
1133 		}
1134 
1135 		/*
1136 		 * conn refcnt must have been bumped up at the start of
1137 		 * the ioctl. So we can safely access the conn.
1138 		 */
1139 		ASSERT(CONN_Q(curr->b_queue));
1140 		*connpp = Q_TO_CONN(curr->b_queue);
1141 		curr->b_next = NULL;
1142 		curr->b_queue = NULL;
1143 	}
1144 
1145 	mutex_exit(&ill->ill_lock);
1146 
1147 	return (curr);
1148 }
1149 
1150 /*
1151  * Add the pending mp to the list. There can be only 1 pending mp
1152  * in the list. Any exclusive ioctl that needs to wait for a response
1153  * from another module or driver needs to use this function to set
1154  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1155  * the other module/driver. This is also used while waiting for the
1156  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1157  */
1158 boolean_t
1159 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1160     int waitfor)
1161 {
1162 	ipsq_t	*ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1163 
1164 	ASSERT(IAM_WRITER_IPIF(ipif));
1165 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1166 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1167 	ASSERT(ipsq->ipsq_pending_mp == NULL);
1168 	/*
1169 	 * The caller may be using a different ipif than the one passed into
1170 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
1171 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
1172 	 * that `ipsq_current_ipif == ipif'.
1173 	 */
1174 	ASSERT(ipsq->ipsq_current_ipif != NULL);
1175 
1176 	/*
1177 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1178 	 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver.
1179 	 */
1180 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1181 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) ||
1182 	    (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO));
1183 
1184 	if (connp != NULL) {
1185 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1186 		/*
1187 		 * Return error if the conn has started closing. The conn
1188 		 * could have finished cleaning up the pending mp list,
1189 		 * If so we should not add another mp to the list negating
1190 		 * the cleanup.
1191 		 */
1192 		if (connp->conn_state_flags & CONN_CLOSING)
1193 			return (B_FALSE);
1194 	}
1195 	mutex_enter(&ipsq->ipsq_lock);
1196 	ipsq->ipsq_pending_ipif = ipif;
1197 	/*
1198 	 * Note down the queue in b_queue. This will be returned by
1199 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1200 	 * the processing
1201 	 */
1202 	add_mp->b_next = NULL;
1203 	add_mp->b_queue = q;
1204 	ipsq->ipsq_pending_mp = add_mp;
1205 	ipsq->ipsq_waitfor = waitfor;
1206 
1207 	if (connp != NULL)
1208 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1209 	mutex_exit(&ipsq->ipsq_lock);
1210 	return (B_TRUE);
1211 }
1212 
1213 /*
1214  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1215  * queued in the list.
1216  */
1217 mblk_t *
1218 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1219 {
1220 	mblk_t	*curr = NULL;
1221 
1222 	mutex_enter(&ipsq->ipsq_lock);
1223 	*connpp = NULL;
1224 	if (ipsq->ipsq_pending_mp == NULL) {
1225 		mutex_exit(&ipsq->ipsq_lock);
1226 		return (NULL);
1227 	}
1228 
1229 	/* There can be only 1 such excl message */
1230 	curr = ipsq->ipsq_pending_mp;
1231 	ASSERT(curr != NULL && curr->b_next == NULL);
1232 	ipsq->ipsq_pending_ipif = NULL;
1233 	ipsq->ipsq_pending_mp = NULL;
1234 	ipsq->ipsq_waitfor = 0;
1235 	mutex_exit(&ipsq->ipsq_lock);
1236 
1237 	if (CONN_Q(curr->b_queue)) {
1238 		/*
1239 		 * This mp did a refhold on the conn, at the start of the ioctl.
1240 		 * So we can safely return a pointer to the conn to the caller.
1241 		 */
1242 		*connpp = Q_TO_CONN(curr->b_queue);
1243 	} else {
1244 		*connpp = NULL;
1245 	}
1246 	curr->b_next = NULL;
1247 	curr->b_prev = NULL;
1248 	return (curr);
1249 }
1250 
1251 /*
1252  * Cleanup the ioctl mp queued in ipsq_pending_mp
1253  * - Called in the ill_delete path
1254  * - Called in the M_ERROR or M_HANGUP path on the ill.
1255  * - Called in the conn close path.
1256  */
1257 boolean_t
1258 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1259 {
1260 	mblk_t	*mp;
1261 	ipsq_t	*ipsq;
1262 	queue_t	*q;
1263 	ipif_t	*ipif;
1264 
1265 	ASSERT(IAM_WRITER_ILL(ill));
1266 	ipsq = ill->ill_phyint->phyint_ipsq;
1267 	mutex_enter(&ipsq->ipsq_lock);
1268 	/*
1269 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1270 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1271 	 * even if it is meant for another ill, since we have to enqueue
1272 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1273 	 * If connp is non-null we are called from the conn close path.
1274 	 */
1275 	mp = ipsq->ipsq_pending_mp;
1276 	if (mp == NULL || (connp != NULL &&
1277 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1278 		mutex_exit(&ipsq->ipsq_lock);
1279 		return (B_FALSE);
1280 	}
1281 	/* Now remove from the ipsq_pending_mp */
1282 	ipsq->ipsq_pending_mp = NULL;
1283 	q = mp->b_queue;
1284 	mp->b_next = NULL;
1285 	mp->b_prev = NULL;
1286 	mp->b_queue = NULL;
1287 
1288 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1289 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1290 	if (ill->ill_move_in_progress) {
1291 		ILL_CLEAR_MOVE(ill);
1292 	} else if (ill->ill_up_ipifs) {
1293 		ill_group_cleanup(ill);
1294 	}
1295 
1296 	ipif = ipsq->ipsq_pending_ipif;
1297 	ipsq->ipsq_pending_ipif = NULL;
1298 	ipsq->ipsq_waitfor = 0;
1299 	ipsq->ipsq_current_ipif = NULL;
1300 	ipsq->ipsq_current_ioctl = 0;
1301 	ipsq->ipsq_current_done = B_TRUE;
1302 	mutex_exit(&ipsq->ipsq_lock);
1303 
1304 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1305 		if (connp == NULL) {
1306 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1307 		} else {
1308 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
1309 			mutex_enter(&ipif->ipif_ill->ill_lock);
1310 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
1311 			mutex_exit(&ipif->ipif_ill->ill_lock);
1312 		}
1313 	} else {
1314 		/*
1315 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1316 		 * be just inet_freemsg. we have to restart it
1317 		 * otherwise the thread will be stuck.
1318 		 */
1319 		inet_freemsg(mp);
1320 	}
1321 	return (B_TRUE);
1322 }
1323 
1324 /*
1325  * The ill is closing. Cleanup all the pending mps. Called exclusively
1326  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1327  * knows this ill, and hence nobody can add an mp to this list
1328  */
1329 static void
1330 ill_pending_mp_cleanup(ill_t *ill)
1331 {
1332 	mblk_t	*mp;
1333 	queue_t	*q;
1334 
1335 	ASSERT(IAM_WRITER_ILL(ill));
1336 
1337 	mutex_enter(&ill->ill_lock);
1338 	/*
1339 	 * Every mp on the pending mp list originating from an ioctl
1340 	 * added 1 to the conn refcnt, at the start of the ioctl.
1341 	 * So bump it down now.  See comments in ip_wput_nondata()
1342 	 */
1343 	while (ill->ill_pending_mp != NULL) {
1344 		mp = ill->ill_pending_mp;
1345 		ill->ill_pending_mp = mp->b_next;
1346 		mutex_exit(&ill->ill_lock);
1347 
1348 		q = mp->b_queue;
1349 		ASSERT(CONN_Q(q));
1350 		mp->b_next = NULL;
1351 		mp->b_prev = NULL;
1352 		mp->b_queue = NULL;
1353 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
1354 		mutex_enter(&ill->ill_lock);
1355 	}
1356 	ill->ill_pending_ipif = NULL;
1357 
1358 	mutex_exit(&ill->ill_lock);
1359 }
1360 
1361 /*
1362  * Called in the conn close path and ill delete path
1363  */
1364 static void
1365 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1366 {
1367 	ipsq_t	*ipsq;
1368 	mblk_t	*prev;
1369 	mblk_t	*curr;
1370 	mblk_t	*next;
1371 	queue_t	*q;
1372 	mblk_t	*tmp_list = NULL;
1373 
1374 	ASSERT(IAM_WRITER_ILL(ill));
1375 	if (connp != NULL)
1376 		q = CONNP_TO_WQ(connp);
1377 	else
1378 		q = ill->ill_wq;
1379 
1380 	ipsq = ill->ill_phyint->phyint_ipsq;
1381 	/*
1382 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1383 	 * In the case of ioctl from a conn, there can be only 1 mp
1384 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1385 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1386 	 * ioctls meant for this ill form conn's are not flushed. They will
1387 	 * be processed during ipsq_exit and will not find the ill and will
1388 	 * return error.
1389 	 */
1390 	mutex_enter(&ipsq->ipsq_lock);
1391 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1392 	    curr = next) {
1393 		next = curr->b_next;
1394 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1395 			/* Unlink the mblk from the pending mp list */
1396 			if (prev != NULL) {
1397 				prev->b_next = curr->b_next;
1398 			} else {
1399 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1400 				ipsq->ipsq_xopq_mphead = curr->b_next;
1401 			}
1402 			if (ipsq->ipsq_xopq_mptail == curr)
1403 				ipsq->ipsq_xopq_mptail = prev;
1404 			/*
1405 			 * Create a temporary list and release the ipsq lock
1406 			 * New elements are added to the head of the tmp_list
1407 			 */
1408 			curr->b_next = tmp_list;
1409 			tmp_list = curr;
1410 		} else {
1411 			prev = curr;
1412 		}
1413 	}
1414 	mutex_exit(&ipsq->ipsq_lock);
1415 
1416 	while (tmp_list != NULL) {
1417 		curr = tmp_list;
1418 		tmp_list = curr->b_next;
1419 		curr->b_next = NULL;
1420 		curr->b_prev = NULL;
1421 		curr->b_queue = NULL;
1422 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1423 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1424 			    CONN_CLOSE : NO_COPYOUT, NULL);
1425 		} else {
1426 			/*
1427 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1428 			 * this can't be just inet_freemsg. we have to
1429 			 * restart it otherwise the thread will be stuck.
1430 			 */
1431 			inet_freemsg(curr);
1432 		}
1433 	}
1434 }
1435 
1436 /*
1437  * This conn has started closing. Cleanup any pending ioctl from this conn.
1438  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1439  */
1440 void
1441 conn_ioctl_cleanup(conn_t *connp)
1442 {
1443 	mblk_t *curr;
1444 	ipsq_t	*ipsq;
1445 	ill_t	*ill;
1446 	boolean_t refheld;
1447 
1448 	/*
1449 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1450 	 * ioctl has not yet started, the mp is pending in the list headed by
1451 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1452 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1453 	 * is currently executing now the mp is not queued anywhere but
1454 	 * conn_oper_pending_ill is null. The conn close will wait
1455 	 * till the conn_ref drops to zero.
1456 	 */
1457 	mutex_enter(&connp->conn_lock);
1458 	ill = connp->conn_oper_pending_ill;
1459 	if (ill == NULL) {
1460 		mutex_exit(&connp->conn_lock);
1461 		return;
1462 	}
1463 
1464 	curr = ill_pending_mp_get(ill, &connp, 0);
1465 	if (curr != NULL) {
1466 		mutex_exit(&connp->conn_lock);
1467 		CONN_DEC_REF(connp);
1468 		inet_freemsg(curr);
1469 		return;
1470 	}
1471 	/*
1472 	 * We may not be able to refhold the ill if the ill/ipif
1473 	 * is changing. But we need to make sure that the ill will
1474 	 * not vanish. So we just bump up the ill_waiter count.
1475 	 */
1476 	refheld = ill_waiter_inc(ill);
1477 	mutex_exit(&connp->conn_lock);
1478 	if (refheld) {
1479 		if (ipsq_enter(ill, B_TRUE)) {
1480 			ill_waiter_dcr(ill);
1481 			/*
1482 			 * Check whether this ioctl has started and is
1483 			 * pending now in ipsq_pending_mp. If it is not
1484 			 * found there then check whether this ioctl has
1485 			 * not even started and is in the ipsq_xopq list.
1486 			 */
1487 			if (!ipsq_pending_mp_cleanup(ill, connp))
1488 				ipsq_xopq_mp_cleanup(ill, connp);
1489 			ipsq = ill->ill_phyint->phyint_ipsq;
1490 			ipsq_exit(ipsq);
1491 			return;
1492 		}
1493 	}
1494 
1495 	/*
1496 	 * The ill is also closing and we could not bump up the
1497 	 * ill_waiter_count or we could not enter the ipsq. Leave
1498 	 * the cleanup to ill_delete
1499 	 */
1500 	mutex_enter(&connp->conn_lock);
1501 	while (connp->conn_oper_pending_ill != NULL)
1502 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1503 	mutex_exit(&connp->conn_lock);
1504 	if (refheld)
1505 		ill_waiter_dcr(ill);
1506 }
1507 
1508 /*
1509  * ipcl_walk function for cleaning up conn_*_ill fields.
1510  */
1511 static void
1512 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1513 {
1514 	ill_t	*ill = (ill_t *)arg;
1515 	ire_t	*ire;
1516 
1517 	mutex_enter(&connp->conn_lock);
1518 	if (connp->conn_multicast_ill == ill) {
1519 		/* Revert to late binding */
1520 		connp->conn_multicast_ill = NULL;
1521 		connp->conn_orig_multicast_ifindex = 0;
1522 	}
1523 	if (connp->conn_incoming_ill == ill)
1524 		connp->conn_incoming_ill = NULL;
1525 	if (connp->conn_outgoing_ill == ill)
1526 		connp->conn_outgoing_ill = NULL;
1527 	if (connp->conn_outgoing_pill == ill)
1528 		connp->conn_outgoing_pill = NULL;
1529 	if (connp->conn_nofailover_ill == ill)
1530 		connp->conn_nofailover_ill = NULL;
1531 	if (connp->conn_dhcpinit_ill == ill) {
1532 		connp->conn_dhcpinit_ill = NULL;
1533 		ASSERT(ill->ill_dhcpinit != 0);
1534 		atomic_dec_32(&ill->ill_dhcpinit);
1535 	}
1536 	if (connp->conn_ire_cache != NULL) {
1537 		ire = connp->conn_ire_cache;
1538 		/*
1539 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1540 		 * interface X and ipif coming from interface Y, if interface
1541 		 * X and Y are part of the same IPMPgroup. Thus whenever
1542 		 * interface X goes down, remove all references to it by
1543 		 * checking both on ire_ipif and ire_stq.
1544 		 */
1545 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1546 		    (ire->ire_type == IRE_CACHE &&
1547 		    ire->ire_stq == ill->ill_wq)) {
1548 			connp->conn_ire_cache = NULL;
1549 			mutex_exit(&connp->conn_lock);
1550 			ire_refrele_notr(ire);
1551 			return;
1552 		}
1553 	}
1554 	mutex_exit(&connp->conn_lock);
1555 
1556 }
1557 
1558 /* ARGSUSED */
1559 void
1560 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1561 {
1562 	ill_t	*ill = q->q_ptr;
1563 	ipif_t	*ipif;
1564 
1565 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1566 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1567 		ipif_non_duplicate(ipif);
1568 		ipif_down_tail(ipif);
1569 	}
1570 	freemsg(mp);
1571 	ipsq_current_finish(ipsq);
1572 }
1573 
1574 /*
1575  * ill_down_start is called when we want to down this ill and bring it up again
1576  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1577  * all interfaces, but don't tear down any plumbing.
1578  */
1579 boolean_t
1580 ill_down_start(queue_t *q, mblk_t *mp)
1581 {
1582 	ill_t	*ill = q->q_ptr;
1583 	ipif_t	*ipif;
1584 
1585 	ASSERT(IAM_WRITER_ILL(ill));
1586 
1587 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1588 		(void) ipif_down(ipif, NULL, NULL);
1589 
1590 	ill_down(ill);
1591 
1592 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1593 
1594 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1595 
1596 	/*
1597 	 * Atomically test and add the pending mp if references are active.
1598 	 */
1599 	mutex_enter(&ill->ill_lock);
1600 	if (!ill_is_quiescent(ill)) {
1601 		/* call cannot fail since `conn_t *' argument is NULL */
1602 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1603 		    mp, ILL_DOWN);
1604 		mutex_exit(&ill->ill_lock);
1605 		return (B_FALSE);
1606 	}
1607 	mutex_exit(&ill->ill_lock);
1608 	return (B_TRUE);
1609 }
1610 
1611 static void
1612 ill_down(ill_t *ill)
1613 {
1614 	ip_stack_t	*ipst = ill->ill_ipst;
1615 
1616 	/* Blow off any IREs dependent on this ILL. */
1617 	ire_walk(ill_downi, (char *)ill, ipst);
1618 
1619 	/* Remove any conn_*_ill depending on this ill */
1620 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1621 
1622 	if (ill->ill_group != NULL) {
1623 		illgrp_delete(ill);
1624 	}
1625 }
1626 
1627 /*
1628  * ire_walk routine used to delete every IRE that depends on queues
1629  * associated with 'ill'.  (Always called as writer.)
1630  */
1631 static void
1632 ill_downi(ire_t *ire, char *ill_arg)
1633 {
1634 	ill_t	*ill = (ill_t *)ill_arg;
1635 
1636 	/*
1637 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1638 	 * interface X and ipif coming from interface Y, if interface
1639 	 * X and Y are part of the same IPMP group. Thus whenever interface
1640 	 * X goes down, remove all references to it by checking both
1641 	 * on ire_ipif and ire_stq.
1642 	 */
1643 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1644 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1645 		ire_delete(ire);
1646 	}
1647 }
1648 
1649 /*
1650  * Remove ire/nce from the fastpath list.
1651  */
1652 void
1653 ill_fastpath_nack(ill_t *ill)
1654 {
1655 	nce_fastpath_list_dispatch(ill, NULL, NULL);
1656 }
1657 
1658 /* Consume an M_IOCACK of the fastpath probe. */
1659 void
1660 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1661 {
1662 	mblk_t	*mp1 = mp;
1663 
1664 	/*
1665 	 * If this was the first attempt turn on the fastpath probing.
1666 	 */
1667 	mutex_enter(&ill->ill_lock);
1668 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1669 		ill->ill_dlpi_fastpath_state = IDS_OK;
1670 	mutex_exit(&ill->ill_lock);
1671 
1672 	/* Free the M_IOCACK mblk, hold on to the data */
1673 	mp = mp->b_cont;
1674 	freeb(mp1);
1675 	if (mp == NULL)
1676 		return;
1677 	if (mp->b_cont != NULL) {
1678 		/*
1679 		 * Update all IRE's or NCE's that are waiting for
1680 		 * fastpath update.
1681 		 */
1682 		nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp);
1683 		mp1 = mp->b_cont;
1684 		freeb(mp);
1685 		mp = mp1;
1686 	} else {
1687 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1688 	}
1689 
1690 	freeb(mp);
1691 }
1692 
1693 /*
1694  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1695  * The data portion of the request is a dl_unitdata_req_t template for
1696  * what we would send downstream in the absence of a fastpath confirmation.
1697  */
1698 int
1699 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1700 {
1701 	struct iocblk	*ioc;
1702 	mblk_t	*mp;
1703 
1704 	if (dlur_mp == NULL)
1705 		return (EINVAL);
1706 
1707 	mutex_enter(&ill->ill_lock);
1708 	switch (ill->ill_dlpi_fastpath_state) {
1709 	case IDS_FAILED:
1710 		/*
1711 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1712 		 * support it.
1713 		 */
1714 		mutex_exit(&ill->ill_lock);
1715 		return (ENOTSUP);
1716 	case IDS_UNKNOWN:
1717 		/* This is the first probe */
1718 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1719 		break;
1720 	default:
1721 		break;
1722 	}
1723 	mutex_exit(&ill->ill_lock);
1724 
1725 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1726 		return (EAGAIN);
1727 
1728 	mp->b_cont = copyb(dlur_mp);
1729 	if (mp->b_cont == NULL) {
1730 		freeb(mp);
1731 		return (EAGAIN);
1732 	}
1733 
1734 	ioc = (struct iocblk *)mp->b_rptr;
1735 	ioc->ioc_count = msgdsize(mp->b_cont);
1736 
1737 	putnext(ill->ill_wq, mp);
1738 	return (0);
1739 }
1740 
1741 void
1742 ill_capability_probe(ill_t *ill)
1743 {
1744 	/*
1745 	 * Do so only if capabilities are still unknown.
1746 	 */
1747 	if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
1748 		return;
1749 
1750 	ill->ill_dlpi_capab_state = IDS_INPROGRESS;
1751 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1752 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1753 }
1754 
1755 void
1756 ill_capability_reset(ill_t *ill)
1757 {
1758 	mblk_t *sc_mp = NULL;
1759 	mblk_t *tmp;
1760 
1761 	/*
1762 	 * Note here that we reset the state to UNKNOWN, and later send
1763 	 * down the DL_CAPABILITY_REQ without first setting the state to
1764 	 * INPROGRESS.  We do this in order to distinguish the
1765 	 * DL_CAPABILITY_ACK response which may come back in response to
1766 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1767 	 * also handle the case where the driver doesn't send us back
1768 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1769 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1770 	 * features are turned off until the state reaches IDS_OK.
1771 	 */
1772 	ill->ill_dlpi_capab_state = IDS_UNKNOWN;
1773 	ill->ill_capab_reneg = B_FALSE;
1774 
1775 	/*
1776 	 * Disable sub-capabilities and request a list of sub-capability
1777 	 * messages which will be sent down to the driver.  Each handler
1778 	 * allocates the corresponding dl_capability_sub_t inside an
1779 	 * mblk, and links it to the existing sc_mp mblk, or return it
1780 	 * as sc_mp if it's the first sub-capability (the passed in
1781 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1782 	 * sc_mp will be pulled-up, before passing it downstream.
1783 	 */
1784 	ill_capability_mdt_reset(ill, &sc_mp);
1785 	ill_capability_hcksum_reset(ill, &sc_mp);
1786 	ill_capability_zerocopy_reset(ill, &sc_mp);
1787 	ill_capability_ipsec_reset(ill, &sc_mp);
1788 	ill_capability_dls_reset(ill, &sc_mp);
1789 	ill_capability_lso_reset(ill, &sc_mp);
1790 
1791 	/* Nothing to send down in order to disable the capabilities? */
1792 	if (sc_mp == NULL)
1793 		return;
1794 
1795 	tmp = msgpullup(sc_mp, -1);
1796 	freemsg(sc_mp);
1797 	if ((sc_mp = tmp) == NULL) {
1798 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1799 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1800 		return;
1801 	}
1802 
1803 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1804 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1805 }
1806 
1807 /*
1808  * Request or set new-style hardware capabilities supported by DLS provider.
1809  */
1810 static void
1811 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1812 {
1813 	mblk_t *mp;
1814 	dl_capability_req_t *capb;
1815 	size_t size = 0;
1816 	uint8_t *ptr;
1817 
1818 	if (reqp != NULL)
1819 		size = MBLKL(reqp);
1820 
1821 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1822 	if (mp == NULL) {
1823 		freemsg(reqp);
1824 		return;
1825 	}
1826 	ptr = mp->b_rptr;
1827 
1828 	capb = (dl_capability_req_t *)ptr;
1829 	ptr += sizeof (dl_capability_req_t);
1830 
1831 	if (reqp != NULL) {
1832 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1833 		capb->dl_sub_length = size;
1834 		bcopy(reqp->b_rptr, ptr, size);
1835 		ptr += size;
1836 		mp->b_cont = reqp->b_cont;
1837 		freeb(reqp);
1838 	}
1839 	ASSERT(ptr == mp->b_wptr);
1840 
1841 	ill_dlpi_send(ill, mp);
1842 }
1843 
1844 static void
1845 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1846 {
1847 	dl_capab_id_t *id_ic;
1848 	uint_t sub_dl_cap = outers->dl_cap;
1849 	dl_capability_sub_t *inners;
1850 	uint8_t *capend;
1851 
1852 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1853 
1854 	/*
1855 	 * Note: range checks here are not absolutely sufficient to
1856 	 * make us robust against malformed messages sent by drivers;
1857 	 * this is in keeping with the rest of IP's dlpi handling.
1858 	 * (Remember, it's coming from something else in the kernel
1859 	 * address space)
1860 	 */
1861 
1862 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1863 	if (capend > mp->b_wptr) {
1864 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1865 		    "malformed sub-capability too long for mblk");
1866 		return;
1867 	}
1868 
1869 	id_ic = (dl_capab_id_t *)(outers + 1);
1870 
1871 	if (outers->dl_length < sizeof (*id_ic) ||
1872 	    (inners = &id_ic->id_subcap,
1873 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1874 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1875 		    "encapsulated capab type %d too long for mblk",
1876 		    inners->dl_cap);
1877 		return;
1878 	}
1879 
1880 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1881 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1882 		    "isn't as expected; pass-thru module(s) detected, "
1883 		    "discarding capability\n", inners->dl_cap));
1884 		return;
1885 	}
1886 
1887 	/* Process the encapsulated sub-capability */
1888 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1889 }
1890 
1891 /*
1892  * Process Multidata Transmit capability negotiation ack received from a
1893  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1894  * DL_CAPABILITY_ACK message.
1895  */
1896 static void
1897 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1898 {
1899 	mblk_t *nmp = NULL;
1900 	dl_capability_req_t *oc;
1901 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1902 	ill_mdt_capab_t **ill_mdt_capab;
1903 	uint_t sub_dl_cap = isub->dl_cap;
1904 	uint8_t *capend;
1905 
1906 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1907 
1908 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1909 
1910 	/*
1911 	 * Note: range checks here are not absolutely sufficient to
1912 	 * make us robust against malformed messages sent by drivers;
1913 	 * this is in keeping with the rest of IP's dlpi handling.
1914 	 * (Remember, it's coming from something else in the kernel
1915 	 * address space)
1916 	 */
1917 
1918 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1919 	if (capend > mp->b_wptr) {
1920 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1921 		    "malformed sub-capability too long for mblk");
1922 		return;
1923 	}
1924 
1925 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1926 
1927 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1928 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1929 		    "unsupported MDT sub-capability (version %d, expected %d)",
1930 		    mdt_ic->mdt_version, MDT_VERSION_2);
1931 		return;
1932 	}
1933 
1934 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1935 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1936 		    "capability isn't as expected; pass-thru module(s) "
1937 		    "detected, discarding capability\n"));
1938 		return;
1939 	}
1940 
1941 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1942 
1943 		if (*ill_mdt_capab == NULL) {
1944 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1945 			    KM_NOSLEEP);
1946 
1947 			if (*ill_mdt_capab == NULL) {
1948 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1949 				    "could not enable MDT version %d "
1950 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1951 				    ill->ill_name);
1952 				return;
1953 			}
1954 		}
1955 
1956 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1957 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1958 		    "header spaces, %d max pld bufs, %d span limit)\n",
1959 		    ill->ill_name, MDT_VERSION_2,
1960 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1961 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1962 
1963 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1964 		(*ill_mdt_capab)->ill_mdt_on = 1;
1965 		/*
1966 		 * Round the following values to the nearest 32-bit; ULP
1967 		 * may further adjust them to accomodate for additional
1968 		 * protocol headers.  We pass these values to ULP during
1969 		 * bind time.
1970 		 */
1971 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1972 		    roundup(mdt_ic->mdt_hdr_head, 4);
1973 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1974 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1975 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1976 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
1977 
1978 		ill->ill_capabilities |= ILL_CAPAB_MDT;
1979 	} else {
1980 		uint_t size;
1981 		uchar_t *rptr;
1982 
1983 		size = sizeof (dl_capability_req_t) +
1984 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
1985 
1986 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1987 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1988 			    "could not enable MDT for %s (ENOMEM)\n",
1989 			    ill->ill_name);
1990 			return;
1991 		}
1992 
1993 		rptr = nmp->b_rptr;
1994 		/* initialize dl_capability_req_t */
1995 		oc = (dl_capability_req_t *)nmp->b_rptr;
1996 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1997 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1998 		    sizeof (dl_capab_mdt_t);
1999 		nmp->b_rptr += sizeof (dl_capability_req_t);
2000 
2001 		/* initialize dl_capability_sub_t */
2002 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2003 		nmp->b_rptr += sizeof (*isub);
2004 
2005 		/* initialize dl_capab_mdt_t */
2006 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2007 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2008 
2009 		nmp->b_rptr = rptr;
2010 
2011 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2012 		    "to enable MDT version %d\n", ill->ill_name,
2013 		    MDT_VERSION_2));
2014 
2015 		/* set ENABLE flag */
2016 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2017 
2018 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2019 		ill_dlpi_send(ill, nmp);
2020 	}
2021 }
2022 
2023 static void
2024 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2025 {
2026 	mblk_t *mp;
2027 	dl_capab_mdt_t *mdt_subcap;
2028 	dl_capability_sub_t *dl_subcap;
2029 	int size;
2030 
2031 	if (!ILL_MDT_CAPABLE(ill))
2032 		return;
2033 
2034 	ASSERT(ill->ill_mdt_capab != NULL);
2035 	/*
2036 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2037 	 * structure since it's possible that another thread is still
2038 	 * referring to it.  The structure only gets deallocated when
2039 	 * we destroy the ill.
2040 	 */
2041 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2042 
2043 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2044 
2045 	mp = allocb(size, BPRI_HI);
2046 	if (mp == NULL) {
2047 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2048 		    "request to disable MDT\n"));
2049 		return;
2050 	}
2051 
2052 	mp->b_wptr = mp->b_rptr + size;
2053 
2054 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2055 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2056 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2057 
2058 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2059 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2060 	mdt_subcap->mdt_flags = 0;
2061 	mdt_subcap->mdt_hdr_head = 0;
2062 	mdt_subcap->mdt_hdr_tail = 0;
2063 
2064 	if (*sc_mp != NULL)
2065 		linkb(*sc_mp, mp);
2066 	else
2067 		*sc_mp = mp;
2068 }
2069 
2070 /*
2071  * Send a DL_NOTIFY_REQ to the specified ill to enable
2072  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2073  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2074  * acceleration.
2075  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2076  */
2077 static boolean_t
2078 ill_enable_promisc_notify(ill_t *ill)
2079 {
2080 	mblk_t *mp;
2081 	dl_notify_req_t *req;
2082 
2083 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2084 
2085 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2086 	if (mp == NULL)
2087 		return (B_FALSE);
2088 
2089 	req = (dl_notify_req_t *)mp->b_rptr;
2090 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2091 	    DL_NOTE_PROMISC_OFF_PHYS;
2092 
2093 	ill_dlpi_send(ill, mp);
2094 
2095 	return (B_TRUE);
2096 }
2097 
2098 
2099 /*
2100  * Allocate an IPsec capability request which will be filled by our
2101  * caller to turn on support for one or more algorithms.
2102  */
2103 static mblk_t *
2104 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2105 {
2106 	mblk_t *nmp;
2107 	dl_capability_req_t	*ocap;
2108 	dl_capab_ipsec_t	*ocip;
2109 	dl_capab_ipsec_t	*icip;
2110 	uint8_t			*ptr;
2111 	icip = (dl_capab_ipsec_t *)(isub + 1);
2112 
2113 	/*
2114 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2115 	 * PROMISC_ON/OFF notification from the provider. We need to
2116 	 * do this before enabling the algorithms to avoid leakage of
2117 	 * cleartext packets.
2118 	 */
2119 
2120 	if (!ill_enable_promisc_notify(ill))
2121 		return (NULL);
2122 
2123 	/*
2124 	 * Allocate new mblk which will contain a new capability
2125 	 * request to enable the capabilities.
2126 	 */
2127 
2128 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2129 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2130 	if (nmp == NULL)
2131 		return (NULL);
2132 
2133 	ptr = nmp->b_rptr;
2134 
2135 	/* initialize dl_capability_req_t */
2136 	ocap = (dl_capability_req_t *)ptr;
2137 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2138 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2139 	ptr += sizeof (dl_capability_req_t);
2140 
2141 	/* initialize dl_capability_sub_t */
2142 	bcopy(isub, ptr, sizeof (*isub));
2143 	ptr += sizeof (*isub);
2144 
2145 	/* initialize dl_capab_ipsec_t */
2146 	ocip = (dl_capab_ipsec_t *)ptr;
2147 	bcopy(icip, ocip, sizeof (*icip));
2148 
2149 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2150 	return (nmp);
2151 }
2152 
2153 /*
2154  * Process an IPsec capability negotiation ack received from a DLS Provider.
2155  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2156  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2157  */
2158 static void
2159 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2160 {
2161 	dl_capab_ipsec_t	*icip;
2162 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2163 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2164 	uint_t cipher, nciphers;
2165 	mblk_t *nmp;
2166 	uint_t alg_len;
2167 	boolean_t need_sadb_dump;
2168 	uint_t sub_dl_cap = isub->dl_cap;
2169 	ill_ipsec_capab_t **ill_capab;
2170 	uint64_t ill_capab_flag;
2171 	uint8_t *capend, *ciphend;
2172 	boolean_t sadb_resync;
2173 
2174 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2175 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2176 
2177 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2178 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2179 		ill_capab_flag = ILL_CAPAB_AH;
2180 	} else {
2181 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2182 		ill_capab_flag = ILL_CAPAB_ESP;
2183 	}
2184 
2185 	/*
2186 	 * If the ill capability structure exists, then this incoming
2187 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2188 	 * If this is so, then we'd need to resynchronize the SADB
2189 	 * after re-enabling the offloaded ciphers.
2190 	 */
2191 	sadb_resync = (*ill_capab != NULL);
2192 
2193 	/*
2194 	 * Note: range checks here are not absolutely sufficient to
2195 	 * make us robust against malformed messages sent by drivers;
2196 	 * this is in keeping with the rest of IP's dlpi handling.
2197 	 * (Remember, it's coming from something else in the kernel
2198 	 * address space)
2199 	 */
2200 
2201 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2202 	if (capend > mp->b_wptr) {
2203 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2204 		    "malformed sub-capability too long for mblk");
2205 		return;
2206 	}
2207 
2208 	/*
2209 	 * There are two types of acks we process here:
2210 	 * 1. acks in reply to a (first form) generic capability req
2211 	 *    (no ENABLE flag set)
2212 	 * 2. acks in reply to a ENABLE capability req.
2213 	 *    (ENABLE flag set)
2214 	 *
2215 	 * We process the subcapability passed as argument as follows:
2216 	 * 1 do initializations
2217 	 *   1.1 initialize nmp = NULL
2218 	 *   1.2 set need_sadb_dump to B_FALSE
2219 	 * 2 for each cipher in subcapability:
2220 	 *   2.1 if ENABLE flag is set:
2221 	 *	2.1.1 update per-ill ipsec capabilities info
2222 	 *	2.1.2 set need_sadb_dump to B_TRUE
2223 	 *   2.2 if ENABLE flag is not set:
2224 	 *	2.2.1 if nmp is NULL:
2225 	 *		2.2.1.1 allocate and initialize nmp
2226 	 *		2.2.1.2 init current pos in nmp
2227 	 *	2.2.2 copy current cipher to current pos in nmp
2228 	 *	2.2.3 set ENABLE flag in nmp
2229 	 *	2.2.4 update current pos
2230 	 * 3 if nmp is not equal to NULL, send enable request
2231 	 *   3.1 send capability request
2232 	 * 4 if need_sadb_dump is B_TRUE
2233 	 *   4.1 enable promiscuous on/off notifications
2234 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2235 	 *	AH or ESP SA's to interface.
2236 	 */
2237 
2238 	nmp = NULL;
2239 	oalg = NULL;
2240 	need_sadb_dump = B_FALSE;
2241 	icip = (dl_capab_ipsec_t *)(isub + 1);
2242 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2243 
2244 	nciphers = icip->cip_nciphers;
2245 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2246 
2247 	if (ciphend > capend) {
2248 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2249 		    "too many ciphers for sub-capability len");
2250 		return;
2251 	}
2252 
2253 	for (cipher = 0; cipher < nciphers; cipher++) {
2254 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2255 
2256 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2257 			/*
2258 			 * TBD: when we provide a way to disable capabilities
2259 			 * from above, need to manage the request-pending state
2260 			 * and fail if we were not expecting this ACK.
2261 			 */
2262 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2263 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2264 
2265 			/*
2266 			 * Update IPsec capabilities for this ill
2267 			 */
2268 
2269 			if (*ill_capab == NULL) {
2270 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2271 				    ("ill_capability_ipsec_ack: "
2272 				    "allocating ipsec_capab for ill\n"));
2273 				*ill_capab = ill_ipsec_capab_alloc();
2274 
2275 				if (*ill_capab == NULL) {
2276 					cmn_err(CE_WARN,
2277 					    "ill_capability_ipsec_ack: "
2278 					    "could not enable IPsec Hardware "
2279 					    "acceleration for %s (ENOMEM)\n",
2280 					    ill->ill_name);
2281 					return;
2282 				}
2283 			}
2284 
2285 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2286 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2287 
2288 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2289 				cmn_err(CE_WARN,
2290 				    "ill_capability_ipsec_ack: "
2291 				    "malformed IPsec algorithm id %d",
2292 				    ialg->alg_prim);
2293 				continue;
2294 			}
2295 
2296 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2297 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2298 				    ialg->alg_prim);
2299 			} else {
2300 				ipsec_capab_algparm_t *alp;
2301 
2302 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2303 				    ialg->alg_prim);
2304 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2305 				    ialg->alg_prim)) {
2306 					cmn_err(CE_WARN,
2307 					    "ill_capability_ipsec_ack: "
2308 					    "no space for IPsec alg id %d",
2309 					    ialg->alg_prim);
2310 					continue;
2311 				}
2312 				alp = &((*ill_capab)->encr_algparm[
2313 				    ialg->alg_prim]);
2314 				alp->minkeylen = ialg->alg_minbits;
2315 				alp->maxkeylen = ialg->alg_maxbits;
2316 			}
2317 			ill->ill_capabilities |= ill_capab_flag;
2318 			/*
2319 			 * indicate that a capability was enabled, which
2320 			 * will be used below to kick off a SADB dump
2321 			 * to the ill.
2322 			 */
2323 			need_sadb_dump = B_TRUE;
2324 		} else {
2325 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2326 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2327 			    ialg->alg_prim));
2328 
2329 			if (nmp == NULL) {
2330 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2331 				if (nmp == NULL) {
2332 					/*
2333 					 * Sending the PROMISC_ON/OFF
2334 					 * notification request failed.
2335 					 * We cannot enable the algorithms
2336 					 * since the Provider will not
2337 					 * notify IP of promiscous mode
2338 					 * changes, which could lead
2339 					 * to leakage of packets.
2340 					 */
2341 					cmn_err(CE_WARN,
2342 					    "ill_capability_ipsec_ack: "
2343 					    "could not enable IPsec Hardware "
2344 					    "acceleration for %s (ENOMEM)\n",
2345 					    ill->ill_name);
2346 					return;
2347 				}
2348 				/* ptr to current output alg specifier */
2349 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2350 			}
2351 
2352 			/*
2353 			 * Copy current alg specifier, set ENABLE
2354 			 * flag, and advance to next output alg.
2355 			 * For now we enable all IPsec capabilities.
2356 			 */
2357 			ASSERT(oalg != NULL);
2358 			bcopy(ialg, oalg, alg_len);
2359 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2360 			nmp->b_wptr += alg_len;
2361 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2362 		}
2363 
2364 		/* move to next input algorithm specifier */
2365 		ialg = (dl_capab_ipsec_alg_t *)
2366 		    ((char *)ialg + alg_len);
2367 	}
2368 
2369 	if (nmp != NULL)
2370 		/*
2371 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2372 		 * IPsec hardware acceleration.
2373 		 */
2374 		ill_dlpi_send(ill, nmp);
2375 
2376 	if (need_sadb_dump)
2377 		/*
2378 		 * An acknowledgement corresponding to a request to
2379 		 * enable acceleration was received, notify SADB.
2380 		 */
2381 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2382 }
2383 
2384 /*
2385  * Given an mblk with enough space in it, create sub-capability entries for
2386  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2387  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2388  * in preparation for the reset the DL_CAPABILITY_REQ message.
2389  */
2390 static void
2391 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2392     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2393 {
2394 	dl_capab_ipsec_t *oipsec;
2395 	dl_capab_ipsec_alg_t *oalg;
2396 	dl_capability_sub_t *dl_subcap;
2397 	int i, k;
2398 
2399 	ASSERT(nciphers > 0);
2400 	ASSERT(ill_cap != NULL);
2401 	ASSERT(mp != NULL);
2402 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2403 
2404 	/* dl_capability_sub_t for "stype" */
2405 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2406 	dl_subcap->dl_cap = stype;
2407 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2408 	mp->b_wptr += sizeof (dl_capability_sub_t);
2409 
2410 	/* dl_capab_ipsec_t for "stype" */
2411 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2412 	oipsec->cip_version = 1;
2413 	oipsec->cip_nciphers = nciphers;
2414 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2415 
2416 	/* create entries for "stype" AUTH ciphers */
2417 	for (i = 0; i < ill_cap->algs_size; i++) {
2418 		for (k = 0; k < BITSPERBYTE; k++) {
2419 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2420 				continue;
2421 
2422 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2423 			bzero((void *)oalg, sizeof (*oalg));
2424 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2425 			oalg->alg_prim = k + (BITSPERBYTE * i);
2426 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2427 		}
2428 	}
2429 	/* create entries for "stype" ENCR ciphers */
2430 	for (i = 0; i < ill_cap->algs_size; i++) {
2431 		for (k = 0; k < BITSPERBYTE; k++) {
2432 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2433 				continue;
2434 
2435 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2436 			bzero((void *)oalg, sizeof (*oalg));
2437 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2438 			oalg->alg_prim = k + (BITSPERBYTE * i);
2439 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2440 		}
2441 	}
2442 }
2443 
2444 /*
2445  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2446  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2447  * POPC instruction, but our macro is more flexible for an arbitrary length
2448  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2449  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2450  * stays that way, we can reduce the number of iterations required.
2451  */
2452 #define	COUNT_1S(val, sum) {					\
2453 	uint8_t x = val & 0xff;					\
2454 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2455 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2456 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2457 }
2458 
2459 /* ARGSUSED */
2460 static void
2461 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2462 {
2463 	mblk_t *mp;
2464 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2465 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2466 	uint64_t ill_capabilities = ill->ill_capabilities;
2467 	int ah_cnt = 0, esp_cnt = 0;
2468 	int ah_len = 0, esp_len = 0;
2469 	int i, size = 0;
2470 
2471 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2472 		return;
2473 
2474 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2475 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2476 
2477 	/* Find out the number of ciphers for AH */
2478 	if (cap_ah != NULL) {
2479 		for (i = 0; i < cap_ah->algs_size; i++) {
2480 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2481 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2482 		}
2483 		if (ah_cnt > 0) {
2484 			size += sizeof (dl_capability_sub_t) +
2485 			    sizeof (dl_capab_ipsec_t);
2486 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2487 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2488 			size += ah_len;
2489 		}
2490 	}
2491 
2492 	/* Find out the number of ciphers for ESP */
2493 	if (cap_esp != NULL) {
2494 		for (i = 0; i < cap_esp->algs_size; i++) {
2495 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2496 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2497 		}
2498 		if (esp_cnt > 0) {
2499 			size += sizeof (dl_capability_sub_t) +
2500 			    sizeof (dl_capab_ipsec_t);
2501 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2502 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2503 			size += esp_len;
2504 		}
2505 	}
2506 
2507 	if (size == 0) {
2508 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2509 		    "there's nothing to reset\n"));
2510 		return;
2511 	}
2512 
2513 	mp = allocb(size, BPRI_HI);
2514 	if (mp == NULL) {
2515 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2516 		    "request to disable IPSEC Hardware Acceleration\n"));
2517 		return;
2518 	}
2519 
2520 	/*
2521 	 * Clear the capability flags for IPsec HA but retain the ill
2522 	 * capability structures since it's possible that another thread
2523 	 * is still referring to them.  The structures only get deallocated
2524 	 * when we destroy the ill.
2525 	 *
2526 	 * Various places check the flags to see if the ill is capable of
2527 	 * hardware acceleration, and by clearing them we ensure that new
2528 	 * outbound IPsec packets are sent down encrypted.
2529 	 */
2530 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2531 
2532 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2533 	if (ah_cnt > 0) {
2534 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2535 		    cap_ah, mp);
2536 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2537 	}
2538 
2539 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2540 	if (esp_cnt > 0) {
2541 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2542 		    cap_esp, mp);
2543 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2544 	}
2545 
2546 	/*
2547 	 * At this point we've composed a bunch of sub-capabilities to be
2548 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2549 	 * by the caller.  Upon receiving this reset message, the driver
2550 	 * must stop inbound decryption (by destroying all inbound SAs)
2551 	 * and let the corresponding packets come in encrypted.
2552 	 */
2553 
2554 	if (*sc_mp != NULL)
2555 		linkb(*sc_mp, mp);
2556 	else
2557 		*sc_mp = mp;
2558 }
2559 
2560 static void
2561 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2562     boolean_t encapsulated)
2563 {
2564 	boolean_t legacy = B_FALSE;
2565 
2566 	/*
2567 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2568 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2569 	 * instructed the driver to disable its advertised capabilities,
2570 	 * so there's no point in accepting any response at this moment.
2571 	 */
2572 	if (ill->ill_dlpi_capab_state == IDS_UNKNOWN)
2573 		return;
2574 
2575 	/*
2576 	 * Note that only the following two sub-capabilities may be
2577 	 * considered as "legacy", since their original definitions
2578 	 * do not incorporate the dl_mid_t module ID token, and hence
2579 	 * may require the use of the wrapper sub-capability.
2580 	 */
2581 	switch (subp->dl_cap) {
2582 	case DL_CAPAB_IPSEC_AH:
2583 	case DL_CAPAB_IPSEC_ESP:
2584 		legacy = B_TRUE;
2585 		break;
2586 	}
2587 
2588 	/*
2589 	 * For legacy sub-capabilities which don't incorporate a queue_t
2590 	 * pointer in their structures, discard them if we detect that
2591 	 * there are intermediate modules in between IP and the driver.
2592 	 */
2593 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2594 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2595 		    "%d discarded; %d module(s) present below IP\n",
2596 		    subp->dl_cap, ill->ill_lmod_cnt));
2597 		return;
2598 	}
2599 
2600 	switch (subp->dl_cap) {
2601 	case DL_CAPAB_IPSEC_AH:
2602 	case DL_CAPAB_IPSEC_ESP:
2603 		ill_capability_ipsec_ack(ill, mp, subp);
2604 		break;
2605 	case DL_CAPAB_MDT:
2606 		ill_capability_mdt_ack(ill, mp, subp);
2607 		break;
2608 	case DL_CAPAB_HCKSUM:
2609 		ill_capability_hcksum_ack(ill, mp, subp);
2610 		break;
2611 	case DL_CAPAB_ZEROCOPY:
2612 		ill_capability_zerocopy_ack(ill, mp, subp);
2613 		break;
2614 	case DL_CAPAB_POLL:
2615 		if (!SOFT_RINGS_ENABLED())
2616 			ill_capability_dls_ack(ill, mp, subp);
2617 		break;
2618 	case DL_CAPAB_SOFT_RING:
2619 		if (SOFT_RINGS_ENABLED())
2620 			ill_capability_dls_ack(ill, mp, subp);
2621 		break;
2622 	case DL_CAPAB_LSO:
2623 		ill_capability_lso_ack(ill, mp, subp);
2624 		break;
2625 	default:
2626 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2627 		    subp->dl_cap));
2628 	}
2629 }
2630 
2631 /*
2632  * As part of negotiating polling capability, the driver tells us
2633  * the default (or normal) blanking interval and packet threshold
2634  * (the receive timer fires if blanking interval is reached or
2635  * the packet threshold is reached).
2636  *
2637  * As part of manipulating the polling interval, we always use our
2638  * estimated interval (avg service time * number of packets queued
2639  * on the squeue) but we try to blank for a minimum of
2640  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2641  * packet threshold during this time. When we are not in polling mode
2642  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2643  * rr_min_blank_ratio but up the packet cnt by a ratio of
2644  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2645  * possible although for a shorter interval.
2646  */
2647 #define	RR_MAX_BLANK_RATIO	20
2648 #define	RR_MIN_BLANK_RATIO	10
2649 #define	RR_MAX_PKT_CNT_RATIO	3
2650 #define	RR_MIN_PKT_CNT_RATIO	3
2651 
2652 /*
2653  * These can be tuned via /etc/system.
2654  */
2655 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2656 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2657 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2658 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2659 
2660 static mac_resource_handle_t
2661 ill_ring_add(void *arg, mac_resource_t *mrp)
2662 {
2663 	ill_t			*ill = (ill_t *)arg;
2664 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2665 	ill_rx_ring_t		*rx_ring;
2666 	int			ip_rx_index;
2667 
2668 	ASSERT(mrp != NULL);
2669 	if (mrp->mr_type != MAC_RX_FIFO) {
2670 		return (NULL);
2671 	}
2672 	ASSERT(ill != NULL);
2673 	ASSERT(ill->ill_dls_capab != NULL);
2674 
2675 	mutex_enter(&ill->ill_lock);
2676 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2677 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2678 		ASSERT(rx_ring != NULL);
2679 
2680 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2681 			time_t normal_blank_time =
2682 			    mrfp->mrf_normal_blank_time;
2683 			uint_t normal_pkt_cnt =
2684 			    mrfp->mrf_normal_pkt_count;
2685 
2686 	bzero(rx_ring, sizeof (ill_rx_ring_t));
2687 
2688 	rx_ring->rr_blank = mrfp->mrf_blank;
2689 	rx_ring->rr_handle = mrfp->mrf_arg;
2690 	rx_ring->rr_ill = ill;
2691 	rx_ring->rr_normal_blank_time = normal_blank_time;
2692 	rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2693 
2694 			rx_ring->rr_max_blank_time =
2695 			    normal_blank_time * rr_max_blank_ratio;
2696 			rx_ring->rr_min_blank_time =
2697 			    normal_blank_time * rr_min_blank_ratio;
2698 			rx_ring->rr_max_pkt_cnt =
2699 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2700 			rx_ring->rr_min_pkt_cnt =
2701 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2702 
2703 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2704 			mutex_exit(&ill->ill_lock);
2705 
2706 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2707 			    (int), ip_rx_index);
2708 			return ((mac_resource_handle_t)rx_ring);
2709 		}
2710 	}
2711 
2712 	/*
2713 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2714 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2715 	 * should be made configurable. Meanwhile it cause no panic because
2716 	 * driver will pass ip_input a NULL handle which will make
2717 	 * IP allocate the default squeue and Polling mode will not
2718 	 * be used for this ring.
2719 	 */
2720 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2721 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2722 
2723 	mutex_exit(&ill->ill_lock);
2724 	return (NULL);
2725 }
2726 
2727 static boolean_t
2728 ill_capability_dls_init(ill_t *ill)
2729 {
2730 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2731 	conn_t 			*connp;
2732 	size_t			sz;
2733 	ip_stack_t *ipst = ill->ill_ipst;
2734 
2735 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2736 		if (ill_dls == NULL) {
2737 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2738 			    "soft_ring enabled for ill=%s (%p) but data "
2739 			    "structs uninitialized\n", ill->ill_name,
2740 			    (void *)ill);
2741 		}
2742 		return (B_TRUE);
2743 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2744 		if (ill_dls == NULL) {
2745 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2746 			    "polling enabled for ill=%s (%p) but data "
2747 			    "structs uninitialized\n", ill->ill_name,
2748 			    (void *)ill);
2749 		}
2750 		return (B_TRUE);
2751 	}
2752 
2753 	if (ill_dls != NULL) {
2754 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2755 		/* Soft_Ring or polling is being re-enabled */
2756 
2757 		connp = ill_dls->ill_unbind_conn;
2758 		ASSERT(rx_ring != NULL);
2759 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2760 		bzero((void *)rx_ring,
2761 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2762 		ill_dls->ill_ring_tbl = rx_ring;
2763 		ill_dls->ill_unbind_conn = connp;
2764 		return (B_TRUE);
2765 	}
2766 
2767 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
2768 	    ipst->ips_netstack)) == NULL)
2769 		return (B_FALSE);
2770 
2771 	sz = sizeof (ill_dls_capab_t);
2772 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2773 
2774 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2775 	if (ill_dls == NULL) {
2776 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2777 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2778 		    (void *)ill);
2779 		CONN_DEC_REF(connp);
2780 		return (B_FALSE);
2781 	}
2782 
2783 	/* Allocate space to hold ring table */
2784 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2785 	ill->ill_dls_capab = ill_dls;
2786 	ill_dls->ill_unbind_conn = connp;
2787 	return (B_TRUE);
2788 }
2789 
2790 /*
2791  * ill_capability_dls_disable: disable soft_ring and/or polling
2792  * capability. Since any of the rings might already be in use, need
2793  * to call ip_squeue_clean_all() which gets behind the squeue to disable
2794  * direct calls if necessary.
2795  */
2796 static void
2797 ill_capability_dls_disable(ill_t *ill)
2798 {
2799 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2800 
2801 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2802 		ip_squeue_clean_all(ill);
2803 		ill_dls->ill_tx = NULL;
2804 		ill_dls->ill_tx_handle = NULL;
2805 		ill_dls->ill_dls_change_status = NULL;
2806 		ill_dls->ill_dls_bind = NULL;
2807 		ill_dls->ill_dls_unbind = NULL;
2808 	}
2809 
2810 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2811 }
2812 
2813 static void
2814 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2815     dl_capability_sub_t *isub)
2816 {
2817 	uint_t			size;
2818 	uchar_t			*rptr;
2819 	dl_capab_dls_t	dls, *odls;
2820 	ill_dls_capab_t	*ill_dls;
2821 	mblk_t			*nmp = NULL;
2822 	dl_capability_req_t	*ocap;
2823 	uint_t			sub_dl_cap = isub->dl_cap;
2824 
2825 	if (!ill_capability_dls_init(ill))
2826 		return;
2827 	ill_dls = ill->ill_dls_capab;
2828 
2829 	/* Copy locally to get the members aligned */
2830 	bcopy((void *)idls, (void *)&dls,
2831 	    sizeof (dl_capab_dls_t));
2832 
2833 	/* Get the tx function and handle from dld */
2834 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2835 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2836 
2837 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2838 		ill_dls->ill_dls_change_status =
2839 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2840 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2841 		ill_dls->ill_dls_unbind =
2842 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2843 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2844 	}
2845 
2846 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2847 	    isub->dl_length;
2848 
2849 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2850 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2851 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2852 		    ill->ill_name, (void *)ill);
2853 		return;
2854 	}
2855 
2856 	/* initialize dl_capability_req_t */
2857 	rptr = nmp->b_rptr;
2858 	ocap = (dl_capability_req_t *)rptr;
2859 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2860 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2861 	rptr += sizeof (dl_capability_req_t);
2862 
2863 	/* initialize dl_capability_sub_t */
2864 	bcopy(isub, rptr, sizeof (*isub));
2865 	rptr += sizeof (*isub);
2866 
2867 	odls = (dl_capab_dls_t *)rptr;
2868 	rptr += sizeof (dl_capab_dls_t);
2869 
2870 	/* initialize dl_capab_dls_t to be sent down */
2871 	dls.dls_rx_handle = (uintptr_t)ill;
2872 	dls.dls_rx = (uintptr_t)ip_input;
2873 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2874 
2875 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2876 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2877 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2878 		dls.dls_flags = SOFT_RING_ENABLE;
2879 	} else {
2880 		dls.dls_flags = POLL_ENABLE;
2881 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2882 		    "to enable polling\n", ill->ill_name));
2883 	}
2884 	bcopy((void *)&dls, (void *)odls,
2885 	    sizeof (dl_capab_dls_t));
2886 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2887 	/*
2888 	 * nmp points to a DL_CAPABILITY_REQ message to
2889 	 * enable either soft_ring or polling
2890 	 */
2891 	ill_dlpi_send(ill, nmp);
2892 }
2893 
2894 static void
2895 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2896 {
2897 	mblk_t *mp;
2898 	dl_capab_dls_t *idls;
2899 	dl_capability_sub_t *dl_subcap;
2900 	int size;
2901 
2902 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2903 		return;
2904 
2905 	ASSERT(ill->ill_dls_capab != NULL);
2906 
2907 	size = sizeof (*dl_subcap) + sizeof (*idls);
2908 
2909 	mp = allocb(size, BPRI_HI);
2910 	if (mp == NULL) {
2911 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2912 		    "request to disable soft_ring\n"));
2913 		return;
2914 	}
2915 
2916 	mp->b_wptr = mp->b_rptr + size;
2917 
2918 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2919 	dl_subcap->dl_length = sizeof (*idls);
2920 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2921 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2922 	else
2923 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2924 
2925 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2926 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2927 		idls->dls_flags = SOFT_RING_DISABLE;
2928 	else
2929 		idls->dls_flags = POLL_DISABLE;
2930 
2931 	if (*sc_mp != NULL)
2932 		linkb(*sc_mp, mp);
2933 	else
2934 		*sc_mp = mp;
2935 }
2936 
2937 /*
2938  * Process a soft_ring/poll capability negotiation ack received
2939  * from a DLS Provider.isub must point to the sub-capability
2940  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2941  */
2942 static void
2943 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2944 {
2945 	dl_capab_dls_t		*idls;
2946 	uint_t			sub_dl_cap = isub->dl_cap;
2947 	uint8_t			*capend;
2948 
2949 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2950 	    sub_dl_cap == DL_CAPAB_POLL);
2951 
2952 	if (ill->ill_isv6)
2953 		return;
2954 
2955 	/*
2956 	 * Note: range checks here are not absolutely sufficient to
2957 	 * make us robust against malformed messages sent by drivers;
2958 	 * this is in keeping with the rest of IP's dlpi handling.
2959 	 * (Remember, it's coming from something else in the kernel
2960 	 * address space)
2961 	 */
2962 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2963 	if (capend > mp->b_wptr) {
2964 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2965 		    "malformed sub-capability too long for mblk");
2966 		return;
2967 	}
2968 
2969 	/*
2970 	 * There are two types of acks we process here:
2971 	 * 1. acks in reply to a (first form) generic capability req
2972 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2973 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2974 	 *    capability req.
2975 	 */
2976 	idls = (dl_capab_dls_t *)(isub + 1);
2977 
2978 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2979 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2980 		    "capability isn't as expected; pass-thru "
2981 		    "module(s) detected, discarding capability\n"));
2982 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2983 			/*
2984 			 * This is a capability renegotitation case.
2985 			 * The interface better be unusable at this
2986 			 * point other wise bad things will happen
2987 			 * if we disable direct calls on a running
2988 			 * and up interface.
2989 			 */
2990 			ill_capability_dls_disable(ill);
2991 		}
2992 		return;
2993 	}
2994 
2995 	switch (idls->dls_flags) {
2996 	default:
2997 		/* Disable if unknown flag */
2998 	case SOFT_RING_DISABLE:
2999 	case POLL_DISABLE:
3000 		ill_capability_dls_disable(ill);
3001 		break;
3002 	case SOFT_RING_CAPABLE:
3003 	case POLL_CAPABLE:
3004 		/*
3005 		 * If the capability was already enabled, its safe
3006 		 * to disable it first to get rid of stale information
3007 		 * and then start enabling it again.
3008 		 */
3009 		ill_capability_dls_disable(ill);
3010 		ill_capability_dls_capable(ill, idls, isub);
3011 		break;
3012 	case SOFT_RING_ENABLE:
3013 	case POLL_ENABLE:
3014 		mutex_enter(&ill->ill_lock);
3015 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3016 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3017 			ASSERT(ill->ill_dls_capab != NULL);
3018 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3019 		}
3020 		if (sub_dl_cap == DL_CAPAB_POLL &&
3021 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3022 			ASSERT(ill->ill_dls_capab != NULL);
3023 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3024 			ip1dbg(("ill_capability_dls_ack: interface %s "
3025 			    "has enabled polling\n", ill->ill_name));
3026 		}
3027 		mutex_exit(&ill->ill_lock);
3028 		break;
3029 	}
3030 }
3031 
3032 /*
3033  * Process a hardware checksum offload capability negotiation ack received
3034  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3035  * of a DL_CAPABILITY_ACK message.
3036  */
3037 static void
3038 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3039 {
3040 	dl_capability_req_t	*ocap;
3041 	dl_capab_hcksum_t	*ihck, *ohck;
3042 	ill_hcksum_capab_t	**ill_hcksum;
3043 	mblk_t			*nmp = NULL;
3044 	uint_t			sub_dl_cap = isub->dl_cap;
3045 	uint8_t			*capend;
3046 
3047 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3048 
3049 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3050 
3051 	/*
3052 	 * Note: range checks here are not absolutely sufficient to
3053 	 * make us robust against malformed messages sent by drivers;
3054 	 * this is in keeping with the rest of IP's dlpi handling.
3055 	 * (Remember, it's coming from something else in the kernel
3056 	 * address space)
3057 	 */
3058 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3059 	if (capend > mp->b_wptr) {
3060 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3061 		    "malformed sub-capability too long for mblk");
3062 		return;
3063 	}
3064 
3065 	/*
3066 	 * There are two types of acks we process here:
3067 	 * 1. acks in reply to a (first form) generic capability req
3068 	 *    (no ENABLE flag set)
3069 	 * 2. acks in reply to a ENABLE capability req.
3070 	 *    (ENABLE flag set)
3071 	 */
3072 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3073 
3074 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3075 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3076 		    "unsupported hardware checksum "
3077 		    "sub-capability (version %d, expected %d)",
3078 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3079 		return;
3080 	}
3081 
3082 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3083 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3084 		    "checksum capability isn't as expected; pass-thru "
3085 		    "module(s) detected, discarding capability\n"));
3086 		return;
3087 	}
3088 
3089 #define	CURR_HCKSUM_CAPAB				\
3090 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3091 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3092 
3093 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3094 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3095 		/* do ENABLE processing */
3096 		if (*ill_hcksum == NULL) {
3097 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3098 			    KM_NOSLEEP);
3099 
3100 			if (*ill_hcksum == NULL) {
3101 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3102 				    "could not enable hcksum version %d "
3103 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3104 				    ill->ill_name);
3105 				return;
3106 			}
3107 		}
3108 
3109 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3110 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3111 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3112 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3113 		    "has enabled hardware checksumming\n ",
3114 		    ill->ill_name));
3115 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3116 		/*
3117 		 * Enabling hardware checksum offload
3118 		 * Currently IP supports {TCP,UDP}/IPv4
3119 		 * partial and full cksum offload and
3120 		 * IPv4 header checksum offload.
3121 		 * Allocate new mblk which will
3122 		 * contain a new capability request
3123 		 * to enable hardware checksum offload.
3124 		 */
3125 		uint_t	size;
3126 		uchar_t	*rptr;
3127 
3128 		size = sizeof (dl_capability_req_t) +
3129 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3130 
3131 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3132 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3133 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3134 			    ill->ill_name);
3135 			return;
3136 		}
3137 
3138 		rptr = nmp->b_rptr;
3139 		/* initialize dl_capability_req_t */
3140 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3141 		ocap->dl_sub_offset =
3142 		    sizeof (dl_capability_req_t);
3143 		ocap->dl_sub_length =
3144 		    sizeof (dl_capability_sub_t) +
3145 		    isub->dl_length;
3146 		nmp->b_rptr += sizeof (dl_capability_req_t);
3147 
3148 		/* initialize dl_capability_sub_t */
3149 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3150 		nmp->b_rptr += sizeof (*isub);
3151 
3152 		/* initialize dl_capab_hcksum_t */
3153 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3154 		bcopy(ihck, ohck, sizeof (*ihck));
3155 
3156 		nmp->b_rptr = rptr;
3157 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3158 
3159 		/* Set ENABLE flag */
3160 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3161 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3162 
3163 		/*
3164 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3165 		 * hardware checksum acceleration.
3166 		 */
3167 		ill_dlpi_send(ill, nmp);
3168 	} else {
3169 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3170 		    "advertised %x hardware checksum capability flags\n",
3171 		    ill->ill_name, ihck->hcksum_txflags));
3172 	}
3173 }
3174 
3175 static void
3176 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3177 {
3178 	mblk_t *mp;
3179 	dl_capab_hcksum_t *hck_subcap;
3180 	dl_capability_sub_t *dl_subcap;
3181 	int size;
3182 
3183 	if (!ILL_HCKSUM_CAPABLE(ill))
3184 		return;
3185 
3186 	ASSERT(ill->ill_hcksum_capab != NULL);
3187 	/*
3188 	 * Clear the capability flag for hardware checksum offload but
3189 	 * retain the ill_hcksum_capab structure since it's possible that
3190 	 * another thread is still referring to it.  The structure only
3191 	 * gets deallocated when we destroy the ill.
3192 	 */
3193 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3194 
3195 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3196 
3197 	mp = allocb(size, BPRI_HI);
3198 	if (mp == NULL) {
3199 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3200 		    "request to disable hardware checksum offload\n"));
3201 		return;
3202 	}
3203 
3204 	mp->b_wptr = mp->b_rptr + size;
3205 
3206 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3207 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3208 	dl_subcap->dl_length = sizeof (*hck_subcap);
3209 
3210 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3211 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3212 	hck_subcap->hcksum_txflags = 0;
3213 
3214 	if (*sc_mp != NULL)
3215 		linkb(*sc_mp, mp);
3216 	else
3217 		*sc_mp = mp;
3218 }
3219 
3220 static void
3221 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3222 {
3223 	mblk_t *nmp = NULL;
3224 	dl_capability_req_t *oc;
3225 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3226 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3227 	uint_t sub_dl_cap = isub->dl_cap;
3228 	uint8_t *capend;
3229 
3230 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3231 
3232 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3233 
3234 	/*
3235 	 * Note: range checks here are not absolutely sufficient to
3236 	 * make us robust against malformed messages sent by drivers;
3237 	 * this is in keeping with the rest of IP's dlpi handling.
3238 	 * (Remember, it's coming from something else in the kernel
3239 	 * address space)
3240 	 */
3241 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3242 	if (capend > mp->b_wptr) {
3243 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3244 		    "malformed sub-capability too long for mblk");
3245 		return;
3246 	}
3247 
3248 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3249 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3250 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3251 		    "unsupported ZEROCOPY sub-capability (version %d, "
3252 		    "expected %d)", zc_ic->zerocopy_version,
3253 		    ZEROCOPY_VERSION_1);
3254 		return;
3255 	}
3256 
3257 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3258 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3259 		    "capability isn't as expected; pass-thru module(s) "
3260 		    "detected, discarding capability\n"));
3261 		return;
3262 	}
3263 
3264 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3265 		if (*ill_zerocopy_capab == NULL) {
3266 			*ill_zerocopy_capab =
3267 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3268 			    KM_NOSLEEP);
3269 
3270 			if (*ill_zerocopy_capab == NULL) {
3271 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3272 				    "could not enable Zero-copy version %d "
3273 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3274 				    ill->ill_name);
3275 				return;
3276 			}
3277 		}
3278 
3279 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3280 		    "supports Zero-copy version %d\n", ill->ill_name,
3281 		    ZEROCOPY_VERSION_1));
3282 
3283 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3284 		    zc_ic->zerocopy_version;
3285 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3286 		    zc_ic->zerocopy_flags;
3287 
3288 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3289 	} else {
3290 		uint_t size;
3291 		uchar_t *rptr;
3292 
3293 		size = sizeof (dl_capability_req_t) +
3294 		    sizeof (dl_capability_sub_t) +
3295 		    sizeof (dl_capab_zerocopy_t);
3296 
3297 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3298 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3299 			    "could not enable zerocopy for %s (ENOMEM)\n",
3300 			    ill->ill_name);
3301 			return;
3302 		}
3303 
3304 		rptr = nmp->b_rptr;
3305 		/* initialize dl_capability_req_t */
3306 		oc = (dl_capability_req_t *)rptr;
3307 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3308 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3309 		    sizeof (dl_capab_zerocopy_t);
3310 		rptr += sizeof (dl_capability_req_t);
3311 
3312 		/* initialize dl_capability_sub_t */
3313 		bcopy(isub, rptr, sizeof (*isub));
3314 		rptr += sizeof (*isub);
3315 
3316 		/* initialize dl_capab_zerocopy_t */
3317 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3318 		*zc_oc = *zc_ic;
3319 
3320 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3321 		    "to enable zero-copy version %d\n", ill->ill_name,
3322 		    ZEROCOPY_VERSION_1));
3323 
3324 		/* set VMSAFE_MEM flag */
3325 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3326 
3327 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3328 		ill_dlpi_send(ill, nmp);
3329 	}
3330 }
3331 
3332 static void
3333 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3334 {
3335 	mblk_t *mp;
3336 	dl_capab_zerocopy_t *zerocopy_subcap;
3337 	dl_capability_sub_t *dl_subcap;
3338 	int size;
3339 
3340 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3341 		return;
3342 
3343 	ASSERT(ill->ill_zerocopy_capab != NULL);
3344 	/*
3345 	 * Clear the capability flag for Zero-copy but retain the
3346 	 * ill_zerocopy_capab structure since it's possible that another
3347 	 * thread is still referring to it.  The structure only gets
3348 	 * deallocated when we destroy the ill.
3349 	 */
3350 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3351 
3352 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3353 
3354 	mp = allocb(size, BPRI_HI);
3355 	if (mp == NULL) {
3356 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3357 		    "request to disable Zero-copy\n"));
3358 		return;
3359 	}
3360 
3361 	mp->b_wptr = mp->b_rptr + size;
3362 
3363 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3364 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3365 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3366 
3367 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3368 	zerocopy_subcap->zerocopy_version =
3369 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3370 	zerocopy_subcap->zerocopy_flags = 0;
3371 
3372 	if (*sc_mp != NULL)
3373 		linkb(*sc_mp, mp);
3374 	else
3375 		*sc_mp = mp;
3376 }
3377 
3378 /*
3379  * Process Large Segment Offload capability negotiation ack received from a
3380  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_LSO) of a
3381  * DL_CAPABILITY_ACK message.
3382  */
3383 static void
3384 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3385 {
3386 	mblk_t *nmp = NULL;
3387 	dl_capability_req_t *oc;
3388 	dl_capab_lso_t *lso_ic, *lso_oc;
3389 	ill_lso_capab_t **ill_lso_capab;
3390 	uint_t sub_dl_cap = isub->dl_cap;
3391 	uint8_t *capend;
3392 
3393 	ASSERT(sub_dl_cap == DL_CAPAB_LSO);
3394 
3395 	ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab;
3396 
3397 	/*
3398 	 * Note: range checks here are not absolutely sufficient to
3399 	 * make us robust against malformed messages sent by drivers;
3400 	 * this is in keeping with the rest of IP's dlpi handling.
3401 	 * (Remember, it's coming from something else in the kernel
3402 	 * address space)
3403 	 */
3404 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3405 	if (capend > mp->b_wptr) {
3406 		cmn_err(CE_WARN, "ill_capability_lso_ack: "
3407 		    "malformed sub-capability too long for mblk");
3408 		return;
3409 	}
3410 
3411 	lso_ic = (dl_capab_lso_t *)(isub + 1);
3412 
3413 	if (lso_ic->lso_version != LSO_VERSION_1) {
3414 		cmn_err(CE_CONT, "ill_capability_lso_ack: "
3415 		    "unsupported LSO sub-capability (version %d, expected %d)",
3416 		    lso_ic->lso_version, LSO_VERSION_1);
3417 		return;
3418 	}
3419 
3420 	if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) {
3421 		ip1dbg(("ill_capability_lso_ack: mid token for LSO "
3422 		    "capability isn't as expected; pass-thru module(s) "
3423 		    "detected, discarding capability\n"));
3424 		return;
3425 	}
3426 
3427 	if ((lso_ic->lso_flags & LSO_TX_ENABLE) &&
3428 	    (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) {
3429 		if (*ill_lso_capab == NULL) {
3430 			*ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
3431 			    KM_NOSLEEP);
3432 
3433 			if (*ill_lso_capab == NULL) {
3434 				cmn_err(CE_WARN, "ill_capability_lso_ack: "
3435 				    "could not enable LSO version %d "
3436 				    "for %s (ENOMEM)\n", LSO_VERSION_1,
3437 				    ill->ill_name);
3438 				return;
3439 			}
3440 		}
3441 
3442 		(*ill_lso_capab)->ill_lso_version = lso_ic->lso_version;
3443 		(*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags;
3444 		(*ill_lso_capab)->ill_lso_max = lso_ic->lso_max;
3445 		ill->ill_capabilities |= ILL_CAPAB_LSO;
3446 
3447 		ip1dbg(("ill_capability_lso_ack: interface %s "
3448 		    "has enabled LSO\n ", ill->ill_name));
3449 	} else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) {
3450 		uint_t size;
3451 		uchar_t *rptr;
3452 
3453 		size = sizeof (dl_capability_req_t) +
3454 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t);
3455 
3456 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3457 			cmn_err(CE_WARN, "ill_capability_lso_ack: "
3458 			    "could not enable LSO for %s (ENOMEM)\n",
3459 			    ill->ill_name);
3460 			return;
3461 		}
3462 
3463 		rptr = nmp->b_rptr;
3464 		/* initialize dl_capability_req_t */
3465 		oc = (dl_capability_req_t *)nmp->b_rptr;
3466 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3467 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3468 		    sizeof (dl_capab_lso_t);
3469 		nmp->b_rptr += sizeof (dl_capability_req_t);
3470 
3471 		/* initialize dl_capability_sub_t */
3472 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3473 		nmp->b_rptr += sizeof (*isub);
3474 
3475 		/* initialize dl_capab_lso_t */
3476 		lso_oc = (dl_capab_lso_t *)nmp->b_rptr;
3477 		bcopy(lso_ic, lso_oc, sizeof (*lso_ic));
3478 
3479 		nmp->b_rptr = rptr;
3480 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3481 
3482 		/* set ENABLE flag */
3483 		lso_oc->lso_flags |= LSO_TX_ENABLE;
3484 
3485 		/* nmp points to a DL_CAPABILITY_REQ message to enable LSO */
3486 		ill_dlpi_send(ill, nmp);
3487 	} else {
3488 		ip1dbg(("ill_capability_lso_ack: interface %s has "
3489 		    "advertised %x LSO capability flags\n",
3490 		    ill->ill_name, lso_ic->lso_flags));
3491 	}
3492 }
3493 
3494 
3495 static void
3496 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp)
3497 {
3498 	mblk_t *mp;
3499 	dl_capab_lso_t *lso_subcap;
3500 	dl_capability_sub_t *dl_subcap;
3501 	int size;
3502 
3503 	if (!(ill->ill_capabilities & ILL_CAPAB_LSO))
3504 		return;
3505 
3506 	ASSERT(ill->ill_lso_capab != NULL);
3507 	/*
3508 	 * Clear the capability flag for LSO but retain the
3509 	 * ill_lso_capab structure since it's possible that another
3510 	 * thread is still referring to it.  The structure only gets
3511 	 * deallocated when we destroy the ill.
3512 	 */
3513 	ill->ill_capabilities &= ~ILL_CAPAB_LSO;
3514 
3515 	size = sizeof (*dl_subcap) + sizeof (*lso_subcap);
3516 
3517 	mp = allocb(size, BPRI_HI);
3518 	if (mp == NULL) {
3519 		ip1dbg(("ill_capability_lso_reset: unable to allocate "
3520 		    "request to disable LSO\n"));
3521 		return;
3522 	}
3523 
3524 	mp->b_wptr = mp->b_rptr + size;
3525 
3526 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3527 	dl_subcap->dl_cap = DL_CAPAB_LSO;
3528 	dl_subcap->dl_length = sizeof (*lso_subcap);
3529 
3530 	lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1);
3531 	lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version;
3532 	lso_subcap->lso_flags = 0;
3533 
3534 	if (*sc_mp != NULL)
3535 		linkb(*sc_mp, mp);
3536 	else
3537 		*sc_mp = mp;
3538 }
3539 
3540 /*
3541  * Consume a new-style hardware capabilities negotiation ack.
3542  * Called from ip_rput_dlpi_writer().
3543  */
3544 void
3545 ill_capability_ack(ill_t *ill, mblk_t *mp)
3546 {
3547 	dl_capability_ack_t *capp;
3548 	dl_capability_sub_t *subp, *endp;
3549 
3550 	if (ill->ill_dlpi_capab_state == IDS_INPROGRESS)
3551 		ill->ill_dlpi_capab_state = IDS_OK;
3552 
3553 	capp = (dl_capability_ack_t *)mp->b_rptr;
3554 
3555 	if (capp->dl_sub_length == 0)
3556 		/* no new-style capabilities */
3557 		return;
3558 
3559 	/* make sure the driver supplied correct dl_sub_length */
3560 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3561 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3562 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3563 		return;
3564 	}
3565 
3566 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3567 	/*
3568 	 * There are sub-capabilities. Process the ones we know about.
3569 	 * Loop until we don't have room for another sub-cap header..
3570 	 */
3571 	for (subp = SC(capp, capp->dl_sub_offset),
3572 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3573 	    subp <= endp;
3574 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3575 
3576 		switch (subp->dl_cap) {
3577 		case DL_CAPAB_ID_WRAPPER:
3578 			ill_capability_id_ack(ill, mp, subp);
3579 			break;
3580 		default:
3581 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3582 			break;
3583 		}
3584 	}
3585 #undef SC
3586 }
3587 
3588 /*
3589  * This routine is called to scan the fragmentation reassembly table for
3590  * the specified ILL for any packets that are starting to smell.
3591  * dead_interval is the maximum time in seconds that will be tolerated.  It
3592  * will either be the value specified in ip_g_frag_timeout, or zero if the
3593  * ILL is shutting down and it is time to blow everything off.
3594  *
3595  * It returns the number of seconds (as a time_t) that the next frag timer
3596  * should be scheduled for, 0 meaning that the timer doesn't need to be
3597  * re-started.  Note that the method of calculating next_timeout isn't
3598  * entirely accurate since time will flow between the time we grab
3599  * current_time and the time we schedule the next timeout.  This isn't a
3600  * big problem since this is the timer for sending an ICMP reassembly time
3601  * exceeded messages, and it doesn't have to be exactly accurate.
3602  *
3603  * This function is
3604  * sometimes called as writer, although this is not required.
3605  */
3606 time_t
3607 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3608 {
3609 	ipfb_t	*ipfb;
3610 	ipfb_t	*endp;
3611 	ipf_t	*ipf;
3612 	ipf_t	*ipfnext;
3613 	mblk_t	*mp;
3614 	time_t	current_time = gethrestime_sec();
3615 	time_t	next_timeout = 0;
3616 	uint32_t	hdr_length;
3617 	mblk_t	*send_icmp_head;
3618 	mblk_t	*send_icmp_head_v6;
3619 	zoneid_t zoneid;
3620 	ip_stack_t *ipst = ill->ill_ipst;
3621 
3622 	ipfb = ill->ill_frag_hash_tbl;
3623 	if (ipfb == NULL)
3624 		return (B_FALSE);
3625 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3626 	/* Walk the frag hash table. */
3627 	for (; ipfb < endp; ipfb++) {
3628 		send_icmp_head = NULL;
3629 		send_icmp_head_v6 = NULL;
3630 		mutex_enter(&ipfb->ipfb_lock);
3631 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3632 			time_t frag_time = current_time - ipf->ipf_timestamp;
3633 			time_t frag_timeout;
3634 
3635 			if (frag_time < dead_interval) {
3636 				/*
3637 				 * There are some outstanding fragments
3638 				 * that will timeout later.  Make note of
3639 				 * the time so that we can reschedule the
3640 				 * next timeout appropriately.
3641 				 */
3642 				frag_timeout = dead_interval - frag_time;
3643 				if (next_timeout == 0 ||
3644 				    frag_timeout < next_timeout) {
3645 					next_timeout = frag_timeout;
3646 				}
3647 				break;
3648 			}
3649 			/* Time's up.  Get it out of here. */
3650 			hdr_length = ipf->ipf_nf_hdr_len;
3651 			ipfnext = ipf->ipf_hash_next;
3652 			if (ipfnext)
3653 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3654 			*ipf->ipf_ptphn = ipfnext;
3655 			mp = ipf->ipf_mp->b_cont;
3656 			for (; mp; mp = mp->b_cont) {
3657 				/* Extra points for neatness. */
3658 				IP_REASS_SET_START(mp, 0);
3659 				IP_REASS_SET_END(mp, 0);
3660 			}
3661 			mp = ipf->ipf_mp->b_cont;
3662 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
3663 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3664 			ipfb->ipfb_count -= ipf->ipf_count;
3665 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3666 			ipfb->ipfb_frag_pkts--;
3667 			/*
3668 			 * We do not send any icmp message from here because
3669 			 * we currently are holding the ipfb_lock for this
3670 			 * hash chain. If we try and send any icmp messages
3671 			 * from here we may end up via a put back into ip
3672 			 * trying to get the same lock, causing a recursive
3673 			 * mutex panic. Instead we build a list and send all
3674 			 * the icmp messages after we have dropped the lock.
3675 			 */
3676 			if (ill->ill_isv6) {
3677 				if (hdr_length != 0) {
3678 					mp->b_next = send_icmp_head_v6;
3679 					send_icmp_head_v6 = mp;
3680 				} else {
3681 					freemsg(mp);
3682 				}
3683 			} else {
3684 				if (hdr_length != 0) {
3685 					mp->b_next = send_icmp_head;
3686 					send_icmp_head = mp;
3687 				} else {
3688 					freemsg(mp);
3689 				}
3690 			}
3691 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
3692 			freeb(ipf->ipf_mp);
3693 		}
3694 		mutex_exit(&ipfb->ipfb_lock);
3695 		/*
3696 		 * Now need to send any icmp messages that we delayed from
3697 		 * above.
3698 		 */
3699 		while (send_icmp_head_v6 != NULL) {
3700 			ip6_t *ip6h;
3701 
3702 			mp = send_icmp_head_v6;
3703 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3704 			mp->b_next = NULL;
3705 			if (mp->b_datap->db_type == M_CTL)
3706 				ip6h = (ip6_t *)mp->b_cont->b_rptr;
3707 			else
3708 				ip6h = (ip6_t *)mp->b_rptr;
3709 			zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
3710 			    ill, ipst);
3711 			if (zoneid == ALL_ZONES) {
3712 				freemsg(mp);
3713 			} else {
3714 				icmp_time_exceeded_v6(ill->ill_wq, mp,
3715 				    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
3716 				    B_FALSE, zoneid, ipst);
3717 			}
3718 		}
3719 		while (send_icmp_head != NULL) {
3720 			ipaddr_t dst;
3721 
3722 			mp = send_icmp_head;
3723 			send_icmp_head = send_icmp_head->b_next;
3724 			mp->b_next = NULL;
3725 
3726 			if (mp->b_datap->db_type == M_CTL)
3727 				dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst;
3728 			else
3729 				dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
3730 
3731 			zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst);
3732 			if (zoneid == ALL_ZONES) {
3733 				freemsg(mp);
3734 			} else {
3735 				icmp_time_exceeded(ill->ill_wq, mp,
3736 				    ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid,
3737 				    ipst);
3738 			}
3739 		}
3740 	}
3741 	/*
3742 	 * A non-dying ILL will use the return value to decide whether to
3743 	 * restart the frag timer, and for how long.
3744 	 */
3745 	return (next_timeout);
3746 }
3747 
3748 /*
3749  * This routine is called when the approximate count of mblk memory used
3750  * for the specified ILL has exceeded max_count.
3751  */
3752 void
3753 ill_frag_prune(ill_t *ill, uint_t max_count)
3754 {
3755 	ipfb_t	*ipfb;
3756 	ipf_t	*ipf;
3757 	size_t	count;
3758 
3759 	/*
3760 	 * If we are here within ip_min_frag_prune_time msecs remove
3761 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3762 	 * ill_frag_free_num_pkts.
3763 	 */
3764 	mutex_enter(&ill->ill_lock);
3765 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3766 	    (ip_min_frag_prune_time != 0 ?
3767 	    ip_min_frag_prune_time : msec_per_tick)) {
3768 
3769 		ill->ill_frag_free_num_pkts++;
3770 
3771 	} else {
3772 		ill->ill_frag_free_num_pkts = 0;
3773 	}
3774 	ill->ill_last_frag_clean_time = lbolt;
3775 	mutex_exit(&ill->ill_lock);
3776 
3777 	/*
3778 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3779 	 */
3780 	if (ill->ill_frag_free_num_pkts != 0) {
3781 		int ix;
3782 
3783 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3784 			ipfb = &ill->ill_frag_hash_tbl[ix];
3785 			mutex_enter(&ipfb->ipfb_lock);
3786 			if (ipfb->ipfb_ipf != NULL) {
3787 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3788 				    ill->ill_frag_free_num_pkts);
3789 			}
3790 			mutex_exit(&ipfb->ipfb_lock);
3791 		}
3792 	}
3793 	/*
3794 	 * While the reassembly list for this ILL is too big, prune a fragment
3795 	 * queue by age, oldest first.
3796 	 */
3797 	while (ill->ill_frag_count > max_count) {
3798 		int	ix;
3799 		ipfb_t	*oipfb = NULL;
3800 		uint_t	oldest = UINT_MAX;
3801 
3802 		count = 0;
3803 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3804 			ipfb = &ill->ill_frag_hash_tbl[ix];
3805 			mutex_enter(&ipfb->ipfb_lock);
3806 			ipf = ipfb->ipfb_ipf;
3807 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3808 				oldest = ipf->ipf_gen;
3809 				oipfb = ipfb;
3810 			}
3811 			count += ipfb->ipfb_count;
3812 			mutex_exit(&ipfb->ipfb_lock);
3813 		}
3814 		if (oipfb == NULL)
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 		atomic_add_32(&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 			if (error != NULL)
4259 				*error = EINPROGRESS;
4260 			return (NULL);
4261 		}
4262 		mutex_exit(&ill->ill_lock);
4263 		RELEASE_CONN_LOCK(q);
4264 	}
4265 	if (error != NULL)
4266 		*error = ENXIO;
4267 	return (NULL);
4268 }
4269 
4270 /*
4271  * comparison function for use with avl.
4272  */
4273 static int
4274 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4275 {
4276 	uint_t ppa;
4277 	uint_t ill_ppa;
4278 
4279 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4280 
4281 	ppa = *((uint_t *)ppa_ptr);
4282 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4283 	/*
4284 	 * We want the ill with the lowest ppa to be on the
4285 	 * top.
4286 	 */
4287 	if (ill_ppa < ppa)
4288 		return (1);
4289 	if (ill_ppa > ppa)
4290 		return (-1);
4291 	return (0);
4292 }
4293 
4294 /*
4295  * remove an interface type from the global list.
4296  */
4297 static void
4298 ill_delete_interface_type(ill_if_t *interface)
4299 {
4300 	ASSERT(interface != NULL);
4301 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4302 
4303 	avl_destroy(&interface->illif_avl_by_ppa);
4304 	if (interface->illif_ppa_arena != NULL)
4305 		vmem_destroy(interface->illif_ppa_arena);
4306 
4307 	remque(interface);
4308 
4309 	mi_free(interface);
4310 }
4311 
4312 /*
4313  * remove ill from the global list.
4314  */
4315 static void
4316 ill_glist_delete(ill_t *ill)
4317 {
4318 	hook_nic_event_t *info;
4319 	ip_stack_t	*ipst;
4320 
4321 	if (ill == NULL)
4322 		return;
4323 	ipst = ill->ill_ipst;
4324 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4325 
4326 	/*
4327 	 * If the ill was never inserted into the AVL tree
4328 	 * we skip the if branch.
4329 	 */
4330 	if (ill->ill_ifptr != NULL) {
4331 		/*
4332 		 * remove from AVL tree and free ppa number
4333 		 */
4334 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4335 
4336 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4337 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4338 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4339 		}
4340 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4341 			ill_delete_interface_type(ill->ill_ifptr);
4342 		}
4343 
4344 		/*
4345 		 * Indicate ill is no longer in the list.
4346 		 */
4347 		ill->ill_ifptr = NULL;
4348 		ill->ill_name_length = 0;
4349 		ill->ill_name[0] = '\0';
4350 		ill->ill_ppa = UINT_MAX;
4351 	}
4352 
4353 	/*
4354 	 * Run the unplumb hook after the NIC has disappeared from being
4355 	 * visible so that attempts to revalidate its existance will fail.
4356 	 *
4357 	 * This needs to be run inside the ill_g_lock perimeter to ensure
4358 	 * that the ordering of delivered events to listeners matches the
4359 	 * order of them in the kernel.
4360 	 */
4361 	info = ill->ill_nic_event_info;
4362 	if (info != NULL && info->hne_event == NE_DOWN) {
4363 		mutex_enter(&ill->ill_lock);
4364 		ill_nic_info_dispatch(ill);
4365 		mutex_exit(&ill->ill_lock);
4366 	}
4367 
4368 	/* Generate NE_UNPLUMB event for ill_name. */
4369 	(void) ill_hook_event_create(ill, 0, NE_UNPLUMB, ill->ill_name,
4370 	    ill->ill_name_length);
4371 
4372 	ill_phyint_free(ill);
4373 	rw_exit(&ipst->ips_ill_g_lock);
4374 }
4375 
4376 /*
4377  * allocate a ppa, if the number of plumbed interfaces of this type are
4378  * less than ill_no_arena do a linear search to find a unused ppa.
4379  * When the number goes beyond ill_no_arena switch to using an arena.
4380  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4381  * is the return value for an error condition, so allocation starts at one
4382  * and is decremented by one.
4383  */
4384 static int
4385 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4386 {
4387 	ill_t *tmp_ill;
4388 	uint_t start, end;
4389 	int ppa;
4390 
4391 	if (ifp->illif_ppa_arena == NULL &&
4392 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4393 		/*
4394 		 * Create an arena.
4395 		 */
4396 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4397 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4398 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4399 			/* allocate what has already been assigned */
4400 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4401 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4402 		    tmp_ill, AVL_AFTER)) {
4403 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4404 			    1,		/* size */
4405 			    1,		/* align/quantum */
4406 			    0,		/* phase */
4407 			    0,		/* nocross */
4408 			    /* minaddr */
4409 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
4410 			    /* maxaddr */
4411 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
4412 			    VM_NOSLEEP|VM_FIRSTFIT);
4413 			if (ppa == 0) {
4414 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4415 				    " failed while switching"));
4416 				vmem_destroy(ifp->illif_ppa_arena);
4417 				ifp->illif_ppa_arena = NULL;
4418 				break;
4419 			}
4420 		}
4421 	}
4422 
4423 	if (ifp->illif_ppa_arena != NULL) {
4424 		if (ill->ill_ppa == UINT_MAX) {
4425 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4426 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4427 			if (ppa == 0)
4428 				return (EAGAIN);
4429 			ill->ill_ppa = --ppa;
4430 		} else {
4431 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4432 			    1, 		/* size */
4433 			    1, 		/* align/quantum */
4434 			    0, 		/* phase */
4435 			    0, 		/* nocross */
4436 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4437 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4438 			    VM_NOSLEEP|VM_FIRSTFIT);
4439 			/*
4440 			 * Most likely the allocation failed because
4441 			 * the requested ppa was in use.
4442 			 */
4443 			if (ppa == 0)
4444 				return (EEXIST);
4445 		}
4446 		return (0);
4447 	}
4448 
4449 	/*
4450 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4451 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4452 	 */
4453 	if (ill->ill_ppa == UINT_MAX) {
4454 		end = UINT_MAX - 1;
4455 		start = 0;
4456 	} else {
4457 		end = start = ill->ill_ppa;
4458 	}
4459 
4460 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4461 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4462 		if (start++ >= end) {
4463 			if (ill->ill_ppa == UINT_MAX)
4464 				return (EAGAIN);
4465 			else
4466 				return (EEXIST);
4467 		}
4468 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4469 	}
4470 	ill->ill_ppa = start;
4471 	return (0);
4472 }
4473 
4474 /*
4475  * Insert ill into the list of configured ill's. Once this function completes,
4476  * the ill is globally visible and is available through lookups. More precisely
4477  * this happens after the caller drops the ill_g_lock.
4478  */
4479 static int
4480 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4481 {
4482 	ill_if_t *ill_interface;
4483 	avl_index_t where = 0;
4484 	int error;
4485 	int name_length;
4486 	int index;
4487 	boolean_t check_length = B_FALSE;
4488 	ip_stack_t	*ipst = ill->ill_ipst;
4489 
4490 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
4491 
4492 	name_length = mi_strlen(name) + 1;
4493 
4494 	if (isv6)
4495 		index = IP_V6_G_HEAD;
4496 	else
4497 		index = IP_V4_G_HEAD;
4498 
4499 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
4500 	/*
4501 	 * Search for interface type based on name
4502 	 */
4503 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4504 		if ((ill_interface->illif_name_len == name_length) &&
4505 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4506 			break;
4507 		}
4508 		ill_interface = ill_interface->illif_next;
4509 	}
4510 
4511 	/*
4512 	 * Interface type not found, create one.
4513 	 */
4514 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
4515 
4516 		ill_g_head_t ghead;
4517 
4518 		/*
4519 		 * allocate ill_if_t structure
4520 		 */
4521 
4522 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4523 		if (ill_interface == NULL) {
4524 			return (ENOMEM);
4525 		}
4526 
4527 
4528 
4529 		(void) strcpy(ill_interface->illif_name, name);
4530 		ill_interface->illif_name_len = name_length;
4531 
4532 		avl_create(&ill_interface->illif_avl_by_ppa,
4533 		    ill_compare_ppa, sizeof (ill_t),
4534 		    offsetof(struct ill_s, ill_avl_byppa));
4535 
4536 		/*
4537 		 * link the structure in the back to maintain order
4538 		 * of configuration for ifconfig output.
4539 		 */
4540 		ghead = ipst->ips_ill_g_heads[index];
4541 		insque(ill_interface, ghead.ill_g_list_tail);
4542 
4543 	}
4544 
4545 	if (ill->ill_ppa == UINT_MAX)
4546 		check_length = B_TRUE;
4547 
4548 	error = ill_alloc_ppa(ill_interface, ill);
4549 	if (error != 0) {
4550 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4551 			ill_delete_interface_type(ill->ill_ifptr);
4552 		return (error);
4553 	}
4554 
4555 	/*
4556 	 * When the ppa is choosen by the system, check that there is
4557 	 * enough space to insert ppa. if a specific ppa was passed in this
4558 	 * check is not required as the interface name passed in will have
4559 	 * the right ppa in it.
4560 	 */
4561 	if (check_length) {
4562 		/*
4563 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4564 		 */
4565 		char buf[sizeof (uint_t) * 3];
4566 
4567 		/*
4568 		 * convert ppa to string to calculate the amount of space
4569 		 * required for it in the name.
4570 		 */
4571 		numtos(ill->ill_ppa, buf);
4572 
4573 		/* Do we have enough space to insert ppa ? */
4574 
4575 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4576 			/* Free ppa and interface type struct */
4577 			if (ill_interface->illif_ppa_arena != NULL) {
4578 				vmem_free(ill_interface->illif_ppa_arena,
4579 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4580 			}
4581 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4582 			    0) {
4583 				ill_delete_interface_type(ill->ill_ifptr);
4584 			}
4585 
4586 			return (EINVAL);
4587 		}
4588 	}
4589 
4590 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4591 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4592 
4593 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4594 	    &where);
4595 	ill->ill_ifptr = ill_interface;
4596 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4597 
4598 	ill_phyint_reinit(ill);
4599 	return (0);
4600 }
4601 
4602 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4603 static boolean_t
4604 ipsq_init(ill_t *ill)
4605 {
4606 	ipsq_t  *ipsq;
4607 
4608 	/* Init the ipsq and impicitly enter as writer */
4609 	ill->ill_phyint->phyint_ipsq =
4610 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4611 	if (ill->ill_phyint->phyint_ipsq == NULL)
4612 		return (B_FALSE);
4613 	ipsq = ill->ill_phyint->phyint_ipsq;
4614 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4615 	ill->ill_phyint->phyint_ipsq_next = NULL;
4616 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4617 	ipsq->ipsq_refs = 1;
4618 	ipsq->ipsq_writer = curthread;
4619 	ipsq->ipsq_reentry_cnt = 1;
4620 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
4621 #ifdef DEBUG
4622 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack,
4623 	    IPSQ_STACK_DEPTH);
4624 #endif
4625 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4626 	return (B_TRUE);
4627 }
4628 
4629 /*
4630  * ill_init is called by ip_open when a device control stream is opened.
4631  * It does a few initializations, and shoots a DL_INFO_REQ message down
4632  * to the driver.  The response is later picked up in ip_rput_dlpi and
4633  * used to set up default mechanisms for talking to the driver.  (Always
4634  * called as writer.)
4635  *
4636  * If this function returns error, ip_open will call ip_close which in
4637  * turn will call ill_delete to clean up any memory allocated here that
4638  * is not yet freed.
4639  */
4640 int
4641 ill_init(queue_t *q, ill_t *ill)
4642 {
4643 	int	count;
4644 	dl_info_req_t	*dlir;
4645 	mblk_t	*info_mp;
4646 	uchar_t *frag_ptr;
4647 
4648 	/*
4649 	 * The ill is initialized to zero by mi_alloc*(). In addition
4650 	 * some fields already contain valid values, initialized in
4651 	 * ip_open(), before we reach here.
4652 	 */
4653 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4654 
4655 	ill->ill_rq = q;
4656 	ill->ill_wq = WR(q);
4657 
4658 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4659 	    BPRI_HI);
4660 	if (info_mp == NULL)
4661 		return (ENOMEM);
4662 
4663 	/*
4664 	 * Allocate sufficient space to contain our fragment hash table and
4665 	 * the device name.
4666 	 */
4667 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4668 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4669 	if (frag_ptr == NULL) {
4670 		freemsg(info_mp);
4671 		return (ENOMEM);
4672 	}
4673 	ill->ill_frag_ptr = frag_ptr;
4674 	ill->ill_frag_free_num_pkts = 0;
4675 	ill->ill_last_frag_clean_time = 0;
4676 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4677 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4678 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4679 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4680 		    NULL, MUTEX_DEFAULT, NULL);
4681 	}
4682 
4683 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4684 	if (ill->ill_phyint == NULL) {
4685 		freemsg(info_mp);
4686 		mi_free(frag_ptr);
4687 		return (ENOMEM);
4688 	}
4689 
4690 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4691 	/*
4692 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4693 	 * at this point because of the following reason. If we can't
4694 	 * enter the ipsq at some point and cv_wait, the writer that
4695 	 * wakes us up tries to locate us using the list of all phyints
4696 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4697 	 * If we don't set it now, we risk a missed wakeup.
4698 	 */
4699 	ill->ill_phyint->phyint_illv4 = ill;
4700 	ill->ill_ppa = UINT_MAX;
4701 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4702 
4703 	if (!ipsq_init(ill)) {
4704 		freemsg(info_mp);
4705 		mi_free(frag_ptr);
4706 		mi_free(ill->ill_phyint);
4707 		return (ENOMEM);
4708 	}
4709 
4710 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4711 
4712 
4713 	/* Frag queue limit stuff */
4714 	ill->ill_frag_count = 0;
4715 	ill->ill_ipf_gen = 0;
4716 
4717 	ill->ill_global_timer = INFINITY;
4718 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4719 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4720 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4721 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4722 
4723 	/*
4724 	 * Initialize IPv6 configuration variables.  The IP module is always
4725 	 * opened as an IPv4 module.  Instead tracking down the cases where
4726 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4727 	 * here for convenience, this has no effect until the ill is set to do
4728 	 * IPv6.
4729 	 */
4730 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4731 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4732 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4733 	ill->ill_max_buf = ND_MAX_Q;
4734 	ill->ill_refcnt = 0;
4735 
4736 	/* Send down the Info Request to the driver. */
4737 	info_mp->b_datap->db_type = M_PCPROTO;
4738 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4739 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4740 	dlir->dl_primitive = DL_INFO_REQ;
4741 
4742 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4743 
4744 	qprocson(q);
4745 	ill_dlpi_send(ill, info_mp);
4746 
4747 	return (0);
4748 }
4749 
4750 /*
4751  * ill_dls_info
4752  * creates datalink socket info from the device.
4753  */
4754 int
4755 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4756 {
4757 	size_t	len;
4758 	ill_t	*ill = ipif->ipif_ill;
4759 
4760 	sdl->sdl_family = AF_LINK;
4761 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4762 	sdl->sdl_type = ill->ill_type;
4763 	ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4764 	len = strlen(sdl->sdl_data);
4765 	ASSERT(len < 256);
4766 	sdl->sdl_nlen = (uchar_t)len;
4767 	sdl->sdl_alen = ill->ill_phys_addr_length;
4768 	sdl->sdl_slen = 0;
4769 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
4770 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
4771 
4772 	return (sizeof (struct sockaddr_dl));
4773 }
4774 
4775 /*
4776  * ill_xarp_info
4777  * creates xarp info from the device.
4778  */
4779 static int
4780 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4781 {
4782 	sdl->sdl_family = AF_LINK;
4783 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4784 	sdl->sdl_type = ill->ill_type;
4785 	ipif_get_name(ill->ill_ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4786 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4787 	sdl->sdl_alen = ill->ill_phys_addr_length;
4788 	sdl->sdl_slen = 0;
4789 	return (sdl->sdl_nlen);
4790 }
4791 
4792 static int
4793 loopback_kstat_update(kstat_t *ksp, int rw)
4794 {
4795 	kstat_named_t *kn;
4796 	netstackid_t	stackid;
4797 	netstack_t	*ns;
4798 	ip_stack_t	*ipst;
4799 
4800 	if (ksp == NULL || ksp->ks_data == NULL)
4801 		return (EIO);
4802 
4803 	if (rw == KSTAT_WRITE)
4804 		return (EACCES);
4805 
4806 	kn = KSTAT_NAMED_PTR(ksp);
4807 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
4808 
4809 	ns = netstack_find_by_stackid(stackid);
4810 	if (ns == NULL)
4811 		return (-1);
4812 
4813 	ipst = ns->netstack_ip;
4814 	if (ipst == NULL) {
4815 		netstack_rele(ns);
4816 		return (-1);
4817 	}
4818 	kn[0].value.ui32 = ipst->ips_loopback_packets;
4819 	kn[1].value.ui32 = ipst->ips_loopback_packets;
4820 	netstack_rele(ns);
4821 	return (0);
4822 }
4823 
4824 
4825 /*
4826  * Has ifindex been plumbed already.
4827  * Compares both phyint_ifindex and phyint_group_ifindex.
4828  */
4829 static boolean_t
4830 phyint_exists(uint_t index, ip_stack_t *ipst)
4831 {
4832 	phyint_t *phyi;
4833 
4834 	ASSERT(index != 0);
4835 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
4836 	/*
4837 	 * Indexes are stored in the phyint - a common structure
4838 	 * to both IPv4 and IPv6.
4839 	 */
4840 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
4841 	for (; phyi != NULL;
4842 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
4843 	    phyi, AVL_AFTER)) {
4844 		if (phyi->phyint_ifindex == index ||
4845 		    phyi->phyint_group_ifindex == index)
4846 			return (B_TRUE);
4847 	}
4848 	return (B_FALSE);
4849 }
4850 
4851 /* Pick a unique ifindex */
4852 boolean_t
4853 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
4854 {
4855 	uint_t starting_index;
4856 
4857 	if (!ipst->ips_ill_index_wrap) {
4858 		*indexp = ipst->ips_ill_index++;
4859 		if (ipst->ips_ill_index == 0) {
4860 			/* Reached the uint_t limit Next time wrap  */
4861 			ipst->ips_ill_index_wrap = B_TRUE;
4862 		}
4863 		return (B_TRUE);
4864 	}
4865 
4866 	/*
4867 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4868 	 * at this point and don't want to call any function that attempts
4869 	 * to get the lock again.
4870 	 */
4871 	starting_index = ipst->ips_ill_index++;
4872 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
4873 		if (ipst->ips_ill_index != 0 &&
4874 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
4875 			/* found unused index - use it */
4876 			*indexp = ipst->ips_ill_index;
4877 			return (B_TRUE);
4878 		}
4879 	}
4880 
4881 	/*
4882 	 * all interface indicies are inuse.
4883 	 */
4884 	return (B_FALSE);
4885 }
4886 
4887 /*
4888  * Assign a unique interface index for the phyint.
4889  */
4890 static boolean_t
4891 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
4892 {
4893 	ASSERT(phyi->phyint_ifindex == 0);
4894 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
4895 }
4896 
4897 /*
4898  * Return a pointer to the ill which matches the supplied name.  Note that
4899  * the ill name length includes the null termination character.  (May be
4900  * called as writer.)
4901  * If do_alloc and the interface is "lo0" it will be automatically created.
4902  * Cannot bump up reference on condemned ills. So dup detect can't be done
4903  * using this func.
4904  */
4905 ill_t *
4906 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4907     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc,
4908     ip_stack_t *ipst)
4909 {
4910 	ill_t	*ill;
4911 	ipif_t	*ipif;
4912 	kstat_named_t	*kn;
4913 	boolean_t isloopback;
4914 	ipsq_t *old_ipsq;
4915 	in6_addr_t ov6addr;
4916 
4917 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4918 
4919 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4920 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4921 	rw_exit(&ipst->ips_ill_g_lock);
4922 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4923 		return (ill);
4924 
4925 	/*
4926 	 * Couldn't find it.  Does this happen to be a lookup for the
4927 	 * loopback device and are we allowed to allocate it?
4928 	 */
4929 	if (!isloopback || !do_alloc)
4930 		return (NULL);
4931 
4932 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
4933 
4934 	ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst);
4935 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4936 		rw_exit(&ipst->ips_ill_g_lock);
4937 		return (ill);
4938 	}
4939 
4940 	/* Create the loopback device on demand */
4941 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4942 	    sizeof (ipif_loopback_name), BPRI_MED));
4943 	if (ill == NULL)
4944 		goto done;
4945 
4946 	*ill = ill_null;
4947 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4948 	ill->ill_ipst = ipst;
4949 	netstack_hold(ipst->ips_netstack);
4950 	/*
4951 	 * For exclusive stacks we set the zoneid to zero
4952 	 * to make IP operate as if in the global zone.
4953 	 */
4954 	ill->ill_zoneid = GLOBAL_ZONEID;
4955 
4956 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4957 	if (ill->ill_phyint == NULL)
4958 		goto done;
4959 
4960 	if (isv6)
4961 		ill->ill_phyint->phyint_illv6 = ill;
4962 	else
4963 		ill->ill_phyint->phyint_illv4 = ill;
4964 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4965 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4966 	/* Add room for tcp+ip headers */
4967 	if (isv6) {
4968 		ill->ill_isv6 = B_TRUE;
4969 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4970 	} else {
4971 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4972 	}
4973 	if (!ill_allocate_mibs(ill))
4974 		goto done;
4975 	ill->ill_max_mtu = ill->ill_max_frag;
4976 	/*
4977 	 * ipif_loopback_name can't be pointed at directly because its used
4978 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4979 	 * from the glist, ill_glist_delete() sets the first character of
4980 	 * ill_name to '\0'.
4981 	 */
4982 	ill->ill_name = (char *)ill + sizeof (*ill);
4983 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4984 	ill->ill_name_length = sizeof (ipif_loopback_name);
4985 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
4986 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4987 
4988 	ill->ill_global_timer = INFINITY;
4989 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4990 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4991 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4992 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4993 
4994 	/* No resolver here. */
4995 	ill->ill_net_type = IRE_LOOPBACK;
4996 
4997 	/* Initialize the ipsq */
4998 	if (!ipsq_init(ill))
4999 		goto done;
5000 
5001 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
5002 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
5003 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
5004 #ifdef DEBUG
5005 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
5006 #endif
5007 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
5008 	if (ipif == NULL)
5009 		goto done;
5010 
5011 	ill->ill_flags = ILLF_MULTICAST;
5012 
5013 	ov6addr = ipif->ipif_v6lcl_addr;
5014 	/* Set up default loopback address and mask. */
5015 	if (!isv6) {
5016 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
5017 
5018 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
5019 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5020 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
5021 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5022 		    ipif->ipif_v6subnet);
5023 		ill->ill_flags |= ILLF_IPV4;
5024 	} else {
5025 		ipif->ipif_v6lcl_addr = ipv6_loopback;
5026 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
5027 		ipif->ipif_v6net_mask = ipv6_all_ones;
5028 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
5029 		    ipif->ipif_v6subnet);
5030 		ill->ill_flags |= ILLF_IPV6;
5031 	}
5032 
5033 	/*
5034 	 * Chain us in at the end of the ill list. hold the ill
5035 	 * before we make it globally visible. 1 for the lookup.
5036 	 */
5037 	ill->ill_refcnt = 0;
5038 	ill_refhold(ill);
5039 
5040 	ill->ill_frag_count = 0;
5041 	ill->ill_frag_free_num_pkts = 0;
5042 	ill->ill_last_frag_clean_time = 0;
5043 
5044 	old_ipsq = ill->ill_phyint->phyint_ipsq;
5045 
5046 	if (ill_glist_insert(ill, "lo", isv6) != 0)
5047 		cmn_err(CE_PANIC, "cannot insert loopback interface");
5048 
5049 	/* Let SCTP know so that it can add this to its list */
5050 	sctp_update_ill(ill, SCTP_ILL_INSERT);
5051 
5052 	/*
5053 	 * We have already assigned ipif_v6lcl_addr above, but we need to
5054 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
5055 	 * requires to be after ill_glist_insert() since we need the
5056 	 * ill_index set. Pass on ipv6_loopback as the old address.
5057 	 */
5058 	sctp_update_ipif_addr(ipif, ov6addr);
5059 
5060 	/*
5061 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
5062 	 */
5063 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
5064 		/* Loopback ills aren't in any IPMP group */
5065 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
5066 		ipsq_delete(old_ipsq);
5067 	}
5068 
5069 	/*
5070 	 * Delay this till the ipif is allocated as ipif_allocate
5071 	 * de-references ill_phyint for getting the ifindex. We
5072 	 * can't do this before ipif_allocate because ill_phyint_reinit
5073 	 * -> phyint_assign_ifindex expects ipif to be present.
5074 	 */
5075 	mutex_enter(&ill->ill_phyint->phyint_lock);
5076 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
5077 	mutex_exit(&ill->ill_phyint->phyint_lock);
5078 
5079 	if (ipst->ips_loopback_ksp == NULL) {
5080 		/* Export loopback interface statistics */
5081 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
5082 		    ipif_loopback_name, "net",
5083 		    KSTAT_TYPE_NAMED, 2, 0,
5084 		    ipst->ips_netstack->netstack_stackid);
5085 		if (ipst->ips_loopback_ksp != NULL) {
5086 			ipst->ips_loopback_ksp->ks_update =
5087 			    loopback_kstat_update;
5088 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
5089 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
5090 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
5091 			ipst->ips_loopback_ksp->ks_private =
5092 			    (void *)(uintptr_t)ipst->ips_netstack->
5093 			    netstack_stackid;
5094 			kstat_install(ipst->ips_loopback_ksp);
5095 		}
5096 	}
5097 
5098 	if (error != NULL)
5099 		*error = 0;
5100 	*did_alloc = B_TRUE;
5101 	rw_exit(&ipst->ips_ill_g_lock);
5102 	return (ill);
5103 done:
5104 	if (ill != NULL) {
5105 		if (ill->ill_phyint != NULL) {
5106 			ipsq_t	*ipsq;
5107 
5108 			ipsq = ill->ill_phyint->phyint_ipsq;
5109 			if (ipsq != NULL) {
5110 				ipsq->ipsq_ipst = NULL;
5111 				kmem_free(ipsq, sizeof (ipsq_t));
5112 			}
5113 			mi_free(ill->ill_phyint);
5114 		}
5115 		ill_free_mib(ill);
5116 		if (ill->ill_ipst != NULL)
5117 			netstack_rele(ill->ill_ipst->ips_netstack);
5118 		mi_free(ill);
5119 	}
5120 	rw_exit(&ipst->ips_ill_g_lock);
5121 	if (error != NULL)
5122 		*error = ENOMEM;
5123 	return (NULL);
5124 }
5125 
5126 /*
5127  * For IPP calls - use the ip_stack_t for global stack.
5128  */
5129 ill_t *
5130 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6,
5131     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
5132 {
5133 	ip_stack_t	*ipst;
5134 	ill_t		*ill;
5135 
5136 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
5137 	if (ipst == NULL) {
5138 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
5139 		return (NULL);
5140 	}
5141 
5142 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
5143 	netstack_rele(ipst->ips_netstack);
5144 	return (ill);
5145 }
5146 
5147 /*
5148  * Return a pointer to the ill which matches the index and IP version type.
5149  */
5150 ill_t *
5151 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
5152     ipsq_func_t func, int *err, ip_stack_t *ipst)
5153 {
5154 	ill_t	*ill;
5155 	ipsq_t  *ipsq;
5156 	phyint_t *phyi;
5157 
5158 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
5159 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
5160 
5161 	if (err != NULL)
5162 		*err = 0;
5163 
5164 	/*
5165 	 * Indexes are stored in the phyint - a common structure
5166 	 * to both IPv4 and IPv6.
5167 	 */
5168 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5169 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5170 	    (void *) &index, NULL);
5171 	if (phyi != NULL) {
5172 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
5173 		if (ill != NULL) {
5174 			/*
5175 			 * The block comment at the start of ipif_down
5176 			 * explains the use of the macros used below
5177 			 */
5178 			GRAB_CONN_LOCK(q);
5179 			mutex_enter(&ill->ill_lock);
5180 			if (ILL_CAN_LOOKUP(ill)) {
5181 				ill_refhold_locked(ill);
5182 				mutex_exit(&ill->ill_lock);
5183 				RELEASE_CONN_LOCK(q);
5184 				rw_exit(&ipst->ips_ill_g_lock);
5185 				return (ill);
5186 			} else if (ILL_CAN_WAIT(ill, q)) {
5187 				ipsq = ill->ill_phyint->phyint_ipsq;
5188 				mutex_enter(&ipsq->ipsq_lock);
5189 				rw_exit(&ipst->ips_ill_g_lock);
5190 				mutex_exit(&ill->ill_lock);
5191 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
5192 				mutex_exit(&ipsq->ipsq_lock);
5193 				RELEASE_CONN_LOCK(q);
5194 				if (err != NULL)
5195 					*err = EINPROGRESS;
5196 				return (NULL);
5197 			}
5198 			RELEASE_CONN_LOCK(q);
5199 			mutex_exit(&ill->ill_lock);
5200 		}
5201 	}
5202 	rw_exit(&ipst->ips_ill_g_lock);
5203 	if (err != NULL)
5204 		*err = ENXIO;
5205 	return (NULL);
5206 }
5207 
5208 /*
5209  * Return the ifindex next in sequence after the passed in ifindex.
5210  * If there is no next ifindex for the given protocol, return 0.
5211  */
5212 uint_t
5213 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
5214 {
5215 	phyint_t *phyi;
5216 	phyint_t *phyi_initial;
5217 	uint_t   ifindex;
5218 
5219 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5220 
5221 	if (index == 0) {
5222 		phyi = avl_first(
5223 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
5224 	} else {
5225 		phyi = phyi_initial = avl_find(
5226 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5227 		    (void *) &index, NULL);
5228 	}
5229 
5230 	for (; phyi != NULL;
5231 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
5232 	    phyi, AVL_AFTER)) {
5233 		/*
5234 		 * If we're not returning the first interface in the tree
5235 		 * and we still haven't moved past the phyint_t that
5236 		 * corresponds to index, avl_walk needs to be called again
5237 		 */
5238 		if (!((index != 0) && (phyi == phyi_initial))) {
5239 			if (isv6) {
5240 				if ((phyi->phyint_illv6) &&
5241 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
5242 				    (phyi->phyint_illv6->ill_isv6 == 1))
5243 					break;
5244 			} else {
5245 				if ((phyi->phyint_illv4) &&
5246 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
5247 				    (phyi->phyint_illv4->ill_isv6 == 0))
5248 					break;
5249 			}
5250 		}
5251 	}
5252 
5253 	rw_exit(&ipst->ips_ill_g_lock);
5254 
5255 	if (phyi != NULL)
5256 		ifindex = phyi->phyint_ifindex;
5257 	else
5258 		ifindex = 0;
5259 
5260 	return (ifindex);
5261 }
5262 
5263 
5264 /*
5265  * Return the ifindex for the named interface.
5266  * If there is no next ifindex for the interface, return 0.
5267  */
5268 uint_t
5269 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
5270 {
5271 	phyint_t	*phyi;
5272 	avl_index_t	where = 0;
5273 	uint_t		ifindex;
5274 
5275 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5276 
5277 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
5278 	    name, &where)) == NULL) {
5279 		rw_exit(&ipst->ips_ill_g_lock);
5280 		return (0);
5281 	}
5282 
5283 	ifindex = phyi->phyint_ifindex;
5284 
5285 	rw_exit(&ipst->ips_ill_g_lock);
5286 
5287 	return (ifindex);
5288 }
5289 
5290 
5291 /*
5292  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
5293  * that gives a running thread a reference to the ill. This reference must be
5294  * released by the thread when it is done accessing the ill and related
5295  * objects. ill_refcnt can not be used to account for static references
5296  * such as other structures pointing to an ill. Callers must generally
5297  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
5298  * or be sure that the ill is not being deleted or changing state before
5299  * calling the refhold functions. A non-zero ill_refcnt ensures that the
5300  * ill won't change any of its critical state such as address, netmask etc.
5301  */
5302 void
5303 ill_refhold(ill_t *ill)
5304 {
5305 	mutex_enter(&ill->ill_lock);
5306 	ill->ill_refcnt++;
5307 	ILL_TRACE_REF(ill);
5308 	mutex_exit(&ill->ill_lock);
5309 }
5310 
5311 void
5312 ill_refhold_locked(ill_t *ill)
5313 {
5314 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5315 	ill->ill_refcnt++;
5316 	ILL_TRACE_REF(ill);
5317 }
5318 
5319 int
5320 ill_check_and_refhold(ill_t *ill)
5321 {
5322 	mutex_enter(&ill->ill_lock);
5323 	if (ILL_CAN_LOOKUP(ill)) {
5324 		ill_refhold_locked(ill);
5325 		mutex_exit(&ill->ill_lock);
5326 		return (0);
5327 	}
5328 	mutex_exit(&ill->ill_lock);
5329 	return (ILL_LOOKUP_FAILED);
5330 }
5331 
5332 /*
5333  * Must not be called while holding any locks. Otherwise if this is
5334  * the last reference to be released, there is a chance of recursive mutex
5335  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5336  * to restart an ioctl.
5337  */
5338 void
5339 ill_refrele(ill_t *ill)
5340 {
5341 	mutex_enter(&ill->ill_lock);
5342 	ASSERT(ill->ill_refcnt != 0);
5343 	ill->ill_refcnt--;
5344 	ILL_UNTRACE_REF(ill);
5345 	if (ill->ill_refcnt != 0) {
5346 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
5347 		mutex_exit(&ill->ill_lock);
5348 		return;
5349 	}
5350 
5351 	/* Drops the ill_lock */
5352 	ipif_ill_refrele_tail(ill);
5353 }
5354 
5355 /*
5356  * Obtain a weak reference count on the ill. This reference ensures the
5357  * ill won't be freed, but the ill may change any of its critical state
5358  * such as netmask, address etc. Returns an error if the ill has started
5359  * closing.
5360  */
5361 boolean_t
5362 ill_waiter_inc(ill_t *ill)
5363 {
5364 	mutex_enter(&ill->ill_lock);
5365 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5366 		mutex_exit(&ill->ill_lock);
5367 		return (B_FALSE);
5368 	}
5369 	ill->ill_waiters++;
5370 	mutex_exit(&ill->ill_lock);
5371 	return (B_TRUE);
5372 }
5373 
5374 void
5375 ill_waiter_dcr(ill_t *ill)
5376 {
5377 	mutex_enter(&ill->ill_lock);
5378 	ill->ill_waiters--;
5379 	if (ill->ill_waiters == 0)
5380 		cv_broadcast(&ill->ill_cv);
5381 	mutex_exit(&ill->ill_lock);
5382 }
5383 
5384 /*
5385  * Named Dispatch routine to produce a formatted report on all ILLs.
5386  * This report is accessed by using the ndd utility to "get" ND variable
5387  * "ip_ill_status".
5388  */
5389 /* ARGSUSED */
5390 int
5391 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5392 {
5393 	ill_t		*ill;
5394 	ill_walk_context_t ctx;
5395 	ip_stack_t	*ipst;
5396 
5397 	ipst = CONNQ_TO_IPST(q);
5398 
5399 	(void) mi_mpprintf(mp,
5400 	    "ILL      " MI_COL_HDRPAD_STR
5401 	/*   01234567[89ABCDEF] */
5402 	    "rq       " MI_COL_HDRPAD_STR
5403 	/*   01234567[89ABCDEF] */
5404 	    "wq       " MI_COL_HDRPAD_STR
5405 	/*   01234567[89ABCDEF] */
5406 	    "upcnt mxfrg err name");
5407 	/*   12345 12345 123 xxxxxxxx  */
5408 
5409 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5410 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5411 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5412 		(void) mi_mpprintf(mp,
5413 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5414 		    "%05u %05u %03d %s",
5415 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5416 		    ill->ill_ipif_up_count,
5417 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5418 	}
5419 	rw_exit(&ipst->ips_ill_g_lock);
5420 
5421 	return (0);
5422 }
5423 
5424 /*
5425  * Named Dispatch routine to produce a formatted report on all IPIFs.
5426  * This report is accessed by using the ndd utility to "get" ND variable
5427  * "ip_ipif_status".
5428  */
5429 /* ARGSUSED */
5430 int
5431 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5432 {
5433 	char	buf1[INET6_ADDRSTRLEN];
5434 	char	buf2[INET6_ADDRSTRLEN];
5435 	char	buf3[INET6_ADDRSTRLEN];
5436 	char	buf4[INET6_ADDRSTRLEN];
5437 	char	buf5[INET6_ADDRSTRLEN];
5438 	char	buf6[INET6_ADDRSTRLEN];
5439 	char	buf[LIFNAMSIZ];
5440 	ill_t	*ill;
5441 	ipif_t	*ipif;
5442 	nv_t	*nvp;
5443 	uint64_t flags;
5444 	zoneid_t zoneid;
5445 	ill_walk_context_t ctx;
5446 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
5447 
5448 	(void) mi_mpprintf(mp,
5449 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5450 	    "\tlocal address\n"
5451 	    "\tsrc address\n"
5452 	    "\tsubnet\n"
5453 	    "\tmask\n"
5454 	    "\tbroadcast\n"
5455 	    "\tp-p-dst");
5456 
5457 	ASSERT(q->q_next == NULL);
5458 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5459 
5460 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5461 	ill = ILL_START_WALK_ALL(&ctx, ipst);
5462 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5463 		for (ipif = ill->ill_ipif; ipif != NULL;
5464 		    ipif = ipif->ipif_next) {
5465 			if (zoneid != GLOBAL_ZONEID &&
5466 			    zoneid != ipif->ipif_zoneid &&
5467 			    ipif->ipif_zoneid != ALL_ZONES)
5468 				continue;
5469 
5470 			ipif_get_name(ipif, buf, sizeof (buf));
5471 			(void) mi_mpprintf(mp,
5472 			    MI_COL_PTRFMT_STR
5473 			    "%04u %05u %u/%u/%u %s %d",
5474 			    (void *)ipif,
5475 			    ipif->ipif_metric, ipif->ipif_mtu,
5476 			    ipif->ipif_ib_pkt_count,
5477 			    ipif->ipif_ob_pkt_count,
5478 			    ipif->ipif_fo_pkt_count,
5479 			    buf,
5480 			    ipif->ipif_zoneid);
5481 
5482 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5483 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5484 
5485 		/* Tack on text strings for any flags. */
5486 		nvp = ipif_nv_tbl;
5487 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5488 			if (nvp->nv_value & flags)
5489 				(void) mi_mpprintf_nr(mp, " %s",
5490 				    nvp->nv_name);
5491 		}
5492 		(void) mi_mpprintf(mp,
5493 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5494 		    inet_ntop(AF_INET6,
5495 		    &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5496 		    inet_ntop(AF_INET6,
5497 		    &ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5498 		    inet_ntop(AF_INET6,
5499 		    &ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5500 		    inet_ntop(AF_INET6,
5501 		    &ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5502 		    inet_ntop(AF_INET6,
5503 		    &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5504 		    inet_ntop(AF_INET6,
5505 		    &ipif->ipif_v6pp_dst_addr, buf6, sizeof (buf6)));
5506 		}
5507 	}
5508 	rw_exit(&ipst->ips_ill_g_lock);
5509 	return (0);
5510 }
5511 
5512 /*
5513  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5514  * driver.  We construct best guess defaults for lower level information that
5515  * we need.  If an interface is brought up without injection of any overriding
5516  * information from outside, we have to be ready to go with these defaults.
5517  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5518  * we primarely want the dl_provider_style.
5519  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5520  * at which point we assume the other part of the information is valid.
5521  */
5522 void
5523 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5524 {
5525 	uchar_t		*brdcst_addr;
5526 	uint_t		brdcst_addr_length, phys_addr_length;
5527 	t_scalar_t	sap_length;
5528 	dl_info_ack_t	*dlia;
5529 	ip_m_t		*ipm;
5530 	dl_qos_cl_sel1_t *sel1;
5531 
5532 	ASSERT(IAM_WRITER_ILL(ill));
5533 
5534 	/*
5535 	 * Till the ill is fully up ILL_CHANGING will be set and
5536 	 * the ill is not globally visible. So no need for a lock.
5537 	 */
5538 	dlia = (dl_info_ack_t *)mp->b_rptr;
5539 	ill->ill_mactype = dlia->dl_mac_type;
5540 
5541 	ipm = ip_m_lookup(dlia->dl_mac_type);
5542 	if (ipm == NULL) {
5543 		ipm = ip_m_lookup(DL_OTHER);
5544 		ASSERT(ipm != NULL);
5545 	}
5546 	ill->ill_media = ipm;
5547 
5548 	/*
5549 	 * When the new DLPI stuff is ready we'll pull lengths
5550 	 * from dlia.
5551 	 */
5552 	if (dlia->dl_version == DL_VERSION_2) {
5553 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5554 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5555 		    brdcst_addr_length);
5556 		if (brdcst_addr == NULL) {
5557 			brdcst_addr_length = 0;
5558 		}
5559 		sap_length = dlia->dl_sap_length;
5560 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5561 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5562 		    brdcst_addr_length, sap_length, phys_addr_length));
5563 	} else {
5564 		brdcst_addr_length = 6;
5565 		brdcst_addr = ip_six_byte_all_ones;
5566 		sap_length = -2;
5567 		phys_addr_length = brdcst_addr_length;
5568 	}
5569 
5570 	ill->ill_bcast_addr_length = brdcst_addr_length;
5571 	ill->ill_phys_addr_length = phys_addr_length;
5572 	ill->ill_sap_length = sap_length;
5573 	ill->ill_max_frag = dlia->dl_max_sdu;
5574 	ill->ill_max_mtu = ill->ill_max_frag;
5575 
5576 	ill->ill_type = ipm->ip_m_type;
5577 
5578 	if (!ill->ill_dlpi_style_set) {
5579 		if (dlia->dl_provider_style == DL_STYLE2)
5580 			ill->ill_needs_attach = 1;
5581 
5582 		/*
5583 		 * Allocate the first ipif on this ill. We don't delay it
5584 		 * further as ioctl handling assumes atleast one ipif to
5585 		 * be present.
5586 		 *
5587 		 * At this point we don't know whether the ill is v4 or v6.
5588 		 * We will know this whan the SIOCSLIFNAME happens and
5589 		 * the correct value for ill_isv6 will be assigned in
5590 		 * ipif_set_values(). We need to hold the ill lock and
5591 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5592 		 * the wakeup.
5593 		 */
5594 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5595 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5596 		mutex_enter(&ill->ill_lock);
5597 		ASSERT(ill->ill_dlpi_style_set == 0);
5598 		ill->ill_dlpi_style_set = 1;
5599 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5600 		cv_broadcast(&ill->ill_cv);
5601 		mutex_exit(&ill->ill_lock);
5602 		freemsg(mp);
5603 		return;
5604 	}
5605 	ASSERT(ill->ill_ipif != NULL);
5606 	/*
5607 	 * We know whether it is IPv4 or IPv6 now, as this is the
5608 	 * second DL_INFO_ACK we are recieving in response to the
5609 	 * DL_INFO_REQ sent in ipif_set_values.
5610 	 */
5611 	if (ill->ill_isv6)
5612 		ill->ill_sap = IP6_DL_SAP;
5613 	else
5614 		ill->ill_sap = IP_DL_SAP;
5615 	/*
5616 	 * Set ipif_mtu which is used to set the IRE's
5617 	 * ire_max_frag value. The driver could have sent
5618 	 * a different mtu from what it sent last time. No
5619 	 * need to call ipif_mtu_change because IREs have
5620 	 * not yet been created.
5621 	 */
5622 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5623 	/*
5624 	 * Clear all the flags that were set based on ill_bcast_addr_length
5625 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5626 	 * changed now and we need to re-evaluate.
5627 	 */
5628 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5629 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5630 
5631 	/*
5632 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5633 	 * changed now.
5634 	 */
5635 	if (ill->ill_bcast_addr_length == 0) {
5636 		if (ill->ill_resolver_mp != NULL)
5637 			freemsg(ill->ill_resolver_mp);
5638 		if (ill->ill_bcast_mp != NULL)
5639 			freemsg(ill->ill_bcast_mp);
5640 		if (ill->ill_flags & ILLF_XRESOLV)
5641 			ill->ill_net_type = IRE_IF_RESOLVER;
5642 		else
5643 			ill->ill_net_type = IRE_IF_NORESOLVER;
5644 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5645 		    ill->ill_phys_addr_length,
5646 		    ill->ill_sap,
5647 		    ill->ill_sap_length);
5648 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5649 
5650 		if (ill->ill_isv6)
5651 			/*
5652 			 * Note: xresolv interfaces will eventually need NOARP
5653 			 * set here as well, but that will require those
5654 			 * external resolvers to have some knowledge of
5655 			 * that flag and act appropriately. Not to be changed
5656 			 * at present.
5657 			 */
5658 			ill->ill_flags |= ILLF_NONUD;
5659 		else
5660 			ill->ill_flags |= ILLF_NOARP;
5661 
5662 		if (ill->ill_phys_addr_length == 0) {
5663 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5664 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5665 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5666 			} else {
5667 				/* pt-pt supports multicast. */
5668 				ill->ill_flags |= ILLF_MULTICAST;
5669 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5670 			}
5671 		}
5672 	} else {
5673 		ill->ill_net_type = IRE_IF_RESOLVER;
5674 		if (ill->ill_bcast_mp != NULL)
5675 			freemsg(ill->ill_bcast_mp);
5676 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5677 		    ill->ill_bcast_addr_length, ill->ill_sap,
5678 		    ill->ill_sap_length);
5679 		/*
5680 		 * Later detect lack of DLPI driver multicast
5681 		 * capability by catching DL_ENABMULTI errors in
5682 		 * ip_rput_dlpi.
5683 		 */
5684 		ill->ill_flags |= ILLF_MULTICAST;
5685 		if (!ill->ill_isv6)
5686 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5687 	}
5688 	/* By default an interface does not support any CoS marking */
5689 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5690 
5691 	/*
5692 	 * If we get QoS information in DL_INFO_ACK, the device supports
5693 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5694 	 */
5695 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5696 	    dlia->dl_qos_length);
5697 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5698 		ill->ill_flags |= ILLF_COS_ENABLED;
5699 	}
5700 
5701 	/* Clear any previous error indication. */
5702 	ill->ill_error = 0;
5703 	freemsg(mp);
5704 }
5705 
5706 /*
5707  * Perform various checks to verify that an address would make sense as a
5708  * local, remote, or subnet interface address.
5709  */
5710 static boolean_t
5711 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5712 {
5713 	ipaddr_t	net_mask;
5714 
5715 	/*
5716 	 * Don't allow all zeroes, or all ones, but allow
5717 	 * all ones netmask.
5718 	 */
5719 	if ((net_mask = ip_net_mask(addr)) == 0)
5720 		return (B_FALSE);
5721 	/* A given netmask overrides the "guess" netmask */
5722 	if (subnet_mask != 0)
5723 		net_mask = subnet_mask;
5724 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5725 	    (addr == (addr | ~net_mask)))) {
5726 		return (B_FALSE);
5727 	}
5728 
5729 	/*
5730 	 * Even if the netmask is all ones, we do not allow address to be
5731 	 * 255.255.255.255
5732 	 */
5733 	if (addr == INADDR_BROADCAST)
5734 		return (B_FALSE);
5735 
5736 	if (CLASSD(addr))
5737 		return (B_FALSE);
5738 
5739 	return (B_TRUE);
5740 }
5741 
5742 #define	V6_IPIF_LINKLOCAL(p)	\
5743 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
5744 
5745 /*
5746  * Compare two given ipifs and check if the second one is better than
5747  * the first one using the order of preference (not taking deprecated
5748  * into acount) specified in ipif_lookup_multicast().
5749  */
5750 static boolean_t
5751 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
5752 {
5753 	/* Check the least preferred first. */
5754 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
5755 		/* If both ipifs are the same, use the first one. */
5756 		if (IS_LOOPBACK(new_ipif->ipif_ill))
5757 			return (B_FALSE);
5758 		else
5759 			return (B_TRUE);
5760 	}
5761 
5762 	/* For IPv6, check for link local address. */
5763 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
5764 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5765 		    V6_IPIF_LINKLOCAL(new_ipif)) {
5766 			/* The second one is equal or less preferred. */
5767 			return (B_FALSE);
5768 		} else {
5769 			return (B_TRUE);
5770 		}
5771 	}
5772 
5773 	/* Then check for point to point interface. */
5774 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
5775 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
5776 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
5777 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
5778 			return (B_FALSE);
5779 		} else {
5780 			return (B_TRUE);
5781 		}
5782 	}
5783 
5784 	/* old_ipif is a normal interface, so no need to use the new one. */
5785 	return (B_FALSE);
5786 }
5787 
5788 /*
5789  * Find any non-virtual, not condemned, and up multicast capable interface
5790  * given an IP instance and zoneid.  Order of preference is:
5791  *
5792  * 1. normal
5793  * 1.1 normal, but deprecated
5794  * 2. point to point
5795  * 2.1 point to point, but deprecated
5796  * 3. link local
5797  * 3.1 link local, but deprecated
5798  * 4. loopback.
5799  */
5800 ipif_t *
5801 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
5802 {
5803 	ill_t			*ill;
5804 	ill_walk_context_t	ctx;
5805 	ipif_t			*ipif;
5806 	ipif_t			*saved_ipif = NULL;
5807 	ipif_t			*dep_ipif = NULL;
5808 
5809 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5810 	if (isv6)
5811 		ill = ILL_START_WALK_V6(&ctx, ipst);
5812 	else
5813 		ill = ILL_START_WALK_V4(&ctx, ipst);
5814 
5815 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5816 		mutex_enter(&ill->ill_lock);
5817 		if (IS_VNI(ill) || !ILL_CAN_LOOKUP(ill) ||
5818 		    !(ill->ill_flags & ILLF_MULTICAST)) {
5819 			mutex_exit(&ill->ill_lock);
5820 			continue;
5821 		}
5822 		for (ipif = ill->ill_ipif; ipif != NULL;
5823 		    ipif = ipif->ipif_next) {
5824 			if (zoneid != ipif->ipif_zoneid &&
5825 			    zoneid != ALL_ZONES &&
5826 			    ipif->ipif_zoneid != ALL_ZONES) {
5827 				continue;
5828 			}
5829 			if (!(ipif->ipif_flags & IPIF_UP) ||
5830 			    !IPIF_CAN_LOOKUP(ipif)) {
5831 				continue;
5832 			}
5833 
5834 			/*
5835 			 * Found one candidate.  If it is deprecated,
5836 			 * remember it in dep_ipif.  If it is not deprecated,
5837 			 * remember it in saved_ipif.
5838 			 */
5839 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
5840 				if (dep_ipif == NULL) {
5841 					dep_ipif = ipif;
5842 				} else if (ipif_comp_multi(dep_ipif, ipif,
5843 				    isv6)) {
5844 					/*
5845 					 * If the previous dep_ipif does not
5846 					 * belong to the same ill, we've done
5847 					 * a ipif_refhold() on it.  So we need
5848 					 * to release it.
5849 					 */
5850 					if (dep_ipif->ipif_ill != ill)
5851 						ipif_refrele(dep_ipif);
5852 					dep_ipif = ipif;
5853 				}
5854 				continue;
5855 			}
5856 			if (saved_ipif == NULL) {
5857 				saved_ipif = ipif;
5858 			} else {
5859 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
5860 					if (saved_ipif->ipif_ill != ill)
5861 						ipif_refrele(saved_ipif);
5862 					saved_ipif = ipif;
5863 				}
5864 			}
5865 		}
5866 		/*
5867 		 * Before going to the next ill, do a ipif_refhold() on the
5868 		 * saved ones.
5869 		 */
5870 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
5871 			ipif_refhold_locked(saved_ipif);
5872 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
5873 			ipif_refhold_locked(dep_ipif);
5874 		mutex_exit(&ill->ill_lock);
5875 	}
5876 	rw_exit(&ipst->ips_ill_g_lock);
5877 
5878 	/*
5879 	 * If we have only the saved_ipif, return it.  But if we have both
5880 	 * saved_ipif and dep_ipif, check to see which one is better.
5881 	 */
5882 	if (saved_ipif != NULL) {
5883 		if (dep_ipif != NULL) {
5884 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
5885 				ipif_refrele(saved_ipif);
5886 				return (dep_ipif);
5887 			} else {
5888 				ipif_refrele(dep_ipif);
5889 				return (saved_ipif);
5890 			}
5891 		}
5892 		return (saved_ipif);
5893 	} else {
5894 		return (dep_ipif);
5895 	}
5896 }
5897 
5898 /*
5899  * This function is called when an application does not specify an interface
5900  * to be used for multicast traffic (joining a group/sending data).  It
5901  * calls ire_lookup_multi() to look for an interface route for the
5902  * specified multicast group.  Doing this allows the administrator to add
5903  * prefix routes for multicast to indicate which interface to be used for
5904  * multicast traffic in the above scenario.  The route could be for all
5905  * multicast (224.0/4), for a single multicast group (a /32 route) or
5906  * anything in between.  If there is no such multicast route, we just find
5907  * any multicast capable interface and return it.  The returned ipif
5908  * is refhold'ed.
5909  */
5910 ipif_t *
5911 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
5912 {
5913 	ire_t			*ire;
5914 	ipif_t			*ipif;
5915 
5916 	ire = ire_lookup_multi(group, zoneid, ipst);
5917 	if (ire != NULL) {
5918 		ipif = ire->ire_ipif;
5919 		ipif_refhold(ipif);
5920 		ire_refrele(ire);
5921 		return (ipif);
5922 	}
5923 
5924 	return (ipif_lookup_multicast(ipst, zoneid, B_FALSE));
5925 }
5926 
5927 /*
5928  * Look for an ipif with the specified interface address and destination.
5929  * The destination address is used only for matching point-to-point interfaces.
5930  */
5931 ipif_t *
5932 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5933     ipsq_func_t func, int *error, ip_stack_t *ipst)
5934 {
5935 	ipif_t	*ipif;
5936 	ill_t	*ill;
5937 	ill_walk_context_t ctx;
5938 	ipsq_t	*ipsq;
5939 
5940 	if (error != NULL)
5941 		*error = 0;
5942 
5943 	/*
5944 	 * First match all the point-to-point interfaces
5945 	 * before looking at non-point-to-point interfaces.
5946 	 * This is done to avoid returning non-point-to-point
5947 	 * ipif instead of unnumbered point-to-point ipif.
5948 	 */
5949 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
5950 	ill = ILL_START_WALK_V4(&ctx, ipst);
5951 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5952 		GRAB_CONN_LOCK(q);
5953 		mutex_enter(&ill->ill_lock);
5954 		for (ipif = ill->ill_ipif; ipif != NULL;
5955 		    ipif = ipif->ipif_next) {
5956 			/* Allow the ipif to be down */
5957 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5958 			    (ipif->ipif_lcl_addr == if_addr) &&
5959 			    (ipif->ipif_pp_dst_addr == dst)) {
5960 				/*
5961 				 * The block comment at the start of ipif_down
5962 				 * explains the use of the macros used below
5963 				 */
5964 				if (IPIF_CAN_LOOKUP(ipif)) {
5965 					ipif_refhold_locked(ipif);
5966 					mutex_exit(&ill->ill_lock);
5967 					RELEASE_CONN_LOCK(q);
5968 					rw_exit(&ipst->ips_ill_g_lock);
5969 					return (ipif);
5970 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5971 					ipsq = ill->ill_phyint->phyint_ipsq;
5972 					mutex_enter(&ipsq->ipsq_lock);
5973 					mutex_exit(&ill->ill_lock);
5974 					rw_exit(&ipst->ips_ill_g_lock);
5975 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5976 					    ill);
5977 					mutex_exit(&ipsq->ipsq_lock);
5978 					RELEASE_CONN_LOCK(q);
5979 					if (error != NULL)
5980 						*error = EINPROGRESS;
5981 					return (NULL);
5982 				}
5983 			}
5984 		}
5985 		mutex_exit(&ill->ill_lock);
5986 		RELEASE_CONN_LOCK(q);
5987 	}
5988 	rw_exit(&ipst->ips_ill_g_lock);
5989 
5990 	/* lookup the ipif based on interface address */
5991 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error,
5992 	    ipst);
5993 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5994 	return (ipif);
5995 }
5996 
5997 /*
5998  * Look for an ipif with the specified address. For point-point links
5999  * we look for matches on either the destination address and the local
6000  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6001  * is set.
6002  * Matches on a specific ill if match_ill is set.
6003  */
6004 ipif_t *
6005 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
6006     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
6007 {
6008 	ipif_t  *ipif;
6009 	ill_t   *ill;
6010 	boolean_t ptp = B_FALSE;
6011 	ipsq_t	*ipsq;
6012 	ill_walk_context_t	ctx;
6013 
6014 	if (error != NULL)
6015 		*error = 0;
6016 
6017 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6018 	/*
6019 	 * Repeat twice, first based on local addresses and
6020 	 * next time for pointopoint.
6021 	 */
6022 repeat:
6023 	ill = ILL_START_WALK_V4(&ctx, ipst);
6024 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6025 		if (match_ill != NULL && ill != match_ill) {
6026 			continue;
6027 		}
6028 		GRAB_CONN_LOCK(q);
6029 		mutex_enter(&ill->ill_lock);
6030 		for (ipif = ill->ill_ipif; ipif != NULL;
6031 		    ipif = ipif->ipif_next) {
6032 			if (zoneid != ALL_ZONES &&
6033 			    zoneid != ipif->ipif_zoneid &&
6034 			    ipif->ipif_zoneid != ALL_ZONES)
6035 				continue;
6036 			/* Allow the ipif to be down */
6037 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6038 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6039 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6040 			    (ipif->ipif_pp_dst_addr == addr))) {
6041 				/*
6042 				 * The block comment at the start of ipif_down
6043 				 * explains the use of the macros used below
6044 				 */
6045 				if (IPIF_CAN_LOOKUP(ipif)) {
6046 					ipif_refhold_locked(ipif);
6047 					mutex_exit(&ill->ill_lock);
6048 					RELEASE_CONN_LOCK(q);
6049 					rw_exit(&ipst->ips_ill_g_lock);
6050 					return (ipif);
6051 				} else if (IPIF_CAN_WAIT(ipif, q)) {
6052 					ipsq = ill->ill_phyint->phyint_ipsq;
6053 					mutex_enter(&ipsq->ipsq_lock);
6054 					mutex_exit(&ill->ill_lock);
6055 					rw_exit(&ipst->ips_ill_g_lock);
6056 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
6057 					    ill);
6058 					mutex_exit(&ipsq->ipsq_lock);
6059 					RELEASE_CONN_LOCK(q);
6060 					if (error != NULL)
6061 						*error = EINPROGRESS;
6062 					return (NULL);
6063 				}
6064 			}
6065 		}
6066 		mutex_exit(&ill->ill_lock);
6067 		RELEASE_CONN_LOCK(q);
6068 	}
6069 
6070 	/* If we already did the ptp case, then we are done */
6071 	if (ptp) {
6072 		rw_exit(&ipst->ips_ill_g_lock);
6073 		if (error != NULL)
6074 			*error = ENXIO;
6075 		return (NULL);
6076 	}
6077 	ptp = B_TRUE;
6078 	goto repeat;
6079 }
6080 
6081 /*
6082  * Look for an ipif with the specified address. For point-point links
6083  * we look for matches on either the destination address and the local
6084  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
6085  * is set.
6086  * Matches on a specific ill if match_ill is set.
6087  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
6088  */
6089 zoneid_t
6090 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
6091 {
6092 	zoneid_t zoneid;
6093 	ipif_t  *ipif;
6094 	ill_t   *ill;
6095 	boolean_t ptp = B_FALSE;
6096 	ill_walk_context_t	ctx;
6097 
6098 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6099 	/*
6100 	 * Repeat twice, first based on local addresses and
6101 	 * next time for pointopoint.
6102 	 */
6103 repeat:
6104 	ill = ILL_START_WALK_V4(&ctx, ipst);
6105 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6106 		if (match_ill != NULL && ill != match_ill) {
6107 			continue;
6108 		}
6109 		mutex_enter(&ill->ill_lock);
6110 		for (ipif = ill->ill_ipif; ipif != NULL;
6111 		    ipif = ipif->ipif_next) {
6112 			/* Allow the ipif to be down */
6113 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
6114 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
6115 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
6116 			    (ipif->ipif_pp_dst_addr == addr)) &&
6117 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
6118 				zoneid = ipif->ipif_zoneid;
6119 				mutex_exit(&ill->ill_lock);
6120 				rw_exit(&ipst->ips_ill_g_lock);
6121 				/*
6122 				 * If ipif_zoneid was ALL_ZONES then we have
6123 				 * a trusted extensions shared IP address.
6124 				 * In that case GLOBAL_ZONEID works to send.
6125 				 */
6126 				if (zoneid == ALL_ZONES)
6127 					zoneid = GLOBAL_ZONEID;
6128 				return (zoneid);
6129 			}
6130 		}
6131 		mutex_exit(&ill->ill_lock);
6132 	}
6133 
6134 	/* If we already did the ptp case, then we are done */
6135 	if (ptp) {
6136 		rw_exit(&ipst->ips_ill_g_lock);
6137 		return (ALL_ZONES);
6138 	}
6139 	ptp = B_TRUE;
6140 	goto repeat;
6141 }
6142 
6143 /*
6144  * Look for an ipif that matches the specified remote address i.e. the
6145  * ipif that would receive the specified packet.
6146  * First look for directly connected interfaces and then do a recursive
6147  * IRE lookup and pick the first ipif corresponding to the source address in the
6148  * ire.
6149  * Returns: held ipif
6150  */
6151 ipif_t *
6152 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
6153 {
6154 	ipif_t	*ipif;
6155 	ire_t	*ire;
6156 	ip_stack_t	*ipst = ill->ill_ipst;
6157 
6158 	ASSERT(!ill->ill_isv6);
6159 
6160 	/*
6161 	 * Someone could be changing this ipif currently or change it
6162 	 * after we return this. Thus  a few packets could use the old
6163 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
6164 	 * will atomically be updated or cleaned up with the new value
6165 	 * Thus we don't need a lock to check the flags or other attrs below.
6166 	 */
6167 	mutex_enter(&ill->ill_lock);
6168 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6169 		if (!IPIF_CAN_LOOKUP(ipif))
6170 			continue;
6171 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
6172 		    ipif->ipif_zoneid != ALL_ZONES)
6173 			continue;
6174 		/* Allow the ipif to be down */
6175 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
6176 			if ((ipif->ipif_pp_dst_addr == addr) ||
6177 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
6178 			    ipif->ipif_lcl_addr == addr)) {
6179 				ipif_refhold_locked(ipif);
6180 				mutex_exit(&ill->ill_lock);
6181 				return (ipif);
6182 			}
6183 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
6184 			ipif_refhold_locked(ipif);
6185 			mutex_exit(&ill->ill_lock);
6186 			return (ipif);
6187 		}
6188 	}
6189 	mutex_exit(&ill->ill_lock);
6190 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
6191 	    NULL, MATCH_IRE_RECURSIVE, ipst);
6192 	if (ire != NULL) {
6193 		/*
6194 		 * The callers of this function wants to know the
6195 		 * interface on which they have to send the replies
6196 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
6197 		 * derived from different ills, we really don't care
6198 		 * what we return here.
6199 		 */
6200 		ipif = ire->ire_ipif;
6201 		if (ipif != NULL) {
6202 			ipif_refhold(ipif);
6203 			ire_refrele(ire);
6204 			return (ipif);
6205 		}
6206 		ire_refrele(ire);
6207 	}
6208 	/* Pick the first interface */
6209 	ipif = ipif_get_next_ipif(NULL, ill);
6210 	return (ipif);
6211 }
6212 
6213 /*
6214  * This func does not prevent refcnt from increasing. But if
6215  * the caller has taken steps to that effect, then this func
6216  * can be used to determine whether the ill has become quiescent
6217  */
6218 static boolean_t
6219 ill_is_quiescent(ill_t *ill)
6220 {
6221 	ipif_t	*ipif;
6222 
6223 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6224 
6225 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6226 		if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6227 			return (B_FALSE);
6228 		}
6229 	}
6230 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6231 		return (B_FALSE);
6232 	}
6233 	return (B_TRUE);
6234 }
6235 
6236 boolean_t
6237 ill_is_freeable(ill_t *ill)
6238 {
6239 	ipif_t	*ipif;
6240 
6241 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6242 
6243 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6244 		if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6245 			return (B_FALSE);
6246 		}
6247 	}
6248 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6249 		return (B_FALSE);
6250 	}
6251 	return (B_TRUE);
6252 }
6253 
6254 /*
6255  * This func does not prevent refcnt from increasing. But if
6256  * the caller has taken steps to that effect, then this func
6257  * can be used to determine whether the ipif has become quiescent
6258  */
6259 static boolean_t
6260 ipif_is_quiescent(ipif_t *ipif)
6261 {
6262 	ill_t *ill;
6263 
6264 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6265 
6266 	if (ipif->ipif_refcnt != 0 || !IPIF_DOWN_OK(ipif)) {
6267 		return (B_FALSE);
6268 	}
6269 
6270 	ill = ipif->ipif_ill;
6271 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6272 	    ill->ill_logical_down) {
6273 		return (B_TRUE);
6274 	}
6275 
6276 	/* This is the last ipif going down or being deleted on this ill */
6277 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
6278 		return (B_FALSE);
6279 	}
6280 
6281 	return (B_TRUE);
6282 }
6283 
6284 /*
6285  * return true if the ipif can be destroyed: the ipif has to be quiescent
6286  * with zero references from ire/nce/ilm to it.
6287  */
6288 static boolean_t
6289 ipif_is_freeable(ipif_t *ipif)
6290 {
6291 
6292 	ill_t *ill;
6293 
6294 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6295 
6296 	if (ipif->ipif_refcnt != 0 || !IPIF_FREE_OK(ipif)) {
6297 		return (B_FALSE);
6298 	}
6299 
6300 	ill = ipif->ipif_ill;
6301 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
6302 	    ill->ill_logical_down) {
6303 		return (B_TRUE);
6304 	}
6305 
6306 	/* This is the last ipif going down or being deleted on this ill */
6307 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
6308 		return (B_FALSE);
6309 	}
6310 
6311 	return (B_TRUE);
6312 }
6313 
6314 /*
6315  * This func does not prevent refcnt from increasing. But if
6316  * the caller has taken steps to that effect, then this func
6317  * can be used to determine whether the ipifs marked with IPIF_MOVING
6318  * have become quiescent and can be moved in a failover/failback.
6319  */
6320 static ipif_t *
6321 ill_quiescent_to_move(ill_t *ill)
6322 {
6323 	ipif_t  *ipif;
6324 
6325 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6326 
6327 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
6328 		if (ipif->ipif_state_flags & IPIF_MOVING) {
6329 			if (ipif->ipif_refcnt != 0 ||
6330 			    !IPIF_DOWN_OK(ipif)) {
6331 				return (ipif);
6332 			}
6333 		}
6334 	}
6335 	return (NULL);
6336 }
6337 
6338 /*
6339  * The ipif/ill/ire has been refreled. Do the tail processing.
6340  * Determine if the ipif or ill in question has become quiescent and if so
6341  * wakeup close and/or restart any queued pending ioctl that is waiting
6342  * for the ipif_down (or ill_down)
6343  */
6344 void
6345 ipif_ill_refrele_tail(ill_t *ill)
6346 {
6347 	mblk_t	*mp;
6348 	conn_t	*connp;
6349 	ipsq_t	*ipsq;
6350 	ipif_t	*ipif;
6351 	dl_notify_ind_t *dlindp;
6352 
6353 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6354 
6355 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
6356 	    ill_is_freeable(ill)) {
6357 		/* ill_close may be waiting */
6358 		cv_broadcast(&ill->ill_cv);
6359 	}
6360 
6361 	/* ipsq can't change because ill_lock  is held */
6362 	ipsq = ill->ill_phyint->phyint_ipsq;
6363 	if (ipsq->ipsq_waitfor == 0) {
6364 		/* Not waiting for anything, just return. */
6365 		mutex_exit(&ill->ill_lock);
6366 		return;
6367 	}
6368 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
6369 	    ipsq->ipsq_pending_ipif != NULL);
6370 	/*
6371 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
6372 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
6373 	 * be zero for restarting an ioctl that ends up downing the ill.
6374 	 */
6375 	ipif = ipsq->ipsq_pending_ipif;
6376 	if (ipif->ipif_ill != ill) {
6377 		/* The ioctl is pending on some other ill. */
6378 		mutex_exit(&ill->ill_lock);
6379 		return;
6380 	}
6381 
6382 	switch (ipsq->ipsq_waitfor) {
6383 	case IPIF_DOWN:
6384 		if (!ipif_is_quiescent(ipif)) {
6385 			mutex_exit(&ill->ill_lock);
6386 			return;
6387 		}
6388 		break;
6389 	case IPIF_FREE:
6390 		if (!ipif_is_freeable(ipif)) {
6391 			mutex_exit(&ill->ill_lock);
6392 			return;
6393 		}
6394 		break;
6395 
6396 	case ILL_DOWN:
6397 		if (!ill_is_quiescent(ill)) {
6398 			mutex_exit(&ill->ill_lock);
6399 			return;
6400 		}
6401 		break;
6402 	case ILL_FREE:
6403 		/*
6404 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
6405 		 * waits synchronously in ip_close, and no message is queued in
6406 		 * ipsq_pending_mp at all in this case
6407 		 */
6408 		if (!ill_is_freeable(ill)) {
6409 			mutex_exit(&ill->ill_lock);
6410 			return;
6411 		}
6412 		break;
6413 
6414 	case ILL_MOVE_OK:
6415 		if (ill_quiescent_to_move(ill) != NULL) {
6416 			mutex_exit(&ill->ill_lock);
6417 			return;
6418 		}
6419 		break;
6420 	default:
6421 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
6422 		    (void *)ipsq, ipsq->ipsq_waitfor);
6423 	}
6424 
6425 	/*
6426 	 * Incr refcnt for the qwriter_ip call below which
6427 	 * does a refrele
6428 	 */
6429 	ill_refhold_locked(ill);
6430 	mp = ipsq_pending_mp_get(ipsq, &connp);
6431 	mutex_exit(&ill->ill_lock);
6432 
6433 	ASSERT(mp != NULL);
6434 	/*
6435 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
6436 	 * we can only get here when the current operation decides it
6437 	 * it needs to quiesce via ipsq_pending_mp_add().
6438 	 */
6439 	switch (mp->b_datap->db_type) {
6440 	case M_PCPROTO:
6441 	case M_PROTO:
6442 		/*
6443 		 * For now, only DL_NOTIFY_IND messages can use this facility.
6444 		 */
6445 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
6446 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
6447 
6448 		switch (dlindp->dl_notification) {
6449 		case DL_NOTE_PHYS_ADDR:
6450 			qwriter_ip(ill, ill->ill_rq, mp,
6451 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
6452 			return;
6453 		default:
6454 			ASSERT(0);
6455 		}
6456 		break;
6457 
6458 	case M_ERROR:
6459 	case M_HANGUP:
6460 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
6461 		    B_TRUE);
6462 		return;
6463 
6464 	case M_IOCTL:
6465 	case M_IOCDATA:
6466 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
6467 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
6468 		return;
6469 
6470 	default:
6471 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
6472 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
6473 	}
6474 }
6475 
6476 #ifdef DEBUG
6477 /* Reuse trace buffer from beginning (if reached the end) and record trace */
6478 static void
6479 th_trace_rrecord(th_trace_t *th_trace)
6480 {
6481 	tr_buf_t *tr_buf;
6482 	uint_t lastref;
6483 
6484 	lastref = th_trace->th_trace_lastref;
6485 	lastref++;
6486 	if (lastref == TR_BUF_MAX)
6487 		lastref = 0;
6488 	th_trace->th_trace_lastref = lastref;
6489 	tr_buf = &th_trace->th_trbuf[lastref];
6490 	tr_buf->tr_time = lbolt;
6491 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
6492 }
6493 
6494 static void
6495 th_trace_free(void *value)
6496 {
6497 	th_trace_t *th_trace = value;
6498 
6499 	ASSERT(th_trace->th_refcnt == 0);
6500 	kmem_free(th_trace, sizeof (*th_trace));
6501 }
6502 
6503 /*
6504  * Find or create the per-thread hash table used to track object references.
6505  * The ipst argument is NULL if we shouldn't allocate.
6506  *
6507  * Accesses per-thread data, so there's no need to lock here.
6508  */
6509 static mod_hash_t *
6510 th_trace_gethash(ip_stack_t *ipst)
6511 {
6512 	th_hash_t *thh;
6513 
6514 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
6515 		mod_hash_t *mh;
6516 		char name[256];
6517 		size_t objsize, rshift;
6518 		int retv;
6519 
6520 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
6521 			return (NULL);
6522 		(void) snprintf(name, sizeof (name), "th_trace_%p", curthread);
6523 
6524 		/*
6525 		 * We use mod_hash_create_extended here rather than the more
6526 		 * obvious mod_hash_create_ptrhash because the latter has a
6527 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
6528 		 * block.
6529 		 */
6530 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
6531 		    MAX(sizeof (ire_t), sizeof (nce_t)));
6532 		rshift = highbit(objsize);
6533 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
6534 		    th_trace_free, mod_hash_byptr, (void *)rshift,
6535 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
6536 		if (mh == NULL) {
6537 			kmem_free(thh, sizeof (*thh));
6538 			return (NULL);
6539 		}
6540 		thh->thh_hash = mh;
6541 		thh->thh_ipst = ipst;
6542 		/*
6543 		 * We trace ills, ipifs, ires, and nces.  All of these are
6544 		 * per-IP-stack, so the lock on the thread list is as well.
6545 		 */
6546 		rw_enter(&ip_thread_rwlock, RW_WRITER);
6547 		list_insert_tail(&ip_thread_list, thh);
6548 		rw_exit(&ip_thread_rwlock);
6549 		retv = tsd_set(ip_thread_data, thh);
6550 		ASSERT(retv == 0);
6551 	}
6552 	return (thh != NULL ? thh->thh_hash : NULL);
6553 }
6554 
6555 boolean_t
6556 th_trace_ref(const void *obj, ip_stack_t *ipst)
6557 {
6558 	th_trace_t *th_trace;
6559 	mod_hash_t *mh;
6560 	mod_hash_val_t val;
6561 
6562 	if ((mh = th_trace_gethash(ipst)) == NULL)
6563 		return (B_FALSE);
6564 
6565 	/*
6566 	 * Attempt to locate the trace buffer for this obj and thread.
6567 	 * If it does not exist, then allocate a new trace buffer and
6568 	 * insert into the hash.
6569 	 */
6570 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
6571 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
6572 		if (th_trace == NULL)
6573 			return (B_FALSE);
6574 
6575 		th_trace->th_id = curthread;
6576 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
6577 		    (mod_hash_val_t)th_trace) != 0) {
6578 			kmem_free(th_trace, sizeof (th_trace_t));
6579 			return (B_FALSE);
6580 		}
6581 	} else {
6582 		th_trace = (th_trace_t *)val;
6583 	}
6584 
6585 	ASSERT(th_trace->th_refcnt >= 0 &&
6586 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
6587 
6588 	th_trace->th_refcnt++;
6589 	th_trace_rrecord(th_trace);
6590 	return (B_TRUE);
6591 }
6592 
6593 /*
6594  * For the purpose of tracing a reference release, we assume that global
6595  * tracing is always on and that the same thread initiated the reference hold
6596  * is releasing.
6597  */
6598 void
6599 th_trace_unref(const void *obj)
6600 {
6601 	int retv;
6602 	mod_hash_t *mh;
6603 	th_trace_t *th_trace;
6604 	mod_hash_val_t val;
6605 
6606 	mh = th_trace_gethash(NULL);
6607 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
6608 	ASSERT(retv == 0);
6609 	th_trace = (th_trace_t *)val;
6610 
6611 	ASSERT(th_trace->th_refcnt > 0);
6612 	th_trace->th_refcnt--;
6613 	th_trace_rrecord(th_trace);
6614 }
6615 
6616 /*
6617  * If tracing has been disabled, then we assume that the reference counts are
6618  * now useless, and we clear them out before destroying the entries.
6619  */
6620 void
6621 th_trace_cleanup(const void *obj, boolean_t trace_disable)
6622 {
6623 	th_hash_t	*thh;
6624 	mod_hash_t	*mh;
6625 	mod_hash_val_t	val;
6626 	th_trace_t	*th_trace;
6627 	int		retv;
6628 
6629 	rw_enter(&ip_thread_rwlock, RW_READER);
6630 	for (thh = list_head(&ip_thread_list); thh != NULL;
6631 	    thh = list_next(&ip_thread_list, thh)) {
6632 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
6633 		    &val) == 0) {
6634 			th_trace = (th_trace_t *)val;
6635 			if (trace_disable)
6636 				th_trace->th_refcnt = 0;
6637 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
6638 			ASSERT(retv == 0);
6639 		}
6640 	}
6641 	rw_exit(&ip_thread_rwlock);
6642 }
6643 
6644 void
6645 ipif_trace_ref(ipif_t *ipif)
6646 {
6647 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6648 
6649 	if (ipif->ipif_trace_disable)
6650 		return;
6651 
6652 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
6653 		ipif->ipif_trace_disable = B_TRUE;
6654 		ipif_trace_cleanup(ipif);
6655 	}
6656 }
6657 
6658 void
6659 ipif_untrace_ref(ipif_t *ipif)
6660 {
6661 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6662 
6663 	if (!ipif->ipif_trace_disable)
6664 		th_trace_unref(ipif);
6665 }
6666 
6667 void
6668 ill_trace_ref(ill_t *ill)
6669 {
6670 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6671 
6672 	if (ill->ill_trace_disable)
6673 		return;
6674 
6675 	if (!th_trace_ref(ill, ill->ill_ipst)) {
6676 		ill->ill_trace_disable = B_TRUE;
6677 		ill_trace_cleanup(ill);
6678 	}
6679 }
6680 
6681 void
6682 ill_untrace_ref(ill_t *ill)
6683 {
6684 	ASSERT(MUTEX_HELD(&ill->ill_lock));
6685 
6686 	if (!ill->ill_trace_disable)
6687 		th_trace_unref(ill);
6688 }
6689 
6690 /*
6691  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
6692  * failure, ipif_trace_disable is set.
6693  */
6694 static void
6695 ipif_trace_cleanup(const ipif_t *ipif)
6696 {
6697 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
6698 }
6699 
6700 /*
6701  * Called when ill is unplumbed or when memory alloc fails.  Note that on
6702  * failure, ill_trace_disable is set.
6703  */
6704 static void
6705 ill_trace_cleanup(const ill_t *ill)
6706 {
6707 	th_trace_cleanup(ill, ill->ill_trace_disable);
6708 }
6709 #endif /* DEBUG */
6710 
6711 void
6712 ipif_refhold_locked(ipif_t *ipif)
6713 {
6714 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6715 	ipif->ipif_refcnt++;
6716 	IPIF_TRACE_REF(ipif);
6717 }
6718 
6719 void
6720 ipif_refhold(ipif_t *ipif)
6721 {
6722 	ill_t	*ill;
6723 
6724 	ill = ipif->ipif_ill;
6725 	mutex_enter(&ill->ill_lock);
6726 	ipif->ipif_refcnt++;
6727 	IPIF_TRACE_REF(ipif);
6728 	mutex_exit(&ill->ill_lock);
6729 }
6730 
6731 /*
6732  * Must not be called while holding any locks. Otherwise if this is
6733  * the last reference to be released there is a chance of recursive mutex
6734  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6735  * to restart an ioctl.
6736  */
6737 void
6738 ipif_refrele(ipif_t *ipif)
6739 {
6740 	ill_t	*ill;
6741 
6742 	ill = ipif->ipif_ill;
6743 
6744 	mutex_enter(&ill->ill_lock);
6745 	ASSERT(ipif->ipif_refcnt != 0);
6746 	ipif->ipif_refcnt--;
6747 	IPIF_UNTRACE_REF(ipif);
6748 	if (ipif->ipif_refcnt != 0) {
6749 		mutex_exit(&ill->ill_lock);
6750 		return;
6751 	}
6752 
6753 	/* Drops the ill_lock */
6754 	ipif_ill_refrele_tail(ill);
6755 }
6756 
6757 ipif_t *
6758 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6759 {
6760 	ipif_t	*ipif;
6761 
6762 	mutex_enter(&ill->ill_lock);
6763 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6764 	    ipif != NULL; ipif = ipif->ipif_next) {
6765 		if (!IPIF_CAN_LOOKUP(ipif))
6766 			continue;
6767 		ipif_refhold_locked(ipif);
6768 		mutex_exit(&ill->ill_lock);
6769 		return (ipif);
6770 	}
6771 	mutex_exit(&ill->ill_lock);
6772 	return (NULL);
6773 }
6774 
6775 /*
6776  * TODO: make this table extendible at run time
6777  * Return a pointer to the mac type info for 'mac_type'
6778  */
6779 static ip_m_t *
6780 ip_m_lookup(t_uscalar_t mac_type)
6781 {
6782 	ip_m_t	*ipm;
6783 
6784 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6785 		if (ipm->ip_m_mac_type == mac_type)
6786 			return (ipm);
6787 	return (NULL);
6788 }
6789 
6790 /*
6791  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6792  * ipif_arg is passed in to associate it with the correct interface.
6793  * We may need to restart this operation if the ipif cannot be looked up
6794  * due to an exclusive operation that is currently in progress. The restart
6795  * entry point is specified by 'func'
6796  */
6797 int
6798 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6799     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ire_t **ire_arg,
6800     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func,
6801     struct rtsa_s *sp, ip_stack_t *ipst)
6802 {
6803 	ire_t	*ire;
6804 	ire_t	*gw_ire = NULL;
6805 	ipif_t	*ipif = NULL;
6806 	boolean_t ipif_refheld = B_FALSE;
6807 	uint_t	type;
6808 	int	match_flags = MATCH_IRE_TYPE;
6809 	int	error;
6810 	tsol_gc_t *gc = NULL;
6811 	tsol_gcgrp_t *gcgrp = NULL;
6812 	boolean_t gcgrp_xtraref = B_FALSE;
6813 
6814 	ip1dbg(("ip_rt_add:"));
6815 
6816 	if (ire_arg != NULL)
6817 		*ire_arg = NULL;
6818 
6819 	/*
6820 	 * If this is the case of RTF_HOST being set, then we set the netmask
6821 	 * to all ones (regardless if one was supplied).
6822 	 */
6823 	if (flags & RTF_HOST)
6824 		mask = IP_HOST_MASK;
6825 
6826 	/*
6827 	 * Prevent routes with a zero gateway from being created (since
6828 	 * interfaces can currently be plumbed and brought up no assigned
6829 	 * address).
6830 	 */
6831 	if (gw_addr == 0)
6832 		return (ENETUNREACH);
6833 	/*
6834 	 * Get the ipif, if any, corresponding to the gw_addr
6835 	 */
6836 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &error,
6837 	    ipst);
6838 	if (ipif != NULL) {
6839 		if (IS_VNI(ipif->ipif_ill)) {
6840 			ipif_refrele(ipif);
6841 			return (EINVAL);
6842 		}
6843 		ipif_refheld = B_TRUE;
6844 	} else if (error == EINPROGRESS) {
6845 		ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6846 		return (EINPROGRESS);
6847 	} else {
6848 		error = 0;
6849 	}
6850 
6851 	if (ipif != NULL) {
6852 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6853 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6854 	} else {
6855 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6856 	}
6857 
6858 	/*
6859 	 * GateD will attempt to create routes with a loopback interface
6860 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6861 	 * these routes to be added, but create them as interface routes
6862 	 * since the gateway is an interface address.
6863 	 */
6864 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
6865 		flags &= ~RTF_GATEWAY;
6866 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6867 		    mask == IP_HOST_MASK) {
6868 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6869 			    ALL_ZONES, NULL, match_flags, ipst);
6870 			if (ire != NULL) {
6871 				ire_refrele(ire);
6872 				if (ipif_refheld)
6873 					ipif_refrele(ipif);
6874 				return (EEXIST);
6875 			}
6876 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6877 			    "for 0x%x\n", (void *)ipif,
6878 			    ipif->ipif_ire_type,
6879 			    ntohl(ipif->ipif_lcl_addr)));
6880 			ire = ire_create(
6881 			    (uchar_t *)&dst_addr,	/* dest address */
6882 			    (uchar_t *)&mask,		/* mask */
6883 			    (uchar_t *)&ipif->ipif_src_addr,
6884 			    NULL,			/* no gateway */
6885 			    &ipif->ipif_mtu,
6886 			    NULL,
6887 			    ipif->ipif_rq,		/* recv-from queue */
6888 			    NULL,			/* no send-to queue */
6889 			    ipif->ipif_ire_type,	/* LOOPBACK */
6890 			    ipif,
6891 			    0,
6892 			    0,
6893 			    0,
6894 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6895 			    RTF_PRIVATE : 0,
6896 			    &ire_uinfo_null,
6897 			    NULL,
6898 			    NULL,
6899 			    ipst);
6900 
6901 			if (ire == NULL) {
6902 				if (ipif_refheld)
6903 					ipif_refrele(ipif);
6904 				return (ENOMEM);
6905 			}
6906 			error = ire_add(&ire, q, mp, func, B_FALSE);
6907 			if (error == 0)
6908 				goto save_ire;
6909 			if (ipif_refheld)
6910 				ipif_refrele(ipif);
6911 			return (error);
6912 
6913 		}
6914 	}
6915 
6916 	/*
6917 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6918 	 * and the gateway address provided is one of the system's interface
6919 	 * addresses.  By using the routing socket interface and supplying an
6920 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6921 	 * specifying an interface route to be created is available which uses
6922 	 * the interface index that specifies the outgoing interface rather than
6923 	 * the address of an outgoing interface (which may not be able to
6924 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6925 	 * flag, routes can be specified which not only specify the next-hop to
6926 	 * be used when routing to a certain prefix, but also which outgoing
6927 	 * interface should be used.
6928 	 *
6929 	 * Previously, interfaces would have unique addresses assigned to them
6930 	 * and so the address assigned to a particular interface could be used
6931 	 * to identify a particular interface.  One exception to this was the
6932 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6933 	 *
6934 	 * With the advent of IPv6 and its link-local addresses, this
6935 	 * restriction was relaxed and interfaces could share addresses between
6936 	 * themselves.  In fact, typically all of the link-local interfaces on
6937 	 * an IPv6 node or router will have the same link-local address.  In
6938 	 * order to differentiate between these interfaces, the use of an
6939 	 * interface index is necessary and this index can be carried inside a
6940 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6941 	 * of using the interface index, however, is that all of the ipif's that
6942 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6943 	 * cannot be used to differentiate between ipif's (or logical
6944 	 * interfaces) that belong to the same ill (physical interface).
6945 	 *
6946 	 * For example, in the following case involving IPv4 interfaces and
6947 	 * logical interfaces
6948 	 *
6949 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6950 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6951 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6952 	 *
6953 	 * the ipif's corresponding to each of these interface routes can be
6954 	 * uniquely identified by the "gateway" (actually interface address).
6955 	 *
6956 	 * In this case involving multiple IPv6 default routes to a particular
6957 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6958 	 * default route is of interest:
6959 	 *
6960 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6961 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6962 	 */
6963 
6964 	/* RTF_GATEWAY not set */
6965 	if (!(flags & RTF_GATEWAY)) {
6966 		queue_t	*stq;
6967 
6968 		if (sp != NULL) {
6969 			ip2dbg(("ip_rt_add: gateway security attributes "
6970 			    "cannot be set with interface route\n"));
6971 			if (ipif_refheld)
6972 				ipif_refrele(ipif);
6973 			return (EINVAL);
6974 		}
6975 
6976 		/*
6977 		 * As the interface index specified with the RTA_IFP sockaddr is
6978 		 * the same for all ipif's off of an ill, the matching logic
6979 		 * below uses MATCH_IRE_ILL if such an index was specified.
6980 		 * This means that routes sharing the same prefix when added
6981 		 * using a RTA_IFP sockaddr must have distinct interface
6982 		 * indices (namely, they must be on distinct ill's).
6983 		 *
6984 		 * On the other hand, since the gateway address will usually be
6985 		 * different for each ipif on the system, the matching logic
6986 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6987 		 * route.  This means that interface routes for the same prefix
6988 		 * can be created if they belong to distinct ipif's and if a
6989 		 * RTA_IFP sockaddr is not present.
6990 		 */
6991 		if (ipif_arg != NULL) {
6992 			if (ipif_refheld)  {
6993 				ipif_refrele(ipif);
6994 				ipif_refheld = B_FALSE;
6995 			}
6996 			ipif = ipif_arg;
6997 			match_flags |= MATCH_IRE_ILL;
6998 		} else {
6999 			/*
7000 			 * Check the ipif corresponding to the gw_addr
7001 			 */
7002 			if (ipif == NULL)
7003 				return (ENETUNREACH);
7004 			match_flags |= MATCH_IRE_IPIF;
7005 		}
7006 		ASSERT(ipif != NULL);
7007 
7008 		/*
7009 		 * We check for an existing entry at this point.
7010 		 *
7011 		 * Since a netmask isn't passed in via the ioctl interface
7012 		 * (SIOCADDRT), we don't check for a matching netmask in that
7013 		 * case.
7014 		 */
7015 		if (!ioctl_msg)
7016 			match_flags |= MATCH_IRE_MASK;
7017 		ire = ire_ftable_lookup(dst_addr, mask, 0, IRE_INTERFACE, ipif,
7018 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7019 		if (ire != NULL) {
7020 			ire_refrele(ire);
7021 			if (ipif_refheld)
7022 				ipif_refrele(ipif);
7023 			return (EEXIST);
7024 		}
7025 
7026 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
7027 		    ? ipif->ipif_rq : ipif->ipif_wq;
7028 
7029 		/*
7030 		 * Create a copy of the IRE_LOOPBACK,
7031 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
7032 		 * the modified address and netmask.
7033 		 */
7034 		ire = ire_create(
7035 		    (uchar_t *)&dst_addr,
7036 		    (uint8_t *)&mask,
7037 		    (uint8_t *)&ipif->ipif_src_addr,
7038 		    NULL,
7039 		    &ipif->ipif_mtu,
7040 		    NULL,
7041 		    NULL,
7042 		    stq,
7043 		    ipif->ipif_net_type,
7044 		    ipif,
7045 		    0,
7046 		    0,
7047 		    0,
7048 		    flags,
7049 		    &ire_uinfo_null,
7050 		    NULL,
7051 		    NULL,
7052 		    ipst);
7053 		if (ire == NULL) {
7054 			if (ipif_refheld)
7055 				ipif_refrele(ipif);
7056 			return (ENOMEM);
7057 		}
7058 
7059 		/*
7060 		 * Some software (for example, GateD and Sun Cluster) attempts
7061 		 * to create (what amount to) IRE_PREFIX routes with the
7062 		 * loopback address as the gateway.  This is primarily done to
7063 		 * set up prefixes with the RTF_REJECT flag set (for example,
7064 		 * when generating aggregate routes.)
7065 		 *
7066 		 * If the IRE type (as defined by ipif->ipif_net_type) is
7067 		 * IRE_LOOPBACK, then we map the request into a
7068 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
7069 		 * these interface routes, by definition, can only be that.
7070 		 *
7071 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
7072 		 * routine, but rather using ire_create() directly.
7073 		 *
7074 		 */
7075 		if (ipif->ipif_net_type == IRE_LOOPBACK) {
7076 			ire->ire_type = IRE_IF_NORESOLVER;
7077 			ire->ire_flags |= RTF_BLACKHOLE;
7078 		}
7079 
7080 		error = ire_add(&ire, q, mp, func, B_FALSE);
7081 		if (error == 0)
7082 			goto save_ire;
7083 
7084 		/*
7085 		 * In the result of failure, ire_add() will have already
7086 		 * deleted the ire in question, so there is no need to
7087 		 * do that here.
7088 		 */
7089 		if (ipif_refheld)
7090 			ipif_refrele(ipif);
7091 		return (error);
7092 	}
7093 	if (ipif_refheld) {
7094 		ipif_refrele(ipif);
7095 		ipif_refheld = B_FALSE;
7096 	}
7097 
7098 	/*
7099 	 * Get an interface IRE for the specified gateway.
7100 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
7101 	 * gateway, it is currently unreachable and we fail the request
7102 	 * accordingly.
7103 	 */
7104 	ipif = ipif_arg;
7105 	if (ipif_arg != NULL)
7106 		match_flags |= MATCH_IRE_ILL;
7107 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
7108 	    ALL_ZONES, 0, NULL, match_flags, ipst);
7109 	if (gw_ire == NULL)
7110 		return (ENETUNREACH);
7111 
7112 	/*
7113 	 * We create one of three types of IREs as a result of this request
7114 	 * based on the netmask.  A netmask of all ones (which is automatically
7115 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
7116 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
7117 	 * created.  Otherwise, an IRE_PREFIX route is created for the
7118 	 * destination prefix.
7119 	 */
7120 	if (mask == IP_HOST_MASK)
7121 		type = IRE_HOST;
7122 	else if (mask == 0)
7123 		type = IRE_DEFAULT;
7124 	else
7125 		type = IRE_PREFIX;
7126 
7127 	/* check for a duplicate entry */
7128 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7129 	    NULL, ALL_ZONES, 0, NULL,
7130 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst);
7131 	if (ire != NULL) {
7132 		ire_refrele(gw_ire);
7133 		ire_refrele(ire);
7134 		return (EEXIST);
7135 	}
7136 
7137 	/* Security attribute exists */
7138 	if (sp != NULL) {
7139 		tsol_gcgrp_addr_t ga;
7140 
7141 		/* find or create the gateway credentials group */
7142 		ga.ga_af = AF_INET;
7143 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
7144 
7145 		/* we hold reference to it upon success */
7146 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
7147 		if (gcgrp == NULL) {
7148 			ire_refrele(gw_ire);
7149 			return (ENOMEM);
7150 		}
7151 
7152 		/*
7153 		 * Create and add the security attribute to the group; a
7154 		 * reference to the group is made upon allocating a new
7155 		 * entry successfully.  If it finds an already-existing
7156 		 * entry for the security attribute in the group, it simply
7157 		 * returns it and no new reference is made to the group.
7158 		 */
7159 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
7160 		if (gc == NULL) {
7161 			/* release reference held by gcgrp_lookup */
7162 			GCGRP_REFRELE(gcgrp);
7163 			ire_refrele(gw_ire);
7164 			return (ENOMEM);
7165 		}
7166 	}
7167 
7168 	/* Create the IRE. */
7169 	ire = ire_create(
7170 	    (uchar_t *)&dst_addr,		/* dest address */
7171 	    (uchar_t *)&mask,			/* mask */
7172 	    /* src address assigned by the caller? */
7173 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
7174 	    (flags & RTF_SETSRC)) ?  &src_addr : NULL),
7175 	    (uchar_t *)&gw_addr,		/* gateway address */
7176 	    &gw_ire->ire_max_frag,
7177 	    NULL,				/* no src nce */
7178 	    NULL,				/* no recv-from queue */
7179 	    NULL,				/* no send-to queue */
7180 	    (ushort_t)type,			/* IRE type */
7181 	    ipif_arg,
7182 	    0,
7183 	    0,
7184 	    0,
7185 	    flags,
7186 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
7187 	    gc,					/* security attribute */
7188 	    NULL,
7189 	    ipst);
7190 
7191 	/*
7192 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
7193 	 * reference to the 'gcgrp'. We can now release the extra reference
7194 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
7195 	 */
7196 	if (gcgrp_xtraref)
7197 		GCGRP_REFRELE(gcgrp);
7198 	if (ire == NULL) {
7199 		if (gc != NULL)
7200 			GC_REFRELE(gc);
7201 		ire_refrele(gw_ire);
7202 		return (ENOMEM);
7203 	}
7204 
7205 	/*
7206 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
7207 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
7208 	 */
7209 
7210 	/* Add the new IRE. */
7211 	error = ire_add(&ire, q, mp, func, B_FALSE);
7212 	if (error != 0) {
7213 		/*
7214 		 * In the result of failure, ire_add() will have already
7215 		 * deleted the ire in question, so there is no need to
7216 		 * do that here.
7217 		 */
7218 		ire_refrele(gw_ire);
7219 		return (error);
7220 	}
7221 
7222 	if (flags & RTF_MULTIRT) {
7223 		/*
7224 		 * Invoke the CGTP (multirouting) filtering module
7225 		 * to add the dst address in the filtering database.
7226 		 * Replicated inbound packets coming from that address
7227 		 * will be filtered to discard the duplicates.
7228 		 * It is not necessary to call the CGTP filter hook
7229 		 * when the dst address is a broadcast or multicast,
7230 		 * because an IP source address cannot be a broadcast
7231 		 * or a multicast.
7232 		 */
7233 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
7234 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7235 		if (ire_dst != NULL) {
7236 			ip_cgtp_bcast_add(ire, ire_dst, ipst);
7237 			ire_refrele(ire_dst);
7238 			goto save_ire;
7239 		}
7240 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
7241 		    !CLASSD(ire->ire_addr)) {
7242 			int res = ipst->ips_ip_cgtp_filter_ops->cfo_add_dest_v4(
7243 			    ipst->ips_netstack->netstack_stackid,
7244 			    ire->ire_addr,
7245 			    ire->ire_gateway_addr,
7246 			    ire->ire_src_addr,
7247 			    gw_ire->ire_src_addr);
7248 			if (res != 0) {
7249 				ire_refrele(gw_ire);
7250 				ire_delete(ire);
7251 				return (res);
7252 			}
7253 		}
7254 	}
7255 
7256 	/*
7257 	 * Now that the prefix IRE entry has been created, delete any
7258 	 * existing gateway IRE cache entries as well as any IRE caches
7259 	 * using the gateway, and force them to be created through
7260 	 * ip_newroute.
7261 	 */
7262 	if (gc != NULL) {
7263 		ASSERT(gcgrp != NULL);
7264 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst);
7265 	}
7266 
7267 save_ire:
7268 	if (gw_ire != NULL) {
7269 		ire_refrele(gw_ire);
7270 	}
7271 	if (ipif != NULL) {
7272 		/*
7273 		 * Save enough information so that we can recreate the IRE if
7274 		 * the interface goes down and then up.  The metrics associated
7275 		 * with the route will be saved as well when rts_setmetrics() is
7276 		 * called after the IRE has been created.  In the case where
7277 		 * memory cannot be allocated, none of this information will be
7278 		 * saved.
7279 		 */
7280 		ipif_save_ire(ipif, ire);
7281 	}
7282 	if (ioctl_msg)
7283 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
7284 	if (ire_arg != NULL) {
7285 		/*
7286 		 * Store the ire that was successfully added into where ire_arg
7287 		 * points to so that callers don't have to look it up
7288 		 * themselves (but they are responsible for ire_refrele()ing
7289 		 * the ire when they are finished with it).
7290 		 */
7291 		*ire_arg = ire;
7292 	} else {
7293 		ire_refrele(ire);		/* Held in ire_add */
7294 	}
7295 	if (ipif_refheld)
7296 		ipif_refrele(ipif);
7297 	return (0);
7298 }
7299 
7300 /*
7301  * ip_rt_delete is called to delete an IPv4 route.
7302  * ipif_arg is passed in to associate it with the correct interface.
7303  * We may need to restart this operation if the ipif cannot be looked up
7304  * due to an exclusive operation that is currently in progress. The restart
7305  * entry point is specified by 'func'
7306  */
7307 /* ARGSUSED4 */
7308 int
7309 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
7310     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, boolean_t ioctl_msg,
7311     queue_t *q, mblk_t *mp, ipsq_func_t func, ip_stack_t *ipst)
7312 {
7313 	ire_t	*ire = NULL;
7314 	ipif_t	*ipif;
7315 	boolean_t ipif_refheld = B_FALSE;
7316 	uint_t	type;
7317 	uint_t	match_flags = MATCH_IRE_TYPE;
7318 	int	err = 0;
7319 
7320 	ip1dbg(("ip_rt_delete:"));
7321 	/*
7322 	 * If this is the case of RTF_HOST being set, then we set the netmask
7323 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
7324 	 */
7325 	if (flags & RTF_HOST) {
7326 		mask = IP_HOST_MASK;
7327 		match_flags |= MATCH_IRE_MASK;
7328 	} else if (rtm_addrs & RTA_NETMASK) {
7329 		match_flags |= MATCH_IRE_MASK;
7330 	}
7331 
7332 	/*
7333 	 * Note that RTF_GATEWAY is never set on a delete, therefore
7334 	 * we check if the gateway address is one of our interfaces first,
7335 	 * and fall back on RTF_GATEWAY routes.
7336 	 *
7337 	 * This makes it possible to delete an original
7338 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
7339 	 *
7340 	 * As the interface index specified with the RTA_IFP sockaddr is the
7341 	 * same for all ipif's off of an ill, the matching logic below uses
7342 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
7343 	 * sharing the same prefix and interface index as the the route
7344 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
7345 	 * is specified in the request.
7346 	 *
7347 	 * On the other hand, since the gateway address will usually be
7348 	 * different for each ipif on the system, the matching logic
7349 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
7350 	 * route.  This means that interface routes for the same prefix can be
7351 	 * uniquely identified if they belong to distinct ipif's and if a
7352 	 * RTA_IFP sockaddr is not present.
7353 	 *
7354 	 * For more detail on specifying routes by gateway address and by
7355 	 * interface index, see the comments in ip_rt_add().
7356 	 */
7357 	ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, &err,
7358 	    ipst);
7359 	if (ipif != NULL)
7360 		ipif_refheld = B_TRUE;
7361 	else if (err == EINPROGRESS)
7362 		return (err);
7363 	else
7364 		err = 0;
7365 	if (ipif != NULL) {
7366 		if (ipif_arg != NULL) {
7367 			if (ipif_refheld) {
7368 				ipif_refrele(ipif);
7369 				ipif_refheld = B_FALSE;
7370 			}
7371 			ipif = ipif_arg;
7372 			match_flags |= MATCH_IRE_ILL;
7373 		} else {
7374 			match_flags |= MATCH_IRE_IPIF;
7375 		}
7376 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
7377 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
7378 			    ALL_ZONES, NULL, match_flags, ipst);
7379 		}
7380 		if (ire == NULL) {
7381 			ire = ire_ftable_lookup(dst_addr, mask, 0,
7382 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL,
7383 			    match_flags, ipst);
7384 		}
7385 	}
7386 
7387 	if (ire == NULL) {
7388 		/*
7389 		 * At this point, the gateway address is not one of our own
7390 		 * addresses or a matching interface route was not found.  We
7391 		 * set the IRE type to lookup based on whether
7392 		 * this is a host route, a default route or just a prefix.
7393 		 *
7394 		 * If an ipif_arg was passed in, then the lookup is based on an
7395 		 * interface index so MATCH_IRE_ILL is added to match_flags.
7396 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
7397 		 * set as the route being looked up is not a traditional
7398 		 * interface route.
7399 		 */
7400 		match_flags &= ~MATCH_IRE_IPIF;
7401 		match_flags |= MATCH_IRE_GW;
7402 		if (ipif_arg != NULL)
7403 			match_flags |= MATCH_IRE_ILL;
7404 		if (mask == IP_HOST_MASK)
7405 			type = IRE_HOST;
7406 		else if (mask == 0)
7407 			type = IRE_DEFAULT;
7408 		else
7409 			type = IRE_PREFIX;
7410 		ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
7411 		    NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
7412 	}
7413 
7414 	if (ipif_refheld)
7415 		ipif_refrele(ipif);
7416 
7417 	/* ipif is not refheld anymore */
7418 	if (ire == NULL)
7419 		return (ESRCH);
7420 
7421 	if (ire->ire_flags & RTF_MULTIRT) {
7422 		/*
7423 		 * Invoke the CGTP (multirouting) filtering module
7424 		 * to remove the dst address from the filtering database.
7425 		 * Packets coming from that address will no longer be
7426 		 * filtered to remove duplicates.
7427 		 */
7428 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
7429 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
7430 			    ipst->ips_netstack->netstack_stackid,
7431 			    ire->ire_addr, ire->ire_gateway_addr);
7432 		}
7433 		ip_cgtp_bcast_delete(ire, ipst);
7434 	}
7435 
7436 	ipif = ire->ire_ipif;
7437 	if (ipif != NULL)
7438 		ipif_remove_ire(ipif, ire);
7439 	if (ioctl_msg)
7440 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
7441 	ire_delete(ire);
7442 	ire_refrele(ire);
7443 	return (err);
7444 }
7445 
7446 /*
7447  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
7448  */
7449 /* ARGSUSED */
7450 int
7451 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7452     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7453 {
7454 	ipaddr_t dst_addr;
7455 	ipaddr_t gw_addr;
7456 	ipaddr_t mask;
7457 	int error = 0;
7458 	mblk_t *mp1;
7459 	struct rtentry *rt;
7460 	ipif_t *ipif = NULL;
7461 	ip_stack_t	*ipst;
7462 
7463 	ASSERT(q->q_next == NULL);
7464 	ipst = CONNQ_TO_IPST(q);
7465 
7466 	ip1dbg(("ip_siocaddrt:"));
7467 	/* Existence of mp1 verified in ip_wput_nondata */
7468 	mp1 = mp->b_cont->b_cont;
7469 	rt = (struct rtentry *)mp1->b_rptr;
7470 
7471 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7472 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7473 
7474 	/*
7475 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
7476 	 * to a particular host address.  In this case, we set the netmask to
7477 	 * all ones for the particular destination address.  Otherwise,
7478 	 * determine the netmask to be used based on dst_addr and the interfaces
7479 	 * in use.
7480 	 */
7481 	if (rt->rt_flags & RTF_HOST) {
7482 		mask = IP_HOST_MASK;
7483 	} else {
7484 		/*
7485 		 * Note that ip_subnet_mask returns a zero mask in the case of
7486 		 * default (an all-zeroes address).
7487 		 */
7488 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7489 	}
7490 
7491 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
7492 	    B_TRUE, q, mp, ip_process_ioctl, NULL, ipst);
7493 	if (ipif != NULL)
7494 		ipif_refrele(ipif);
7495 	return (error);
7496 }
7497 
7498 /*
7499  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
7500  */
7501 /* ARGSUSED */
7502 int
7503 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7504     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
7505 {
7506 	ipaddr_t dst_addr;
7507 	ipaddr_t gw_addr;
7508 	ipaddr_t mask;
7509 	int error;
7510 	mblk_t *mp1;
7511 	struct rtentry *rt;
7512 	ipif_t *ipif = NULL;
7513 	ip_stack_t	*ipst;
7514 
7515 	ASSERT(q->q_next == NULL);
7516 	ipst = CONNQ_TO_IPST(q);
7517 
7518 	ip1dbg(("ip_siocdelrt:"));
7519 	/* Existence of mp1 verified in ip_wput_nondata */
7520 	mp1 = mp->b_cont->b_cont;
7521 	rt = (struct rtentry *)mp1->b_rptr;
7522 
7523 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
7524 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
7525 
7526 	/*
7527 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
7528 	 * to a particular host address.  In this case, we set the netmask to
7529 	 * all ones for the particular destination address.  Otherwise,
7530 	 * determine the netmask to be used based on dst_addr and the interfaces
7531 	 * in use.
7532 	 */
7533 	if (rt->rt_flags & RTF_HOST) {
7534 		mask = IP_HOST_MASK;
7535 	} else {
7536 		/*
7537 		 * Note that ip_subnet_mask returns a zero mask in the case of
7538 		 * default (an all-zeroes address).
7539 		 */
7540 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
7541 	}
7542 
7543 	error = ip_rt_delete(dst_addr, mask, gw_addr,
7544 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE, q,
7545 	    mp, ip_process_ioctl, ipst);
7546 	if (ipif != NULL)
7547 		ipif_refrele(ipif);
7548 	return (error);
7549 }
7550 
7551 /*
7552  * Enqueue the mp onto the ipsq, chained by b_next.
7553  * b_prev stores the function to be executed later, and b_queue the queue
7554  * where this mp originated.
7555  */
7556 void
7557 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7558     ill_t *pending_ill)
7559 {
7560 	conn_t	*connp = NULL;
7561 
7562 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7563 	ASSERT(func != NULL);
7564 
7565 	mp->b_queue = q;
7566 	mp->b_prev = (void *)func;
7567 	mp->b_next = NULL;
7568 
7569 	switch (type) {
7570 	case CUR_OP:
7571 		if (ipsq->ipsq_mptail != NULL) {
7572 			ASSERT(ipsq->ipsq_mphead != NULL);
7573 			ipsq->ipsq_mptail->b_next = mp;
7574 		} else {
7575 			ASSERT(ipsq->ipsq_mphead == NULL);
7576 			ipsq->ipsq_mphead = mp;
7577 		}
7578 		ipsq->ipsq_mptail = mp;
7579 		break;
7580 
7581 	case NEW_OP:
7582 		if (ipsq->ipsq_xopq_mptail != NULL) {
7583 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7584 			ipsq->ipsq_xopq_mptail->b_next = mp;
7585 		} else {
7586 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7587 			ipsq->ipsq_xopq_mphead = mp;
7588 		}
7589 		ipsq->ipsq_xopq_mptail = mp;
7590 		break;
7591 	default:
7592 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7593 	}
7594 
7595 	if (CONN_Q(q) && pending_ill != NULL) {
7596 		connp = Q_TO_CONN(q);
7597 
7598 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7599 		connp->conn_oper_pending_ill = pending_ill;
7600 	}
7601 }
7602 
7603 /*
7604  * Return the mp at the head of the ipsq. After emptying the ipsq
7605  * look at the next ioctl, if this ioctl is complete. Otherwise
7606  * return, we will resume when we complete the current ioctl.
7607  * The current ioctl will wait till it gets a response from the
7608  * driver below.
7609  */
7610 static mblk_t *
7611 ipsq_dq(ipsq_t *ipsq)
7612 {
7613 	mblk_t	*mp;
7614 
7615 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7616 
7617 	mp = ipsq->ipsq_mphead;
7618 	if (mp != NULL) {
7619 		ipsq->ipsq_mphead = mp->b_next;
7620 		if (ipsq->ipsq_mphead == NULL)
7621 			ipsq->ipsq_mptail = NULL;
7622 		mp->b_next = NULL;
7623 		return (mp);
7624 	}
7625 	if (ipsq->ipsq_current_ipif != NULL)
7626 		return (NULL);
7627 	mp = ipsq->ipsq_xopq_mphead;
7628 	if (mp != NULL) {
7629 		ipsq->ipsq_xopq_mphead = mp->b_next;
7630 		if (ipsq->ipsq_xopq_mphead == NULL)
7631 			ipsq->ipsq_xopq_mptail = NULL;
7632 		mp->b_next = NULL;
7633 		return (mp);
7634 	}
7635 	return (NULL);
7636 }
7637 
7638 /*
7639  * Enter the ipsq corresponding to ill, by waiting synchronously till
7640  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7641  * will have to drain completely before ipsq_enter returns success.
7642  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7643  * and the ipsq_exit logic will start the next enqueued ioctl after
7644  * completion of the current ioctl. If 'force' is used, we don't wait
7645  * for the enqueued ioctls. This is needed when a conn_close wants to
7646  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7647  * of an ill can also use this option. But we dont' use it currently.
7648  */
7649 #define	ENTER_SQ_WAIT_TICKS 100
7650 boolean_t
7651 ipsq_enter(ill_t *ill, boolean_t force)
7652 {
7653 	ipsq_t	*ipsq;
7654 	boolean_t waited_enough = B_FALSE;
7655 
7656 	/*
7657 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7658 	 * Since the <ill-ipsq> assocs could change while we wait for the
7659 	 * writer, it is easier to wait on a fixed global rather than try to
7660 	 * cv_wait on a changing ipsq.
7661 	 */
7662 	mutex_enter(&ill->ill_lock);
7663 	for (;;) {
7664 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7665 			mutex_exit(&ill->ill_lock);
7666 			return (B_FALSE);
7667 		}
7668 
7669 		ipsq = ill->ill_phyint->phyint_ipsq;
7670 		mutex_enter(&ipsq->ipsq_lock);
7671 		if (ipsq->ipsq_writer == NULL &&
7672 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7673 			break;
7674 		} else if (ipsq->ipsq_writer != NULL) {
7675 			mutex_exit(&ipsq->ipsq_lock);
7676 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7677 		} else {
7678 			mutex_exit(&ipsq->ipsq_lock);
7679 			if (force) {
7680 				(void) cv_timedwait(&ill->ill_cv,
7681 				    &ill->ill_lock,
7682 				    lbolt + ENTER_SQ_WAIT_TICKS);
7683 				waited_enough = B_TRUE;
7684 				continue;
7685 			} else {
7686 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7687 			}
7688 		}
7689 	}
7690 
7691 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7692 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7693 	ipsq->ipsq_writer = curthread;
7694 	ipsq->ipsq_reentry_cnt++;
7695 #ifdef DEBUG
7696 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IPSQ_STACK_DEPTH);
7697 #endif
7698 	mutex_exit(&ipsq->ipsq_lock);
7699 	mutex_exit(&ill->ill_lock);
7700 	return (B_TRUE);
7701 }
7702 
7703 /*
7704  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7705  * certain critical operations like plumbing (i.e. most set ioctls),
7706  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7707  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7708  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7709  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7710  * threads executing in the ipsq. Responses from the driver pertain to the
7711  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7712  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7713  *
7714  * If a thread does not want to reenter the ipsq when it is already writer,
7715  * it must make sure that the specified reentry point to be called later
7716  * when the ipsq is empty, nor any code path starting from the specified reentry
7717  * point must never ever try to enter the ipsq again. Otherwise it can lead
7718  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7719  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7720  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7721  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7722  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7723  * ioctl if the current ioctl has completed. If the current ioctl is still
7724  * in progress it simply returns. The current ioctl could be waiting for
7725  * a response from another module (arp_ or the driver or could be waiting for
7726  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7727  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7728  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7729  * ipsq_current_ipif is clear which happens only on ioctl completion.
7730  */
7731 
7732 /*
7733  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7734  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7735  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7736  * completion.
7737  */
7738 ipsq_t *
7739 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7740     ipsq_func_t func, int type, boolean_t reentry_ok)
7741 {
7742 	ipsq_t	*ipsq;
7743 
7744 	/* Only 1 of ipif or ill can be specified */
7745 	ASSERT((ipif != NULL) ^ (ill != NULL));
7746 	if (ipif != NULL)
7747 		ill = ipif->ipif_ill;
7748 
7749 	/*
7750 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7751 	 * ipsq of an ill can't change when ill_lock is held.
7752 	 */
7753 	GRAB_CONN_LOCK(q);
7754 	mutex_enter(&ill->ill_lock);
7755 	ipsq = ill->ill_phyint->phyint_ipsq;
7756 	mutex_enter(&ipsq->ipsq_lock);
7757 
7758 	/*
7759 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7760 	 *    (Note: If the caller does not specify reentry_ok then neither
7761 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7762 	 *    again. Otherwise it can lead to an infinite loop
7763 	 * 2. Enter the ipsq if there is no current writer and this attempted
7764 	 *    entry is part of the current ioctl or operation
7765 	 * 3. Enter the ipsq if there is no current writer and this is a new
7766 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7767 	 *    empty and there is no ioctl (or operation) currently in progress
7768 	 */
7769 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7770 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7771 	    ipsq->ipsq_current_ipif == NULL))) ||
7772 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7773 		/* Success. */
7774 		ipsq->ipsq_reentry_cnt++;
7775 		ipsq->ipsq_writer = curthread;
7776 		mutex_exit(&ipsq->ipsq_lock);
7777 		mutex_exit(&ill->ill_lock);
7778 		RELEASE_CONN_LOCK(q);
7779 #ifdef DEBUG
7780 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack,
7781 		    IPSQ_STACK_DEPTH);
7782 #endif
7783 		return (ipsq);
7784 	}
7785 
7786 	ipsq_enq(ipsq, q, mp, func, type, ill);
7787 
7788 	mutex_exit(&ipsq->ipsq_lock);
7789 	mutex_exit(&ill->ill_lock);
7790 	RELEASE_CONN_LOCK(q);
7791 	return (NULL);
7792 }
7793 
7794 /*
7795  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
7796  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
7797  * cannot be entered, the mp is queued for completion.
7798  */
7799 void
7800 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
7801     boolean_t reentry_ok)
7802 {
7803 	ipsq_t	*ipsq;
7804 
7805 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
7806 
7807 	/*
7808 	 * Drop the caller's refhold on the ill.  This is safe since we either
7809 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
7810 	 * IPSQ, in which case we return without accessing ill anymore.  This
7811 	 * is needed because func needs to see the correct refcount.
7812 	 * e.g. removeif can work only then.
7813 	 */
7814 	ill_refrele(ill);
7815 	if (ipsq != NULL) {
7816 		(*func)(ipsq, q, mp, NULL);
7817 		ipsq_exit(ipsq);
7818 	}
7819 }
7820 
7821 /*
7822  * If there are more than ILL_GRP_CNT ills in a group,
7823  * we use kmem alloc'd buffers, else use the stack
7824  */
7825 #define	ILL_GRP_CNT	14
7826 /*
7827  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7828  * Called by a thread that is currently exclusive on this ipsq.
7829  */
7830 void
7831 ipsq_exit(ipsq_t *ipsq)
7832 {
7833 	queue_t	*q;
7834 	mblk_t	*mp;
7835 	ipsq_func_t	func;
7836 	int	next;
7837 	ill_t	**ill_list = NULL;
7838 	size_t	ill_list_size = 0;
7839 	int	cnt = 0;
7840 	boolean_t need_ipsq_free = B_FALSE;
7841 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
7842 
7843 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7844 	mutex_enter(&ipsq->ipsq_lock);
7845 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7846 	if (ipsq->ipsq_reentry_cnt != 1) {
7847 		ipsq->ipsq_reentry_cnt--;
7848 		mutex_exit(&ipsq->ipsq_lock);
7849 		return;
7850 	}
7851 
7852 	mp = ipsq_dq(ipsq);
7853 	while (mp != NULL) {
7854 again:
7855 		mutex_exit(&ipsq->ipsq_lock);
7856 		func = (ipsq_func_t)mp->b_prev;
7857 		q = (queue_t *)mp->b_queue;
7858 		mp->b_prev = NULL;
7859 		mp->b_queue = NULL;
7860 
7861 		/*
7862 		 * If 'q' is an conn queue, it is valid, since we did a
7863 		 * a refhold on the connp, at the start of the ioctl.
7864 		 * If 'q' is an ill queue, it is valid, since close of an
7865 		 * ill will clean up the 'ipsq'.
7866 		 */
7867 		(*func)(ipsq, q, mp, NULL);
7868 
7869 		mutex_enter(&ipsq->ipsq_lock);
7870 		mp = ipsq_dq(ipsq);
7871 	}
7872 
7873 	mutex_exit(&ipsq->ipsq_lock);
7874 
7875 	/*
7876 	 * Need to grab the locks in the right order. Need to
7877 	 * atomically check (under ipsq_lock) that there are no
7878 	 * messages before relinquishing the ipsq. Also need to
7879 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7880 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7881 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7882 	 * to grab ill_g_lock as writer.
7883 	 */
7884 	rw_enter(&ipst->ips_ill_g_lock,
7885 	    ipsq->ipsq_split ? RW_WRITER : RW_READER);
7886 
7887 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7888 	if (ipsq->ipsq_refs != 0) {
7889 		/* At most 2 ills v4/v6 per phyint */
7890 		cnt = ipsq->ipsq_refs << 1;
7891 		ill_list_size = cnt * sizeof (ill_t *);
7892 		/*
7893 		 * If memory allocation fails, we will do the split
7894 		 * the next time ipsq_exit is called for whatever reason.
7895 		 * As long as the ipsq_split flag is set the need to
7896 		 * split is remembered.
7897 		 */
7898 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7899 		if (ill_list != NULL)
7900 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7901 	}
7902 	mutex_enter(&ipsq->ipsq_lock);
7903 	mp = ipsq_dq(ipsq);
7904 	if (mp != NULL) {
7905 		/* oops, some message has landed up, we can't get out */
7906 		if (ill_list != NULL)
7907 			ill_unlock_ills(ill_list, cnt);
7908 		rw_exit(&ipst->ips_ill_g_lock);
7909 		if (ill_list != NULL)
7910 			kmem_free(ill_list, ill_list_size);
7911 		ill_list = NULL;
7912 		ill_list_size = 0;
7913 		cnt = 0;
7914 		goto again;
7915 	}
7916 
7917 	/*
7918 	 * Split only if no ioctl is pending and if memory alloc succeeded
7919 	 * above.
7920 	 */
7921 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7922 	    ill_list != NULL) {
7923 		/*
7924 		 * No new ill can join this ipsq since we are holding the
7925 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7926 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7927 		 * If so we will retry on the next ipsq_exit.
7928 		 */
7929 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7930 	}
7931 
7932 	/*
7933 	 * We are holding the ipsq lock, hence no new messages can
7934 	 * land up on the ipsq, and there are no messages currently.
7935 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7936 	 * atomically while holding ill locks.
7937 	 */
7938 	ipsq->ipsq_writer = NULL;
7939 	ipsq->ipsq_reentry_cnt--;
7940 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7941 #ifdef DEBUG
7942 	ipsq->ipsq_depth = 0;
7943 #endif
7944 	mutex_exit(&ipsq->ipsq_lock);
7945 	/*
7946 	 * For IPMP this should wake up all ills in this ipsq.
7947 	 * We need to hold the ill_lock while waking up waiters to
7948 	 * avoid missed wakeups. But there is no need to acquire all
7949 	 * the ill locks and then wakeup. If we have not acquired all
7950 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7951 	 * wakes up ills one at a time after getting the right ill_lock
7952 	 */
7953 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7954 	if (ill_list != NULL)
7955 		ill_unlock_ills(ill_list, cnt);
7956 	if (ipsq->ipsq_refs == 0)
7957 		need_ipsq_free = B_TRUE;
7958 	rw_exit(&ipst->ips_ill_g_lock);
7959 	if (ill_list != 0)
7960 		kmem_free(ill_list, ill_list_size);
7961 
7962 	if (need_ipsq_free) {
7963 		/*
7964 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7965 		 * looked up. ipsq can be looked up only thru ill or phyint
7966 		 * and there are no ills/phyint on this ipsq.
7967 		 */
7968 		ipsq_delete(ipsq);
7969 	}
7970 
7971 	/*
7972 	 * Now that we're outside the IPSQ, start any IGMP/MLD timers.  We
7973 	 * can't start these inside the IPSQ since e.g. igmp_start_timers() ->
7974 	 * untimeout() (inside the IPSQ, waiting for an executing timeout to
7975 	 * finish) could deadlock with igmp_timeout_handler() -> ipsq_enter()
7976 	 * (executing the timeout, waiting to get inside the IPSQ).
7977 	 *
7978 	 * However, there is one exception to the above: if this thread *is*
7979 	 * the IGMP/MLD timeout handler thread, then we must not start its
7980 	 * timer until the current handler is done.
7981 	 */
7982 	mutex_enter(&ipst->ips_igmp_timer_lock);
7983 	if (curthread != ipst->ips_igmp_timer_thread) {
7984 		next = ipst->ips_igmp_deferred_next;
7985 		ipst->ips_igmp_deferred_next = INFINITY;
7986 		mutex_exit(&ipst->ips_igmp_timer_lock);
7987 
7988 		if (next != INFINITY)
7989 			igmp_start_timers(next, ipst);
7990 	} else {
7991 		mutex_exit(&ipst->ips_igmp_timer_lock);
7992 	}
7993 
7994 	mutex_enter(&ipst->ips_mld_timer_lock);
7995 	if (curthread != ipst->ips_mld_timer_thread) {
7996 		next = ipst->ips_mld_deferred_next;
7997 		ipst->ips_mld_deferred_next = INFINITY;
7998 		mutex_exit(&ipst->ips_mld_timer_lock);
7999 
8000 		if (next != INFINITY)
8001 			mld_start_timers(next, ipst);
8002 	} else {
8003 		mutex_exit(&ipst->ips_mld_timer_lock);
8004 	}
8005 }
8006 
8007 /*
8008  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
8009  * and `ioccmd'.
8010  */
8011 void
8012 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
8013 {
8014 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8015 
8016 	mutex_enter(&ipsq->ipsq_lock);
8017 	ASSERT(ipsq->ipsq_current_ipif == NULL);
8018 	ASSERT(ipsq->ipsq_current_ioctl == 0);
8019 	ipsq->ipsq_current_done = B_FALSE;
8020 	ipsq->ipsq_current_ipif = ipif;
8021 	ipsq->ipsq_current_ioctl = ioccmd;
8022 	mutex_exit(&ipsq->ipsq_lock);
8023 }
8024 
8025 /*
8026  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
8027  * the next exclusive operation to begin once we ipsq_exit().  However, if
8028  * pending DLPI operations remain, then we will wait for the queue to drain
8029  * before allowing the next exclusive operation to begin.  This ensures that
8030  * DLPI operations from one exclusive operation are never improperly processed
8031  * as part of a subsequent exclusive operation.
8032  */
8033 void
8034 ipsq_current_finish(ipsq_t *ipsq)
8035 {
8036 	ipif_t *ipif = ipsq->ipsq_current_ipif;
8037 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
8038 
8039 	ASSERT(IAM_WRITER_IPSQ(ipsq));
8040 
8041 	/*
8042 	 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away
8043 	 * (but in that case, IPIF_CHANGING will already be clear and no
8044 	 * pending DLPI messages can remain).
8045 	 */
8046 	if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) {
8047 		ill_t *ill = ipif->ipif_ill;
8048 
8049 		mutex_enter(&ill->ill_lock);
8050 		dlpi_pending = ill->ill_dlpi_pending;
8051 		ipif->ipif_state_flags &= ~IPIF_CHANGING;
8052 		/* Send any queued event */
8053 		ill_nic_info_dispatch(ill);
8054 		mutex_exit(&ill->ill_lock);
8055 	}
8056 
8057 	mutex_enter(&ipsq->ipsq_lock);
8058 	ipsq->ipsq_current_ioctl = 0;
8059 	ipsq->ipsq_current_done = B_TRUE;
8060 	if (dlpi_pending == DL_PRIM_INVAL)
8061 		ipsq->ipsq_current_ipif = NULL;
8062 	mutex_exit(&ipsq->ipsq_lock);
8063 }
8064 
8065 /*
8066  * The ill is closing. Flush all messages on the ipsq that originated
8067  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
8068  * for this ill since ipsq_enter could not have entered until then.
8069  * New messages can't be queued since the CONDEMNED flag is set.
8070  */
8071 static void
8072 ipsq_flush(ill_t *ill)
8073 {
8074 	queue_t	*q;
8075 	mblk_t	*prev;
8076 	mblk_t	*mp;
8077 	mblk_t	*mp_next;
8078 	ipsq_t	*ipsq;
8079 
8080 	ASSERT(IAM_WRITER_ILL(ill));
8081 	ipsq = ill->ill_phyint->phyint_ipsq;
8082 	/*
8083 	 * Flush any messages sent up by the driver.
8084 	 */
8085 	mutex_enter(&ipsq->ipsq_lock);
8086 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
8087 		mp_next = mp->b_next;
8088 		q = mp->b_queue;
8089 		if (q == ill->ill_rq || q == ill->ill_wq) {
8090 			/* Remove the mp from the ipsq */
8091 			if (prev == NULL)
8092 				ipsq->ipsq_mphead = mp->b_next;
8093 			else
8094 				prev->b_next = mp->b_next;
8095 			if (ipsq->ipsq_mptail == mp) {
8096 				ASSERT(mp_next == NULL);
8097 				ipsq->ipsq_mptail = prev;
8098 			}
8099 			inet_freemsg(mp);
8100 		} else {
8101 			prev = mp;
8102 		}
8103 	}
8104 	mutex_exit(&ipsq->ipsq_lock);
8105 	(void) ipsq_pending_mp_cleanup(ill, NULL);
8106 	ipsq_xopq_mp_cleanup(ill, NULL);
8107 	ill_pending_mp_cleanup(ill);
8108 }
8109 
8110 /* ARGSUSED */
8111 int
8112 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8113     ip_ioctl_cmd_t *ipip, void *ifreq)
8114 {
8115 	ill_t	*ill;
8116 	struct lifreq	*lifr = (struct lifreq *)ifreq;
8117 	boolean_t isv6;
8118 	conn_t	*connp;
8119 	ip_stack_t	*ipst;
8120 
8121 	connp = Q_TO_CONN(q);
8122 	ipst = connp->conn_netstack->netstack_ip;
8123 	isv6 = connp->conn_af_isv6;
8124 	/*
8125 	 * Set original index.
8126 	 * Failover and failback move logical interfaces
8127 	 * from one physical interface to another.  The
8128 	 * original index indicates the parent of a logical
8129 	 * interface, in other words, the physical interface
8130 	 * the logical interface will be moved back to on
8131 	 * failback.
8132 	 */
8133 
8134 	/*
8135 	 * Don't allow the original index to be changed
8136 	 * for non-failover addresses, autoconfigured
8137 	 * addresses, or IPv6 link local addresses.
8138 	 */
8139 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
8140 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
8141 		return (EINVAL);
8142 	}
8143 	/*
8144 	 * The new original index must be in use by some
8145 	 * physical interface.
8146 	 */
8147 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
8148 	    NULL, NULL, ipst);
8149 	if (ill == NULL)
8150 		return (ENXIO);
8151 	ill_refrele(ill);
8152 
8153 	ipif->ipif_orig_ifindex = lifr->lifr_index;
8154 	/*
8155 	 * When this ipif gets failed back, don't
8156 	 * preserve the original id, as it is no
8157 	 * longer applicable.
8158 	 */
8159 	ipif->ipif_orig_ipifid = 0;
8160 	/*
8161 	 * For IPv4, change the original index of any
8162 	 * multicast addresses associated with the
8163 	 * ipif to the new value.
8164 	 */
8165 	if (!isv6) {
8166 		ilm_t *ilm;
8167 
8168 		mutex_enter(&ipif->ipif_ill->ill_lock);
8169 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
8170 		    ilm = ilm->ilm_next) {
8171 			if (ilm->ilm_ipif == ipif) {
8172 				ilm->ilm_orig_ifindex = lifr->lifr_index;
8173 			}
8174 		}
8175 		mutex_exit(&ipif->ipif_ill->ill_lock);
8176 	}
8177 	return (0);
8178 }
8179 
8180 /* ARGSUSED */
8181 int
8182 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8183     ip_ioctl_cmd_t *ipip, void *ifreq)
8184 {
8185 	struct lifreq *lifr = (struct lifreq *)ifreq;
8186 
8187 	/*
8188 	 * Get the original interface index i.e the one
8189 	 * before FAILOVER if it ever happened.
8190 	 */
8191 	lifr->lifr_index = ipif->ipif_orig_ifindex;
8192 	return (0);
8193 }
8194 
8195 /*
8196  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
8197  * refhold and return the associated ipif
8198  */
8199 /* ARGSUSED */
8200 int
8201 ip_extract_tunreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8202     cmd_info_t *ci, ipsq_func_t func)
8203 {
8204 	boolean_t exists;
8205 	struct iftun_req *ta;
8206 	ipif_t	*ipif;
8207 	ill_t	*ill;
8208 	boolean_t isv6;
8209 	mblk_t	*mp1;
8210 	int	error;
8211 	conn_t	*connp;
8212 	ip_stack_t	*ipst;
8213 
8214 	/* Existence verified in ip_wput_nondata */
8215 	mp1 = mp->b_cont->b_cont;
8216 	ta = (struct iftun_req *)mp1->b_rptr;
8217 	/*
8218 	 * Null terminate the string to protect against buffer
8219 	 * overrun. String was generated by user code and may not
8220 	 * be trusted.
8221 	 */
8222 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
8223 
8224 	connp = Q_TO_CONN(q);
8225 	isv6 = connp->conn_af_isv6;
8226 	ipst = connp->conn_netstack->netstack_ip;
8227 
8228 	/* Disallows implicit create */
8229 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
8230 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
8231 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst);
8232 	if (ipif == NULL)
8233 		return (error);
8234 
8235 	if (ipif->ipif_id != 0) {
8236 		/*
8237 		 * We really don't want to set/get tunnel parameters
8238 		 * on virtual tunnel interfaces.  Only allow the
8239 		 * base tunnel to do these.
8240 		 */
8241 		ipif_refrele(ipif);
8242 		return (EINVAL);
8243 	}
8244 
8245 	/*
8246 	 * Send down to tunnel mod for ioctl processing.
8247 	 * Will finish ioctl in ip_rput_other().
8248 	 */
8249 	ill = ipif->ipif_ill;
8250 	if (ill->ill_net_type == IRE_LOOPBACK) {
8251 		ipif_refrele(ipif);
8252 		return (EOPNOTSUPP);
8253 	}
8254 
8255 	if (ill->ill_wq == NULL) {
8256 		ipif_refrele(ipif);
8257 		return (ENXIO);
8258 	}
8259 	/*
8260 	 * Mark the ioctl as coming from an IPv6 interface for
8261 	 * tun's convenience.
8262 	 */
8263 	if (ill->ill_isv6)
8264 		ta->ifta_flags |= 0x80000000;
8265 	ci->ci_ipif = ipif;
8266 	return (0);
8267 }
8268 
8269 /*
8270  * Parse an ifreq or lifreq struct coming down ioctls and refhold
8271  * and return the associated ipif.
8272  * Return value:
8273  *	Non zero: An error has occurred. ci may not be filled out.
8274  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
8275  *	a held ipif in ci.ci_ipif.
8276  */
8277 int
8278 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8279     cmd_info_t *ci, ipsq_func_t func)
8280 {
8281 	sin_t		*sin;
8282 	sin6_t		*sin6;
8283 	char		*name;
8284 	struct ifreq    *ifr;
8285 	struct lifreq    *lifr;
8286 	ipif_t		*ipif = NULL;
8287 	ill_t		*ill;
8288 	conn_t		*connp;
8289 	boolean_t	isv6;
8290 	boolean_t	exists;
8291 	int		err;
8292 	mblk_t		*mp1;
8293 	zoneid_t	zoneid;
8294 	ip_stack_t	*ipst;
8295 
8296 	if (q->q_next != NULL) {
8297 		ill = (ill_t *)q->q_ptr;
8298 		isv6 = ill->ill_isv6;
8299 		connp = NULL;
8300 		zoneid = ALL_ZONES;
8301 		ipst = ill->ill_ipst;
8302 	} else {
8303 		ill = NULL;
8304 		connp = Q_TO_CONN(q);
8305 		isv6 = connp->conn_af_isv6;
8306 		zoneid = connp->conn_zoneid;
8307 		if (zoneid == GLOBAL_ZONEID) {
8308 			/* global zone can access ipifs in all zones */
8309 			zoneid = ALL_ZONES;
8310 		}
8311 		ipst = connp->conn_netstack->netstack_ip;
8312 	}
8313 
8314 	/* Has been checked in ip_wput_nondata */
8315 	mp1 = mp->b_cont->b_cont;
8316 
8317 	if (ipip->ipi_cmd_type == IF_CMD) {
8318 		/* This a old style SIOC[GS]IF* command */
8319 		ifr = (struct ifreq *)mp1->b_rptr;
8320 		/*
8321 		 * Null terminate the string to protect against buffer
8322 		 * overrun. String was generated by user code and may not
8323 		 * be trusted.
8324 		 */
8325 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
8326 		sin = (sin_t *)&ifr->ifr_addr;
8327 		name = ifr->ifr_name;
8328 		ci->ci_sin = sin;
8329 		ci->ci_sin6 = NULL;
8330 		ci->ci_lifr = (struct lifreq *)ifr;
8331 	} else {
8332 		/* This a new style SIOC[GS]LIF* command */
8333 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
8334 		lifr = (struct lifreq *)mp1->b_rptr;
8335 		/*
8336 		 * Null terminate the string to protect against buffer
8337 		 * overrun. String was generated by user code and may not
8338 		 * be trusted.
8339 		 */
8340 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
8341 		name = lifr->lifr_name;
8342 		sin = (sin_t *)&lifr->lifr_addr;
8343 		sin6 = (sin6_t *)&lifr->lifr_addr;
8344 		if (ipip->ipi_cmd == SIOCSLIFGROUPNAME) {
8345 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
8346 			    LIFNAMSIZ);
8347 		}
8348 		ci->ci_sin = sin;
8349 		ci->ci_sin6 = sin6;
8350 		ci->ci_lifr = lifr;
8351 	}
8352 
8353 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
8354 		/*
8355 		 * The ioctl will be failed if the ioctl comes down
8356 		 * an conn stream
8357 		 */
8358 		if (ill == NULL) {
8359 			/*
8360 			 * Not an ill queue, return EINVAL same as the
8361 			 * old error code.
8362 			 */
8363 			return (ENXIO);
8364 		}
8365 		ipif = ill->ill_ipif;
8366 		ipif_refhold(ipif);
8367 	} else {
8368 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
8369 		    &exists, isv6, zoneid,
8370 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err,
8371 		    ipst);
8372 		if (ipif == NULL) {
8373 			if (err == EINPROGRESS)
8374 				return (err);
8375 			if (ipip->ipi_cmd == SIOCLIFFAILOVER ||
8376 			    ipip->ipi_cmd == SIOCLIFFAILBACK) {
8377 				/*
8378 				 * Need to try both v4 and v6 since this
8379 				 * ioctl can come down either v4 or v6
8380 				 * socket. The lifreq.lifr_family passed
8381 				 * down by this ioctl is AF_UNSPEC.
8382 				 */
8383 				ipif = ipif_lookup_on_name(name,
8384 				    mi_strlen(name), B_FALSE, &exists, !isv6,
8385 				    zoneid, (connp == NULL) ? q :
8386 				    CONNP_TO_WQ(connp), mp, func, &err, ipst);
8387 				if (err == EINPROGRESS)
8388 					return (err);
8389 			}
8390 			err = 0;	/* Ensure we don't use it below */
8391 		}
8392 	}
8393 
8394 	/*
8395 	 * Old style [GS]IFCMD does not admit IPv6 ipif
8396 	 */
8397 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
8398 		ipif_refrele(ipif);
8399 		return (ENXIO);
8400 	}
8401 
8402 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
8403 	    name[0] == '\0') {
8404 		/*
8405 		 * Handle a or a SIOC?IF* with a null name
8406 		 * during plumb (on the ill queue before the I_PLINK).
8407 		 */
8408 		ipif = ill->ill_ipif;
8409 		ipif_refhold(ipif);
8410 	}
8411 
8412 	if (ipif == NULL)
8413 		return (ENXIO);
8414 
8415 	/*
8416 	 * Allow only GET operations if this ipif has been created
8417 	 * temporarily due to a MOVE operation.
8418 	 */
8419 	if (ipif->ipif_replace_zero && !(ipip->ipi_flags & IPI_REPL)) {
8420 		ipif_refrele(ipif);
8421 		return (EINVAL);
8422 	}
8423 
8424 	ci->ci_ipif = ipif;
8425 	return (0);
8426 }
8427 
8428 /*
8429  * Return the total number of ipifs.
8430  */
8431 static uint_t
8432 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
8433 {
8434 	uint_t numifs = 0;
8435 	ill_t	*ill;
8436 	ill_walk_context_t	ctx;
8437 	ipif_t	*ipif;
8438 
8439 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8440 	ill = ILL_START_WALK_V4(&ctx, ipst);
8441 
8442 	while (ill != NULL) {
8443 		for (ipif = ill->ill_ipif; ipif != NULL;
8444 		    ipif = ipif->ipif_next) {
8445 			if (ipif->ipif_zoneid == zoneid ||
8446 			    ipif->ipif_zoneid == ALL_ZONES)
8447 				numifs++;
8448 		}
8449 		ill = ill_next(&ctx, ill);
8450 	}
8451 	rw_exit(&ipst->ips_ill_g_lock);
8452 	return (numifs);
8453 }
8454 
8455 /*
8456  * Return the total number of ipifs.
8457  */
8458 static uint_t
8459 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
8460 {
8461 	uint_t numifs = 0;
8462 	ill_t	*ill;
8463 	ipif_t	*ipif;
8464 	ill_walk_context_t	ctx;
8465 
8466 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
8467 
8468 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8469 	if (family == AF_INET)
8470 		ill = ILL_START_WALK_V4(&ctx, ipst);
8471 	else if (family == AF_INET6)
8472 		ill = ILL_START_WALK_V6(&ctx, ipst);
8473 	else
8474 		ill = ILL_START_WALK_ALL(&ctx, ipst);
8475 
8476 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8477 		for (ipif = ill->ill_ipif; ipif != NULL;
8478 		    ipif = ipif->ipif_next) {
8479 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8480 			    !(lifn_flags & LIFC_NOXMIT))
8481 				continue;
8482 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8483 			    !(lifn_flags & LIFC_TEMPORARY))
8484 				continue;
8485 			if (((ipif->ipif_flags &
8486 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8487 			    IPIF_DEPRECATED)) ||
8488 			    IS_LOOPBACK(ill) ||
8489 			    !(ipif->ipif_flags & IPIF_UP)) &&
8490 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
8491 				continue;
8492 
8493 			if (zoneid != ipif->ipif_zoneid &&
8494 			    ipif->ipif_zoneid != ALL_ZONES &&
8495 			    (zoneid != GLOBAL_ZONEID ||
8496 			    !(lifn_flags & LIFC_ALLZONES)))
8497 				continue;
8498 
8499 			numifs++;
8500 		}
8501 	}
8502 	rw_exit(&ipst->ips_ill_g_lock);
8503 	return (numifs);
8504 }
8505 
8506 uint_t
8507 ip_get_lifsrcofnum(ill_t *ill)
8508 {
8509 	uint_t numifs = 0;
8510 	ill_t	*ill_head = ill;
8511 	ip_stack_t	*ipst = ill->ill_ipst;
8512 
8513 	/*
8514 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
8515 	 * other thread may be trying to relink the ILLs in this usesrc group
8516 	 * and adjusting the ill_usesrc_grp_next pointers
8517 	 */
8518 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8519 	if ((ill->ill_usesrc_ifindex == 0) &&
8520 	    (ill->ill_usesrc_grp_next != NULL)) {
8521 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
8522 		    ill = ill->ill_usesrc_grp_next)
8523 			numifs++;
8524 	}
8525 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8526 
8527 	return (numifs);
8528 }
8529 
8530 /* Null values are passed in for ipif, sin, and ifreq */
8531 /* ARGSUSED */
8532 int
8533 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8534     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8535 {
8536 	int *nump;
8537 	conn_t *connp = Q_TO_CONN(q);
8538 
8539 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8540 
8541 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8542 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8543 
8544 	*nump = ip_get_numifs(connp->conn_zoneid,
8545 	    connp->conn_netstack->netstack_ip);
8546 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8547 	return (0);
8548 }
8549 
8550 /* Null values are passed in for ipif, sin, and ifreq */
8551 /* ARGSUSED */
8552 int
8553 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8554     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8555 {
8556 	struct lifnum *lifn;
8557 	mblk_t	*mp1;
8558 	conn_t *connp = Q_TO_CONN(q);
8559 
8560 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8561 
8562 	/* Existence checked in ip_wput_nondata */
8563 	mp1 = mp->b_cont->b_cont;
8564 
8565 	lifn = (struct lifnum *)mp1->b_rptr;
8566 	switch (lifn->lifn_family) {
8567 	case AF_UNSPEC:
8568 	case AF_INET:
8569 	case AF_INET6:
8570 		break;
8571 	default:
8572 		return (EAFNOSUPPORT);
8573 	}
8574 
8575 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8576 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
8577 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8578 	return (0);
8579 }
8580 
8581 /* ARGSUSED */
8582 int
8583 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8584     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8585 {
8586 	STRUCT_HANDLE(ifconf, ifc);
8587 	mblk_t *mp1;
8588 	struct iocblk *iocp;
8589 	struct ifreq *ifr;
8590 	ill_walk_context_t	ctx;
8591 	ill_t	*ill;
8592 	ipif_t	*ipif;
8593 	struct sockaddr_in *sin;
8594 	int32_t	ifclen;
8595 	zoneid_t zoneid;
8596 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8597 
8598 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8599 
8600 	ip1dbg(("ip_sioctl_get_ifconf"));
8601 	/* Existence verified in ip_wput_nondata */
8602 	mp1 = mp->b_cont->b_cont;
8603 	iocp = (struct iocblk *)mp->b_rptr;
8604 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8605 
8606 	/*
8607 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8608 	 * the user buffer address and length into which the list of struct
8609 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8610 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8611 	 * the SIOCGIFCONF operation was redefined to simply provide
8612 	 * a large output buffer into which we are supposed to jam the ifreq
8613 	 * array.  The same ioctl command code was used, despite the fact that
8614 	 * both the applications and the kernel code had to change, thus making
8615 	 * it impossible to support both interfaces.
8616 	 *
8617 	 * For reasons not good enough to try to explain, the following
8618 	 * algorithm is used for deciding what to do with one of these:
8619 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8620 	 * form with the output buffer coming down as the continuation message.
8621 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8622 	 * and we have to copy in the ifconf structure to find out how big the
8623 	 * output buffer is and where to copy out to.  Sure no problem...
8624 	 *
8625 	 */
8626 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8627 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8628 		int numifs = 0;
8629 		size_t ifc_bufsize;
8630 
8631 		/*
8632 		 * Must be (better be!) continuation of a TRANSPARENT
8633 		 * IOCTL.  We just copied in the ifconf structure.
8634 		 */
8635 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8636 		    (struct ifconf *)mp1->b_rptr);
8637 
8638 		/*
8639 		 * Allocate a buffer to hold requested information.
8640 		 *
8641 		 * If ifc_len is larger than what is needed, we only
8642 		 * allocate what we will use.
8643 		 *
8644 		 * If ifc_len is smaller than what is needed, return
8645 		 * EINVAL.
8646 		 *
8647 		 * XXX: the ill_t structure can hava 2 counters, for
8648 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8649 		 * number of interfaces for a device, so we don't need
8650 		 * to count them here...
8651 		 */
8652 		numifs = ip_get_numifs(zoneid, ipst);
8653 
8654 		ifclen = STRUCT_FGET(ifc, ifc_len);
8655 		ifc_bufsize = numifs * sizeof (struct ifreq);
8656 		if (ifc_bufsize > ifclen) {
8657 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8658 				/* old behaviour */
8659 				return (EINVAL);
8660 			} else {
8661 				ifc_bufsize = ifclen;
8662 			}
8663 		}
8664 
8665 		mp1 = mi_copyout_alloc(q, mp,
8666 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8667 		if (mp1 == NULL)
8668 			return (ENOMEM);
8669 
8670 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8671 	}
8672 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8673 	/*
8674 	 * the SIOCGIFCONF ioctl only knows about
8675 	 * IPv4 addresses, so don't try to tell
8676 	 * it about interfaces with IPv6-only
8677 	 * addresses. (Last parm 'isv6' is B_FALSE)
8678 	 */
8679 
8680 	ifr = (struct ifreq *)mp1->b_rptr;
8681 
8682 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8683 	ill = ILL_START_WALK_V4(&ctx, ipst);
8684 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8685 		for (ipif = ill->ill_ipif; ipif != NULL;
8686 		    ipif = ipif->ipif_next) {
8687 			if (zoneid != ipif->ipif_zoneid &&
8688 			    ipif->ipif_zoneid != ALL_ZONES)
8689 				continue;
8690 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8691 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8692 					/* old behaviour */
8693 					rw_exit(&ipst->ips_ill_g_lock);
8694 					return (EINVAL);
8695 				} else {
8696 					goto if_copydone;
8697 				}
8698 			}
8699 			ipif_get_name(ipif, ifr->ifr_name,
8700 			    sizeof (ifr->ifr_name));
8701 			sin = (sin_t *)&ifr->ifr_addr;
8702 			*sin = sin_null;
8703 			sin->sin_family = AF_INET;
8704 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8705 			ifr++;
8706 		}
8707 	}
8708 if_copydone:
8709 	rw_exit(&ipst->ips_ill_g_lock);
8710 	mp1->b_wptr = (uchar_t *)ifr;
8711 
8712 	if (STRUCT_BUF(ifc) != NULL) {
8713 		STRUCT_FSET(ifc, ifc_len,
8714 		    (int)((uchar_t *)ifr - mp1->b_rptr));
8715 	}
8716 	return (0);
8717 }
8718 
8719 /*
8720  * Get the interfaces using the address hosted on the interface passed in,
8721  * as a source adddress
8722  */
8723 /* ARGSUSED */
8724 int
8725 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8726     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8727 {
8728 	mblk_t *mp1;
8729 	ill_t	*ill, *ill_head;
8730 	ipif_t	*ipif, *orig_ipif;
8731 	int	numlifs = 0;
8732 	size_t	lifs_bufsize, lifsmaxlen;
8733 	struct	lifreq *lifr;
8734 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8735 	uint_t	ifindex;
8736 	zoneid_t zoneid;
8737 	int err = 0;
8738 	boolean_t isv6 = B_FALSE;
8739 	struct	sockaddr_in	*sin;
8740 	struct	sockaddr_in6	*sin6;
8741 	STRUCT_HANDLE(lifsrcof, lifs);
8742 	ip_stack_t		*ipst;
8743 
8744 	ipst = CONNQ_TO_IPST(q);
8745 
8746 	ASSERT(q->q_next == NULL);
8747 
8748 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8749 
8750 	/* Existence verified in ip_wput_nondata */
8751 	mp1 = mp->b_cont->b_cont;
8752 
8753 	/*
8754 	 * Must be (better be!) continuation of a TRANSPARENT
8755 	 * IOCTL.  We just copied in the lifsrcof structure.
8756 	 */
8757 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8758 	    (struct lifsrcof *)mp1->b_rptr);
8759 
8760 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8761 		return (EINVAL);
8762 
8763 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8764 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8765 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8766 	    ip_process_ioctl, &err, ipst);
8767 	if (ipif == NULL) {
8768 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8769 		    ifindex));
8770 		return (err);
8771 	}
8772 
8773 
8774 	/* Allocate a buffer to hold requested information */
8775 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8776 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8777 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8778 	/* The actual size needed is always returned in lifs_len */
8779 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8780 
8781 	/* If the amount we need is more than what is passed in, abort */
8782 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8783 		ipif_refrele(ipif);
8784 		return (0);
8785 	}
8786 
8787 	mp1 = mi_copyout_alloc(q, mp,
8788 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8789 	if (mp1 == NULL) {
8790 		ipif_refrele(ipif);
8791 		return (ENOMEM);
8792 	}
8793 
8794 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8795 	bzero(mp1->b_rptr, lifs_bufsize);
8796 
8797 	lifr = (struct lifreq *)mp1->b_rptr;
8798 
8799 	ill = ill_head = ipif->ipif_ill;
8800 	orig_ipif = ipif;
8801 
8802 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8803 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
8804 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8805 
8806 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8807 	for (; (ill != NULL) && (ill != ill_head);
8808 	    ill = ill->ill_usesrc_grp_next) {
8809 
8810 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8811 			break;
8812 
8813 		ipif = ill->ill_ipif;
8814 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
8815 		if (ipif->ipif_isv6) {
8816 			sin6 = (sin6_t *)&lifr->lifr_addr;
8817 			*sin6 = sin6_null;
8818 			sin6->sin6_family = AF_INET6;
8819 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8820 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8821 			    &ipif->ipif_v6net_mask);
8822 		} else {
8823 			sin = (sin_t *)&lifr->lifr_addr;
8824 			*sin = sin_null;
8825 			sin->sin_family = AF_INET;
8826 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8827 			lifr->lifr_addrlen = ip_mask_to_plen(
8828 			    ipif->ipif_net_mask);
8829 		}
8830 		lifr++;
8831 	}
8832 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
8833 	rw_exit(&ipst->ips_ill_g_lock);
8834 	ipif_refrele(orig_ipif);
8835 	mp1->b_wptr = (uchar_t *)lifr;
8836 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8837 
8838 	return (0);
8839 }
8840 
8841 /* ARGSUSED */
8842 int
8843 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8844     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8845 {
8846 	mblk_t *mp1;
8847 	int	list;
8848 	ill_t	*ill;
8849 	ipif_t	*ipif;
8850 	int	flags;
8851 	int	numlifs = 0;
8852 	size_t	lifc_bufsize;
8853 	struct	lifreq *lifr;
8854 	sa_family_t	family;
8855 	struct	sockaddr_in	*sin;
8856 	struct	sockaddr_in6	*sin6;
8857 	ill_walk_context_t	ctx;
8858 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8859 	int32_t	lifclen;
8860 	zoneid_t zoneid;
8861 	STRUCT_HANDLE(lifconf, lifc);
8862 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
8863 
8864 	ip1dbg(("ip_sioctl_get_lifconf"));
8865 
8866 	ASSERT(q->q_next == NULL);
8867 
8868 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8869 
8870 	/* Existence verified in ip_wput_nondata */
8871 	mp1 = mp->b_cont->b_cont;
8872 
8873 	/*
8874 	 * An extended version of SIOCGIFCONF that takes an
8875 	 * additional address family and flags field.
8876 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8877 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8878 	 * interfaces are omitted.
8879 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8880 	 * unless LIFC_TEMPORARY is specified.
8881 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8882 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8883 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8884 	 * has priority over LIFC_NOXMIT.
8885 	 */
8886 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8887 
8888 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8889 		return (EINVAL);
8890 
8891 	/*
8892 	 * Must be (better be!) continuation of a TRANSPARENT
8893 	 * IOCTL.  We just copied in the lifconf structure.
8894 	 */
8895 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8896 
8897 	family = STRUCT_FGET(lifc, lifc_family);
8898 	flags = STRUCT_FGET(lifc, lifc_flags);
8899 
8900 	switch (family) {
8901 	case AF_UNSPEC:
8902 		/*
8903 		 * walk all ILL's.
8904 		 */
8905 		list = MAX_G_HEADS;
8906 		break;
8907 	case AF_INET:
8908 		/*
8909 		 * walk only IPV4 ILL's.
8910 		 */
8911 		list = IP_V4_G_HEAD;
8912 		break;
8913 	case AF_INET6:
8914 		/*
8915 		 * walk only IPV6 ILL's.
8916 		 */
8917 		list = IP_V6_G_HEAD;
8918 		break;
8919 	default:
8920 		return (EAFNOSUPPORT);
8921 	}
8922 
8923 	/*
8924 	 * Allocate a buffer to hold requested information.
8925 	 *
8926 	 * If lifc_len is larger than what is needed, we only
8927 	 * allocate what we will use.
8928 	 *
8929 	 * If lifc_len is smaller than what is needed, return
8930 	 * EINVAL.
8931 	 */
8932 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
8933 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8934 	lifclen = STRUCT_FGET(lifc, lifc_len);
8935 	if (lifc_bufsize > lifclen) {
8936 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8937 			return (EINVAL);
8938 		else
8939 			lifc_bufsize = lifclen;
8940 	}
8941 
8942 	mp1 = mi_copyout_alloc(q, mp,
8943 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8944 	if (mp1 == NULL)
8945 		return (ENOMEM);
8946 
8947 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8948 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8949 
8950 	lifr = (struct lifreq *)mp1->b_rptr;
8951 
8952 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
8953 	ill = ill_first(list, list, &ctx, ipst);
8954 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8955 		for (ipif = ill->ill_ipif; ipif != NULL;
8956 		    ipif = ipif->ipif_next) {
8957 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8958 			    !(flags & LIFC_NOXMIT))
8959 				continue;
8960 
8961 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8962 			    !(flags & LIFC_TEMPORARY))
8963 				continue;
8964 
8965 			if (((ipif->ipif_flags &
8966 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8967 			    IPIF_DEPRECATED)) ||
8968 			    IS_LOOPBACK(ill) ||
8969 			    !(ipif->ipif_flags & IPIF_UP)) &&
8970 			    (flags & LIFC_EXTERNAL_SOURCE))
8971 				continue;
8972 
8973 			if (zoneid != ipif->ipif_zoneid &&
8974 			    ipif->ipif_zoneid != ALL_ZONES &&
8975 			    (zoneid != GLOBAL_ZONEID ||
8976 			    !(flags & LIFC_ALLZONES)))
8977 				continue;
8978 
8979 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8980 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8981 					rw_exit(&ipst->ips_ill_g_lock);
8982 					return (EINVAL);
8983 				} else {
8984 					goto lif_copydone;
8985 				}
8986 			}
8987 
8988 			ipif_get_name(ipif, lifr->lifr_name,
8989 			    sizeof (lifr->lifr_name));
8990 			if (ipif->ipif_isv6) {
8991 				sin6 = (sin6_t *)&lifr->lifr_addr;
8992 				*sin6 = sin6_null;
8993 				sin6->sin6_family = AF_INET6;
8994 				sin6->sin6_addr =
8995 				    ipif->ipif_v6lcl_addr;
8996 				lifr->lifr_addrlen =
8997 				    ip_mask_to_plen_v6(
8998 				    &ipif->ipif_v6net_mask);
8999 			} else {
9000 				sin = (sin_t *)&lifr->lifr_addr;
9001 				*sin = sin_null;
9002 				sin->sin_family = AF_INET;
9003 				sin->sin_addr.s_addr =
9004 				    ipif->ipif_lcl_addr;
9005 				lifr->lifr_addrlen =
9006 				    ip_mask_to_plen(
9007 				    ipif->ipif_net_mask);
9008 			}
9009 			lifr++;
9010 		}
9011 	}
9012 lif_copydone:
9013 	rw_exit(&ipst->ips_ill_g_lock);
9014 
9015 	mp1->b_wptr = (uchar_t *)lifr;
9016 	if (STRUCT_BUF(lifc) != NULL) {
9017 		STRUCT_FSET(lifc, lifc_len,
9018 		    (int)((uchar_t *)lifr - mp1->b_rptr));
9019 	}
9020 	return (0);
9021 }
9022 
9023 /* ARGSUSED */
9024 int
9025 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
9026     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
9027 {
9028 	ip_stack_t	*ipst;
9029 
9030 	if (q->q_next == NULL)
9031 		ipst = CONNQ_TO_IPST(q);
9032 	else
9033 		ipst = ILLQ_TO_IPST(q);
9034 
9035 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
9036 	ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
9037 	return (0);
9038 }
9039 
9040 static void
9041 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
9042 {
9043 	ip6_asp_t *table;
9044 	size_t table_size;
9045 	mblk_t *data_mp;
9046 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9047 	ip_stack_t	*ipst;
9048 
9049 	if (q->q_next == NULL)
9050 		ipst = CONNQ_TO_IPST(q);
9051 	else
9052 		ipst = ILLQ_TO_IPST(q);
9053 
9054 	/* These two ioctls are I_STR only */
9055 	if (iocp->ioc_count == TRANSPARENT) {
9056 		miocnak(q, mp, 0, EINVAL);
9057 		return;
9058 	}
9059 
9060 	data_mp = mp->b_cont;
9061 	if (data_mp == NULL) {
9062 		/* The user passed us a NULL argument */
9063 		table = NULL;
9064 		table_size = iocp->ioc_count;
9065 	} else {
9066 		/*
9067 		 * The user provided a table.  The stream head
9068 		 * may have copied in the user data in chunks,
9069 		 * so make sure everything is pulled up
9070 		 * properly.
9071 		 */
9072 		if (MBLKL(data_mp) < iocp->ioc_count) {
9073 			mblk_t *new_data_mp;
9074 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
9075 			    NULL) {
9076 				miocnak(q, mp, 0, ENOMEM);
9077 				return;
9078 			}
9079 			freemsg(data_mp);
9080 			data_mp = new_data_mp;
9081 			mp->b_cont = data_mp;
9082 		}
9083 		table = (ip6_asp_t *)data_mp->b_rptr;
9084 		table_size = iocp->ioc_count;
9085 	}
9086 
9087 	switch (iocp->ioc_cmd) {
9088 	case SIOCGIP6ADDRPOLICY:
9089 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
9090 		if (iocp->ioc_rval == -1)
9091 			iocp->ioc_error = EINVAL;
9092 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9093 		else if (table != NULL &&
9094 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
9095 			ip6_asp_t *src = table;
9096 			ip6_asp32_t *dst = (void *)table;
9097 			int count = table_size / sizeof (ip6_asp_t);
9098 			int i;
9099 
9100 			/*
9101 			 * We need to do an in-place shrink of the array
9102 			 * to match the alignment attributes of the
9103 			 * 32-bit ABI looking at it.
9104 			 */
9105 			/* LINTED: logical expression always true: op "||" */
9106 			ASSERT(sizeof (*src) > sizeof (*dst));
9107 			for (i = 1; i < count; i++)
9108 				bcopy(src + i, dst + i, sizeof (*dst));
9109 		}
9110 #endif
9111 		break;
9112 
9113 	case SIOCSIP6ADDRPOLICY:
9114 		ASSERT(mp->b_prev == NULL);
9115 		mp->b_prev = (void *)q;
9116 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
9117 		/*
9118 		 * We pass in the datamodel here so that the ip6_asp_replace()
9119 		 * routine can handle converting from 32-bit to native formats
9120 		 * where necessary.
9121 		 *
9122 		 * A better way to handle this might be to convert the inbound
9123 		 * data structure here, and hang it off a new 'mp'; thus the
9124 		 * ip6_asp_replace() logic would always be dealing with native
9125 		 * format data structures..
9126 		 *
9127 		 * (An even simpler way to handle these ioctls is to just
9128 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
9129 		 * and just recompile everything that depends on it.)
9130 		 */
9131 #endif
9132 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
9133 		    iocp->ioc_flag & IOC_MODELS);
9134 		return;
9135 	}
9136 
9137 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
9138 	qreply(q, mp);
9139 }
9140 
9141 static void
9142 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
9143 {
9144 	mblk_t 		*data_mp;
9145 	struct dstinforeq	*dir;
9146 	uint8_t		*end, *cur;
9147 	in6_addr_t	*daddr, *saddr;
9148 	ipaddr_t	v4daddr;
9149 	ire_t		*ire;
9150 	char		*slabel, *dlabel;
9151 	boolean_t	isipv4;
9152 	int		match_ire;
9153 	ill_t		*dst_ill;
9154 	ipif_t		*src_ipif, *ire_ipif;
9155 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9156 	zoneid_t	zoneid;
9157 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
9158 
9159 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9160 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9161 
9162 	/*
9163 	 * This ioctl is I_STR only, and must have a
9164 	 * data mblk following the M_IOCTL mblk.
9165 	 */
9166 	data_mp = mp->b_cont;
9167 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
9168 		miocnak(q, mp, 0, EINVAL);
9169 		return;
9170 	}
9171 
9172 	if (MBLKL(data_mp) < iocp->ioc_count) {
9173 		mblk_t *new_data_mp;
9174 
9175 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
9176 			miocnak(q, mp, 0, ENOMEM);
9177 			return;
9178 		}
9179 		freemsg(data_mp);
9180 		data_mp = new_data_mp;
9181 		mp->b_cont = data_mp;
9182 	}
9183 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
9184 
9185 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
9186 	    end - cur >= sizeof (struct dstinforeq);
9187 	    cur += sizeof (struct dstinforeq)) {
9188 		dir = (struct dstinforeq *)cur;
9189 		daddr = &dir->dir_daddr;
9190 		saddr = &dir->dir_saddr;
9191 
9192 		/*
9193 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
9194 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
9195 		 * and ipif_select_source[_v6]() do not.
9196 		 */
9197 		dir->dir_dscope = ip_addr_scope_v6(daddr);
9198 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
9199 
9200 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
9201 		if (isipv4) {
9202 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
9203 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
9204 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9205 		} else {
9206 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
9207 			    0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst);
9208 		}
9209 		if (ire == NULL) {
9210 			dir->dir_dreachable = 0;
9211 
9212 			/* move on to next dst addr */
9213 			continue;
9214 		}
9215 		dir->dir_dreachable = 1;
9216 
9217 		ire_ipif = ire->ire_ipif;
9218 		if (ire_ipif == NULL)
9219 			goto next_dst;
9220 
9221 		/*
9222 		 * We expect to get back an interface ire or a
9223 		 * gateway ire cache entry.  For both types, the
9224 		 * output interface is ire_ipif->ipif_ill.
9225 		 */
9226 		dst_ill = ire_ipif->ipif_ill;
9227 		dir->dir_dmactype = dst_ill->ill_mactype;
9228 
9229 		if (isipv4) {
9230 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
9231 		} else {
9232 			src_ipif = ipif_select_source_v6(dst_ill,
9233 			    daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT,
9234 			    zoneid);
9235 		}
9236 		if (src_ipif == NULL)
9237 			goto next_dst;
9238 
9239 		*saddr = src_ipif->ipif_v6lcl_addr;
9240 		dir->dir_sscope = ip_addr_scope_v6(saddr);
9241 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
9242 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
9243 		dir->dir_sdeprecated =
9244 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
9245 		ipif_refrele(src_ipif);
9246 next_dst:
9247 		ire_refrele(ire);
9248 	}
9249 	miocack(q, mp, iocp->ioc_count, 0);
9250 }
9251 
9252 
9253 /*
9254  * Check if this is an address assigned to this machine.
9255  * Skips interfaces that are down by using ire checks.
9256  * Translates mapped addresses to v4 addresses and then
9257  * treats them as such, returning true if the v4 address
9258  * associated with this mapped address is configured.
9259  * Note: Applications will have to be careful what they do
9260  * with the response; use of mapped addresses limits
9261  * what can be done with the socket, especially with
9262  * respect to socket options and ioctls - neither IPv4
9263  * options nor IPv6 sticky options/ancillary data options
9264  * may be used.
9265  */
9266 /* ARGSUSED */
9267 int
9268 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9269     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9270 {
9271 	struct sioc_addrreq *sia;
9272 	sin_t *sin;
9273 	ire_t *ire;
9274 	mblk_t *mp1;
9275 	zoneid_t zoneid;
9276 	ip_stack_t	*ipst;
9277 
9278 	ip1dbg(("ip_sioctl_tmyaddr"));
9279 
9280 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9281 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9282 	ipst = CONNQ_TO_IPST(q);
9283 
9284 	/* Existence verified in ip_wput_nondata */
9285 	mp1 = mp->b_cont->b_cont;
9286 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9287 	sin = (sin_t *)&sia->sa_addr;
9288 	switch (sin->sin_family) {
9289 	case AF_INET6: {
9290 		sin6_t *sin6 = (sin6_t *)sin;
9291 
9292 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9293 			ipaddr_t v4_addr;
9294 
9295 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9296 			    v4_addr);
9297 			ire = ire_ctable_lookup(v4_addr, 0,
9298 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9299 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9300 		} else {
9301 			in6_addr_t v6addr;
9302 
9303 			v6addr = sin6->sin6_addr;
9304 			ire = ire_ctable_lookup_v6(&v6addr, 0,
9305 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9306 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9307 		}
9308 		break;
9309 	}
9310 	case AF_INET: {
9311 		ipaddr_t v4addr;
9312 
9313 		v4addr = sin->sin_addr.s_addr;
9314 		ire = ire_ctable_lookup(v4addr, 0,
9315 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
9316 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst);
9317 		break;
9318 	}
9319 	default:
9320 		return (EAFNOSUPPORT);
9321 	}
9322 	if (ire != NULL) {
9323 		sia->sa_res = 1;
9324 		ire_refrele(ire);
9325 	} else {
9326 		sia->sa_res = 0;
9327 	}
9328 	return (0);
9329 }
9330 
9331 /*
9332  * Check if this is an address assigned on-link i.e. neighbor,
9333  * and makes sure it's reachable from the current zone.
9334  * Returns true for my addresses as well.
9335  * Translates mapped addresses to v4 addresses and then
9336  * treats them as such, returning true if the v4 address
9337  * associated with this mapped address is configured.
9338  * Note: Applications will have to be careful what they do
9339  * with the response; use of mapped addresses limits
9340  * what can be done with the socket, especially with
9341  * respect to socket options and ioctls - neither IPv4
9342  * options nor IPv6 sticky options/ancillary data options
9343  * may be used.
9344  */
9345 /* ARGSUSED */
9346 int
9347 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9348     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
9349 {
9350 	struct sioc_addrreq *sia;
9351 	sin_t *sin;
9352 	mblk_t	*mp1;
9353 	ire_t *ire = NULL;
9354 	zoneid_t zoneid;
9355 	ip_stack_t	*ipst;
9356 
9357 	ip1dbg(("ip_sioctl_tonlink"));
9358 
9359 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
9360 	zoneid = Q_TO_CONN(q)->conn_zoneid;
9361 	ipst = CONNQ_TO_IPST(q);
9362 
9363 	/* Existence verified in ip_wput_nondata */
9364 	mp1 = mp->b_cont->b_cont;
9365 	sia = (struct sioc_addrreq *)mp1->b_rptr;
9366 	sin = (sin_t *)&sia->sa_addr;
9367 
9368 	/*
9369 	 * Match addresses with a zero gateway field to avoid
9370 	 * routes going through a router.
9371 	 * Exclude broadcast and multicast addresses.
9372 	 */
9373 	switch (sin->sin_family) {
9374 	case AF_INET6: {
9375 		sin6_t *sin6 = (sin6_t *)sin;
9376 
9377 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
9378 			ipaddr_t v4_addr;
9379 
9380 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
9381 			    v4_addr);
9382 			if (!CLASSD(v4_addr)) {
9383 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
9384 				    NULL, NULL, zoneid, NULL,
9385 				    MATCH_IRE_GW, ipst);
9386 			}
9387 		} else {
9388 			in6_addr_t v6addr;
9389 			in6_addr_t v6gw;
9390 
9391 			v6addr = sin6->sin6_addr;
9392 			v6gw = ipv6_all_zeros;
9393 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
9394 				ire = ire_route_lookup_v6(&v6addr, 0,
9395 				    &v6gw, 0, NULL, NULL, zoneid,
9396 				    NULL, MATCH_IRE_GW, ipst);
9397 			}
9398 		}
9399 		break;
9400 	}
9401 	case AF_INET: {
9402 		ipaddr_t v4addr;
9403 
9404 		v4addr = sin->sin_addr.s_addr;
9405 		if (!CLASSD(v4addr)) {
9406 			ire = ire_route_lookup(v4addr, 0, 0, 0,
9407 			    NULL, NULL, zoneid, NULL,
9408 			    MATCH_IRE_GW, ipst);
9409 		}
9410 		break;
9411 	}
9412 	default:
9413 		return (EAFNOSUPPORT);
9414 	}
9415 	sia->sa_res = 0;
9416 	if (ire != NULL) {
9417 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
9418 		    IRE_LOCAL|IRE_LOOPBACK)) {
9419 			sia->sa_res = 1;
9420 		}
9421 		ire_refrele(ire);
9422 	}
9423 	return (0);
9424 }
9425 
9426 /*
9427  * TBD: implement when kernel maintaines a list of site prefixes.
9428  */
9429 /* ARGSUSED */
9430 int
9431 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9432     ip_ioctl_cmd_t *ipip, void *ifreq)
9433 {
9434 	return (ENXIO);
9435 }
9436 
9437 /* ARGSUSED */
9438 int
9439 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9440     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9441 {
9442 	ill_t  		*ill;
9443 	mblk_t		*mp1;
9444 	conn_t		*connp;
9445 	boolean_t	success;
9446 
9447 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
9448 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9449 	/* ioctl comes down on an conn */
9450 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9451 	connp = Q_TO_CONN(q);
9452 
9453 	mp->b_datap->db_type = M_IOCTL;
9454 
9455 	/*
9456 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
9457 	 * The original mp contains contaminated b_next values due to 'mi',
9458 	 * which is needed to do the mi_copy_done. Unfortunately if we
9459 	 * send down the original mblk itself and if we are popped due to an
9460 	 * an unplumb before the response comes back from tunnel,
9461 	 * the streamhead (which does a freemsg) will see this contaminated
9462 	 * message and the assertion in freemsg about non-null b_next/b_prev
9463 	 * will panic a DEBUG kernel.
9464 	 */
9465 	mp1 = copymsg(mp);
9466 	if (mp1 == NULL)
9467 		return (ENOMEM);
9468 
9469 	ill = ipif->ipif_ill;
9470 	mutex_enter(&connp->conn_lock);
9471 	mutex_enter(&ill->ill_lock);
9472 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
9473 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
9474 		    mp, 0);
9475 	} else {
9476 		success = ill_pending_mp_add(ill, connp, mp);
9477 	}
9478 	mutex_exit(&ill->ill_lock);
9479 	mutex_exit(&connp->conn_lock);
9480 
9481 	if (success) {
9482 		ip1dbg(("sending down tunparam request "));
9483 		putnext(ill->ill_wq, mp1);
9484 		return (EINPROGRESS);
9485 	} else {
9486 		/* The conn has started closing */
9487 		freemsg(mp1);
9488 		return (EINTR);
9489 	}
9490 }
9491 
9492 /*
9493  * ARP IOCTLs.
9494  * How does IP get in the business of fronting ARP configuration/queries?
9495  * Well it's like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9496  * are by tradition passed in through a datagram socket.  That lands in IP.
9497  * As it happens, this is just as well since the interface is quite crude in
9498  * that it passes in no information about protocol or hardware types, or
9499  * interface association.  After making the protocol assumption, IP is in
9500  * the position to look up the name of the ILL, which ARP will need, and
9501  * format a request that can be handled by ARP.  The request is passed up
9502  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9503  * back a response.  ARP supports its own set of more general IOCTLs, in
9504  * case anyone is interested.
9505  */
9506 /* ARGSUSED */
9507 int
9508 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9509     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9510 {
9511 	mblk_t *mp1;
9512 	mblk_t *mp2;
9513 	mblk_t *pending_mp;
9514 	ipaddr_t ipaddr;
9515 	area_t *area;
9516 	struct iocblk *iocp;
9517 	conn_t *connp;
9518 	struct arpreq *ar;
9519 	struct xarpreq *xar;
9520 	int flags, alength;
9521 	char *lladdr;
9522 	ip_stack_t	*ipst;
9523 	ill_t *ill = ipif->ipif_ill;
9524 	boolean_t if_arp_ioctl = B_FALSE;
9525 
9526 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9527 	connp = Q_TO_CONN(q);
9528 	ipst = connp->conn_netstack->netstack_ip;
9529 
9530 	if (ipip->ipi_cmd_type == XARP_CMD) {
9531 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
9532 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
9533 		ar = NULL;
9534 
9535 		flags = xar->xarp_flags;
9536 		lladdr = LLADDR(&xar->xarp_ha);
9537 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
9538 		/*
9539 		 * Validate against user's link layer address length
9540 		 * input and name and addr length limits.
9541 		 */
9542 		alength = ill->ill_phys_addr_length;
9543 		if (ipip->ipi_cmd == SIOCSXARP) {
9544 			if (alength != xar->xarp_ha.sdl_alen ||
9545 			    (alength + xar->xarp_ha.sdl_nlen >
9546 			    sizeof (xar->xarp_ha.sdl_data)))
9547 				return (EINVAL);
9548 		}
9549 	} else {
9550 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
9551 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
9552 		xar = NULL;
9553 
9554 		flags = ar->arp_flags;
9555 		lladdr = ar->arp_ha.sa_data;
9556 		/*
9557 		 * Theoretically, the sa_family could tell us what link
9558 		 * layer type this operation is trying to deal with. By
9559 		 * common usage AF_UNSPEC means ethernet. We'll assume
9560 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9561 		 * for now. Our new SIOC*XARP ioctls can be used more
9562 		 * generally.
9563 		 *
9564 		 * If the underlying media happens to have a non 6 byte
9565 		 * address, arp module will fail set/get, but the del
9566 		 * operation will succeed.
9567 		 */
9568 		alength = 6;
9569 		if ((ipip->ipi_cmd != SIOCDARP) &&
9570 		    (alength != ill->ill_phys_addr_length)) {
9571 			return (EINVAL);
9572 		}
9573 	}
9574 
9575 	/*
9576 	 * We are going to pass up to ARP a packet chain that looks
9577 	 * like:
9578 	 *
9579 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9580 	 *
9581 	 * Get a copy of the original IOCTL mblk to head the chain,
9582 	 * to be sent up (in mp1). Also get another copy to store
9583 	 * in the ill_pending_mp list, for matching the response
9584 	 * when it comes back from ARP.
9585 	 */
9586 	mp1 = copyb(mp);
9587 	pending_mp = copymsg(mp);
9588 	if (mp1 == NULL || pending_mp == NULL) {
9589 		if (mp1 != NULL)
9590 			freeb(mp1);
9591 		if (pending_mp != NULL)
9592 			inet_freemsg(pending_mp);
9593 		return (ENOMEM);
9594 	}
9595 
9596 	ipaddr = sin->sin_addr.s_addr;
9597 
9598 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9599 	    (caddr_t)&ipaddr);
9600 	if (mp2 == NULL) {
9601 		freeb(mp1);
9602 		inet_freemsg(pending_mp);
9603 		return (ENOMEM);
9604 	}
9605 	/* Put together the chain. */
9606 	mp1->b_cont = mp2;
9607 	mp1->b_datap->db_type = M_IOCTL;
9608 	mp2->b_cont = mp;
9609 	mp2->b_datap->db_type = M_DATA;
9610 
9611 	iocp = (struct iocblk *)mp1->b_rptr;
9612 
9613 	/*
9614 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9615 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9616 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9617 	 * ioc_count field; set ioc_count to be correct.
9618 	 */
9619 	iocp->ioc_count = MBLKL(mp1->b_cont);
9620 
9621 	/*
9622 	 * Set the proper command in the ARP message.
9623 	 * Convert the SIOC{G|S|D}ARP calls into our
9624 	 * AR_ENTRY_xxx calls.
9625 	 */
9626 	area = (area_t *)mp2->b_rptr;
9627 	switch (iocp->ioc_cmd) {
9628 	case SIOCDARP:
9629 	case SIOCDXARP:
9630 		/*
9631 		 * We defer deleting the corresponding IRE until
9632 		 * we return from arp.
9633 		 */
9634 		area->area_cmd = AR_ENTRY_DELETE;
9635 		area->area_proto_mask_offset = 0;
9636 		break;
9637 	case SIOCGARP:
9638 	case SIOCGXARP:
9639 		area->area_cmd = AR_ENTRY_SQUERY;
9640 		area->area_proto_mask_offset = 0;
9641 		break;
9642 	case SIOCSARP:
9643 	case SIOCSXARP:
9644 		/*
9645 		 * Delete the corresponding ire to make sure IP will
9646 		 * pick up any change from arp.
9647 		 */
9648 		if (!if_arp_ioctl) {
9649 			(void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst);
9650 		} else {
9651 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9652 			if (ipif != NULL) {
9653 				(void) ip_ire_clookup_and_delete(ipaddr, ipif,
9654 				    ipst);
9655 				ipif_refrele(ipif);
9656 			}
9657 		}
9658 		break;
9659 	}
9660 	iocp->ioc_cmd = area->area_cmd;
9661 
9662 	/*
9663 	 * Fill in the rest of the ARP operation fields.
9664 	 */
9665 	area->area_hw_addr_length = alength;
9666 	bcopy(lladdr, (char *)area + area->area_hw_addr_offset, alength);
9667 
9668 	/* Translate the flags. */
9669 	if (flags & ATF_PERM)
9670 		area->area_flags |= ACE_F_PERMANENT;
9671 	if (flags & ATF_PUBL)
9672 		area->area_flags |= ACE_F_PUBLISH;
9673 	if (flags & ATF_AUTHORITY)
9674 		area->area_flags |= ACE_F_AUTHORITY;
9675 
9676 	/*
9677 	 * Before sending 'mp' to ARP, we have to clear the b_next
9678 	 * and b_prev. Otherwise if STREAMS encounters such a message
9679 	 * in freemsg(), (because ARP can close any time) it can cause
9680 	 * a panic. But mi code needs the b_next and b_prev values of
9681 	 * mp->b_cont, to complete the ioctl. So we store it here
9682 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9683 	 * when the response comes down from ARP.
9684 	 */
9685 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9686 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9687 	mp->b_cont->b_next = NULL;
9688 	mp->b_cont->b_prev = NULL;
9689 
9690 	mutex_enter(&connp->conn_lock);
9691 	mutex_enter(&ill->ill_lock);
9692 	/* conn has not yet started closing, hence this can't fail */
9693 	VERIFY(ill_pending_mp_add(ill, connp, pending_mp) != 0);
9694 	mutex_exit(&ill->ill_lock);
9695 	mutex_exit(&connp->conn_lock);
9696 
9697 	/*
9698 	 * Up to ARP it goes.  The response will come back in ip_wput() as an
9699 	 * M_IOCACK, and will be handed to ip_sioctl_iocack() for completion.
9700 	 */
9701 	putnext(ill->ill_rq, mp1);
9702 	return (EINPROGRESS);
9703 }
9704 
9705 /*
9706  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
9707  * the associated sin and refhold and return the associated ipif via `ci'.
9708  */
9709 int
9710 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
9711     cmd_info_t *ci, ipsq_func_t func)
9712 {
9713 	mblk_t	*mp1;
9714 	int	err;
9715 	sin_t	*sin;
9716 	conn_t	*connp;
9717 	ipif_t	*ipif;
9718 	ire_t	*ire = NULL;
9719 	ill_t	*ill = NULL;
9720 	boolean_t exists;
9721 	ip_stack_t *ipst;
9722 	struct arpreq *ar;
9723 	struct xarpreq *xar;
9724 	struct sockaddr_dl *sdl;
9725 
9726 	/* ioctl comes down on a conn */
9727 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9728 	connp = Q_TO_CONN(q);
9729 	if (connp->conn_af_isv6)
9730 		return (ENXIO);
9731 
9732 	ipst = connp->conn_netstack->netstack_ip;
9733 
9734 	/* Verified in ip_wput_nondata */
9735 	mp1 = mp->b_cont->b_cont;
9736 
9737 	if (ipip->ipi_cmd_type == XARP_CMD) {
9738 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
9739 		xar = (struct xarpreq *)mp1->b_rptr;
9740 		sin = (sin_t *)&xar->xarp_pa;
9741 		sdl = &xar->xarp_ha;
9742 
9743 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
9744 			return (ENXIO);
9745 		if (sdl->sdl_nlen >= LIFNAMSIZ)
9746 			return (EINVAL);
9747 	} else {
9748 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
9749 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
9750 		ar = (struct arpreq *)mp1->b_rptr;
9751 		sin = (sin_t *)&ar->arp_pa;
9752 	}
9753 
9754 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
9755 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
9756 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, CONNP_TO_WQ(connp),
9757 		    mp, func, &err, ipst);
9758 		if (ipif == NULL)
9759 			return (err);
9760 		if (ipif->ipif_id != 0 ||
9761 		    ipif->ipif_net_type != IRE_IF_RESOLVER) {
9762 			ipif_refrele(ipif);
9763 			return (ENXIO);
9764 		}
9765 	} else {
9766 		/*
9767 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with sdl_nlen ==
9768 		 * 0: use the IP address to figure out the ill.	 In the IPMP
9769 		 * case, a simple forwarding table lookup will return the
9770 		 * IRE_IF_RESOLVER for the first interface in the group, which
9771 		 * might not be the interface on which the requested IP
9772 		 * address was resolved due to the ill selection algorithm
9773 		 * (see ip_newroute_get_dst_ill()).  So we do a cache table
9774 		 * lookup first: if the IRE cache entry for the IP address is
9775 		 * still there, it will contain the ill pointer for the right
9776 		 * interface, so we use that. If the cache entry has been
9777 		 * flushed, we fall back to the forwarding table lookup. This
9778 		 * should be rare enough since IRE cache entries have a longer
9779 		 * life expectancy than ARP cache entries.
9780 		 */
9781 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL,
9782 		    ipst);
9783 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9784 		    ((ill = ire_to_ill(ire)) == NULL) ||
9785 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9786 			if (ire != NULL)
9787 				ire_refrele(ire);
9788 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9789 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9790 			    NULL, MATCH_IRE_TYPE, ipst);
9791 			if (ire == NULL || ((ill = ire_to_ill(ire)) == NULL)) {
9792 
9793 				if (ire != NULL)
9794 					ire_refrele(ire);
9795 				return (ENXIO);
9796 			}
9797 		}
9798 		ASSERT(ire != NULL && ill != NULL);
9799 		ipif = ill->ill_ipif;
9800 		ipif_refhold(ipif);
9801 		ire_refrele(ire);
9802 	}
9803 	ci->ci_sin = sin;
9804 	ci->ci_ipif = ipif;
9805 	return (0);
9806 }
9807 
9808 /*
9809  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9810  * atomically set/clear the muxids. Also complete the ioctl by acking or
9811  * naking it.  Note that the code is structured such that the link type,
9812  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9813  * its clones use the persistent link, while pppd(1M) and perhaps many
9814  * other daemons may use non-persistent link.  When combined with some
9815  * ill_t states, linking and unlinking lower streams may be used as
9816  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9817  */
9818 /* ARGSUSED */
9819 void
9820 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9821 {
9822 	mblk_t		*mp1, *mp2;
9823 	struct linkblk	*li;
9824 	struct ipmx_s	*ipmxp;
9825 	ill_t		*ill;
9826 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
9827 	int		err = 0;
9828 	boolean_t	entered_ipsq = B_FALSE;
9829 	boolean_t	islink;
9830 	ip_stack_t	*ipst;
9831 
9832 	if (CONN_Q(q))
9833 		ipst = CONNQ_TO_IPST(q);
9834 	else
9835 		ipst = ILLQ_TO_IPST(q);
9836 
9837 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
9838 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
9839 
9840 	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9841 
9842 	mp1 = mp->b_cont;	/* This is the linkblk info */
9843 	li = (struct linkblk *)mp1->b_rptr;
9844 
9845 	/*
9846 	 * ARP has added this special mblk, and the utility is asking us
9847 	 * to perform consistency checks, and also atomically set the
9848 	 * muxid. Ifconfig is an example.  It achieves this by using
9849 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9850 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9851 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9852 	 * and other comments in this routine for more details.
9853 	 */
9854 	mp2 = mp1->b_cont;	/* This is added by ARP */
9855 
9856 	/*
9857 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9858 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9859 	 * get the special mblk above.  For backward compatibility, we
9860 	 * request ip_sioctl_plink_ipmod() to skip the consistency checks.
9861 	 * The utility will use SIOCSLIFMUXID to store the muxids.  This is
9862 	 * not atomic, and can leave the streams unplumbable if the utility
9863 	 * is interrupted before it does the SIOCSLIFMUXID.
9864 	 */
9865 	if (mp2 == NULL) {
9866 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_FALSE);
9867 		if (err == EINPROGRESS)
9868 			return;
9869 		goto done;
9870 	}
9871 
9872 	/*
9873 	 * This is an I_{P}LINK sent down by ifconfig through the ARP module;
9874 	 * ARP has appended this last mblk to tell us whether the lower stream
9875 	 * is an arp-dev stream or an IP module stream.
9876 	 */
9877 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9878 	if (ipmxp->ipmx_arpdev_stream) {
9879 		/*
9880 		 * The lower stream is the arp-dev stream.
9881 		 */
9882 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9883 		    q, mp, ip_sioctl_plink, &err, NULL, ipst);
9884 		if (ill == NULL) {
9885 			if (err == EINPROGRESS)
9886 				return;
9887 			err = EINVAL;
9888 			goto done;
9889 		}
9890 
9891 		if (ipsq == NULL) {
9892 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9893 			    NEW_OP, B_TRUE);
9894 			if (ipsq == NULL) {
9895 				ill_refrele(ill);
9896 				return;
9897 			}
9898 			entered_ipsq = B_TRUE;
9899 		}
9900 		ASSERT(IAM_WRITER_ILL(ill));
9901 		ill_refrele(ill);
9902 
9903 		/*
9904 		 * To ensure consistency between IP and ARP, the following
9905 		 * LIFO scheme is used in plink/punlink. (IP first, ARP last).
9906 		 * This is because the muxid's are stored in the IP stream on
9907 		 * the ill.
9908 		 *
9909 		 * I_{P}LINK: ifconfig plinks the IP stream before plinking
9910 		 * the ARP stream. On an arp-dev stream, IP checks that it is
9911 		 * not yet plinked, and it also checks that the corresponding
9912 		 * IP stream is already plinked.
9913 		 *
9914 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream before
9915 		 * punlinking the IP stream. IP does not allow punlink of the
9916 		 * IP stream unless the arp stream has been punlinked.
9917 		 */
9918 		if ((islink &&
9919 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9920 		    (!islink && ill->ill_arp_muxid != li->l_index)) {
9921 			err = EINVAL;
9922 			goto done;
9923 		}
9924 		ill->ill_arp_muxid = islink ? li->l_index : 0;
9925 	} else {
9926 		/*
9927 		 * The lower stream is probably an IP module stream.  Do
9928 		 * consistency checking.
9929 		 */
9930 		err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li, B_TRUE);
9931 		if (err == EINPROGRESS)
9932 			return;
9933 	}
9934 done:
9935 	if (err == 0)
9936 		miocack(q, mp, 0, 0);
9937 	else
9938 		miocnak(q, mp, 0, err);
9939 
9940 	/* Conn was refheld in ip_sioctl_copyin_setup */
9941 	if (CONN_Q(q))
9942 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9943 	if (entered_ipsq)
9944 		ipsq_exit(ipsq);
9945 }
9946 
9947 /*
9948  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
9949  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
9950  * module stream).  If `doconsist' is set, then do the extended consistency
9951  * checks requested by ifconfig(1M) and (atomically) set ill_ip_muxid here.
9952  * Returns zero on success, EINPROGRESS if the operation is still pending, or
9953  * an error code on failure.
9954  */
9955 static int
9956 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
9957     struct linkblk *li, boolean_t doconsist)
9958 {
9959 	ill_t  		*ill;
9960 	queue_t		*ipwq, *dwq;
9961 	const char	*name;
9962 	struct qinit	*qinfo;
9963 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
9964 	boolean_t	entered_ipsq = B_FALSE;
9965 
9966 	/*
9967 	 * Walk the lower stream to verify it's the IP module stream.
9968 	 * The IP module is identified by its name, wput function,
9969 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
9970 	 * (li->l_qbot) will not vanish until this ioctl completes.
9971 	 */
9972 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
9973 		qinfo = ipwq->q_qinfo;
9974 		name = qinfo->qi_minfo->mi_idname;
9975 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
9976 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
9977 			break;
9978 		}
9979 	}
9980 
9981 	/*
9982 	 * If this isn't an IP module stream, bail.
9983 	 */
9984 	if (ipwq == NULL)
9985 		return (0);
9986 
9987 	ill = ipwq->q_ptr;
9988 	ASSERT(ill != NULL);
9989 
9990 	if (ipsq == NULL) {
9991 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9992 		    NEW_OP, B_TRUE);
9993 		if (ipsq == NULL)
9994 			return (EINPROGRESS);
9995 		entered_ipsq = B_TRUE;
9996 	}
9997 	ASSERT(IAM_WRITER_ILL(ill));
9998 
9999 	if (doconsist) {
10000 		/*
10001 		 * Consistency checking requires that I_{P}LINK occurs
10002 		 * prior to setting ill_ip_muxid, and that I_{P}UNLINK
10003 		 * occurs prior to clearing ill_arp_muxid.
10004 		 */
10005 		if ((islink && ill->ill_ip_muxid != 0) ||
10006 		    (!islink && ill->ill_arp_muxid != 0)) {
10007 			if (entered_ipsq)
10008 				ipsq_exit(ipsq);
10009 			return (EINVAL);
10010 		}
10011 	}
10012 
10013 	/*
10014 	 * As part of I_{P}LINKing, stash the number of downstream modules and
10015 	 * the read queue of the module immediately below IP in the ill.
10016 	 * These are used during the capability negotiation below.
10017 	 */
10018 	ill->ill_lmod_rq = NULL;
10019 	ill->ill_lmod_cnt = 0;
10020 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
10021 		ill->ill_lmod_rq = RD(dwq);
10022 		for (; dwq != NULL; dwq = dwq->q_next)
10023 			ill->ill_lmod_cnt++;
10024 	}
10025 
10026 	if (doconsist)
10027 		ill->ill_ip_muxid = islink ? li->l_index : 0;
10028 
10029 	/*
10030 	 * If there's at least one up ipif on this ill, then we're bound to
10031 	 * the underlying driver via DLPI.  In that case, renegotiate
10032 	 * capabilities to account for any possible change in modules
10033 	 * interposed between IP and the driver.
10034 	 */
10035 	if (ill->ill_ipif_up_count > 0) {
10036 		if (islink)
10037 			ill_capability_probe(ill);
10038 		else
10039 			ill_capability_reset(ill);
10040 	}
10041 
10042 	if (entered_ipsq)
10043 		ipsq_exit(ipsq);
10044 
10045 	return (0);
10046 }
10047 
10048 /*
10049  * Search the ioctl command in the ioctl tables and return a pointer
10050  * to the ioctl command information. The ioctl command tables are
10051  * static and fully populated at compile time.
10052  */
10053 ip_ioctl_cmd_t *
10054 ip_sioctl_lookup(int ioc_cmd)
10055 {
10056 	int index;
10057 	ip_ioctl_cmd_t *ipip;
10058 	ip_ioctl_cmd_t *ipip_end;
10059 
10060 	if (ioc_cmd == IPI_DONTCARE)
10061 		return (NULL);
10062 
10063 	/*
10064 	 * Do a 2 step search. First search the indexed table
10065 	 * based on the least significant byte of the ioctl cmd.
10066 	 * If we don't find a match, then search the misc table
10067 	 * serially.
10068 	 */
10069 	index = ioc_cmd & 0xFF;
10070 	if (index < ip_ndx_ioctl_count) {
10071 		ipip = &ip_ndx_ioctl_table[index];
10072 		if (ipip->ipi_cmd == ioc_cmd) {
10073 			/* Found a match in the ndx table */
10074 			return (ipip);
10075 		}
10076 	}
10077 
10078 	/* Search the misc table */
10079 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
10080 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
10081 		if (ipip->ipi_cmd == ioc_cmd)
10082 			/* Found a match in the misc table */
10083 			return (ipip);
10084 	}
10085 
10086 	return (NULL);
10087 }
10088 
10089 /*
10090  * Wrapper function for resuming deferred ioctl processing
10091  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
10092  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
10093  */
10094 /* ARGSUSED */
10095 void
10096 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
10097     void *dummy_arg)
10098 {
10099 	ip_sioctl_copyin_setup(q, mp);
10100 }
10101 
10102 /*
10103  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
10104  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
10105  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
10106  * We establish here the size of the block to be copied in.  mi_copyin
10107  * arranges for this to happen, an processing continues in ip_wput with
10108  * an M_IOCDATA message.
10109  */
10110 void
10111 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
10112 {
10113 	int	copyin_size;
10114 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10115 	ip_ioctl_cmd_t *ipip;
10116 	cred_t *cr;
10117 	ip_stack_t	*ipst;
10118 
10119 	if (CONN_Q(q))
10120 		ipst = CONNQ_TO_IPST(q);
10121 	else
10122 		ipst = ILLQ_TO_IPST(q);
10123 
10124 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
10125 	if (ipip == NULL) {
10126 		/*
10127 		 * The ioctl is not one we understand or own.
10128 		 * Pass it along to be processed down stream,
10129 		 * if this is a module instance of IP, else nak
10130 		 * the ioctl.
10131 		 */
10132 		if (q->q_next == NULL) {
10133 			goto nak;
10134 		} else {
10135 			putnext(q, mp);
10136 			return;
10137 		}
10138 	}
10139 
10140 	/*
10141 	 * If this is deferred, then we will do all the checks when we
10142 	 * come back.
10143 	 */
10144 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
10145 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
10146 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
10147 		return;
10148 	}
10149 
10150 	/*
10151 	 * Only allow a very small subset of IP ioctls on this stream if
10152 	 * IP is a module and not a driver. Allowing ioctls to be processed
10153 	 * in this case may cause assert failures or data corruption.
10154 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
10155 	 * ioctls allowed on an IP module stream, after which this stream
10156 	 * normally becomes a multiplexor (at which time the stream head
10157 	 * will fail all ioctls).
10158 	 */
10159 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
10160 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
10161 			/*
10162 			 * Pass common Streams ioctls which the IP
10163 			 * module does not own or consume along to
10164 			 * be processed down stream.
10165 			 */
10166 			putnext(q, mp);
10167 			return;
10168 		} else {
10169 			goto nak;
10170 		}
10171 	}
10172 
10173 	/* Make sure we have ioctl data to process. */
10174 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
10175 		goto nak;
10176 
10177 	/*
10178 	 * Prefer dblk credential over ioctl credential; some synthesized
10179 	 * ioctls have kcred set because there's no way to crhold()
10180 	 * a credential in some contexts.  (ioc_cr is not crfree() by
10181 	 * the framework; the caller of ioctl needs to hold the reference
10182 	 * for the duration of the call).
10183 	 */
10184 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
10185 
10186 	/* Make sure normal users don't send down privileged ioctls */
10187 	if ((ipip->ipi_flags & IPI_PRIV) &&
10188 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
10189 		/* We checked the privilege earlier but log it here */
10190 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
10191 		return;
10192 	}
10193 
10194 	/*
10195 	 * The ioctl command tables can only encode fixed length
10196 	 * ioctl data. If the length is variable, the table will
10197 	 * encode the length as zero. Such special cases are handled
10198 	 * below in the switch.
10199 	 */
10200 	if (ipip->ipi_copyin_size != 0) {
10201 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
10202 		return;
10203 	}
10204 
10205 	switch (iocp->ioc_cmd) {
10206 	case O_SIOCGIFCONF:
10207 	case SIOCGIFCONF:
10208 		/*
10209 		 * This IOCTL is hilarious.  See comments in
10210 		 * ip_sioctl_get_ifconf for the story.
10211 		 */
10212 		if (iocp->ioc_count == TRANSPARENT)
10213 			copyin_size = SIZEOF_STRUCT(ifconf,
10214 			    iocp->ioc_flag);
10215 		else
10216 			copyin_size = iocp->ioc_count;
10217 		mi_copyin(q, mp, NULL, copyin_size);
10218 		return;
10219 
10220 	case O_SIOCGLIFCONF:
10221 	case SIOCGLIFCONF:
10222 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
10223 		mi_copyin(q, mp, NULL, copyin_size);
10224 		return;
10225 
10226 	case SIOCGLIFSRCOF:
10227 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
10228 		mi_copyin(q, mp, NULL, copyin_size);
10229 		return;
10230 	case SIOCGIP6ADDRPOLICY:
10231 		ip_sioctl_ip6addrpolicy(q, mp);
10232 		ip6_asp_table_refrele(ipst);
10233 		return;
10234 
10235 	case SIOCSIP6ADDRPOLICY:
10236 		ip_sioctl_ip6addrpolicy(q, mp);
10237 		return;
10238 
10239 	case SIOCGDSTINFO:
10240 		ip_sioctl_dstinfo(q, mp);
10241 		ip6_asp_table_refrele(ipst);
10242 		return;
10243 
10244 	case I_PLINK:
10245 	case I_PUNLINK:
10246 	case I_LINK:
10247 	case I_UNLINK:
10248 		/*
10249 		 * We treat non-persistent link similarly as the persistent
10250 		 * link case, in terms of plumbing/unplumbing, as well as
10251 		 * dynamic re-plumbing events indicator.  See comments
10252 		 * in ip_sioctl_plink() for more.
10253 		 *
10254 		 * Request can be enqueued in the 'ipsq' while waiting
10255 		 * to become exclusive. So bump up the conn ref.
10256 		 */
10257 		if (CONN_Q(q))
10258 			CONN_INC_REF(Q_TO_CONN(q));
10259 		ip_sioctl_plink(NULL, q, mp, NULL);
10260 		return;
10261 
10262 	case ND_GET:
10263 	case ND_SET:
10264 		/*
10265 		 * Use of the nd table requires holding the reader lock.
10266 		 * Modifying the nd table thru nd_load/nd_unload requires
10267 		 * the writer lock.
10268 		 */
10269 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
10270 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
10271 			rw_exit(&ipst->ips_ip_g_nd_lock);
10272 
10273 			if (iocp->ioc_error)
10274 				iocp->ioc_count = 0;
10275 			mp->b_datap->db_type = M_IOCACK;
10276 			qreply(q, mp);
10277 			return;
10278 		}
10279 		rw_exit(&ipst->ips_ip_g_nd_lock);
10280 		/*
10281 		 * We don't understand this subioctl of ND_GET / ND_SET.
10282 		 * Maybe intended for some driver / module below us
10283 		 */
10284 		if (q->q_next) {
10285 			putnext(q, mp);
10286 		} else {
10287 			iocp->ioc_error = ENOENT;
10288 			mp->b_datap->db_type = M_IOCNAK;
10289 			iocp->ioc_count = 0;
10290 			qreply(q, mp);
10291 		}
10292 		return;
10293 
10294 	case IP_IOCTL:
10295 		ip_wput_ioctl(q, mp);
10296 		return;
10297 	default:
10298 		cmn_err(CE_PANIC, "should not happen ");
10299 	}
10300 nak:
10301 	if (mp->b_cont != NULL) {
10302 		freemsg(mp->b_cont);
10303 		mp->b_cont = NULL;
10304 	}
10305 	iocp->ioc_error = EINVAL;
10306 	mp->b_datap->db_type = M_IOCNAK;
10307 	iocp->ioc_count = 0;
10308 	qreply(q, mp);
10309 }
10310 
10311 /* ip_wput hands off ARP IOCTL responses to us */
10312 void
10313 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
10314 {
10315 	struct arpreq *ar;
10316 	struct xarpreq *xar;
10317 	area_t	*area;
10318 	mblk_t	*area_mp;
10319 	struct iocblk *iocp;
10320 	mblk_t	*orig_ioc_mp, *tmp;
10321 	struct iocblk	*orig_iocp;
10322 	ill_t *ill;
10323 	conn_t *connp = NULL;
10324 	uint_t ioc_id;
10325 	mblk_t *pending_mp;
10326 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
10327 	int *flagsp;
10328 	char *storage = NULL;
10329 	sin_t *sin;
10330 	ipaddr_t addr;
10331 	int err;
10332 	ip_stack_t *ipst;
10333 
10334 	ill = q->q_ptr;
10335 	ASSERT(ill != NULL);
10336 	ipst = ill->ill_ipst;
10337 
10338 	/*
10339 	 * We should get back from ARP a packet chain that looks like:
10340 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
10341 	 */
10342 	if (!(area_mp = mp->b_cont) ||
10343 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
10344 	    !(orig_ioc_mp = area_mp->b_cont) ||
10345 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
10346 		freemsg(mp);
10347 		return;
10348 	}
10349 
10350 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
10351 
10352 	tmp = (orig_ioc_mp->b_cont)->b_cont;
10353 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
10354 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
10355 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
10356 		x_arp_ioctl = B_TRUE;
10357 		xar = (struct xarpreq *)tmp->b_rptr;
10358 		sin = (sin_t *)&xar->xarp_pa;
10359 		flagsp = &xar->xarp_flags;
10360 		storage = xar->xarp_ha.sdl_data;
10361 		if (xar->xarp_ha.sdl_nlen != 0)
10362 			ifx_arp_ioctl = B_TRUE;
10363 	} else {
10364 		ar = (struct arpreq *)tmp->b_rptr;
10365 		sin = (sin_t *)&ar->arp_pa;
10366 		flagsp = &ar->arp_flags;
10367 		storage = ar->arp_ha.sa_data;
10368 	}
10369 
10370 	iocp = (struct iocblk *)mp->b_rptr;
10371 
10372 	/*
10373 	 * Pick out the originating queue based on the ioc_id.
10374 	 */
10375 	ioc_id = iocp->ioc_id;
10376 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
10377 	if (pending_mp == NULL) {
10378 		ASSERT(connp == NULL);
10379 		inet_freemsg(mp);
10380 		return;
10381 	}
10382 	ASSERT(connp != NULL);
10383 	q = CONNP_TO_WQ(connp);
10384 
10385 	/* Uncouple the internally generated IOCTL from the original one */
10386 	area = (area_t *)area_mp->b_rptr;
10387 	area_mp->b_cont = NULL;
10388 
10389 	/*
10390 	 * Restore the b_next and b_prev used by mi code. This is needed
10391 	 * to complete the ioctl using mi* functions. We stored them in
10392 	 * the pending mp prior to sending the request to ARP.
10393 	 */
10394 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
10395 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
10396 	inet_freemsg(pending_mp);
10397 
10398 	/*
10399 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
10400 	 * Catch the case where there is an IRE_CACHE by no entry in the
10401 	 * arp table.
10402 	 */
10403 	addr = sin->sin_addr.s_addr;
10404 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
10405 		ire_t			*ire;
10406 		dl_unitdata_req_t	*dlup;
10407 		mblk_t			*llmp;
10408 		int			addr_len;
10409 		ill_t			*ipsqill = NULL;
10410 
10411 		if (ifx_arp_ioctl) {
10412 			/*
10413 			 * There's no need to lookup the ill, since
10414 			 * we've already done that when we started
10415 			 * processing the ioctl and sent the message
10416 			 * to ARP on that ill.  So use the ill that
10417 			 * is stored in q->q_ptr.
10418 			 */
10419 			ipsqill = ill;
10420 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10421 			    ipsqill->ill_ipif, ALL_ZONES,
10422 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
10423 		} else {
10424 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
10425 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
10426 			if (ire != NULL)
10427 				ipsqill = ire_to_ill(ire);
10428 		}
10429 
10430 		if ((x_arp_ioctl) && (ipsqill != NULL))
10431 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
10432 
10433 		if (ire != NULL) {
10434 			/*
10435 			 * Since the ire obtained from cachetable is used for
10436 			 * mac addr copying below, treat an incomplete ire as if
10437 			 * as if we never found it.
10438 			 */
10439 			if (ire->ire_nce != NULL &&
10440 			    ire->ire_nce->nce_state != ND_REACHABLE) {
10441 				ire_refrele(ire);
10442 				ire = NULL;
10443 				ipsqill = NULL;
10444 				goto errack;
10445 			}
10446 			*flagsp = ATF_INUSE;
10447 			llmp = (ire->ire_nce != NULL ?
10448 			    ire->ire_nce->nce_res_mp : NULL);
10449 			if (llmp != NULL && ipsqill != NULL) {
10450 				uchar_t *macaddr;
10451 
10452 				addr_len = ipsqill->ill_phys_addr_length;
10453 				if (x_arp_ioctl && ((addr_len +
10454 				    ipsqill->ill_name_length) >
10455 				    sizeof (xar->xarp_ha.sdl_data))) {
10456 					ire_refrele(ire);
10457 					freemsg(mp);
10458 					ip_ioctl_finish(q, orig_ioc_mp,
10459 					    EINVAL, NO_COPYOUT, NULL);
10460 					return;
10461 				}
10462 				*flagsp |= ATF_COM;
10463 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10464 				if (ipsqill->ill_sap_length < 0)
10465 					macaddr = llmp->b_rptr +
10466 					    dlup->dl_dest_addr_offset;
10467 				else
10468 					macaddr = llmp->b_rptr +
10469 					    dlup->dl_dest_addr_offset +
10470 					    ipsqill->ill_sap_length;
10471 				/*
10472 				 * For SIOCGARP, MAC address length
10473 				 * validation has already been done
10474 				 * before the ioctl was issued to ARP to
10475 				 * allow it to progress only on 6 byte
10476 				 * addressable (ethernet like) media. Thus
10477 				 * the mac address copying can not overwrite
10478 				 * the sa_data area below.
10479 				 */
10480 				bcopy(macaddr, storage, addr_len);
10481 			}
10482 			/* Ditch the internal IOCTL. */
10483 			freemsg(mp);
10484 			ire_refrele(ire);
10485 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10486 			return;
10487 		}
10488 	}
10489 
10490 	/*
10491 	 * Delete the coresponding IRE_CACHE if any.
10492 	 * Reset the error if there was one (in case there was no entry
10493 	 * in arp.)
10494 	 */
10495 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10496 		ipif_t *ipintf = NULL;
10497 
10498 		if (ifx_arp_ioctl) {
10499 			/*
10500 			 * There's no need to lookup the ill, since
10501 			 * we've already done that when we started
10502 			 * processing the ioctl and sent the message
10503 			 * to ARP on that ill.  So use the ill that
10504 			 * is stored in q->q_ptr.
10505 			 */
10506 			ipintf = ill->ill_ipif;
10507 		}
10508 		if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) {
10509 			/*
10510 			 * The address in "addr" may be an entry for a
10511 			 * router. If that's true, then any off-net
10512 			 * IRE_CACHE entries that go through the router
10513 			 * with address "addr" must be clobbered. Use
10514 			 * ire_walk to achieve this goal.
10515 			 */
10516 			if (ifx_arp_ioctl)
10517 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10518 				    ire_delete_cache_gw, (char *)&addr, ill);
10519 			else
10520 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10521 				    ALL_ZONES, ipst);
10522 			iocp->ioc_error = 0;
10523 		}
10524 	}
10525 errack:
10526 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10527 		err = iocp->ioc_error;
10528 		freemsg(mp);
10529 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL);
10530 		return;
10531 	}
10532 
10533 	/*
10534 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10535 	 * the area_t into the struct {x}arpreq.
10536 	 */
10537 	if (x_arp_ioctl) {
10538 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10539 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10540 		    sizeof (xar->xarp_ha.sdl_data)) {
10541 			freemsg(mp);
10542 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT,
10543 			    NULL);
10544 			return;
10545 		}
10546 	}
10547 	*flagsp = ATF_INUSE;
10548 	if (area->area_flags & ACE_F_PERMANENT)
10549 		*flagsp |= ATF_PERM;
10550 	if (area->area_flags & ACE_F_PUBLISH)
10551 		*flagsp |= ATF_PUBL;
10552 	if (area->area_flags & ACE_F_AUTHORITY)
10553 		*flagsp |= ATF_AUTHORITY;
10554 	if (area->area_hw_addr_length != 0) {
10555 		*flagsp |= ATF_COM;
10556 		/*
10557 		 * For SIOCGARP, MAC address length validation has
10558 		 * already been done before the ioctl was issued to ARP
10559 		 * to allow it to progress only on 6 byte addressable
10560 		 * (ethernet like) media. Thus the mac address copying
10561 		 * can not overwrite the sa_data area below.
10562 		 */
10563 		bcopy((char *)area + area->area_hw_addr_offset,
10564 		    storage, area->area_hw_addr_length);
10565 	}
10566 
10567 	/* Ditch the internal IOCTL. */
10568 	freemsg(mp);
10569 	/* Complete the original. */
10570 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL);
10571 }
10572 
10573 /*
10574  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10575  * interface) create the next available logical interface for this
10576  * physical interface.
10577  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10578  * ipif with the specified name.
10579  *
10580  * If the address family is not AF_UNSPEC then set the address as well.
10581  *
10582  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10583  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10584  *
10585  * Executed as a writer on the ill or ill group.
10586  * So no lock is needed to traverse the ipif chain, or examine the
10587  * phyint flags.
10588  */
10589 /* ARGSUSED */
10590 int
10591 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10592     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10593 {
10594 	mblk_t	*mp1;
10595 	struct lifreq *lifr;
10596 	boolean_t	isv6;
10597 	boolean_t	exists;
10598 	char 	*name;
10599 	char	*endp;
10600 	char	*cp;
10601 	int	namelen;
10602 	ipif_t	*ipif;
10603 	long	id;
10604 	ipsq_t	*ipsq;
10605 	ill_t	*ill;
10606 	sin_t	*sin;
10607 	int	err = 0;
10608 	boolean_t found_sep = B_FALSE;
10609 	conn_t	*connp;
10610 	zoneid_t zoneid;
10611 	int	orig_ifindex = 0;
10612 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
10613 
10614 	ASSERT(q->q_next == NULL);
10615 	ip1dbg(("ip_sioctl_addif\n"));
10616 	/* Existence of mp1 has been checked in ip_wput_nondata */
10617 	mp1 = mp->b_cont->b_cont;
10618 	/*
10619 	 * Null terminate the string to protect against buffer
10620 	 * overrun. String was generated by user code and may not
10621 	 * be trusted.
10622 	 */
10623 	lifr = (struct lifreq *)mp1->b_rptr;
10624 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10625 	name = lifr->lifr_name;
10626 	ASSERT(CONN_Q(q));
10627 	connp = Q_TO_CONN(q);
10628 	isv6 = connp->conn_af_isv6;
10629 	zoneid = connp->conn_zoneid;
10630 	namelen = mi_strlen(name);
10631 	if (namelen == 0)
10632 		return (EINVAL);
10633 
10634 	exists = B_FALSE;
10635 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10636 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10637 		/*
10638 		 * Allow creating lo0 using SIOCLIFADDIF.
10639 		 * can't be any other writer thread. So can pass null below
10640 		 * for the last 4 args to ipif_lookup_name.
10641 		 */
10642 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
10643 		    &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst);
10644 		/* Prevent any further action */
10645 		if (ipif == NULL) {
10646 			return (ENOBUFS);
10647 		} else if (!exists) {
10648 			/* We created the ipif now and as writer */
10649 			ipif_refrele(ipif);
10650 			return (0);
10651 		} else {
10652 			ill = ipif->ipif_ill;
10653 			ill_refhold(ill);
10654 			ipif_refrele(ipif);
10655 		}
10656 	} else {
10657 		/* Look for a colon in the name. */
10658 		endp = &name[namelen];
10659 		for (cp = endp; --cp > name; ) {
10660 			if (*cp == IPIF_SEPARATOR_CHAR) {
10661 				found_sep = B_TRUE;
10662 				/*
10663 				 * Reject any non-decimal aliases for plumbing
10664 				 * of logical interfaces. Aliases with leading
10665 				 * zeroes are also rejected as they introduce
10666 				 * ambiguity in the naming of the interfaces.
10667 				 * Comparing with "0" takes care of all such
10668 				 * cases.
10669 				 */
10670 				if ((strncmp("0", cp+1, 1)) == 0)
10671 					return (EINVAL);
10672 
10673 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10674 				    id <= 0 || *endp != '\0') {
10675 					return (EINVAL);
10676 				}
10677 				*cp = '\0';
10678 				break;
10679 			}
10680 		}
10681 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10682 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst);
10683 		if (found_sep)
10684 			*cp = IPIF_SEPARATOR_CHAR;
10685 		if (ill == NULL)
10686 			return (err);
10687 	}
10688 
10689 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10690 	    B_TRUE);
10691 
10692 	/*
10693 	 * Release the refhold due to the lookup, now that we are excl
10694 	 * or we are just returning
10695 	 */
10696 	ill_refrele(ill);
10697 
10698 	if (ipsq == NULL)
10699 		return (EINPROGRESS);
10700 
10701 	/*
10702 	 * If the interface is failed, inactive or offlined, look for a working
10703 	 * interface in the ill group and create the ipif there. If we can't
10704 	 * find a good interface, create the ipif anyway so that in.mpathd can
10705 	 * move it to the first repaired interface.
10706 	 */
10707 	if ((ill->ill_phyint->phyint_flags &
10708 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10709 	    ill->ill_phyint->phyint_groupname_len != 0) {
10710 		phyint_t *phyi;
10711 		char *groupname = ill->ill_phyint->phyint_groupname;
10712 
10713 		/*
10714 		 * We're looking for a working interface, but it doesn't matter
10715 		 * if it's up or down; so instead of following the group lists,
10716 		 * we look at each physical interface and compare the groupname.
10717 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10718 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10719 		 * Otherwise we create the ipif on the failed interface.
10720 		 */
10721 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10722 		phyi = avl_first(&ipst->ips_phyint_g_list->
10723 		    phyint_list_avl_by_index);
10724 		for (; phyi != NULL;
10725 		    phyi = avl_walk(&ipst->ips_phyint_g_list->
10726 		    phyint_list_avl_by_index,
10727 		    phyi, AVL_AFTER)) {
10728 			if (phyi->phyint_groupname_len == 0)
10729 				continue;
10730 			ASSERT(phyi->phyint_groupname != NULL);
10731 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10732 			    !(phyi->phyint_flags &
10733 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10734 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10735 			    (phyi->phyint_illv4 != NULL))) {
10736 				break;
10737 			}
10738 		}
10739 		rw_exit(&ipst->ips_ill_g_lock);
10740 
10741 		if (phyi != NULL) {
10742 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10743 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10744 			    phyi->phyint_illv4);
10745 		}
10746 	}
10747 
10748 	/*
10749 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10750 	 * before or after us.
10751 	 */
10752 	ASSERT(IAM_WRITER_ILL(ill));
10753 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10754 
10755 	if (found_sep && orig_ifindex == 0) {
10756 		/* Now see if there is an IPIF with this unit number. */
10757 		for (ipif = ill->ill_ipif; ipif != NULL;
10758 		    ipif = ipif->ipif_next) {
10759 			if (ipif->ipif_id == id) {
10760 				err = EEXIST;
10761 				goto done;
10762 			}
10763 		}
10764 	}
10765 
10766 	/*
10767 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10768 	 * of lo0. We never come here when we plumb lo0:0. It
10769 	 * happens in ipif_lookup_on_name.
10770 	 * The specified unit number is ignored when we create the ipif on a
10771 	 * different interface. However, we save it in ipif_orig_ipifid below so
10772 	 * that the ipif fails back to the right position.
10773 	 */
10774 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10775 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10776 		err = ENOBUFS;
10777 		goto done;
10778 	}
10779 
10780 	/* Return created name with ioctl */
10781 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10782 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10783 	ip1dbg(("created %s\n", lifr->lifr_name));
10784 
10785 	/* Set address */
10786 	sin = (sin_t *)&lifr->lifr_addr;
10787 	if (sin->sin_family != AF_UNSPEC) {
10788 		err = ip_sioctl_addr(ipif, sin, q, mp,
10789 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10790 	}
10791 
10792 	/* Set ifindex and unit number for failback */
10793 	if (err == 0 && orig_ifindex != 0) {
10794 		ipif->ipif_orig_ifindex = orig_ifindex;
10795 		if (found_sep) {
10796 			ipif->ipif_orig_ipifid = id;
10797 		}
10798 	}
10799 
10800 done:
10801 	ipsq_exit(ipsq);
10802 	return (err);
10803 }
10804 
10805 /*
10806  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10807  * interface) delete it based on the IP address (on this physical interface).
10808  * Otherwise delete it based on the ipif_id.
10809  * Also, special handling to allow a removeif of lo0.
10810  */
10811 /* ARGSUSED */
10812 int
10813 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10814     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10815 {
10816 	conn_t		*connp;
10817 	ill_t		*ill = ipif->ipif_ill;
10818 	boolean_t	 success;
10819 	ip_stack_t	*ipst;
10820 
10821 	ipst = CONNQ_TO_IPST(q);
10822 
10823 	ASSERT(q->q_next == NULL);
10824 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10825 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10826 	ASSERT(IAM_WRITER_IPIF(ipif));
10827 
10828 	connp = Q_TO_CONN(q);
10829 	/*
10830 	 * Special case for unplumbing lo0 (the loopback physical interface).
10831 	 * If unplumbing lo0, the incoming address structure has been
10832 	 * initialized to all zeros. When unplumbing lo0, all its logical
10833 	 * interfaces must be removed too.
10834 	 *
10835 	 * Note that this interface may be called to remove a specific
10836 	 * loopback logical interface (eg, lo0:1). But in that case
10837 	 * ipif->ipif_id != 0 so that the code path for that case is the
10838 	 * same as any other interface (meaning it skips the code directly
10839 	 * below).
10840 	 */
10841 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10842 		if (sin->sin_family == AF_UNSPEC &&
10843 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10844 			/*
10845 			 * Mark it condemned. No new ref. will be made to ill.
10846 			 */
10847 			mutex_enter(&ill->ill_lock);
10848 			ill->ill_state_flags |= ILL_CONDEMNED;
10849 			for (ipif = ill->ill_ipif; ipif != NULL;
10850 			    ipif = ipif->ipif_next) {
10851 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10852 			}
10853 			mutex_exit(&ill->ill_lock);
10854 
10855 			ipif = ill->ill_ipif;
10856 			/* unplumb the loopback interface */
10857 			ill_delete(ill);
10858 			mutex_enter(&connp->conn_lock);
10859 			mutex_enter(&ill->ill_lock);
10860 			ASSERT(ill->ill_group == NULL);
10861 
10862 			/* Are any references to this ill active */
10863 			if (ill_is_freeable(ill)) {
10864 				mutex_exit(&ill->ill_lock);
10865 				mutex_exit(&connp->conn_lock);
10866 				ill_delete_tail(ill);
10867 				mutex_enter(&ill->ill_lock);
10868 				ill_nic_info_dispatch(ill);
10869 				mutex_exit(&ill->ill_lock);
10870 				mi_free(ill);
10871 				return (0);
10872 			}
10873 			success = ipsq_pending_mp_add(connp, ipif,
10874 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10875 			mutex_exit(&connp->conn_lock);
10876 			mutex_exit(&ill->ill_lock);
10877 			if (success)
10878 				return (EINPROGRESS);
10879 			else
10880 				return (EINTR);
10881 		}
10882 	}
10883 
10884 	/*
10885 	 * We are exclusive on the ipsq, so an ill move will be serialized
10886 	 * before or after us.
10887 	 */
10888 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10889 
10890 	if (ipif->ipif_id == 0) {
10891 
10892 		ipsq_t *ipsq;
10893 
10894 		/* Find based on address */
10895 		if (ipif->ipif_isv6) {
10896 			sin6_t *sin6;
10897 
10898 			if (sin->sin_family != AF_INET6)
10899 				return (EAFNOSUPPORT);
10900 
10901 			sin6 = (sin6_t *)sin;
10902 			/* We are a writer, so we should be able to lookup */
10903 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10904 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
10905 			if (ipif == NULL) {
10906 				/*
10907 				 * Maybe the address in on another interface in
10908 				 * the same IPMP group? We check this below.
10909 				 */
10910 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10911 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL,
10912 				    ipst);
10913 			}
10914 		} else {
10915 			ipaddr_t addr;
10916 
10917 			if (sin->sin_family != AF_INET)
10918 				return (EAFNOSUPPORT);
10919 
10920 			addr = sin->sin_addr.s_addr;
10921 			/* We are a writer, so we should be able to lookup */
10922 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10923 			    NULL, NULL, NULL, ipst);
10924 			if (ipif == NULL) {
10925 				/*
10926 				 * Maybe the address in on another interface in
10927 				 * the same IPMP group? We check this below.
10928 				 */
10929 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10930 				    NULL, NULL, NULL, NULL, ipst);
10931 			}
10932 		}
10933 		if (ipif == NULL) {
10934 			return (EADDRNOTAVAIL);
10935 		}
10936 
10937 		/*
10938 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
10939 		 * lifr_name of the physical interface but with an ip address
10940 		 * lifr_addr of a logical interface plumbed over it.
10941 		 * So update ipsq_current_ipif once ipif points to the
10942 		 * correct interface after doing ipif_lookup_addr().
10943 		 */
10944 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
10945 		ASSERT(ipsq != NULL);
10946 
10947 		mutex_enter(&ipsq->ipsq_lock);
10948 		ipsq->ipsq_current_ipif = ipif;
10949 		mutex_exit(&ipsq->ipsq_lock);
10950 
10951 		/*
10952 		 * When the address to be removed is hosted on a different
10953 		 * interface, we check if the interface is in the same IPMP
10954 		 * group as the specified one; if so we proceed with the
10955 		 * removal.
10956 		 * ill->ill_group is NULL when the ill is down, so we have to
10957 		 * compare the group names instead.
10958 		 */
10959 		if (ipif->ipif_ill != ill &&
10960 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10961 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10962 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10963 		    ill->ill_phyint->phyint_groupname) != 0)) {
10964 			ipif_refrele(ipif);
10965 			return (EADDRNOTAVAIL);
10966 		}
10967 
10968 		/* This is a writer */
10969 		ipif_refrele(ipif);
10970 	}
10971 
10972 	/*
10973 	 * Can not delete instance zero since it is tied to the ill.
10974 	 */
10975 	if (ipif->ipif_id == 0)
10976 		return (EBUSY);
10977 
10978 	mutex_enter(&ill->ill_lock);
10979 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10980 	mutex_exit(&ill->ill_lock);
10981 
10982 	ipif_free(ipif);
10983 
10984 	mutex_enter(&connp->conn_lock);
10985 	mutex_enter(&ill->ill_lock);
10986 
10987 
10988 	/* Are any references to this ipif active */
10989 	if (ipif_is_freeable(ipif)) {
10990 		mutex_exit(&ill->ill_lock);
10991 		mutex_exit(&connp->conn_lock);
10992 		ipif_non_duplicate(ipif);
10993 		ipif_down_tail(ipif);
10994 		ipif_free_tail(ipif); /* frees ipif */
10995 		return (0);
10996 	}
10997 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10998 	    IPIF_FREE);
10999 	mutex_exit(&ill->ill_lock);
11000 	mutex_exit(&connp->conn_lock);
11001 	if (success)
11002 		return (EINPROGRESS);
11003 	else
11004 		return (EINTR);
11005 }
11006 
11007 /*
11008  * Restart the removeif ioctl. The refcnt has gone down to 0.
11009  * The ipif is already condemned. So can't find it thru lookups.
11010  */
11011 /* ARGSUSED */
11012 int
11013 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
11014     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
11015 {
11016 	ill_t *ill = ipif->ipif_ill;
11017 
11018 	ASSERT(IAM_WRITER_IPIF(ipif));
11019 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
11020 
11021 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
11022 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11023 
11024 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
11025 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
11026 		ill_delete_tail(ill);
11027 		mutex_enter(&ill->ill_lock);
11028 		ill_nic_info_dispatch(ill);
11029 		mutex_exit(&ill->ill_lock);
11030 		mi_free(ill);
11031 		return (0);
11032 	}
11033 
11034 	ipif_non_duplicate(ipif);
11035 	ipif_down_tail(ipif);
11036 	ipif_free_tail(ipif);
11037 
11038 	ILL_UNMARK_CHANGING(ill);
11039 	return (0);
11040 }
11041 
11042 /*
11043  * Set the local interface address.
11044  * Allow an address of all zero when the interface is down.
11045  */
11046 /* ARGSUSED */
11047 int
11048 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11049     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
11050 {
11051 	int err = 0;
11052 	in6_addr_t v6addr;
11053 	boolean_t need_up = B_FALSE;
11054 
11055 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
11056 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11057 
11058 	ASSERT(IAM_WRITER_IPIF(ipif));
11059 
11060 	if (ipif->ipif_isv6) {
11061 		sin6_t *sin6;
11062 		ill_t *ill;
11063 		phyint_t *phyi;
11064 
11065 		if (sin->sin_family != AF_INET6)
11066 			return (EAFNOSUPPORT);
11067 
11068 		sin6 = (sin6_t *)sin;
11069 		v6addr = sin6->sin6_addr;
11070 		ill = ipif->ipif_ill;
11071 		phyi = ill->ill_phyint;
11072 
11073 		/*
11074 		 * Enforce that true multicast interfaces have a link-local
11075 		 * address for logical unit 0.
11076 		 */
11077 		if (ipif->ipif_id == 0 &&
11078 		    (ill->ill_flags & ILLF_MULTICAST) &&
11079 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
11080 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
11081 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
11082 			return (EADDRNOTAVAIL);
11083 		}
11084 
11085 		/*
11086 		 * up interfaces shouldn't have the unspecified address
11087 		 * unless they also have the IPIF_NOLOCAL flags set and
11088 		 * have a subnet assigned.
11089 		 */
11090 		if ((ipif->ipif_flags & IPIF_UP) &&
11091 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
11092 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
11093 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
11094 			return (EADDRNOTAVAIL);
11095 		}
11096 
11097 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11098 			return (EADDRNOTAVAIL);
11099 	} else {
11100 		ipaddr_t addr;
11101 
11102 		if (sin->sin_family != AF_INET)
11103 			return (EAFNOSUPPORT);
11104 
11105 		addr = sin->sin_addr.s_addr;
11106 
11107 		/* Allow 0 as the local address. */
11108 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11109 			return (EADDRNOTAVAIL);
11110 
11111 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11112 	}
11113 
11114 
11115 	/*
11116 	 * Even if there is no change we redo things just to rerun
11117 	 * ipif_set_default.
11118 	 */
11119 	if (ipif->ipif_flags & IPIF_UP) {
11120 		/*
11121 		 * Setting a new local address, make sure
11122 		 * we have net and subnet bcast ire's for
11123 		 * the old address if we need them.
11124 		 */
11125 		if (!ipif->ipif_isv6)
11126 			ipif_check_bcast_ires(ipif);
11127 		/*
11128 		 * If the interface is already marked up,
11129 		 * we call ipif_down which will take care
11130 		 * of ditching any IREs that have been set
11131 		 * up based on the old interface address.
11132 		 */
11133 		err = ipif_logical_down(ipif, q, mp);
11134 		if (err == EINPROGRESS)
11135 			return (err);
11136 		ipif_down_tail(ipif);
11137 		need_up = 1;
11138 	}
11139 
11140 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
11141 	return (err);
11142 }
11143 
11144 int
11145 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11146     boolean_t need_up)
11147 {
11148 	in6_addr_t v6addr;
11149 	in6_addr_t ov6addr;
11150 	ipaddr_t addr;
11151 	sin6_t	*sin6;
11152 	int	sinlen;
11153 	int	err = 0;
11154 	ill_t	*ill = ipif->ipif_ill;
11155 	boolean_t need_dl_down;
11156 	boolean_t need_arp_down;
11157 	struct iocblk *iocp;
11158 
11159 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
11160 
11161 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
11162 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
11163 	ASSERT(IAM_WRITER_IPIF(ipif));
11164 
11165 	/* Must cancel any pending timer before taking the ill_lock */
11166 	if (ipif->ipif_recovery_id != 0)
11167 		(void) untimeout(ipif->ipif_recovery_id);
11168 	ipif->ipif_recovery_id = 0;
11169 
11170 	if (ipif->ipif_isv6) {
11171 		sin6 = (sin6_t *)sin;
11172 		v6addr = sin6->sin6_addr;
11173 		sinlen = sizeof (struct sockaddr_in6);
11174 	} else {
11175 		addr = sin->sin_addr.s_addr;
11176 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11177 		sinlen = sizeof (struct sockaddr_in);
11178 	}
11179 	mutex_enter(&ill->ill_lock);
11180 	ov6addr = ipif->ipif_v6lcl_addr;
11181 	ipif->ipif_v6lcl_addr = v6addr;
11182 	sctp_update_ipif_addr(ipif, ov6addr);
11183 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
11184 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11185 	} else {
11186 		ipif->ipif_v6src_addr = v6addr;
11187 	}
11188 	ipif->ipif_addr_ready = 0;
11189 
11190 	/*
11191 	 * If the interface was previously marked as a duplicate, then since
11192 	 * we've now got a "new" address, it should no longer be considered a
11193 	 * duplicate -- even if the "new" address is the same as the old one.
11194 	 * Note that if all ipifs are down, we may have a pending ARP down
11195 	 * event to handle.  This is because we want to recover from duplicates
11196 	 * and thus delay tearing down ARP until the duplicates have been
11197 	 * removed or disabled.
11198 	 */
11199 	need_dl_down = need_arp_down = B_FALSE;
11200 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11201 		need_arp_down = !need_up;
11202 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11203 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11204 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11205 			need_dl_down = B_TRUE;
11206 		}
11207 	}
11208 
11209 	if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) &&
11210 	    !ill->ill_is_6to4tun) {
11211 		queue_t *wqp = ill->ill_wq;
11212 
11213 		/*
11214 		 * The local address of this interface is a 6to4 address,
11215 		 * check if this interface is in fact a 6to4 tunnel or just
11216 		 * an interface configured with a 6to4 address.  We are only
11217 		 * interested in the former.
11218 		 */
11219 		if (wqp != NULL) {
11220 			while ((wqp->q_next != NULL) &&
11221 			    (wqp->q_next->q_qinfo != NULL) &&
11222 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
11223 
11224 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
11225 				    == TUN6TO4_MODID) {
11226 					/* set for use in IP */
11227 					ill->ill_is_6to4tun = 1;
11228 					break;
11229 				}
11230 				wqp = wqp->q_next;
11231 			}
11232 		}
11233 	}
11234 
11235 	ipif_set_default(ipif);
11236 
11237 	/*
11238 	 * When publishing an interface address change event, we only notify
11239 	 * the event listeners of the new address.  It is assumed that if they
11240 	 * actively care about the addresses assigned that they will have
11241 	 * already discovered the previous address assigned (if there was one.)
11242 	 *
11243 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
11244 	 */
11245 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
11246 		(void) ill_hook_event_create(ill, MAP_IPIF_ID(ipif->ipif_id),
11247 		    NE_ADDRESS_CHANGE, sin, sinlen);
11248 	}
11249 
11250 	mutex_exit(&ill->ill_lock);
11251 
11252 	if (need_up) {
11253 		/*
11254 		 * Now bring the interface back up.  If this
11255 		 * is the only IPIF for the ILL, ipif_up
11256 		 * will have to re-bind to the device, so
11257 		 * we may get back EINPROGRESS, in which
11258 		 * case, this IOCTL will get completed in
11259 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11260 		 */
11261 		err = ipif_up(ipif, q, mp);
11262 	}
11263 
11264 	if (need_dl_down)
11265 		ill_dl_down(ill);
11266 	if (need_arp_down)
11267 		ipif_arp_down(ipif);
11268 
11269 	return (err);
11270 }
11271 
11272 
11273 /*
11274  * Restart entry point to restart the address set operation after the
11275  * refcounts have dropped to zero.
11276  */
11277 /* ARGSUSED */
11278 int
11279 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11280     ip_ioctl_cmd_t *ipip, void *ifreq)
11281 {
11282 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
11283 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11284 	ASSERT(IAM_WRITER_IPIF(ipif));
11285 	ipif_down_tail(ipif);
11286 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
11287 }
11288 
11289 /* ARGSUSED */
11290 int
11291 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11292     ip_ioctl_cmd_t *ipip, void *if_req)
11293 {
11294 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
11295 	struct lifreq *lifr = (struct lifreq *)if_req;
11296 
11297 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
11298 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11299 	/*
11300 	 * The net mask and address can't change since we have a
11301 	 * reference to the ipif. So no lock is necessary.
11302 	 */
11303 	if (ipif->ipif_isv6) {
11304 		*sin6 = sin6_null;
11305 		sin6->sin6_family = AF_INET6;
11306 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
11307 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11308 		lifr->lifr_addrlen =
11309 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11310 	} else {
11311 		*sin = sin_null;
11312 		sin->sin_family = AF_INET;
11313 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
11314 		if (ipip->ipi_cmd_type == LIF_CMD) {
11315 			lifr->lifr_addrlen =
11316 			    ip_mask_to_plen(ipif->ipif_net_mask);
11317 		}
11318 	}
11319 	return (0);
11320 }
11321 
11322 /*
11323  * Set the destination address for a pt-pt interface.
11324  */
11325 /* ARGSUSED */
11326 int
11327 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11328     ip_ioctl_cmd_t *ipip, void *if_req)
11329 {
11330 	int err = 0;
11331 	in6_addr_t v6addr;
11332 	boolean_t need_up = B_FALSE;
11333 
11334 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
11335 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11336 	ASSERT(IAM_WRITER_IPIF(ipif));
11337 
11338 	if (ipif->ipif_isv6) {
11339 		sin6_t *sin6;
11340 
11341 		if (sin->sin_family != AF_INET6)
11342 			return (EAFNOSUPPORT);
11343 
11344 		sin6 = (sin6_t *)sin;
11345 		v6addr = sin6->sin6_addr;
11346 
11347 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
11348 			return (EADDRNOTAVAIL);
11349 	} else {
11350 		ipaddr_t addr;
11351 
11352 		if (sin->sin_family != AF_INET)
11353 			return (EAFNOSUPPORT);
11354 
11355 		addr = sin->sin_addr.s_addr;
11356 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
11357 			return (EADDRNOTAVAIL);
11358 
11359 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11360 	}
11361 
11362 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
11363 		return (0);	/* No change */
11364 
11365 	if (ipif->ipif_flags & IPIF_UP) {
11366 		/*
11367 		 * If the interface is already marked up,
11368 		 * we call ipif_down which will take care
11369 		 * of ditching any IREs that have been set
11370 		 * up based on the old pp dst address.
11371 		 */
11372 		err = ipif_logical_down(ipif, q, mp);
11373 		if (err == EINPROGRESS)
11374 			return (err);
11375 		ipif_down_tail(ipif);
11376 		need_up = B_TRUE;
11377 	}
11378 	/*
11379 	 * could return EINPROGRESS. If so ioctl will complete in
11380 	 * ip_rput_dlpi_writer
11381 	 */
11382 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
11383 	return (err);
11384 }
11385 
11386 static int
11387 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11388     boolean_t need_up)
11389 {
11390 	in6_addr_t v6addr;
11391 	ill_t	*ill = ipif->ipif_ill;
11392 	int	err = 0;
11393 	boolean_t need_dl_down;
11394 	boolean_t need_arp_down;
11395 
11396 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
11397 	    ipif->ipif_id, (void *)ipif));
11398 
11399 	/* Must cancel any pending timer before taking the ill_lock */
11400 	if (ipif->ipif_recovery_id != 0)
11401 		(void) untimeout(ipif->ipif_recovery_id);
11402 	ipif->ipif_recovery_id = 0;
11403 
11404 	if (ipif->ipif_isv6) {
11405 		sin6_t *sin6;
11406 
11407 		sin6 = (sin6_t *)sin;
11408 		v6addr = sin6->sin6_addr;
11409 	} else {
11410 		ipaddr_t addr;
11411 
11412 		addr = sin->sin_addr.s_addr;
11413 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11414 	}
11415 	mutex_enter(&ill->ill_lock);
11416 	/* Set point to point destination address. */
11417 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11418 		/*
11419 		 * Allow this as a means of creating logical
11420 		 * pt-pt interfaces on top of e.g. an Ethernet.
11421 		 * XXX Undocumented HACK for testing.
11422 		 * pt-pt interfaces are created with NUD disabled.
11423 		 */
11424 		ipif->ipif_flags |= IPIF_POINTOPOINT;
11425 		ipif->ipif_flags &= ~IPIF_BROADCAST;
11426 		if (ipif->ipif_isv6)
11427 			ill->ill_flags |= ILLF_NONUD;
11428 	}
11429 
11430 	/*
11431 	 * If the interface was previously marked as a duplicate, then since
11432 	 * we've now got a "new" address, it should no longer be considered a
11433 	 * duplicate -- even if the "new" address is the same as the old one.
11434 	 * Note that if all ipifs are down, we may have a pending ARP down
11435 	 * event to handle.
11436 	 */
11437 	need_dl_down = need_arp_down = B_FALSE;
11438 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
11439 		need_arp_down = !need_up;
11440 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
11441 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
11442 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
11443 			need_dl_down = B_TRUE;
11444 		}
11445 	}
11446 
11447 	/* Set the new address. */
11448 	ipif->ipif_v6pp_dst_addr = v6addr;
11449 	/* Make sure subnet tracks pp_dst */
11450 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
11451 	mutex_exit(&ill->ill_lock);
11452 
11453 	if (need_up) {
11454 		/*
11455 		 * Now bring the interface back up.  If this
11456 		 * is the only IPIF for the ILL, ipif_up
11457 		 * will have to re-bind to the device, so
11458 		 * we may get back EINPROGRESS, in which
11459 		 * case, this IOCTL will get completed in
11460 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11461 		 */
11462 		err = ipif_up(ipif, q, mp);
11463 	}
11464 
11465 	if (need_dl_down)
11466 		ill_dl_down(ill);
11467 
11468 	if (need_arp_down)
11469 		ipif_arp_down(ipif);
11470 	return (err);
11471 }
11472 
11473 /*
11474  * Restart entry point to restart the dstaddress set operation after the
11475  * refcounts have dropped to zero.
11476  */
11477 /* ARGSUSED */
11478 int
11479 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11480     ip_ioctl_cmd_t *ipip, void *ifreq)
11481 {
11482 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
11483 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11484 	ipif_down_tail(ipif);
11485 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
11486 }
11487 
11488 /* ARGSUSED */
11489 int
11490 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11491     ip_ioctl_cmd_t *ipip, void *if_req)
11492 {
11493 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
11494 
11495 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
11496 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11497 	/*
11498 	 * Get point to point destination address. The addresses can't
11499 	 * change since we hold a reference to the ipif.
11500 	 */
11501 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
11502 		return (EADDRNOTAVAIL);
11503 
11504 	if (ipif->ipif_isv6) {
11505 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11506 		*sin6 = sin6_null;
11507 		sin6->sin6_family = AF_INET6;
11508 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
11509 	} else {
11510 		*sin = sin_null;
11511 		sin->sin_family = AF_INET;
11512 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
11513 	}
11514 	return (0);
11515 }
11516 
11517 /*
11518  * part of ipmp, make this func return the active/inactive state and
11519  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
11520  */
11521 /*
11522  * This function either sets or clears the IFF_INACTIVE flag.
11523  *
11524  * As long as there are some addresses or multicast memberships on the
11525  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
11526  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
11527  * will be used for outbound packets.
11528  *
11529  * Caller needs to verify the validity of setting IFF_INACTIVE.
11530  */
11531 static void
11532 phyint_inactive(phyint_t *phyi)
11533 {
11534 	ill_t *ill_v4;
11535 	ill_t *ill_v6;
11536 	ipif_t *ipif;
11537 	ilm_t *ilm;
11538 
11539 	ill_v4 = phyi->phyint_illv4;
11540 	ill_v6 = phyi->phyint_illv6;
11541 
11542 	/*
11543 	 * No need for a lock while traversing the list since iam
11544 	 * a writer
11545 	 */
11546 	if (ill_v4 != NULL) {
11547 		ASSERT(IAM_WRITER_ILL(ill_v4));
11548 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
11549 		    ipif = ipif->ipif_next) {
11550 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11551 				mutex_enter(&phyi->phyint_lock);
11552 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11553 				mutex_exit(&phyi->phyint_lock);
11554 				return;
11555 			}
11556 		}
11557 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
11558 		    ilm = ilm->ilm_next) {
11559 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11560 				mutex_enter(&phyi->phyint_lock);
11561 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11562 				mutex_exit(&phyi->phyint_lock);
11563 				return;
11564 			}
11565 		}
11566 	}
11567 	if (ill_v6 != NULL) {
11568 		ill_v6 = phyi->phyint_illv6;
11569 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11570 		    ipif = ipif->ipif_next) {
11571 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11572 				mutex_enter(&phyi->phyint_lock);
11573 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11574 				mutex_exit(&phyi->phyint_lock);
11575 				return;
11576 			}
11577 		}
11578 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11579 		    ilm = ilm->ilm_next) {
11580 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11581 				mutex_enter(&phyi->phyint_lock);
11582 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11583 				mutex_exit(&phyi->phyint_lock);
11584 				return;
11585 			}
11586 		}
11587 	}
11588 	mutex_enter(&phyi->phyint_lock);
11589 	phyi->phyint_flags |= PHYI_INACTIVE;
11590 	mutex_exit(&phyi->phyint_lock);
11591 }
11592 
11593 /*
11594  * This function is called only when the phyint flags change. Currently
11595  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11596  * that we can select a good ill.
11597  */
11598 static void
11599 ip_redo_nomination(phyint_t *phyi)
11600 {
11601 	ill_t *ill_v4;
11602 
11603 	ill_v4 = phyi->phyint_illv4;
11604 
11605 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11606 		ASSERT(IAM_WRITER_ILL(ill_v4));
11607 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11608 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11609 	}
11610 }
11611 
11612 /*
11613  * Heuristic to check if ill is INACTIVE.
11614  * Checks if ill has an ipif with an usable ip address.
11615  *
11616  * Return values:
11617  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11618  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11619  */
11620 static boolean_t
11621 ill_is_inactive(ill_t *ill)
11622 {
11623 	ipif_t *ipif;
11624 
11625 	/* Check whether it is in an IPMP group */
11626 	if (ill->ill_phyint->phyint_groupname == NULL)
11627 		return (B_FALSE);
11628 
11629 	if (ill->ill_ipif_up_count == 0)
11630 		return (B_TRUE);
11631 
11632 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11633 		uint64_t flags = ipif->ipif_flags;
11634 
11635 		/*
11636 		 * This ipif is usable if it is IPIF_UP and not a
11637 		 * dedicated test address.  A dedicated test address
11638 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11639 		 * (note in particular that V6 test addresses are
11640 		 * link-local data addresses and thus are marked
11641 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11642 		 */
11643 		if ((flags & IPIF_UP) &&
11644 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11645 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11646 			return (B_FALSE);
11647 	}
11648 	return (B_TRUE);
11649 }
11650 
11651 /*
11652  * Set interface flags.
11653  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11654  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11655  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11656  *
11657  * NOTE : We really don't enforce that ipif_id zero should be used
11658  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11659  *	  is because applications generally does SICGLIFFLAGS and
11660  *	  ORs in the new flags (that affects the logical) and does a
11661  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11662  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11663  *	  flags that will be turned on is correct with respect to
11664  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11665  */
11666 /* ARGSUSED */
11667 int
11668 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11669     ip_ioctl_cmd_t *ipip, void *if_req)
11670 {
11671 	uint64_t turn_on;
11672 	uint64_t turn_off;
11673 	int	err;
11674 	boolean_t need_up = B_FALSE;
11675 	phyint_t *phyi;
11676 	ill_t *ill;
11677 	uint64_t intf_flags;
11678 	boolean_t phyint_flags_modified = B_FALSE;
11679 	uint64_t flags;
11680 	struct ifreq *ifr;
11681 	struct lifreq *lifr;
11682 	boolean_t set_linklocal = B_FALSE;
11683 	boolean_t zero_source = B_FALSE;
11684 	ip_stack_t *ipst;
11685 
11686 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11687 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11688 
11689 	ASSERT(IAM_WRITER_IPIF(ipif));
11690 
11691 	ill = ipif->ipif_ill;
11692 	phyi = ill->ill_phyint;
11693 	ipst = ill->ill_ipst;
11694 
11695 	if (ipip->ipi_cmd_type == IF_CMD) {
11696 		ifr = (struct ifreq *)if_req;
11697 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11698 	} else {
11699 		lifr = (struct lifreq *)if_req;
11700 		flags = lifr->lifr_flags;
11701 	}
11702 
11703 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11704 
11705 	/*
11706 	 * Has the flags been set correctly till now ?
11707 	 */
11708 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11709 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11710 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11711 	/*
11712 	 * Compare the new flags to the old, and partition
11713 	 * into those coming on and those going off.
11714 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11715 	 */
11716 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11717 		flags |= intf_flags & ~0xFFFF;
11718 
11719 	/*
11720 	 * First check which bits will change and then which will
11721 	 * go on and off
11722 	 */
11723 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11724 	if (!turn_on)
11725 		return (0);	/* No change */
11726 
11727 	turn_off = intf_flags & turn_on;
11728 	turn_on ^= turn_off;
11729 	err = 0;
11730 
11731 	/*
11732 	 * Don't allow any bits belonging to the logical interface
11733 	 * to be set or cleared on the replacement ipif that was
11734 	 * created temporarily during a MOVE.
11735 	 */
11736 	if (ipif->ipif_replace_zero &&
11737 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11738 		return (EINVAL);
11739 	}
11740 
11741 	/*
11742 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11743 	 * IPv6 interfaces.
11744 	 */
11745 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11746 		return (EINVAL);
11747 
11748 	/*
11749 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
11750 	 */
11751 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
11752 		return (EINVAL);
11753 
11754 	/*
11755 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11756 	 * interfaces.  It makes no sense in that context.
11757 	 */
11758 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11759 		return (EINVAL);
11760 
11761 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11762 		zero_source = B_TRUE;
11763 
11764 	/*
11765 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11766 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11767 	 * If the link local address isn't set, and can be set, it will get
11768 	 * set later on in this function.
11769 	 */
11770 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11771 	    (flags & IFF_UP) && !zero_source &&
11772 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11773 		if (ipif_cant_setlinklocal(ipif))
11774 			return (EINVAL);
11775 		set_linklocal = B_TRUE;
11776 	}
11777 
11778 	/*
11779 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11780 	 * same time. No need to grab ill_g_usesrc_lock here, see
11781 	 * synchronization notes in ip.c
11782 	 */
11783 	if (turn_on & PHYI_STANDBY &&
11784 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11785 		return (EINVAL);
11786 	}
11787 
11788 	/*
11789 	 * If we modify physical interface flags, we'll potentially need to
11790 	 * send up two routing socket messages for the changes (one for the
11791 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11792 	 */
11793 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11794 		phyint_flags_modified = B_TRUE;
11795 
11796 	/*
11797 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11798 	 * we need to flush the IRE_CACHES belonging to this ill.
11799 	 * We handle this case here without doing the DOWN/UP dance
11800 	 * like it is done for other flags. If some other flags are
11801 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11802 	 * below will handle it by bringing it down and then
11803 	 * bringing it UP.
11804 	 */
11805 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11806 		ill_t *ill_v4, *ill_v6;
11807 
11808 		ill_v4 = phyi->phyint_illv4;
11809 		ill_v6 = phyi->phyint_illv6;
11810 
11811 		/*
11812 		 * First set the INACTIVE flag if needed. Then delete the ires.
11813 		 * ire_add will atomically prevent creating new IRE_CACHEs
11814 		 * unless hidden flag is set.
11815 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11816 		 */
11817 		if ((turn_on & PHYI_FAILED) &&
11818 		    ((intf_flags & PHYI_STANDBY) ||
11819 		    !ipst->ips_ipmp_enable_failback)) {
11820 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11821 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11822 		}
11823 		if ((turn_off & PHYI_FAILED) &&
11824 		    ((intf_flags & PHYI_STANDBY) ||
11825 		    (!ipst->ips_ipmp_enable_failback &&
11826 		    ill_is_inactive(ill)))) {
11827 			phyint_inactive(phyi);
11828 		}
11829 
11830 		if (turn_on & PHYI_STANDBY) {
11831 			/*
11832 			 * We implicitly set INACTIVE only when STANDBY is set.
11833 			 * INACTIVE is also set on non-STANDBY phyint when user
11834 			 * disables FAILBACK using configuration file.
11835 			 * Do not allow STANDBY to be set on such INACTIVE
11836 			 * phyint
11837 			 */
11838 			if (phyi->phyint_flags & PHYI_INACTIVE)
11839 				return (EINVAL);
11840 			if (!(phyi->phyint_flags & PHYI_FAILED))
11841 				phyint_inactive(phyi);
11842 		}
11843 		if (turn_off & PHYI_STANDBY) {
11844 			if (ipst->ips_ipmp_enable_failback) {
11845 				/*
11846 				 * Reset PHYI_INACTIVE.
11847 				 */
11848 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11849 			} else if (ill_is_inactive(ill) &&
11850 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11851 				/*
11852 				 * Need to set INACTIVE, when user sets
11853 				 * STANDBY on a non-STANDBY phyint and
11854 				 * later resets STANDBY
11855 				 */
11856 				phyint_inactive(phyi);
11857 			}
11858 		}
11859 		/*
11860 		 * We should always send up a message so that the
11861 		 * daemons come to know of it. Note that the zeroth
11862 		 * interface can be down and the check below for IPIF_UP
11863 		 * will not make sense as we are actually setting
11864 		 * a phyint flag here. We assume that the ipif used
11865 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11866 		 * send up any message for non-zero ipifs).
11867 		 */
11868 		phyint_flags_modified = B_TRUE;
11869 
11870 		if (ill_v4 != NULL) {
11871 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11872 			    IRE_CACHE, ill_stq_cache_delete,
11873 			    (char *)ill_v4, ill_v4);
11874 			illgrp_reset_schednext(ill_v4);
11875 		}
11876 		if (ill_v6 != NULL) {
11877 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11878 			    IRE_CACHE, ill_stq_cache_delete,
11879 			    (char *)ill_v6, ill_v6);
11880 			illgrp_reset_schednext(ill_v6);
11881 		}
11882 	}
11883 
11884 	/*
11885 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11886 	 * status of the interface and, if the interface is part of an IPMP
11887 	 * group, all other interfaces that are part of the same IPMP
11888 	 * group.
11889 	 */
11890 	if ((turn_on | turn_off) & ILLF_ROUTER)
11891 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
11892 
11893 	/*
11894 	 * If the interface is not UP and we are not going to
11895 	 * bring it UP, record the flags and return. When the
11896 	 * interface comes UP later, the right actions will be
11897 	 * taken.
11898 	 */
11899 	if (!(ipif->ipif_flags & IPIF_UP) &&
11900 	    !(turn_on & IPIF_UP)) {
11901 		/* Record new flags in their respective places. */
11902 		mutex_enter(&ill->ill_lock);
11903 		mutex_enter(&ill->ill_phyint->phyint_lock);
11904 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11905 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11906 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11907 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11908 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11909 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11910 		mutex_exit(&ill->ill_lock);
11911 		mutex_exit(&ill->ill_phyint->phyint_lock);
11912 
11913 		/*
11914 		 * We do the broadcast and nomination here rather
11915 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11916 		 * the case of FAILBACK from INACTIVE standby to the
11917 		 * interface that has been repaired, PHYI_FAILED has not
11918 		 * been cleared yet. If there are only two interfaces in
11919 		 * that group, all we have is a FAILED and INACTIVE
11920 		 * interface. If we do the nomination soon after a failback,
11921 		 * the broadcast nomination code would select the
11922 		 * INACTIVE interface for receiving broadcasts as FAILED is
11923 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11924 		 * receive broadcast packets, we need to redo nomination
11925 		 * when the FAILED is cleared here. Thus, in general we
11926 		 * always do the nomination here for FAILED, STANDBY
11927 		 * and OFFLINE.
11928 		 */
11929 		if (((turn_on | turn_off) &
11930 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11931 			ip_redo_nomination(phyi);
11932 		}
11933 		if (phyint_flags_modified) {
11934 			if (phyi->phyint_illv4 != NULL) {
11935 				ip_rts_ifmsg(phyi->phyint_illv4->
11936 				    ill_ipif);
11937 			}
11938 			if (phyi->phyint_illv6 != NULL) {
11939 				ip_rts_ifmsg(phyi->phyint_illv6->
11940 				    ill_ipif);
11941 			}
11942 		}
11943 		return (0);
11944 	} else if (set_linklocal || zero_source) {
11945 		mutex_enter(&ill->ill_lock);
11946 		if (set_linklocal)
11947 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11948 		if (zero_source)
11949 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11950 		mutex_exit(&ill->ill_lock);
11951 	}
11952 
11953 	/*
11954 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11955 	 * or point-to-point interfaces with an unspecified destination. We do
11956 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11957 	 * have a subnet assigned, which is how in.ndpd currently manages its
11958 	 * onlink prefix list when no addresses are configured with those
11959 	 * prefixes.
11960 	 */
11961 	if (ipif->ipif_isv6 &&
11962 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11963 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11964 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11965 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11966 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11967 		return (EINVAL);
11968 	}
11969 
11970 	/*
11971 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11972 	 * from being brought up.
11973 	 */
11974 	if (!ipif->ipif_isv6 &&
11975 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11976 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11977 		return (EINVAL);
11978 	}
11979 
11980 	/*
11981 	 * The only flag changes that we currently take specific action on
11982 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11983 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11984 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11985 	 * the flags and bringing it back up again.
11986 	 */
11987 	if ((turn_on|turn_off) &
11988 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11989 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11990 		/*
11991 		 * Taking this ipif down, make sure we have
11992 		 * valid net and subnet bcast ire's for other
11993 		 * logical interfaces, if we need them.
11994 		 */
11995 		if (!ipif->ipif_isv6)
11996 			ipif_check_bcast_ires(ipif);
11997 
11998 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11999 		    !(turn_off & IPIF_UP)) {
12000 			need_up = B_TRUE;
12001 			if (ipif->ipif_flags & IPIF_UP)
12002 				ill->ill_logical_down = 1;
12003 			turn_on &= ~IPIF_UP;
12004 		}
12005 		err = ipif_down(ipif, q, mp);
12006 		ip1dbg(("ipif_down returns %d err ", err));
12007 		if (err == EINPROGRESS)
12008 			return (err);
12009 		ipif_down_tail(ipif);
12010 	}
12011 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
12012 }
12013 
12014 static int
12015 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
12016     boolean_t need_up)
12017 {
12018 	ill_t	*ill;
12019 	phyint_t *phyi;
12020 	uint64_t turn_on;
12021 	uint64_t turn_off;
12022 	uint64_t intf_flags;
12023 	boolean_t phyint_flags_modified = B_FALSE;
12024 	int	err = 0;
12025 	boolean_t set_linklocal = B_FALSE;
12026 	boolean_t zero_source = B_FALSE;
12027 
12028 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
12029 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12030 
12031 	ASSERT(IAM_WRITER_IPIF(ipif));
12032 
12033 	ill = ipif->ipif_ill;
12034 	phyi = ill->ill_phyint;
12035 
12036 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
12037 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
12038 
12039 	turn_off = intf_flags & turn_on;
12040 	turn_on ^= turn_off;
12041 
12042 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
12043 		phyint_flags_modified = B_TRUE;
12044 
12045 	/*
12046 	 * Now we change the flags. Track current value of
12047 	 * other flags in their respective places.
12048 	 */
12049 	mutex_enter(&ill->ill_lock);
12050 	mutex_enter(&phyi->phyint_lock);
12051 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
12052 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
12053 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
12054 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
12055 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
12056 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
12057 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
12058 		set_linklocal = B_TRUE;
12059 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
12060 	}
12061 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
12062 		zero_source = B_TRUE;
12063 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
12064 	}
12065 	mutex_exit(&ill->ill_lock);
12066 	mutex_exit(&phyi->phyint_lock);
12067 
12068 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
12069 		ip_redo_nomination(phyi);
12070 
12071 	if (set_linklocal)
12072 		(void) ipif_setlinklocal(ipif);
12073 
12074 	if (zero_source)
12075 		ipif->ipif_v6src_addr = ipv6_all_zeros;
12076 	else
12077 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
12078 
12079 	if (need_up) {
12080 		/*
12081 		 * XXX ipif_up really does not know whether a phyint flags
12082 		 * was modified or not. So, it sends up information on
12083 		 * only one routing sockets message. As we don't bring up
12084 		 * the interface and also set STANDBY/FAILED simultaneously
12085 		 * it should be okay.
12086 		 */
12087 		err = ipif_up(ipif, q, mp);
12088 	} else {
12089 		/*
12090 		 * Make sure routing socket sees all changes to the flags.
12091 		 * ipif_up_done* handles this when we use ipif_up.
12092 		 */
12093 		if (phyint_flags_modified) {
12094 			if (phyi->phyint_illv4 != NULL) {
12095 				ip_rts_ifmsg(phyi->phyint_illv4->
12096 				    ill_ipif);
12097 			}
12098 			if (phyi->phyint_illv6 != NULL) {
12099 				ip_rts_ifmsg(phyi->phyint_illv6->
12100 				    ill_ipif);
12101 			}
12102 		} else {
12103 			ip_rts_ifmsg(ipif);
12104 		}
12105 		/*
12106 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
12107 		 * this in need_up case.
12108 		 */
12109 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12110 	}
12111 	return (err);
12112 }
12113 
12114 /*
12115  * Restart entry point to restart the flags restart operation after the
12116  * refcounts have dropped to zero.
12117  */
12118 /* ARGSUSED */
12119 int
12120 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12121     ip_ioctl_cmd_t *ipip, void *if_req)
12122 {
12123 	int	err;
12124 	struct ifreq *ifr = (struct ifreq *)if_req;
12125 	struct lifreq *lifr = (struct lifreq *)if_req;
12126 
12127 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
12128 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12129 
12130 	ipif_down_tail(ipif);
12131 	if (ipip->ipi_cmd_type == IF_CMD) {
12132 		/*
12133 		 * Since ip_sioctl_flags expects an int and ifr_flags
12134 		 * is a short we need to cast ifr_flags into an int
12135 		 * to avoid having sign extension cause bits to get
12136 		 * set that should not be.
12137 		 */
12138 		err = ip_sioctl_flags_tail(ipif,
12139 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
12140 		    q, mp, B_TRUE);
12141 	} else {
12142 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
12143 		    q, mp, B_TRUE);
12144 	}
12145 	return (err);
12146 }
12147 
12148 /*
12149  * Can operate on either a module or a driver queue.
12150  */
12151 /* ARGSUSED */
12152 int
12153 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12154     ip_ioctl_cmd_t *ipip, void *if_req)
12155 {
12156 	/*
12157 	 * Has the flags been set correctly till now ?
12158 	 */
12159 	ill_t *ill = ipif->ipif_ill;
12160 	phyint_t *phyi = ill->ill_phyint;
12161 
12162 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
12163 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12164 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
12165 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
12166 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
12167 
12168 	/*
12169 	 * Need a lock since some flags can be set even when there are
12170 	 * references to the ipif.
12171 	 */
12172 	mutex_enter(&ill->ill_lock);
12173 	if (ipip->ipi_cmd_type == IF_CMD) {
12174 		struct ifreq *ifr = (struct ifreq *)if_req;
12175 
12176 		/* Get interface flags (low 16 only). */
12177 		ifr->ifr_flags = ((ipif->ipif_flags |
12178 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
12179 	} else {
12180 		struct lifreq *lifr = (struct lifreq *)if_req;
12181 
12182 		/* Get interface flags. */
12183 		lifr->lifr_flags = ipif->ipif_flags |
12184 		    ill->ill_flags | phyi->phyint_flags;
12185 	}
12186 	mutex_exit(&ill->ill_lock);
12187 	return (0);
12188 }
12189 
12190 /* ARGSUSED */
12191 int
12192 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12193     ip_ioctl_cmd_t *ipip, void *if_req)
12194 {
12195 	int mtu;
12196 	int ip_min_mtu;
12197 	struct ifreq	*ifr;
12198 	struct lifreq *lifr;
12199 	ire_t	*ire;
12200 	ip_stack_t *ipst;
12201 
12202 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
12203 	    ipif->ipif_id, (void *)ipif));
12204 	if (ipip->ipi_cmd_type == IF_CMD) {
12205 		ifr = (struct ifreq *)if_req;
12206 		mtu = ifr->ifr_metric;
12207 	} else {
12208 		lifr = (struct lifreq *)if_req;
12209 		mtu = lifr->lifr_mtu;
12210 	}
12211 
12212 	if (ipif->ipif_isv6)
12213 		ip_min_mtu = IPV6_MIN_MTU;
12214 	else
12215 		ip_min_mtu = IP_MIN_MTU;
12216 
12217 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
12218 		return (EINVAL);
12219 
12220 	/*
12221 	 * Change the MTU size in all relevant ire's.
12222 	 * Mtu change Vs. new ire creation - protocol below.
12223 	 * First change ipif_mtu and the ire_max_frag of the
12224 	 * interface ire. Then do an ire walk and change the
12225 	 * ire_max_frag of all affected ires. During ire_add
12226 	 * under the bucket lock, set the ire_max_frag of the
12227 	 * new ire being created from the ipif/ire from which
12228 	 * it is being derived. If an mtu change happens after
12229 	 * the ire is added, the new ire will be cleaned up.
12230 	 * Conversely if the mtu change happens before the ire
12231 	 * is added, ire_add will see the new value of the mtu.
12232 	 */
12233 	ipif->ipif_mtu = mtu;
12234 	ipif->ipif_flags |= IPIF_FIXEDMTU;
12235 
12236 	if (ipif->ipif_isv6)
12237 		ire = ipif_to_ire_v6(ipif);
12238 	else
12239 		ire = ipif_to_ire(ipif);
12240 	if (ire != NULL) {
12241 		ire->ire_max_frag = ipif->ipif_mtu;
12242 		ire_refrele(ire);
12243 	}
12244 	ipst = ipif->ipif_ill->ill_ipst;
12245 	if (ipif->ipif_flags & IPIF_UP) {
12246 		if (ipif->ipif_isv6)
12247 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12248 			    ipst);
12249 		else
12250 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES,
12251 			    ipst);
12252 	}
12253 	/* Update the MTU in SCTP's list */
12254 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
12255 	return (0);
12256 }
12257 
12258 /* Get interface MTU. */
12259 /* ARGSUSED */
12260 int
12261 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12262 	ip_ioctl_cmd_t *ipip, void *if_req)
12263 {
12264 	struct ifreq	*ifr;
12265 	struct lifreq	*lifr;
12266 
12267 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
12268 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12269 	if (ipip->ipi_cmd_type == IF_CMD) {
12270 		ifr = (struct ifreq *)if_req;
12271 		ifr->ifr_metric = ipif->ipif_mtu;
12272 	} else {
12273 		lifr = (struct lifreq *)if_req;
12274 		lifr->lifr_mtu = ipif->ipif_mtu;
12275 	}
12276 	return (0);
12277 }
12278 
12279 /* Set interface broadcast address. */
12280 /* ARGSUSED2 */
12281 int
12282 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12283 	ip_ioctl_cmd_t *ipip, void *if_req)
12284 {
12285 	ipaddr_t addr;
12286 	ire_t	*ire;
12287 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12288 
12289 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
12290 	    ipif->ipif_id));
12291 
12292 	ASSERT(IAM_WRITER_IPIF(ipif));
12293 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12294 		return (EADDRNOTAVAIL);
12295 
12296 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
12297 
12298 	if (sin->sin_family != AF_INET)
12299 		return (EAFNOSUPPORT);
12300 
12301 	addr = sin->sin_addr.s_addr;
12302 	if (ipif->ipif_flags & IPIF_UP) {
12303 		/*
12304 		 * If we are already up, make sure the new
12305 		 * broadcast address makes sense.  If it does,
12306 		 * there should be an IRE for it already.
12307 		 * Don't match on ipif, only on the ill
12308 		 * since we are sharing these now. Don't use
12309 		 * MATCH_IRE_ILL_GROUP as we are looking for
12310 		 * the broadcast ire on this ill and each ill
12311 		 * in the group has its own broadcast ire.
12312 		 */
12313 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
12314 		    ipif, ALL_ZONES, NULL,
12315 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst);
12316 		if (ire == NULL) {
12317 			return (EINVAL);
12318 		} else {
12319 			ire_refrele(ire);
12320 		}
12321 	}
12322 	/*
12323 	 * Changing the broadcast addr for this ipif.
12324 	 * Make sure we have valid net and subnet bcast
12325 	 * ire's for other logical interfaces, if needed.
12326 	 */
12327 	if (addr != ipif->ipif_brd_addr)
12328 		ipif_check_bcast_ires(ipif);
12329 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
12330 	return (0);
12331 }
12332 
12333 /* Get interface broadcast address. */
12334 /* ARGSUSED */
12335 int
12336 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12337     ip_ioctl_cmd_t *ipip, void *if_req)
12338 {
12339 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
12340 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12341 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
12342 		return (EADDRNOTAVAIL);
12343 
12344 	/* IPIF_BROADCAST not possible with IPv6 */
12345 	ASSERT(!ipif->ipif_isv6);
12346 	*sin = sin_null;
12347 	sin->sin_family = AF_INET;
12348 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
12349 	return (0);
12350 }
12351 
12352 /*
12353  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
12354  */
12355 /* ARGSUSED */
12356 int
12357 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12358     ip_ioctl_cmd_t *ipip, void *if_req)
12359 {
12360 	int err = 0;
12361 	in6_addr_t v6mask;
12362 
12363 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
12364 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12365 
12366 	ASSERT(IAM_WRITER_IPIF(ipif));
12367 
12368 	if (ipif->ipif_isv6) {
12369 		sin6_t *sin6;
12370 
12371 		if (sin->sin_family != AF_INET6)
12372 			return (EAFNOSUPPORT);
12373 
12374 		sin6 = (sin6_t *)sin;
12375 		v6mask = sin6->sin6_addr;
12376 	} else {
12377 		ipaddr_t mask;
12378 
12379 		if (sin->sin_family != AF_INET)
12380 			return (EAFNOSUPPORT);
12381 
12382 		mask = sin->sin_addr.s_addr;
12383 		V4MASK_TO_V6(mask, v6mask);
12384 	}
12385 
12386 	/*
12387 	 * No big deal if the interface isn't already up, or the mask
12388 	 * isn't really changing, or this is pt-pt.
12389 	 */
12390 	if (!(ipif->ipif_flags & IPIF_UP) ||
12391 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
12392 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
12393 		ipif->ipif_v6net_mask = v6mask;
12394 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12395 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
12396 			    ipif->ipif_v6net_mask,
12397 			    ipif->ipif_v6subnet);
12398 		}
12399 		return (0);
12400 	}
12401 	/*
12402 	 * Make sure we have valid net and subnet broadcast ire's
12403 	 * for the old netmask, if needed by other logical interfaces.
12404 	 */
12405 	if (!ipif->ipif_isv6)
12406 		ipif_check_bcast_ires(ipif);
12407 
12408 	err = ipif_logical_down(ipif, q, mp);
12409 	if (err == EINPROGRESS)
12410 		return (err);
12411 	ipif_down_tail(ipif);
12412 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
12413 	return (err);
12414 }
12415 
12416 static int
12417 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
12418 {
12419 	in6_addr_t v6mask;
12420 	int err = 0;
12421 
12422 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
12423 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12424 
12425 	if (ipif->ipif_isv6) {
12426 		sin6_t *sin6;
12427 
12428 		sin6 = (sin6_t *)sin;
12429 		v6mask = sin6->sin6_addr;
12430 	} else {
12431 		ipaddr_t mask;
12432 
12433 		mask = sin->sin_addr.s_addr;
12434 		V4MASK_TO_V6(mask, v6mask);
12435 	}
12436 
12437 	ipif->ipif_v6net_mask = v6mask;
12438 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12439 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
12440 		    ipif->ipif_v6subnet);
12441 	}
12442 	err = ipif_up(ipif, q, mp);
12443 
12444 	if (err == 0 || err == EINPROGRESS) {
12445 		/*
12446 		 * The interface must be DL_BOUND if this packet has to
12447 		 * go out on the wire. Since we only go through a logical
12448 		 * down and are bound with the driver during an internal
12449 		 * down/up that is satisfied.
12450 		 */
12451 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
12452 			/* Potentially broadcast an address mask reply. */
12453 			ipif_mask_reply(ipif);
12454 		}
12455 	}
12456 	return (err);
12457 }
12458 
12459 /* ARGSUSED */
12460 int
12461 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12462     ip_ioctl_cmd_t *ipip, void *if_req)
12463 {
12464 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
12465 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12466 	ipif_down_tail(ipif);
12467 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
12468 }
12469 
12470 /* Get interface net mask. */
12471 /* ARGSUSED */
12472 int
12473 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12474     ip_ioctl_cmd_t *ipip, void *if_req)
12475 {
12476 	struct lifreq *lifr = (struct lifreq *)if_req;
12477 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
12478 
12479 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
12480 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12481 
12482 	/*
12483 	 * net mask can't change since we have a reference to the ipif.
12484 	 */
12485 	if (ipif->ipif_isv6) {
12486 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12487 		*sin6 = sin6_null;
12488 		sin6->sin6_family = AF_INET6;
12489 		sin6->sin6_addr = ipif->ipif_v6net_mask;
12490 		lifr->lifr_addrlen =
12491 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12492 	} else {
12493 		*sin = sin_null;
12494 		sin->sin_family = AF_INET;
12495 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
12496 		if (ipip->ipi_cmd_type == LIF_CMD) {
12497 			lifr->lifr_addrlen =
12498 			    ip_mask_to_plen(ipif->ipif_net_mask);
12499 		}
12500 	}
12501 	return (0);
12502 }
12503 
12504 /* ARGSUSED */
12505 int
12506 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12507     ip_ioctl_cmd_t *ipip, void *if_req)
12508 {
12509 
12510 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
12511 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12512 	/*
12513 	 * Set interface metric.  We don't use this for
12514 	 * anything but we keep track of it in case it is
12515 	 * important to routing applications or such.
12516 	 */
12517 	if (ipip->ipi_cmd_type == IF_CMD) {
12518 		struct ifreq    *ifr;
12519 
12520 		ifr = (struct ifreq *)if_req;
12521 		ipif->ipif_metric = ifr->ifr_metric;
12522 	} else {
12523 		struct lifreq   *lifr;
12524 
12525 		lifr = (struct lifreq *)if_req;
12526 		ipif->ipif_metric = lifr->lifr_metric;
12527 	}
12528 	return (0);
12529 }
12530 
12531 
12532 /* ARGSUSED */
12533 int
12534 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12535     ip_ioctl_cmd_t *ipip, void *if_req)
12536 {
12537 
12538 	/* Get interface metric. */
12539 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
12540 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12541 	if (ipip->ipi_cmd_type == IF_CMD) {
12542 		struct ifreq    *ifr;
12543 
12544 		ifr = (struct ifreq *)if_req;
12545 		ifr->ifr_metric = ipif->ipif_metric;
12546 	} else {
12547 		struct lifreq   *lifr;
12548 
12549 		lifr = (struct lifreq *)if_req;
12550 		lifr->lifr_metric = ipif->ipif_metric;
12551 	}
12552 
12553 	return (0);
12554 }
12555 
12556 /* ARGSUSED */
12557 int
12558 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12559     ip_ioctl_cmd_t *ipip, void *if_req)
12560 {
12561 
12562 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
12563 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12564 	/*
12565 	 * Set the muxid returned from I_PLINK.
12566 	 */
12567 	if (ipip->ipi_cmd_type == IF_CMD) {
12568 		struct ifreq *ifr = (struct ifreq *)if_req;
12569 
12570 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
12571 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
12572 	} else {
12573 		struct lifreq *lifr = (struct lifreq *)if_req;
12574 
12575 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
12576 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
12577 	}
12578 	return (0);
12579 }
12580 
12581 /* ARGSUSED */
12582 int
12583 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12584     ip_ioctl_cmd_t *ipip, void *if_req)
12585 {
12586 
12587 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
12588 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12589 	/*
12590 	 * Get the muxid saved in ill for I_PUNLINK.
12591 	 */
12592 	if (ipip->ipi_cmd_type == IF_CMD) {
12593 		struct ifreq *ifr = (struct ifreq *)if_req;
12594 
12595 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12596 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12597 	} else {
12598 		struct lifreq *lifr = (struct lifreq *)if_req;
12599 
12600 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12601 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12602 	}
12603 	return (0);
12604 }
12605 
12606 /*
12607  * Set the subnet prefix. Does not modify the broadcast address.
12608  */
12609 /* ARGSUSED */
12610 int
12611 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12612     ip_ioctl_cmd_t *ipip, void *if_req)
12613 {
12614 	int err = 0;
12615 	in6_addr_t v6addr;
12616 	in6_addr_t v6mask;
12617 	boolean_t need_up = B_FALSE;
12618 	int addrlen;
12619 
12620 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12621 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12622 
12623 	ASSERT(IAM_WRITER_IPIF(ipif));
12624 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12625 
12626 	if (ipif->ipif_isv6) {
12627 		sin6_t *sin6;
12628 
12629 		if (sin->sin_family != AF_INET6)
12630 			return (EAFNOSUPPORT);
12631 
12632 		sin6 = (sin6_t *)sin;
12633 		v6addr = sin6->sin6_addr;
12634 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12635 			return (EADDRNOTAVAIL);
12636 	} else {
12637 		ipaddr_t addr;
12638 
12639 		if (sin->sin_family != AF_INET)
12640 			return (EAFNOSUPPORT);
12641 
12642 		addr = sin->sin_addr.s_addr;
12643 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12644 			return (EADDRNOTAVAIL);
12645 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12646 		/* Add 96 bits */
12647 		addrlen += IPV6_ABITS - IP_ABITS;
12648 	}
12649 
12650 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12651 		return (EINVAL);
12652 
12653 	/* Check if bits in the address is set past the mask */
12654 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12655 		return (EINVAL);
12656 
12657 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12658 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12659 		return (0);	/* No change */
12660 
12661 	if (ipif->ipif_flags & IPIF_UP) {
12662 		/*
12663 		 * If the interface is already marked up,
12664 		 * we call ipif_down which will take care
12665 		 * of ditching any IREs that have been set
12666 		 * up based on the old interface address.
12667 		 */
12668 		err = ipif_logical_down(ipif, q, mp);
12669 		if (err == EINPROGRESS)
12670 			return (err);
12671 		ipif_down_tail(ipif);
12672 		need_up = B_TRUE;
12673 	}
12674 
12675 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12676 	return (err);
12677 }
12678 
12679 static int
12680 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12681     queue_t *q, mblk_t *mp, boolean_t need_up)
12682 {
12683 	ill_t	*ill = ipif->ipif_ill;
12684 	int	err = 0;
12685 
12686 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12687 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12688 
12689 	/* Set the new address. */
12690 	mutex_enter(&ill->ill_lock);
12691 	ipif->ipif_v6net_mask = v6mask;
12692 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12693 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12694 		    ipif->ipif_v6subnet);
12695 	}
12696 	mutex_exit(&ill->ill_lock);
12697 
12698 	if (need_up) {
12699 		/*
12700 		 * Now bring the interface back up.  If this
12701 		 * is the only IPIF for the ILL, ipif_up
12702 		 * will have to re-bind to the device, so
12703 		 * we may get back EINPROGRESS, in which
12704 		 * case, this IOCTL will get completed in
12705 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12706 		 */
12707 		err = ipif_up(ipif, q, mp);
12708 		if (err == EINPROGRESS)
12709 			return (err);
12710 	}
12711 	return (err);
12712 }
12713 
12714 /* ARGSUSED */
12715 int
12716 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12717     ip_ioctl_cmd_t *ipip, void *if_req)
12718 {
12719 	int	addrlen;
12720 	in6_addr_t v6addr;
12721 	in6_addr_t v6mask;
12722 	struct lifreq *lifr = (struct lifreq *)if_req;
12723 
12724 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12725 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12726 	ipif_down_tail(ipif);
12727 
12728 	addrlen = lifr->lifr_addrlen;
12729 	if (ipif->ipif_isv6) {
12730 		sin6_t *sin6;
12731 
12732 		sin6 = (sin6_t *)sin;
12733 		v6addr = sin6->sin6_addr;
12734 	} else {
12735 		ipaddr_t addr;
12736 
12737 		addr = sin->sin_addr.s_addr;
12738 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12739 		addrlen += IPV6_ABITS - IP_ABITS;
12740 	}
12741 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12742 
12743 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12744 }
12745 
12746 /* ARGSUSED */
12747 int
12748 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12749     ip_ioctl_cmd_t *ipip, void *if_req)
12750 {
12751 	struct lifreq *lifr = (struct lifreq *)if_req;
12752 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12753 
12754 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12755 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12756 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12757 
12758 	if (ipif->ipif_isv6) {
12759 		*sin6 = sin6_null;
12760 		sin6->sin6_family = AF_INET6;
12761 		sin6->sin6_addr = ipif->ipif_v6subnet;
12762 		lifr->lifr_addrlen =
12763 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12764 	} else {
12765 		*sin = sin_null;
12766 		sin->sin_family = AF_INET;
12767 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12768 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12769 	}
12770 	return (0);
12771 }
12772 
12773 /*
12774  * Set the IPv6 address token.
12775  */
12776 /* ARGSUSED */
12777 int
12778 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12779     ip_ioctl_cmd_t *ipi, void *if_req)
12780 {
12781 	ill_t *ill = ipif->ipif_ill;
12782 	int err;
12783 	in6_addr_t v6addr;
12784 	in6_addr_t v6mask;
12785 	boolean_t need_up = B_FALSE;
12786 	int i;
12787 	sin6_t *sin6 = (sin6_t *)sin;
12788 	struct lifreq *lifr = (struct lifreq *)if_req;
12789 	int addrlen;
12790 
12791 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12792 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12793 	ASSERT(IAM_WRITER_IPIF(ipif));
12794 
12795 	addrlen = lifr->lifr_addrlen;
12796 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12797 	if (ipif->ipif_id != 0)
12798 		return (EINVAL);
12799 
12800 	if (!ipif->ipif_isv6)
12801 		return (EINVAL);
12802 
12803 	if (addrlen > IPV6_ABITS)
12804 		return (EINVAL);
12805 
12806 	v6addr = sin6->sin6_addr;
12807 
12808 	/*
12809 	 * The length of the token is the length from the end.  To get
12810 	 * the proper mask for this, compute the mask of the bits not
12811 	 * in the token; ie. the prefix, and then xor to get the mask.
12812 	 */
12813 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12814 		return (EINVAL);
12815 	for (i = 0; i < 4; i++) {
12816 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12817 	}
12818 
12819 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12820 	    ill->ill_token_length == addrlen)
12821 		return (0);	/* No change */
12822 
12823 	if (ipif->ipif_flags & IPIF_UP) {
12824 		err = ipif_logical_down(ipif, q, mp);
12825 		if (err == EINPROGRESS)
12826 			return (err);
12827 		ipif_down_tail(ipif);
12828 		need_up = B_TRUE;
12829 	}
12830 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12831 	return (err);
12832 }
12833 
12834 static int
12835 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12836     mblk_t *mp, boolean_t need_up)
12837 {
12838 	in6_addr_t v6addr;
12839 	in6_addr_t v6mask;
12840 	ill_t	*ill = ipif->ipif_ill;
12841 	int	i;
12842 	int	err = 0;
12843 
12844 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12845 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12846 	v6addr = sin6->sin6_addr;
12847 	/*
12848 	 * The length of the token is the length from the end.  To get
12849 	 * the proper mask for this, compute the mask of the bits not
12850 	 * in the token; ie. the prefix, and then xor to get the mask.
12851 	 */
12852 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12853 	for (i = 0; i < 4; i++)
12854 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12855 
12856 	mutex_enter(&ill->ill_lock);
12857 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12858 	ill->ill_token_length = addrlen;
12859 	mutex_exit(&ill->ill_lock);
12860 
12861 	if (need_up) {
12862 		/*
12863 		 * Now bring the interface back up.  If this
12864 		 * is the only IPIF for the ILL, ipif_up
12865 		 * will have to re-bind to the device, so
12866 		 * we may get back EINPROGRESS, in which
12867 		 * case, this IOCTL will get completed in
12868 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12869 		 */
12870 		err = ipif_up(ipif, q, mp);
12871 		if (err == EINPROGRESS)
12872 			return (err);
12873 	}
12874 	return (err);
12875 }
12876 
12877 /* ARGSUSED */
12878 int
12879 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12880     ip_ioctl_cmd_t *ipi, void *if_req)
12881 {
12882 	ill_t *ill;
12883 	sin6_t *sin6 = (sin6_t *)sin;
12884 	struct lifreq *lifr = (struct lifreq *)if_req;
12885 
12886 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12887 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12888 	if (ipif->ipif_id != 0)
12889 		return (EINVAL);
12890 
12891 	ill = ipif->ipif_ill;
12892 	if (!ill->ill_isv6)
12893 		return (ENXIO);
12894 
12895 	*sin6 = sin6_null;
12896 	sin6->sin6_family = AF_INET6;
12897 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12898 	sin6->sin6_addr = ill->ill_token;
12899 	lifr->lifr_addrlen = ill->ill_token_length;
12900 	return (0);
12901 }
12902 
12903 /*
12904  * Set (hardware) link specific information that might override
12905  * what was acquired through the DL_INFO_ACK.
12906  * The logic is as follows.
12907  *
12908  * become exclusive
12909  * set CHANGING flag
12910  * change mtu on affected IREs
12911  * clear CHANGING flag
12912  *
12913  * An ire add that occurs before the CHANGING flag is set will have its mtu
12914  * changed by the ip_sioctl_lnkinfo.
12915  *
12916  * During the time the CHANGING flag is set, no new ires will be added to the
12917  * bucket, and ire add will fail (due the CHANGING flag).
12918  *
12919  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12920  * before it is added to the bucket.
12921  *
12922  * Obviously only 1 thread can set the CHANGING flag and we need to become
12923  * exclusive to set the flag.
12924  */
12925 /* ARGSUSED */
12926 int
12927 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12928     ip_ioctl_cmd_t *ipi, void *if_req)
12929 {
12930 	ill_t		*ill = ipif->ipif_ill;
12931 	ipif_t		*nipif;
12932 	int		ip_min_mtu;
12933 	boolean_t	mtu_walk = B_FALSE;
12934 	struct lifreq	*lifr = (struct lifreq *)if_req;
12935 	lif_ifinfo_req_t *lir;
12936 	ire_t		*ire;
12937 
12938 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12939 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12940 	lir = &lifr->lifr_ifinfo;
12941 	ASSERT(IAM_WRITER_IPIF(ipif));
12942 
12943 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12944 	if (ipif->ipif_id != 0)
12945 		return (EINVAL);
12946 
12947 	/* Set interface MTU. */
12948 	if (ipif->ipif_isv6)
12949 		ip_min_mtu = IPV6_MIN_MTU;
12950 	else
12951 		ip_min_mtu = IP_MIN_MTU;
12952 
12953 	/*
12954 	 * Verify values before we set anything. Allow zero to
12955 	 * mean unspecified.
12956 	 */
12957 	if (lir->lir_maxmtu != 0 &&
12958 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12959 	    lir->lir_maxmtu < ip_min_mtu))
12960 		return (EINVAL);
12961 	if (lir->lir_reachtime != 0 &&
12962 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12963 		return (EINVAL);
12964 	if (lir->lir_reachretrans != 0 &&
12965 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12966 		return (EINVAL);
12967 
12968 	mutex_enter(&ill->ill_lock);
12969 	ill->ill_state_flags |= ILL_CHANGING;
12970 	for (nipif = ill->ill_ipif; nipif != NULL;
12971 	    nipif = nipif->ipif_next) {
12972 		nipif->ipif_state_flags |= IPIF_CHANGING;
12973 	}
12974 
12975 	mutex_exit(&ill->ill_lock);
12976 
12977 	if (lir->lir_maxmtu != 0) {
12978 		ill->ill_max_mtu = lir->lir_maxmtu;
12979 		ill->ill_mtu_userspecified = 1;
12980 		mtu_walk = B_TRUE;
12981 	}
12982 
12983 	if (lir->lir_reachtime != 0)
12984 		ill->ill_reachable_time = lir->lir_reachtime;
12985 
12986 	if (lir->lir_reachretrans != 0)
12987 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12988 
12989 	ill->ill_max_hops = lir->lir_maxhops;
12990 
12991 	ill->ill_max_buf = ND_MAX_Q;
12992 
12993 	if (mtu_walk) {
12994 		/*
12995 		 * Set the MTU on all ipifs associated with this ill except
12996 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12997 		 */
12998 		for (nipif = ill->ill_ipif; nipif != NULL;
12999 		    nipif = nipif->ipif_next) {
13000 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
13001 				continue;
13002 
13003 			nipif->ipif_mtu = ill->ill_max_mtu;
13004 
13005 			if (!(nipif->ipif_flags & IPIF_UP))
13006 				continue;
13007 
13008 			if (nipif->ipif_isv6)
13009 				ire = ipif_to_ire_v6(nipif);
13010 			else
13011 				ire = ipif_to_ire(nipif);
13012 			if (ire != NULL) {
13013 				ire->ire_max_frag = ipif->ipif_mtu;
13014 				ire_refrele(ire);
13015 			}
13016 			if (ill->ill_isv6) {
13017 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
13018 				    ipif_mtu_change, (char *)nipif,
13019 				    ill);
13020 			} else {
13021 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
13022 				    ipif_mtu_change, (char *)nipif,
13023 				    ill);
13024 			}
13025 		}
13026 	}
13027 
13028 	mutex_enter(&ill->ill_lock);
13029 	for (nipif = ill->ill_ipif; nipif != NULL;
13030 	    nipif = nipif->ipif_next) {
13031 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
13032 	}
13033 	ILL_UNMARK_CHANGING(ill);
13034 	mutex_exit(&ill->ill_lock);
13035 
13036 	return (0);
13037 }
13038 
13039 /* ARGSUSED */
13040 int
13041 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
13042     ip_ioctl_cmd_t *ipi, void *if_req)
13043 {
13044 	struct lif_ifinfo_req *lir;
13045 	ill_t *ill = ipif->ipif_ill;
13046 
13047 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
13048 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
13049 	if (ipif->ipif_id != 0)
13050 		return (EINVAL);
13051 
13052 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
13053 	lir->lir_maxhops = ill->ill_max_hops;
13054 	lir->lir_reachtime = ill->ill_reachable_time;
13055 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
13056 	lir->lir_maxmtu = ill->ill_max_mtu;
13057 
13058 	return (0);
13059 }
13060 
13061 /*
13062  * Return best guess as to the subnet mask for the specified address.
13063  * Based on the subnet masks for all the configured interfaces.
13064  *
13065  * We end up returning a zero mask in the case of default, multicast or
13066  * experimental.
13067  */
13068 static ipaddr_t
13069 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
13070 {
13071 	ipaddr_t net_mask;
13072 	ill_t	*ill;
13073 	ipif_t	*ipif;
13074 	ill_walk_context_t ctx;
13075 	ipif_t	*fallback_ipif = NULL;
13076 
13077 	net_mask = ip_net_mask(addr);
13078 	if (net_mask == 0) {
13079 		*ipifp = NULL;
13080 		return (0);
13081 	}
13082 
13083 	/* Let's check to see if this is maybe a local subnet route. */
13084 	/* this function only applies to IPv4 interfaces */
13085 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13086 	ill = ILL_START_WALK_V4(&ctx, ipst);
13087 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13088 		mutex_enter(&ill->ill_lock);
13089 		for (ipif = ill->ill_ipif; ipif != NULL;
13090 		    ipif = ipif->ipif_next) {
13091 			if (!IPIF_CAN_LOOKUP(ipif))
13092 				continue;
13093 			if (!(ipif->ipif_flags & IPIF_UP))
13094 				continue;
13095 			if ((ipif->ipif_subnet & net_mask) ==
13096 			    (addr & net_mask)) {
13097 				/*
13098 				 * Don't trust pt-pt interfaces if there are
13099 				 * other interfaces.
13100 				 */
13101 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13102 					if (fallback_ipif == NULL) {
13103 						ipif_refhold_locked(ipif);
13104 						fallback_ipif = ipif;
13105 					}
13106 					continue;
13107 				}
13108 
13109 				/*
13110 				 * Fine. Just assume the same net mask as the
13111 				 * directly attached subnet interface is using.
13112 				 */
13113 				ipif_refhold_locked(ipif);
13114 				mutex_exit(&ill->ill_lock);
13115 				rw_exit(&ipst->ips_ill_g_lock);
13116 				if (fallback_ipif != NULL)
13117 					ipif_refrele(fallback_ipif);
13118 				*ipifp = ipif;
13119 				return (ipif->ipif_net_mask);
13120 			}
13121 		}
13122 		mutex_exit(&ill->ill_lock);
13123 	}
13124 	rw_exit(&ipst->ips_ill_g_lock);
13125 
13126 	*ipifp = fallback_ipif;
13127 	return ((fallback_ipif != NULL) ?
13128 	    fallback_ipif->ipif_net_mask : net_mask);
13129 }
13130 
13131 /*
13132  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
13133  */
13134 static void
13135 ip_wput_ioctl(queue_t *q, mblk_t *mp)
13136 {
13137 	IOCP	iocp;
13138 	ipft_t	*ipft;
13139 	ipllc_t	*ipllc;
13140 	mblk_t	*mp1;
13141 	cred_t	*cr;
13142 	int	error = 0;
13143 	conn_t	*connp;
13144 
13145 	ip1dbg(("ip_wput_ioctl"));
13146 	iocp = (IOCP)mp->b_rptr;
13147 	mp1 = mp->b_cont;
13148 	if (mp1 == NULL) {
13149 		iocp->ioc_error = EINVAL;
13150 		mp->b_datap->db_type = M_IOCNAK;
13151 		iocp->ioc_count = 0;
13152 		qreply(q, mp);
13153 		return;
13154 	}
13155 
13156 	/*
13157 	 * These IOCTLs provide various control capabilities to
13158 	 * upstream agents such as ULPs and processes.	There
13159 	 * are currently two such IOCTLs implemented.  They
13160 	 * are used by TCP to provide update information for
13161 	 * existing IREs and to forcibly delete an IRE for a
13162 	 * host that is not responding, thereby forcing an
13163 	 * attempt at a new route.
13164 	 */
13165 	iocp->ioc_error = EINVAL;
13166 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
13167 		goto done;
13168 
13169 	ipllc = (ipllc_t *)mp1->b_rptr;
13170 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
13171 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
13172 			break;
13173 	}
13174 	/*
13175 	 * prefer credential from mblk over ioctl;
13176 	 * see ip_sioctl_copyin_setup
13177 	 */
13178 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
13179 
13180 	/*
13181 	 * Refhold the conn in case the request gets queued up in some lookup
13182 	 */
13183 	ASSERT(CONN_Q(q));
13184 	connp = Q_TO_CONN(q);
13185 	CONN_INC_REF(connp);
13186 	if (ipft->ipft_pfi &&
13187 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
13188 	    pullupmsg(mp1, ipft->ipft_min_size))) {
13189 		error = (*ipft->ipft_pfi)(q,
13190 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
13191 	}
13192 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
13193 		/*
13194 		 * CONN_OPER_PENDING_DONE happens in the function called
13195 		 * through ipft_pfi above.
13196 		 */
13197 		return;
13198 	}
13199 
13200 	CONN_OPER_PENDING_DONE(connp);
13201 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
13202 		freemsg(mp);
13203 		return;
13204 	}
13205 	iocp->ioc_error = error;
13206 
13207 done:
13208 	mp->b_datap->db_type = M_IOCACK;
13209 	if (iocp->ioc_error)
13210 		iocp->ioc_count = 0;
13211 	qreply(q, mp);
13212 }
13213 
13214 /*
13215  * Lookup an ipif using the sequence id (ipif_seqid)
13216  */
13217 ipif_t *
13218 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
13219 {
13220 	ipif_t *ipif;
13221 
13222 	ASSERT(MUTEX_HELD(&ill->ill_lock));
13223 
13224 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13225 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
13226 			return (ipif);
13227 	}
13228 	return (NULL);
13229 }
13230 
13231 /*
13232  * Assign a unique id for the ipif. This is used later when we send
13233  * IRES to ARP for resolution where we initialize ire_ipif_seqid
13234  * to the value pointed by ire_ipif->ipif_seqid. Later when the
13235  * IRE is added, we verify that ipif has not disappeared.
13236  */
13237 
13238 static void
13239 ipif_assign_seqid(ipif_t *ipif)
13240 {
13241 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13242 
13243 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
13244 }
13245 
13246 /*
13247  * Insert the ipif, so that the list of ipifs on the ill will be sorted
13248  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
13249  * be inserted into the first space available in the list. The value of
13250  * ipif_id will then be set to the appropriate value for its position.
13251  */
13252 static int
13253 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
13254 {
13255 	ill_t *ill;
13256 	ipif_t *tipif;
13257 	ipif_t **tipifp;
13258 	int id;
13259 	ip_stack_t	*ipst;
13260 
13261 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
13262 	    IAM_WRITER_IPIF(ipif));
13263 
13264 	ill = ipif->ipif_ill;
13265 	ASSERT(ill != NULL);
13266 	ipst = ill->ill_ipst;
13267 
13268 	/*
13269 	 * In the case of lo0:0 we already hold the ill_g_lock.
13270 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
13271 	 * ipif_insert. Another such caller is ipif_move.
13272 	 */
13273 	if (acquire_g_lock)
13274 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13275 	if (acquire_ill_lock)
13276 		mutex_enter(&ill->ill_lock);
13277 	id = ipif->ipif_id;
13278 	tipifp = &(ill->ill_ipif);
13279 	if (id == -1) {	/* need to find a real id */
13280 		id = 0;
13281 		while ((tipif = *tipifp) != NULL) {
13282 			ASSERT(tipif->ipif_id >= id);
13283 			if (tipif->ipif_id != id)
13284 				break; /* non-consecutive id */
13285 			id++;
13286 			tipifp = &(tipif->ipif_next);
13287 		}
13288 		/* limit number of logical interfaces */
13289 		if (id >= ipst->ips_ip_addrs_per_if) {
13290 			if (acquire_ill_lock)
13291 				mutex_exit(&ill->ill_lock);
13292 			if (acquire_g_lock)
13293 				rw_exit(&ipst->ips_ill_g_lock);
13294 			return (-1);
13295 		}
13296 		ipif->ipif_id = id; /* assign new id */
13297 	} else if (id < ipst->ips_ip_addrs_per_if) {
13298 		/* we have a real id; insert ipif in the right place */
13299 		while ((tipif = *tipifp) != NULL) {
13300 			ASSERT(tipif->ipif_id != id);
13301 			if (tipif->ipif_id > id)
13302 				break; /* found correct location */
13303 			tipifp = &(tipif->ipif_next);
13304 		}
13305 	} else {
13306 		if (acquire_ill_lock)
13307 			mutex_exit(&ill->ill_lock);
13308 		if (acquire_g_lock)
13309 			rw_exit(&ipst->ips_ill_g_lock);
13310 		return (-1);
13311 	}
13312 
13313 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
13314 
13315 	ipif->ipif_next = tipif;
13316 	*tipifp = ipif;
13317 	if (acquire_ill_lock)
13318 		mutex_exit(&ill->ill_lock);
13319 	if (acquire_g_lock)
13320 		rw_exit(&ipst->ips_ill_g_lock);
13321 	return (0);
13322 }
13323 
13324 static void
13325 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock)
13326 {
13327 	ipif_t	**ipifp;
13328 	ill_t	*ill = ipif->ipif_ill;
13329 
13330 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
13331 	if (acquire_ill_lock)
13332 		mutex_enter(&ill->ill_lock);
13333 	else
13334 		ASSERT(MUTEX_HELD(&ill->ill_lock));
13335 
13336 	ipifp = &ill->ill_ipif;
13337 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
13338 		if (*ipifp == ipif) {
13339 			*ipifp = ipif->ipif_next;
13340 			break;
13341 		}
13342 	}
13343 
13344 	if (acquire_ill_lock)
13345 		mutex_exit(&ill->ill_lock);
13346 }
13347 
13348 /*
13349  * Allocate and initialize a new interface control structure.  (Always
13350  * called as writer.)
13351  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
13352  * is not part of the global linked list of ills. ipif_seqid is unique
13353  * in the system and to preserve the uniqueness, it is assigned only
13354  * when ill becomes part of the global list. At that point ill will
13355  * have a name. If it doesn't get assigned here, it will get assigned
13356  * in ipif_set_values() as part of SIOCSLIFNAME processing.
13357  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
13358  * the interface flags or any other information from the DL_INFO_ACK for
13359  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
13360  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
13361  * second DL_INFO_ACK comes in from the driver.
13362  */
13363 static ipif_t *
13364 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
13365 {
13366 	ipif_t	*ipif;
13367 	phyint_t *phyi;
13368 
13369 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
13370 	    ill->ill_name, id, (void *)ill));
13371 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
13372 
13373 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
13374 		return (NULL);
13375 	*ipif = ipif_zero;	/* start clean */
13376 
13377 	ipif->ipif_ill = ill;
13378 	ipif->ipif_id = id;	/* could be -1 */
13379 	/*
13380 	 * Inherit the zoneid from the ill; for the shared stack instance
13381 	 * this is always the global zone
13382 	 */
13383 	ipif->ipif_zoneid = ill->ill_zoneid;
13384 
13385 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
13386 
13387 	ipif->ipif_refcnt = 0;
13388 	ipif->ipif_saved_ire_cnt = 0;
13389 
13390 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
13391 		mi_free(ipif);
13392 		return (NULL);
13393 	}
13394 	/* -1 id should have been replaced by real id */
13395 	id = ipif->ipif_id;
13396 	ASSERT(id >= 0);
13397 
13398 	if (ill->ill_name[0] != '\0')
13399 		ipif_assign_seqid(ipif);
13400 
13401 	/*
13402 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
13403 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
13404 	 * ioctl sets ipif_orig_ipifid to zero.
13405 	 */
13406 	ipif->ipif_orig_ipifid = id;
13407 
13408 	/*
13409 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
13410 	 * The ipif is still not up and can't be looked up until the
13411 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
13412 	 */
13413 	mutex_enter(&ill->ill_lock);
13414 	mutex_enter(&ill->ill_phyint->phyint_lock);
13415 	/*
13416 	 * Set the running flag when logical interface zero is created.
13417 	 * For subsequent logical interfaces, a DLPI link down
13418 	 * notification message may have cleared the running flag to
13419 	 * indicate the link is down, so we shouldn't just blindly set it.
13420 	 */
13421 	if (id == 0)
13422 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
13423 	ipif->ipif_ire_type = ire_type;
13424 	phyi = ill->ill_phyint;
13425 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
13426 
13427 	if (ipif->ipif_isv6) {
13428 		ill->ill_flags |= ILLF_IPV6;
13429 	} else {
13430 		ipaddr_t inaddr_any = INADDR_ANY;
13431 
13432 		ill->ill_flags |= ILLF_IPV4;
13433 
13434 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
13435 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13436 		    &ipif->ipif_v6lcl_addr);
13437 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13438 		    &ipif->ipif_v6src_addr);
13439 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13440 		    &ipif->ipif_v6subnet);
13441 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13442 		    &ipif->ipif_v6net_mask);
13443 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13444 		    &ipif->ipif_v6brd_addr);
13445 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
13446 		    &ipif->ipif_v6pp_dst_addr);
13447 	}
13448 
13449 	/*
13450 	 * Don't set the interface flags etc. now, will do it in
13451 	 * ip_ll_subnet_defaults.
13452 	 */
13453 	if (!initialize) {
13454 		mutex_exit(&ill->ill_lock);
13455 		mutex_exit(&ill->ill_phyint->phyint_lock);
13456 		return (ipif);
13457 	}
13458 	ipif->ipif_mtu = ill->ill_max_mtu;
13459 
13460 	if (ill->ill_bcast_addr_length != 0) {
13461 		/*
13462 		 * Later detect lack of DLPI driver multicast
13463 		 * capability by catching DL_ENABMULTI errors in
13464 		 * ip_rput_dlpi.
13465 		 */
13466 		ill->ill_flags |= ILLF_MULTICAST;
13467 		if (!ipif->ipif_isv6)
13468 			ipif->ipif_flags |= IPIF_BROADCAST;
13469 	} else {
13470 		if (ill->ill_net_type != IRE_LOOPBACK) {
13471 			if (ipif->ipif_isv6)
13472 				/*
13473 				 * Note: xresolv interfaces will eventually need
13474 				 * NOARP set here as well, but that will require
13475 				 * those external resolvers to have some
13476 				 * knowledge of that flag and act appropriately.
13477 				 * Not to be changed at present.
13478 				 */
13479 				ill->ill_flags |= ILLF_NONUD;
13480 			else
13481 				ill->ill_flags |= ILLF_NOARP;
13482 		}
13483 		if (ill->ill_phys_addr_length == 0) {
13484 			if (ill->ill_media &&
13485 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
13486 				ipif->ipif_flags |= IPIF_NOXMIT;
13487 				phyi->phyint_flags |= PHYI_VIRTUAL;
13488 			} else {
13489 				/* pt-pt supports multicast. */
13490 				ill->ill_flags |= ILLF_MULTICAST;
13491 				if (ill->ill_net_type == IRE_LOOPBACK) {
13492 					phyi->phyint_flags |=
13493 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
13494 				} else {
13495 					ipif->ipif_flags |= IPIF_POINTOPOINT;
13496 				}
13497 			}
13498 		}
13499 	}
13500 	mutex_exit(&ill->ill_lock);
13501 	mutex_exit(&ill->ill_phyint->phyint_lock);
13502 	return (ipif);
13503 }
13504 
13505 /*
13506  * If appropriate, send a message up to the resolver delete the entry
13507  * for the address of this interface which is going out of business.
13508  * (Always called as writer).
13509  *
13510  * NOTE : We need to check for NULL mps as some of the fields are
13511  *	  initialized only for some interface types. See ipif_resolver_up()
13512  *	  for details.
13513  */
13514 void
13515 ipif_arp_down(ipif_t *ipif)
13516 {
13517 	mblk_t	*mp;
13518 	ill_t	*ill = ipif->ipif_ill;
13519 
13520 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13521 	ASSERT(IAM_WRITER_IPIF(ipif));
13522 
13523 	/* Delete the mapping for the local address */
13524 	mp = ipif->ipif_arp_del_mp;
13525 	if (mp != NULL) {
13526 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13527 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13528 		putnext(ill->ill_rq, mp);
13529 		ipif->ipif_arp_del_mp = NULL;
13530 	}
13531 
13532 	/*
13533 	 * If this is the last ipif that is going down and there are no
13534 	 * duplicate addresses we may yet attempt to re-probe, then we need to
13535 	 * clean up ARP completely.
13536 	 */
13537 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) {
13538 
13539 		/* Send up AR_INTERFACE_DOWN message */
13540 		mp = ill->ill_arp_down_mp;
13541 		if (mp != NULL) {
13542 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13543 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13544 			    ipif->ipif_id));
13545 			putnext(ill->ill_rq, mp);
13546 			ill->ill_arp_down_mp = NULL;
13547 		}
13548 
13549 		/* Tell ARP to delete the multicast mappings */
13550 		mp = ill->ill_arp_del_mapping_mp;
13551 		if (mp != NULL) {
13552 			ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13553 			    *(unsigned *)mp->b_rptr, ill->ill_name,
13554 			    ipif->ipif_id));
13555 			putnext(ill->ill_rq, mp);
13556 			ill->ill_arp_del_mapping_mp = NULL;
13557 		}
13558 	}
13559 }
13560 
13561 /*
13562  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
13563  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
13564  * that it wants the add_mp allocated in this function to be returned
13565  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
13566  * just re-do the multicast, it wants us to send the add_mp to ARP also.
13567  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
13568  * as it does a ipif_arp_down after calling this function - which will
13569  * remove what we add here.
13570  *
13571  * Returns -1 on failures and 0 on success.
13572  */
13573 int
13574 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
13575 {
13576 	mblk_t	*del_mp = NULL;
13577 	mblk_t *add_mp = NULL;
13578 	mblk_t *mp;
13579 	ill_t	*ill = ipif->ipif_ill;
13580 	phyint_t *phyi = ill->ill_phyint;
13581 	ipaddr_t addr, mask, extract_mask = 0;
13582 	arma_t	*arma;
13583 	uint8_t *maddr, *bphys_addr;
13584 	uint32_t hw_start;
13585 	dl_unitdata_req_t *dlur;
13586 
13587 	ASSERT(IAM_WRITER_IPIF(ipif));
13588 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13589 		return (0);
13590 
13591 	/*
13592 	 * Delete the existing mapping from ARP. Normally ipif_down
13593 	 * -> ipif_arp_down should send this up to ARP. The only
13594 	 * reason we would find this when we are switching from
13595 	 * Multicast to Broadcast where we did not do a down.
13596 	 */
13597 	mp = ill->ill_arp_del_mapping_mp;
13598 	if (mp != NULL) {
13599 		ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n",
13600 		    *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
13601 		putnext(ill->ill_rq, mp);
13602 		ill->ill_arp_del_mapping_mp = NULL;
13603 	}
13604 
13605 	if (arp_add_mapping_mp != NULL)
13606 		*arp_add_mapping_mp = NULL;
13607 
13608 	/*
13609 	 * Check that the address is not to long for the constant
13610 	 * length reserved in the template arma_t.
13611 	 */
13612 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
13613 		return (-1);
13614 
13615 	/* Add mapping mblk */
13616 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13617 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13618 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13619 	    (caddr_t)&addr);
13620 	if (add_mp == NULL)
13621 		return (-1);
13622 	arma = (arma_t *)add_mp->b_rptr;
13623 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13624 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13625 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13626 
13627 	/*
13628 	 * Determine the broadcast address.
13629 	 */
13630 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13631 	if (ill->ill_sap_length < 0)
13632 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13633 	else
13634 		bphys_addr = (uchar_t *)dlur +
13635 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13636 	/*
13637 	 * Check PHYI_MULTI_BCAST and length of physical
13638 	 * address to determine if we use the mapping or the
13639 	 * broadcast address.
13640 	 */
13641 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13642 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13643 		    bphys_addr, maddr, &hw_start, &extract_mask))
13644 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13645 
13646 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13647 	    (ill->ill_flags & ILLF_MULTICAST)) {
13648 		/* Make sure this will not match the "exact" entry. */
13649 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13650 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13651 		    (caddr_t)&addr);
13652 		if (del_mp == NULL) {
13653 			freemsg(add_mp);
13654 			return (-1);
13655 		}
13656 		bcopy(&extract_mask, (char *)arma +
13657 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13658 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13659 			/* Use link-layer broadcast address for MULTI_BCAST */
13660 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13661 			ip2dbg(("ipif_arp_setup_multicast: adding"
13662 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13663 		} else {
13664 			arma->arma_hw_mapping_start = hw_start;
13665 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13666 			    " ARP setup for %s\n", ill->ill_name));
13667 		}
13668 	} else {
13669 		freemsg(add_mp);
13670 		ASSERT(del_mp == NULL);
13671 		/* It is neither MULTICAST nor MULTI_BCAST */
13672 		return (0);
13673 	}
13674 	ASSERT(add_mp != NULL && del_mp != NULL);
13675 	ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13676 	ill->ill_arp_del_mapping_mp = del_mp;
13677 	if (arp_add_mapping_mp != NULL) {
13678 		/* The caller just wants the mblks allocated */
13679 		*arp_add_mapping_mp = add_mp;
13680 	} else {
13681 		/* The caller wants us to send it to arp */
13682 		putnext(ill->ill_rq, add_mp);
13683 	}
13684 	return (0);
13685 }
13686 
13687 /*
13688  * Get the resolver set up for a new interface address.
13689  * (Always called as writer.)
13690  * Called both for IPv4 and IPv6 interfaces,
13691  * though it only sets up the resolver for v6
13692  * if it's an xresolv interface (one using an external resolver).
13693  * Honors ILLF_NOARP.
13694  * The enumerated value res_act is used to tune the behavior.
13695  * If set to Res_act_initial, then we set up all the resolver
13696  * structures for a new interface.  If set to Res_act_move, then
13697  * we just send an AR_ENTRY_ADD message up to ARP for IPv4
13698  * interfaces; this is called by ip_rput_dlpi_writer() to handle
13699  * asynchronous hardware address change notification.  If set to
13700  * Res_act_defend, then we tell ARP that it needs to send a single
13701  * gratuitous message in defense of the address.
13702  * Returns error on failure.
13703  */
13704 int
13705 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
13706 {
13707 	caddr_t	addr;
13708 	mblk_t	*arp_up_mp = NULL;
13709 	mblk_t	*arp_down_mp = NULL;
13710 	mblk_t	*arp_add_mp = NULL;
13711 	mblk_t	*arp_del_mp = NULL;
13712 	mblk_t	*arp_add_mapping_mp = NULL;
13713 	mblk_t	*arp_del_mapping_mp = NULL;
13714 	ill_t	*ill = ipif->ipif_ill;
13715 	uchar_t	*area_p = NULL;
13716 	uchar_t	*ared_p = NULL;
13717 	int	err = ENOMEM;
13718 	boolean_t was_dup;
13719 
13720 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13721 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
13722 	ASSERT(IAM_WRITER_IPIF(ipif));
13723 
13724 	was_dup = B_FALSE;
13725 	if (res_act == Res_act_initial) {
13726 		ipif->ipif_addr_ready = 0;
13727 		/*
13728 		 * We're bringing an interface up here.  There's no way that we
13729 		 * should need to shut down ARP now.
13730 		 */
13731 		mutex_enter(&ill->ill_lock);
13732 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
13733 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
13734 			ill->ill_ipif_dup_count--;
13735 			was_dup = B_TRUE;
13736 		}
13737 		mutex_exit(&ill->ill_lock);
13738 	}
13739 	if (ipif->ipif_recovery_id != 0)
13740 		(void) untimeout(ipif->ipif_recovery_id);
13741 	ipif->ipif_recovery_id = 0;
13742 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
13743 		ipif->ipif_addr_ready = 1;
13744 		return (0);
13745 	}
13746 	/* NDP will set the ipif_addr_ready flag when it's ready */
13747 	if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
13748 		return (0);
13749 
13750 	if (ill->ill_isv6) {
13751 		/*
13752 		 * External resolver for IPv6
13753 		 */
13754 		ASSERT(res_act == Res_act_initial);
13755 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13756 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13757 			area_p = (uchar_t *)&ip6_area_template;
13758 			ared_p = (uchar_t *)&ip6_ared_template;
13759 		}
13760 	} else {
13761 		/*
13762 		 * IPv4 arp case. If the ARP stream has already started
13763 		 * closing, fail this request for ARP bringup. Else
13764 		 * record the fact that an ARP bringup is pending.
13765 		 */
13766 		mutex_enter(&ill->ill_lock);
13767 		if (ill->ill_arp_closing) {
13768 			mutex_exit(&ill->ill_lock);
13769 			err = EINVAL;
13770 			goto failed;
13771 		} else {
13772 			if (ill->ill_ipif_up_count == 0 &&
13773 			    ill->ill_ipif_dup_count == 0 && !was_dup)
13774 				ill->ill_arp_bringup_pending = 1;
13775 			mutex_exit(&ill->ill_lock);
13776 		}
13777 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13778 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13779 			area_p = (uchar_t *)&ip_area_template;
13780 			ared_p = (uchar_t *)&ip_ared_template;
13781 		}
13782 	}
13783 
13784 	/*
13785 	 * Add an entry for the local address in ARP only if it
13786 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13787 	 */
13788 	if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) {
13789 		area_t *area;
13790 
13791 		/* Now ask ARP to publish our address. */
13792 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13793 		if (arp_add_mp == NULL)
13794 			goto failed;
13795 		area = (area_t *)arp_add_mp->b_rptr;
13796 		if (res_act != Res_act_initial) {
13797 			/*
13798 			 * Copy the new hardware address and length into
13799 			 * arp_add_mp to be sent to ARP.
13800 			 */
13801 			area->area_hw_addr_length = ill->ill_phys_addr_length;
13802 			bcopy(ill->ill_phys_addr,
13803 			    ((char *)area + area->area_hw_addr_offset),
13804 			    area->area_hw_addr_length);
13805 		}
13806 
13807 		area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH |
13808 		    ACE_F_MYADDR;
13809 
13810 		if (res_act == Res_act_defend) {
13811 			area->area_flags |= ACE_F_DEFEND;
13812 			/*
13813 			 * If we're just defending our address now, then
13814 			 * there's no need to set up ARP multicast mappings.
13815 			 * The publish command is enough.
13816 			 */
13817 			goto done;
13818 		}
13819 
13820 		if (res_act != Res_act_initial)
13821 			goto arp_setup_multicast;
13822 
13823 		/*
13824 		 * Allocate an ARP deletion message so we know we can tell ARP
13825 		 * when the interface goes down.
13826 		 */
13827 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13828 		if (arp_del_mp == NULL)
13829 			goto failed;
13830 
13831 	} else {
13832 		if (res_act != Res_act_initial)
13833 			goto done;
13834 	}
13835 	/*
13836 	 * Need to bring up ARP or setup multicast mapping only
13837 	 * when the first interface is coming UP.
13838 	 */
13839 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
13840 	    was_dup) {
13841 		goto done;
13842 	}
13843 
13844 	/*
13845 	 * Allocate an ARP down message (to be saved) and an ARP up
13846 	 * message.
13847 	 */
13848 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13849 	if (arp_down_mp == NULL)
13850 		goto failed;
13851 
13852 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13853 	if (arp_up_mp == NULL)
13854 		goto failed;
13855 
13856 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13857 		goto done;
13858 
13859 arp_setup_multicast:
13860 	/*
13861 	 * Setup the multicast mappings. This function initializes
13862 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13863 	 * IPv6.
13864 	 */
13865 	if (!ill->ill_isv6) {
13866 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13867 		if (err != 0)
13868 			goto failed;
13869 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13870 		ASSERT(arp_add_mapping_mp != NULL);
13871 	}
13872 
13873 done:
13874 	if (arp_del_mp != NULL) {
13875 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13876 		ipif->ipif_arp_del_mp = arp_del_mp;
13877 	}
13878 	if (arp_down_mp != NULL) {
13879 		ASSERT(ill->ill_arp_down_mp == NULL);
13880 		ill->ill_arp_down_mp = arp_down_mp;
13881 	}
13882 	if (arp_del_mapping_mp != NULL) {
13883 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13884 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13885 	}
13886 	if (arp_up_mp != NULL) {
13887 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13888 		    ill->ill_name, ipif->ipif_id));
13889 		putnext(ill->ill_rq, arp_up_mp);
13890 	}
13891 	if (arp_add_mp != NULL) {
13892 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13893 		    ill->ill_name, ipif->ipif_id));
13894 		/*
13895 		 * If it's an extended ARP implementation, then we'll wait to
13896 		 * hear that DAD has finished before using the interface.
13897 		 */
13898 		if (!ill->ill_arp_extend)
13899 			ipif->ipif_addr_ready = 1;
13900 		putnext(ill->ill_rq, arp_add_mp);
13901 	} else {
13902 		ipif->ipif_addr_ready = 1;
13903 	}
13904 	if (arp_add_mapping_mp != NULL) {
13905 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13906 		    ill->ill_name, ipif->ipif_id));
13907 		putnext(ill->ill_rq, arp_add_mapping_mp);
13908 	}
13909 	if (res_act != Res_act_initial)
13910 		return (0);
13911 
13912 	if (ill->ill_flags & ILLF_NOARP)
13913 		err = ill_arp_off(ill);
13914 	else
13915 		err = ill_arp_on(ill);
13916 	if (err != 0) {
13917 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13918 		freemsg(ipif->ipif_arp_del_mp);
13919 		freemsg(ill->ill_arp_down_mp);
13920 		freemsg(ill->ill_arp_del_mapping_mp);
13921 		ipif->ipif_arp_del_mp = NULL;
13922 		ill->ill_arp_down_mp = NULL;
13923 		ill->ill_arp_del_mapping_mp = NULL;
13924 		return (err);
13925 	}
13926 	return ((ill->ill_ipif_up_count != 0 || was_dup ||
13927 	    ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS);
13928 
13929 failed:
13930 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13931 	freemsg(arp_add_mp);
13932 	freemsg(arp_del_mp);
13933 	freemsg(arp_add_mapping_mp);
13934 	freemsg(arp_up_mp);
13935 	freemsg(arp_down_mp);
13936 	ill->ill_arp_bringup_pending = 0;
13937 	return (err);
13938 }
13939 
13940 /*
13941  * This routine restarts IPv4 duplicate address detection (DAD) when a link has
13942  * just gone back up.
13943  */
13944 static void
13945 ipif_arp_start_dad(ipif_t *ipif)
13946 {
13947 	ill_t *ill = ipif->ipif_ill;
13948 	mblk_t *arp_add_mp;
13949 	area_t *area;
13950 
13951 	if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing ||
13952 	    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13953 	    ipif->ipif_lcl_addr == INADDR_ANY ||
13954 	    (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
13955 	    (char *)&ipif->ipif_lcl_addr)) == NULL) {
13956 		/*
13957 		 * If we can't contact ARP for some reason, that's not really a
13958 		 * problem.  Just send out the routing socket notification that
13959 		 * DAD completion would have done, and continue.
13960 		 */
13961 		ipif_mask_reply(ipif);
13962 		ip_rts_ifmsg(ipif);
13963 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13964 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13965 		ipif->ipif_addr_ready = 1;
13966 		return;
13967 	}
13968 
13969 	/* Setting the 'unverified' flag restarts DAD */
13970 	area = (area_t *)arp_add_mp->b_rptr;
13971 	area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
13972 	    ACE_F_UNVERIFIED;
13973 	putnext(ill->ill_rq, arp_add_mp);
13974 }
13975 
13976 static void
13977 ipif_ndp_start_dad(ipif_t *ipif)
13978 {
13979 	nce_t *nce;
13980 
13981 	nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE);
13982 	if (nce == NULL)
13983 		return;
13984 
13985 	if (!ndp_restart_dad(nce)) {
13986 		/*
13987 		 * If we can't restart DAD for some reason, that's not really a
13988 		 * problem.  Just send out the routing socket notification that
13989 		 * DAD completion would have done, and continue.
13990 		 */
13991 		ip_rts_ifmsg(ipif);
13992 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
13993 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
13994 		ipif->ipif_addr_ready = 1;
13995 	}
13996 	NCE_REFRELE(nce);
13997 }
13998 
13999 /*
14000  * Restart duplicate address detection on all interfaces on the given ill.
14001  *
14002  * This is called when an interface transitions from down to up
14003  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
14004  *
14005  * Note that since the underlying physical link has transitioned, we must cause
14006  * at least one routing socket message to be sent here, either via DAD
14007  * completion or just by default on the first ipif.  (If we don't do this, then
14008  * in.mpathd will see long delays when doing link-based failure recovery.)
14009  */
14010 void
14011 ill_restart_dad(ill_t *ill, boolean_t went_up)
14012 {
14013 	ipif_t *ipif;
14014 
14015 	if (ill == NULL)
14016 		return;
14017 
14018 	/*
14019 	 * If layer two doesn't support duplicate address detection, then just
14020 	 * send the routing socket message now and be done with it.
14021 	 */
14022 	if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) ||
14023 	    (!ill->ill_isv6 && !ill->ill_arp_extend)) {
14024 		ip_rts_ifmsg(ill->ill_ipif);
14025 		return;
14026 	}
14027 
14028 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14029 		if (went_up) {
14030 			if (ipif->ipif_flags & IPIF_UP) {
14031 				if (ill->ill_isv6)
14032 					ipif_ndp_start_dad(ipif);
14033 				else
14034 					ipif_arp_start_dad(ipif);
14035 			} else if (ill->ill_isv6 &&
14036 			    (ipif->ipif_flags & IPIF_DUPLICATE)) {
14037 				/*
14038 				 * For IPv4, the ARP module itself will
14039 				 * automatically start the DAD process when it
14040 				 * sees DL_NOTE_LINK_UP.  We respond to the
14041 				 * AR_CN_READY at the completion of that task.
14042 				 * For IPv6, we must kick off the bring-up
14043 				 * process now.
14044 				 */
14045 				ndp_do_recovery(ipif);
14046 			} else {
14047 				/*
14048 				 * Unfortunately, the first ipif is "special"
14049 				 * and represents the underlying ill in the
14050 				 * routing socket messages.  Thus, when this
14051 				 * one ipif is down, we must still notify so
14052 				 * that the user knows the IFF_RUNNING status
14053 				 * change.  (If the first ipif is up, then
14054 				 * we'll handle eventual routing socket
14055 				 * notification via DAD completion.)
14056 				 */
14057 				if (ipif == ill->ill_ipif)
14058 					ip_rts_ifmsg(ill->ill_ipif);
14059 			}
14060 		} else {
14061 			/*
14062 			 * After link down, we'll need to send a new routing
14063 			 * message when the link comes back, so clear
14064 			 * ipif_addr_ready.
14065 			 */
14066 			ipif->ipif_addr_ready = 0;
14067 		}
14068 	}
14069 
14070 	/*
14071 	 * If we've torn down links, then notify the user right away.
14072 	 */
14073 	if (!went_up)
14074 		ip_rts_ifmsg(ill->ill_ipif);
14075 }
14076 
14077 /*
14078  * Wakeup all threads waiting to enter the ipsq, and sleeping
14079  * on any of the ills in this ipsq. The ill_lock of the ill
14080  * must be held so that waiters don't miss wakeups
14081  */
14082 static void
14083 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
14084 {
14085 	phyint_t *phyint;
14086 
14087 	phyint = ipsq->ipsq_phyint_list;
14088 	while (phyint != NULL) {
14089 		if (phyint->phyint_illv4) {
14090 			if (!caller_holds_lock)
14091 				mutex_enter(&phyint->phyint_illv4->ill_lock);
14092 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14093 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
14094 			if (!caller_holds_lock)
14095 				mutex_exit(&phyint->phyint_illv4->ill_lock);
14096 		}
14097 		if (phyint->phyint_illv6) {
14098 			if (!caller_holds_lock)
14099 				mutex_enter(&phyint->phyint_illv6->ill_lock);
14100 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14101 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
14102 			if (!caller_holds_lock)
14103 				mutex_exit(&phyint->phyint_illv6->ill_lock);
14104 		}
14105 		phyint = phyint->phyint_ipsq_next;
14106 	}
14107 }
14108 
14109 static ipsq_t *
14110 ipsq_create(char *groupname, ip_stack_t *ipst)
14111 {
14112 	ipsq_t	*ipsq;
14113 
14114 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14115 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
14116 	if (ipsq == NULL) {
14117 		return (NULL);
14118 	}
14119 
14120 	if (groupname != NULL)
14121 		(void) strcpy(ipsq->ipsq_name, groupname);
14122 	else
14123 		ipsq->ipsq_name[0] = '\0';
14124 
14125 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
14126 	ipsq->ipsq_flags |= IPSQ_GROUP;
14127 	ipsq->ipsq_next = ipst->ips_ipsq_g_head;
14128 	ipst->ips_ipsq_g_head = ipsq;
14129 	ipsq->ipsq_ipst = ipst;		/* No netstack_hold */
14130 	return (ipsq);
14131 }
14132 
14133 /*
14134  * Return an ipsq correspoding to the groupname. If 'create' is true
14135  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
14136  * uniquely with an IPMP group. However during IPMP groupname operations,
14137  * multiple IPMP groups may be associated with a single ipsq. But no
14138  * IPMP group can be associated with more than 1 ipsq at any time.
14139  * For example
14140  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
14141  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
14142  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
14143  *
14144  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
14145  * status shown below during the execution of the above command.
14146  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
14147  *
14148  * After the completion of the above groupname command we return to the stable
14149  * state shown below.
14150  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
14151  *	hme4			mpk17-85	ipsq2	mpk17-85	1
14152  *
14153  * Because of the above, we don't search based on the ipsq_name since that
14154  * would miss the correct ipsq during certain windows as shown above.
14155  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
14156  * natural state.
14157  */
14158 static ipsq_t *
14159 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq,
14160     ip_stack_t *ipst)
14161 {
14162 	ipsq_t	*ipsq;
14163 	int	group_len;
14164 	phyint_t *phyint;
14165 
14166 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14167 
14168 	group_len = strlen(groupname);
14169 	ASSERT(group_len != 0);
14170 	group_len++;
14171 
14172 	for (ipsq = ipst->ips_ipsq_g_head;
14173 	    ipsq != NULL;
14174 	    ipsq = ipsq->ipsq_next) {
14175 		/*
14176 		 * When an ipsq is being split, and ill_split_ipsq
14177 		 * calls this function, we exclude it from being considered.
14178 		 */
14179 		if (ipsq == exclude_ipsq)
14180 			continue;
14181 
14182 		/*
14183 		 * Compare against the ipsq_name. The groupname change happens
14184 		 * in 2 phases. The 1st phase merges the from group into
14185 		 * the to group's ipsq, by calling ill_merge_groups and restarts
14186 		 * the ioctl. The 2nd phase then locates the ipsq again thru
14187 		 * ipsq_name. At this point the phyint_groupname has not been
14188 		 * updated.
14189 		 */
14190 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
14191 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
14192 			/*
14193 			 * Verify that an ipmp groupname is exactly
14194 			 * part of 1 ipsq and is not found in any other
14195 			 * ipsq.
14196 			 */
14197 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) ==
14198 			    NULL);
14199 			return (ipsq);
14200 		}
14201 
14202 		/*
14203 		 * Comparison against ipsq_name alone is not sufficient.
14204 		 * In the case when groups are currently being
14205 		 * merged, the ipsq could hold other IPMP groups temporarily.
14206 		 * so we walk the phyint list and compare against the
14207 		 * phyint_groupname as well.
14208 		 */
14209 		phyint = ipsq->ipsq_phyint_list;
14210 		while (phyint != NULL) {
14211 			if ((group_len == phyint->phyint_groupname_len) &&
14212 			    (bcmp(phyint->phyint_groupname, groupname,
14213 			    group_len) == 0)) {
14214 				/*
14215 				 * Verify that an ipmp groupname is exactly
14216 				 * part of 1 ipsq and is not found in any other
14217 				 * ipsq.
14218 				 */
14219 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq,
14220 				    ipst) == NULL);
14221 				return (ipsq);
14222 			}
14223 			phyint = phyint->phyint_ipsq_next;
14224 		}
14225 	}
14226 	if (create)
14227 		ipsq = ipsq_create(groupname, ipst);
14228 	return (ipsq);
14229 }
14230 
14231 static void
14232 ipsq_delete(ipsq_t *ipsq)
14233 {
14234 	ipsq_t *nipsq;
14235 	ipsq_t *pipsq = NULL;
14236 	ip_stack_t *ipst = ipsq->ipsq_ipst;
14237 
14238 	/*
14239 	 * We don't hold the ipsq lock, but we are sure no new
14240 	 * messages can land up, since the ipsq_refs is zero.
14241 	 * i.e. this ipsq is unnamed and no phyint or phyint group
14242 	 * is associated with this ipsq. (Lookups are based on ill_name
14243 	 * or phyint_groupname)
14244 	 */
14245 	ASSERT(ipsq->ipsq_refs == 0);
14246 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
14247 	ASSERT(ipsq->ipsq_pending_mp == NULL);
14248 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
14249 		/*
14250 		 * This is not the ipsq of an IPMP group.
14251 		 */
14252 		ipsq->ipsq_ipst = NULL;
14253 		kmem_free(ipsq, sizeof (ipsq_t));
14254 		return;
14255 	}
14256 
14257 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14258 
14259 	/*
14260 	 * Locate the ipsq  before we can remove it from
14261 	 * the singly linked list of ipsq's.
14262 	 */
14263 	for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL;
14264 	    nipsq = nipsq->ipsq_next) {
14265 		if (nipsq == ipsq) {
14266 			break;
14267 		}
14268 		pipsq = nipsq;
14269 	}
14270 
14271 	ASSERT(nipsq == ipsq);
14272 
14273 	/* unlink ipsq from the list */
14274 	if (pipsq != NULL)
14275 		pipsq->ipsq_next = ipsq->ipsq_next;
14276 	else
14277 		ipst->ips_ipsq_g_head = ipsq->ipsq_next;
14278 	ipsq->ipsq_ipst = NULL;
14279 	kmem_free(ipsq, sizeof (ipsq_t));
14280 	rw_exit(&ipst->ips_ill_g_lock);
14281 }
14282 
14283 static void
14284 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
14285     queue_t *q)
14286 {
14287 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
14288 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
14289 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
14290 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
14291 	ASSERT(current_mp != NULL);
14292 
14293 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
14294 	    NEW_OP, NULL);
14295 
14296 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
14297 	    new_ipsq->ipsq_xopq_mphead != NULL);
14298 
14299 	/*
14300 	 * move from old ipsq to the new ipsq.
14301 	 */
14302 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
14303 	if (old_ipsq->ipsq_xopq_mphead != NULL)
14304 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
14305 
14306 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
14307 }
14308 
14309 void
14310 ill_group_cleanup(ill_t *ill)
14311 {
14312 	ill_t *ill_v4;
14313 	ill_t *ill_v6;
14314 	ipif_t *ipif;
14315 
14316 	ill_v4 = ill->ill_phyint->phyint_illv4;
14317 	ill_v6 = ill->ill_phyint->phyint_illv6;
14318 
14319 	if (ill_v4 != NULL) {
14320 		mutex_enter(&ill_v4->ill_lock);
14321 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14322 		    ipif = ipif->ipif_next) {
14323 			IPIF_UNMARK_MOVING(ipif);
14324 		}
14325 		ill_v4->ill_up_ipifs = B_FALSE;
14326 		mutex_exit(&ill_v4->ill_lock);
14327 	}
14328 
14329 	if (ill_v6 != NULL) {
14330 		mutex_enter(&ill_v6->ill_lock);
14331 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14332 		    ipif = ipif->ipif_next) {
14333 			IPIF_UNMARK_MOVING(ipif);
14334 		}
14335 		ill_v6->ill_up_ipifs = B_FALSE;
14336 		mutex_exit(&ill_v6->ill_lock);
14337 	}
14338 }
14339 /*
14340  * This function is called when an ill has had a change in its group status
14341  * to bring up all the ipifs that were up before the change.
14342  */
14343 int
14344 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
14345 {
14346 	ipif_t *ipif;
14347 	ill_t *ill_v4;
14348 	ill_t *ill_v6;
14349 	ill_t *from_ill;
14350 	int err = 0;
14351 
14352 
14353 	ASSERT(IAM_WRITER_ILL(ill));
14354 
14355 	/*
14356 	 * Except for ipif_state_flags and ill_state_flags the other
14357 	 * fields of the ipif/ill that are modified below are protected
14358 	 * implicitly since we are a writer. We would have tried to down
14359 	 * even an ipif that was already down, in ill_down_ipifs. So we
14360 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
14361 	 */
14362 	ill_v4 = ill->ill_phyint->phyint_illv4;
14363 	ill_v6 = ill->ill_phyint->phyint_illv6;
14364 	if (ill_v4 != NULL) {
14365 		ill_v4->ill_up_ipifs = B_TRUE;
14366 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
14367 		    ipif = ipif->ipif_next) {
14368 			mutex_enter(&ill_v4->ill_lock);
14369 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14370 			IPIF_UNMARK_MOVING(ipif);
14371 			mutex_exit(&ill_v4->ill_lock);
14372 			if (ipif->ipif_was_up) {
14373 				if (!(ipif->ipif_flags & IPIF_UP))
14374 					err = ipif_up(ipif, q, mp);
14375 				ipif->ipif_was_up = B_FALSE;
14376 				if (err != 0) {
14377 					/*
14378 					 * Can there be any other error ?
14379 					 */
14380 					ASSERT(err == EINPROGRESS);
14381 					return (err);
14382 				}
14383 			}
14384 		}
14385 		mutex_enter(&ill_v4->ill_lock);
14386 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
14387 		mutex_exit(&ill_v4->ill_lock);
14388 		ill_v4->ill_up_ipifs = B_FALSE;
14389 		if (ill_v4->ill_move_in_progress) {
14390 			ASSERT(ill_v4->ill_move_peer != NULL);
14391 			ill_v4->ill_move_in_progress = B_FALSE;
14392 			from_ill = ill_v4->ill_move_peer;
14393 			from_ill->ill_move_in_progress = B_FALSE;
14394 			from_ill->ill_move_peer = NULL;
14395 			mutex_enter(&from_ill->ill_lock);
14396 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14397 			mutex_exit(&from_ill->ill_lock);
14398 			if (ill_v6 == NULL) {
14399 				if (from_ill->ill_phyint->phyint_flags &
14400 				    PHYI_STANDBY) {
14401 					phyint_inactive(from_ill->ill_phyint);
14402 				}
14403 				if (ill_v4->ill_phyint->phyint_flags &
14404 				    PHYI_STANDBY) {
14405 					phyint_inactive(ill_v4->ill_phyint);
14406 				}
14407 			}
14408 			ill_v4->ill_move_peer = NULL;
14409 		}
14410 	}
14411 
14412 	if (ill_v6 != NULL) {
14413 		ill_v6->ill_up_ipifs = B_TRUE;
14414 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
14415 		    ipif = ipif->ipif_next) {
14416 			mutex_enter(&ill_v6->ill_lock);
14417 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
14418 			IPIF_UNMARK_MOVING(ipif);
14419 			mutex_exit(&ill_v6->ill_lock);
14420 			if (ipif->ipif_was_up) {
14421 				if (!(ipif->ipif_flags & IPIF_UP))
14422 					err = ipif_up(ipif, q, mp);
14423 				ipif->ipif_was_up = B_FALSE;
14424 				if (err != 0) {
14425 					/*
14426 					 * Can there be any other error ?
14427 					 */
14428 					ASSERT(err == EINPROGRESS);
14429 					return (err);
14430 				}
14431 			}
14432 		}
14433 		mutex_enter(&ill_v6->ill_lock);
14434 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
14435 		mutex_exit(&ill_v6->ill_lock);
14436 		ill_v6->ill_up_ipifs = B_FALSE;
14437 		if (ill_v6->ill_move_in_progress) {
14438 			ASSERT(ill_v6->ill_move_peer != NULL);
14439 			ill_v6->ill_move_in_progress = B_FALSE;
14440 			from_ill = ill_v6->ill_move_peer;
14441 			from_ill->ill_move_in_progress = B_FALSE;
14442 			from_ill->ill_move_peer = NULL;
14443 			mutex_enter(&from_ill->ill_lock);
14444 			from_ill->ill_state_flags &= ~ILL_CHANGING;
14445 			mutex_exit(&from_ill->ill_lock);
14446 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
14447 				phyint_inactive(from_ill->ill_phyint);
14448 			}
14449 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
14450 				phyint_inactive(ill_v6->ill_phyint);
14451 			}
14452 			ill_v6->ill_move_peer = NULL;
14453 		}
14454 	}
14455 	return (0);
14456 }
14457 
14458 /*
14459  * bring down all the approriate ipifs.
14460  */
14461 /* ARGSUSED */
14462 static void
14463 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
14464 {
14465 	ipif_t *ipif;
14466 
14467 	ASSERT(IAM_WRITER_ILL(ill));
14468 
14469 	/*
14470 	 * Except for ipif_state_flags the other fields of the ipif/ill that
14471 	 * are modified below are protected implicitly since we are a writer
14472 	 */
14473 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14474 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
14475 			continue;
14476 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
14477 			/*
14478 			 * We go through the ipif_down logic even if the ipif
14479 			 * is already down, since routes can be added based
14480 			 * on down ipifs. Going through ipif_down once again
14481 			 * will delete any IREs created based on these routes.
14482 			 */
14483 			if (ipif->ipif_flags & IPIF_UP)
14484 				ipif->ipif_was_up = B_TRUE;
14485 			/*
14486 			 * If called with chk_nofailover true ipif is moving.
14487 			 */
14488 			mutex_enter(&ill->ill_lock);
14489 			if (chk_nofailover) {
14490 				ipif->ipif_state_flags |=
14491 				    IPIF_MOVING | IPIF_CHANGING;
14492 			} else {
14493 				ipif->ipif_state_flags |= IPIF_CHANGING;
14494 			}
14495 			mutex_exit(&ill->ill_lock);
14496 			/*
14497 			 * Need to re-create net/subnet bcast ires if
14498 			 * they are dependent on ipif.
14499 			 */
14500 			if (!ipif->ipif_isv6)
14501 				ipif_check_bcast_ires(ipif);
14502 			(void) ipif_logical_down(ipif, NULL, NULL);
14503 			ipif_non_duplicate(ipif);
14504 			ipif_down_tail(ipif);
14505 		}
14506 	}
14507 }
14508 
14509 #define	IPSQ_INC_REF(ipsq, ipst)	{			\
14510 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14511 	(ipsq)->ipsq_refs++;				\
14512 }
14513 
14514 #define	IPSQ_DEC_REF(ipsq, ipst)	{			\
14515 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));		\
14516 	(ipsq)->ipsq_refs--;				\
14517 	if ((ipsq)->ipsq_refs == 0)				\
14518 		(ipsq)->ipsq_name[0] = '\0'; 		\
14519 }
14520 
14521 /*
14522  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14523  * new_ipsq.
14524  */
14525 static void
14526 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst)
14527 {
14528 	phyint_t *phyint;
14529 	phyint_t *next_phyint;
14530 
14531 	/*
14532 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
14533 	 * writer and the ill_lock of the ill in question. Also the dest
14534 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
14535 	 */
14536 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14537 
14538 	phyint = cur_ipsq->ipsq_phyint_list;
14539 	cur_ipsq->ipsq_phyint_list = NULL;
14540 	while (phyint != NULL) {
14541 		next_phyint = phyint->phyint_ipsq_next;
14542 		IPSQ_DEC_REF(cur_ipsq, ipst);
14543 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
14544 		new_ipsq->ipsq_phyint_list = phyint;
14545 		IPSQ_INC_REF(new_ipsq, ipst);
14546 		phyint->phyint_ipsq = new_ipsq;
14547 		phyint = next_phyint;
14548 	}
14549 }
14550 
14551 #define	SPLIT_SUCCESS		0
14552 #define	SPLIT_NOT_NEEDED	1
14553 #define	SPLIT_FAILED		2
14554 
14555 int
14556 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry,
14557     ip_stack_t *ipst)
14558 {
14559 	ipsq_t *newipsq = NULL;
14560 
14561 	/*
14562 	 * Assertions denote pre-requisites for changing the ipsq of
14563 	 * a phyint
14564 	 */
14565 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14566 	/*
14567 	 * <ill-phyint> assocs can't change while ill_g_lock
14568 	 * is held as writer. See ill_phyint_reinit()
14569 	 */
14570 	ASSERT(phyint->phyint_illv4 == NULL ||
14571 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14572 	ASSERT(phyint->phyint_illv6 == NULL ||
14573 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14574 
14575 	if ((phyint->phyint_groupname_len !=
14576 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
14577 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
14578 	    phyint->phyint_groupname_len) != 0)) {
14579 		/*
14580 		 * Once we fail in creating a new ipsq due to memory shortage,
14581 		 * don't attempt to create new ipsq again, based on another
14582 		 * phyint, since we want all phyints belonging to an IPMP group
14583 		 * to be in the same ipsq even in the event of mem alloc fails.
14584 		 */
14585 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
14586 		    cur_ipsq, ipst);
14587 		if (newipsq == NULL) {
14588 			/* Memory allocation failure */
14589 			return (SPLIT_FAILED);
14590 		} else {
14591 			/* ipsq_refs protected by ill_g_lock (writer) */
14592 			IPSQ_DEC_REF(cur_ipsq, ipst);
14593 			phyint->phyint_ipsq = newipsq;
14594 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
14595 			newipsq->ipsq_phyint_list = phyint;
14596 			IPSQ_INC_REF(newipsq, ipst);
14597 			return (SPLIT_SUCCESS);
14598 		}
14599 	}
14600 	return (SPLIT_NOT_NEEDED);
14601 }
14602 
14603 /*
14604  * The ill locks of the phyint and the ill_g_lock (writer) must be held
14605  * to do this split
14606  */
14607 static int
14608 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst)
14609 {
14610 	ipsq_t *newipsq;
14611 
14612 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
14613 	/*
14614 	 * <ill-phyint> assocs can't change while ill_g_lock
14615 	 * is held as writer. See ill_phyint_reinit()
14616 	 */
14617 
14618 	ASSERT(phyint->phyint_illv4 == NULL ||
14619 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
14620 	ASSERT(phyint->phyint_illv6 == NULL ||
14621 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
14622 
14623 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
14624 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
14625 		/*
14626 		 * ipsq_init failed due to no memory
14627 		 * caller will use the same ipsq
14628 		 */
14629 		return (SPLIT_FAILED);
14630 	}
14631 
14632 	/* ipsq_ref is protected by ill_g_lock (writer) */
14633 	IPSQ_DEC_REF(cur_ipsq, ipst);
14634 
14635 	/*
14636 	 * This is a new ipsq that is unknown to the world.
14637 	 * So we don't need to hold ipsq_lock,
14638 	 */
14639 	newipsq = phyint->phyint_ipsq;
14640 	newipsq->ipsq_writer = NULL;
14641 	newipsq->ipsq_reentry_cnt--;
14642 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
14643 #ifdef DEBUG
14644 	newipsq->ipsq_depth = 0;
14645 #endif
14646 
14647 	return (SPLIT_SUCCESS);
14648 }
14649 
14650 /*
14651  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
14652  * ipsq's representing their individual groups or themselves. Return
14653  * whether split needs to be retried again later.
14654  */
14655 static boolean_t
14656 ill_split_ipsq(ipsq_t *cur_ipsq)
14657 {
14658 	phyint_t *phyint;
14659 	phyint_t *next_phyint;
14660 	int	error;
14661 	boolean_t need_retry = B_FALSE;
14662 	ip_stack_t	*ipst = cur_ipsq->ipsq_ipst;
14663 
14664 	phyint = cur_ipsq->ipsq_phyint_list;
14665 	cur_ipsq->ipsq_phyint_list = NULL;
14666 	while (phyint != NULL) {
14667 		next_phyint = phyint->phyint_ipsq_next;
14668 		/*
14669 		 * 'created' will tell us whether the callee actually
14670 		 * created an ipsq. Lack of memory may force the callee
14671 		 * to return without creating an ipsq.
14672 		 */
14673 		if (phyint->phyint_groupname == NULL) {
14674 			error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst);
14675 		} else {
14676 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
14677 			    need_retry, ipst);
14678 		}
14679 
14680 		switch (error) {
14681 		case SPLIT_FAILED:
14682 			need_retry = B_TRUE;
14683 			/* FALLTHRU */
14684 		case SPLIT_NOT_NEEDED:
14685 			/*
14686 			 * Keep it on the list.
14687 			 */
14688 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
14689 			cur_ipsq->ipsq_phyint_list = phyint;
14690 			break;
14691 		case SPLIT_SUCCESS:
14692 			break;
14693 		default:
14694 			ASSERT(0);
14695 		}
14696 
14697 		phyint = next_phyint;
14698 	}
14699 	return (need_retry);
14700 }
14701 
14702 /*
14703  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
14704  * and return the ills in the list. This list will be
14705  * needed to unlock all the ills later on by the caller.
14706  * The <ill-ipsq> associations could change between the
14707  * lock and unlock. Hence the unlock can't traverse the
14708  * ipsq to get the list of ills.
14709  */
14710 static int
14711 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
14712 {
14713 	int	cnt = 0;
14714 	phyint_t	*phyint;
14715 	ip_stack_t	*ipst = ipsq->ipsq_ipst;
14716 
14717 	/*
14718 	 * The caller holds ill_g_lock to ensure that the ill memberships
14719 	 * of the ipsq don't change
14720 	 */
14721 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
14722 
14723 	phyint = ipsq->ipsq_phyint_list;
14724 	while (phyint != NULL) {
14725 		if (phyint->phyint_illv4 != NULL) {
14726 			ASSERT(cnt < list_max);
14727 			list[cnt++] = phyint->phyint_illv4;
14728 		}
14729 		if (phyint->phyint_illv6 != NULL) {
14730 			ASSERT(cnt < list_max);
14731 			list[cnt++] = phyint->phyint_illv6;
14732 		}
14733 		phyint = phyint->phyint_ipsq_next;
14734 	}
14735 	ill_lock_ills(list, cnt);
14736 	return (cnt);
14737 }
14738 
14739 void
14740 ill_lock_ills(ill_t **list, int cnt)
14741 {
14742 	int	i;
14743 
14744 	if (cnt > 1) {
14745 		boolean_t try_again;
14746 		do {
14747 			try_again = B_FALSE;
14748 			for (i = 0; i < cnt - 1; i++) {
14749 				if (list[i] < list[i + 1]) {
14750 					ill_t	*tmp;
14751 
14752 					/* swap the elements */
14753 					tmp = list[i];
14754 					list[i] = list[i + 1];
14755 					list[i + 1] = tmp;
14756 					try_again = B_TRUE;
14757 				}
14758 			}
14759 		} while (try_again);
14760 	}
14761 
14762 	for (i = 0; i < cnt; i++) {
14763 		if (i == 0) {
14764 			if (list[i] != NULL)
14765 				mutex_enter(&list[i]->ill_lock);
14766 			else
14767 				return;
14768 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14769 			mutex_enter(&list[i]->ill_lock);
14770 		}
14771 	}
14772 }
14773 
14774 void
14775 ill_unlock_ills(ill_t **list, int cnt)
14776 {
14777 	int	i;
14778 
14779 	for (i = 0; i < cnt; i++) {
14780 		if ((i == 0) && (list[i] != NULL)) {
14781 			mutex_exit(&list[i]->ill_lock);
14782 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
14783 			mutex_exit(&list[i]->ill_lock);
14784 		}
14785 	}
14786 }
14787 
14788 /*
14789  * Merge all the ills from 1 ipsq group into another ipsq group.
14790  * The source ipsq group is specified by the ipsq associated with
14791  * 'from_ill'. The destination ipsq group is specified by the ipsq
14792  * associated with 'to_ill' or 'groupname' respectively.
14793  * Note that ipsq itself does not have a reference count mechanism
14794  * and functions don't look up an ipsq and pass it around. Instead
14795  * functions pass around an ill or groupname, and the ipsq is looked
14796  * up from the ill or groupname and the required operation performed
14797  * atomically with the lookup on the ipsq.
14798  */
14799 static int
14800 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
14801     queue_t *q)
14802 {
14803 	ipsq_t *old_ipsq;
14804 	ipsq_t *new_ipsq;
14805 	ill_t	**ill_list;
14806 	int	cnt;
14807 	size_t	ill_list_size;
14808 	boolean_t became_writer_on_new_sq = B_FALSE;
14809 	ip_stack_t	*ipst = from_ill->ill_ipst;
14810 
14811 	ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst);
14812 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14813 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14814 
14815 	/*
14816 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14817 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14818 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14819 	 */
14820 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14821 
14822 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14823 	if (groupname != NULL)
14824 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst);
14825 	else {
14826 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14827 	}
14828 
14829 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14830 
14831 	/*
14832 	 * both groups are on the same ipsq.
14833 	 */
14834 	if (old_ipsq == new_ipsq) {
14835 		rw_exit(&ipst->ips_ill_g_lock);
14836 		return (0);
14837 	}
14838 
14839 	cnt = old_ipsq->ipsq_refs << 1;
14840 	ill_list_size = cnt * sizeof (ill_t *);
14841 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14842 	if (ill_list == NULL) {
14843 		rw_exit(&ipst->ips_ill_g_lock);
14844 		return (ENOMEM);
14845 	}
14846 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14847 
14848 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14849 	mutex_enter(&new_ipsq->ipsq_lock);
14850 	if ((new_ipsq->ipsq_writer == NULL &&
14851 	    new_ipsq->ipsq_current_ipif == NULL) ||
14852 	    (new_ipsq->ipsq_writer == curthread)) {
14853 		new_ipsq->ipsq_writer = curthread;
14854 		new_ipsq->ipsq_reentry_cnt++;
14855 		became_writer_on_new_sq = B_TRUE;
14856 	}
14857 
14858 	/*
14859 	 * We are holding ill_g_lock as writer and all the ill locks of
14860 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14861 	 * message can land up on the old ipsq even though we don't hold the
14862 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14863 	 */
14864 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14865 
14866 	/*
14867 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14868 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14869 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14870 	 */
14871 	ill_merge_ipsq(old_ipsq, new_ipsq, ipst);
14872 
14873 	/*
14874 	 * Mark the new ipsq as needing a split since it is currently
14875 	 * being shared by more than 1 IPMP group. The split will
14876 	 * occur at the end of ipsq_exit
14877 	 */
14878 	new_ipsq->ipsq_split = B_TRUE;
14879 
14880 	/* Now release all the locks */
14881 	mutex_exit(&new_ipsq->ipsq_lock);
14882 	ill_unlock_ills(ill_list, cnt);
14883 	rw_exit(&ipst->ips_ill_g_lock);
14884 
14885 	kmem_free(ill_list, ill_list_size);
14886 
14887 	/*
14888 	 * If we succeeded in becoming writer on the new ipsq, then
14889 	 * drain the new ipsq and start processing  all enqueued messages
14890 	 * including the current ioctl we are processing which is either
14891 	 * a set groupname or failover/failback.
14892 	 */
14893 	if (became_writer_on_new_sq)
14894 		ipsq_exit(new_ipsq);
14895 
14896 	/*
14897 	 * syncq has been changed and all the messages have been moved.
14898 	 */
14899 	mutex_enter(&old_ipsq->ipsq_lock);
14900 	old_ipsq->ipsq_current_ipif = NULL;
14901 	old_ipsq->ipsq_current_ioctl = 0;
14902 	old_ipsq->ipsq_current_done = B_TRUE;
14903 	mutex_exit(&old_ipsq->ipsq_lock);
14904 	return (EINPROGRESS);
14905 }
14906 
14907 /*
14908  * Delete and add the loopback copy and non-loopback copy of
14909  * the BROADCAST ire corresponding to ill and addr. Used to
14910  * group broadcast ires together when ill becomes part of
14911  * a group.
14912  *
14913  * This function is also called when ill is leaving the group
14914  * so that the ires belonging to the group gets re-grouped.
14915  */
14916 static void
14917 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14918 {
14919 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14920 	ire_t **ire_ptpn = &ire_head;
14921 	ip_stack_t	*ipst = ill->ill_ipst;
14922 
14923 	/*
14924 	 * The loopback and non-loopback IREs are inserted in the order in which
14925 	 * they're found, on the basis that they are correctly ordered (loopback
14926 	 * first).
14927 	 */
14928 	for (;;) {
14929 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14930 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
14931 		if (ire == NULL)
14932 			break;
14933 
14934 		/*
14935 		 * we are passing in KM_SLEEP because it is not easy to
14936 		 * go back to a sane state in case of memory failure.
14937 		 */
14938 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14939 		ASSERT(nire != NULL);
14940 		bzero(nire, sizeof (ire_t));
14941 		/*
14942 		 * Don't use ire_max_frag directly since we don't
14943 		 * hold on to 'ire' until we add the new ire 'nire' and
14944 		 * we don't want the new ire to have a dangling reference
14945 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14946 		 * be in sync with the ipif_mtu of the associate ipif.
14947 		 * For eg. this happens as a result of SIOCSLIFNAME,
14948 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14949 		 * the driver. A change in ire_max_frag triggered as
14950 		 * as a result of path mtu discovery, or due to an
14951 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14952 		 * route change -mtu command does not apply to broadcast ires.
14953 		 *
14954 		 * XXX We need a recovery strategy here if ire_init fails
14955 		 */
14956 		if (ire_init(nire,
14957 		    (uchar_t *)&ire->ire_addr,
14958 		    (uchar_t *)&ire->ire_mask,
14959 		    (uchar_t *)&ire->ire_src_addr,
14960 		    (uchar_t *)&ire->ire_gateway_addr,
14961 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14962 		    &ire->ire_ipif->ipif_mtu,
14963 		    ire->ire_nce,
14964 		    ire->ire_rfq,
14965 		    ire->ire_stq,
14966 		    ire->ire_type,
14967 		    ire->ire_ipif,
14968 		    ire->ire_cmask,
14969 		    ire->ire_phandle,
14970 		    ire->ire_ihandle,
14971 		    ire->ire_flags,
14972 		    &ire->ire_uinfo,
14973 		    NULL,
14974 		    NULL,
14975 		    ipst) == NULL) {
14976 			cmn_err(CE_PANIC, "ire_init() failed");
14977 		}
14978 		ire_delete(ire);
14979 		ire_refrele(ire);
14980 
14981 		/*
14982 		 * The newly created IREs are inserted at the tail of the list
14983 		 * starting with ire_head. As we've just allocated them no one
14984 		 * knows about them so it's safe.
14985 		 */
14986 		*ire_ptpn = nire;
14987 		ire_ptpn = &nire->ire_next;
14988 	}
14989 
14990 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14991 		int error;
14992 		ire_t *oire;
14993 		/* unlink the IRE from our list before calling ire_add() */
14994 		nire_next = nire->ire_next;
14995 		nire->ire_next = NULL;
14996 
14997 		/* ire_add adds the ire at the right place in the list */
14998 		oire = nire;
14999 		error = ire_add(&nire, NULL, NULL, NULL, B_FALSE);
15000 		ASSERT(error == 0);
15001 		ASSERT(oire == nire);
15002 		ire_refrele(nire);	/* Held in ire_add */
15003 	}
15004 }
15005 
15006 /*
15007  * This function is usually called when an ill is inserted in
15008  * a group and all the ipifs are already UP. As all the ipifs
15009  * are already UP, the broadcast ires have already been created
15010  * and been inserted. But, ire_add_v4 would not have grouped properly.
15011  * We need to re-group for the benefit of ip_wput_ire which
15012  * expects BROADCAST ires to be grouped properly to avoid sending
15013  * more than one copy of the broadcast packet per group.
15014  *
15015  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
15016  *	  because when ipif_up_done ends up calling this, ires have
15017  *        already been added before illgrp_insert i.e before ill_group
15018  *	  has been initialized.
15019  */
15020 static void
15021 ill_group_bcast_for_xmit(ill_t *ill)
15022 {
15023 	ill_group_t *illgrp;
15024 	ipif_t *ipif;
15025 	ipaddr_t addr;
15026 	ipaddr_t net_mask;
15027 	ipaddr_t subnet_netmask;
15028 
15029 	illgrp = ill->ill_group;
15030 
15031 	/*
15032 	 * This function is called even when an ill is deleted from
15033 	 * the group. Hence, illgrp could be null.
15034 	 */
15035 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
15036 		return;
15037 
15038 	/*
15039 	 * Delete all the BROADCAST ires matching this ill and add
15040 	 * them back. This time, ire_add_v4 should take care of
15041 	 * grouping them with others because ill is part of the
15042 	 * group.
15043 	 */
15044 	ill_bcast_delete_and_add(ill, 0);
15045 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
15046 
15047 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15048 
15049 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15050 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15051 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15052 		} else {
15053 			net_mask = htonl(IN_CLASSA_NET);
15054 		}
15055 		addr = net_mask & ipif->ipif_subnet;
15056 		ill_bcast_delete_and_add(ill, addr);
15057 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
15058 
15059 		subnet_netmask = ipif->ipif_net_mask;
15060 		addr = ipif->ipif_subnet;
15061 		ill_bcast_delete_and_add(ill, addr);
15062 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
15063 	}
15064 }
15065 
15066 /*
15067  * This function is called from illgrp_delete when ill is being deleted
15068  * from the group.
15069  *
15070  * As ill is not there in the group anymore, any address belonging
15071  * to this ill should be cleared of IRE_MARK_NORECV.
15072  */
15073 static void
15074 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
15075 {
15076 	ire_t *ire;
15077 	irb_t *irb;
15078 	ip_stack_t	*ipst = ill->ill_ipst;
15079 
15080 	ASSERT(ill->ill_group == NULL);
15081 
15082 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
15083 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst);
15084 
15085 	if (ire != NULL) {
15086 		/*
15087 		 * IPMP and plumbing operations are serialized on the ipsq, so
15088 		 * no one will insert or delete a broadcast ire under our feet.
15089 		 */
15090 		irb = ire->ire_bucket;
15091 		rw_enter(&irb->irb_lock, RW_READER);
15092 		ire_refrele(ire);
15093 
15094 		for (; ire != NULL; ire = ire->ire_next) {
15095 			if (ire->ire_addr != addr)
15096 				break;
15097 			if (ire_to_ill(ire) != ill)
15098 				continue;
15099 
15100 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
15101 			ire->ire_marks &= ~IRE_MARK_NORECV;
15102 		}
15103 		rw_exit(&irb->irb_lock);
15104 	}
15105 }
15106 
15107 /*
15108  * This function must be called only after the broadcast ires
15109  * have been grouped together. For a given address addr, nominate
15110  * only one of the ires whose interface is not FAILED or OFFLINE.
15111  *
15112  * This is also called when an ipif goes down, so that we can nominate
15113  * a different ire with the same address for receiving.
15114  */
15115 static void
15116 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst)
15117 {
15118 	irb_t *irb;
15119 	ire_t *ire;
15120 	ire_t *ire1;
15121 	ire_t *save_ire;
15122 	ire_t **irep = NULL;
15123 	boolean_t first = B_TRUE;
15124 	ire_t *clear_ire = NULL;
15125 	ire_t *start_ire = NULL;
15126 	ire_t	*new_lb_ire;
15127 	ire_t	*new_nlb_ire;
15128 	boolean_t new_lb_ire_used = B_FALSE;
15129 	boolean_t new_nlb_ire_used = B_FALSE;
15130 	uint64_t match_flags;
15131 	uint64_t phyi_flags;
15132 	boolean_t fallback = B_FALSE;
15133 	uint_t	max_frag;
15134 
15135 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
15136 	    NULL, MATCH_IRE_TYPE, ipst);
15137 	/*
15138 	 * We may not be able to find some ires if a previous
15139 	 * ire_create failed. This happens when an ipif goes
15140 	 * down and we are unable to create BROADCAST ires due
15141 	 * to memory failure. Thus, we have to check for NULL
15142 	 * below. This should handle the case for LOOPBACK,
15143 	 * POINTOPOINT and interfaces with some POINTOPOINT
15144 	 * logicals for which there are no BROADCAST ires.
15145 	 */
15146 	if (ire == NULL)
15147 		return;
15148 	/*
15149 	 * Currently IRE_BROADCASTS are deleted when an ipif
15150 	 * goes down which runs exclusively. Thus, setting
15151 	 * IRE_MARK_RCVD should not race with ire_delete marking
15152 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
15153 	 * be consistent with other parts of the code that walks
15154 	 * a given bucket.
15155 	 */
15156 	save_ire = ire;
15157 	irb = ire->ire_bucket;
15158 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15159 	if (new_lb_ire == NULL) {
15160 		ire_refrele(ire);
15161 		return;
15162 	}
15163 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
15164 	if (new_nlb_ire == NULL) {
15165 		ire_refrele(ire);
15166 		kmem_cache_free(ire_cache, new_lb_ire);
15167 		return;
15168 	}
15169 	IRB_REFHOLD(irb);
15170 	rw_enter(&irb->irb_lock, RW_WRITER);
15171 	/*
15172 	 * Get to the first ire matching the address and the
15173 	 * group. If the address does not match we are done
15174 	 * as we could not find the IRE. If the address matches
15175 	 * we should get to the first one matching the group.
15176 	 */
15177 	while (ire != NULL) {
15178 		if (ire->ire_addr != addr ||
15179 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15180 			break;
15181 		}
15182 		ire = ire->ire_next;
15183 	}
15184 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
15185 	start_ire = ire;
15186 redo:
15187 	while (ire != NULL && ire->ire_addr == addr &&
15188 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
15189 		/*
15190 		 * The first ire for any address within a group
15191 		 * should always be the one with IRE_MARK_NORECV cleared
15192 		 * so that ip_wput_ire can avoid searching for one.
15193 		 * Note down the insertion point which will be used
15194 		 * later.
15195 		 */
15196 		if (first && (irep == NULL))
15197 			irep = ire->ire_ptpn;
15198 		/*
15199 		 * PHYI_FAILED is set when the interface fails.
15200 		 * This interface might have become good, but the
15201 		 * daemon has not yet detected. We should still
15202 		 * not receive on this. PHYI_OFFLINE should never
15203 		 * be picked as this has been offlined and soon
15204 		 * be removed.
15205 		 */
15206 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
15207 		if (phyi_flags & PHYI_OFFLINE) {
15208 			ire->ire_marks |= IRE_MARK_NORECV;
15209 			ire = ire->ire_next;
15210 			continue;
15211 		}
15212 		if (phyi_flags & match_flags) {
15213 			ire->ire_marks |= IRE_MARK_NORECV;
15214 			ire = ire->ire_next;
15215 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
15216 			    PHYI_INACTIVE) {
15217 				fallback = B_TRUE;
15218 			}
15219 			continue;
15220 		}
15221 		if (first) {
15222 			/*
15223 			 * We will move this to the front of the list later
15224 			 * on.
15225 			 */
15226 			clear_ire = ire;
15227 			ire->ire_marks &= ~IRE_MARK_NORECV;
15228 		} else {
15229 			ire->ire_marks |= IRE_MARK_NORECV;
15230 		}
15231 		first = B_FALSE;
15232 		ire = ire->ire_next;
15233 	}
15234 	/*
15235 	 * If we never nominated anybody, try nominating at least
15236 	 * an INACTIVE, if we found one. Do it only once though.
15237 	 */
15238 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
15239 	    fallback) {
15240 		match_flags = PHYI_FAILED;
15241 		ire = start_ire;
15242 		irep = NULL;
15243 		goto redo;
15244 	}
15245 	ire_refrele(save_ire);
15246 
15247 	/*
15248 	 * irep non-NULL indicates that we entered the while loop
15249 	 * above. If clear_ire is at the insertion point, we don't
15250 	 * have to do anything. clear_ire will be NULL if all the
15251 	 * interfaces are failed.
15252 	 *
15253 	 * We cannot unlink and reinsert the ire at the right place
15254 	 * in the list since there can be other walkers of this bucket.
15255 	 * Instead we delete and recreate the ire
15256 	 */
15257 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
15258 		ire_t *clear_ire_stq = NULL;
15259 
15260 		bzero(new_lb_ire, sizeof (ire_t));
15261 		/* XXX We need a recovery strategy here. */
15262 		if (ire_init(new_lb_ire,
15263 		    (uchar_t *)&clear_ire->ire_addr,
15264 		    (uchar_t *)&clear_ire->ire_mask,
15265 		    (uchar_t *)&clear_ire->ire_src_addr,
15266 		    (uchar_t *)&clear_ire->ire_gateway_addr,
15267 		    &clear_ire->ire_max_frag,
15268 		    NULL, /* let ire_nce_init derive the resolver info */
15269 		    clear_ire->ire_rfq,
15270 		    clear_ire->ire_stq,
15271 		    clear_ire->ire_type,
15272 		    clear_ire->ire_ipif,
15273 		    clear_ire->ire_cmask,
15274 		    clear_ire->ire_phandle,
15275 		    clear_ire->ire_ihandle,
15276 		    clear_ire->ire_flags,
15277 		    &clear_ire->ire_uinfo,
15278 		    NULL,
15279 		    NULL,
15280 		    ipst) == NULL)
15281 			cmn_err(CE_PANIC, "ire_init() failed");
15282 		if (clear_ire->ire_stq == NULL) {
15283 			ire_t *ire_next = clear_ire->ire_next;
15284 			if (ire_next != NULL &&
15285 			    ire_next->ire_stq != NULL &&
15286 			    ire_next->ire_addr == clear_ire->ire_addr &&
15287 			    ire_next->ire_ipif->ipif_ill ==
15288 			    clear_ire->ire_ipif->ipif_ill) {
15289 				clear_ire_stq = ire_next;
15290 
15291 				bzero(new_nlb_ire, sizeof (ire_t));
15292 				/* XXX We need a recovery strategy here. */
15293 				if (ire_init(new_nlb_ire,
15294 				    (uchar_t *)&clear_ire_stq->ire_addr,
15295 				    (uchar_t *)&clear_ire_stq->ire_mask,
15296 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
15297 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
15298 				    &clear_ire_stq->ire_max_frag,
15299 				    NULL,
15300 				    clear_ire_stq->ire_rfq,
15301 				    clear_ire_stq->ire_stq,
15302 				    clear_ire_stq->ire_type,
15303 				    clear_ire_stq->ire_ipif,
15304 				    clear_ire_stq->ire_cmask,
15305 				    clear_ire_stq->ire_phandle,
15306 				    clear_ire_stq->ire_ihandle,
15307 				    clear_ire_stq->ire_flags,
15308 				    &clear_ire_stq->ire_uinfo,
15309 				    NULL,
15310 				    NULL,
15311 				    ipst) == NULL)
15312 					cmn_err(CE_PANIC, "ire_init() failed");
15313 			}
15314 		}
15315 
15316 		/*
15317 		 * Delete the ire. We can't call ire_delete() since
15318 		 * we are holding the bucket lock. We can't release the
15319 		 * bucket lock since we can't allow irep to change. So just
15320 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
15321 		 * ire from the list and do the refrele.
15322 		 */
15323 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
15324 		irb->irb_marks |= IRB_MARK_CONDEMNED;
15325 
15326 		if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) {
15327 			nce_fastpath_list_delete(clear_ire_stq->ire_nce);
15328 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
15329 		}
15330 
15331 		/*
15332 		 * Also take care of otherfields like ib/ob pkt count
15333 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
15334 		 */
15335 
15336 		/* Set the max_frag before adding the ire */
15337 		max_frag = *new_lb_ire->ire_max_fragp;
15338 		new_lb_ire->ire_max_fragp = NULL;
15339 		new_lb_ire->ire_max_frag = max_frag;
15340 
15341 		/* Add the new ire's. Insert at *irep */
15342 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
15343 		ire1 = *irep;
15344 		if (ire1 != NULL)
15345 			ire1->ire_ptpn = &new_lb_ire->ire_next;
15346 		new_lb_ire->ire_next = ire1;
15347 		/* Link the new one in. */
15348 		new_lb_ire->ire_ptpn = irep;
15349 		membar_producer();
15350 		*irep = new_lb_ire;
15351 		new_lb_ire_used = B_TRUE;
15352 		BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted);
15353 		new_lb_ire->ire_bucket->irb_ire_cnt++;
15354 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), new_lb_ire->ire_ipif,
15355 		    (char *), "ire", (void *), new_lb_ire);
15356 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
15357 
15358 		if (clear_ire_stq != NULL) {
15359 			/* Set the max_frag before adding the ire */
15360 			max_frag = *new_nlb_ire->ire_max_fragp;
15361 			new_nlb_ire->ire_max_fragp = NULL;
15362 			new_nlb_ire->ire_max_frag = max_frag;
15363 
15364 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
15365 			irep = &new_lb_ire->ire_next;
15366 			/* Add the new ire. Insert at *irep */
15367 			ire1 = *irep;
15368 			if (ire1 != NULL)
15369 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
15370 			new_nlb_ire->ire_next = ire1;
15371 			/* Link the new one in. */
15372 			new_nlb_ire->ire_ptpn = irep;
15373 			membar_producer();
15374 			*irep = new_nlb_ire;
15375 			new_nlb_ire_used = B_TRUE;
15376 			BUMP_IRE_STATS(ipst->ips_ire_stats_v4,
15377 			    ire_stats_inserted);
15378 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
15379 			DTRACE_PROBE3(ipif__incr__cnt,
15380 			    (ipif_t *), new_nlb_ire->ire_ipif,
15381 			    (char *), "ire", (void *), new_nlb_ire);
15382 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
15383 			DTRACE_PROBE3(ill__incr__cnt,
15384 			    (ill_t *), new_nlb_ire->ire_stq->q_ptr,
15385 			    (char *), "ire", (void *), new_nlb_ire);
15386 			((ill_t *)(new_nlb_ire->ire_stq->q_ptr))->ill_ire_cnt++;
15387 		}
15388 	}
15389 	rw_exit(&irb->irb_lock);
15390 	if (!new_lb_ire_used)
15391 		kmem_cache_free(ire_cache, new_lb_ire);
15392 	if (!new_nlb_ire_used)
15393 		kmem_cache_free(ire_cache, new_nlb_ire);
15394 	IRB_REFRELE(irb);
15395 }
15396 
15397 /*
15398  * Whenever an ipif goes down we have to renominate a different
15399  * broadcast ire to receive. Whenever an ipif comes up, we need
15400  * to make sure that we have only one nominated to receive.
15401  */
15402 static void
15403 ipif_renominate_bcast(ipif_t *ipif)
15404 {
15405 	ill_t *ill = ipif->ipif_ill;
15406 	ipaddr_t subnet_addr;
15407 	ipaddr_t net_addr;
15408 	ipaddr_t net_mask = 0;
15409 	ipaddr_t subnet_netmask;
15410 	ipaddr_t addr;
15411 	ill_group_t *illgrp;
15412 	ip_stack_t	*ipst = ill->ill_ipst;
15413 
15414 	illgrp = ill->ill_group;
15415 	/*
15416 	 * If this is the last ipif going down, it might take
15417 	 * the ill out of the group. In that case ipif_down ->
15418 	 * illgrp_delete takes care of doing the nomination.
15419 	 * ipif_down does not call for this case.
15420 	 */
15421 	ASSERT(illgrp != NULL);
15422 
15423 	/* There could not have been any ires associated with this */
15424 	if (ipif->ipif_subnet == 0)
15425 		return;
15426 
15427 	ill_mark_bcast(illgrp, 0, ipst);
15428 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15429 
15430 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15431 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15432 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15433 	} else {
15434 		net_mask = htonl(IN_CLASSA_NET);
15435 	}
15436 	addr = net_mask & ipif->ipif_subnet;
15437 	ill_mark_bcast(illgrp, addr, ipst);
15438 
15439 	net_addr = ~net_mask | addr;
15440 	ill_mark_bcast(illgrp, net_addr, ipst);
15441 
15442 	subnet_netmask = ipif->ipif_net_mask;
15443 	addr = ipif->ipif_subnet;
15444 	ill_mark_bcast(illgrp, addr, ipst);
15445 
15446 	subnet_addr = ~subnet_netmask | addr;
15447 	ill_mark_bcast(illgrp, subnet_addr, ipst);
15448 }
15449 
15450 /*
15451  * Whenever we form or delete ill groups, we need to nominate one set of
15452  * BROADCAST ires for receiving in the group.
15453  *
15454  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
15455  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
15456  *    for ill_ipif_up_count to be non-zero. This is the only case where
15457  *    ill_ipif_up_count is zero and we would still find the ires.
15458  *
15459  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
15460  *    ipif is UP and we just have to do the nomination.
15461  *
15462  * 3) When ill_handoff_responsibility calls us, some ill has been removed
15463  *    from the group. So, we have to do the nomination.
15464  *
15465  * Because of (3), there could be just one ill in the group. But we have
15466  * to nominate still as IRE_MARK_NORCV may have been marked on this.
15467  * Thus, this function does not optimize when there is only one ill as
15468  * it is not correct for (3).
15469  */
15470 static void
15471 ill_nominate_bcast_rcv(ill_group_t *illgrp)
15472 {
15473 	ill_t *ill;
15474 	ipif_t *ipif;
15475 	ipaddr_t subnet_addr;
15476 	ipaddr_t prev_subnet_addr = 0;
15477 	ipaddr_t net_addr;
15478 	ipaddr_t prev_net_addr = 0;
15479 	ipaddr_t net_mask = 0;
15480 	ipaddr_t subnet_netmask;
15481 	ipaddr_t addr;
15482 	ip_stack_t	*ipst;
15483 
15484 	/*
15485 	 * When the last memeber is leaving, there is nothing to
15486 	 * nominate.
15487 	 */
15488 	if (illgrp->illgrp_ill_count == 0) {
15489 		ASSERT(illgrp->illgrp_ill == NULL);
15490 		return;
15491 	}
15492 
15493 	ill = illgrp->illgrp_ill;
15494 	ASSERT(!ill->ill_isv6);
15495 	ipst = ill->ill_ipst;
15496 	/*
15497 	 * We assume that ires with same address and belonging to the
15498 	 * same group, has been grouped together. Nominating a *single*
15499 	 * ill in the group for sending and receiving broadcast is done
15500 	 * by making sure that the first BROADCAST ire (which will be
15501 	 * the one returned by ire_ctable_lookup for ip_rput and the
15502 	 * one that will be used in ip_wput_ire) will be the one that
15503 	 * will not have IRE_MARK_NORECV set.
15504 	 *
15505 	 * 1) ip_rput checks and discards packets received on ires marked
15506 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
15507 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
15508 	 *    first ire in the group for every broadcast address in the group.
15509 	 *    ip_rput will accept packets only on the first ire i.e only
15510 	 *    one copy of the ill.
15511 	 *
15512 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
15513 	 *    packet for the whole group. It needs to send out on the ill
15514 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
15515 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
15516 	 *    the copy echoed back on other port where the ire is not marked
15517 	 *    with IRE_MARK_NORECV.
15518 	 *
15519 	 * Note that we just need to have the first IRE either loopback or
15520 	 * non-loopback (either of them may not exist if ire_create failed
15521 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
15522 	 * always hit the first one and hence will always accept one copy.
15523 	 *
15524 	 * We have a broadcast ire per ill for all the unique prefixes
15525 	 * hosted on that ill. As we don't have a way of knowing the
15526 	 * unique prefixes on a given ill and hence in the whole group,
15527 	 * we just call ill_mark_bcast on all the prefixes that exist
15528 	 * in the group. For the common case of one prefix, the code
15529 	 * below optimizes by remebering the last address used for
15530 	 * markng. In the case of multiple prefixes, this will still
15531 	 * optimize depending the order of prefixes.
15532 	 *
15533 	 * The only unique address across the whole group is 0.0.0.0 and
15534 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
15535 	 * the first ire in the bucket for receiving and disables the
15536 	 * others.
15537 	 */
15538 	ill_mark_bcast(illgrp, 0, ipst);
15539 	ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst);
15540 	for (; ill != NULL; ill = ill->ill_group_next) {
15541 
15542 		for (ipif = ill->ill_ipif; ipif != NULL;
15543 		    ipif = ipif->ipif_next) {
15544 
15545 			if (!(ipif->ipif_flags & IPIF_UP) ||
15546 			    ipif->ipif_subnet == 0) {
15547 				continue;
15548 			}
15549 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15550 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15551 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15552 			} else {
15553 				net_mask = htonl(IN_CLASSA_NET);
15554 			}
15555 			addr = net_mask & ipif->ipif_subnet;
15556 			if (prev_net_addr == 0 || prev_net_addr != addr) {
15557 				ill_mark_bcast(illgrp, addr, ipst);
15558 				net_addr = ~net_mask | addr;
15559 				ill_mark_bcast(illgrp, net_addr, ipst);
15560 			}
15561 			prev_net_addr = addr;
15562 
15563 			subnet_netmask = ipif->ipif_net_mask;
15564 			addr = ipif->ipif_subnet;
15565 			if (prev_subnet_addr == 0 ||
15566 			    prev_subnet_addr != addr) {
15567 				ill_mark_bcast(illgrp, addr, ipst);
15568 				subnet_addr = ~subnet_netmask | addr;
15569 				ill_mark_bcast(illgrp, subnet_addr, ipst);
15570 			}
15571 			prev_subnet_addr = addr;
15572 		}
15573 	}
15574 }
15575 
15576 /*
15577  * This function is called while forming ill groups.
15578  *
15579  * Currently, we handle only allmulti groups. We want to join
15580  * allmulti on only one of the ills in the groups. In future,
15581  * when we have link aggregation, we may have to join normal
15582  * multicast groups on multiple ills as switch does inbound load
15583  * balancing. Following are the functions that calls this
15584  * function :
15585  *
15586  * 1) ill_recover_multicast : Interface is coming back UP.
15587  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
15588  *    will call ill_recover_multicast to recover all the multicast
15589  *    groups. We need to make sure that only one member is joined
15590  *    in the ill group.
15591  *
15592  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
15593  *    Somebody is joining allmulti. We need to make sure that only one
15594  *    member is joined in the group.
15595  *
15596  * 3) illgrp_insert : If allmulti has already joined, we need to make
15597  *    sure that only one member is joined in the group.
15598  *
15599  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
15600  *    allmulti who we have nominated. We need to pick someother ill.
15601  *
15602  * 5) illgrp_delete : The ill we nominated is leaving the group,
15603  *    we need to pick a new ill to join the group.
15604  *
15605  * For (1), (2), (5) - we just have to check whether there is
15606  * a good ill joined in the group. If we could not find any ills
15607  * joined the group, we should join.
15608  *
15609  * For (4), the one that was nominated to receive, left the group.
15610  * There could be nobody joined in the group when this function is
15611  * called.
15612  *
15613  * For (3) - we need to explicitly check whether there are multiple
15614  * ills joined in the group.
15615  *
15616  * For simplicity, we don't differentiate any of the above cases. We
15617  * just leave the group if it is joined on any of them and join on
15618  * the first good ill.
15619  */
15620 int
15621 ill_nominate_mcast_rcv(ill_group_t *illgrp)
15622 {
15623 	ilm_t *ilm;
15624 	ill_t *ill;
15625 	ill_t *fallback_inactive_ill = NULL;
15626 	ill_t *fallback_failed_ill = NULL;
15627 	int ret = 0;
15628 
15629 	/*
15630 	 * Leave the allmulti on all the ills and start fresh.
15631 	 */
15632 	for (ill = illgrp->illgrp_ill; ill != NULL;
15633 	    ill = ill->ill_group_next) {
15634 		if (ill->ill_join_allmulti)
15635 			(void) ip_leave_allmulti(ill->ill_ipif);
15636 	}
15637 
15638 	/*
15639 	 * Choose a good ill. Fallback to inactive or failed if
15640 	 * none available. We need to fallback to FAILED in the
15641 	 * case where we have 2 interfaces in a group - where
15642 	 * one of them is failed and another is a good one and
15643 	 * the good one (not marked inactive) is leaving the group.
15644 	 */
15645 	ret = 0;
15646 	for (ill = illgrp->illgrp_ill; ill != NULL;
15647 	    ill = ill->ill_group_next) {
15648 		/* Never pick an offline interface */
15649 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
15650 			continue;
15651 
15652 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
15653 			fallback_failed_ill = ill;
15654 			continue;
15655 		}
15656 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
15657 			fallback_inactive_ill = ill;
15658 			continue;
15659 		}
15660 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15661 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15662 				ret = ip_join_allmulti(ill->ill_ipif);
15663 				/*
15664 				 * ip_join_allmulti can fail because of memory
15665 				 * failures. So, make sure we join at least
15666 				 * on one ill.
15667 				 */
15668 				if (ill->ill_join_allmulti)
15669 					return (0);
15670 			}
15671 		}
15672 	}
15673 	if (ret != 0) {
15674 		/*
15675 		 * If we tried nominating above and failed to do so,
15676 		 * return error. We might have tried multiple times.
15677 		 * But, return the latest error.
15678 		 */
15679 		return (ret);
15680 	}
15681 	if ((ill = fallback_inactive_ill) != NULL) {
15682 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15683 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15684 				ret = ip_join_allmulti(ill->ill_ipif);
15685 				return (ret);
15686 			}
15687 		}
15688 	} else if ((ill = fallback_failed_ill) != NULL) {
15689 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15690 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15691 				ret = ip_join_allmulti(ill->ill_ipif);
15692 				return (ret);
15693 			}
15694 		}
15695 	}
15696 	return (0);
15697 }
15698 
15699 /*
15700  * This function is called from illgrp_delete after it is
15701  * deleted from the group to reschedule responsibilities
15702  * to a different ill.
15703  */
15704 static void
15705 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
15706 {
15707 	ilm_t	*ilm;
15708 	ipif_t	*ipif;
15709 	ipaddr_t subnet_addr;
15710 	ipaddr_t net_addr;
15711 	ipaddr_t net_mask = 0;
15712 	ipaddr_t subnet_netmask;
15713 	ipaddr_t addr;
15714 	ip_stack_t *ipst = ill->ill_ipst;
15715 
15716 	ASSERT(ill->ill_group == NULL);
15717 	/*
15718 	 * Broadcast Responsibility:
15719 	 *
15720 	 * 1. If this ill has been nominated for receiving broadcast
15721 	 * packets, we need to find a new one. Before we find a new
15722 	 * one, we need to re-group the ires that are part of this new
15723 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
15724 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
15725 	 * thing for us.
15726 	 *
15727 	 * 2. If this ill was not nominated for receiving broadcast
15728 	 * packets, we need to clear the IRE_MARK_NORECV flag
15729 	 * so that we continue to send up broadcast packets.
15730 	 */
15731 	if (!ill->ill_isv6) {
15732 		/*
15733 		 * Case 1 above : No optimization here. Just redo the
15734 		 * nomination.
15735 		 */
15736 		ill_group_bcast_for_xmit(ill);
15737 		ill_nominate_bcast_rcv(illgrp);
15738 
15739 		/*
15740 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
15741 		 */
15742 		ill_clear_bcast_mark(ill, 0);
15743 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
15744 
15745 		for (ipif = ill->ill_ipif; ipif != NULL;
15746 		    ipif = ipif->ipif_next) {
15747 
15748 			if (!(ipif->ipif_flags & IPIF_UP) ||
15749 			    ipif->ipif_subnet == 0) {
15750 				continue;
15751 			}
15752 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
15753 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
15754 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
15755 			} else {
15756 				net_mask = htonl(IN_CLASSA_NET);
15757 			}
15758 			addr = net_mask & ipif->ipif_subnet;
15759 			ill_clear_bcast_mark(ill, addr);
15760 
15761 			net_addr = ~net_mask | addr;
15762 			ill_clear_bcast_mark(ill, net_addr);
15763 
15764 			subnet_netmask = ipif->ipif_net_mask;
15765 			addr = ipif->ipif_subnet;
15766 			ill_clear_bcast_mark(ill, addr);
15767 
15768 			subnet_addr = ~subnet_netmask | addr;
15769 			ill_clear_bcast_mark(ill, subnet_addr);
15770 		}
15771 	}
15772 
15773 	/*
15774 	 * Multicast Responsibility.
15775 	 *
15776 	 * If we have joined allmulti on this one, find a new member
15777 	 * in the group to join allmulti. As this ill is already part
15778 	 * of allmulti, we don't have to join on this one.
15779 	 *
15780 	 * If we have not joined allmulti on this one, there is no
15781 	 * responsibility to handoff. But we need to take new
15782 	 * responsibility i.e, join allmulti on this one if we need
15783 	 * to.
15784 	 */
15785 	if (ill->ill_join_allmulti) {
15786 		(void) ill_nominate_mcast_rcv(illgrp);
15787 	} else {
15788 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15789 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15790 				(void) ip_join_allmulti(ill->ill_ipif);
15791 				break;
15792 			}
15793 		}
15794 	}
15795 
15796 	/*
15797 	 * We intentionally do the flushing of IRE_CACHES only matching
15798 	 * on the ill and not on groups. Note that we are already deleted
15799 	 * from the group.
15800 	 *
15801 	 * This will make sure that all IRE_CACHES whose stq is pointing
15802 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
15803 	 * deleted and IRE_CACHES that are not pointing at this ill will
15804 	 * be left alone.
15805 	 */
15806 	if (ill->ill_isv6) {
15807 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15808 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15809 	} else {
15810 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
15811 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
15812 	}
15813 
15814 	/*
15815 	 * Some conn may have cached one of the IREs deleted above. By removing
15816 	 * the ire reference, we clean up the extra reference to the ill held in
15817 	 * ire->ire_stq.
15818 	 */
15819 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
15820 
15821 	/*
15822 	 * Re-do source address selection for all the members in the
15823 	 * group, if they borrowed source address from one of the ipifs
15824 	 * in this ill.
15825 	 */
15826 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15827 		if (ill->ill_isv6) {
15828 			ipif_update_other_ipifs_v6(ipif, illgrp);
15829 		} else {
15830 			ipif_update_other_ipifs(ipif, illgrp);
15831 		}
15832 	}
15833 }
15834 
15835 /*
15836  * Delete the ill from the group. The caller makes sure that it is
15837  * in a group and it okay to delete from the group. So, we always
15838  * delete here.
15839  */
15840 static void
15841 illgrp_delete(ill_t *ill)
15842 {
15843 	ill_group_t *illgrp;
15844 	ill_group_t *tmpg;
15845 	ill_t *tmp_ill;
15846 	ip_stack_t	*ipst = ill->ill_ipst;
15847 
15848 	/*
15849 	 * Reset illgrp_ill_schednext if it was pointing at us.
15850 	 * We need to do this before we set ill_group to NULL.
15851 	 */
15852 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15853 	mutex_enter(&ill->ill_lock);
15854 
15855 	illgrp_reset_schednext(ill);
15856 
15857 	illgrp = ill->ill_group;
15858 
15859 	/* Delete the ill from illgrp. */
15860 	if (illgrp->illgrp_ill == ill) {
15861 		illgrp->illgrp_ill = ill->ill_group_next;
15862 	} else {
15863 		tmp_ill = illgrp->illgrp_ill;
15864 		while (tmp_ill->ill_group_next != ill) {
15865 			tmp_ill = tmp_ill->ill_group_next;
15866 			ASSERT(tmp_ill != NULL);
15867 		}
15868 		tmp_ill->ill_group_next = ill->ill_group_next;
15869 	}
15870 	ill->ill_group = NULL;
15871 	ill->ill_group_next = NULL;
15872 
15873 	illgrp->illgrp_ill_count--;
15874 	mutex_exit(&ill->ill_lock);
15875 	rw_exit(&ipst->ips_ill_g_lock);
15876 
15877 	/*
15878 	 * As this ill is leaving the group, we need to hand off
15879 	 * the responsibilities to the other ills in the group, if
15880 	 * this ill had some responsibilities.
15881 	 */
15882 
15883 	ill_handoff_responsibility(ill, illgrp);
15884 
15885 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15886 
15887 	if (illgrp->illgrp_ill_count == 0) {
15888 
15889 		ASSERT(illgrp->illgrp_ill == NULL);
15890 		if (ill->ill_isv6) {
15891 			if (illgrp == ipst->ips_illgrp_head_v6) {
15892 				ipst->ips_illgrp_head_v6 = illgrp->illgrp_next;
15893 			} else {
15894 				tmpg = ipst->ips_illgrp_head_v6;
15895 				while (tmpg->illgrp_next != illgrp) {
15896 					tmpg = tmpg->illgrp_next;
15897 					ASSERT(tmpg != NULL);
15898 				}
15899 				tmpg->illgrp_next = illgrp->illgrp_next;
15900 			}
15901 		} else {
15902 			if (illgrp == ipst->ips_illgrp_head_v4) {
15903 				ipst->ips_illgrp_head_v4 = illgrp->illgrp_next;
15904 			} else {
15905 				tmpg = ipst->ips_illgrp_head_v4;
15906 				while (tmpg->illgrp_next != illgrp) {
15907 					tmpg = tmpg->illgrp_next;
15908 					ASSERT(tmpg != NULL);
15909 				}
15910 				tmpg->illgrp_next = illgrp->illgrp_next;
15911 			}
15912 		}
15913 		mutex_destroy(&illgrp->illgrp_lock);
15914 		mi_free(illgrp);
15915 	}
15916 	rw_exit(&ipst->ips_ill_g_lock);
15917 
15918 	/*
15919 	 * Even though the ill is out of the group its not necessary
15920 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15921 	 * We will split the ipsq when phyint_groupname is set to NULL.
15922 	 */
15923 
15924 	/*
15925 	 * Send a routing sockets message if we are deleting from
15926 	 * groups with names.
15927 	 */
15928 	if (ill->ill_phyint->phyint_groupname_len != 0)
15929 		ip_rts_ifmsg(ill->ill_ipif);
15930 }
15931 
15932 /*
15933  * Re-do source address selection. This is normally called when
15934  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15935  * ipif comes up.
15936  */
15937 void
15938 ill_update_source_selection(ill_t *ill)
15939 {
15940 	ipif_t *ipif;
15941 
15942 	ASSERT(IAM_WRITER_ILL(ill));
15943 
15944 	if (ill->ill_group != NULL)
15945 		ill = ill->ill_group->illgrp_ill;
15946 
15947 	for (; ill != NULL; ill = ill->ill_group_next) {
15948 		for (ipif = ill->ill_ipif; ipif != NULL;
15949 		    ipif = ipif->ipif_next) {
15950 			if (ill->ill_isv6)
15951 				ipif_recreate_interface_routes_v6(NULL, ipif);
15952 			else
15953 				ipif_recreate_interface_routes(NULL, ipif);
15954 		}
15955 	}
15956 }
15957 
15958 /*
15959  * Insert ill in a group headed by illgrp_head. The caller can either
15960  * pass a groupname in which case we search for a group with the
15961  * same name to insert in or pass a group to insert in. This function
15962  * would only search groups with names.
15963  *
15964  * NOTE : The caller should make sure that there is at least one ipif
15965  *	  UP on this ill so that illgrp_scheduler can pick this ill
15966  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15967  *	  already sent a DL_UNBIND to the driver and we don't want to
15968  *	  send anymore packets. We don't assert for ipif_up_count
15969  *	  to be greater than zero, because ipif_up_done wants to call
15970  *	  this function before bumping up the ipif_up_count. See
15971  *	  ipif_up_done() for details.
15972  */
15973 int
15974 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15975     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15976 {
15977 	ill_group_t *illgrp;
15978 	ill_t *prev_ill;
15979 	phyint_t *phyi;
15980 	ip_stack_t	*ipst = ill->ill_ipst;
15981 
15982 	ASSERT(ill->ill_group == NULL);
15983 
15984 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15985 	mutex_enter(&ill->ill_lock);
15986 
15987 	if (groupname != NULL) {
15988 		/*
15989 		 * Look for a group with a matching groupname to insert.
15990 		 */
15991 		for (illgrp = *illgrp_head; illgrp != NULL;
15992 		    illgrp = illgrp->illgrp_next) {
15993 
15994 			ill_t *tmp_ill;
15995 
15996 			/*
15997 			 * If we have an ill_group_t in the list which has
15998 			 * no ill_t assigned then we must be in the process of
15999 			 * removing this group. We skip this as illgrp_delete()
16000 			 * will remove it from the list.
16001 			 */
16002 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
16003 				ASSERT(illgrp->illgrp_ill_count == 0);
16004 				continue;
16005 			}
16006 
16007 			ASSERT(tmp_ill->ill_phyint != NULL);
16008 			phyi = tmp_ill->ill_phyint;
16009 			/*
16010 			 * Look at groups which has names only.
16011 			 */
16012 			if (phyi->phyint_groupname_len == 0)
16013 				continue;
16014 			/*
16015 			 * Names are stored in the phyint common to both
16016 			 * IPv4 and IPv6.
16017 			 */
16018 			if (mi_strcmp(phyi->phyint_groupname,
16019 			    groupname) == 0) {
16020 				break;
16021 			}
16022 		}
16023 	} else {
16024 		/*
16025 		 * If the caller passes in a NULL "grp_to_insert", we
16026 		 * allocate one below and insert this singleton.
16027 		 */
16028 		illgrp = grp_to_insert;
16029 	}
16030 
16031 	ill->ill_group_next = NULL;
16032 
16033 	if (illgrp == NULL) {
16034 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
16035 		if (illgrp == NULL) {
16036 			return (ENOMEM);
16037 		}
16038 		illgrp->illgrp_next = *illgrp_head;
16039 		*illgrp_head = illgrp;
16040 		illgrp->illgrp_ill = ill;
16041 		illgrp->illgrp_ill_count = 1;
16042 		ill->ill_group = illgrp;
16043 		/*
16044 		 * Used in illgrp_scheduler to protect multiple threads
16045 		 * from traversing the list.
16046 		 */
16047 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
16048 	} else {
16049 		ASSERT(ill->ill_net_type ==
16050 		    illgrp->illgrp_ill->ill_net_type);
16051 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
16052 
16053 		/* Insert ill at tail of this group */
16054 		prev_ill = illgrp->illgrp_ill;
16055 		while (prev_ill->ill_group_next != NULL)
16056 			prev_ill = prev_ill->ill_group_next;
16057 		prev_ill->ill_group_next = ill;
16058 		ill->ill_group = illgrp;
16059 		illgrp->illgrp_ill_count++;
16060 		/*
16061 		 * Inherit group properties. Currently only forwarding
16062 		 * is the property we try to keep the same with all the
16063 		 * ills. When there are more, we will abstract this into
16064 		 * a function.
16065 		 */
16066 		ill->ill_flags &= ~ILLF_ROUTER;
16067 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
16068 	}
16069 	mutex_exit(&ill->ill_lock);
16070 	rw_exit(&ipst->ips_ill_g_lock);
16071 
16072 	/*
16073 	 * 1) When ipif_up_done() calls this function, ipif_up_count
16074 	 *    may be zero as it has not yet been bumped. But the ires
16075 	 *    have already been added. So, we do the nomination here
16076 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
16077 	 *    for ill_ipif_up_count != 0. Thus we don't check for
16078 	 *    ill_ipif_up_count here while nominating broadcast ires for
16079 	 *    receive.
16080 	 *
16081 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
16082 	 *    to group them properly as ire_add() has already happened
16083 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
16084 	 *    case, we need to do it here anyway.
16085 	 */
16086 	if (!ill->ill_isv6) {
16087 		ill_group_bcast_for_xmit(ill);
16088 		ill_nominate_bcast_rcv(illgrp);
16089 	}
16090 
16091 	if (!ipif_is_coming_up) {
16092 		/*
16093 		 * When ipif_up_done() calls this function, the multicast
16094 		 * groups have not been joined yet. So, there is no point in
16095 		 * nomination. ip_join_allmulti will handle groups when
16096 		 * ill_recover_multicast is called from ipif_up_done() later.
16097 		 */
16098 		(void) ill_nominate_mcast_rcv(illgrp);
16099 		/*
16100 		 * ipif_up_done calls ill_update_source_selection
16101 		 * anyway. Moreover, we don't want to re-create
16102 		 * interface routes while ipif_up_done() still has reference
16103 		 * to them. Refer to ipif_up_done() for more details.
16104 		 */
16105 		ill_update_source_selection(ill);
16106 	}
16107 
16108 	/*
16109 	 * Send a routing sockets message if we are inserting into
16110 	 * groups with names.
16111 	 */
16112 	if (groupname != NULL)
16113 		ip_rts_ifmsg(ill->ill_ipif);
16114 	return (0);
16115 }
16116 
16117 /*
16118  * Return the first phyint matching the groupname. There could
16119  * be more than one when there are ill groups.
16120  *
16121  * If 'usable' is set, then we exclude ones that are marked with any of
16122  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16123  * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo
16124  * emulation of ipmp.
16125  */
16126 phyint_t *
16127 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst)
16128 {
16129 	phyint_t *phyi;
16130 
16131 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16132 	/*
16133 	 * Group names are stored in the phyint - a common structure
16134 	 * to both IPv4 and IPv6.
16135 	 */
16136 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16137 	for (; phyi != NULL;
16138 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16139 	    phyi, AVL_AFTER)) {
16140 		if (phyi->phyint_groupname_len == 0)
16141 			continue;
16142 		/*
16143 		 * Skip the ones that should not be used since the callers
16144 		 * sometime use this for sending packets.
16145 		 */
16146 		if (usable && (phyi->phyint_flags &
16147 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)))
16148 			continue;
16149 
16150 		ASSERT(phyi->phyint_groupname != NULL);
16151 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
16152 			return (phyi);
16153 	}
16154 	return (NULL);
16155 }
16156 
16157 
16158 /*
16159  * Return the first usable phyint matching the group index. By 'usable'
16160  * we exclude ones that are marked ununsable with any of
16161  * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE).
16162  *
16163  * Used only for the ipmp/netinfo emulation of ipmp.
16164  */
16165 phyint_t *
16166 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst)
16167 {
16168 	phyint_t *phyi;
16169 
16170 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
16171 
16172 	if (!ipst->ips_ipmp_hook_emulation)
16173 		return (NULL);
16174 
16175 	/*
16176 	 * Group indicies are stored in the phyint - a common structure
16177 	 * to both IPv4 and IPv6.
16178 	 */
16179 	phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index);
16180 	for (; phyi != NULL;
16181 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16182 	    phyi, AVL_AFTER)) {
16183 		/* Ignore the ones that do not have a group */
16184 		if (phyi->phyint_groupname_len == 0)
16185 			continue;
16186 
16187 		ASSERT(phyi->phyint_group_ifindex != 0);
16188 		/*
16189 		 * Skip the ones that should not be used since the callers
16190 		 * sometime use this for sending packets.
16191 		 */
16192 		if (phyi->phyint_flags &
16193 		    (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))
16194 			continue;
16195 		if (phyi->phyint_group_ifindex == group_ifindex)
16196 			return (phyi);
16197 	}
16198 	return (NULL);
16199 }
16200 
16201 
16202 /*
16203  * MT notes on creation and deletion of IPMP groups
16204  *
16205  * Creation and deletion of IPMP groups introduce the need to merge or
16206  * split the associated serialization objects i.e the ipsq's. Normally all
16207  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
16208  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
16209  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
16210  * is a need to change the <ill-ipsq> association and we have to operate on both
16211  * the source and destination IPMP groups. For eg. attempting to set the
16212  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
16213  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
16214  * source or destination IPMP group are mapped to a single ipsq for executing
16215  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
16216  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
16217  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
16218  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
16219  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
16220  * ipsq has to be examined for redoing the <ill-ipsq> associations.
16221  *
16222  * In the above example the ioctl handling code locates the current ipsq of hme0
16223  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
16224  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
16225  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
16226  * the destination ipsq. If the destination ipsq is not busy, it also enters
16227  * the destination ipsq exclusively. Now the actual groupname setting operation
16228  * can proceed. If the destination ipsq is busy, the operation is enqueued
16229  * on the destination (merged) ipsq and will be handled in the unwind from
16230  * ipsq_exit.
16231  *
16232  * To prevent other threads accessing the ill while the group name change is
16233  * in progres, we bring down the ipifs which also removes the ill from the
16234  * group. The group is changed in phyint and when the first ipif on the ill
16235  * is brought up, the ill is inserted into the right IPMP group by
16236  * illgrp_insert.
16237  */
16238 /* ARGSUSED */
16239 int
16240 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16241     ip_ioctl_cmd_t *ipip, void *ifreq)
16242 {
16243 	int i;
16244 	char *tmp;
16245 	int namelen;
16246 	ill_t *ill = ipif->ipif_ill;
16247 	ill_t *ill_v4, *ill_v6;
16248 	int err = 0;
16249 	phyint_t *phyi;
16250 	phyint_t *phyi_tmp;
16251 	struct lifreq *lifr;
16252 	mblk_t	*mp1;
16253 	char *groupname;
16254 	ipsq_t *ipsq;
16255 	ip_stack_t	*ipst = ill->ill_ipst;
16256 
16257 	ASSERT(IAM_WRITER_IPIF(ipif));
16258 
16259 	/* Existance verified in ip_wput_nondata */
16260 	mp1 = mp->b_cont->b_cont;
16261 	lifr = (struct lifreq *)mp1->b_rptr;
16262 	groupname = lifr->lifr_groupname;
16263 
16264 	if (ipif->ipif_id != 0)
16265 		return (EINVAL);
16266 
16267 	phyi = ill->ill_phyint;
16268 	ASSERT(phyi != NULL);
16269 
16270 	if (phyi->phyint_flags & PHYI_VIRTUAL)
16271 		return (EINVAL);
16272 
16273 	tmp = groupname;
16274 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
16275 		;
16276 
16277 	if (i == LIFNAMSIZ) {
16278 		/* no null termination */
16279 		return (EINVAL);
16280 	}
16281 
16282 	/*
16283 	 * Calculate the namelen exclusive of the null
16284 	 * termination character.
16285 	 */
16286 	namelen = tmp - groupname;
16287 
16288 	ill_v4 = phyi->phyint_illv4;
16289 	ill_v6 = phyi->phyint_illv6;
16290 
16291 	/*
16292 	 * ILL cannot be part of a usesrc group and and IPMP group at the
16293 	 * same time. No need to grab the ill_g_usesrc_lock here, see
16294 	 * synchronization notes in ip.c
16295 	 */
16296 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
16297 		return (EINVAL);
16298 	}
16299 
16300 	/*
16301 	 * mark the ill as changing.
16302 	 * this should queue all new requests on the syncq.
16303 	 */
16304 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
16305 
16306 	if (ill_v4 != NULL)
16307 		ill_v4->ill_state_flags |= ILL_CHANGING;
16308 	if (ill_v6 != NULL)
16309 		ill_v6->ill_state_flags |= ILL_CHANGING;
16310 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16311 
16312 	if (namelen == 0) {
16313 		/*
16314 		 * Null string means remove this interface from the
16315 		 * existing group.
16316 		 */
16317 		if (phyi->phyint_groupname_len == 0) {
16318 			/*
16319 			 * Never was in a group.
16320 			 */
16321 			err = 0;
16322 			goto done;
16323 		}
16324 
16325 		/*
16326 		 * IPv4 or IPv6 may be temporarily out of the group when all
16327 		 * the ipifs are down. Thus, we need to check for ill_group to
16328 		 * be non-NULL.
16329 		 */
16330 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
16331 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16332 			mutex_enter(&ill_v4->ill_lock);
16333 			if (!ill_is_quiescent(ill_v4)) {
16334 				/*
16335 				 * ipsq_pending_mp_add will not fail since
16336 				 * connp is NULL
16337 				 */
16338 				(void) ipsq_pending_mp_add(NULL,
16339 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16340 				mutex_exit(&ill_v4->ill_lock);
16341 				err = EINPROGRESS;
16342 				goto done;
16343 			}
16344 			mutex_exit(&ill_v4->ill_lock);
16345 		}
16346 
16347 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
16348 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16349 			mutex_enter(&ill_v6->ill_lock);
16350 			if (!ill_is_quiescent(ill_v6)) {
16351 				(void) ipsq_pending_mp_add(NULL,
16352 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16353 				mutex_exit(&ill_v6->ill_lock);
16354 				err = EINPROGRESS;
16355 				goto done;
16356 			}
16357 			mutex_exit(&ill_v6->ill_lock);
16358 		}
16359 
16360 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16361 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16362 		mutex_enter(&phyi->phyint_lock);
16363 		ASSERT(phyi->phyint_groupname != NULL);
16364 		mi_free(phyi->phyint_groupname);
16365 		phyi->phyint_groupname = NULL;
16366 		phyi->phyint_groupname_len = 0;
16367 
16368 		/* Restore the ifindex used to be the per interface one */
16369 		phyi->phyint_group_ifindex = 0;
16370 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16371 		mutex_exit(&phyi->phyint_lock);
16372 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16373 		rw_exit(&ipst->ips_ill_g_lock);
16374 		err = ill_up_ipifs(ill, q, mp);
16375 
16376 		/*
16377 		 * set the split flag so that the ipsq can be split
16378 		 */
16379 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16380 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
16381 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16382 
16383 	} else {
16384 		if (phyi->phyint_groupname_len != 0) {
16385 			ASSERT(phyi->phyint_groupname != NULL);
16386 			/* Are we inserting in the same group ? */
16387 			if (mi_strcmp(groupname,
16388 			    phyi->phyint_groupname) == 0) {
16389 				err = 0;
16390 				goto done;
16391 			}
16392 		}
16393 
16394 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16395 		/*
16396 		 * Merge ipsq for the group's.
16397 		 * This check is here as multiple groups/ills might be
16398 		 * sharing the same ipsq.
16399 		 * If we have to merege than the operation is restarted
16400 		 * on the new ipsq.
16401 		 */
16402 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst);
16403 		if (phyi->phyint_ipsq != ipsq) {
16404 			rw_exit(&ipst->ips_ill_g_lock);
16405 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
16406 			goto done;
16407 		}
16408 		/*
16409 		 * Running exclusive on new ipsq.
16410 		 */
16411 
16412 		ASSERT(ipsq != NULL);
16413 		ASSERT(ipsq->ipsq_writer == curthread);
16414 
16415 		/*
16416 		 * Check whether the ill_type and ill_net_type matches before
16417 		 * we allocate any memory so that the cleanup is easier.
16418 		 *
16419 		 * We can't group dissimilar ones as we can't load spread
16420 		 * packets across the group because of potential link-level
16421 		 * header differences.
16422 		 */
16423 		phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst);
16424 		if (phyi_tmp != NULL) {
16425 			if ((ill_v4 != NULL &&
16426 			    phyi_tmp->phyint_illv4 != NULL) &&
16427 			    ((ill_v4->ill_net_type !=
16428 			    phyi_tmp->phyint_illv4->ill_net_type) ||
16429 			    (ill_v4->ill_type !=
16430 			    phyi_tmp->phyint_illv4->ill_type))) {
16431 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16432 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16433 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16434 				rw_exit(&ipst->ips_ill_g_lock);
16435 				return (EINVAL);
16436 			}
16437 			if ((ill_v6 != NULL &&
16438 			    phyi_tmp->phyint_illv6 != NULL) &&
16439 			    ((ill_v6->ill_net_type !=
16440 			    phyi_tmp->phyint_illv6->ill_net_type) ||
16441 			    (ill_v6->ill_type !=
16442 			    phyi_tmp->phyint_illv6->ill_type))) {
16443 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
16444 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
16445 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
16446 				rw_exit(&ipst->ips_ill_g_lock);
16447 				return (EINVAL);
16448 			}
16449 		}
16450 
16451 		rw_exit(&ipst->ips_ill_g_lock);
16452 
16453 		/*
16454 		 * bring down all v4 ipifs.
16455 		 */
16456 		if (ill_v4 != NULL) {
16457 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
16458 		}
16459 
16460 		/*
16461 		 * bring down all v6 ipifs.
16462 		 */
16463 		if (ill_v6 != NULL) {
16464 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
16465 		}
16466 
16467 		/*
16468 		 * make sure all ipifs are down and there are no active
16469 		 * references. Call to ipsq_pending_mp_add will not fail
16470 		 * since connp is NULL.
16471 		 */
16472 		if (ill_v4 != NULL) {
16473 			mutex_enter(&ill_v4->ill_lock);
16474 			if (!ill_is_quiescent(ill_v4)) {
16475 				(void) ipsq_pending_mp_add(NULL,
16476 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
16477 				mutex_exit(&ill_v4->ill_lock);
16478 				err = EINPROGRESS;
16479 				goto done;
16480 			}
16481 			mutex_exit(&ill_v4->ill_lock);
16482 		}
16483 
16484 		if (ill_v6 != NULL) {
16485 			mutex_enter(&ill_v6->ill_lock);
16486 			if (!ill_is_quiescent(ill_v6)) {
16487 				(void) ipsq_pending_mp_add(NULL,
16488 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
16489 				mutex_exit(&ill_v6->ill_lock);
16490 				err = EINPROGRESS;
16491 				goto done;
16492 			}
16493 			mutex_exit(&ill_v6->ill_lock);
16494 		}
16495 
16496 		/*
16497 		 * allocate including space for null terminator
16498 		 * before we insert.
16499 		 */
16500 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
16501 		if (tmp == NULL)
16502 			return (ENOMEM);
16503 
16504 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16505 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16506 		mutex_enter(&phyi->phyint_lock);
16507 		if (phyi->phyint_groupname_len != 0) {
16508 			ASSERT(phyi->phyint_groupname != NULL);
16509 			mi_free(phyi->phyint_groupname);
16510 		}
16511 
16512 		/*
16513 		 * setup the new group name.
16514 		 */
16515 		phyi->phyint_groupname = tmp;
16516 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
16517 		phyi->phyint_groupname_len = namelen + 1;
16518 
16519 		if (ipst->ips_ipmp_hook_emulation) {
16520 			/*
16521 			 * If the group already exists we use the existing
16522 			 * group_ifindex, otherwise we pick a new index here.
16523 			 */
16524 			if (phyi_tmp != NULL) {
16525 				phyi->phyint_group_ifindex =
16526 				    phyi_tmp->phyint_group_ifindex;
16527 			} else {
16528 				/* XXX We need a recovery strategy here. */
16529 				if (!ip_assign_ifindex(
16530 				    &phyi->phyint_group_ifindex, ipst))
16531 					cmn_err(CE_PANIC,
16532 					    "ip_assign_ifindex() failed");
16533 			}
16534 		}
16535 		/*
16536 		 * Select whether the netinfo and hook use the per-interface
16537 		 * or per-group ifindex.
16538 		 */
16539 		if (ipst->ips_ipmp_hook_emulation)
16540 			phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex;
16541 		else
16542 			phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
16543 
16544 		if (ipst->ips_ipmp_hook_emulation &&
16545 		    phyi_tmp != NULL) {
16546 			/* First phyint in group - group PLUMB event */
16547 			ill_nic_info_plumb(ill, B_TRUE);
16548 		}
16549 		mutex_exit(&phyi->phyint_lock);
16550 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16551 		rw_exit(&ipst->ips_ill_g_lock);
16552 
16553 		err = ill_up_ipifs(ill, q, mp);
16554 	}
16555 
16556 done:
16557 	/*
16558 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
16559 	 */
16560 	if (err != EINPROGRESS) {
16561 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
16562 		if (ill_v4 != NULL)
16563 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
16564 		if (ill_v6 != NULL)
16565 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
16566 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
16567 	}
16568 	return (err);
16569 }
16570 
16571 /* ARGSUSED */
16572 int
16573 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
16574     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
16575 {
16576 	ill_t *ill;
16577 	phyint_t *phyi;
16578 	struct lifreq *lifr;
16579 	mblk_t	*mp1;
16580 
16581 	/* Existence verified in ip_wput_nondata */
16582 	mp1 = mp->b_cont->b_cont;
16583 	lifr = (struct lifreq *)mp1->b_rptr;
16584 	ill = ipif->ipif_ill;
16585 	phyi = ill->ill_phyint;
16586 
16587 	lifr->lifr_groupname[0] = '\0';
16588 	/*
16589 	 * ill_group may be null if all the interfaces
16590 	 * are down. But still, the phyint should always
16591 	 * hold the name.
16592 	 */
16593 	if (phyi->phyint_groupname_len != 0) {
16594 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
16595 		    phyi->phyint_groupname_len);
16596 	}
16597 
16598 	return (0);
16599 }
16600 
16601 
16602 typedef struct conn_move_s {
16603 	ill_t	*cm_from_ill;
16604 	ill_t	*cm_to_ill;
16605 	int	cm_ifindex;
16606 } conn_move_t;
16607 
16608 /*
16609  * ipcl_walk function for moving conn_multicast_ill for a given ill.
16610  */
16611 static void
16612 conn_move(conn_t *connp, caddr_t arg)
16613 {
16614 	conn_move_t *connm;
16615 	int ifindex;
16616 	int i;
16617 	ill_t *from_ill;
16618 	ill_t *to_ill;
16619 	ilg_t *ilg;
16620 	ilm_t *ret_ilm;
16621 
16622 	connm = (conn_move_t *)arg;
16623 	ifindex = connm->cm_ifindex;
16624 	from_ill = connm->cm_from_ill;
16625 	to_ill = connm->cm_to_ill;
16626 
16627 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
16628 
16629 	/* All multicast fields protected by conn_lock */
16630 	mutex_enter(&connp->conn_lock);
16631 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
16632 	if ((connp->conn_outgoing_ill == from_ill) &&
16633 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
16634 		connp->conn_outgoing_ill = to_ill;
16635 		connp->conn_incoming_ill = to_ill;
16636 	}
16637 
16638 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
16639 
16640 	if ((connp->conn_multicast_ill == from_ill) &&
16641 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
16642 		connp->conn_multicast_ill = connm->cm_to_ill;
16643 	}
16644 
16645 	/*
16646 	 * Change the ilg_ill to point to the new one. This assumes
16647 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
16648 	 * has been told to receive packets on this interface.
16649 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
16650 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
16651 	 * some ilms may not have moved. We check to see whether
16652 	 * the ilms have moved to to_ill. We can't check on from_ill
16653 	 * as in the process of moving, we could have split an ilm
16654 	 * in to two - which has the same orig_ifindex and v6group.
16655 	 *
16656 	 * For IPv4, ilg_ipif moves implicitly. The code below really
16657 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
16658 	 */
16659 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
16660 		ilg = &connp->conn_ilg[i];
16661 		if ((ilg->ilg_ill == from_ill) &&
16662 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
16663 			/* ifindex != 0 indicates failback */
16664 			if (ifindex != 0) {
16665 				connp->conn_ilg[i].ilg_ill = to_ill;
16666 				continue;
16667 			}
16668 
16669 			mutex_enter(&to_ill->ill_lock);
16670 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
16671 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
16672 			    connp->conn_zoneid);
16673 			mutex_exit(&to_ill->ill_lock);
16674 
16675 			if (ret_ilm != NULL)
16676 				connp->conn_ilg[i].ilg_ill = to_ill;
16677 		}
16678 	}
16679 	mutex_exit(&connp->conn_lock);
16680 }
16681 
16682 static void
16683 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
16684 {
16685 	conn_move_t connm;
16686 	ip_stack_t	*ipst = from_ill->ill_ipst;
16687 
16688 	connm.cm_from_ill = from_ill;
16689 	connm.cm_to_ill = to_ill;
16690 	connm.cm_ifindex = ifindex;
16691 
16692 	ipcl_walk(conn_move, (caddr_t)&connm, ipst);
16693 }
16694 
16695 /*
16696  * ilm has been moved from from_ill to to_ill.
16697  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
16698  * appropriately.
16699  *
16700  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
16701  *	  the code there de-references ipif_ill to get the ill to
16702  *	  send multicast requests. It does not work as ipif is on its
16703  *	  move and already moved when this function is called.
16704  *	  Thus, we need to use from_ill and to_ill send down multicast
16705  *	  requests.
16706  */
16707 static void
16708 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
16709 {
16710 	ipif_t *ipif;
16711 	ilm_t *ilm;
16712 
16713 	/*
16714 	 * See whether we need to send down DL_ENABMULTI_REQ on
16715 	 * to_ill as ilm has just been added.
16716 	 */
16717 	ASSERT(IAM_WRITER_ILL(to_ill));
16718 	ASSERT(IAM_WRITER_ILL(from_ill));
16719 
16720 	ILM_WALKER_HOLD(to_ill);
16721 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
16722 
16723 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
16724 			continue;
16725 		/*
16726 		 * no locks held, ill/ipif cannot dissappear as long
16727 		 * as we are writer.
16728 		 */
16729 		ipif = to_ill->ill_ipif;
16730 		/*
16731 		 * No need to hold any lock as we are the writer and this
16732 		 * can only be changed by a writer.
16733 		 */
16734 		ilm->ilm_is_new = B_FALSE;
16735 
16736 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
16737 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16738 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
16739 			    "resolver\n"));
16740 			continue;		/* Must be IRE_IF_NORESOLVER */
16741 		}
16742 
16743 
16744 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16745 			ip1dbg(("ilm_send_multicast_reqs: "
16746 			    "to_ill MULTI_BCAST\n"));
16747 			goto from;
16748 		}
16749 
16750 		if (to_ill->ill_isv6)
16751 			mld_joingroup(ilm);
16752 		else
16753 			igmp_joingroup(ilm);
16754 
16755 		if (to_ill->ill_ipif_up_count == 0) {
16756 			/*
16757 			 * Nobody there. All multicast addresses will be
16758 			 * re-joined when we get the DL_BIND_ACK bringing the
16759 			 * interface up.
16760 			 */
16761 			ilm->ilm_notify_driver = B_FALSE;
16762 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
16763 			goto from;
16764 		}
16765 
16766 		/*
16767 		 * For allmulti address, we want to join on only one interface.
16768 		 * Checking for ilm_numentries_v6 is not correct as you may
16769 		 * find an ilm with zero address on to_ill, but we may not
16770 		 * have nominated to_ill for receiving. Thus, if we have
16771 		 * nominated from_ill (ill_join_allmulti is set), nominate
16772 		 * only if to_ill is not already nominated (to_ill normally
16773 		 * should not have been nominated if "from_ill" has already
16774 		 * been nominated. As we don't prevent failovers from happening
16775 		 * across groups, we don't assert).
16776 		 */
16777 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16778 			/*
16779 			 * There is no need to hold ill locks as we are
16780 			 * writer on both ills and when ill_join_allmulti
16781 			 * is changed the thread is always a writer.
16782 			 */
16783 			if (from_ill->ill_join_allmulti &&
16784 			    !to_ill->ill_join_allmulti) {
16785 				(void) ip_join_allmulti(to_ill->ill_ipif);
16786 			}
16787 		} else if (ilm->ilm_notify_driver) {
16788 
16789 			/*
16790 			 * This is a newly moved ilm so we need to tell the
16791 			 * driver about the new group. There can be more than
16792 			 * one ilm's for the same group in the list each with a
16793 			 * different orig_ifindex. We have to inform the driver
16794 			 * once. In ilm_move_v[4,6] we only set the flag
16795 			 * ilm_notify_driver for the first ilm.
16796 			 */
16797 
16798 			(void) ip_ll_send_enabmulti_req(to_ill,
16799 			    &ilm->ilm_v6addr);
16800 		}
16801 
16802 		ilm->ilm_notify_driver = B_FALSE;
16803 
16804 		/*
16805 		 * See whether we need to send down DL_DISABMULTI_REQ on
16806 		 * from_ill as ilm has just been removed.
16807 		 */
16808 from:
16809 		ipif = from_ill->ill_ipif;
16810 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
16811 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
16812 			ip1dbg(("ilm_send_multicast_reqs: "
16813 			    "from_ill not resolver\n"));
16814 			continue;		/* Must be IRE_IF_NORESOLVER */
16815 		}
16816 
16817 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
16818 			ip1dbg(("ilm_send_multicast_reqs: "
16819 			    "from_ill MULTI_BCAST\n"));
16820 			continue;
16821 		}
16822 
16823 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
16824 			if (from_ill->ill_join_allmulti)
16825 				(void) ip_leave_allmulti(from_ill->ill_ipif);
16826 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
16827 			(void) ip_ll_send_disabmulti_req(from_ill,
16828 			    &ilm->ilm_v6addr);
16829 		}
16830 	}
16831 	ILM_WALKER_RELE(to_ill);
16832 }
16833 
16834 /*
16835  * This function is called when all multicast memberships needs
16836  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
16837  * called only once unlike the IPv4 counterpart where it is called after
16838  * every logical interface is moved. The reason is due to multicast
16839  * memberships are joined using an interface address in IPv4 while in
16840  * IPv6, interface index is used.
16841  */
16842 static void
16843 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
16844 {
16845 	ilm_t	*ilm;
16846 	ilm_t	*ilm_next;
16847 	ilm_t	*new_ilm;
16848 	ilm_t	**ilmp;
16849 	int	count;
16850 	char buf[INET6_ADDRSTRLEN];
16851 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
16852 	ip_stack_t	*ipst = from_ill->ill_ipst;
16853 
16854 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16855 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16856 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16857 
16858 	if (ifindex == 0) {
16859 		/*
16860 		 * Form the solicited node mcast address which is used later.
16861 		 */
16862 		ipif_t *ipif;
16863 
16864 		ipif = from_ill->ill_ipif;
16865 		ASSERT(ipif->ipif_id == 0);
16866 
16867 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
16868 	}
16869 
16870 	ilmp = &from_ill->ill_ilm;
16871 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16872 		ilm_next = ilm->ilm_next;
16873 
16874 		if (ilm->ilm_flags & ILM_DELETED) {
16875 			ilmp = &ilm->ilm_next;
16876 			continue;
16877 		}
16878 
16879 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
16880 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
16881 		ASSERT(ilm->ilm_orig_ifindex != 0);
16882 		if (ilm->ilm_orig_ifindex == ifindex) {
16883 			/*
16884 			 * We are failing back multicast memberships.
16885 			 * If the same ilm exists in to_ill, it means somebody
16886 			 * has joined the same group there e.g. ff02::1
16887 			 * is joined within the kernel when the interfaces
16888 			 * came UP.
16889 			 */
16890 			ASSERT(ilm->ilm_ipif == NULL);
16891 			if (new_ilm != NULL) {
16892 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16893 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16894 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16895 					new_ilm->ilm_is_new = B_TRUE;
16896 				}
16897 			} else {
16898 				/*
16899 				 * check if we can just move the ilm
16900 				 */
16901 				if (from_ill->ill_ilm_walker_cnt != 0) {
16902 					/*
16903 					 * We have walkers we cannot move
16904 					 * the ilm, so allocate a new ilm,
16905 					 * this (old) ilm will be marked
16906 					 * ILM_DELETED at the end of the loop
16907 					 * and will be freed when the
16908 					 * last walker exits.
16909 					 */
16910 					new_ilm = (ilm_t *)mi_zalloc
16911 					    (sizeof (ilm_t));
16912 					if (new_ilm == NULL) {
16913 						ip0dbg(("ilm_move_v6: "
16914 						    "FAILBACK of IPv6"
16915 						    " multicast address %s : "
16916 						    "from %s to"
16917 						    " %s failed : ENOMEM \n",
16918 						    inet_ntop(AF_INET6,
16919 						    &ilm->ilm_v6addr, buf,
16920 						    sizeof (buf)),
16921 						    from_ill->ill_name,
16922 						    to_ill->ill_name));
16923 
16924 							ilmp = &ilm->ilm_next;
16925 							continue;
16926 					}
16927 					*new_ilm = *ilm;
16928 					/*
16929 					 * we don't want new_ilm linked to
16930 					 * ilm's filter list.
16931 					 */
16932 					new_ilm->ilm_filter = NULL;
16933 				} else {
16934 					/*
16935 					 * No walkers we can move the ilm.
16936 					 * lets take it out of the list.
16937 					 */
16938 					*ilmp = ilm->ilm_next;
16939 					ilm->ilm_next = NULL;
16940 					DTRACE_PROBE3(ill__decr__cnt,
16941 					    (ill_t *), from_ill,
16942 					    (char *), "ilm", (void *), ilm);
16943 					ASSERT(from_ill->ill_ilm_cnt > 0);
16944 					from_ill->ill_ilm_cnt--;
16945 
16946 					new_ilm = ilm;
16947 				}
16948 
16949 				/*
16950 				 * if this is the first ilm for the group
16951 				 * set ilm_notify_driver so that we notify the
16952 				 * driver in ilm_send_multicast_reqs.
16953 				 */
16954 				if (ilm_lookup_ill_v6(to_ill,
16955 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16956 					new_ilm->ilm_notify_driver = B_TRUE;
16957 
16958 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
16959 				    (char *), "ilm", (void *), new_ilm);
16960 				new_ilm->ilm_ill = to_ill;
16961 				to_ill->ill_ilm_cnt++;
16962 
16963 				/* Add to the to_ill's list */
16964 				new_ilm->ilm_next = to_ill->ill_ilm;
16965 				to_ill->ill_ilm = new_ilm;
16966 				/*
16967 				 * set the flag so that mld_joingroup is
16968 				 * called in ilm_send_multicast_reqs().
16969 				 */
16970 				new_ilm->ilm_is_new = B_TRUE;
16971 			}
16972 			goto bottom;
16973 		} else if (ifindex != 0) {
16974 			/*
16975 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16976 			 * has not matched above, look at the next ilm.
16977 			 */
16978 			ilmp = &ilm->ilm_next;
16979 			continue;
16980 		}
16981 		/*
16982 		 * If we are here, it means ifindex is 0. Failover
16983 		 * everything.
16984 		 *
16985 		 * We need to handle solicited node mcast address
16986 		 * and all_nodes mcast address differently as they
16987 		 * are joined witin the kenrel (ipif_multicast_up)
16988 		 * and potentially from the userland. We are called
16989 		 * after the ipifs of from_ill has been moved.
16990 		 * If we still find ilms on ill with solicited node
16991 		 * mcast address or all_nodes mcast address, it must
16992 		 * belong to the UP interface that has not moved e.g.
16993 		 * ipif_id 0 with the link local prefix does not move.
16994 		 * We join this on the new ill accounting for all the
16995 		 * userland memberships so that applications don't
16996 		 * see any failure.
16997 		 *
16998 		 * We need to make sure that we account only for the
16999 		 * solicited node and all node multicast addresses
17000 		 * that was brought UP on these. In the case of
17001 		 * a failover from A to B, we might have ilms belonging
17002 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
17003 		 * for the membership from the userland. If we are failing
17004 		 * over from B to C now, we will find the ones belonging
17005 		 * to A on B. These don't account for the ill_ipif_up_count.
17006 		 * They just move from B to C. The check below on
17007 		 * ilm_orig_ifindex ensures that.
17008 		 */
17009 		if ((ilm->ilm_orig_ifindex ==
17010 		    from_ill->ill_phyint->phyint_ifindex) &&
17011 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
17012 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
17013 		    &ilm->ilm_v6addr))) {
17014 			ASSERT(ilm->ilm_refcnt > 0);
17015 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
17016 			/*
17017 			 * For indentation reasons, we are not using a
17018 			 * "else" here.
17019 			 */
17020 			if (count == 0) {
17021 				ilmp = &ilm->ilm_next;
17022 				continue;
17023 			}
17024 			ilm->ilm_refcnt -= count;
17025 			if (new_ilm != NULL) {
17026 				/*
17027 				 * Can find one with the same
17028 				 * ilm_orig_ifindex, if we are failing
17029 				 * over to a STANDBY. This happens
17030 				 * when somebody wants to join a group
17031 				 * on a STANDBY interface and we
17032 				 * internally join on a different one.
17033 				 * If we had joined on from_ill then, a
17034 				 * failover now will find a new ilm
17035 				 * with this index.
17036 				 */
17037 				ip1dbg(("ilm_move_v6: FAILOVER, found"
17038 				    " new ilm on %s, group address %s\n",
17039 				    to_ill->ill_name,
17040 				    inet_ntop(AF_INET6,
17041 				    &ilm->ilm_v6addr, buf,
17042 				    sizeof (buf))));
17043 				new_ilm->ilm_refcnt += count;
17044 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17045 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17046 					new_ilm->ilm_is_new = B_TRUE;
17047 				}
17048 			} else {
17049 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17050 				if (new_ilm == NULL) {
17051 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
17052 					    " multicast address %s : from %s to"
17053 					    " %s failed : ENOMEM \n",
17054 					    inet_ntop(AF_INET6,
17055 					    &ilm->ilm_v6addr, buf,
17056 					    sizeof (buf)), from_ill->ill_name,
17057 					    to_ill->ill_name));
17058 					ilmp = &ilm->ilm_next;
17059 					continue;
17060 				}
17061 				*new_ilm = *ilm;
17062 				new_ilm->ilm_filter = NULL;
17063 				new_ilm->ilm_refcnt = count;
17064 				new_ilm->ilm_timer = INFINITY;
17065 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
17066 				new_ilm->ilm_is_new = B_TRUE;
17067 				/*
17068 				 * If the to_ill has not joined this
17069 				 * group we need to tell the driver in
17070 				 * ill_send_multicast_reqs.
17071 				 */
17072 				if (ilm_lookup_ill_v6(to_ill,
17073 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17074 					new_ilm->ilm_notify_driver = B_TRUE;
17075 
17076 				new_ilm->ilm_ill = to_ill;
17077 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17078 				    (char *), "ilm", (void *), new_ilm);
17079 				to_ill->ill_ilm_cnt++;
17080 
17081 				/* Add to the to_ill's list */
17082 				new_ilm->ilm_next = to_ill->ill_ilm;
17083 				to_ill->ill_ilm = new_ilm;
17084 				ASSERT(new_ilm->ilm_ipif == NULL);
17085 			}
17086 			if (ilm->ilm_refcnt == 0) {
17087 				goto bottom;
17088 			} else {
17089 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17090 				CLEAR_SLIST(new_ilm->ilm_filter);
17091 				ilmp = &ilm->ilm_next;
17092 			}
17093 			continue;
17094 		} else {
17095 			/*
17096 			 * ifindex = 0 means, move everything pointing at
17097 			 * from_ill. We are doing this becuase ill has
17098 			 * either FAILED or became INACTIVE.
17099 			 *
17100 			 * As we would like to move things later back to
17101 			 * from_ill, we want to retain the identity of this
17102 			 * ilm. Thus, we don't blindly increment the reference
17103 			 * count on the ilms matching the address alone. We
17104 			 * need to match on the ilm_orig_index also. new_ilm
17105 			 * was obtained by matching ilm_orig_index also.
17106 			 */
17107 			if (new_ilm != NULL) {
17108 				/*
17109 				 * This is possible only if a previous restore
17110 				 * was incomplete i.e restore to
17111 				 * ilm_orig_ifindex left some ilms because
17112 				 * of some failures. Thus when we are failing
17113 				 * again, we might find our old friends there.
17114 				 */
17115 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
17116 				    " on %s, group address %s\n",
17117 				    to_ill->ill_name,
17118 				    inet_ntop(AF_INET6,
17119 				    &ilm->ilm_v6addr, buf,
17120 				    sizeof (buf))));
17121 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17122 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
17123 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
17124 					new_ilm->ilm_is_new = B_TRUE;
17125 				}
17126 			} else {
17127 				if (from_ill->ill_ilm_walker_cnt != 0) {
17128 					new_ilm = (ilm_t *)
17129 					    mi_zalloc(sizeof (ilm_t));
17130 					if (new_ilm == NULL) {
17131 						ip0dbg(("ilm_move_v6: "
17132 						    "FAILOVER of IPv6"
17133 						    " multicast address %s : "
17134 						    "from %s to"
17135 						    " %s failed : ENOMEM \n",
17136 						    inet_ntop(AF_INET6,
17137 						    &ilm->ilm_v6addr, buf,
17138 						    sizeof (buf)),
17139 						    from_ill->ill_name,
17140 						    to_ill->ill_name));
17141 
17142 							ilmp = &ilm->ilm_next;
17143 							continue;
17144 					}
17145 					*new_ilm = *ilm;
17146 					new_ilm->ilm_filter = NULL;
17147 				} else {
17148 					*ilmp = ilm->ilm_next;
17149 					DTRACE_PROBE3(ill__decr__cnt,
17150 					    (ill_t *), from_ill,
17151 					    (char *), "ilm", (void *), ilm);
17152 					ASSERT(from_ill->ill_ilm_cnt > 0);
17153 					from_ill->ill_ilm_cnt--;
17154 
17155 					new_ilm = ilm;
17156 				}
17157 				/*
17158 				 * If the to_ill has not joined this
17159 				 * group we need to tell the driver in
17160 				 * ill_send_multicast_reqs.
17161 				 */
17162 				if (ilm_lookup_ill_v6(to_ill,
17163 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
17164 					new_ilm->ilm_notify_driver = B_TRUE;
17165 
17166 				/* Add to the to_ill's list */
17167 				new_ilm->ilm_next = to_ill->ill_ilm;
17168 				to_ill->ill_ilm = new_ilm;
17169 				ASSERT(ilm->ilm_ipif == NULL);
17170 				new_ilm->ilm_ill = to_ill;
17171 				DTRACE_PROBE3(ill__incr__cnt, (ill_t *), to_ill,
17172 				    (char *), "ilm", (void *), new_ilm);
17173 				to_ill->ill_ilm_cnt++;
17174 				new_ilm->ilm_is_new = B_TRUE;
17175 			}
17176 
17177 		}
17178 
17179 bottom:
17180 		/*
17181 		 * Revert multicast filter state to (EXCLUDE, NULL).
17182 		 * new_ilm->ilm_is_new should already be set if needed.
17183 		 */
17184 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17185 		CLEAR_SLIST(new_ilm->ilm_filter);
17186 		/*
17187 		 * We allocated/got a new ilm, free the old one.
17188 		 */
17189 		if (new_ilm != ilm) {
17190 			if (from_ill->ill_ilm_walker_cnt == 0) {
17191 				*ilmp = ilm->ilm_next;
17192 
17193 				ASSERT(ilm->ilm_ipif == NULL); /* ipv6 */
17194 				DTRACE_PROBE3(ill__decr__cnt, (ill_t *),
17195 				    from_ill, (char *), "ilm", (void *), ilm);
17196 				ASSERT(from_ill->ill_ilm_cnt > 0);
17197 				from_ill->ill_ilm_cnt--;
17198 
17199 				ilm_inactive(ilm); /* frees this ilm */
17200 
17201 			} else {
17202 				ilm->ilm_flags |= ILM_DELETED;
17203 				from_ill->ill_ilm_cleanup_reqd = 1;
17204 				ilmp = &ilm->ilm_next;
17205 			}
17206 		}
17207 	}
17208 }
17209 
17210 /*
17211  * Move all the multicast memberships to to_ill. Called when
17212  * an ipif moves from "from_ill" to "to_ill". This function is slightly
17213  * different from IPv6 counterpart as multicast memberships are associated
17214  * with ills in IPv6. This function is called after every ipif is moved
17215  * unlike IPv6, where it is moved only once.
17216  */
17217 static void
17218 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
17219 {
17220 	ilm_t	*ilm;
17221 	ilm_t	*ilm_next;
17222 	ilm_t	*new_ilm;
17223 	ilm_t	**ilmp;
17224 	ip_stack_t	*ipst = from_ill->ill_ipst;
17225 
17226 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17227 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17228 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17229 
17230 	ilmp = &from_ill->ill_ilm;
17231 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
17232 		ilm_next = ilm->ilm_next;
17233 
17234 		if (ilm->ilm_flags & ILM_DELETED) {
17235 			ilmp = &ilm->ilm_next;
17236 			continue;
17237 		}
17238 
17239 		ASSERT(ilm->ilm_ipif != NULL);
17240 
17241 		if (ilm->ilm_ipif != ipif) {
17242 			ilmp = &ilm->ilm_next;
17243 			continue;
17244 		}
17245 
17246 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
17247 		    htonl(INADDR_ALLHOSTS_GROUP)) {
17248 			new_ilm = ilm_lookup_ipif(ipif,
17249 			    V4_PART_OF_V6(ilm->ilm_v6addr));
17250 			if (new_ilm != NULL) {
17251 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
17252 				/*
17253 				 * We still need to deal with the from_ill.
17254 				 */
17255 				new_ilm->ilm_is_new = B_TRUE;
17256 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17257 				CLEAR_SLIST(new_ilm->ilm_filter);
17258 				ASSERT(ilm->ilm_ipif == ipif);
17259 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17260 				if (from_ill->ill_ilm_walker_cnt == 0) {
17261 					DTRACE_PROBE3(ill__decr__cnt,
17262 					    (ill_t *), from_ill,
17263 					    (char *), "ilm", (void *), ilm);
17264 					ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17265 				}
17266 				goto delete_ilm;
17267 			}
17268 			/*
17269 			 * If we could not find one e.g. ipif is
17270 			 * still down on to_ill, we add this ilm
17271 			 * on ill_new to preserve the reference
17272 			 * count.
17273 			 */
17274 		}
17275 		/*
17276 		 * When ipifs move, ilms always move with it
17277 		 * to the NEW ill. Thus we should never be
17278 		 * able to find ilm till we really move it here.
17279 		 */
17280 		ASSERT(ilm_lookup_ipif(ipif,
17281 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
17282 
17283 		if (from_ill->ill_ilm_walker_cnt != 0) {
17284 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
17285 			if (new_ilm == NULL) {
17286 				char buf[INET6_ADDRSTRLEN];
17287 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
17288 				    " multicast address %s : "
17289 				    "from %s to"
17290 				    " %s failed : ENOMEM \n",
17291 				    inet_ntop(AF_INET,
17292 				    &ilm->ilm_v6addr, buf,
17293 				    sizeof (buf)),
17294 				    from_ill->ill_name,
17295 				    to_ill->ill_name));
17296 
17297 				ilmp = &ilm->ilm_next;
17298 				continue;
17299 			}
17300 			*new_ilm = *ilm;
17301 			DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
17302 			    (char *), "ilm", (void *), ilm);
17303 			new_ilm->ilm_ipif->ipif_ilm_cnt++;
17304 			/* We don't want new_ilm linked to ilm's filter list */
17305 			new_ilm->ilm_filter = NULL;
17306 		} else {
17307 			/* Remove from the list */
17308 			*ilmp = ilm->ilm_next;
17309 			new_ilm = ilm;
17310 		}
17311 
17312 		/*
17313 		 * If we have never joined this group on the to_ill
17314 		 * make sure we tell the driver.
17315 		 */
17316 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
17317 		    ALL_ZONES) == NULL)
17318 			new_ilm->ilm_notify_driver = B_TRUE;
17319 
17320 		/* Add to the to_ill's list */
17321 		new_ilm->ilm_next = to_ill->ill_ilm;
17322 		to_ill->ill_ilm = new_ilm;
17323 		new_ilm->ilm_is_new = B_TRUE;
17324 
17325 		/*
17326 		 * Revert multicast filter state to (EXCLUDE, NULL)
17327 		 */
17328 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
17329 		CLEAR_SLIST(new_ilm->ilm_filter);
17330 
17331 		/*
17332 		 * Delete only if we have allocated a new ilm.
17333 		 */
17334 		if (new_ilm != ilm) {
17335 delete_ilm:
17336 			if (from_ill->ill_ilm_walker_cnt == 0) {
17337 				/* Remove from the list */
17338 				*ilmp = ilm->ilm_next;
17339 				ilm->ilm_next = NULL;
17340 				DTRACE_PROBE3(ipif__decr__cnt,
17341 				    (ipif_t *), ilm->ilm_ipif,
17342 				    (char *), "ilm", (void *), ilm);
17343 				ASSERT(ilm->ilm_ipif->ipif_ilm_cnt > 0);
17344 				ilm->ilm_ipif->ipif_ilm_cnt--;
17345 				ilm_inactive(ilm);
17346 			} else {
17347 				ilm->ilm_flags |= ILM_DELETED;
17348 				from_ill->ill_ilm_cleanup_reqd = 1;
17349 				ilmp = &ilm->ilm_next;
17350 			}
17351 		}
17352 	}
17353 }
17354 
17355 static uint_t
17356 ipif_get_id(ill_t *ill, uint_t id)
17357 {
17358 	uint_t	unit;
17359 	ipif_t	*tipif;
17360 	boolean_t found = B_FALSE;
17361 	ip_stack_t	*ipst = ill->ill_ipst;
17362 
17363 	/*
17364 	 * During failback, we want to go back to the same id
17365 	 * instead of the smallest id so that the original
17366 	 * configuration is maintained. id is non-zero in that
17367 	 * case.
17368 	 */
17369 	if (id != 0) {
17370 		/*
17371 		 * While failing back, if we still have an ipif with
17372 		 * MAX_ADDRS_PER_IF, it means this will be replaced
17373 		 * as soon as we return from this function. It was
17374 		 * to set to MAX_ADDRS_PER_IF by the caller so that
17375 		 * we can choose the smallest id. Thus we return zero
17376 		 * in that case ignoring the hint.
17377 		 */
17378 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
17379 			return (0);
17380 		for (tipif = ill->ill_ipif; tipif != NULL;
17381 		    tipif = tipif->ipif_next) {
17382 			if (tipif->ipif_id == id) {
17383 				found = B_TRUE;
17384 				break;
17385 			}
17386 		}
17387 		/*
17388 		 * If somebody already plumbed another logical
17389 		 * with the same id, we won't be able to find it.
17390 		 */
17391 		if (!found)
17392 			return (id);
17393 	}
17394 	for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) {
17395 		found = B_FALSE;
17396 		for (tipif = ill->ill_ipif; tipif != NULL;
17397 		    tipif = tipif->ipif_next) {
17398 			if (tipif->ipif_id == unit) {
17399 				found = B_TRUE;
17400 				break;
17401 			}
17402 		}
17403 		if (!found)
17404 			break;
17405 	}
17406 	return (unit);
17407 }
17408 
17409 /* ARGSUSED */
17410 static int
17411 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
17412     ipif_t **rep_ipif_ptr)
17413 {
17414 	ill_t	*from_ill;
17415 	ipif_t	*rep_ipif;
17416 	uint_t	unit;
17417 	int err = 0;
17418 	ipif_t	*to_ipif;
17419 	struct iocblk	*iocp;
17420 	boolean_t failback_cmd;
17421 	boolean_t remove_ipif;
17422 	int	rc;
17423 	ip_stack_t	*ipst;
17424 
17425 	ASSERT(IAM_WRITER_ILL(to_ill));
17426 	ASSERT(IAM_WRITER_IPIF(ipif));
17427 
17428 	iocp = (struct iocblk *)mp->b_rptr;
17429 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
17430 	remove_ipif = B_FALSE;
17431 
17432 	from_ill = ipif->ipif_ill;
17433 	ipst = from_ill->ill_ipst;
17434 
17435 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
17436 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
17437 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
17438 
17439 	/*
17440 	 * Don't move LINK LOCAL addresses as they are tied to
17441 	 * physical interface.
17442 	 */
17443 	if (from_ill->ill_isv6 &&
17444 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
17445 		ipif->ipif_was_up = B_FALSE;
17446 		IPIF_UNMARK_MOVING(ipif);
17447 		return (0);
17448 	}
17449 
17450 	/*
17451 	 * We set the ipif_id to maximum so that the search for
17452 	 * ipif_id will pick the lowest number i.e 0 in the
17453 	 * following 2 cases :
17454 	 *
17455 	 * 1) We have a replacement ipif at the head of to_ill.
17456 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
17457 	 *    on to_ill and hence the MOVE might fail. We want to
17458 	 *    remove it only if we could move the ipif. Thus, by
17459 	 *    setting it to the MAX value, we make the search in
17460 	 *    ipif_get_id return the zeroth id.
17461 	 *
17462 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
17463 	 *    we might just have a zero address plumbed on the ipif
17464 	 *    with zero id in the case of IPv4. We remove that while
17465 	 *    doing the failback. We want to remove it only if we
17466 	 *    could move the ipif. Thus, by setting it to the MAX
17467 	 *    value, we make the search in ipif_get_id return the
17468 	 *    zeroth id.
17469 	 *
17470 	 * Both (1) and (2) are done only when when we are moving
17471 	 * an ipif (either due to failover/failback) which originally
17472 	 * belonged to this interface i.e the ipif_orig_ifindex is
17473 	 * the same as to_ill's ifindex. This is needed so that
17474 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
17475 	 * from B -> A (B is being removed from the group) and
17476 	 * FAILBACK from A -> B restores the original configuration.
17477 	 * Without the check for orig_ifindex, the second FAILOVER
17478 	 * could make the ipif belonging to B replace the A's zeroth
17479 	 * ipif and the subsequent failback re-creating the replacement
17480 	 * ipif again.
17481 	 *
17482 	 * NOTE : We created the replacement ipif when we did a
17483 	 * FAILOVER (See below). We could check for FAILBACK and
17484 	 * then look for replacement ipif to be removed. But we don't
17485 	 * want to do that because we wan't to allow the possibility
17486 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
17487 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
17488 	 * from B -> A.
17489 	 */
17490 	to_ipif = to_ill->ill_ipif;
17491 	if ((to_ill->ill_phyint->phyint_ifindex ==
17492 	    ipif->ipif_orig_ifindex) &&
17493 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
17494 		ASSERT(to_ipif->ipif_id == 0);
17495 		remove_ipif = B_TRUE;
17496 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
17497 	}
17498 	/*
17499 	 * Find the lowest logical unit number on the to_ill.
17500 	 * If we are failing back, try to get the original id
17501 	 * rather than the lowest one so that the original
17502 	 * configuration is maintained.
17503 	 *
17504 	 * XXX need a better scheme for this.
17505 	 */
17506 	if (failback_cmd) {
17507 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
17508 	} else {
17509 		unit = ipif_get_id(to_ill, 0);
17510 	}
17511 
17512 	/* Reset back to zero in case we fail below */
17513 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
17514 		to_ipif->ipif_id = 0;
17515 
17516 	if (unit == ipst->ips_ip_addrs_per_if) {
17517 		ipif->ipif_was_up = B_FALSE;
17518 		IPIF_UNMARK_MOVING(ipif);
17519 		return (EINVAL);
17520 	}
17521 
17522 	/*
17523 	 * ipif is ready to move from "from_ill" to "to_ill".
17524 	 *
17525 	 * 1) If we are moving ipif with id zero, create a
17526 	 *    replacement ipif for this ipif on from_ill. If this fails
17527 	 *    fail the MOVE operation.
17528 	 *
17529 	 * 2) Remove the replacement ipif on to_ill if any.
17530 	 *    We could remove the replacement ipif when we are moving
17531 	 *    the ipif with id zero. But what if somebody already
17532 	 *    unplumbed it ? Thus we always remove it if it is present.
17533 	 *    We want to do it only if we are sure we are going to
17534 	 *    move the ipif to to_ill which is why there are no
17535 	 *    returns due to error till ipif is linked to to_ill.
17536 	 *    Note that the first ipif that we failback will always
17537 	 *    be zero if it is present.
17538 	 */
17539 	if (ipif->ipif_id == 0) {
17540 		ipaddr_t inaddr_any = INADDR_ANY;
17541 
17542 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
17543 		if (rep_ipif == NULL) {
17544 			ipif->ipif_was_up = B_FALSE;
17545 			IPIF_UNMARK_MOVING(ipif);
17546 			return (ENOMEM);
17547 		}
17548 		*rep_ipif = ipif_zero;
17549 		/*
17550 		 * Before we put the ipif on the list, store the addresses
17551 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
17552 		 * assumes so. This logic is not any different from what
17553 		 * ipif_allocate does.
17554 		 */
17555 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17556 		    &rep_ipif->ipif_v6lcl_addr);
17557 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17558 		    &rep_ipif->ipif_v6src_addr);
17559 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17560 		    &rep_ipif->ipif_v6subnet);
17561 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17562 		    &rep_ipif->ipif_v6net_mask);
17563 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17564 		    &rep_ipif->ipif_v6brd_addr);
17565 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
17566 		    &rep_ipif->ipif_v6pp_dst_addr);
17567 		/*
17568 		 * We mark IPIF_NOFAILOVER so that this can never
17569 		 * move.
17570 		 */
17571 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
17572 		rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE;
17573 		rep_ipif->ipif_replace_zero = B_TRUE;
17574 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
17575 		    MUTEX_DEFAULT, NULL);
17576 		rep_ipif->ipif_id = 0;
17577 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
17578 		rep_ipif->ipif_ill = from_ill;
17579 		rep_ipif->ipif_orig_ifindex =
17580 		    from_ill->ill_phyint->phyint_ifindex;
17581 		/* Insert at head */
17582 		rep_ipif->ipif_next = from_ill->ill_ipif;
17583 		from_ill->ill_ipif = rep_ipif;
17584 		/*
17585 		 * We don't really care to let apps know about
17586 		 * this interface.
17587 		 */
17588 	}
17589 
17590 	if (remove_ipif) {
17591 		/*
17592 		 * We set to a max value above for this case to get
17593 		 * id zero. ASSERT that we did get one.
17594 		 */
17595 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
17596 		rep_ipif = to_ipif;
17597 		to_ill->ill_ipif = rep_ipif->ipif_next;
17598 		rep_ipif->ipif_next = NULL;
17599 		/*
17600 		 * If some apps scanned and find this interface,
17601 		 * it is time to let them know, so that they can
17602 		 * delete it.
17603 		 */
17604 
17605 		*rep_ipif_ptr = rep_ipif;
17606 	}
17607 
17608 	/* Get it out of the ILL interface list. */
17609 	ipif_remove(ipif, B_FALSE);
17610 
17611 	/* Assign the new ill */
17612 	ipif->ipif_ill = to_ill;
17613 	ipif->ipif_id = unit;
17614 	/* id has already been checked */
17615 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
17616 	ASSERT(rc == 0);
17617 	/* Let SCTP update its list */
17618 	sctp_move_ipif(ipif, from_ill, to_ill);
17619 	/*
17620 	 * Handle the failover and failback of ipif_t between
17621 	 * ill_t that have differing maximum mtu values.
17622 	 */
17623 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
17624 		if (ipif->ipif_saved_mtu == 0) {
17625 			/*
17626 			 * As this ipif_t is moving to an ill_t
17627 			 * that has a lower ill_max_mtu, its
17628 			 * ipif_mtu needs to be saved so it can
17629 			 * be restored during failback or during
17630 			 * failover to an ill_t which has a
17631 			 * higher ill_max_mtu.
17632 			 */
17633 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
17634 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17635 		} else {
17636 			/*
17637 			 * The ipif_t is, once again, moving to
17638 			 * an ill_t that has a lower maximum mtu
17639 			 * value.
17640 			 */
17641 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17642 		}
17643 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
17644 	    ipif->ipif_saved_mtu != 0) {
17645 		/*
17646 		 * The mtu of this ipif_t had to be reduced
17647 		 * during an earlier failover; this is an
17648 		 * opportunity for it to be increased (either as
17649 		 * part of another failover or a failback).
17650 		 */
17651 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
17652 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
17653 			ipif->ipif_saved_mtu = 0;
17654 		} else {
17655 			ipif->ipif_mtu = to_ill->ill_max_mtu;
17656 		}
17657 	}
17658 
17659 	/*
17660 	 * We preserve all the other fields of the ipif including
17661 	 * ipif_saved_ire_mp. The routes that are saved here will
17662 	 * be recreated on the new interface and back on the old
17663 	 * interface when we move back.
17664 	 */
17665 	ASSERT(ipif->ipif_arp_del_mp == NULL);
17666 
17667 	return (err);
17668 }
17669 
17670 static int
17671 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
17672     int ifindex, ipif_t **rep_ipif_ptr)
17673 {
17674 	ipif_t *mipif;
17675 	ipif_t *ipif_next;
17676 	int err;
17677 
17678 	/*
17679 	 * We don't really try to MOVE back things if some of the
17680 	 * operations fail. The daemon will take care of moving again
17681 	 * later on.
17682 	 */
17683 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
17684 		ipif_next = mipif->ipif_next;
17685 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
17686 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
17687 
17688 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
17689 
17690 			/*
17691 			 * When the MOVE fails, it is the job of the
17692 			 * application to take care of this properly
17693 			 * i.e try again if it is ENOMEM.
17694 			 */
17695 			if (mipif->ipif_ill != from_ill) {
17696 				/*
17697 				 * ipif has moved.
17698 				 *
17699 				 * Move the multicast memberships associated
17700 				 * with this ipif to the new ill. For IPv6, we
17701 				 * do it once after all the ipifs are moved
17702 				 * (in ill_move) as they are not associated
17703 				 * with ipifs.
17704 				 *
17705 				 * We need to move the ilms as the ipif has
17706 				 * already been moved to a new ill even
17707 				 * in the case of errors. Neither
17708 				 * ilm_free(ipif) will find the ilm
17709 				 * when somebody unplumbs this ipif nor
17710 				 * ilm_delete(ilm) will be able to find the
17711 				 * ilm, if we don't move now.
17712 				 */
17713 				if (!from_ill->ill_isv6)
17714 					ilm_move_v4(from_ill, to_ill, mipif);
17715 			}
17716 
17717 			if (err != 0)
17718 				return (err);
17719 		}
17720 	}
17721 	return (0);
17722 }
17723 
17724 static int
17725 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
17726 {
17727 	int ifindex;
17728 	int err;
17729 	struct iocblk	*iocp;
17730 	ipif_t	*ipif;
17731 	ipif_t *rep_ipif_ptr = NULL;
17732 	ipif_t	*from_ipif = NULL;
17733 	boolean_t check_rep_if = B_FALSE;
17734 	ip_stack_t	*ipst = from_ill->ill_ipst;
17735 
17736 	iocp = (struct iocblk *)mp->b_rptr;
17737 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
17738 		/*
17739 		 * Move everything pointing at from_ill to to_ill.
17740 		 * We acheive this by passing in 0 as ifindex.
17741 		 */
17742 		ifindex = 0;
17743 	} else {
17744 		/*
17745 		 * Move everything pointing at from_ill whose original
17746 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
17747 		 * We acheive this by passing in ifindex rather than 0.
17748 		 * Multicast vifs, ilgs move implicitly because ipifs move.
17749 		 */
17750 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
17751 		ifindex = to_ill->ill_phyint->phyint_ifindex;
17752 	}
17753 
17754 	/*
17755 	 * Determine if there is at least one ipif that would move from
17756 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
17757 	 * ipif (if it exists) on the to_ill would be consumed as a result of
17758 	 * the move, in which case we need to quiesce the replacement ipif also.
17759 	 */
17760 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
17761 	    from_ipif = from_ipif->ipif_next) {
17762 		if (((ifindex == 0) ||
17763 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
17764 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
17765 			check_rep_if = B_TRUE;
17766 			break;
17767 		}
17768 	}
17769 
17770 
17771 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
17772 
17773 	GRAB_ILL_LOCKS(from_ill, to_ill);
17774 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
17775 		(void) ipsq_pending_mp_add(NULL, ipif, q,
17776 		    mp, ILL_MOVE_OK);
17777 		RELEASE_ILL_LOCKS(from_ill, to_ill);
17778 		return (EINPROGRESS);
17779 	}
17780 
17781 	/* Check if the replacement ipif is quiescent to delete */
17782 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
17783 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
17784 		to_ill->ill_ipif->ipif_state_flags |=
17785 		    IPIF_MOVING | IPIF_CHANGING;
17786 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
17787 			(void) ipsq_pending_mp_add(NULL, ipif, q,
17788 			    mp, ILL_MOVE_OK);
17789 			RELEASE_ILL_LOCKS(from_ill, to_ill);
17790 			return (EINPROGRESS);
17791 		}
17792 	}
17793 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17794 
17795 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
17796 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17797 	GRAB_ILL_LOCKS(from_ill, to_ill);
17798 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
17799 
17800 	/* ilm_move is done inside ipif_move for IPv4 */
17801 	if (err == 0 && from_ill->ill_isv6)
17802 		ilm_move_v6(from_ill, to_ill, ifindex);
17803 
17804 	RELEASE_ILL_LOCKS(from_ill, to_ill);
17805 	rw_exit(&ipst->ips_ill_g_lock);
17806 
17807 	/*
17808 	 * send rts messages and multicast messages.
17809 	 */
17810 	if (rep_ipif_ptr != NULL) {
17811 		if (rep_ipif_ptr->ipif_recovery_id != 0) {
17812 			(void) untimeout(rep_ipif_ptr->ipif_recovery_id);
17813 			rep_ipif_ptr->ipif_recovery_id = 0;
17814 		}
17815 		ip_rts_ifmsg(rep_ipif_ptr);
17816 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
17817 #ifdef DEBUG
17818 		ipif_trace_cleanup(rep_ipif_ptr);
17819 #endif
17820 		mi_free(rep_ipif_ptr);
17821 	}
17822 
17823 	conn_move_ill(from_ill, to_ill, ifindex);
17824 
17825 	return (err);
17826 }
17827 
17828 /*
17829  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
17830  * Also checks for the validity of the arguments.
17831  * Note: We are already exclusive inside the from group.
17832  * It is upto the caller to release refcnt on the to_ill's.
17833  */
17834 static int
17835 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
17836     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
17837 {
17838 	int dst_index;
17839 	ipif_t *ipif_v4, *ipif_v6;
17840 	struct lifreq *lifr;
17841 	mblk_t *mp1;
17842 	boolean_t exists;
17843 	sin_t	*sin;
17844 	int	err = 0;
17845 	ip_stack_t	*ipst;
17846 
17847 	if (CONN_Q(q))
17848 		ipst = CONNQ_TO_IPST(q);
17849 	else
17850 		ipst = ILLQ_TO_IPST(q);
17851 
17852 
17853 	if ((mp1 = mp->b_cont) == NULL)
17854 		return (EPROTO);
17855 
17856 	if ((mp1 = mp1->b_cont) == NULL)
17857 		return (EPROTO);
17858 
17859 	lifr = (struct lifreq *)mp1->b_rptr;
17860 	sin = (sin_t *)&lifr->lifr_addr;
17861 
17862 	/*
17863 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
17864 	 * specific operations.
17865 	 */
17866 	if (sin->sin_family != AF_UNSPEC)
17867 		return (EINVAL);
17868 
17869 	/*
17870 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
17871 	 * NULLs for the last 4 args and we know the lookup won't fail
17872 	 * with EINPROGRESS.
17873 	 */
17874 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
17875 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
17876 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17877 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
17878 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
17879 	    ALL_ZONES, NULL, NULL, NULL, NULL, ipst);
17880 
17881 	if (ipif_v4 == NULL && ipif_v6 == NULL)
17882 		return (ENXIO);
17883 
17884 	if (ipif_v4 != NULL) {
17885 		ASSERT(ipif_v4->ipif_refcnt != 0);
17886 		if (ipif_v4->ipif_id != 0) {
17887 			err = EINVAL;
17888 			goto done;
17889 		}
17890 
17891 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
17892 		*ill_from_v4 = ipif_v4->ipif_ill;
17893 	}
17894 
17895 	if (ipif_v6 != NULL) {
17896 		ASSERT(ipif_v6->ipif_refcnt != 0);
17897 		if (ipif_v6->ipif_id != 0) {
17898 			err = EINVAL;
17899 			goto done;
17900 		}
17901 
17902 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
17903 		*ill_from_v6 = ipif_v6->ipif_ill;
17904 	}
17905 
17906 	err = 0;
17907 	dst_index = lifr->lifr_movetoindex;
17908 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
17909 	    q, mp, ip_process_ioctl, &err, ipst);
17910 	if (err != 0) {
17911 		/*
17912 		 * There could be only v6.
17913 		 */
17914 		if (err != ENXIO)
17915 			goto done;
17916 		err = 0;
17917 	}
17918 
17919 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
17920 	    q, mp, ip_process_ioctl, &err, ipst);
17921 	if (err != 0) {
17922 		if (err != ENXIO)
17923 			goto done;
17924 		if (*ill_to_v4 == NULL) {
17925 			err = ENXIO;
17926 			goto done;
17927 		}
17928 		err = 0;
17929 	}
17930 
17931 	/*
17932 	 * If we have something to MOVE i.e "from" not NULL,
17933 	 * "to" should be non-NULL.
17934 	 */
17935 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
17936 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
17937 		err = EINVAL;
17938 	}
17939 
17940 done:
17941 	if (ipif_v4 != NULL)
17942 		ipif_refrele(ipif_v4);
17943 	if (ipif_v6 != NULL)
17944 		ipif_refrele(ipif_v6);
17945 	return (err);
17946 }
17947 
17948 /*
17949  * FAILOVER and FAILBACK are modelled as MOVE operations.
17950  *
17951  * We don't check whether the MOVE is within the same group or
17952  * not, because this ioctl can be used as a generic mechanism
17953  * to failover from interface A to B, though things will function
17954  * only if they are really part of the same group. Moreover,
17955  * all ipifs may be down and hence temporarily out of the group.
17956  *
17957  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17958  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17959  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17960  * have been deleted and there are no active references. Once quiescent the
17961  * ipif's are moved and brought up on the new ill.
17962  *
17963  * Normally the source ill and destination ill belong to the same IPMP group
17964  * and hence the same ipsq_t. In the event they don't belong to the same
17965  * same group the two ipsq's are first merged into one ipsq - that of the
17966  * to_ill. The multicast memberships on the source and destination ill cannot
17967  * change during the move operation since multicast joins/leaves also have to
17968  * execute on the same ipsq and are hence serialized.
17969  */
17970 /* ARGSUSED */
17971 int
17972 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17973     ip_ioctl_cmd_t *ipip, void *ifreq)
17974 {
17975 	ill_t *ill_to_v4 = NULL;
17976 	ill_t *ill_to_v6 = NULL;
17977 	ill_t *ill_from_v4 = NULL;
17978 	ill_t *ill_from_v6 = NULL;
17979 	int err = 0;
17980 
17981 	/*
17982 	 * setup from and to ill's, we can get EINPROGRESS only for
17983 	 * to_ill's.
17984 	 */
17985 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17986 	    &ill_to_v4, &ill_to_v6);
17987 
17988 	if (err != 0) {
17989 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17990 		goto done;
17991 	}
17992 
17993 	/*
17994 	 * nothing to do.
17995 	 */
17996 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17997 		goto done;
17998 	}
17999 
18000 	/*
18001 	 * nothing to do.
18002 	 */
18003 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
18004 		goto done;
18005 	}
18006 
18007 	/*
18008 	 * Mark the ill as changing.
18009 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
18010 	 * in ill_up_ipifs in case of error they are cleared below.
18011 	 */
18012 
18013 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18014 	if (ill_from_v4 != NULL)
18015 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
18016 	if (ill_from_v6 != NULL)
18017 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
18018 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18019 
18020 	/*
18021 	 * Make sure that both src and dst are
18022 	 * in the same syncq group. If not make it happen.
18023 	 * We are not holding any locks because we are the writer
18024 	 * on the from_ipsq and we will hold locks in ill_merge_groups
18025 	 * to protect to_ipsq against changing.
18026 	 */
18027 	if (ill_from_v4 != NULL) {
18028 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
18029 		    ill_to_v4->ill_phyint->phyint_ipsq) {
18030 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
18031 			    NULL, mp, q);
18032 			goto err_ret;
18033 
18034 		}
18035 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
18036 	} else {
18037 
18038 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
18039 		    ill_to_v6->ill_phyint->phyint_ipsq) {
18040 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
18041 			    NULL, mp, q);
18042 			goto err_ret;
18043 
18044 		}
18045 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
18046 	}
18047 
18048 	/*
18049 	 * Now that the ipsq's have been merged and we are the writer
18050 	 * lets mark to_ill as changing as well.
18051 	 */
18052 
18053 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18054 	if (ill_to_v4 != NULL)
18055 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
18056 	if (ill_to_v6 != NULL)
18057 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
18058 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18059 
18060 	/*
18061 	 * Its ok for us to proceed with the move even if
18062 	 * ill_pending_mp is non null on one of the from ill's as the reply
18063 	 * should not be looking at the ipif, it should only care about the
18064 	 * ill itself.
18065 	 */
18066 
18067 	/*
18068 	 * lets move ipv4 first.
18069 	 */
18070 	if (ill_from_v4 != NULL) {
18071 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
18072 		ill_from_v4->ill_move_in_progress = B_TRUE;
18073 		ill_to_v4->ill_move_in_progress = B_TRUE;
18074 		ill_to_v4->ill_move_peer = ill_from_v4;
18075 		ill_from_v4->ill_move_peer = ill_to_v4;
18076 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
18077 	}
18078 
18079 	/*
18080 	 * Now lets move ipv6.
18081 	 */
18082 	if (err == 0 && ill_from_v6 != NULL) {
18083 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
18084 		ill_from_v6->ill_move_in_progress = B_TRUE;
18085 		ill_to_v6->ill_move_in_progress = B_TRUE;
18086 		ill_to_v6->ill_move_peer = ill_from_v6;
18087 		ill_from_v6->ill_move_peer = ill_to_v6;
18088 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
18089 	}
18090 
18091 err_ret:
18092 	/*
18093 	 * EINPROGRESS means we are waiting for the ipif's that need to be
18094 	 * moved to become quiescent.
18095 	 */
18096 	if (err == EINPROGRESS) {
18097 		goto done;
18098 	}
18099 
18100 	/*
18101 	 * if err is set ill_up_ipifs will not be called
18102 	 * lets clear the flags.
18103 	 */
18104 
18105 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
18106 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
18107 	/*
18108 	 * Some of the clearing may be redundant. But it is simple
18109 	 * not making any extra checks.
18110 	 */
18111 	if (ill_from_v6 != NULL) {
18112 		ill_from_v6->ill_move_in_progress = B_FALSE;
18113 		ill_from_v6->ill_move_peer = NULL;
18114 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
18115 	}
18116 	if (ill_from_v4 != NULL) {
18117 		ill_from_v4->ill_move_in_progress = B_FALSE;
18118 		ill_from_v4->ill_move_peer = NULL;
18119 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
18120 	}
18121 	if (ill_to_v6 != NULL) {
18122 		ill_to_v6->ill_move_in_progress = B_FALSE;
18123 		ill_to_v6->ill_move_peer = NULL;
18124 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
18125 	}
18126 	if (ill_to_v4 != NULL) {
18127 		ill_to_v4->ill_move_in_progress = B_FALSE;
18128 		ill_to_v4->ill_move_peer = NULL;
18129 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
18130 	}
18131 
18132 	/*
18133 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
18134 	 * Do this always to maintain proper state i.e even in case of errors.
18135 	 * As phyint_inactive looks at both v4 and v6 interfaces,
18136 	 * we need not call on both v4 and v6 interfaces.
18137 	 */
18138 	if (ill_from_v4 != NULL) {
18139 		if ((ill_from_v4->ill_phyint->phyint_flags &
18140 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18141 			phyint_inactive(ill_from_v4->ill_phyint);
18142 		}
18143 	} else if (ill_from_v6 != NULL) {
18144 		if ((ill_from_v6->ill_phyint->phyint_flags &
18145 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
18146 			phyint_inactive(ill_from_v6->ill_phyint);
18147 		}
18148 	}
18149 
18150 	if (ill_to_v4 != NULL) {
18151 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18152 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18153 		}
18154 	} else if (ill_to_v6 != NULL) {
18155 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
18156 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
18157 		}
18158 	}
18159 
18160 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
18161 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
18162 
18163 no_err:
18164 	/*
18165 	 * lets bring the interfaces up on the to_ill.
18166 	 */
18167 	if (err == 0) {
18168 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
18169 		    q, mp);
18170 	}
18171 
18172 	if (err == 0) {
18173 		if (ill_from_v4 != NULL && ill_to_v4 != NULL)
18174 			ilm_send_multicast_reqs(ill_from_v4, ill_to_v4);
18175 
18176 		if (ill_from_v6 != NULL && ill_to_v6 != NULL)
18177 			ilm_send_multicast_reqs(ill_from_v6, ill_to_v6);
18178 	}
18179 done:
18180 
18181 	if (ill_to_v4 != NULL) {
18182 		ill_refrele(ill_to_v4);
18183 	}
18184 	if (ill_to_v6 != NULL) {
18185 		ill_refrele(ill_to_v6);
18186 	}
18187 
18188 	return (err);
18189 }
18190 
18191 static void
18192 ill_dl_down(ill_t *ill)
18193 {
18194 	/*
18195 	 * The ill is down; unbind but stay attached since we're still
18196 	 * associated with a PPA. If we have negotiated DLPI capabilites
18197 	 * with the data link service provider (IDS_OK) then reset them.
18198 	 * The interval between unbinding and rebinding is potentially
18199 	 * unbounded hence we cannot assume things will be the same.
18200 	 * The DLPI capabilities will be probed again when the data link
18201 	 * is brought up.
18202 	 */
18203 	mblk_t	*mp = ill->ill_unbind_mp;
18204 
18205 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
18206 
18207 	ill->ill_unbind_mp = NULL;
18208 	if (mp != NULL) {
18209 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
18210 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
18211 		    ill->ill_name));
18212 		mutex_enter(&ill->ill_lock);
18213 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
18214 		mutex_exit(&ill->ill_lock);
18215 		/*
18216 		 * Reset the capabilities if the negotiation is done or is
18217 		 * still in progress. Note that ill_capability_reset() will
18218 		 * set ill_dlpi_capab_state to IDS_UNKNOWN, so the subsequent
18219 		 * DL_CAPABILITY_ACK and DL_NOTE_CAPAB_RENEG will be ignored.
18220 		 *
18221 		 * Further, reset ill_capab_reneg to be B_FALSE so that the
18222 		 * subsequent DL_CAPABILITY_ACK can be ignored, to prevent
18223 		 * the capabilities renegotiation from happening.
18224 		 */
18225 		if (ill->ill_dlpi_capab_state != IDS_UNKNOWN)
18226 			ill_capability_reset(ill);
18227 		ill->ill_capab_reneg = B_FALSE;
18228 
18229 		ill_dlpi_send(ill, mp);
18230 	}
18231 
18232 	/*
18233 	 * Toss all of our multicast memberships.  We could keep them, but
18234 	 * then we'd have to do bookkeeping of any joins and leaves performed
18235 	 * by the application while the the interface is down (we can't just
18236 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
18237 	 * on a downed interface).
18238 	 */
18239 	ill_leave_multicast(ill);
18240 
18241 	mutex_enter(&ill->ill_lock);
18242 	ill->ill_dl_up = 0;
18243 	(void) ill_hook_event_create(ill, 0, NE_DOWN, NULL, 0);
18244 	mutex_exit(&ill->ill_lock);
18245 }
18246 
18247 static void
18248 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
18249 {
18250 	union DL_primitives *dlp;
18251 	t_uscalar_t prim;
18252 
18253 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18254 
18255 	dlp = (union DL_primitives *)mp->b_rptr;
18256 	prim = dlp->dl_primitive;
18257 
18258 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
18259 	    dl_primstr(prim), prim, ill->ill_name));
18260 
18261 	switch (prim) {
18262 	case DL_PHYS_ADDR_REQ:
18263 	{
18264 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
18265 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
18266 		break;
18267 	}
18268 	case DL_BIND_REQ:
18269 		mutex_enter(&ill->ill_lock);
18270 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
18271 		mutex_exit(&ill->ill_lock);
18272 		break;
18273 	}
18274 
18275 	/*
18276 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
18277 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
18278 	 * we only wait for the ACK of the DL_UNBIND_REQ.
18279 	 */
18280 	mutex_enter(&ill->ill_lock);
18281 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
18282 	    (prim == DL_UNBIND_REQ)) {
18283 		ill->ill_dlpi_pending = prim;
18284 	}
18285 	mutex_exit(&ill->ill_lock);
18286 
18287 	putnext(ill->ill_wq, mp);
18288 }
18289 
18290 /*
18291  * Helper function for ill_dlpi_send().
18292  */
18293 /* ARGSUSED */
18294 static void
18295 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
18296 {
18297 	ill_dlpi_send(q->q_ptr, mp);
18298 }
18299 
18300 /*
18301  * Send a DLPI control message to the driver but make sure there
18302  * is only one outstanding message. Uses ill_dlpi_pending to tell
18303  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
18304  * when an ACK or a NAK is received to process the next queued message.
18305  */
18306 void
18307 ill_dlpi_send(ill_t *ill, mblk_t *mp)
18308 {
18309 	mblk_t **mpp;
18310 
18311 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
18312 
18313 	/*
18314 	 * To ensure that any DLPI requests for current exclusive operation
18315 	 * are always completely sent before any DLPI messages for other
18316 	 * operations, require writer access before enqueuing.
18317 	 */
18318 	if (!IAM_WRITER_ILL(ill)) {
18319 		ill_refhold(ill);
18320 		/* qwriter_ip() does the ill_refrele() */
18321 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
18322 		    NEW_OP, B_TRUE);
18323 		return;
18324 	}
18325 
18326 	mutex_enter(&ill->ill_lock);
18327 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
18328 		/* Must queue message. Tail insertion */
18329 		mpp = &ill->ill_dlpi_deferred;
18330 		while (*mpp != NULL)
18331 			mpp = &((*mpp)->b_next);
18332 
18333 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
18334 		    ill->ill_name));
18335 
18336 		*mpp = mp;
18337 		mutex_exit(&ill->ill_lock);
18338 		return;
18339 	}
18340 	mutex_exit(&ill->ill_lock);
18341 	ill_dlpi_dispatch(ill, mp);
18342 }
18343 
18344 /*
18345  * Send all deferred DLPI messages without waiting for their ACKs.
18346  */
18347 void
18348 ill_dlpi_send_deferred(ill_t *ill)
18349 {
18350 	mblk_t *mp, *nextmp;
18351 
18352 	/*
18353 	 * Clear ill_dlpi_pending so that the message is not queued in
18354 	 * ill_dlpi_send().
18355 	 */
18356 	mutex_enter(&ill->ill_lock);
18357 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
18358 	mp = ill->ill_dlpi_deferred;
18359 	ill->ill_dlpi_deferred = NULL;
18360 	mutex_exit(&ill->ill_lock);
18361 
18362 	for (; mp != NULL; mp = nextmp) {
18363 		nextmp = mp->b_next;
18364 		mp->b_next = NULL;
18365 		ill_dlpi_send(ill, mp);
18366 	}
18367 }
18368 
18369 /*
18370  * Check if the DLPI primitive `prim' is pending; print a warning if not.
18371  */
18372 boolean_t
18373 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
18374 {
18375 	t_uscalar_t pending;
18376 
18377 	mutex_enter(&ill->ill_lock);
18378 	if (ill->ill_dlpi_pending == prim) {
18379 		mutex_exit(&ill->ill_lock);
18380 		return (B_TRUE);
18381 	}
18382 
18383 	/*
18384 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
18385 	 * without waiting, so don't print any warnings in that case.
18386 	 */
18387 	if (ill->ill_state_flags & ILL_CONDEMNED) {
18388 		mutex_exit(&ill->ill_lock);
18389 		return (B_FALSE);
18390 	}
18391 	pending = ill->ill_dlpi_pending;
18392 	mutex_exit(&ill->ill_lock);
18393 
18394 	if (pending == DL_PRIM_INVAL) {
18395 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18396 		    "received unsolicited ack for %s on %s\n",
18397 		    dl_primstr(prim), ill->ill_name);
18398 	} else {
18399 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
18400 		    "received unexpected ack for %s on %s (expecting %s)\n",
18401 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
18402 	}
18403 	return (B_FALSE);
18404 }
18405 
18406 /*
18407  * Complete the current DLPI operation associated with `prim' on `ill' and
18408  * start the next queued DLPI operation (if any).  If there are no queued DLPI
18409  * operations and the ill's current exclusive IPSQ operation has finished
18410  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
18411  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
18412  * the comments above ipsq_current_finish() for details.
18413  */
18414 void
18415 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
18416 {
18417 	mblk_t *mp;
18418 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
18419 
18420 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18421 	mutex_enter(&ill->ill_lock);
18422 
18423 	ASSERT(prim != DL_PRIM_INVAL);
18424 	ASSERT(ill->ill_dlpi_pending == prim);
18425 
18426 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
18427 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
18428 
18429 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
18430 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
18431 
18432 		mutex_enter(&ipsq->ipsq_lock);
18433 		if (ipsq->ipsq_current_done)
18434 			ipsq->ipsq_current_ipif = NULL;
18435 		mutex_exit(&ipsq->ipsq_lock);
18436 
18437 		cv_signal(&ill->ill_cv);
18438 		mutex_exit(&ill->ill_lock);
18439 		return;
18440 	}
18441 
18442 	ill->ill_dlpi_deferred = mp->b_next;
18443 	mp->b_next = NULL;
18444 	mutex_exit(&ill->ill_lock);
18445 
18446 	ill_dlpi_dispatch(ill, mp);
18447 }
18448 
18449 void
18450 conn_delete_ire(conn_t *connp, caddr_t arg)
18451 {
18452 	ipif_t	*ipif = (ipif_t *)arg;
18453 	ire_t	*ire;
18454 
18455 	/*
18456 	 * Look at the cached ires on conns which has pointers to ipifs.
18457 	 * We just call ire_refrele which clears up the reference
18458 	 * to ire. Called when a conn closes. Also called from ipif_free
18459 	 * to cleanup indirect references to the stale ipif via the cached ire.
18460 	 */
18461 	mutex_enter(&connp->conn_lock);
18462 	ire = connp->conn_ire_cache;
18463 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
18464 		connp->conn_ire_cache = NULL;
18465 		mutex_exit(&connp->conn_lock);
18466 		IRE_REFRELE_NOTR(ire);
18467 		return;
18468 	}
18469 	mutex_exit(&connp->conn_lock);
18470 
18471 }
18472 
18473 /*
18474  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
18475  * of IREs. Those IREs may have been previously cached in the conn structure.
18476  * This ipcl_walk() walker function releases all references to such IREs based
18477  * on the condemned flag.
18478  */
18479 /* ARGSUSED */
18480 void
18481 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
18482 {
18483 	ire_t	*ire;
18484 
18485 	mutex_enter(&connp->conn_lock);
18486 	ire = connp->conn_ire_cache;
18487 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
18488 		connp->conn_ire_cache = NULL;
18489 		mutex_exit(&connp->conn_lock);
18490 		IRE_REFRELE_NOTR(ire);
18491 		return;
18492 	}
18493 	mutex_exit(&connp->conn_lock);
18494 }
18495 
18496 /*
18497  * Take down a specific interface, but don't lose any information about it.
18498  * Also delete interface from its interface group (ifgrp).
18499  * (Always called as writer.)
18500  * This function goes through the down sequence even if the interface is
18501  * already down. There are 2 reasons.
18502  * a. Currently we permit interface routes that depend on down interfaces
18503  *    to be added. This behaviour itself is questionable. However it appears
18504  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
18505  *    time. We go thru the cleanup in order to remove these routes.
18506  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
18507  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
18508  *    down, but we need to cleanup i.e. do ill_dl_down and
18509  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
18510  *
18511  * IP-MT notes:
18512  *
18513  * Model of reference to interfaces.
18514  *
18515  * The following members in ipif_t track references to the ipif.
18516  *	int     ipif_refcnt;    Active reference count
18517  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
18518  *	uint_t  ipif_ilm_cnt;   Number of ilms's references this ipif.
18519  *
18520  * The following members in ill_t track references to the ill.
18521  *	int             ill_refcnt;     active refcnt
18522  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
18523  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
18524  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
18525  *
18526  * Reference to an ipif or ill can be obtained in any of the following ways.
18527  *
18528  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
18529  * Pointers to ipif / ill from other data structures viz ire and conn.
18530  * Implicit reference to the ipif / ill by holding a reference to the ire.
18531  *
18532  * The ipif/ill lookup functions return a reference held ipif / ill.
18533  * ipif_refcnt and ill_refcnt track the reference counts respectively.
18534  * This is a purely dynamic reference count associated with threads holding
18535  * references to the ipif / ill. Pointers from other structures do not
18536  * count towards this reference count.
18537  *
18538  * ipif_ire_cnt/ill_ire_cnt is the number of ire's
18539  * associated with the ipif/ill. This is incremented whenever a new
18540  * ire is created referencing the ipif/ill. This is done atomically inside
18541  * ire_add_v[46] where the ire is actually added to the ire hash table.
18542  * The count is decremented in ire_inactive where the ire is destroyed.
18543  *
18544  * nce's reference ill's thru nce_ill and the count of nce's associated with
18545  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
18546  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
18547  * table. Similarly it is decremented in ndp_inactive() where the nce
18548  * is destroyed.
18549  *
18550  * ilm's reference to the ipif (for IPv4 ilm's) or the ill (for IPv6 ilm's)
18551  * is incremented in ilm_add_v6() and decremented before the ilm is freed
18552  * in ilm_walker_cleanup() or ilm_delete().
18553  *
18554  * Flow of ioctls involving interface down/up
18555  *
18556  * The following is the sequence of an attempt to set some critical flags on an
18557  * up interface.
18558  * ip_sioctl_flags
18559  * ipif_down
18560  * wait for ipif to be quiescent
18561  * ipif_down_tail
18562  * ip_sioctl_flags_tail
18563  *
18564  * All set ioctls that involve down/up sequence would have a skeleton similar
18565  * to the above. All the *tail functions are called after the refcounts have
18566  * dropped to the appropriate values.
18567  *
18568  * The mechanism to quiesce an ipif is as follows.
18569  *
18570  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
18571  * on the ipif. Callers either pass a flag requesting wait or the lookup
18572  *  functions will return NULL.
18573  *
18574  * Delete all ires referencing this ipif
18575  *
18576  * Any thread attempting to do an ipif_refhold on an ipif that has been
18577  * obtained thru a cached pointer will first make sure that
18578  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
18579  * increment the refcount.
18580  *
18581  * The above guarantees that the ipif refcount will eventually come down to
18582  * zero and the ipif will quiesce, once all threads that currently hold a
18583  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
18584  * ipif_refcount has dropped to zero and all ire's associated with this ipif
18585  * have also been ire_inactive'd. i.e. when ipif_{ire, ill}_cnt and
18586  * ipif_refcnt both drop to zero. See also: comments above IPIF_DOWN_OK()
18587  * in ip.h
18588  *
18589  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
18590  *
18591  * Threads trying to lookup an ipif or ill can pass a flag requesting
18592  * wait and restart if the ipif / ill cannot be looked up currently.
18593  * For eg. bind, and route operations (Eg. route add / delete) cannot return
18594  * failure if the ipif is currently undergoing an exclusive operation, and
18595  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
18596  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
18597  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
18598  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
18599  * change while the ill_lock is held. Before dropping the ill_lock we acquire
18600  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
18601  * until we release the ipsq_lock, even though the the ill/ipif state flags
18602  * can change after we drop the ill_lock.
18603  *
18604  * An attempt to send out a packet using an ipif that is currently
18605  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
18606  * operation and restart it later when the exclusive condition on the ipif ends.
18607  * This is an example of not passing the wait flag to the lookup functions. For
18608  * example an attempt to refhold and use conn->conn_multicast_ipif and send
18609  * out a multicast packet on that ipif will fail while the ipif is
18610  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
18611  * currently IPIF_CHANGING will also fail.
18612  */
18613 int
18614 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18615 {
18616 	ill_t		*ill = ipif->ipif_ill;
18617 	phyint_t	*phyi;
18618 	conn_t		*connp;
18619 	boolean_t	success;
18620 	boolean_t	ipif_was_up = B_FALSE;
18621 	ip_stack_t	*ipst = ill->ill_ipst;
18622 
18623 	ASSERT(IAM_WRITER_IPIF(ipif));
18624 
18625 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18626 
18627 	if (ipif->ipif_flags & IPIF_UP) {
18628 		mutex_enter(&ill->ill_lock);
18629 		ipif->ipif_flags &= ~IPIF_UP;
18630 		ASSERT(ill->ill_ipif_up_count > 0);
18631 		--ill->ill_ipif_up_count;
18632 		mutex_exit(&ill->ill_lock);
18633 		ipif_was_up = B_TRUE;
18634 		/* Update status in SCTP's list */
18635 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
18636 	}
18637 
18638 	/*
18639 	 * Blow away memberships we established in ipif_multicast_up().
18640 	 */
18641 	ipif_multicast_down(ipif);
18642 
18643 	/*
18644 	 * Remove from the mapping for __sin6_src_id. We insert only
18645 	 * when the address is not INADDR_ANY. As IPv4 addresses are
18646 	 * stored as mapped addresses, we need to check for mapped
18647 	 * INADDR_ANY also.
18648 	 */
18649 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
18650 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
18651 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
18652 		int err;
18653 
18654 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
18655 		    ipif->ipif_zoneid, ipst);
18656 		if (err != 0) {
18657 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
18658 		}
18659 	}
18660 
18661 	/*
18662 	 * Before we delete the ill from the group (if any), we need
18663 	 * to make sure that we delete all the routes dependent on
18664 	 * this and also any ipifs dependent on this ipif for
18665 	 * source address. We need to do before we delete from
18666 	 * the group because
18667 	 *
18668 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
18669 	 *
18670 	 * 2) ipif_update_other_ipifs needs to walk the whole group
18671 	 *    for re-doing source address selection. Note that
18672 	 *    ipif_select_source[_v6] called from
18673 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
18674 	 *    because we have already marked down here i.e cleared
18675 	 *    IPIF_UP.
18676 	 */
18677 	if (ipif->ipif_isv6) {
18678 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18679 		    ipst);
18680 	} else {
18681 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES,
18682 		    ipst);
18683 	}
18684 
18685 	/*
18686 	 * Cleaning up the conn_ire_cache or conns must be done only after the
18687 	 * ires have been deleted above. Otherwise a thread could end up
18688 	 * caching an ire in a conn after we have finished the cleanup of the
18689 	 * conn. The caching is done after making sure that the ire is not yet
18690 	 * condemned. Also documented in the block comment above ip_output
18691 	 */
18692 	ipcl_walk(conn_cleanup_stale_ire, NULL, ipst);
18693 	/* Also, delete the ires cached in SCTP */
18694 	sctp_ire_cache_flush(ipif);
18695 
18696 	/*
18697 	 * Update any other ipifs which have used "our" local address as
18698 	 * a source address. This entails removing and recreating IRE_INTERFACE
18699 	 * entries for such ipifs.
18700 	 */
18701 	if (ipif->ipif_isv6)
18702 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
18703 	else
18704 		ipif_update_other_ipifs(ipif, ill->ill_group);
18705 
18706 	if (ipif_was_up) {
18707 		/*
18708 		 * Check whether it is last ipif to leave this group.
18709 		 * If this is the last ipif to leave, we should remove
18710 		 * this ill from the group as ipif_select_source will not
18711 		 * be able to find any useful ipifs if this ill is selected
18712 		 * for load balancing.
18713 		 *
18714 		 * For nameless groups, we should call ifgrp_delete if this
18715 		 * belongs to some group. As this ipif is going down, we may
18716 		 * need to reconstruct groups.
18717 		 */
18718 		phyi = ill->ill_phyint;
18719 		/*
18720 		 * If the phyint_groupname_len is 0, it may or may not
18721 		 * be in the nameless group. If the phyint_groupname_len is
18722 		 * not 0, then this ill should be part of some group.
18723 		 * As we always insert this ill in the group if
18724 		 * phyint_groupname_len is not zero when the first ipif
18725 		 * comes up (in ipif_up_done), it should be in a group
18726 		 * when the namelen is not 0.
18727 		 *
18728 		 * NOTE : When we delete the ill from the group,it will
18729 		 * blow away all the IRE_CACHES pointing either at this ipif or
18730 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
18731 		 * should be pointing at this ill.
18732 		 */
18733 		ASSERT(phyi->phyint_groupname_len == 0 ||
18734 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
18735 
18736 		if (phyi->phyint_groupname_len != 0) {
18737 			if (ill->ill_ipif_up_count == 0)
18738 				illgrp_delete(ill);
18739 		}
18740 
18741 		/*
18742 		 * If we have deleted some of the broadcast ires associated
18743 		 * with this ipif, we need to re-nominate somebody else if
18744 		 * the ires that we deleted were the nominated ones.
18745 		 */
18746 		if (ill->ill_group != NULL && !ill->ill_isv6)
18747 			ipif_renominate_bcast(ipif);
18748 	}
18749 
18750 	/*
18751 	 * neighbor-discovery or arp entries for this interface.
18752 	 */
18753 	ipif_ndp_down(ipif);
18754 
18755 	/*
18756 	 * If mp is NULL the caller will wait for the appropriate refcnt.
18757 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
18758 	 * and ill_delete -> ipif_free -> ipif_down
18759 	 */
18760 	if (mp == NULL) {
18761 		ASSERT(q == NULL);
18762 		return (0);
18763 	}
18764 
18765 	if (CONN_Q(q)) {
18766 		connp = Q_TO_CONN(q);
18767 		mutex_enter(&connp->conn_lock);
18768 	} else {
18769 		connp = NULL;
18770 	}
18771 	mutex_enter(&ill->ill_lock);
18772 	/*
18773 	 * Are there any ire's pointing to this ipif that are still active ?
18774 	 * If this is the last ipif going down, are there any ire's pointing
18775 	 * to this ill that are still active ?
18776 	 */
18777 	if (ipif_is_quiescent(ipif)) {
18778 		mutex_exit(&ill->ill_lock);
18779 		if (connp != NULL)
18780 			mutex_exit(&connp->conn_lock);
18781 		return (0);
18782 	}
18783 
18784 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
18785 	    ill->ill_name, (void *)ill));
18786 	/*
18787 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
18788 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
18789 	 * which in turn is called by the last refrele on the ipif/ill/ire.
18790 	 */
18791 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
18792 	if (!success) {
18793 		/* The conn is closing. So just return */
18794 		ASSERT(connp != NULL);
18795 		mutex_exit(&ill->ill_lock);
18796 		mutex_exit(&connp->conn_lock);
18797 		return (EINTR);
18798 	}
18799 
18800 	mutex_exit(&ill->ill_lock);
18801 	if (connp != NULL)
18802 		mutex_exit(&connp->conn_lock);
18803 	return (EINPROGRESS);
18804 }
18805 
18806 void
18807 ipif_down_tail(ipif_t *ipif)
18808 {
18809 	ill_t	*ill = ipif->ipif_ill;
18810 
18811 	/*
18812 	 * Skip any loopback interface (null wq).
18813 	 * If this is the last logical interface on the ill
18814 	 * have ill_dl_down tell the driver we are gone (unbind)
18815 	 * Note that lun 0 can ipif_down even though
18816 	 * there are other logical units that are up.
18817 	 * This occurs e.g. when we change a "significant" IFF_ flag.
18818 	 */
18819 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
18820 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
18821 	    ill->ill_dl_up) {
18822 		ill_dl_down(ill);
18823 	}
18824 	ill->ill_logical_down = 0;
18825 
18826 	/*
18827 	 * Have to be after removing the routes in ipif_down_delete_ire.
18828 	 */
18829 	if (ipif->ipif_isv6) {
18830 		if (ill->ill_flags & ILLF_XRESOLV)
18831 			ipif_arp_down(ipif);
18832 	} else {
18833 		ipif_arp_down(ipif);
18834 	}
18835 
18836 	ip_rts_ifmsg(ipif);
18837 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
18838 }
18839 
18840 /*
18841  * Bring interface logically down without bringing the physical interface
18842  * down e.g. when the netmask is changed. This avoids long lasting link
18843  * negotiations between an ethernet interface and a certain switches.
18844  */
18845 static int
18846 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
18847 {
18848 	/*
18849 	 * The ill_logical_down flag is a transient flag. It is set here
18850 	 * and is cleared once the down has completed in ipif_down_tail.
18851 	 * This flag does not indicate whether the ill stream is in the
18852 	 * DL_BOUND state with the driver. Instead this flag is used by
18853 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
18854 	 * the driver. The state of the ill stream i.e. whether it is
18855 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
18856 	 */
18857 	ipif->ipif_ill->ill_logical_down = 1;
18858 	return (ipif_down(ipif, q, mp));
18859 }
18860 
18861 /*
18862  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
18863  * If the usesrc client ILL is already part of a usesrc group or not,
18864  * in either case a ire_stq with the matching usesrc client ILL will
18865  * locate the IRE's that need to be deleted. We want IREs to be created
18866  * with the new source address.
18867  */
18868 static void
18869 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
18870 {
18871 	ill_t	*ucill = (ill_t *)ill_arg;
18872 
18873 	ASSERT(IAM_WRITER_ILL(ucill));
18874 
18875 	if (ire->ire_stq == NULL)
18876 		return;
18877 
18878 	if ((ire->ire_type == IRE_CACHE) &&
18879 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
18880 		ire_delete(ire);
18881 }
18882 
18883 /*
18884  * ire_walk routine to delete every IRE dependent on the interface
18885  * address that is going down.	(Always called as writer.)
18886  * Works for both v4 and v6.
18887  * In addition for checking for ire_ipif matches it also checks for
18888  * IRE_CACHE entries which have the same source address as the
18889  * disappearing ipif since ipif_select_source might have picked
18890  * that source. Note that ipif_down/ipif_update_other_ipifs takes
18891  * care of any IRE_INTERFACE with the disappearing source address.
18892  */
18893 static void
18894 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
18895 {
18896 	ipif_t	*ipif = (ipif_t *)ipif_arg;
18897 	ill_t *ire_ill;
18898 	ill_t *ipif_ill;
18899 
18900 	ASSERT(IAM_WRITER_IPIF(ipif));
18901 	if (ire->ire_ipif == NULL)
18902 		return;
18903 
18904 	/*
18905 	 * For IPv4, we derive source addresses for an IRE from ipif's
18906 	 * belonging to the same IPMP group as the IRE's outgoing
18907 	 * interface.  If an IRE's outgoing interface isn't in the
18908 	 * same IPMP group as a particular ipif, then that ipif
18909 	 * couldn't have been used as a source address for this IRE.
18910 	 *
18911 	 * For IPv6, source addresses are only restricted to the IPMP group
18912 	 * if the IRE is for a link-local address or a multicast address.
18913 	 * Otherwise, source addresses for an IRE can be chosen from
18914 	 * interfaces other than the the outgoing interface for that IRE.
18915 	 *
18916 	 * For source address selection details, see ipif_select_source()
18917 	 * and ipif_select_source_v6().
18918 	 */
18919 	if (ire->ire_ipversion == IPV4_VERSION ||
18920 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
18921 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
18922 		ire_ill = ire->ire_ipif->ipif_ill;
18923 		ipif_ill = ipif->ipif_ill;
18924 
18925 		if (ire_ill->ill_group != ipif_ill->ill_group) {
18926 			return;
18927 		}
18928 	}
18929 
18930 
18931 	if (ire->ire_ipif != ipif) {
18932 		/*
18933 		 * Look for a matching source address.
18934 		 */
18935 		if (ire->ire_type != IRE_CACHE)
18936 			return;
18937 		if (ipif->ipif_flags & IPIF_NOLOCAL)
18938 			return;
18939 
18940 		if (ire->ire_ipversion == IPV4_VERSION) {
18941 			if (ire->ire_src_addr != ipif->ipif_src_addr)
18942 				return;
18943 		} else {
18944 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
18945 			    &ipif->ipif_v6lcl_addr))
18946 				return;
18947 		}
18948 		ire_delete(ire);
18949 		return;
18950 	}
18951 	/*
18952 	 * ire_delete() will do an ire_flush_cache which will delete
18953 	 * all ire_ipif matches
18954 	 */
18955 	ire_delete(ire);
18956 }
18957 
18958 /*
18959  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
18960  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
18961  * 2) when an interface is brought up or down (on that ill).
18962  * This ensures that the IRE_CACHE entries don't retain stale source
18963  * address selection results.
18964  */
18965 void
18966 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
18967 {
18968 	ill_t	*ill = (ill_t *)ill_arg;
18969 	ill_t	*ipif_ill;
18970 
18971 	ASSERT(IAM_WRITER_ILL(ill));
18972 	/*
18973 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
18974 	 * Hence this should be IRE_CACHE.
18975 	 */
18976 	ASSERT(ire->ire_type == IRE_CACHE);
18977 
18978 	/*
18979 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
18980 	 * We are only interested in IRE_CACHES that has borrowed
18981 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
18982 	 * for which we need to look at ire_ipif->ipif_ill match
18983 	 * with ill.
18984 	 */
18985 	ASSERT(ire->ire_ipif != NULL);
18986 	ipif_ill = ire->ire_ipif->ipif_ill;
18987 	if (ipif_ill == ill || (ill->ill_group != NULL &&
18988 	    ipif_ill->ill_group == ill->ill_group)) {
18989 		ire_delete(ire);
18990 	}
18991 }
18992 
18993 /*
18994  * Delete all the ire whose stq references ill_arg.
18995  */
18996 static void
18997 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
18998 {
18999 	ill_t	*ill = (ill_t *)ill_arg;
19000 	ill_t	*ire_ill;
19001 
19002 	ASSERT(IAM_WRITER_ILL(ill));
19003 	/*
19004 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19005 	 * Hence this should be IRE_CACHE.
19006 	 */
19007 	ASSERT(ire->ire_type == IRE_CACHE);
19008 
19009 	/*
19010 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19011 	 * matches ill. We are only interested in IRE_CACHES that
19012 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
19013 	 * filtering here.
19014 	 */
19015 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
19016 
19017 	if (ire_ill == ill)
19018 		ire_delete(ire);
19019 }
19020 
19021 /*
19022  * This is called when an ill leaves the group. We want to delete
19023  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
19024  * pointing at ill.
19025  */
19026 static void
19027 illgrp_cache_delete(ire_t *ire, char *ill_arg)
19028 {
19029 	ill_t	*ill = (ill_t *)ill_arg;
19030 
19031 	ASSERT(IAM_WRITER_ILL(ill));
19032 	ASSERT(ill->ill_group == NULL);
19033 	/*
19034 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
19035 	 * Hence this should be IRE_CACHE.
19036 	 */
19037 	ASSERT(ire->ire_type == IRE_CACHE);
19038 	/*
19039 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
19040 	 * matches ill. We are interested in both.
19041 	 */
19042 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
19043 	    (ire->ire_ipif->ipif_ill == ill));
19044 
19045 	ire_delete(ire);
19046 }
19047 
19048 /*
19049  * Initiate deallocate of an IPIF. Always called as writer. Called by
19050  * ill_delete or ip_sioctl_removeif.
19051  */
19052 static void
19053 ipif_free(ipif_t *ipif)
19054 {
19055 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19056 
19057 	ASSERT(IAM_WRITER_IPIF(ipif));
19058 
19059 	if (ipif->ipif_recovery_id != 0)
19060 		(void) untimeout(ipif->ipif_recovery_id);
19061 	ipif->ipif_recovery_id = 0;
19062 
19063 	/* Remove conn references */
19064 	reset_conn_ipif(ipif);
19065 
19066 	/*
19067 	 * Make sure we have valid net and subnet broadcast ire's for the
19068 	 * other ipif's which share them with this ipif.
19069 	 */
19070 	if (!ipif->ipif_isv6)
19071 		ipif_check_bcast_ires(ipif);
19072 
19073 	/*
19074 	 * Take down the interface. We can be called either from ill_delete
19075 	 * or from ip_sioctl_removeif.
19076 	 */
19077 	(void) ipif_down(ipif, NULL, NULL);
19078 
19079 	/*
19080 	 * Now that the interface is down, there's no chance it can still
19081 	 * become a duplicate.  Cancel any timer that may have been set while
19082 	 * tearing down.
19083 	 */
19084 	if (ipif->ipif_recovery_id != 0)
19085 		(void) untimeout(ipif->ipif_recovery_id);
19086 	ipif->ipif_recovery_id = 0;
19087 
19088 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19089 	/* Remove pointers to this ill in the multicast routing tables */
19090 	reset_mrt_vif_ipif(ipif);
19091 	rw_exit(&ipst->ips_ill_g_lock);
19092 }
19093 
19094 /*
19095  * Warning: this is not the only function that calls mi_free on an ipif_t.  See
19096  * also ill_move().
19097  */
19098 static void
19099 ipif_free_tail(ipif_t *ipif)
19100 {
19101 	mblk_t	*mp;
19102 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
19103 
19104 	/*
19105 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
19106 	 */
19107 	mutex_enter(&ipif->ipif_saved_ire_lock);
19108 	mp = ipif->ipif_saved_ire_mp;
19109 	ipif->ipif_saved_ire_mp = NULL;
19110 	mutex_exit(&ipif->ipif_saved_ire_lock);
19111 	freemsg(mp);
19112 
19113 	/*
19114 	 * Need to hold both ill_g_lock and ill_lock while
19115 	 * inserting or removing an ipif from the linked list
19116 	 * of ipifs hanging off the ill.
19117 	 */
19118 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
19119 
19120 	ASSERT(ilm_walk_ipif(ipif) == 0);
19121 
19122 #ifdef DEBUG
19123 	ipif_trace_cleanup(ipif);
19124 #endif
19125 
19126 	/* Ask SCTP to take it out of it list */
19127 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
19128 
19129 	/* Get it out of the ILL interface list. */
19130 	ipif_remove(ipif, B_TRUE);
19131 	rw_exit(&ipst->ips_ill_g_lock);
19132 
19133 	mutex_destroy(&ipif->ipif_saved_ire_lock);
19134 
19135 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
19136 	ASSERT(ipif->ipif_recovery_id == 0);
19137 
19138 	/* Free the memory. */
19139 	mi_free(ipif);
19140 }
19141 
19142 /*
19143  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
19144  * is zero.
19145  */
19146 void
19147 ipif_get_name(const ipif_t *ipif, char *buf, int len)
19148 {
19149 	char	lbuf[LIFNAMSIZ];
19150 	char	*name;
19151 	size_t	name_len;
19152 
19153 	buf[0] = '\0';
19154 	name = ipif->ipif_ill->ill_name;
19155 	name_len = ipif->ipif_ill->ill_name_length;
19156 	if (ipif->ipif_id != 0) {
19157 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
19158 		    ipif->ipif_id);
19159 		name = lbuf;
19160 		name_len = mi_strlen(name) + 1;
19161 	}
19162 	len -= 1;
19163 	buf[len] = '\0';
19164 	len = MIN(len, name_len);
19165 	bcopy(name, buf, len);
19166 }
19167 
19168 /*
19169  * Find an IPIF based on the name passed in.  Names can be of the
19170  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
19171  * The <phys> string can have forms like <dev><#> (e.g., le0),
19172  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
19173  * When there is no colon, the implied unit id is zero. <phys> must
19174  * correspond to the name of an ILL.  (May be called as writer.)
19175  */
19176 static ipif_t *
19177 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
19178     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
19179     mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst)
19180 {
19181 	char	*cp;
19182 	char	*endp;
19183 	long	id;
19184 	ill_t	*ill;
19185 	ipif_t	*ipif;
19186 	uint_t	ire_type;
19187 	boolean_t did_alloc = B_FALSE;
19188 	ipsq_t	*ipsq;
19189 
19190 	if (error != NULL)
19191 		*error = 0;
19192 
19193 	/*
19194 	 * If the caller wants to us to create the ipif, make sure we have a
19195 	 * valid zoneid
19196 	 */
19197 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
19198 
19199 	if (namelen == 0) {
19200 		if (error != NULL)
19201 			*error = ENXIO;
19202 		return (NULL);
19203 	}
19204 
19205 	*exists = B_FALSE;
19206 	/* Look for a colon in the name. */
19207 	endp = &name[namelen];
19208 	for (cp = endp; --cp > name; ) {
19209 		if (*cp == IPIF_SEPARATOR_CHAR)
19210 			break;
19211 	}
19212 
19213 	if (*cp == IPIF_SEPARATOR_CHAR) {
19214 		/*
19215 		 * Reject any non-decimal aliases for logical
19216 		 * interfaces. Aliases with leading zeroes
19217 		 * are also rejected as they introduce ambiguity
19218 		 * in the naming of the interfaces.
19219 		 * In order to confirm with existing semantics,
19220 		 * and to not break any programs/script relying
19221 		 * on that behaviour, if<0>:0 is considered to be
19222 		 * a valid interface.
19223 		 *
19224 		 * If alias has two or more digits and the first
19225 		 * is zero, fail.
19226 		 */
19227 		if (&cp[2] < endp && cp[1] == '0') {
19228 			if (error != NULL)
19229 				*error = EINVAL;
19230 			return (NULL);
19231 		}
19232 	}
19233 
19234 	if (cp <= name) {
19235 		cp = endp;
19236 	} else {
19237 		*cp = '\0';
19238 	}
19239 
19240 	/*
19241 	 * Look up the ILL, based on the portion of the name
19242 	 * before the slash. ill_lookup_on_name returns a held ill.
19243 	 * Temporary to check whether ill exists already. If so
19244 	 * ill_lookup_on_name will clear it.
19245 	 */
19246 	ill = ill_lookup_on_name(name, do_alloc, isv6,
19247 	    q, mp, func, error, &did_alloc, ipst);
19248 	if (cp != endp)
19249 		*cp = IPIF_SEPARATOR_CHAR;
19250 	if (ill == NULL)
19251 		return (NULL);
19252 
19253 	/* Establish the unit number in the name. */
19254 	id = 0;
19255 	if (cp < endp && *endp == '\0') {
19256 		/* If there was a colon, the unit number follows. */
19257 		cp++;
19258 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
19259 			ill_refrele(ill);
19260 			if (error != NULL)
19261 				*error = ENXIO;
19262 			return (NULL);
19263 		}
19264 	}
19265 
19266 	GRAB_CONN_LOCK(q);
19267 	mutex_enter(&ill->ill_lock);
19268 	/* Now see if there is an IPIF with this unit number. */
19269 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
19270 		if (ipif->ipif_id == id) {
19271 			if (zoneid != ALL_ZONES &&
19272 			    zoneid != ipif->ipif_zoneid &&
19273 			    ipif->ipif_zoneid != ALL_ZONES) {
19274 				mutex_exit(&ill->ill_lock);
19275 				RELEASE_CONN_LOCK(q);
19276 				ill_refrele(ill);
19277 				if (error != NULL)
19278 					*error = ENXIO;
19279 				return (NULL);
19280 			}
19281 			/*
19282 			 * The block comment at the start of ipif_down
19283 			 * explains the use of the macros used below
19284 			 */
19285 			if (IPIF_CAN_LOOKUP(ipif)) {
19286 				ipif_refhold_locked(ipif);
19287 				mutex_exit(&ill->ill_lock);
19288 				if (!did_alloc)
19289 					*exists = B_TRUE;
19290 				/*
19291 				 * Drop locks before calling ill_refrele
19292 				 * since it can potentially call into
19293 				 * ipif_ill_refrele_tail which can end up
19294 				 * in trying to acquire any lock.
19295 				 */
19296 				RELEASE_CONN_LOCK(q);
19297 				ill_refrele(ill);
19298 				return (ipif);
19299 			} else if (IPIF_CAN_WAIT(ipif, q)) {
19300 				ipsq = ill->ill_phyint->phyint_ipsq;
19301 				mutex_enter(&ipsq->ipsq_lock);
19302 				mutex_exit(&ill->ill_lock);
19303 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
19304 				mutex_exit(&ipsq->ipsq_lock);
19305 				RELEASE_CONN_LOCK(q);
19306 				ill_refrele(ill);
19307 				if (error != NULL)
19308 					*error = EINPROGRESS;
19309 				return (NULL);
19310 			}
19311 		}
19312 	}
19313 	RELEASE_CONN_LOCK(q);
19314 
19315 	if (!do_alloc) {
19316 		mutex_exit(&ill->ill_lock);
19317 		ill_refrele(ill);
19318 		if (error != NULL)
19319 			*error = ENXIO;
19320 		return (NULL);
19321 	}
19322 
19323 	/*
19324 	 * If none found, atomically allocate and return a new one.
19325 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
19326 	 * to support "receive only" use of lo0:1 etc. as is still done
19327 	 * below as an initial guess.
19328 	 * However, this is now likely to be overriden later in ipif_up_done()
19329 	 * when we know for sure what address has been configured on the
19330 	 * interface, since we might have more than one loopback interface
19331 	 * with a loopback address, e.g. in the case of zones, and all the
19332 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
19333 	 */
19334 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
19335 		ire_type = IRE_LOOPBACK;
19336 	else
19337 		ire_type = IRE_LOCAL;
19338 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
19339 	if (ipif != NULL)
19340 		ipif_refhold_locked(ipif);
19341 	else if (error != NULL)
19342 		*error = ENOMEM;
19343 	mutex_exit(&ill->ill_lock);
19344 	ill_refrele(ill);
19345 	return (ipif);
19346 }
19347 
19348 /*
19349  * This routine is called whenever a new address comes up on an ipif.  If
19350  * we are configured to respond to address mask requests, then we are supposed
19351  * to broadcast an address mask reply at this time.  This routine is also
19352  * called if we are already up, but a netmask change is made.  This is legal
19353  * but might not make the system manager very popular.	(May be called
19354  * as writer.)
19355  */
19356 void
19357 ipif_mask_reply(ipif_t *ipif)
19358 {
19359 	icmph_t	*icmph;
19360 	ipha_t	*ipha;
19361 	mblk_t	*mp;
19362 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19363 
19364 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
19365 
19366 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
19367 		return;
19368 
19369 	/* ICMP mask reply is IPv4 only */
19370 	ASSERT(!ipif->ipif_isv6);
19371 	/* ICMP mask reply is not for a loopback interface */
19372 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
19373 
19374 	mp = allocb(REPLY_LEN, BPRI_HI);
19375 	if (mp == NULL)
19376 		return;
19377 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
19378 
19379 	ipha = (ipha_t *)mp->b_rptr;
19380 	bzero(ipha, REPLY_LEN);
19381 	*ipha = icmp_ipha;
19382 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
19383 	ipha->ipha_src = ipif->ipif_src_addr;
19384 	ipha->ipha_dst = ipif->ipif_brd_addr;
19385 	ipha->ipha_length = htons(REPLY_LEN);
19386 	ipha->ipha_ident = 0;
19387 
19388 	icmph = (icmph_t *)&ipha[1];
19389 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
19390 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
19391 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
19392 
19393 	put(ipif->ipif_wq, mp);
19394 
19395 #undef	REPLY_LEN
19396 }
19397 
19398 /*
19399  * When the mtu in the ipif changes, we call this routine through ire_walk
19400  * to update all the relevant IREs.
19401  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19402  */
19403 static void
19404 ipif_mtu_change(ire_t *ire, char *ipif_arg)
19405 {
19406 	ipif_t *ipif = (ipif_t *)ipif_arg;
19407 
19408 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
19409 		return;
19410 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
19411 }
19412 
19413 /*
19414  * When the mtu in the ill changes, we call this routine through ire_walk
19415  * to update all the relevant IREs.
19416  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
19417  */
19418 void
19419 ill_mtu_change(ire_t *ire, char *ill_arg)
19420 {
19421 	ill_t	*ill = (ill_t *)ill_arg;
19422 
19423 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
19424 		return;
19425 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
19426 }
19427 
19428 /*
19429  * Join the ipif specific multicast groups.
19430  * Must be called after a mapping has been set up in the resolver.  (Always
19431  * called as writer.)
19432  */
19433 void
19434 ipif_multicast_up(ipif_t *ipif)
19435 {
19436 	int err, index;
19437 	ill_t *ill;
19438 
19439 	ASSERT(IAM_WRITER_IPIF(ipif));
19440 
19441 	ill = ipif->ipif_ill;
19442 	index = ill->ill_phyint->phyint_ifindex;
19443 
19444 	ip1dbg(("ipif_multicast_up\n"));
19445 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
19446 		return;
19447 
19448 	if (ipif->ipif_isv6) {
19449 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
19450 			return;
19451 
19452 		/* Join the all hosts multicast address */
19453 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19454 		/*
19455 		 * Passing B_TRUE means we have to join the multicast
19456 		 * membership on this interface even though this is
19457 		 * FAILED. If we join on a different one in the group,
19458 		 * we will not be able to delete the membership later
19459 		 * as we currently don't track where we join when we
19460 		 * join within the kernel unlike applications where
19461 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
19462 		 * for more on this.
19463 		 */
19464 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
19465 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19466 		if (err != 0) {
19467 			ip0dbg(("ipif_multicast_up: "
19468 			    "all_hosts_mcast failed %d\n",
19469 			    err));
19470 			return;
19471 		}
19472 		/*
19473 		 * Enable multicast for the solicited node multicast address
19474 		 */
19475 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19476 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19477 
19478 			ipv6_multi.s6_addr32[3] |=
19479 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
19480 
19481 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
19482 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
19483 			    NULL);
19484 			if (err != 0) {
19485 				ip0dbg(("ipif_multicast_up: solicited MC"
19486 				    " failed %d\n", err));
19487 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
19488 				    ill, ill->ill_phyint->phyint_ifindex,
19489 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19490 				return;
19491 			}
19492 		}
19493 	} else {
19494 		if (ipif->ipif_lcl_addr == INADDR_ANY)
19495 			return;
19496 
19497 		/* Join the all hosts multicast address */
19498 		ip1dbg(("ipif_multicast_up - addmulti\n"));
19499 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
19500 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
19501 		if (err) {
19502 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
19503 			return;
19504 		}
19505 	}
19506 	ipif->ipif_multicast_up = 1;
19507 }
19508 
19509 /*
19510  * Blow away any multicast groups that we joined in ipif_multicast_up().
19511  * (Explicit memberships are blown away in ill_leave_multicast() when the
19512  * ill is brought down.)
19513  */
19514 static void
19515 ipif_multicast_down(ipif_t *ipif)
19516 {
19517 	int err;
19518 
19519 	ASSERT(IAM_WRITER_IPIF(ipif));
19520 
19521 	ip1dbg(("ipif_multicast_down\n"));
19522 	if (!ipif->ipif_multicast_up)
19523 		return;
19524 
19525 	ip1dbg(("ipif_multicast_down - delmulti\n"));
19526 
19527 	if (!ipif->ipif_isv6) {
19528 		err = ip_delmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, B_TRUE,
19529 		    B_TRUE);
19530 		if (err != 0)
19531 			ip0dbg(("ipif_multicast_down: failed %d\n", err));
19532 
19533 		ipif->ipif_multicast_up = 0;
19534 		return;
19535 	}
19536 
19537 	/*
19538 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
19539 	 * we should look for ilms on this ill rather than the ones that have
19540 	 * been failed over here.  They are here temporarily. As
19541 	 * ipif_multicast_up has joined on this ill, we should delete only
19542 	 * from this ill.
19543 	 */
19544 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
19545 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
19546 	    B_TRUE, B_TRUE);
19547 	if (err != 0) {
19548 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
19549 		    err));
19550 	}
19551 	/*
19552 	 * Disable multicast for the solicited node multicast address
19553 	 */
19554 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
19555 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
19556 
19557 		ipv6_multi.s6_addr32[3] |=
19558 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
19559 
19560 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
19561 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
19562 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
19563 
19564 		if (err != 0) {
19565 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
19566 			    err));
19567 		}
19568 	}
19569 
19570 	ipif->ipif_multicast_up = 0;
19571 }
19572 
19573 /*
19574  * Used when an interface comes up to recreate any extra routes on this
19575  * interface.
19576  */
19577 static ire_t **
19578 ipif_recover_ire(ipif_t *ipif)
19579 {
19580 	mblk_t	*mp;
19581 	ire_t	**ipif_saved_irep;
19582 	ire_t	**irep;
19583 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
19584 
19585 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
19586 	    ipif->ipif_id));
19587 
19588 	mutex_enter(&ipif->ipif_saved_ire_lock);
19589 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
19590 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
19591 	if (ipif_saved_irep == NULL) {
19592 		mutex_exit(&ipif->ipif_saved_ire_lock);
19593 		return (NULL);
19594 	}
19595 
19596 	irep = ipif_saved_irep;
19597 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
19598 		ire_t		*ire;
19599 		queue_t		*rfq;
19600 		queue_t		*stq;
19601 		ifrt_t		*ifrt;
19602 		uchar_t		*src_addr;
19603 		uchar_t		*gateway_addr;
19604 		ushort_t	type;
19605 
19606 		/*
19607 		 * When the ire was initially created and then added in
19608 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
19609 		 * in the case of a traditional interface route, or as one of
19610 		 * the IRE_OFFSUBNET types (with the exception of
19611 		 * IRE_HOST types ire which is created by icmp_redirect() and
19612 		 * which we don't need to save or recover).  In the case where
19613 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
19614 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
19615 		 * to satisfy software like GateD and Sun Cluster which creates
19616 		 * routes using the the loopback interface's address as a
19617 		 * gateway.
19618 		 *
19619 		 * As ifrt->ifrt_type reflects the already updated ire_type,
19620 		 * ire_create() will be called in the same way here as
19621 		 * in ip_rt_add(), namely using ipif->ipif_net_type when
19622 		 * the route looks like a traditional interface route (where
19623 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
19624 		 * the saved ifrt->ifrt_type.  This means that in the case where
19625 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
19626 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
19627 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
19628 		 */
19629 		ifrt = (ifrt_t *)mp->b_rptr;
19630 		ASSERT(ifrt->ifrt_type != IRE_CACHE);
19631 		if (ifrt->ifrt_type & IRE_INTERFACE) {
19632 			rfq = NULL;
19633 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
19634 			    ? ipif->ipif_rq : ipif->ipif_wq;
19635 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19636 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19637 			    : (uint8_t *)&ipif->ipif_src_addr;
19638 			gateway_addr = NULL;
19639 			type = ipif->ipif_net_type;
19640 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
19641 			/* Recover multiroute broadcast IRE. */
19642 			rfq = ipif->ipif_rq;
19643 			stq = ipif->ipif_wq;
19644 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19645 			    ? (uint8_t *)&ifrt->ifrt_src_addr
19646 			    : (uint8_t *)&ipif->ipif_src_addr;
19647 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19648 			type = ifrt->ifrt_type;
19649 		} else {
19650 			rfq = NULL;
19651 			stq = NULL;
19652 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
19653 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
19654 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
19655 			type = ifrt->ifrt_type;
19656 		}
19657 
19658 		/*
19659 		 * Create a copy of the IRE with the saved address and netmask.
19660 		 */
19661 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
19662 		    "0x%x/0x%x\n",
19663 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
19664 		    ntohl(ifrt->ifrt_addr),
19665 		    ntohl(ifrt->ifrt_mask)));
19666 		ire = ire_create(
19667 		    (uint8_t *)&ifrt->ifrt_addr,
19668 		    (uint8_t *)&ifrt->ifrt_mask,
19669 		    src_addr,
19670 		    gateway_addr,
19671 		    &ifrt->ifrt_max_frag,
19672 		    NULL,
19673 		    rfq,
19674 		    stq,
19675 		    type,
19676 		    ipif,
19677 		    0,
19678 		    0,
19679 		    0,
19680 		    ifrt->ifrt_flags,
19681 		    &ifrt->ifrt_iulp_info,
19682 		    NULL,
19683 		    NULL,
19684 		    ipst);
19685 
19686 		if (ire == NULL) {
19687 			mutex_exit(&ipif->ipif_saved_ire_lock);
19688 			kmem_free(ipif_saved_irep,
19689 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
19690 			return (NULL);
19691 		}
19692 
19693 		/*
19694 		 * Some software (for example, GateD and Sun Cluster) attempts
19695 		 * to create (what amount to) IRE_PREFIX routes with the
19696 		 * loopback address as the gateway.  This is primarily done to
19697 		 * set up prefixes with the RTF_REJECT flag set (for example,
19698 		 * when generating aggregate routes.)
19699 		 *
19700 		 * If the IRE type (as defined by ipif->ipif_net_type) is
19701 		 * IRE_LOOPBACK, then we map the request into a
19702 		 * IRE_IF_NORESOLVER.
19703 		 */
19704 		if (ipif->ipif_net_type == IRE_LOOPBACK)
19705 			ire->ire_type = IRE_IF_NORESOLVER;
19706 		/*
19707 		 * ire held by ire_add, will be refreled' towards the
19708 		 * the end of ipif_up_done
19709 		 */
19710 		(void) ire_add(&ire, NULL, NULL, NULL, B_FALSE);
19711 		*irep = ire;
19712 		irep++;
19713 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
19714 	}
19715 	mutex_exit(&ipif->ipif_saved_ire_lock);
19716 	return (ipif_saved_irep);
19717 }
19718 
19719 /*
19720  * Used to set the netmask and broadcast address to default values when the
19721  * interface is brought up.  (Always called as writer.)
19722  */
19723 static void
19724 ipif_set_default(ipif_t *ipif)
19725 {
19726 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19727 
19728 	if (!ipif->ipif_isv6) {
19729 		/*
19730 		 * Interface holds an IPv4 address. Default
19731 		 * mask is the natural netmask.
19732 		 */
19733 		if (!ipif->ipif_net_mask) {
19734 			ipaddr_t	v4mask;
19735 
19736 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
19737 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
19738 		}
19739 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19740 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19741 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19742 		} else {
19743 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19744 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19745 		}
19746 		/*
19747 		 * NOTE: SunOS 4.X does this even if the broadcast address
19748 		 * has been already set thus we do the same here.
19749 		 */
19750 		if (ipif->ipif_flags & IPIF_BROADCAST) {
19751 			ipaddr_t	v4addr;
19752 
19753 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
19754 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
19755 		}
19756 	} else {
19757 		/*
19758 		 * Interface holds an IPv6-only address.  Default
19759 		 * mask is all-ones.
19760 		 */
19761 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
19762 			ipif->ipif_v6net_mask = ipv6_all_ones;
19763 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19764 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19765 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
19766 		} else {
19767 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
19768 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
19769 		}
19770 	}
19771 }
19772 
19773 /*
19774  * Return 0 if this address can be used as local address without causing
19775  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
19776  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
19777  * Special checks are needed to allow the same IPv6 link-local address
19778  * on different ills.
19779  * TODO: allowing the same site-local address on different ill's.
19780  */
19781 int
19782 ip_addr_availability_check(ipif_t *new_ipif)
19783 {
19784 	in6_addr_t our_v6addr;
19785 	ill_t *ill;
19786 	ipif_t *ipif;
19787 	ill_walk_context_t ctx;
19788 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
19789 
19790 	ASSERT(IAM_WRITER_IPIF(new_ipif));
19791 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
19792 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
19793 
19794 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
19795 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
19796 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
19797 		return (0);
19798 
19799 	our_v6addr = new_ipif->ipif_v6lcl_addr;
19800 
19801 	if (new_ipif->ipif_isv6)
19802 		ill = ILL_START_WALK_V6(&ctx, ipst);
19803 	else
19804 		ill = ILL_START_WALK_V4(&ctx, ipst);
19805 
19806 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
19807 		for (ipif = ill->ill_ipif; ipif != NULL;
19808 		    ipif = ipif->ipif_next) {
19809 			if ((ipif == new_ipif) ||
19810 			    !(ipif->ipif_flags & IPIF_UP) ||
19811 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
19812 				continue;
19813 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
19814 			    &our_v6addr)) {
19815 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
19816 					new_ipif->ipif_flags |= IPIF_UNNUMBERED;
19817 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
19818 					ipif->ipif_flags |= IPIF_UNNUMBERED;
19819 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
19820 				    new_ipif->ipif_ill != ill)
19821 					continue;
19822 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
19823 				    new_ipif->ipif_ill != ill)
19824 					continue;
19825 				else if (new_ipif->ipif_zoneid !=
19826 				    ipif->ipif_zoneid &&
19827 				    ipif->ipif_zoneid != ALL_ZONES &&
19828 				    IS_LOOPBACK(ill))
19829 					continue;
19830 				else if (new_ipif->ipif_ill == ill)
19831 					return (EADDRINUSE);
19832 				else
19833 					return (EADDRNOTAVAIL);
19834 			}
19835 		}
19836 	}
19837 
19838 	return (0);
19839 }
19840 
19841 /*
19842  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
19843  * IREs for the ipif.
19844  * When the routine returns EINPROGRESS then mp has been consumed and
19845  * the ioctl will be acked from ip_rput_dlpi.
19846  */
19847 static int
19848 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
19849 {
19850 	ill_t	*ill = ipif->ipif_ill;
19851 	boolean_t isv6 = ipif->ipif_isv6;
19852 	int	err = 0;
19853 	boolean_t success;
19854 
19855 	ASSERT(IAM_WRITER_IPIF(ipif));
19856 
19857 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
19858 
19859 	/* Shouldn't get here if it is already up. */
19860 	if (ipif->ipif_flags & IPIF_UP)
19861 		return (EALREADY);
19862 
19863 	/* Skip arp/ndp for any loopback interface. */
19864 	if (ill->ill_wq != NULL) {
19865 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
19866 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
19867 
19868 		if (!ill->ill_dl_up) {
19869 			/*
19870 			 * ill_dl_up is not yet set. i.e. we are yet to
19871 			 * DL_BIND with the driver and this is the first
19872 			 * logical interface on the ill to become "up".
19873 			 * Tell the driver to get going (via DL_BIND_REQ).
19874 			 * Note that changing "significant" IFF_ flags
19875 			 * address/netmask etc cause a down/up dance, but
19876 			 * does not cause an unbind (DL_UNBIND) with the driver
19877 			 */
19878 			return (ill_dl_up(ill, ipif, mp, q));
19879 		}
19880 
19881 		/*
19882 		 * ipif_resolver_up may end up sending an
19883 		 * AR_INTERFACE_UP message to ARP, which would, in
19884 		 * turn send a DLPI message to the driver. ioctls are
19885 		 * serialized and so we cannot send more than one
19886 		 * interface up message at a time. If ipif_resolver_up
19887 		 * does send an interface up message to ARP, we get
19888 		 * EINPROGRESS and we will complete in ip_arp_done.
19889 		 */
19890 
19891 		ASSERT(connp != NULL || !CONN_Q(q));
19892 		ASSERT(ipsq->ipsq_pending_mp == NULL);
19893 		if (connp != NULL)
19894 			mutex_enter(&connp->conn_lock);
19895 		mutex_enter(&ill->ill_lock);
19896 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19897 		mutex_exit(&ill->ill_lock);
19898 		if (connp != NULL)
19899 			mutex_exit(&connp->conn_lock);
19900 		if (!success)
19901 			return (EINTR);
19902 
19903 		/*
19904 		 * Crank up IPv6 neighbor discovery
19905 		 * Unlike ARP, this should complete when
19906 		 * ipif_ndp_up returns. However, for
19907 		 * ILLF_XRESOLV interfaces we also send a
19908 		 * AR_INTERFACE_UP to the external resolver.
19909 		 * That ioctl will complete in ip_rput.
19910 		 */
19911 		if (isv6) {
19912 			err = ipif_ndp_up(ipif);
19913 			if (err != 0) {
19914 				if (err != EINPROGRESS)
19915 					mp = ipsq_pending_mp_get(ipsq, &connp);
19916 				return (err);
19917 			}
19918 		}
19919 		/* Now, ARP */
19920 		err = ipif_resolver_up(ipif, Res_act_initial);
19921 		if (err == EINPROGRESS) {
19922 			/* We will complete it in ip_arp_done */
19923 			return (err);
19924 		}
19925 		mp = ipsq_pending_mp_get(ipsq, &connp);
19926 		ASSERT(mp != NULL);
19927 		if (err != 0)
19928 			return (err);
19929 	} else {
19930 		/*
19931 		 * Interfaces without underlying hardware don't do duplicate
19932 		 * address detection.
19933 		 */
19934 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
19935 		ipif->ipif_addr_ready = 1;
19936 	}
19937 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
19938 }
19939 
19940 /*
19941  * Perform a bind for the physical device.
19942  * When the routine returns EINPROGRESS then mp has been consumed and
19943  * the ioctl will be acked from ip_rput_dlpi.
19944  * Allocate an unbind message and save it until ipif_down.
19945  */
19946 static int
19947 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
19948 {
19949 	areq_t	*areq;
19950 	mblk_t	*areq_mp = NULL;
19951 	mblk_t	*bind_mp = NULL;
19952 	mblk_t	*unbind_mp = NULL;
19953 	conn_t	*connp;
19954 	boolean_t success;
19955 	uint16_t sap_addr;
19956 
19957 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
19958 	ASSERT(IAM_WRITER_ILL(ill));
19959 	ASSERT(mp != NULL);
19960 
19961 	/* Create a resolver cookie for ARP */
19962 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
19963 		areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0);
19964 		if (areq_mp == NULL)
19965 			return (ENOMEM);
19966 
19967 		freemsg(ill->ill_resolver_mp);
19968 		ill->ill_resolver_mp = areq_mp;
19969 		areq = (areq_t *)areq_mp->b_rptr;
19970 		sap_addr = ill->ill_sap;
19971 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
19972 	}
19973 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
19974 	    DL_BIND_REQ);
19975 	if (bind_mp == NULL)
19976 		goto bad;
19977 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
19978 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
19979 
19980 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
19981 	if (unbind_mp == NULL)
19982 		goto bad;
19983 
19984 	/*
19985 	 * Record state needed to complete this operation when the
19986 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
19987 	 */
19988 	ASSERT(WR(q)->q_next == NULL);
19989 	connp = Q_TO_CONN(q);
19990 
19991 	mutex_enter(&connp->conn_lock);
19992 	mutex_enter(&ipif->ipif_ill->ill_lock);
19993 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
19994 	mutex_exit(&ipif->ipif_ill->ill_lock);
19995 	mutex_exit(&connp->conn_lock);
19996 	if (!success)
19997 		goto bad;
19998 
19999 	/*
20000 	 * Save the unbind message for ill_dl_down(); it will be consumed when
20001 	 * the interface goes down.
20002 	 */
20003 	ASSERT(ill->ill_unbind_mp == NULL);
20004 	ill->ill_unbind_mp = unbind_mp;
20005 
20006 	ill_dlpi_send(ill, bind_mp);
20007 	/* Send down link-layer capabilities probe if not already done. */
20008 	ill_capability_probe(ill);
20009 
20010 	/*
20011 	 * Sysid used to rely on the fact that netboots set domainname
20012 	 * and the like. Now that miniroot boots aren't strictly netboots
20013 	 * and miniroot network configuration is driven from userland
20014 	 * these things still need to be set. This situation can be detected
20015 	 * by comparing the interface being configured here to the one
20016 	 * dhcifname was set to reference by the boot loader. Once sysid is
20017 	 * converted to use dhcp_ipc_getinfo() this call can go away.
20018 	 */
20019 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
20020 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
20021 	    (strlen(srpc_domain) == 0)) {
20022 		if (dhcpinit() != 0)
20023 			cmn_err(CE_WARN, "no cached dhcp response");
20024 	}
20025 
20026 	/*
20027 	 * This operation will complete in ip_rput_dlpi with either
20028 	 * a DL_BIND_ACK or DL_ERROR_ACK.
20029 	 */
20030 	return (EINPROGRESS);
20031 bad:
20032 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
20033 	/*
20034 	 * We don't have to check for possible removal from illgrp
20035 	 * as we have not yet inserted in illgrp. For groups
20036 	 * without names, this ipif is still not UP and hence
20037 	 * this could not have possibly had any influence in forming
20038 	 * groups.
20039 	 */
20040 
20041 	freemsg(bind_mp);
20042 	freemsg(unbind_mp);
20043 	return (ENOMEM);
20044 }
20045 
20046 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
20047 
20048 /*
20049  * DLPI and ARP is up.
20050  * Create all the IREs associated with an interface bring up multicast.
20051  * Set the interface flag and finish other initialization
20052  * that potentially had to be differed to after DL_BIND_ACK.
20053  */
20054 int
20055 ipif_up_done(ipif_t *ipif)
20056 {
20057 	ire_t	*ire_array[20];
20058 	ire_t	**irep = ire_array;
20059 	ire_t	**irep1;
20060 	ipaddr_t net_mask = 0;
20061 	ipaddr_t subnet_mask, route_mask;
20062 	ill_t	*ill = ipif->ipif_ill;
20063 	queue_t	*stq;
20064 	ipif_t	 *src_ipif;
20065 	ipif_t   *tmp_ipif;
20066 	boolean_t	flush_ire_cache = B_TRUE;
20067 	int	err = 0;
20068 	phyint_t *phyi;
20069 	ire_t	**ipif_saved_irep = NULL;
20070 	int ipif_saved_ire_cnt;
20071 	int	cnt;
20072 	boolean_t	src_ipif_held = B_FALSE;
20073 	boolean_t	ire_added = B_FALSE;
20074 	boolean_t	loopback = B_FALSE;
20075 	ip_stack_t	*ipst = ill->ill_ipst;
20076 
20077 	ip1dbg(("ipif_up_done(%s:%u)\n",
20078 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
20079 	/* Check if this is a loopback interface */
20080 	if (ipif->ipif_ill->ill_wq == NULL)
20081 		loopback = B_TRUE;
20082 
20083 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20084 	/*
20085 	 * If all other interfaces for this ill are down or DEPRECATED,
20086 	 * or otherwise unsuitable for source address selection, remove
20087 	 * any IRE_CACHE entries for this ill to make sure source
20088 	 * address selection gets to take this new ipif into account.
20089 	 * No need to hold ill_lock while traversing the ipif list since
20090 	 * we are writer
20091 	 */
20092 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
20093 	    tmp_ipif = tmp_ipif->ipif_next) {
20094 		if (((tmp_ipif->ipif_flags &
20095 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
20096 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
20097 		    (tmp_ipif == ipif))
20098 			continue;
20099 		/* first useable pre-existing interface */
20100 		flush_ire_cache = B_FALSE;
20101 		break;
20102 	}
20103 	if (flush_ire_cache)
20104 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
20105 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
20106 
20107 	/*
20108 	 * Figure out which way the send-to queue should go.  Only
20109 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
20110 	 * should show up here.
20111 	 */
20112 	switch (ill->ill_net_type) {
20113 	case IRE_IF_RESOLVER:
20114 		stq = ill->ill_rq;
20115 		break;
20116 	case IRE_IF_NORESOLVER:
20117 	case IRE_LOOPBACK:
20118 		stq = ill->ill_wq;
20119 		break;
20120 	default:
20121 		return (EINVAL);
20122 	}
20123 
20124 	if (IS_LOOPBACK(ill)) {
20125 		/*
20126 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
20127 		 * ipif_lookup_on_name(), but in the case of zones we can have
20128 		 * several loopback addresses on lo0. So all the interfaces with
20129 		 * loopback addresses need to be marked IRE_LOOPBACK.
20130 		 */
20131 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
20132 		    htonl(INADDR_LOOPBACK))
20133 			ipif->ipif_ire_type = IRE_LOOPBACK;
20134 		else
20135 			ipif->ipif_ire_type = IRE_LOCAL;
20136 	}
20137 
20138 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
20139 		/*
20140 		 * Can't use our source address. Select a different
20141 		 * source address for the IRE_INTERFACE and IRE_LOCAL
20142 		 */
20143 		src_ipif = ipif_select_source(ipif->ipif_ill,
20144 		    ipif->ipif_subnet, ipif->ipif_zoneid);
20145 		if (src_ipif == NULL)
20146 			src_ipif = ipif;	/* Last resort */
20147 		else
20148 			src_ipif_held = B_TRUE;
20149 	} else {
20150 		src_ipif = ipif;
20151 	}
20152 
20153 	/* Create all the IREs associated with this interface */
20154 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20155 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20156 
20157 		/*
20158 		 * If we're on a labeled system then make sure that zone-
20159 		 * private addresses have proper remote host database entries.
20160 		 */
20161 		if (is_system_labeled() &&
20162 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
20163 		    !tsol_check_interface_address(ipif))
20164 			return (EINVAL);
20165 
20166 		/* Register the source address for __sin6_src_id */
20167 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
20168 		    ipif->ipif_zoneid, ipst);
20169 		if (err != 0) {
20170 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
20171 			return (err);
20172 		}
20173 
20174 		/* If the interface address is set, create the local IRE. */
20175 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
20176 		    (void *)ipif,
20177 		    ipif->ipif_ire_type,
20178 		    ntohl(ipif->ipif_lcl_addr)));
20179 		*irep++ = ire_create(
20180 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
20181 		    (uchar_t *)&ip_g_all_ones,		/* mask */
20182 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
20183 		    NULL,				/* no gateway */
20184 		    &ip_loopback_mtuplus,		/* max frag size */
20185 		    NULL,
20186 		    ipif->ipif_rq,			/* recv-from queue */
20187 		    NULL,				/* no send-to queue */
20188 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
20189 		    ipif,
20190 		    0,
20191 		    0,
20192 		    0,
20193 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
20194 		    RTF_PRIVATE : 0,
20195 		    &ire_uinfo_null,
20196 		    NULL,
20197 		    NULL,
20198 		    ipst);
20199 	} else {
20200 		ip1dbg((
20201 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
20202 		    ipif->ipif_ire_type,
20203 		    ntohl(ipif->ipif_lcl_addr),
20204 		    (uint_t)ipif->ipif_flags));
20205 	}
20206 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
20207 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
20208 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
20209 	} else {
20210 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
20211 	}
20212 
20213 	subnet_mask = ipif->ipif_net_mask;
20214 
20215 	/*
20216 	 * If mask was not specified, use natural netmask of
20217 	 * interface address. Also, store this mask back into the
20218 	 * ipif struct.
20219 	 */
20220 	if (subnet_mask == 0) {
20221 		subnet_mask = net_mask;
20222 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
20223 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
20224 		    ipif->ipif_v6subnet);
20225 	}
20226 
20227 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
20228 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
20229 	    ipif->ipif_subnet != INADDR_ANY) {
20230 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
20231 
20232 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
20233 			route_mask = IP_HOST_MASK;
20234 		} else {
20235 			route_mask = subnet_mask;
20236 		}
20237 
20238 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
20239 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
20240 		    (void *)ipif, (void *)ill,
20241 		    ill->ill_net_type,
20242 		    ntohl(ipif->ipif_subnet)));
20243 		*irep++ = ire_create(
20244 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
20245 		    (uchar_t *)&route_mask,		/* mask */
20246 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
20247 		    NULL,				/* no gateway */
20248 		    &ipif->ipif_mtu,			/* max frag */
20249 		    NULL,
20250 		    NULL,				/* no recv queue */
20251 		    stq,				/* send-to queue */
20252 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20253 		    ipif,
20254 		    0,
20255 		    0,
20256 		    0,
20257 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
20258 		    &ire_uinfo_null,
20259 		    NULL,
20260 		    NULL,
20261 		    ipst);
20262 	}
20263 
20264 	/*
20265 	 * Create any necessary broadcast IREs.
20266 	 */
20267 	if (ipif->ipif_flags & IPIF_BROADCAST)
20268 		irep = ipif_create_bcast_ires(ipif, irep);
20269 
20270 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
20271 
20272 	/* If an earlier ire_create failed, get out now */
20273 	for (irep1 = irep; irep1 > ire_array; ) {
20274 		irep1--;
20275 		if (*irep1 == NULL) {
20276 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
20277 			err = ENOMEM;
20278 			goto bad;
20279 		}
20280 	}
20281 
20282 	/*
20283 	 * Need to atomically check for ip_addr_availablity_check
20284 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
20285 	 * from group also.The ill_g_lock is grabbed as reader
20286 	 * just to make sure no new ills or new ipifs are being added
20287 	 * to the system while we are checking the uniqueness of addresses.
20288 	 */
20289 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20290 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
20291 	/* Mark it up, and increment counters. */
20292 	ipif->ipif_flags |= IPIF_UP;
20293 	ill->ill_ipif_up_count++;
20294 	err = ip_addr_availability_check(ipif);
20295 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
20296 	rw_exit(&ipst->ips_ill_g_lock);
20297 
20298 	if (err != 0) {
20299 		/*
20300 		 * Our address may already be up on the same ill. In this case,
20301 		 * the ARP entry for our ipif replaced the one for the other
20302 		 * ipif. So we don't want to delete it (otherwise the other ipif
20303 		 * would be unable to send packets).
20304 		 * ip_addr_availability_check() identifies this case for us and
20305 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
20306 		 * which is the expected error code.
20307 		 */
20308 		if (err == EADDRINUSE) {
20309 			freemsg(ipif->ipif_arp_del_mp);
20310 			ipif->ipif_arp_del_mp = NULL;
20311 			err = EADDRNOTAVAIL;
20312 		}
20313 		ill->ill_ipif_up_count--;
20314 		ipif->ipif_flags &= ~IPIF_UP;
20315 		goto bad;
20316 	}
20317 
20318 	/*
20319 	 * Add in all newly created IREs.  ire_create_bcast() has
20320 	 * already checked for duplicates of the IRE_BROADCAST type.
20321 	 * We want to add before we call ifgrp_insert which wants
20322 	 * to know whether IRE_IF_RESOLVER exists or not.
20323 	 *
20324 	 * NOTE : We refrele the ire though we may branch to "bad"
20325 	 *	  later on where we do ire_delete. This is okay
20326 	 *	  because nobody can delete it as we are running
20327 	 *	  exclusively.
20328 	 */
20329 	for (irep1 = irep; irep1 > ire_array; ) {
20330 		irep1--;
20331 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
20332 		/*
20333 		 * refheld by ire_add. refele towards the end of the func
20334 		 */
20335 		(void) ire_add(irep1, NULL, NULL, NULL, B_FALSE);
20336 	}
20337 	ire_added = B_TRUE;
20338 	/*
20339 	 * Form groups if possible.
20340 	 *
20341 	 * If we are supposed to be in a ill_group with a name, insert it
20342 	 * now as we know that at least one ipif is UP. Otherwise form
20343 	 * nameless groups.
20344 	 *
20345 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
20346 	 * this ipif into the appropriate interface group, or create a
20347 	 * new one. If this is already in a nameless group, we try to form
20348 	 * a bigger group looking at other ills potentially sharing this
20349 	 * ipif's prefix.
20350 	 */
20351 	phyi = ill->ill_phyint;
20352 	if (phyi->phyint_groupname_len != 0) {
20353 		ASSERT(phyi->phyint_groupname != NULL);
20354 		if (ill->ill_ipif_up_count == 1) {
20355 			ASSERT(ill->ill_group == NULL);
20356 			err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill,
20357 			    phyi->phyint_groupname, NULL, B_TRUE);
20358 			if (err != 0) {
20359 				ip1dbg(("ipif_up_done: illgrp allocation "
20360 				    "failed, error %d\n", err));
20361 				goto bad;
20362 			}
20363 		}
20364 		ASSERT(ill->ill_group != NULL);
20365 	}
20366 
20367 	/*
20368 	 * When this is part of group, we need to make sure that
20369 	 * any broadcast ires created because of this ipif coming
20370 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
20371 	 * so that we don't receive duplicate broadcast packets.
20372 	 */
20373 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
20374 		ipif_renominate_bcast(ipif);
20375 
20376 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
20377 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
20378 	ipif_saved_irep = ipif_recover_ire(ipif);
20379 
20380 	if (!loopback) {
20381 		/*
20382 		 * If the broadcast address has been set, make sure it makes
20383 		 * sense based on the interface address.
20384 		 * Only match on ill since we are sharing broadcast addresses.
20385 		 */
20386 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
20387 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
20388 			ire_t	*ire;
20389 
20390 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
20391 			    IRE_BROADCAST, ipif, ALL_ZONES,
20392 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst);
20393 
20394 			if (ire == NULL) {
20395 				/*
20396 				 * If there isn't a matching broadcast IRE,
20397 				 * revert to the default for this netmask.
20398 				 */
20399 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
20400 				mutex_enter(&ipif->ipif_ill->ill_lock);
20401 				ipif_set_default(ipif);
20402 				mutex_exit(&ipif->ipif_ill->ill_lock);
20403 			} else {
20404 				ire_refrele(ire);
20405 			}
20406 		}
20407 
20408 	}
20409 
20410 	/* This is the first interface on this ill */
20411 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
20412 		/*
20413 		 * Need to recover all multicast memberships in the driver.
20414 		 * This had to be deferred until we had attached.
20415 		 */
20416 		ill_recover_multicast(ill);
20417 	}
20418 	/* Join the allhosts multicast address */
20419 	ipif_multicast_up(ipif);
20420 
20421 	if (!loopback) {
20422 		/*
20423 		 * See whether anybody else would benefit from the
20424 		 * new ipif that we added. We call this always rather
20425 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
20426 		 * ipif is for the benefit of illgrp_insert (done above)
20427 		 * which does not do source address selection as it does
20428 		 * not want to re-create interface routes that we are
20429 		 * having reference to it here.
20430 		 */
20431 		ill_update_source_selection(ill);
20432 	}
20433 
20434 	for (irep1 = irep; irep1 > ire_array; ) {
20435 		irep1--;
20436 		if (*irep1 != NULL) {
20437 			/* was held in ire_add */
20438 			ire_refrele(*irep1);
20439 		}
20440 	}
20441 
20442 	cnt = ipif_saved_ire_cnt;
20443 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
20444 		if (*irep1 != NULL) {
20445 			/* was held in ire_add */
20446 			ire_refrele(*irep1);
20447 		}
20448 	}
20449 
20450 	if (!loopback && ipif->ipif_addr_ready) {
20451 		/* Broadcast an address mask reply. */
20452 		ipif_mask_reply(ipif);
20453 	}
20454 	if (ipif_saved_irep != NULL) {
20455 		kmem_free(ipif_saved_irep,
20456 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20457 	}
20458 	if (src_ipif_held)
20459 		ipif_refrele(src_ipif);
20460 
20461 	/*
20462 	 * This had to be deferred until we had bound.  Tell routing sockets and
20463 	 * others that this interface is up if it looks like the address has
20464 	 * been validated.  Otherwise, if it isn't ready yet, wait for
20465 	 * duplicate address detection to do its thing.
20466 	 */
20467 	if (ipif->ipif_addr_ready) {
20468 		ip_rts_ifmsg(ipif);
20469 		ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
20470 		/* Let SCTP update the status for this ipif */
20471 		sctp_update_ipif(ipif, SCTP_IPIF_UP);
20472 	}
20473 	return (0);
20474 
20475 bad:
20476 	ip1dbg(("ipif_up_done: FAILED \n"));
20477 	/*
20478 	 * We don't have to bother removing from ill groups because
20479 	 *
20480 	 * 1) For groups with names, we insert only when the first ipif
20481 	 *    comes up. In that case if it fails, it will not be in any
20482 	 *    group. So, we need not try to remove for that case.
20483 	 *
20484 	 * 2) For groups without names, either we tried to insert ipif_ill
20485 	 *    in a group as singleton or found some other group to become
20486 	 *    a bigger group. For the former, if it fails we don't have
20487 	 *    anything to do as ipif_ill is not in the group and for the
20488 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
20489 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
20490 	 */
20491 	while (irep > ire_array) {
20492 		irep--;
20493 		if (*irep != NULL) {
20494 			ire_delete(*irep);
20495 			if (ire_added)
20496 				ire_refrele(*irep);
20497 		}
20498 	}
20499 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
20500 
20501 	if (ipif_saved_irep != NULL) {
20502 		kmem_free(ipif_saved_irep,
20503 		    ipif_saved_ire_cnt * sizeof (ire_t *));
20504 	}
20505 	if (src_ipif_held)
20506 		ipif_refrele(src_ipif);
20507 
20508 	ipif_arp_down(ipif);
20509 	return (err);
20510 }
20511 
20512 /*
20513  * Turn off the ARP with the ILLF_NOARP flag.
20514  */
20515 static int
20516 ill_arp_off(ill_t *ill)
20517 {
20518 	mblk_t	*arp_off_mp = NULL;
20519 	mblk_t	*arp_on_mp = NULL;
20520 
20521 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
20522 
20523 	ASSERT(IAM_WRITER_ILL(ill));
20524 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20525 
20526 	/*
20527 	 * If the on message is still around we've already done
20528 	 * an arp_off without doing an arp_on thus there is no
20529 	 * work needed.
20530 	 */
20531 	if (ill->ill_arp_on_mp != NULL)
20532 		return (0);
20533 
20534 	/*
20535 	 * Allocate an ARP on message (to be saved) and an ARP off message
20536 	 */
20537 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
20538 	if (!arp_off_mp)
20539 		return (ENOMEM);
20540 
20541 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
20542 	if (!arp_on_mp)
20543 		goto failed;
20544 
20545 	ASSERT(ill->ill_arp_on_mp == NULL);
20546 	ill->ill_arp_on_mp = arp_on_mp;
20547 
20548 	/* Send an AR_INTERFACE_OFF request */
20549 	putnext(ill->ill_rq, arp_off_mp);
20550 	return (0);
20551 failed:
20552 
20553 	if (arp_off_mp)
20554 		freemsg(arp_off_mp);
20555 	return (ENOMEM);
20556 }
20557 
20558 /*
20559  * Turn on ARP by turning off the ILLF_NOARP flag.
20560  */
20561 static int
20562 ill_arp_on(ill_t *ill)
20563 {
20564 	mblk_t	*mp;
20565 
20566 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
20567 
20568 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
20569 
20570 	ASSERT(IAM_WRITER_ILL(ill));
20571 	/*
20572 	 * Send an AR_INTERFACE_ON request if we have already done
20573 	 * an arp_off (which allocated the message).
20574 	 */
20575 	if (ill->ill_arp_on_mp != NULL) {
20576 		mp = ill->ill_arp_on_mp;
20577 		ill->ill_arp_on_mp = NULL;
20578 		putnext(ill->ill_rq, mp);
20579 	}
20580 	return (0);
20581 }
20582 
20583 /*
20584  * Called after either deleting ill from the group or when setting
20585  * FAILED or STANDBY on the interface.
20586  */
20587 static void
20588 illgrp_reset_schednext(ill_t *ill)
20589 {
20590 	ill_group_t *illgrp;
20591 	ill_t *save_ill;
20592 
20593 	ASSERT(IAM_WRITER_ILL(ill));
20594 	/*
20595 	 * When called from illgrp_delete, ill_group will be non-NULL.
20596 	 * But when called from ip_sioctl_flags, it could be NULL if
20597 	 * somebody is setting FAILED/INACTIVE on some interface which
20598 	 * is not part of a group.
20599 	 */
20600 	illgrp = ill->ill_group;
20601 	if (illgrp == NULL)
20602 		return;
20603 	if (illgrp->illgrp_ill_schednext != ill)
20604 		return;
20605 
20606 	illgrp->illgrp_ill_schednext = NULL;
20607 	save_ill = ill;
20608 	/*
20609 	 * Choose a good ill to be the next one for
20610 	 * outbound traffic. As the flags FAILED/STANDBY is
20611 	 * not yet marked when called from ip_sioctl_flags,
20612 	 * we check for ill separately.
20613 	 */
20614 	for (ill = illgrp->illgrp_ill; ill != NULL;
20615 	    ill = ill->ill_group_next) {
20616 		if ((ill != save_ill) &&
20617 		    !(ill->ill_phyint->phyint_flags &
20618 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
20619 			illgrp->illgrp_ill_schednext = ill;
20620 			return;
20621 		}
20622 	}
20623 }
20624 
20625 /*
20626  * Given an ill, find the next ill in the group to be scheduled.
20627  * (This should be called by ip_newroute() before ire_create().)
20628  * The passed in ill may be pulled out of the group, after we have picked
20629  * up a different outgoing ill from the same group. However ire add will
20630  * atomically check this.
20631  */
20632 ill_t *
20633 illgrp_scheduler(ill_t *ill)
20634 {
20635 	ill_t *retill;
20636 	ill_group_t *illgrp;
20637 	int illcnt;
20638 	int i;
20639 	uint64_t flags;
20640 	ip_stack_t	*ipst = ill->ill_ipst;
20641 
20642 	/*
20643 	 * We don't use a lock to check for the ill_group. If this ill
20644 	 * is currently being inserted we may end up just returning this
20645 	 * ill itself. That is ok.
20646 	 */
20647 	if (ill->ill_group == NULL) {
20648 		ill_refhold(ill);
20649 		return (ill);
20650 	}
20651 
20652 	/*
20653 	 * Grab the ill_g_lock as reader to make sure we are dealing with
20654 	 * a set of stable ills. No ill can be added or deleted or change
20655 	 * group while we hold the reader lock.
20656 	 */
20657 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20658 	if ((illgrp = ill->ill_group) == NULL) {
20659 		rw_exit(&ipst->ips_ill_g_lock);
20660 		ill_refhold(ill);
20661 		return (ill);
20662 	}
20663 
20664 	illcnt = illgrp->illgrp_ill_count;
20665 	mutex_enter(&illgrp->illgrp_lock);
20666 	retill = illgrp->illgrp_ill_schednext;
20667 
20668 	if (retill == NULL)
20669 		retill = illgrp->illgrp_ill;
20670 
20671 	/*
20672 	 * We do a circular search beginning at illgrp_ill_schednext
20673 	 * or illgrp_ill. We don't check the flags against the ill lock
20674 	 * since it can change anytime. The ire creation will be atomic
20675 	 * and will fail if the ill is FAILED or OFFLINE.
20676 	 */
20677 	for (i = 0; i < illcnt; i++) {
20678 		flags = retill->ill_phyint->phyint_flags;
20679 
20680 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
20681 		    ILL_CAN_LOOKUP(retill)) {
20682 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
20683 			ill_refhold(retill);
20684 			break;
20685 		}
20686 		retill = retill->ill_group_next;
20687 		if (retill == NULL)
20688 			retill = illgrp->illgrp_ill;
20689 	}
20690 	mutex_exit(&illgrp->illgrp_lock);
20691 	rw_exit(&ipst->ips_ill_g_lock);
20692 
20693 	return (i == illcnt ? NULL : retill);
20694 }
20695 
20696 /*
20697  * Checks for availbility of a usable source address (if there is one) when the
20698  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
20699  * this selection is done regardless of the destination.
20700  */
20701 boolean_t
20702 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
20703 {
20704 	uint_t	ifindex;
20705 	ipif_t	*ipif = NULL;
20706 	ill_t	*uill;
20707 	boolean_t isv6;
20708 	ip_stack_t	*ipst = ill->ill_ipst;
20709 
20710 	ASSERT(ill != NULL);
20711 
20712 	isv6 = ill->ill_isv6;
20713 	ifindex = ill->ill_usesrc_ifindex;
20714 	if (ifindex != 0) {
20715 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
20716 		    NULL, ipst);
20717 		if (uill == NULL)
20718 			return (NULL);
20719 		mutex_enter(&uill->ill_lock);
20720 		for (ipif = uill->ill_ipif; ipif != NULL;
20721 		    ipif = ipif->ipif_next) {
20722 			if (!IPIF_CAN_LOOKUP(ipif))
20723 				continue;
20724 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20725 				continue;
20726 			if (!(ipif->ipif_flags & IPIF_UP))
20727 				continue;
20728 			if (ipif->ipif_zoneid != zoneid)
20729 				continue;
20730 			if ((isv6 &&
20731 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
20732 			    (ipif->ipif_lcl_addr == INADDR_ANY))
20733 				continue;
20734 			mutex_exit(&uill->ill_lock);
20735 			ill_refrele(uill);
20736 			return (B_TRUE);
20737 		}
20738 		mutex_exit(&uill->ill_lock);
20739 		ill_refrele(uill);
20740 	}
20741 	return (B_FALSE);
20742 }
20743 
20744 /*
20745  * Determine the best source address given a destination address and an ill.
20746  * Prefers non-deprecated over deprecated but will return a deprecated
20747  * address if there is no other choice. If there is a usable source address
20748  * on the interface pointed to by ill_usesrc_ifindex then that is given
20749  * first preference.
20750  *
20751  * Returns NULL if there is no suitable source address for the ill.
20752  * This only occurs when there is no valid source address for the ill.
20753  */
20754 ipif_t *
20755 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
20756 {
20757 	ipif_t *ipif;
20758 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
20759 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
20760 	int index = 0;
20761 	boolean_t wrapped = B_FALSE;
20762 	boolean_t same_subnet_only = B_FALSE;
20763 	boolean_t ipif_same_found, ipif_other_found;
20764 	boolean_t specific_found;
20765 	ill_t	*till, *usill = NULL;
20766 	tsol_tpc_t *src_rhtp, *dst_rhtp;
20767 	ip_stack_t	*ipst = ill->ill_ipst;
20768 
20769 	if (ill->ill_usesrc_ifindex != 0) {
20770 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
20771 		    B_FALSE, NULL, NULL, NULL, NULL, ipst);
20772 		if (usill != NULL)
20773 			ill = usill;	/* Select source from usesrc ILL */
20774 		else
20775 			return (NULL);
20776 	}
20777 
20778 	/*
20779 	 * If we're dealing with an unlabeled destination on a labeled system,
20780 	 * make sure that we ignore source addresses that are incompatible with
20781 	 * the destination's default label.  That destination's default label
20782 	 * must dominate the minimum label on the source address.
20783 	 */
20784 	dst_rhtp = NULL;
20785 	if (is_system_labeled()) {
20786 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
20787 		if (dst_rhtp == NULL)
20788 			return (NULL);
20789 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
20790 			TPC_RELE(dst_rhtp);
20791 			dst_rhtp = NULL;
20792 		}
20793 	}
20794 
20795 	/*
20796 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
20797 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
20798 	 * After selecting the right ipif, under ill_lock make sure ipif is
20799 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
20800 	 * we retry. Inside the loop we still need to check for CONDEMNED,
20801 	 * but not under a lock.
20802 	 */
20803 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
20804 
20805 retry:
20806 	till = ill;
20807 	ipif_arr[0] = NULL;
20808 
20809 	if (till->ill_group != NULL)
20810 		till = till->ill_group->illgrp_ill;
20811 
20812 	/*
20813 	 * Choose one good source address from each ill across the group.
20814 	 * If possible choose a source address in the same subnet as
20815 	 * the destination address.
20816 	 *
20817 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
20818 	 * This is okay because of the following.
20819 	 *
20820 	 *    If PHYI_FAILED is set and we still have non-deprecated
20821 	 *    addresses, it means the addresses have not yet been
20822 	 *    failed over to a different interface. We potentially
20823 	 *    select them to create IRE_CACHES, which will be later
20824 	 *    flushed when the addresses move over.
20825 	 *
20826 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
20827 	 *    addresses, it means either the user has configured them
20828 	 *    or PHYI_INACTIVE has not been cleared after the addresses
20829 	 *    been moved over. For the former, in.mpathd does a failover
20830 	 *    when the interface becomes INACTIVE and hence we should
20831 	 *    not find them. Once INACTIVE is set, we don't allow them
20832 	 *    to create logical interfaces anymore. For the latter, a
20833 	 *    flush will happen when INACTIVE is cleared which will
20834 	 *    flush the IRE_CACHES.
20835 	 *
20836 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
20837 	 *    over soon. We potentially select them to create IRE_CACHEs,
20838 	 *    which will be later flushed when the addresses move over.
20839 	 *
20840 	 * NOTE : As ipif_select_source is called to borrow source address
20841 	 * for an ipif that is part of a group, source address selection
20842 	 * will be re-done whenever the group changes i.e either an
20843 	 * insertion/deletion in the group.
20844 	 *
20845 	 * Fill ipif_arr[] with source addresses, using these rules:
20846 	 *
20847 	 *	1. At most one source address from a given ill ends up
20848 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
20849 	 *	   associated with a given ill ends up in ipif_arr[].
20850 	 *
20851 	 *	2. If there is at least one non-deprecated ipif in the
20852 	 *	   IPMP group with a source address on the same subnet as
20853 	 *	   our destination, then fill ipif_arr[] only with
20854 	 *	   source addresses on the same subnet as our destination.
20855 	 *	   Note that because of (1), only the first
20856 	 *	   non-deprecated ipif found with a source address
20857 	 *	   matching the destination ends up in ipif_arr[].
20858 	 *
20859 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
20860 	 *	   addresses not in the same subnet as our destination.
20861 	 *	   Again, because of (1), only the first off-subnet source
20862 	 *	   address will be chosen.
20863 	 *
20864 	 *	4. If there are no non-deprecated ipifs, then just use
20865 	 *	   the source address associated with the last deprecated
20866 	 *	   one we find that happens to be on the same subnet,
20867 	 *	   otherwise the first one not in the same subnet.
20868 	 */
20869 	specific_found = B_FALSE;
20870 	for (; till != NULL; till = till->ill_group_next) {
20871 		ipif_same_found = B_FALSE;
20872 		ipif_other_found = B_FALSE;
20873 		for (ipif = till->ill_ipif; ipif != NULL;
20874 		    ipif = ipif->ipif_next) {
20875 			if (!IPIF_CAN_LOOKUP(ipif))
20876 				continue;
20877 			/* Always skip NOLOCAL and ANYCAST interfaces */
20878 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
20879 				continue;
20880 			if (!(ipif->ipif_flags & IPIF_UP) ||
20881 			    !ipif->ipif_addr_ready)
20882 				continue;
20883 			if (ipif->ipif_zoneid != zoneid &&
20884 			    ipif->ipif_zoneid != ALL_ZONES)
20885 				continue;
20886 			/*
20887 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
20888 			 * but are not valid as source addresses.
20889 			 */
20890 			if (ipif->ipif_lcl_addr == INADDR_ANY)
20891 				continue;
20892 
20893 			/*
20894 			 * Check compatibility of local address for
20895 			 * destination's default label if we're on a labeled
20896 			 * system.  Incompatible addresses can't be used at
20897 			 * all.
20898 			 */
20899 			if (dst_rhtp != NULL) {
20900 				boolean_t incompat;
20901 
20902 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
20903 				    IPV4_VERSION, B_FALSE);
20904 				if (src_rhtp == NULL)
20905 					continue;
20906 				incompat =
20907 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
20908 				    src_rhtp->tpc_tp.tp_doi !=
20909 				    dst_rhtp->tpc_tp.tp_doi ||
20910 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
20911 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
20912 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
20913 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
20914 				TPC_RELE(src_rhtp);
20915 				if (incompat)
20916 					continue;
20917 			}
20918 
20919 			/*
20920 			 * We prefer not to use all all-zones addresses, if we
20921 			 * can avoid it, as they pose problems with unlabeled
20922 			 * destinations.
20923 			 */
20924 			if (ipif->ipif_zoneid != ALL_ZONES) {
20925 				if (!specific_found &&
20926 				    (!same_subnet_only ||
20927 				    (ipif->ipif_net_mask & dst) ==
20928 				    ipif->ipif_subnet)) {
20929 					index = 0;
20930 					specific_found = B_TRUE;
20931 					ipif_other_found = B_FALSE;
20932 				}
20933 			} else {
20934 				if (specific_found)
20935 					continue;
20936 			}
20937 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
20938 				if (ipif_dep == NULL ||
20939 				    (ipif->ipif_net_mask & dst) ==
20940 				    ipif->ipif_subnet)
20941 					ipif_dep = ipif;
20942 				continue;
20943 			}
20944 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
20945 				/* found a source address in the same subnet */
20946 				if (!same_subnet_only) {
20947 					same_subnet_only = B_TRUE;
20948 					index = 0;
20949 				}
20950 				ipif_same_found = B_TRUE;
20951 			} else {
20952 				if (same_subnet_only || ipif_other_found)
20953 					continue;
20954 				ipif_other_found = B_TRUE;
20955 			}
20956 			ipif_arr[index++] = ipif;
20957 			if (index == MAX_IPIF_SELECT_SOURCE) {
20958 				wrapped = B_TRUE;
20959 				index = 0;
20960 			}
20961 			if (ipif_same_found)
20962 				break;
20963 		}
20964 	}
20965 
20966 	if (ipif_arr[0] == NULL) {
20967 		ipif = ipif_dep;
20968 	} else {
20969 		if (wrapped)
20970 			index = MAX_IPIF_SELECT_SOURCE;
20971 		ipif = ipif_arr[ipif_rand(ipst) % index];
20972 		ASSERT(ipif != NULL);
20973 	}
20974 
20975 	if (ipif != NULL) {
20976 		mutex_enter(&ipif->ipif_ill->ill_lock);
20977 		if (!IPIF_CAN_LOOKUP(ipif)) {
20978 			mutex_exit(&ipif->ipif_ill->ill_lock);
20979 			goto retry;
20980 		}
20981 		ipif_refhold_locked(ipif);
20982 		mutex_exit(&ipif->ipif_ill->ill_lock);
20983 	}
20984 
20985 	rw_exit(&ipst->ips_ill_g_lock);
20986 	if (usill != NULL)
20987 		ill_refrele(usill);
20988 	if (dst_rhtp != NULL)
20989 		TPC_RELE(dst_rhtp);
20990 
20991 #ifdef DEBUG
20992 	if (ipif == NULL) {
20993 		char buf1[INET6_ADDRSTRLEN];
20994 
20995 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
20996 		    ill->ill_name,
20997 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
20998 	} else {
20999 		char buf1[INET6_ADDRSTRLEN];
21000 		char buf2[INET6_ADDRSTRLEN];
21001 
21002 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
21003 		    ipif->ipif_ill->ill_name,
21004 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
21005 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
21006 		    buf2, sizeof (buf2))));
21007 	}
21008 #endif /* DEBUG */
21009 	return (ipif);
21010 }
21011 
21012 
21013 /*
21014  * If old_ipif is not NULL, see if ipif was derived from old
21015  * ipif and if so, recreate the interface route by re-doing
21016  * source address selection. This happens when ipif_down ->
21017  * ipif_update_other_ipifs calls us.
21018  *
21019  * If old_ipif is NULL, just redo the source address selection
21020  * if needed. This happens when illgrp_insert or ipif_up_done
21021  * calls us.
21022  */
21023 static void
21024 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
21025 {
21026 	ire_t *ire;
21027 	ire_t *ipif_ire;
21028 	queue_t *stq;
21029 	ipif_t *nipif;
21030 	ill_t *ill;
21031 	boolean_t need_rele = B_FALSE;
21032 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21033 
21034 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
21035 	ASSERT(IAM_WRITER_IPIF(ipif));
21036 
21037 	ill = ipif->ipif_ill;
21038 	if (!(ipif->ipif_flags &
21039 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
21040 		/*
21041 		 * Can't possibly have borrowed the source
21042 		 * from old_ipif.
21043 		 */
21044 		return;
21045 	}
21046 
21047 	/*
21048 	 * Is there any work to be done? No work if the address
21049 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
21050 	 * ipif_select_source() does not borrow addresses from
21051 	 * NOLOCAL and ANYCAST interfaces).
21052 	 */
21053 	if ((old_ipif != NULL) &&
21054 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
21055 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
21056 	    (old_ipif->ipif_flags &
21057 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
21058 		return;
21059 	}
21060 
21061 	/*
21062 	 * Perform the same checks as when creating the
21063 	 * IRE_INTERFACE in ipif_up_done.
21064 	 */
21065 	if (!(ipif->ipif_flags & IPIF_UP))
21066 		return;
21067 
21068 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
21069 	    (ipif->ipif_subnet == INADDR_ANY))
21070 		return;
21071 
21072 	ipif_ire = ipif_to_ire(ipif);
21073 	if (ipif_ire == NULL)
21074 		return;
21075 
21076 	/*
21077 	 * We know that ipif uses some other source for its
21078 	 * IRE_INTERFACE. Is it using the source of this
21079 	 * old_ipif?
21080 	 */
21081 	if (old_ipif != NULL &&
21082 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
21083 		ire_refrele(ipif_ire);
21084 		return;
21085 	}
21086 	if (ip_debug > 2) {
21087 		/* ip1dbg */
21088 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
21089 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
21090 	}
21091 
21092 	stq = ipif_ire->ire_stq;
21093 
21094 	/*
21095 	 * Can't use our source address. Select a different
21096 	 * source address for the IRE_INTERFACE.
21097 	 */
21098 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
21099 	if (nipif == NULL) {
21100 		/* Last resort - all ipif's have IPIF_NOLOCAL */
21101 		nipif = ipif;
21102 	} else {
21103 		need_rele = B_TRUE;
21104 	}
21105 
21106 	ire = ire_create(
21107 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
21108 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
21109 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
21110 	    NULL,				/* no gateway */
21111 	    &ipif->ipif_mtu,			/* max frag */
21112 	    NULL,				/* no src nce */
21113 	    NULL,				/* no recv from queue */
21114 	    stq,				/* send-to queue */
21115 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
21116 	    ipif,
21117 	    0,
21118 	    0,
21119 	    0,
21120 	    0,
21121 	    &ire_uinfo_null,
21122 	    NULL,
21123 	    NULL,
21124 	    ipst);
21125 
21126 	if (ire != NULL) {
21127 		ire_t *ret_ire;
21128 		int error;
21129 
21130 		/*
21131 		 * We don't need ipif_ire anymore. We need to delete
21132 		 * before we add so that ire_add does not detect
21133 		 * duplicates.
21134 		 */
21135 		ire_delete(ipif_ire);
21136 		ret_ire = ire;
21137 		error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE);
21138 		ASSERT(error == 0);
21139 		ASSERT(ire == ret_ire);
21140 		/* Held in ire_add */
21141 		ire_refrele(ret_ire);
21142 	}
21143 	/*
21144 	 * Either we are falling through from above or could not
21145 	 * allocate a replacement.
21146 	 */
21147 	ire_refrele(ipif_ire);
21148 	if (need_rele)
21149 		ipif_refrele(nipif);
21150 }
21151 
21152 /*
21153  * This old_ipif is going away.
21154  *
21155  * Determine if any other ipif's is using our address as
21156  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
21157  * IPIF_DEPRECATED).
21158  * Find the IRE_INTERFACE for such ipifs and recreate them
21159  * to use an different source address following the rules in
21160  * ipif_up_done.
21161  *
21162  * This function takes an illgrp as an argument so that illgrp_delete
21163  * can call this to update source address even after deleting the
21164  * old_ipif->ipif_ill from the ill group.
21165  */
21166 static void
21167 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
21168 {
21169 	ipif_t *ipif;
21170 	ill_t *ill;
21171 	char	buf[INET6_ADDRSTRLEN];
21172 
21173 	ASSERT(IAM_WRITER_IPIF(old_ipif));
21174 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
21175 
21176 	ill = old_ipif->ipif_ill;
21177 
21178 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
21179 	    ill->ill_name,
21180 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
21181 	    buf, sizeof (buf))));
21182 	/*
21183 	 * If this part of a group, look at all ills as ipif_select_source
21184 	 * borrows source address across all the ills in the group.
21185 	 */
21186 	if (illgrp != NULL)
21187 		ill = illgrp->illgrp_ill;
21188 
21189 	for (; ill != NULL; ill = ill->ill_group_next) {
21190 		for (ipif = ill->ill_ipif; ipif != NULL;
21191 		    ipif = ipif->ipif_next) {
21192 
21193 			if (ipif == old_ipif)
21194 				continue;
21195 
21196 			ipif_recreate_interface_routes(old_ipif, ipif);
21197 		}
21198 	}
21199 }
21200 
21201 /* ARGSUSED */
21202 int
21203 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21204 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21205 {
21206 	/*
21207 	 * ill_phyint_reinit merged the v4 and v6 into a single
21208 	 * ipsq. Could also have become part of a ipmp group in the
21209 	 * process, and we might not have been able to complete the
21210 	 * operation in ipif_set_values, if we could not become
21211 	 * exclusive.  If so restart it here.
21212 	 */
21213 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21214 }
21215 
21216 
21217 /*
21218  * Can operate on either a module or a driver queue.
21219  * Returns an error if not a module queue.
21220  */
21221 /* ARGSUSED */
21222 int
21223 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21224     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21225 {
21226 	queue_t		*q1 = q;
21227 	char 		*cp;
21228 	char		interf_name[LIFNAMSIZ];
21229 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
21230 
21231 	if (q->q_next == NULL) {
21232 		ip1dbg((
21233 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
21234 		return (EINVAL);
21235 	}
21236 
21237 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
21238 		return (EALREADY);
21239 
21240 	do {
21241 		q1 = q1->q_next;
21242 	} while (q1->q_next);
21243 	cp = q1->q_qinfo->qi_minfo->mi_idname;
21244 	(void) sprintf(interf_name, "%s%d", cp, ppa);
21245 
21246 	/*
21247 	 * Here we are not going to delay the ioack until after
21248 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
21249 	 * original ioctl message before sending the requests.
21250 	 */
21251 	return (ipif_set_values(q, mp, interf_name, &ppa));
21252 }
21253 
21254 /* ARGSUSED */
21255 int
21256 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
21257     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
21258 {
21259 	return (ENXIO);
21260 }
21261 
21262 /*
21263  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
21264  * `irep'.  Returns a pointer to the next free `irep' entry (just like
21265  * ire_check_and_create_bcast()).
21266  */
21267 static ire_t **
21268 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
21269 {
21270 	ipaddr_t addr;
21271 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
21272 	ipaddr_t subnetmask = ipif->ipif_net_mask;
21273 	int flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
21274 
21275 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
21276 
21277 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
21278 
21279 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
21280 	    (ipif->ipif_flags & IPIF_NOLOCAL))
21281 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
21282 
21283 	irep = ire_check_and_create_bcast(ipif, 0, irep, flags);
21284 	irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, flags);
21285 
21286 	/*
21287 	 * For backward compatibility, we create net broadcast IREs based on
21288 	 * the old "IP address class system", since some old machines only
21289 	 * respond to these class derived net broadcast.  However, we must not
21290 	 * create these net broadcast IREs if the subnetmask is shorter than
21291 	 * the IP address class based derived netmask.  Otherwise, we may
21292 	 * create a net broadcast address which is the same as an IP address
21293 	 * on the subnet -- and then TCP will refuse to talk to that address.
21294 	 */
21295 	if (netmask < subnetmask) {
21296 		addr = netmask & ipif->ipif_subnet;
21297 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21298 		irep = ire_check_and_create_bcast(ipif, ~netmask | addr, irep,
21299 		    flags);
21300 	}
21301 
21302 	/*
21303 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
21304 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
21305 	 * created.  Creating these broadcast IREs will only create confusion
21306 	 * as `addr' will be the same as the IP address.
21307 	 */
21308 	if (subnetmask != 0xFFFFFFFF) {
21309 		addr = ipif->ipif_subnet;
21310 		irep = ire_check_and_create_bcast(ipif, addr, irep, flags);
21311 		irep = ire_check_and_create_bcast(ipif, ~subnetmask | addr,
21312 		    irep, flags);
21313 	}
21314 
21315 	return (irep);
21316 }
21317 
21318 /*
21319  * Broadcast IRE info structure used in the functions below.  Since we
21320  * allocate BCAST_COUNT of them on the stack, keep the bit layout compact.
21321  */
21322 typedef struct bcast_ireinfo {
21323 	uchar_t		bi_type;	/* BCAST_* value from below */
21324 	uchar_t		bi_willdie:1, 	/* will this IRE be going away? */
21325 			bi_needrep:1,	/* do we need to replace it? */
21326 			bi_haverep:1,	/* have we replaced it? */
21327 			bi_pad:5;
21328 	ipaddr_t	bi_addr;	/* IRE address */
21329 	ipif_t		*bi_backup;	/* last-ditch ipif to replace it on */
21330 } bcast_ireinfo_t;
21331 
21332 enum { BCAST_ALLONES, BCAST_ALLZEROES, BCAST_NET, BCAST_SUBNET, BCAST_COUNT };
21333 
21334 /*
21335  * Check if `ipif' needs the dying broadcast IRE described by `bireinfop', and
21336  * return B_TRUE if it should immediately be used to recreate the IRE.
21337  */
21338 static boolean_t
21339 ipif_consider_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop)
21340 {
21341 	ipaddr_t addr;
21342 
21343 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_willdie);
21344 
21345 	switch (bireinfop->bi_type) {
21346 	case BCAST_NET:
21347 		addr = ipif->ipif_subnet & ip_net_mask(ipif->ipif_subnet);
21348 		if (addr != bireinfop->bi_addr)
21349 			return (B_FALSE);
21350 		break;
21351 	case BCAST_SUBNET:
21352 		if (ipif->ipif_subnet != bireinfop->bi_addr)
21353 			return (B_FALSE);
21354 		break;
21355 	}
21356 
21357 	bireinfop->bi_needrep = 1;
21358 	if (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_NOLOCAL|IPIF_ANYCAST)) {
21359 		if (bireinfop->bi_backup == NULL)
21360 			bireinfop->bi_backup = ipif;
21361 		return (B_FALSE);
21362 	}
21363 	return (B_TRUE);
21364 }
21365 
21366 /*
21367  * Create the broadcast IREs described by `bireinfop' on `ipif', and return
21368  * them ala ire_check_and_create_bcast().
21369  */
21370 static ire_t **
21371 ipif_create_bcast(ipif_t *ipif, bcast_ireinfo_t *bireinfop, ire_t **irep)
21372 {
21373 	ipaddr_t mask, addr;
21374 
21375 	ASSERT(!bireinfop->bi_haverep && bireinfop->bi_needrep);
21376 
21377 	addr = bireinfop->bi_addr;
21378 	irep = ire_create_bcast(ipif, addr, irep);
21379 
21380 	switch (bireinfop->bi_type) {
21381 	case BCAST_NET:
21382 		mask = ip_net_mask(ipif->ipif_subnet);
21383 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21384 		break;
21385 	case BCAST_SUBNET:
21386 		mask = ipif->ipif_net_mask;
21387 		irep = ire_create_bcast(ipif, addr | ~mask, irep);
21388 		break;
21389 	}
21390 
21391 	bireinfop->bi_haverep = 1;
21392 	return (irep);
21393 }
21394 
21395 /*
21396  * Walk through all of the ipifs on `ill' that will be affected by `test_ipif'
21397  * going away, and determine if any of the broadcast IREs (named by `bireinfop')
21398  * that are going away are still needed.  If so, have ipif_create_bcast()
21399  * recreate them (except for the deprecated case, as explained below).
21400  */
21401 static ire_t **
21402 ill_create_bcast(ill_t *ill, ipif_t *test_ipif, bcast_ireinfo_t *bireinfo,
21403     ire_t **irep)
21404 {
21405 	int i;
21406 	ipif_t *ipif;
21407 
21408 	ASSERT(!ill->ill_isv6);
21409 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
21410 		/*
21411 		 * Skip this ipif if it's (a) the one being taken down, (b)
21412 		 * not in the same zone, or (c) has no valid local address.
21413 		 */
21414 		if (ipif == test_ipif ||
21415 		    ipif->ipif_zoneid != test_ipif->ipif_zoneid ||
21416 		    ipif->ipif_subnet == 0 ||
21417 		    (ipif->ipif_flags & (IPIF_UP|IPIF_BROADCAST|IPIF_NOXMIT)) !=
21418 		    (IPIF_UP|IPIF_BROADCAST))
21419 			continue;
21420 
21421 		/*
21422 		 * For each dying IRE that hasn't yet been replaced, see if
21423 		 * `ipif' needs it and whether the IRE should be recreated on
21424 		 * `ipif'.  If `ipif' is deprecated, ipif_consider_bcast()
21425 		 * will return B_FALSE even if `ipif' needs the IRE on the
21426 		 * hopes that we'll later find a needy non-deprecated ipif.
21427 		 * However, the ipif is recorded in bi_backup for possible
21428 		 * subsequent use by ipif_check_bcast_ires().
21429 		 */
21430 		for (i = 0; i < BCAST_COUNT; i++) {
21431 			if (!bireinfo[i].bi_willdie || bireinfo[i].bi_haverep)
21432 				continue;
21433 			if (!ipif_consider_bcast(ipif, &bireinfo[i]))
21434 				continue;
21435 			irep = ipif_create_bcast(ipif, &bireinfo[i], irep);
21436 		}
21437 
21438 		/*
21439 		 * If we've replaced all of the broadcast IREs that are going
21440 		 * to be taken down, we know we're done.
21441 		 */
21442 		for (i = 0; i < BCAST_COUNT; i++) {
21443 			if (bireinfo[i].bi_willdie && !bireinfo[i].bi_haverep)
21444 				break;
21445 		}
21446 		if (i == BCAST_COUNT)
21447 			break;
21448 	}
21449 	return (irep);
21450 }
21451 
21452 /*
21453  * Check if `test_ipif' (which is going away) is associated with any existing
21454  * broadcast IREs, and whether any other ipifs (e.g., on the same ill) were
21455  * using those broadcast IREs.  If so, recreate the broadcast IREs on one or
21456  * more of those other ipifs.  (The old IREs will be deleted in ipif_down().)
21457  *
21458  * This is necessary because broadcast IREs are shared.  In particular, a
21459  * given ill has one set of all-zeroes and all-ones broadcast IREs (for every
21460  * zone), plus one set of all-subnet-ones, all-subnet-zeroes, all-net-ones,
21461  * and all-net-zeroes for every net/subnet (and every zone) it has IPIF_UP
21462  * ipifs on.  Thus, if there are two IPIF_UP ipifs on the same subnet with the
21463  * same zone, they will share the same set of broadcast IREs.
21464  *
21465  * Note: the upper bound of 12 IREs comes from the worst case of replacing all
21466  * six pairs (loopback and non-loopback) of broadcast IREs (all-zeroes,
21467  * all-ones, subnet-zeroes, subnet-ones, net-zeroes, and net-ones).
21468  */
21469 static void
21470 ipif_check_bcast_ires(ipif_t *test_ipif)
21471 {
21472 	ill_t		*ill = test_ipif->ipif_ill;
21473 	ire_t		*ire, *ire_array[12]; 		/* see note above */
21474 	ire_t		**irep1, **irep = &ire_array[0];
21475 	uint_t 		i, willdie;
21476 	ipaddr_t	mask = ip_net_mask(test_ipif->ipif_subnet);
21477 	bcast_ireinfo_t	bireinfo[BCAST_COUNT];
21478 
21479 	ASSERT(!test_ipif->ipif_isv6);
21480 	ASSERT(IAM_WRITER_IPIF(test_ipif));
21481 
21482 	/*
21483 	 * No broadcast IREs for the LOOPBACK interface
21484 	 * or others such as point to point and IPIF_NOXMIT.
21485 	 */
21486 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
21487 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
21488 		return;
21489 
21490 	bzero(bireinfo, sizeof (bireinfo));
21491 	bireinfo[0].bi_type = BCAST_ALLZEROES;
21492 	bireinfo[0].bi_addr = 0;
21493 
21494 	bireinfo[1].bi_type = BCAST_ALLONES;
21495 	bireinfo[1].bi_addr = INADDR_BROADCAST;
21496 
21497 	bireinfo[2].bi_type = BCAST_NET;
21498 	bireinfo[2].bi_addr = test_ipif->ipif_subnet & mask;
21499 
21500 	if (test_ipif->ipif_net_mask != 0)
21501 		mask = test_ipif->ipif_net_mask;
21502 	bireinfo[3].bi_type = BCAST_SUBNET;
21503 	bireinfo[3].bi_addr = test_ipif->ipif_subnet & mask;
21504 
21505 	/*
21506 	 * Figure out what (if any) broadcast IREs will die as a result of
21507 	 * `test_ipif' going away.  If none will die, we're done.
21508 	 */
21509 	for (i = 0, willdie = 0; i < BCAST_COUNT; i++) {
21510 		ire = ire_ctable_lookup(bireinfo[i].bi_addr, 0, IRE_BROADCAST,
21511 		    test_ipif, ALL_ZONES, NULL,
21512 		    (MATCH_IRE_TYPE | MATCH_IRE_IPIF), ill->ill_ipst);
21513 		if (ire != NULL) {
21514 			willdie++;
21515 			bireinfo[i].bi_willdie = 1;
21516 			ire_refrele(ire);
21517 		}
21518 	}
21519 
21520 	if (willdie == 0)
21521 		return;
21522 
21523 	/*
21524 	 * Walk through all the ipifs that will be affected by the dying IREs,
21525 	 * and recreate the IREs as necessary.
21526 	 */
21527 	irep = ill_create_bcast(ill, test_ipif, bireinfo, irep);
21528 
21529 	/*
21530 	 * Scan through the set of broadcast IREs and see if there are any
21531 	 * that we need to replace that have not yet been replaced.  If so,
21532 	 * replace them using the appropriate backup ipif.
21533 	 */
21534 	for (i = 0; i < BCAST_COUNT; i++) {
21535 		if (bireinfo[i].bi_needrep && !bireinfo[i].bi_haverep)
21536 			irep = ipif_create_bcast(bireinfo[i].bi_backup,
21537 			    &bireinfo[i], irep);
21538 	}
21539 
21540 	/*
21541 	 * If we can't create all of them, don't add any of them.  (Code in
21542 	 * ip_wput_ire() and ire_to_ill() assumes that we always have a
21543 	 * non-loopback copy and loopback copy for a given address.)
21544 	 */
21545 	for (irep1 = irep; irep1 > ire_array; ) {
21546 		irep1--;
21547 		if (*irep1 == NULL) {
21548 			ip0dbg(("ipif_check_bcast_ires: can't create "
21549 			    "IRE_BROADCAST, memory allocation failure\n"));
21550 			while (irep > ire_array) {
21551 				irep--;
21552 				if (*irep != NULL)
21553 					ire_delete(*irep);
21554 			}
21555 			return;
21556 		}
21557 	}
21558 
21559 	for (irep1 = irep; irep1 > ire_array; ) {
21560 		irep1--;
21561 		if (ire_add(irep1, NULL, NULL, NULL, B_FALSE) == 0)
21562 			ire_refrele(*irep1);		/* Held in ire_add */
21563 	}
21564 }
21565 
21566 /*
21567  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
21568  * from lifr_flags and the name from lifr_name.
21569  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
21570  * since ipif_lookup_on_name uses the _isv6 flags when matching.
21571  * Returns EINPROGRESS when mp has been consumed by queueing it on
21572  * ill_pending_mp and the ioctl will complete in ip_rput.
21573  *
21574  * Can operate on either a module or a driver queue.
21575  * Returns an error if not a module queue.
21576  */
21577 /* ARGSUSED */
21578 int
21579 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21580     ip_ioctl_cmd_t *ipip, void *if_req)
21581 {
21582 	ill_t	*ill = q->q_ptr;
21583 	phyint_t *phyi;
21584 	ip_stack_t *ipst;
21585 	struct lifreq *lifr = if_req;
21586 
21587 	ASSERT(ipif != NULL);
21588 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
21589 
21590 	if (q->q_next == NULL) {
21591 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
21592 		return (EINVAL);
21593 	}
21594 
21595 	/*
21596 	 * If we are not writer on 'q' then this interface exists already
21597 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
21598 	 * so return EALREADY.
21599 	 */
21600 	if (ill != ipif->ipif_ill)
21601 		return (EALREADY);
21602 
21603 	if (ill->ill_name[0] != '\0')
21604 		return (EALREADY);
21605 
21606 	/*
21607 	 * Set all the flags. Allows all kinds of override. Provide some
21608 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
21609 	 * unless there is either multicast/broadcast support in the driver
21610 	 * or it is a pt-pt link.
21611 	 */
21612 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
21613 		/* Meaningless to IP thus don't allow them to be set. */
21614 		ip1dbg(("ip_setname: EINVAL 1\n"));
21615 		return (EINVAL);
21616 	}
21617 
21618 	/*
21619 	 * If there's another ill already with the requested name, ensure
21620 	 * that it's of the same type.	Otherwise, ill_phyint_reinit() will
21621 	 * fuse together two unrelated ills, which will cause chaos.
21622 	 */
21623 	ipst = ill->ill_ipst;
21624 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
21625 	    lifr->lifr_name, NULL);
21626 	if (phyi != NULL) {
21627 		ill_t *ill_mate = phyi->phyint_illv4;
21628 
21629 		if (ill_mate == NULL)
21630 			ill_mate = phyi->phyint_illv6;
21631 		ASSERT(ill_mate != NULL);
21632 
21633 		if (ill_mate->ill_media->ip_m_mac_type !=
21634 		    ill->ill_media->ip_m_mac_type) {
21635 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
21636 			    "use the same ill name on differing media\n"));
21637 			return (EINVAL);
21638 		}
21639 	}
21640 
21641 	/*
21642 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
21643 	 * ill_bcast_addr_length info.
21644 	 */
21645 	if (!ill->ill_needs_attach &&
21646 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
21647 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
21648 	    ill->ill_bcast_addr_length == 0)) {
21649 		/* Link not broadcast/pt-pt capable i.e. no multicast */
21650 		ip1dbg(("ip_setname: EINVAL 2\n"));
21651 		return (EINVAL);
21652 	}
21653 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
21654 	    ((lifr->lifr_flags & IFF_IPV6) ||
21655 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
21656 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
21657 		ip1dbg(("ip_setname: EINVAL 3\n"));
21658 		return (EINVAL);
21659 	}
21660 	if (lifr->lifr_flags & IFF_UP) {
21661 		/* Can only be set with SIOCSLIFFLAGS */
21662 		ip1dbg(("ip_setname: EINVAL 4\n"));
21663 		return (EINVAL);
21664 	}
21665 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
21666 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
21667 		ip1dbg(("ip_setname: EINVAL 5\n"));
21668 		return (EINVAL);
21669 	}
21670 	/*
21671 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
21672 	 */
21673 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
21674 	    !(lifr->lifr_flags & IFF_IPV6) &&
21675 	    !(ipif->ipif_isv6)) {
21676 		ip1dbg(("ip_setname: EINVAL 6\n"));
21677 		return (EINVAL);
21678 	}
21679 
21680 	/*
21681 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
21682 	 * we have all the flags here. So, we assign rather than we OR.
21683 	 * We can't OR the flags here because we don't want to set
21684 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
21685 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
21686 	 * on lifr_flags value here.
21687 	 */
21688 	/*
21689 	 * This ill has not been inserted into the global list.
21690 	 * So we are still single threaded and don't need any lock
21691 	 */
21692 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & ~IFF_DUPLICATE;
21693 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
21694 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
21695 
21696 	/* We started off as V4. */
21697 	if (ill->ill_flags & ILLF_IPV6) {
21698 		ill->ill_phyint->phyint_illv6 = ill;
21699 		ill->ill_phyint->phyint_illv4 = NULL;
21700 	}
21701 
21702 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
21703 }
21704 
21705 /* ARGSUSED */
21706 int
21707 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21708     ip_ioctl_cmd_t *ipip, void *if_req)
21709 {
21710 	/*
21711 	 * ill_phyint_reinit merged the v4 and v6 into a single
21712 	 * ipsq. Could also have become part of a ipmp group in the
21713 	 * process, and we might not have been able to complete the
21714 	 * slifname in ipif_set_values, if we could not become
21715 	 * exclusive.  If so restart it here
21716 	 */
21717 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
21718 }
21719 
21720 /*
21721  * Return a pointer to the ipif which matches the index, IP version type and
21722  * zoneid.
21723  */
21724 ipif_t *
21725 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
21726     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst)
21727 {
21728 	ill_t	*ill;
21729 	ipif_t	*ipif = NULL;
21730 
21731 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
21732 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
21733 
21734 	if (err != NULL)
21735 		*err = 0;
21736 
21737 	ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst);
21738 	if (ill != NULL) {
21739 		mutex_enter(&ill->ill_lock);
21740 		for (ipif = ill->ill_ipif; ipif != NULL;
21741 		    ipif = ipif->ipif_next) {
21742 			if (IPIF_CAN_LOOKUP(ipif) && (zoneid == ALL_ZONES ||
21743 			    zoneid == ipif->ipif_zoneid ||
21744 			    ipif->ipif_zoneid == ALL_ZONES)) {
21745 				ipif_refhold_locked(ipif);
21746 				break;
21747 			}
21748 		}
21749 		mutex_exit(&ill->ill_lock);
21750 		ill_refrele(ill);
21751 		if (ipif == NULL && err != NULL)
21752 			*err = ENXIO;
21753 	}
21754 	return (ipif);
21755 }
21756 
21757 typedef struct conn_change_s {
21758 	uint_t cc_old_ifindex;
21759 	uint_t cc_new_ifindex;
21760 } conn_change_t;
21761 
21762 /*
21763  * ipcl_walk function for changing interface index.
21764  */
21765 static void
21766 conn_change_ifindex(conn_t *connp, caddr_t arg)
21767 {
21768 	conn_change_t *connc;
21769 	uint_t old_ifindex;
21770 	uint_t new_ifindex;
21771 	int i;
21772 	ilg_t *ilg;
21773 
21774 	connc = (conn_change_t *)arg;
21775 	old_ifindex = connc->cc_old_ifindex;
21776 	new_ifindex = connc->cc_new_ifindex;
21777 
21778 	if (connp->conn_orig_bound_ifindex == old_ifindex)
21779 		connp->conn_orig_bound_ifindex = new_ifindex;
21780 
21781 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
21782 		connp->conn_orig_multicast_ifindex = new_ifindex;
21783 
21784 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
21785 		ilg = &connp->conn_ilg[i];
21786 		if (ilg->ilg_orig_ifindex == old_ifindex)
21787 			ilg->ilg_orig_ifindex = new_ifindex;
21788 	}
21789 }
21790 
21791 /*
21792  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
21793  * to new_index if it matches the old_index.
21794  *
21795  * Failovers typically happen within a group of ills. But somebody
21796  * can remove an ill from the group after a failover happened. If
21797  * we are setting the ifindex after this, we potentially need to
21798  * look at all the ills rather than just the ones in the group.
21799  * We cut down the work by looking at matching ill_net_types
21800  * and ill_types as we could not possibly grouped them together.
21801  */
21802 static void
21803 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
21804 {
21805 	ill_t *ill;
21806 	ipif_t *ipif;
21807 	uint_t old_ifindex;
21808 	uint_t new_ifindex;
21809 	ilm_t *ilm;
21810 	ill_walk_context_t ctx;
21811 	ip_stack_t	*ipst = ill_orig->ill_ipst;
21812 
21813 	old_ifindex = connc->cc_old_ifindex;
21814 	new_ifindex = connc->cc_new_ifindex;
21815 
21816 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
21817 	ill = ILL_START_WALK_ALL(&ctx, ipst);
21818 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
21819 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
21820 		    (ill_orig->ill_type != ill->ill_type)) {
21821 			continue;
21822 		}
21823 		for (ipif = ill->ill_ipif; ipif != NULL;
21824 		    ipif = ipif->ipif_next) {
21825 			if (ipif->ipif_orig_ifindex == old_ifindex)
21826 				ipif->ipif_orig_ifindex = new_ifindex;
21827 		}
21828 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
21829 			if (ilm->ilm_orig_ifindex == old_ifindex)
21830 				ilm->ilm_orig_ifindex = new_ifindex;
21831 		}
21832 	}
21833 	rw_exit(&ipst->ips_ill_g_lock);
21834 }
21835 
21836 /*
21837  * We first need to ensure that the new index is unique, and
21838  * then carry the change across both v4 and v6 ill representation
21839  * of the physical interface.
21840  */
21841 /* ARGSUSED */
21842 int
21843 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21844     ip_ioctl_cmd_t *ipip, void *ifreq)
21845 {
21846 	ill_t		*ill;
21847 	ill_t		*ill_other;
21848 	phyint_t	*phyi;
21849 	int		old_index;
21850 	conn_change_t	connc;
21851 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21852 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21853 	uint_t	index;
21854 	ill_t	*ill_v4;
21855 	ill_t	*ill_v6;
21856 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
21857 
21858 	if (ipip->ipi_cmd_type == IF_CMD)
21859 		index = ifr->ifr_index;
21860 	else
21861 		index = lifr->lifr_index;
21862 
21863 	/*
21864 	 * Only allow on physical interface. Also, index zero is illegal.
21865 	 *
21866 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
21867 	 *
21868 	 * 1) If PHYI_FAILED is set, a failover could have happened which
21869 	 *    implies a possible failback might have to happen. As failback
21870 	 *    depends on the old index, we should fail setting the index.
21871 	 *
21872 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
21873 	 *    any addresses or multicast memberships are failed over to
21874 	 *    a non-STANDBY interface. As failback depends on the old
21875 	 *    index, we should fail setting the index for this case also.
21876 	 *
21877 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
21878 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
21879 	 */
21880 	ill = ipif->ipif_ill;
21881 	phyi = ill->ill_phyint;
21882 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
21883 	    ipif->ipif_id != 0 || index == 0) {
21884 		return (EINVAL);
21885 	}
21886 	old_index = phyi->phyint_ifindex;
21887 
21888 	/* If the index is not changing, no work to do */
21889 	if (old_index == index)
21890 		return (0);
21891 
21892 	/*
21893 	 * Use ill_lookup_on_ifindex to determine if the
21894 	 * new index is unused and if so allow the change.
21895 	 */
21896 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL,
21897 	    ipst);
21898 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL,
21899 	    ipst);
21900 	if (ill_v6 != NULL || ill_v4 != NULL) {
21901 		if (ill_v4 != NULL)
21902 			ill_refrele(ill_v4);
21903 		if (ill_v6 != NULL)
21904 			ill_refrele(ill_v6);
21905 		return (EBUSY);
21906 	}
21907 
21908 	/*
21909 	 * The new index is unused. Set it in the phyint.
21910 	 * Locate the other ill so that we can send a routing
21911 	 * sockets message.
21912 	 */
21913 	if (ill->ill_isv6) {
21914 		ill_other = phyi->phyint_illv4;
21915 	} else {
21916 		ill_other = phyi->phyint_illv6;
21917 	}
21918 
21919 	phyi->phyint_ifindex = index;
21920 
21921 	/* Update SCTP's ILL list */
21922 	sctp_ill_reindex(ill, old_index);
21923 
21924 	connc.cc_old_ifindex = old_index;
21925 	connc.cc_new_ifindex = index;
21926 	ip_change_ifindex(ill, &connc);
21927 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst);
21928 
21929 	/* Send the routing sockets message */
21930 	ip_rts_ifmsg(ipif);
21931 	if (ill_other != NULL)
21932 		ip_rts_ifmsg(ill_other->ill_ipif);
21933 
21934 	return (0);
21935 }
21936 
21937 /* ARGSUSED */
21938 int
21939 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21940     ip_ioctl_cmd_t *ipip, void *ifreq)
21941 {
21942 	struct ifreq	*ifr = (struct ifreq *)ifreq;
21943 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21944 
21945 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
21946 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21947 	/* Get the interface index */
21948 	if (ipip->ipi_cmd_type == IF_CMD) {
21949 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21950 	} else {
21951 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
21952 	}
21953 	return (0);
21954 }
21955 
21956 /* ARGSUSED */
21957 int
21958 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21959     ip_ioctl_cmd_t *ipip, void *ifreq)
21960 {
21961 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21962 
21963 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
21964 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21965 	/* Get the interface zone */
21966 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21967 	lifr->lifr_zoneid = ipif->ipif_zoneid;
21968 	return (0);
21969 }
21970 
21971 /*
21972  * Set the zoneid of an interface.
21973  */
21974 /* ARGSUSED */
21975 int
21976 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21977     ip_ioctl_cmd_t *ipip, void *ifreq)
21978 {
21979 	struct lifreq	*lifr = (struct lifreq *)ifreq;
21980 	int err = 0;
21981 	boolean_t need_up = B_FALSE;
21982 	zone_t *zptr;
21983 	zone_status_t status;
21984 	zoneid_t zoneid;
21985 
21986 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21987 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
21988 		if (!is_system_labeled())
21989 			return (ENOTSUP);
21990 		zoneid = GLOBAL_ZONEID;
21991 	}
21992 
21993 	/* cannot assign instance zero to a non-global zone */
21994 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
21995 		return (ENOTSUP);
21996 
21997 	/*
21998 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
21999 	 * the event of a race with the zone shutdown processing, since IP
22000 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
22001 	 * interface will be cleaned up even if the zone is shut down
22002 	 * immediately after the status check. If the interface can't be brought
22003 	 * down right away, and the zone is shut down before the restart
22004 	 * function is called, we resolve the possible races by rechecking the
22005 	 * zone status in the restart function.
22006 	 */
22007 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22008 		return (EINVAL);
22009 	status = zone_status_get(zptr);
22010 	zone_rele(zptr);
22011 
22012 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
22013 		return (EINVAL);
22014 
22015 	if (ipif->ipif_flags & IPIF_UP) {
22016 		/*
22017 		 * If the interface is already marked up,
22018 		 * we call ipif_down which will take care
22019 		 * of ditching any IREs that have been set
22020 		 * up based on the old interface address.
22021 		 */
22022 		err = ipif_logical_down(ipif, q, mp);
22023 		if (err == EINPROGRESS)
22024 			return (err);
22025 		ipif_down_tail(ipif);
22026 		need_up = B_TRUE;
22027 	}
22028 
22029 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
22030 	return (err);
22031 }
22032 
22033 static int
22034 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
22035     queue_t *q, mblk_t *mp, boolean_t need_up)
22036 {
22037 	int	err = 0;
22038 	ip_stack_t	*ipst;
22039 
22040 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
22041 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22042 
22043 	if (CONN_Q(q))
22044 		ipst = CONNQ_TO_IPST(q);
22045 	else
22046 		ipst = ILLQ_TO_IPST(q);
22047 
22048 	/*
22049 	 * For exclusive stacks we don't allow a different zoneid than
22050 	 * global.
22051 	 */
22052 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
22053 	    zoneid != GLOBAL_ZONEID)
22054 		return (EINVAL);
22055 
22056 	/* Set the new zone id. */
22057 	ipif->ipif_zoneid = zoneid;
22058 
22059 	/* Update sctp list */
22060 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
22061 
22062 	if (need_up) {
22063 		/*
22064 		 * Now bring the interface back up.  If this
22065 		 * is the only IPIF for the ILL, ipif_up
22066 		 * will have to re-bind to the device, so
22067 		 * we may get back EINPROGRESS, in which
22068 		 * case, this IOCTL will get completed in
22069 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
22070 		 */
22071 		err = ipif_up(ipif, q, mp);
22072 	}
22073 	return (err);
22074 }
22075 
22076 /* ARGSUSED */
22077 int
22078 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22079     ip_ioctl_cmd_t *ipip, void *if_req)
22080 {
22081 	struct lifreq *lifr = (struct lifreq *)if_req;
22082 	zoneid_t zoneid;
22083 	zone_t *zptr;
22084 	zone_status_t status;
22085 
22086 	ASSERT(ipif->ipif_id != 0);
22087 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
22088 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
22089 		zoneid = GLOBAL_ZONEID;
22090 
22091 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
22092 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22093 
22094 	/*
22095 	 * We recheck the zone status to resolve the following race condition:
22096 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
22097 	 * 2) hme0:1 is up and can't be brought down right away;
22098 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
22099 	 * 3) zone "myzone" is halted; the zone status switches to
22100 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
22101 	 * the interfaces to remove - hme0:1 is not returned because it's not
22102 	 * yet in "myzone", so it won't be removed;
22103 	 * 4) the restart function for SIOCSLIFZONE is called; without the
22104 	 * status check here, we would have hme0:1 in "myzone" after it's been
22105 	 * destroyed.
22106 	 * Note that if the status check fails, we need to bring the interface
22107 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
22108 	 * ipif_up_done[_v6]().
22109 	 */
22110 	status = ZONE_IS_UNINITIALIZED;
22111 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
22112 		status = zone_status_get(zptr);
22113 		zone_rele(zptr);
22114 	}
22115 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
22116 		if (ipif->ipif_isv6) {
22117 			(void) ipif_up_done_v6(ipif);
22118 		} else {
22119 			(void) ipif_up_done(ipif);
22120 		}
22121 		return (EINVAL);
22122 	}
22123 
22124 	ipif_down_tail(ipif);
22125 
22126 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
22127 	    B_TRUE));
22128 }
22129 
22130 /* ARGSUSED */
22131 int
22132 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22133 	ip_ioctl_cmd_t *ipip, void *ifreq)
22134 {
22135 	struct lifreq	*lifr = ifreq;
22136 
22137 	ASSERT(q->q_next == NULL);
22138 	ASSERT(CONN_Q(q));
22139 
22140 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
22141 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
22142 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
22143 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
22144 
22145 	return (0);
22146 }
22147 
22148 
22149 /* Find the previous ILL in this usesrc group */
22150 static ill_t *
22151 ill_prev_usesrc(ill_t *uill)
22152 {
22153 	ill_t *ill;
22154 
22155 	for (ill = uill->ill_usesrc_grp_next;
22156 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
22157 	    ill = ill->ill_usesrc_grp_next)
22158 		/* do nothing */;
22159 	return (ill);
22160 }
22161 
22162 /*
22163  * Release all members of the usesrc group. This routine is called
22164  * from ill_delete when the interface being unplumbed is the
22165  * group head.
22166  */
22167 static void
22168 ill_disband_usesrc_group(ill_t *uill)
22169 {
22170 	ill_t *next_ill, *tmp_ill;
22171 	ip_stack_t	*ipst = uill->ill_ipst;
22172 
22173 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22174 	next_ill = uill->ill_usesrc_grp_next;
22175 
22176 	do {
22177 		ASSERT(next_ill != NULL);
22178 		tmp_ill = next_ill->ill_usesrc_grp_next;
22179 		ASSERT(tmp_ill != NULL);
22180 		next_ill->ill_usesrc_grp_next = NULL;
22181 		next_ill->ill_usesrc_ifindex = 0;
22182 		next_ill = tmp_ill;
22183 	} while (next_ill->ill_usesrc_ifindex != 0);
22184 	uill->ill_usesrc_grp_next = NULL;
22185 }
22186 
22187 /*
22188  * Remove the client usesrc ILL from the list and relink to a new list
22189  */
22190 int
22191 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
22192 {
22193 	ill_t *ill, *tmp_ill;
22194 	ip_stack_t	*ipst = ucill->ill_ipst;
22195 
22196 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
22197 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
22198 
22199 	/*
22200 	 * Check if the usesrc client ILL passed in is not already
22201 	 * in use as a usesrc ILL i.e one whose source address is
22202 	 * in use OR a usesrc ILL is not already in use as a usesrc
22203 	 * client ILL
22204 	 */
22205 	if ((ucill->ill_usesrc_ifindex == 0) ||
22206 	    (uill->ill_usesrc_ifindex != 0)) {
22207 		return (-1);
22208 	}
22209 
22210 	ill = ill_prev_usesrc(ucill);
22211 	ASSERT(ill->ill_usesrc_grp_next != NULL);
22212 
22213 	/* Remove from the current list */
22214 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
22215 		/* Only two elements in the list */
22216 		ASSERT(ill->ill_usesrc_ifindex == 0);
22217 		ill->ill_usesrc_grp_next = NULL;
22218 	} else {
22219 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
22220 	}
22221 
22222 	if (ifindex == 0) {
22223 		ucill->ill_usesrc_ifindex = 0;
22224 		ucill->ill_usesrc_grp_next = NULL;
22225 		return (0);
22226 	}
22227 
22228 	ucill->ill_usesrc_ifindex = ifindex;
22229 	tmp_ill = uill->ill_usesrc_grp_next;
22230 	uill->ill_usesrc_grp_next = ucill;
22231 	ucill->ill_usesrc_grp_next =
22232 	    (tmp_ill != NULL) ? tmp_ill : uill;
22233 	return (0);
22234 }
22235 
22236 /*
22237  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
22238  * ip.c for locking details.
22239  */
22240 /* ARGSUSED */
22241 int
22242 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
22243     ip_ioctl_cmd_t *ipip, void *ifreq)
22244 {
22245 	struct lifreq *lifr = (struct lifreq *)ifreq;
22246 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
22247 	    ill_flag_changed = B_FALSE;
22248 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
22249 	int err = 0, ret;
22250 	uint_t ifindex;
22251 	phyint_t *us_phyint, *us_cli_phyint;
22252 	ipsq_t *ipsq = NULL;
22253 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
22254 
22255 	ASSERT(IAM_WRITER_IPIF(ipif));
22256 	ASSERT(q->q_next == NULL);
22257 	ASSERT(CONN_Q(q));
22258 
22259 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
22260 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
22261 
22262 	ASSERT(us_cli_phyint != NULL);
22263 
22264 	/*
22265 	 * If the client ILL is being used for IPMP, abort.
22266 	 * Note, this can be done before ipsq_try_enter since we are already
22267 	 * exclusive on this ILL
22268 	 */
22269 	if ((us_cli_phyint->phyint_groupname != NULL) ||
22270 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
22271 		return (EINVAL);
22272 	}
22273 
22274 	ifindex = lifr->lifr_index;
22275 	if (ifindex == 0) {
22276 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
22277 			/* non usesrc group interface, nothing to reset */
22278 			return (0);
22279 		}
22280 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
22281 		/* valid reset request */
22282 		reset_flg = B_TRUE;
22283 	}
22284 
22285 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
22286 	    ip_process_ioctl, &err, ipst);
22287 
22288 	if (usesrc_ill == NULL) {
22289 		return (err);
22290 	}
22291 
22292 	/*
22293 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
22294 	 * group nor can either of the interfaces be used for standy. So
22295 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
22296 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
22297 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
22298 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
22299 	 * the usesrc_cli_ill
22300 	 */
22301 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
22302 	    NEW_OP, B_TRUE);
22303 	if (ipsq == NULL) {
22304 		err = EINPROGRESS;
22305 		/* Operation enqueued on the ipsq of the usesrc ILL */
22306 		goto done;
22307 	}
22308 
22309 	/* Check if the usesrc_ill is used for IPMP */
22310 	us_phyint = usesrc_ill->ill_phyint;
22311 	if ((us_phyint->phyint_groupname != NULL) ||
22312 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
22313 		err = EINVAL;
22314 		goto done;
22315 	}
22316 
22317 	/*
22318 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
22319 	 * already a client then return EINVAL
22320 	 */
22321 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
22322 		err = EINVAL;
22323 		goto done;
22324 	}
22325 
22326 	/*
22327 	 * If the ill_usesrc_ifindex field is already set to what it needs to
22328 	 * be then this is a duplicate operation.
22329 	 */
22330 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
22331 		err = 0;
22332 		goto done;
22333 	}
22334 
22335 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
22336 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
22337 	    usesrc_ill->ill_isv6));
22338 
22339 	/*
22340 	 * The next step ensures that no new ires will be created referencing
22341 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
22342 	 * we go through an ire walk deleting all ire caches that reference
22343 	 * the client ill. New ires referencing the client ill that are added
22344 	 * to the ire table before the ILL_CHANGING flag is set, will be
22345 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
22346 	 * the client ill while the ILL_CHANGING flag is set will be failed
22347 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
22348 	 * checks (under the ill_g_usesrc_lock) that the ire being added
22349 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
22350 	 * belong to the same usesrc group.
22351 	 */
22352 	mutex_enter(&usesrc_cli_ill->ill_lock);
22353 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
22354 	mutex_exit(&usesrc_cli_ill->ill_lock);
22355 	ill_flag_changed = B_TRUE;
22356 
22357 	if (ipif->ipif_isv6)
22358 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22359 		    ALL_ZONES, ipst);
22360 	else
22361 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
22362 		    ALL_ZONES, ipst);
22363 
22364 	/*
22365 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
22366 	 * and the ill_usesrc_ifindex fields
22367 	 */
22368 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
22369 
22370 	if (reset_flg) {
22371 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
22372 		if (ret != 0) {
22373 			err = EINVAL;
22374 		}
22375 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
22376 		goto done;
22377 	}
22378 
22379 	/*
22380 	 * Four possibilities to consider:
22381 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
22382 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
22383 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
22384 	 * 4. Both are part of their respective usesrc groups
22385 	 */
22386 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
22387 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22388 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
22389 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22390 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22391 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
22392 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
22393 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
22394 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
22395 		/* Insert at head of list */
22396 		usesrc_cli_ill->ill_usesrc_grp_next =
22397 		    usesrc_ill->ill_usesrc_grp_next;
22398 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
22399 	} else {
22400 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
22401 		    ifindex);
22402 		if (ret != 0)
22403 			err = EINVAL;
22404 	}
22405 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
22406 
22407 done:
22408 	if (ill_flag_changed) {
22409 		mutex_enter(&usesrc_cli_ill->ill_lock);
22410 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
22411 		mutex_exit(&usesrc_cli_ill->ill_lock);
22412 	}
22413 	if (ipsq != NULL)
22414 		ipsq_exit(ipsq);
22415 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
22416 	ill_refrele(usesrc_ill);
22417 	return (err);
22418 }
22419 
22420 /*
22421  * comparison function used by avl.
22422  */
22423 static int
22424 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
22425 {
22426 
22427 	uint_t index;
22428 
22429 	ASSERT(phyip != NULL && index_ptr != NULL);
22430 
22431 	index = *((uint_t *)index_ptr);
22432 	/*
22433 	 * let the phyint with the lowest index be on top.
22434 	 */
22435 	if (((phyint_t *)phyip)->phyint_ifindex < index)
22436 		return (1);
22437 	if (((phyint_t *)phyip)->phyint_ifindex > index)
22438 		return (-1);
22439 	return (0);
22440 }
22441 
22442 /*
22443  * comparison function used by avl.
22444  */
22445 static int
22446 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
22447 {
22448 	ill_t *ill;
22449 	int res = 0;
22450 
22451 	ASSERT(phyip != NULL && name_ptr != NULL);
22452 
22453 	if (((phyint_t *)phyip)->phyint_illv4)
22454 		ill = ((phyint_t *)phyip)->phyint_illv4;
22455 	else
22456 		ill = ((phyint_t *)phyip)->phyint_illv6;
22457 	ASSERT(ill != NULL);
22458 
22459 	res = strcmp(ill->ill_name, (char *)name_ptr);
22460 	if (res > 0)
22461 		return (1);
22462 	else if (res < 0)
22463 		return (-1);
22464 	return (0);
22465 }
22466 /*
22467  * This function is called from ill_delete when the ill is being
22468  * unplumbed. We remove the reference from the phyint and we also
22469  * free the phyint when there are no more references to it.
22470  */
22471 static void
22472 ill_phyint_free(ill_t *ill)
22473 {
22474 	phyint_t *phyi;
22475 	phyint_t *next_phyint;
22476 	ipsq_t *cur_ipsq;
22477 	ip_stack_t	*ipst = ill->ill_ipst;
22478 
22479 	ASSERT(ill->ill_phyint != NULL);
22480 
22481 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22482 	phyi = ill->ill_phyint;
22483 	ill->ill_phyint = NULL;
22484 	/*
22485 	 * ill_init allocates a phyint always to store the copy
22486 	 * of flags relevant to phyint. At that point in time, we could
22487 	 * not assign the name and hence phyint_illv4/v6 could not be
22488 	 * initialized. Later in ipif_set_values, we assign the name to
22489 	 * the ill, at which point in time we assign phyint_illv4/v6.
22490 	 * Thus we don't rely on phyint_illv6 to be initialized always.
22491 	 */
22492 	if (ill->ill_flags & ILLF_IPV6) {
22493 		phyi->phyint_illv6 = NULL;
22494 	} else {
22495 		phyi->phyint_illv4 = NULL;
22496 	}
22497 	/*
22498 	 * ipif_down removes it from the group when the last ipif goes
22499 	 * down.
22500 	 */
22501 	ASSERT(ill->ill_group == NULL);
22502 
22503 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
22504 		return;
22505 
22506 	/*
22507 	 * Make sure this phyint was put in the list.
22508 	 */
22509 	if (phyi->phyint_ifindex > 0) {
22510 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22511 		    phyi);
22512 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22513 		    phyi);
22514 	}
22515 	/*
22516 	 * remove phyint from the ipsq list.
22517 	 */
22518 	cur_ipsq = phyi->phyint_ipsq;
22519 	if (phyi == cur_ipsq->ipsq_phyint_list) {
22520 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
22521 	} else {
22522 		next_phyint = cur_ipsq->ipsq_phyint_list;
22523 		while (next_phyint != NULL) {
22524 			if (next_phyint->phyint_ipsq_next == phyi) {
22525 				next_phyint->phyint_ipsq_next =
22526 				    phyi->phyint_ipsq_next;
22527 				break;
22528 			}
22529 			next_phyint = next_phyint->phyint_ipsq_next;
22530 		}
22531 		ASSERT(next_phyint != NULL);
22532 	}
22533 	IPSQ_DEC_REF(cur_ipsq, ipst);
22534 
22535 	if (phyi->phyint_groupname_len != 0) {
22536 		ASSERT(phyi->phyint_groupname != NULL);
22537 		mi_free(phyi->phyint_groupname);
22538 	}
22539 	mi_free(phyi);
22540 }
22541 
22542 /*
22543  * Attach the ill to the phyint structure which can be shared by both
22544  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
22545  * function is called from ipif_set_values and ill_lookup_on_name (for
22546  * loopback) where we know the name of the ill. We lookup the ill and if
22547  * there is one present already with the name use that phyint. Otherwise
22548  * reuse the one allocated by ill_init.
22549  */
22550 static void
22551 ill_phyint_reinit(ill_t *ill)
22552 {
22553 	boolean_t isv6 = ill->ill_isv6;
22554 	phyint_t *phyi_old;
22555 	phyint_t *phyi;
22556 	avl_index_t where = 0;
22557 	ill_t	*ill_other = NULL;
22558 	ipsq_t	*ipsq;
22559 	ip_stack_t	*ipst = ill->ill_ipst;
22560 
22561 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
22562 
22563 	phyi_old = ill->ill_phyint;
22564 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
22565 	    phyi_old->phyint_illv6 == NULL));
22566 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
22567 	    phyi_old->phyint_illv4 == NULL));
22568 	ASSERT(phyi_old->phyint_ifindex == 0);
22569 
22570 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22571 	    ill->ill_name, &where);
22572 
22573 	/*
22574 	 * 1. We grabbed the ill_g_lock before inserting this ill into
22575 	 *    the global list of ills. So no other thread could have located
22576 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
22577 	 * 2. Now locate the other protocol instance of this ill.
22578 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
22579 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
22580 	 *    of neither ill can change.
22581 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
22582 	 *    other ill.
22583 	 * 5. Release all locks.
22584 	 */
22585 
22586 	/*
22587 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
22588 	 * we are initializing IPv4.
22589 	 */
22590 	if (phyi != NULL) {
22591 		ill_other = (isv6) ? phyi->phyint_illv4 :
22592 		    phyi->phyint_illv6;
22593 		ASSERT(ill_other->ill_phyint != NULL);
22594 		ASSERT((isv6 && !ill_other->ill_isv6) ||
22595 		    (!isv6 && ill_other->ill_isv6));
22596 		GRAB_ILL_LOCKS(ill, ill_other);
22597 		/*
22598 		 * We are potentially throwing away phyint_flags which
22599 		 * could be different from the one that we obtain from
22600 		 * ill_other->ill_phyint. But it is okay as we are assuming
22601 		 * that the state maintained within IP is correct.
22602 		 */
22603 		mutex_enter(&phyi->phyint_lock);
22604 		if (isv6) {
22605 			ASSERT(phyi->phyint_illv6 == NULL);
22606 			phyi->phyint_illv6 = ill;
22607 		} else {
22608 			ASSERT(phyi->phyint_illv4 == NULL);
22609 			phyi->phyint_illv4 = ill;
22610 		}
22611 		/*
22612 		 * This is a new ill, currently undergoing SLIFNAME
22613 		 * So we could not have joined an IPMP group until now.
22614 		 */
22615 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
22616 		    phyi_old->phyint_groupname == NULL);
22617 
22618 		/*
22619 		 * This phyi_old is going away. Decref ipsq_refs and
22620 		 * assert it is zero. The ipsq itself will be freed in
22621 		 * ipsq_exit
22622 		 */
22623 		ipsq = phyi_old->phyint_ipsq;
22624 		IPSQ_DEC_REF(ipsq, ipst);
22625 		ASSERT(ipsq->ipsq_refs == 0);
22626 		/* Get the singleton phyint out of the ipsq list */
22627 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
22628 		ipsq->ipsq_phyint_list = NULL;
22629 		phyi_old->phyint_illv4 = NULL;
22630 		phyi_old->phyint_illv6 = NULL;
22631 		mi_free(phyi_old);
22632 	} else {
22633 		mutex_enter(&ill->ill_lock);
22634 		/*
22635 		 * We don't need to acquire any lock, since
22636 		 * the ill is not yet visible globally  and we
22637 		 * have not yet released the ill_g_lock.
22638 		 */
22639 		phyi = phyi_old;
22640 		mutex_enter(&phyi->phyint_lock);
22641 		/* XXX We need a recovery strategy here. */
22642 		if (!phyint_assign_ifindex(phyi, ipst))
22643 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
22644 
22645 		/* No IPMP group yet, thus the hook uses the ifindex */
22646 		phyi->phyint_hook_ifindex = phyi->phyint_ifindex;
22647 
22648 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
22649 		    (void *)phyi, where);
22650 
22651 		(void) avl_find(&ipst->ips_phyint_g_list->
22652 		    phyint_list_avl_by_index,
22653 		    &phyi->phyint_ifindex, &where);
22654 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
22655 		    (void *)phyi, where);
22656 	}
22657 
22658 	/*
22659 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
22660 	 * pending mp is not affected because that is per ill basis.
22661 	 */
22662 	ill->ill_phyint = phyi;
22663 
22664 	/*
22665 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
22666 	 * We do this here as when the first ipif was allocated,
22667 	 * ipif_allocate does not know the right interface index.
22668 	 */
22669 
22670 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
22671 	/*
22672 	 * Now that the phyint's ifindex has been assigned, complete the
22673 	 * remaining
22674 	 */
22675 
22676 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
22677 	if (ill->ill_isv6) {
22678 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
22679 		    ill->ill_phyint->phyint_ifindex;
22680 		ill->ill_mcast_type = ipst->ips_mld_max_version;
22681 	} else {
22682 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
22683 	}
22684 
22685 	/*
22686 	 * Generate an event within the hooks framework to indicate that
22687 	 * a new interface has just been added to IP.  For this event to
22688 	 * be generated, the network interface must, at least, have an
22689 	 * ifindex assigned to it.
22690 	 *
22691 	 * This needs to be run inside the ill_g_lock perimeter to ensure
22692 	 * that the ordering of delivered events to listeners matches the
22693 	 * order of them in the kernel.
22694 	 *
22695 	 * This function could be called from ill_lookup_on_name. In that case
22696 	 * the interface is loopback "lo", which will not generate a NIC event.
22697 	 */
22698 	if (ill->ill_name_length <= 2 ||
22699 	    ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') {
22700 		/*
22701 		 * Generate nic plumb event for ill_name even if
22702 		 * ipmp_hook_emulation is set. That avoids generating events
22703 		 * for the ill_names should ipmp_hook_emulation be turned on
22704 		 * later.
22705 		 */
22706 		ill_nic_info_plumb(ill, B_FALSE);
22707 	}
22708 	RELEASE_ILL_LOCKS(ill, ill_other);
22709 	mutex_exit(&phyi->phyint_lock);
22710 }
22711 
22712 /*
22713  * Allocate a NE_PLUMB nic info event and store in the ill.
22714  * If 'group' is set we do it for the group name, otherwise the ill name.
22715  * It will be sent when we leave the ipsq.
22716  */
22717 void
22718 ill_nic_info_plumb(ill_t *ill, boolean_t group)
22719 {
22720 	phyint_t	*phyi = ill->ill_phyint;
22721 	char		*name;
22722 	int		namelen;
22723 
22724 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22725 
22726 	if (group) {
22727 		ASSERT(phyi->phyint_groupname_len != 0);
22728 		namelen = phyi->phyint_groupname_len;
22729 		name = phyi->phyint_groupname;
22730 	} else {
22731 		namelen = ill->ill_name_length;
22732 		name = ill->ill_name;
22733 	}
22734 
22735 	(void) ill_hook_event_create(ill, 0, NE_PLUMB, name, namelen);
22736 }
22737 
22738 /*
22739  * Unhook the nic event message from the ill and enqueue it
22740  * into the nic event taskq.
22741  */
22742 void
22743 ill_nic_info_dispatch(ill_t *ill)
22744 {
22745 	hook_nic_event_t *info;
22746 
22747 	ASSERT(MUTEX_HELD(&ill->ill_lock));
22748 
22749 	if ((info = ill->ill_nic_event_info) != NULL) {
22750 		if (ddi_taskq_dispatch(eventq_queue_nic,
22751 		    ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) {
22752 			ip2dbg(("ill_nic_info_dispatch: "
22753 			    "ddi_taskq_dispatch failed\n"));
22754 			if (info->hne_data != NULL)
22755 				kmem_free(info->hne_data, info->hne_datalen);
22756 			kmem_free(info, sizeof (hook_nic_event_t));
22757 		}
22758 		ill->ill_nic_event_info = NULL;
22759 	}
22760 }
22761 
22762 /*
22763  * Notify any downstream modules of the name of this interface.
22764  * An M_IOCTL is used even though we don't expect a successful reply.
22765  * Any reply message from the driver (presumably an M_IOCNAK) will
22766  * eventually get discarded somewhere upstream.  The message format is
22767  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
22768  * to IP.
22769  */
22770 static void
22771 ip_ifname_notify(ill_t *ill, queue_t *q)
22772 {
22773 	mblk_t *mp1, *mp2;
22774 	struct iocblk *iocp;
22775 	struct lifreq *lifr;
22776 
22777 	mp1 = mkiocb(SIOCSLIFNAME);
22778 	if (mp1 == NULL)
22779 		return;
22780 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
22781 	if (mp2 == NULL) {
22782 		freeb(mp1);
22783 		return;
22784 	}
22785 
22786 	mp1->b_cont = mp2;
22787 	iocp = (struct iocblk *)mp1->b_rptr;
22788 	iocp->ioc_count = sizeof (struct lifreq);
22789 
22790 	lifr = (struct lifreq *)mp2->b_rptr;
22791 	mp2->b_wptr += sizeof (struct lifreq);
22792 	bzero(lifr, sizeof (struct lifreq));
22793 
22794 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
22795 	lifr->lifr_ppa = ill->ill_ppa;
22796 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
22797 
22798 	putnext(q, mp1);
22799 }
22800 
22801 static int
22802 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
22803 {
22804 	int err;
22805 	ip_stack_t	*ipst = ill->ill_ipst;
22806 
22807 	/* Set the obsolete NDD per-interface forwarding name. */
22808 	err = ill_set_ndd_name(ill);
22809 	if (err != 0) {
22810 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
22811 		    err);
22812 	}
22813 
22814 	/* Tell downstream modules where they are. */
22815 	ip_ifname_notify(ill, q);
22816 
22817 	/*
22818 	 * ill_dl_phys returns EINPROGRESS in the usual case.
22819 	 * Error cases are ENOMEM ...
22820 	 */
22821 	err = ill_dl_phys(ill, ipif, mp, q);
22822 
22823 	/*
22824 	 * If there is no IRE expiration timer running, get one started.
22825 	 * igmp and mld timers will be triggered by the first multicast
22826 	 */
22827 	if (ipst->ips_ip_ire_expire_id == 0) {
22828 		/*
22829 		 * acquire the lock and check again.
22830 		 */
22831 		mutex_enter(&ipst->ips_ip_trash_timer_lock);
22832 		if (ipst->ips_ip_ire_expire_id == 0) {
22833 			ipst->ips_ip_ire_expire_id = timeout(
22834 			    ip_trash_timer_expire, ipst,
22835 			    MSEC_TO_TICK(ipst->ips_ip_timer_interval));
22836 		}
22837 		mutex_exit(&ipst->ips_ip_trash_timer_lock);
22838 	}
22839 
22840 	if (ill->ill_isv6) {
22841 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
22842 		if (ipst->ips_mld_slowtimeout_id == 0) {
22843 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
22844 			    (void *)ipst,
22845 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22846 		}
22847 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
22848 	} else {
22849 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
22850 		if (ipst->ips_igmp_slowtimeout_id == 0) {
22851 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
22852 			    (void *)ipst,
22853 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
22854 		}
22855 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
22856 	}
22857 
22858 	return (err);
22859 }
22860 
22861 /*
22862  * Common routine for ppa and ifname setting. Should be called exclusive.
22863  *
22864  * Returns EINPROGRESS when mp has been consumed by queueing it on
22865  * ill_pending_mp and the ioctl will complete in ip_rput.
22866  *
22867  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
22868  * the new name and new ppa in lifr_name and lifr_ppa respectively.
22869  * For SLIFNAME, we pass these values back to the userland.
22870  */
22871 static int
22872 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
22873 {
22874 	ill_t	*ill;
22875 	ipif_t	*ipif;
22876 	ipsq_t	*ipsq;
22877 	char	*ppa_ptr;
22878 	char	*old_ptr;
22879 	char	old_char;
22880 	int	error;
22881 	ip_stack_t	*ipst;
22882 
22883 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
22884 	ASSERT(q->q_next != NULL);
22885 	ASSERT(interf_name != NULL);
22886 
22887 	ill = (ill_t *)q->q_ptr;
22888 	ipst = ill->ill_ipst;
22889 
22890 	ASSERT(ill->ill_ipst != NULL);
22891 	ASSERT(ill->ill_name[0] == '\0');
22892 	ASSERT(IAM_WRITER_ILL(ill));
22893 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
22894 	ASSERT(ill->ill_ppa == UINT_MAX);
22895 
22896 	/* The ppa is sent down by ifconfig or is chosen */
22897 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
22898 		return (EINVAL);
22899 	}
22900 
22901 	/*
22902 	 * make sure ppa passed in is same as ppa in the name.
22903 	 * This check is not made when ppa == UINT_MAX in that case ppa
22904 	 * in the name could be anything. System will choose a ppa and
22905 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
22906 	 */
22907 	if (*new_ppa_ptr != UINT_MAX) {
22908 		/* stoi changes the pointer */
22909 		old_ptr = ppa_ptr;
22910 		/*
22911 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
22912 		 * (they don't have an externally visible ppa).  We assign one
22913 		 * here so that we can manage the interface.  Note that in
22914 		 * the past this value was always 0 for DLPI 1 drivers.
22915 		 */
22916 		if (*new_ppa_ptr == 0)
22917 			*new_ppa_ptr = stoi(&old_ptr);
22918 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
22919 			return (EINVAL);
22920 	}
22921 	/*
22922 	 * terminate string before ppa
22923 	 * save char at that location.
22924 	 */
22925 	old_char = ppa_ptr[0];
22926 	ppa_ptr[0] = '\0';
22927 
22928 	ill->ill_ppa = *new_ppa_ptr;
22929 	/*
22930 	 * Finish as much work now as possible before calling ill_glist_insert
22931 	 * which makes the ill globally visible and also merges it with the
22932 	 * other protocol instance of this phyint. The remaining work is
22933 	 * done after entering the ipsq which may happen sometime later.
22934 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
22935 	 */
22936 	ipif = ill->ill_ipif;
22937 
22938 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
22939 	ipif_assign_seqid(ipif);
22940 
22941 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
22942 		ill->ill_flags |= ILLF_IPV4;
22943 
22944 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
22945 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
22946 
22947 	if (ill->ill_flags & ILLF_IPV6) {
22948 
22949 		ill->ill_isv6 = B_TRUE;
22950 		if (ill->ill_rq != NULL) {
22951 			ill->ill_rq->q_qinfo = &iprinitv6;
22952 			ill->ill_wq->q_qinfo = &ipwinitv6;
22953 		}
22954 
22955 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
22956 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
22957 		ipif->ipif_v6src_addr = ipv6_all_zeros;
22958 		ipif->ipif_v6subnet = ipv6_all_zeros;
22959 		ipif->ipif_v6net_mask = ipv6_all_zeros;
22960 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
22961 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
22962 		/*
22963 		 * point-to-point or Non-mulicast capable
22964 		 * interfaces won't do NUD unless explicitly
22965 		 * configured to do so.
22966 		 */
22967 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
22968 		    !(ill->ill_flags & ILLF_MULTICAST)) {
22969 			ill->ill_flags |= ILLF_NONUD;
22970 		}
22971 		/* Make sure IPv4 specific flag is not set on IPv6 if */
22972 		if (ill->ill_flags & ILLF_NOARP) {
22973 			/*
22974 			 * Note: xresolv interfaces will eventually need
22975 			 * NOARP set here as well, but that will require
22976 			 * those external resolvers to have some
22977 			 * knowledge of that flag and act appropriately.
22978 			 * Not to be changed at present.
22979 			 */
22980 			ill->ill_flags &= ~ILLF_NOARP;
22981 		}
22982 		/*
22983 		 * Set the ILLF_ROUTER flag according to the global
22984 		 * IPv6 forwarding policy.
22985 		 */
22986 		if (ipst->ips_ipv6_forward != 0)
22987 			ill->ill_flags |= ILLF_ROUTER;
22988 	} else if (ill->ill_flags & ILLF_IPV4) {
22989 		ill->ill_isv6 = B_FALSE;
22990 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
22991 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
22992 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
22993 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
22994 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
22995 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
22996 		/*
22997 		 * Set the ILLF_ROUTER flag according to the global
22998 		 * IPv4 forwarding policy.
22999 		 */
23000 		if (ipst->ips_ip_g_forward != 0)
23001 			ill->ill_flags |= ILLF_ROUTER;
23002 	}
23003 
23004 	ASSERT(ill->ill_phyint != NULL);
23005 
23006 	/*
23007 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
23008 	 * be completed in ill_glist_insert -> ill_phyint_reinit
23009 	 */
23010 	if (!ill_allocate_mibs(ill))
23011 		return (ENOMEM);
23012 
23013 	/*
23014 	 * Pick a default sap until we get the DL_INFO_ACK back from
23015 	 * the driver.
23016 	 */
23017 	if (ill->ill_sap == 0) {
23018 		if (ill->ill_isv6)
23019 			ill->ill_sap  = IP6_DL_SAP;
23020 		else
23021 			ill->ill_sap  = IP_DL_SAP;
23022 	}
23023 
23024 	ill->ill_ifname_pending = 1;
23025 	ill->ill_ifname_pending_err = 0;
23026 
23027 	ill_refhold(ill);
23028 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
23029 	if ((error = ill_glist_insert(ill, interf_name,
23030 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
23031 		ill->ill_ppa = UINT_MAX;
23032 		ill->ill_name[0] = '\0';
23033 		/*
23034 		 * undo null termination done above.
23035 		 */
23036 		ppa_ptr[0] = old_char;
23037 		rw_exit(&ipst->ips_ill_g_lock);
23038 		ill_refrele(ill);
23039 		return (error);
23040 	}
23041 
23042 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
23043 
23044 	/*
23045 	 * When we return the buffer pointed to by interf_name should contain
23046 	 * the same name as in ill_name.
23047 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
23048 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
23049 	 * so copy full name and update the ppa ptr.
23050 	 * When ppa passed in != UINT_MAX all values are correct just undo
23051 	 * null termination, this saves a bcopy.
23052 	 */
23053 	if (*new_ppa_ptr == UINT_MAX) {
23054 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
23055 		*new_ppa_ptr = ill->ill_ppa;
23056 	} else {
23057 		/*
23058 		 * undo null termination done above.
23059 		 */
23060 		ppa_ptr[0] = old_char;
23061 	}
23062 
23063 	/* Let SCTP know about this ILL */
23064 	sctp_update_ill(ill, SCTP_ILL_INSERT);
23065 
23066 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
23067 	    B_TRUE);
23068 
23069 	rw_exit(&ipst->ips_ill_g_lock);
23070 	ill_refrele(ill);
23071 	if (ipsq == NULL)
23072 		return (EINPROGRESS);
23073 
23074 	/*
23075 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
23076 	 */
23077 	if (ipsq->ipsq_current_ipif == NULL)
23078 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
23079 	else
23080 		ASSERT(ipsq->ipsq_current_ipif == ipif);
23081 
23082 	error = ipif_set_values_tail(ill, ipif, mp, q);
23083 	ipsq_exit(ipsq);
23084 	if (error != 0 && error != EINPROGRESS) {
23085 		/*
23086 		 * restore previous values
23087 		 */
23088 		ill->ill_isv6 = B_FALSE;
23089 	}
23090 	return (error);
23091 }
23092 
23093 
23094 void
23095 ipif_init(ip_stack_t *ipst)
23096 {
23097 	hrtime_t hrt;
23098 	int i;
23099 
23100 	/*
23101 	 * Can't call drv_getparm here as it is too early in the boot.
23102 	 * As we use ipif_src_random just for picking a different
23103 	 * source address everytime, this need not be really random.
23104 	 */
23105 	hrt = gethrtime();
23106 	ipst->ips_ipif_src_random =
23107 	    ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
23108 
23109 	for (i = 0; i < MAX_G_HEADS; i++) {
23110 		ipst->ips_ill_g_heads[i].ill_g_list_head =
23111 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23112 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
23113 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
23114 	}
23115 
23116 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
23117 	    ill_phyint_compare_index,
23118 	    sizeof (phyint_t),
23119 	    offsetof(struct phyint, phyint_avl_by_index));
23120 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
23121 	    ill_phyint_compare_name,
23122 	    sizeof (phyint_t),
23123 	    offsetof(struct phyint, phyint_avl_by_name));
23124 }
23125 
23126 /*
23127  * Lookup the ipif corresponding to the onlink destination address. For
23128  * point-to-point interfaces, it matches with remote endpoint destination
23129  * address. For point-to-multipoint interfaces it only tries to match the
23130  * destination with the interface's subnet address. The longest, most specific
23131  * match is found to take care of such rare network configurations like -
23132  * le0: 129.146.1.1/16
23133  * le1: 129.146.2.2/24
23134  * It is used only by SO_DONTROUTE at the moment.
23135  */
23136 ipif_t *
23137 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst)
23138 {
23139 	ipif_t	*ipif, *best_ipif;
23140 	ill_t	*ill;
23141 	ill_walk_context_t ctx;
23142 
23143 	ASSERT(zoneid != ALL_ZONES);
23144 	best_ipif = NULL;
23145 
23146 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
23147 	ill = ILL_START_WALK_V4(&ctx, ipst);
23148 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
23149 		mutex_enter(&ill->ill_lock);
23150 		for (ipif = ill->ill_ipif; ipif != NULL;
23151 		    ipif = ipif->ipif_next) {
23152 			if (!IPIF_CAN_LOOKUP(ipif))
23153 				continue;
23154 			if (ipif->ipif_zoneid != zoneid &&
23155 			    ipif->ipif_zoneid != ALL_ZONES)
23156 				continue;
23157 			/*
23158 			 * Point-to-point case. Look for exact match with
23159 			 * destination address.
23160 			 */
23161 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
23162 				if (ipif->ipif_pp_dst_addr == addr) {
23163 					ipif_refhold_locked(ipif);
23164 					mutex_exit(&ill->ill_lock);
23165 					rw_exit(&ipst->ips_ill_g_lock);
23166 					if (best_ipif != NULL)
23167 						ipif_refrele(best_ipif);
23168 					return (ipif);
23169 				}
23170 			} else if (ipif->ipif_subnet == (addr &
23171 			    ipif->ipif_net_mask)) {
23172 				/*
23173 				 * Point-to-multipoint case. Looping through to
23174 				 * find the most specific match. If there are
23175 				 * multiple best match ipif's then prefer ipif's
23176 				 * that are UP. If there is only one best match
23177 				 * ipif and it is DOWN we must still return it.
23178 				 */
23179 				if ((best_ipif == NULL) ||
23180 				    (ipif->ipif_net_mask >
23181 				    best_ipif->ipif_net_mask) ||
23182 				    ((ipif->ipif_net_mask ==
23183 				    best_ipif->ipif_net_mask) &&
23184 				    ((ipif->ipif_flags & IPIF_UP) &&
23185 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
23186 					ipif_refhold_locked(ipif);
23187 					mutex_exit(&ill->ill_lock);
23188 					rw_exit(&ipst->ips_ill_g_lock);
23189 					if (best_ipif != NULL)
23190 						ipif_refrele(best_ipif);
23191 					best_ipif = ipif;
23192 					rw_enter(&ipst->ips_ill_g_lock,
23193 					    RW_READER);
23194 					mutex_enter(&ill->ill_lock);
23195 				}
23196 			}
23197 		}
23198 		mutex_exit(&ill->ill_lock);
23199 	}
23200 	rw_exit(&ipst->ips_ill_g_lock);
23201 	return (best_ipif);
23202 }
23203 
23204 
23205 /*
23206  * Save enough information so that we can recreate the IRE if
23207  * the interface goes down and then up.
23208  */
23209 static void
23210 ipif_save_ire(ipif_t *ipif, ire_t *ire)
23211 {
23212 	mblk_t	*save_mp;
23213 
23214 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
23215 	if (save_mp != NULL) {
23216 		ifrt_t	*ifrt;
23217 
23218 		save_mp->b_wptr += sizeof (ifrt_t);
23219 		ifrt = (ifrt_t *)save_mp->b_rptr;
23220 		bzero(ifrt, sizeof (ifrt_t));
23221 		ifrt->ifrt_type = ire->ire_type;
23222 		ifrt->ifrt_addr = ire->ire_addr;
23223 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
23224 		ifrt->ifrt_src_addr = ire->ire_src_addr;
23225 		ifrt->ifrt_mask = ire->ire_mask;
23226 		ifrt->ifrt_flags = ire->ire_flags;
23227 		ifrt->ifrt_max_frag = ire->ire_max_frag;
23228 		mutex_enter(&ipif->ipif_saved_ire_lock);
23229 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
23230 		ipif->ipif_saved_ire_mp = save_mp;
23231 		ipif->ipif_saved_ire_cnt++;
23232 		mutex_exit(&ipif->ipif_saved_ire_lock);
23233 	}
23234 }
23235 
23236 
23237 static void
23238 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
23239 {
23240 	mblk_t	**mpp;
23241 	mblk_t	*mp;
23242 	ifrt_t	*ifrt;
23243 
23244 	/* Remove from ipif_saved_ire_mp list if it is there */
23245 	mutex_enter(&ipif->ipif_saved_ire_lock);
23246 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
23247 	    mpp = &(*mpp)->b_cont) {
23248 		/*
23249 		 * On a given ipif, the triple of address, gateway and
23250 		 * mask is unique for each saved IRE (in the case of
23251 		 * ordinary interface routes, the gateway address is
23252 		 * all-zeroes).
23253 		 */
23254 		mp = *mpp;
23255 		ifrt = (ifrt_t *)mp->b_rptr;
23256 		if (ifrt->ifrt_addr == ire->ire_addr &&
23257 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
23258 		    ifrt->ifrt_mask == ire->ire_mask) {
23259 			*mpp = mp->b_cont;
23260 			ipif->ipif_saved_ire_cnt--;
23261 			freeb(mp);
23262 			break;
23263 		}
23264 	}
23265 	mutex_exit(&ipif->ipif_saved_ire_lock);
23266 }
23267 
23268 
23269 /*
23270  * IP multirouting broadcast routes handling
23271  * Append CGTP broadcast IREs to regular ones created
23272  * at ifconfig time.
23273  */
23274 static void
23275 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst)
23276 {
23277 	ire_t *ire_prim;
23278 
23279 	ASSERT(ire != NULL);
23280 	ASSERT(ire_dst != NULL);
23281 
23282 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23283 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23284 	if (ire_prim != NULL) {
23285 		/*
23286 		 * We are in the special case of broadcasts for
23287 		 * CGTP. We add an IRE_BROADCAST that holds
23288 		 * the RTF_MULTIRT flag, the destination
23289 		 * address of ire_dst and the low level
23290 		 * info of ire_prim. In other words, CGTP
23291 		 * broadcast is added to the redundant ipif.
23292 		 */
23293 		ipif_t *ipif_prim;
23294 		ire_t  *bcast_ire;
23295 
23296 		ipif_prim = ire_prim->ire_ipif;
23297 
23298 		ip2dbg(("ip_cgtp_filter_bcast_add: "
23299 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23300 		    (void *)ire_dst, (void *)ire_prim,
23301 		    (void *)ipif_prim));
23302 
23303 		bcast_ire = ire_create(
23304 		    (uchar_t *)&ire->ire_addr,
23305 		    (uchar_t *)&ip_g_all_ones,
23306 		    (uchar_t *)&ire_dst->ire_src_addr,
23307 		    (uchar_t *)&ire->ire_gateway_addr,
23308 		    &ipif_prim->ipif_mtu,
23309 		    NULL,
23310 		    ipif_prim->ipif_rq,
23311 		    ipif_prim->ipif_wq,
23312 		    IRE_BROADCAST,
23313 		    ipif_prim,
23314 		    0,
23315 		    0,
23316 		    0,
23317 		    ire->ire_flags,
23318 		    &ire_uinfo_null,
23319 		    NULL,
23320 		    NULL,
23321 		    ipst);
23322 
23323 		if (bcast_ire != NULL) {
23324 
23325 			if (ire_add(&bcast_ire, NULL, NULL, NULL,
23326 			    B_FALSE) == 0) {
23327 				ip2dbg(("ip_cgtp_filter_bcast_add: "
23328 				    "added bcast_ire %p\n",
23329 				    (void *)bcast_ire));
23330 
23331 				ipif_save_ire(bcast_ire->ire_ipif,
23332 				    bcast_ire);
23333 				ire_refrele(bcast_ire);
23334 			}
23335 		}
23336 		ire_refrele(ire_prim);
23337 	}
23338 }
23339 
23340 
23341 /*
23342  * IP multirouting broadcast routes handling
23343  * Remove the broadcast ire
23344  */
23345 static void
23346 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
23347 {
23348 	ire_t *ire_dst;
23349 
23350 	ASSERT(ire != NULL);
23351 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
23352 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23353 	if (ire_dst != NULL) {
23354 		ire_t *ire_prim;
23355 
23356 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
23357 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
23358 		if (ire_prim != NULL) {
23359 			ipif_t *ipif_prim;
23360 			ire_t  *bcast_ire;
23361 
23362 			ipif_prim = ire_prim->ire_ipif;
23363 
23364 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
23365 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
23366 			    (void *)ire_dst, (void *)ire_prim,
23367 			    (void *)ipif_prim));
23368 
23369 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
23370 			    ire->ire_gateway_addr,
23371 			    IRE_BROADCAST,
23372 			    ipif_prim, ALL_ZONES,
23373 			    NULL,
23374 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
23375 			    MATCH_IRE_MASK, ipst);
23376 
23377 			if (bcast_ire != NULL) {
23378 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
23379 				    "looked up bcast_ire %p\n",
23380 				    (void *)bcast_ire));
23381 				ipif_remove_ire(bcast_ire->ire_ipif,
23382 				    bcast_ire);
23383 				ire_delete(bcast_ire);
23384 				ire_refrele(bcast_ire);
23385 			}
23386 			ire_refrele(ire_prim);
23387 		}
23388 		ire_refrele(ire_dst);
23389 	}
23390 }
23391 
23392 /*
23393  * IPsec hardware acceleration capabilities related functions.
23394  */
23395 
23396 /*
23397  * Free a per-ill IPsec capabilities structure.
23398  */
23399 static void
23400 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
23401 {
23402 	if (capab->auth_hw_algs != NULL)
23403 		kmem_free(capab->auth_hw_algs, capab->algs_size);
23404 	if (capab->encr_hw_algs != NULL)
23405 		kmem_free(capab->encr_hw_algs, capab->algs_size);
23406 	if (capab->encr_algparm != NULL)
23407 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
23408 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
23409 }
23410 
23411 /*
23412  * Allocate a new per-ill IPsec capabilities structure. This structure
23413  * is specific to an IPsec protocol (AH or ESP). It is implemented as
23414  * an array which specifies, for each algorithm, whether this algorithm
23415  * is supported by the ill or not.
23416  */
23417 static ill_ipsec_capab_t *
23418 ill_ipsec_capab_alloc(void)
23419 {
23420 	ill_ipsec_capab_t *capab;
23421 	uint_t nelems;
23422 
23423 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
23424 	if (capab == NULL)
23425 		return (NULL);
23426 
23427 	/* we need one bit per algorithm */
23428 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
23429 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
23430 
23431 	/* allocate memory to store algorithm flags */
23432 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23433 	if (capab->encr_hw_algs == NULL)
23434 		goto nomem;
23435 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
23436 	if (capab->auth_hw_algs == NULL)
23437 		goto nomem;
23438 	/*
23439 	 * Leave encr_algparm NULL for now since we won't need it half
23440 	 * the time
23441 	 */
23442 	return (capab);
23443 
23444 nomem:
23445 	ill_ipsec_capab_free(capab);
23446 	return (NULL);
23447 }
23448 
23449 /*
23450  * Resize capability array.  Since we're exclusive, this is OK.
23451  */
23452 static boolean_t
23453 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
23454 {
23455 	ipsec_capab_algparm_t *nalp, *oalp;
23456 	uint32_t olen, nlen;
23457 
23458 	oalp = capab->encr_algparm;
23459 	olen = capab->encr_algparm_size;
23460 
23461 	if (oalp != NULL) {
23462 		if (algid < capab->encr_algparm_end)
23463 			return (B_TRUE);
23464 	}
23465 
23466 	nlen = (algid + 1) * sizeof (*nalp);
23467 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
23468 	if (nalp == NULL)
23469 		return (B_FALSE);
23470 
23471 	if (oalp != NULL) {
23472 		bcopy(oalp, nalp, olen);
23473 		kmem_free(oalp, olen);
23474 	}
23475 	capab->encr_algparm = nalp;
23476 	capab->encr_algparm_size = nlen;
23477 	capab->encr_algparm_end = algid + 1;
23478 
23479 	return (B_TRUE);
23480 }
23481 
23482 /*
23483  * Compare the capabilities of the specified ill with the protocol
23484  * and algorithms specified by the SA passed as argument.
23485  * If they match, returns B_TRUE, B_FALSE if they do not match.
23486  *
23487  * The ill can be passed as a pointer to it, or by specifying its index
23488  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
23489  *
23490  * Called by ipsec_out_is_accelerated() do decide whether an outbound
23491  * packet is eligible for hardware acceleration, and by
23492  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
23493  * to a particular ill.
23494  */
23495 boolean_t
23496 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
23497     ipsa_t *sa, netstack_t *ns)
23498 {
23499 	boolean_t sa_isv6;
23500 	uint_t algid;
23501 	struct ill_ipsec_capab_s *cpp;
23502 	boolean_t need_refrele = B_FALSE;
23503 	ip_stack_t	*ipst = ns->netstack_ip;
23504 
23505 	if (ill == NULL) {
23506 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
23507 		    NULL, NULL, NULL, ipst);
23508 		if (ill == NULL) {
23509 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
23510 			return (B_FALSE);
23511 		}
23512 		need_refrele = B_TRUE;
23513 	}
23514 
23515 	/*
23516 	 * Use the address length specified by the SA to determine
23517 	 * if it corresponds to a IPv6 address, and fail the matching
23518 	 * if the isv6 flag passed as argument does not match.
23519 	 * Note: this check is used for SADB capability checking before
23520 	 * sending SA information to an ill.
23521 	 */
23522 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
23523 	if (sa_isv6 != ill_isv6)
23524 		/* protocol mismatch */
23525 		goto done;
23526 
23527 	/*
23528 	 * Check if the ill supports the protocol, algorithm(s) and
23529 	 * key size(s) specified by the SA, and get the pointers to
23530 	 * the algorithms supported by the ill.
23531 	 */
23532 	switch (sa->ipsa_type) {
23533 
23534 	case SADB_SATYPE_ESP:
23535 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
23536 			/* ill does not support ESP acceleration */
23537 			goto done;
23538 		cpp = ill->ill_ipsec_capab_esp;
23539 		algid = sa->ipsa_auth_alg;
23540 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
23541 			goto done;
23542 		algid = sa->ipsa_encr_alg;
23543 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
23544 			goto done;
23545 		if (algid < cpp->encr_algparm_end) {
23546 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
23547 			if (sa->ipsa_encrkeybits < alp->minkeylen)
23548 				goto done;
23549 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
23550 				goto done;
23551 		}
23552 		break;
23553 
23554 	case SADB_SATYPE_AH:
23555 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
23556 			/* ill does not support AH acceleration */
23557 			goto done;
23558 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
23559 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
23560 			goto done;
23561 		break;
23562 	}
23563 
23564 	if (need_refrele)
23565 		ill_refrele(ill);
23566 	return (B_TRUE);
23567 done:
23568 	if (need_refrele)
23569 		ill_refrele(ill);
23570 	return (B_FALSE);
23571 }
23572 
23573 
23574 /*
23575  * Add a new ill to the list of IPsec capable ills.
23576  * Called from ill_capability_ipsec_ack() when an ACK was received
23577  * indicating that IPsec hardware processing was enabled for an ill.
23578  *
23579  * ill must point to the ill for which acceleration was enabled.
23580  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
23581  */
23582 static void
23583 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
23584 {
23585 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
23586 	uint_t sa_type;
23587 	uint_t ipproto;
23588 	ip_stack_t	*ipst = ill->ill_ipst;
23589 
23590 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
23591 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
23592 
23593 	switch (dl_cap) {
23594 	case DL_CAPAB_IPSEC_AH:
23595 		sa_type = SADB_SATYPE_AH;
23596 		ills = &ipst->ips_ipsec_capab_ills_ah;
23597 		ipproto = IPPROTO_AH;
23598 		break;
23599 	case DL_CAPAB_IPSEC_ESP:
23600 		sa_type = SADB_SATYPE_ESP;
23601 		ills = &ipst->ips_ipsec_capab_ills_esp;
23602 		ipproto = IPPROTO_ESP;
23603 		break;
23604 	}
23605 
23606 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23607 
23608 	/*
23609 	 * Add ill index to list of hardware accelerators. If
23610 	 * already in list, do nothing.
23611 	 */
23612 	for (cur_ill = *ills; cur_ill != NULL &&
23613 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
23614 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
23615 		;
23616 
23617 	if (cur_ill == NULL) {
23618 		/* if this is a new entry for this ill */
23619 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
23620 		if (new_ill == NULL) {
23621 			rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23622 			return;
23623 		}
23624 
23625 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
23626 		new_ill->ill_isv6 = ill->ill_isv6;
23627 		new_ill->next = *ills;
23628 		*ills = new_ill;
23629 	} else if (!sadb_resync) {
23630 		/* not resync'ing SADB and an entry exists for this ill */
23631 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23632 		return;
23633 	}
23634 
23635 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23636 
23637 	if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
23638 		/*
23639 		 * IPsec module for protocol loaded, initiate dump
23640 		 * of the SADB to this ill.
23641 		 */
23642 		sadb_ill_download(ill, sa_type);
23643 }
23644 
23645 /*
23646  * Remove an ill from the list of IPsec capable ills.
23647  */
23648 static void
23649 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
23650 {
23651 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
23652 	ip_stack_t	*ipst = ill->ill_ipst;
23653 
23654 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
23655 	    dl_cap == DL_CAPAB_IPSEC_ESP);
23656 
23657 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah :
23658 	    &ipst->ips_ipsec_capab_ills_esp;
23659 
23660 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER);
23661 
23662 	prev_ill = NULL;
23663 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
23664 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
23665 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
23666 		;
23667 	if (cur_ill == NULL) {
23668 		/* entry not found */
23669 		rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23670 		return;
23671 	}
23672 	if (prev_ill == NULL) {
23673 		/* entry at front of list */
23674 		*ills = NULL;
23675 	} else {
23676 		prev_ill->next = cur_ill->next;
23677 	}
23678 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
23679 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23680 }
23681 
23682 /*
23683  * Called by SADB to send a DL_CONTROL_REQ message to every ill
23684  * supporting the specified IPsec protocol acceleration.
23685  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
23686  * We free the mblk and, if sa is non-null, release the held referece.
23687  */
23688 void
23689 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa,
23690     netstack_t *ns)
23691 {
23692 	ipsec_capab_ill_t *ici, *cur_ici;
23693 	ill_t *ill;
23694 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
23695 	ip_stack_t	*ipst = ns->netstack_ip;
23696 
23697 	ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah :
23698 	    ipst->ips_ipsec_capab_ills_esp;
23699 
23700 	rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER);
23701 
23702 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
23703 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
23704 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst);
23705 
23706 		/*
23707 		 * Handle the case where the ill goes away while the SADB is
23708 		 * attempting to send messages.  If it's going away, it's
23709 		 * nuking its shadow SADB, so we don't care..
23710 		 */
23711 
23712 		if (ill == NULL)
23713 			continue;
23714 
23715 		if (sa != NULL) {
23716 			/*
23717 			 * Make sure capabilities match before
23718 			 * sending SA to ill.
23719 			 */
23720 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
23721 			    cur_ici->ill_isv6, sa, ipst->ips_netstack)) {
23722 				ill_refrele(ill);
23723 				continue;
23724 			}
23725 
23726 			mutex_enter(&sa->ipsa_lock);
23727 			sa->ipsa_flags |= IPSA_F_HW;
23728 			mutex_exit(&sa->ipsa_lock);
23729 		}
23730 
23731 		/*
23732 		 * Copy template message, and add it to the front
23733 		 * of the mblk ship list. We want to avoid holding
23734 		 * the ipsec_capab_ills_lock while sending the
23735 		 * message to the ills.
23736 		 *
23737 		 * The b_next and b_prev are temporarily used
23738 		 * to build a list of mblks to be sent down, and to
23739 		 * save the ill to which they must be sent.
23740 		 */
23741 		nmp = copymsg(mp);
23742 		if (nmp == NULL) {
23743 			ill_refrele(ill);
23744 			continue;
23745 		}
23746 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
23747 		nmp->b_next = mp_ship_list;
23748 		mp_ship_list = nmp;
23749 		nmp->b_prev = (mblk_t *)ill;
23750 	}
23751 
23752 	rw_exit(&ipst->ips_ipsec_capab_ills_lock);
23753 
23754 	for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) {
23755 		/* restore the mblk to a sane state */
23756 		next_mp = nmp->b_next;
23757 		nmp->b_next = NULL;
23758 		ill = (ill_t *)nmp->b_prev;
23759 		nmp->b_prev = NULL;
23760 
23761 		ill_dlpi_send(ill, nmp);
23762 		ill_refrele(ill);
23763 	}
23764 
23765 	if (sa != NULL)
23766 		IPSA_REFRELE(sa);
23767 	freemsg(mp);
23768 }
23769 
23770 /*
23771  * Derive an interface id from the link layer address.
23772  * Knows about IEEE 802 and IEEE EUI-64 mappings.
23773  */
23774 static boolean_t
23775 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23776 {
23777 	char		*addr;
23778 
23779 	if (phys_length != ETHERADDRL)
23780 		return (B_FALSE);
23781 
23782 	/* Form EUI-64 like address */
23783 	addr = (char *)&v6addr->s6_addr32[2];
23784 	bcopy((char *)phys_addr, addr, 3);
23785 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
23786 	addr[3] = (char)0xff;
23787 	addr[4] = (char)0xfe;
23788 	bcopy((char *)phys_addr + 3, addr + 5, 3);
23789 	return (B_TRUE);
23790 }
23791 
23792 /* ARGSUSED */
23793 static boolean_t
23794 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23795 {
23796 	return (B_FALSE);
23797 }
23798 
23799 /* ARGSUSED */
23800 static boolean_t
23801 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23802     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23803 {
23804 	/*
23805 	 * Multicast address mappings used over Ethernet/802.X.
23806 	 * This address is used as a base for mappings.
23807 	 */
23808 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
23809 	    0x00, 0x00, 0x00};
23810 
23811 	/*
23812 	 * Extract low order 32 bits from IPv6 multicast address.
23813 	 * Or that into the link layer address, starting from the
23814 	 * second byte.
23815 	 */
23816 	*hw_start = 2;
23817 	v6_extract_mask->s6_addr32[0] = 0;
23818 	v6_extract_mask->s6_addr32[1] = 0;
23819 	v6_extract_mask->s6_addr32[2] = 0;
23820 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23821 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
23822 	return (B_TRUE);
23823 }
23824 
23825 /*
23826  * Indicate by return value whether multicast is supported. If not,
23827  * this code should not touch/change any parameters.
23828  */
23829 /* ARGSUSED */
23830 static boolean_t
23831 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23832     uint32_t *hw_start, ipaddr_t *extract_mask)
23833 {
23834 	/*
23835 	 * Multicast address mappings used over Ethernet/802.X.
23836 	 * This address is used as a base for mappings.
23837 	 */
23838 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
23839 	    0x00, 0x00, 0x00 };
23840 
23841 	if (phys_length != ETHERADDRL)
23842 		return (B_FALSE);
23843 
23844 	*extract_mask = htonl(0x007fffff);
23845 	*hw_start = 2;
23846 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
23847 	return (B_TRUE);
23848 }
23849 
23850 /*
23851  * Derive IPoIB interface id from the link layer address.
23852  */
23853 static boolean_t
23854 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
23855 {
23856 	char		*addr;
23857 
23858 	if (phys_length != 20)
23859 		return (B_FALSE);
23860 	addr = (char *)&v6addr->s6_addr32[2];
23861 	bcopy(phys_addr + 12, addr, 8);
23862 	/*
23863 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
23864 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
23865 	 * rules. In these cases, the IBA considers these GUIDs to be in
23866 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
23867 	 * required; vendors are required not to assign global EUI-64's
23868 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
23869 	 * of the interface identifier. Whether the GUID is in modified
23870 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
23871 	 * bit set to 1.
23872 	 */
23873 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
23874 	return (B_TRUE);
23875 }
23876 
23877 /*
23878  * Note on mapping from multicast IP addresses to IPoIB multicast link
23879  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
23880  * The format of an IPoIB multicast address is:
23881  *
23882  *  4 byte QPN      Scope Sign.  Pkey
23883  * +--------------------------------------------+
23884  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
23885  * +--------------------------------------------+
23886  *
23887  * The Scope and Pkey components are properties of the IBA port and
23888  * network interface. They can be ascertained from the broadcast address.
23889  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
23890  */
23891 
23892 static boolean_t
23893 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
23894     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
23895 {
23896 	/*
23897 	 * Base IPoIB IPv6 multicast address used for mappings.
23898 	 * Does not contain the IBA scope/Pkey values.
23899 	 */
23900 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23901 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
23902 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23903 
23904 	/*
23905 	 * Extract low order 80 bits from IPv6 multicast address.
23906 	 * Or that into the link layer address, starting from the
23907 	 * sixth byte.
23908 	 */
23909 	*hw_start = 6;
23910 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
23911 
23912 	/*
23913 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23914 	 */
23915 	*(maddr + 5) = *(bphys_addr + 5);
23916 	*(maddr + 8) = *(bphys_addr + 8);
23917 	*(maddr + 9) = *(bphys_addr + 9);
23918 
23919 	v6_extract_mask->s6_addr32[0] = 0;
23920 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
23921 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
23922 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
23923 	return (B_TRUE);
23924 }
23925 
23926 static boolean_t
23927 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23928     uint32_t *hw_start, ipaddr_t *extract_mask)
23929 {
23930 	/*
23931 	 * Base IPoIB IPv4 multicast address used for mappings.
23932 	 * Does not contain the IBA scope/Pkey values.
23933 	 */
23934 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23935 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23936 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23937 
23938 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23939 		return (B_FALSE);
23940 
23941 	/*
23942 	 * Extract low order 28 bits from IPv4 multicast address.
23943 	 * Or that into the link layer address, starting from the
23944 	 * sixteenth byte.
23945 	 */
23946 	*extract_mask = htonl(0x0fffffff);
23947 	*hw_start = 16;
23948 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23949 
23950 	/*
23951 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23952 	 */
23953 	*(maddr + 5) = *(bphys_addr + 5);
23954 	*(maddr + 8) = *(bphys_addr + 8);
23955 	*(maddr + 9) = *(bphys_addr + 9);
23956 	return (B_TRUE);
23957 }
23958 
23959 /*
23960  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23961  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23962  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23963  * the link-local address is preferred.
23964  */
23965 boolean_t
23966 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23967 {
23968 	ipif_t	*ipif;
23969 	ipif_t	*maybe_ipif = NULL;
23970 
23971 	mutex_enter(&ill->ill_lock);
23972 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23973 		mutex_exit(&ill->ill_lock);
23974 		if (ipifp != NULL)
23975 			*ipifp = NULL;
23976 		return (B_FALSE);
23977 	}
23978 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23979 		if (!IPIF_CAN_LOOKUP(ipif))
23980 			continue;
23981 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23982 		    ipif->ipif_zoneid != ALL_ZONES)
23983 			continue;
23984 		if ((ipif->ipif_flags & flags) != flags)
23985 			continue;
23986 
23987 		if (ipifp == NULL) {
23988 			mutex_exit(&ill->ill_lock);
23989 			ASSERT(maybe_ipif == NULL);
23990 			return (B_TRUE);
23991 		}
23992 		if (!ill->ill_isv6 ||
23993 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23994 			ipif_refhold_locked(ipif);
23995 			mutex_exit(&ill->ill_lock);
23996 			*ipifp = ipif;
23997 			return (B_TRUE);
23998 		}
23999 		if (maybe_ipif == NULL)
24000 			maybe_ipif = ipif;
24001 	}
24002 	if (ipifp != NULL) {
24003 		if (maybe_ipif != NULL)
24004 			ipif_refhold_locked(maybe_ipif);
24005 		*ipifp = maybe_ipif;
24006 	}
24007 	mutex_exit(&ill->ill_lock);
24008 	return (maybe_ipif != NULL);
24009 }
24010 
24011 /*
24012  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
24013  */
24014 boolean_t
24015 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
24016 {
24017 	ill_t *illg;
24018 	ip_stack_t	*ipst = ill->ill_ipst;
24019 
24020 	/*
24021 	 * We look at the passed-in ill first without grabbing ill_g_lock.
24022 	 */
24023 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
24024 		return (B_TRUE);
24025 	}
24026 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
24027 	if (ill->ill_group == NULL) {
24028 		/* ill not in a group */
24029 		rw_exit(&ipst->ips_ill_g_lock);
24030 		return (B_FALSE);
24031 	}
24032 
24033 	/*
24034 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
24035 	 * group. We need to look for an ipif in the zone on all the ills in the
24036 	 * group.
24037 	 */
24038 	illg = ill->ill_group->illgrp_ill;
24039 	do {
24040 		/*
24041 		 * We don't call ipif_lookup_zoneid() on ill as we already know
24042 		 * that it's not there.
24043 		 */
24044 		if (illg != ill &&
24045 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
24046 			break;
24047 		}
24048 	} while ((illg = illg->ill_group_next) != NULL);
24049 	rw_exit(&ipst->ips_ill_g_lock);
24050 	return (illg != NULL);
24051 }
24052 
24053 /*
24054  * Check if this ill is only being used to send ICMP probes for IPMP
24055  */
24056 boolean_t
24057 ill_is_probeonly(ill_t *ill)
24058 {
24059 	/*
24060 	 * Check if the interface is FAILED, or INACTIVE
24061 	 */
24062 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
24063 		return (B_TRUE);
24064 
24065 	return (B_FALSE);
24066 }
24067 
24068 /*
24069  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
24070  * If a pointer to an ipif_t is returned then the caller will need to do
24071  * an ill_refrele().
24072  *
24073  * If there is no real interface which matches the ifindex, then it looks
24074  * for a group that has a matching index. In the case of a group match the
24075  * lifidx must be zero. We don't need emulate the logical interfaces
24076  * since IP Filter's use of netinfo doesn't use that.
24077  */
24078 ipif_t *
24079 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
24080     ip_stack_t *ipst)
24081 {
24082 	ipif_t *ipif;
24083 	ill_t *ill;
24084 
24085 	ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
24086 	    ipst);
24087 
24088 	if (ill == NULL) {
24089 		/* Fallback to group names only if hook_emulation set */
24090 		if (!ipst->ips_ipmp_hook_emulation)
24091 			return (NULL);
24092 
24093 		if (lifidx != 0)
24094 			return (NULL);
24095 		ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst);
24096 		if (ill == NULL)
24097 			return (NULL);
24098 	}
24099 
24100 	mutex_enter(&ill->ill_lock);
24101 	if (ill->ill_state_flags & ILL_CONDEMNED) {
24102 		mutex_exit(&ill->ill_lock);
24103 		ill_refrele(ill);
24104 		return (NULL);
24105 	}
24106 
24107 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
24108 		if (!IPIF_CAN_LOOKUP(ipif))
24109 			continue;
24110 		if (lifidx == ipif->ipif_id) {
24111 			ipif_refhold_locked(ipif);
24112 			break;
24113 		}
24114 	}
24115 
24116 	mutex_exit(&ill->ill_lock);
24117 	ill_refrele(ill);
24118 	return (ipif);
24119 }
24120 
24121 /*
24122  * Flush the fastpath by deleting any nce's that are waiting for the fastpath,
24123  * There is one exceptions IRE_BROADCAST are difficult to recreate,
24124  * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush()
24125  * for details.
24126  */
24127 void
24128 ill_fastpath_flush(ill_t *ill)
24129 {
24130 	ip_stack_t *ipst = ill->ill_ipst;
24131 
24132 	nce_fastpath_list_dispatch(ill, NULL, NULL);
24133 	ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4),
24134 	    ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE);
24135 }
24136 
24137 /*
24138  * Set the physical address information for `ill' to the contents of the
24139  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
24140  * asynchronous if `ill' cannot immediately be quiesced -- in which case
24141  * EINPROGRESS will be returned.
24142  */
24143 int
24144 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
24145 {
24146 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
24147 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
24148 
24149 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24150 
24151 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
24152 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
24153 		/* Changing DL_IPV6_TOKEN is not yet supported */
24154 		return (0);
24155 	}
24156 
24157 	/*
24158 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
24159 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
24160 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
24161 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
24162 	 */
24163 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
24164 		freemsg(mp);
24165 		return (ENOMEM);
24166 	}
24167 
24168 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
24169 
24170 	/*
24171 	 * If we can quiesce the ill, then set the address.  If not, then
24172 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
24173 	 */
24174 	ill_down_ipifs(ill, NULL, 0, B_FALSE);
24175 	mutex_enter(&ill->ill_lock);
24176 	if (!ill_is_quiescent(ill)) {
24177 		/* call cannot fail since `conn_t *' argument is NULL */
24178 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
24179 		    mp, ILL_DOWN);
24180 		mutex_exit(&ill->ill_lock);
24181 		return (EINPROGRESS);
24182 	}
24183 	mutex_exit(&ill->ill_lock);
24184 
24185 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
24186 	return (0);
24187 }
24188 
24189 /*
24190  * Once the ill associated with `q' has quiesced, set its physical address
24191  * information to the values in `addrmp'.  Note that two copies of `addrmp'
24192  * are passed (linked by b_cont), since we sometimes need to save two distinct
24193  * copies in the ill_t, and our context doesn't permit sleeping or allocation
24194  * failure (we'll free the other copy if it's not needed).  Since the ill_t
24195  * is quiesced, we know any stale IREs with the old address information have
24196  * already been removed, so we don't need to call ill_fastpath_flush().
24197  */
24198 /* ARGSUSED */
24199 static void
24200 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
24201 {
24202 	ill_t		*ill = q->q_ptr;
24203 	mblk_t		*addrmp2 = unlinkb(addrmp);
24204 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
24205 	uint_t		addrlen, addroff;
24206 
24207 	ASSERT(IAM_WRITER_IPSQ(ipsq));
24208 
24209 	addroff	= dlindp->dl_addr_offset;
24210 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
24211 
24212 	switch (dlindp->dl_data) {
24213 	case DL_IPV6_LINK_LAYER_ADDR:
24214 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
24215 		freemsg(addrmp2);
24216 		break;
24217 
24218 	case DL_CURR_PHYS_ADDR:
24219 		freemsg(ill->ill_phys_addr_mp);
24220 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
24221 		ill->ill_phys_addr_mp = addrmp;
24222 		ill->ill_phys_addr_length = addrlen;
24223 
24224 		if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))
24225 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
24226 		else
24227 			freemsg(addrmp2);
24228 		break;
24229 	default:
24230 		ASSERT(0);
24231 	}
24232 
24233 	/*
24234 	 * If there are ipifs to bring up, ill_up_ipifs() will return
24235 	 * EINPROGRESS, and ipsq_current_finish() will be called by
24236 	 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is
24237 	 * brought up.
24238 	 */
24239 	if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS)
24240 		ipsq_current_finish(ipsq);
24241 }
24242 
24243 /*
24244  * Helper routine for setting the ill_nd_lla fields.
24245  */
24246 void
24247 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
24248 {
24249 	freemsg(ill->ill_nd_lla_mp);
24250 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
24251 	ill->ill_nd_lla_mp = ndmp;
24252 	ill->ill_nd_lla_len = addrlen;
24253 }
24254 
24255 major_t IP_MAJ;
24256 #define	IP	"ip"
24257 
24258 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
24259 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
24260 
24261 /*
24262  * Issue REMOVEIF ioctls to have the loopback interfaces
24263  * go away.  Other interfaces are either I_LINKed or I_PLINKed;
24264  * the former going away when the user-level processes in the zone
24265  * are killed  * and the latter are cleaned up by the stream head
24266  * str_stack_shutdown callback that undoes all I_PLINKs.
24267  */
24268 void
24269 ip_loopback_cleanup(ip_stack_t *ipst)
24270 {
24271 	int error;
24272 	ldi_handle_t	lh = NULL;
24273 	ldi_ident_t	li = NULL;
24274 	int		rval;
24275 	cred_t		*cr;
24276 	struct strioctl iocb;
24277 	struct lifreq	lifreq;
24278 
24279 	IP_MAJ = ddi_name_to_major(IP);
24280 
24281 #ifdef NS_DEBUG
24282 	(void) printf("ip_loopback_cleanup() stackid %d\n",
24283 	    ipst->ips_netstack->netstack_stackid);
24284 #endif
24285 
24286 	bzero(&lifreq, sizeof (lifreq));
24287 	(void) strcpy(lifreq.lifr_name, ipif_loopback_name);
24288 
24289 	error = ldi_ident_from_major(IP_MAJ, &li);
24290 	if (error) {
24291 #ifdef DEBUG
24292 		printf("ip_loopback_cleanup: lyr ident get failed error %d\n",
24293 		    error);
24294 #endif
24295 		return;
24296 	}
24297 
24298 	cr = zone_get_kcred(netstackid_to_zoneid(
24299 	    ipst->ips_netstack->netstack_stackid));
24300 	ASSERT(cr != NULL);
24301 	error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li);
24302 	if (error) {
24303 #ifdef DEBUG
24304 		printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n",
24305 		    error);
24306 #endif
24307 		goto out;
24308 	}
24309 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24310 	iocb.ic_timout = 15;
24311 	iocb.ic_len = sizeof (lifreq);
24312 	iocb.ic_dp = (char *)&lifreq;
24313 
24314 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24315 	/* LINTED - statement has no consequent */
24316 	if (error) {
24317 #ifdef NS_DEBUG
24318 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24319 		    "UDP6 error %d\n", error);
24320 #endif
24321 	}
24322 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24323 	lh = NULL;
24324 
24325 	error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li);
24326 	if (error) {
24327 #ifdef NS_DEBUG
24328 		printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n",
24329 		    error);
24330 #endif
24331 		goto out;
24332 	}
24333 
24334 	iocb.ic_cmd = SIOCLIFREMOVEIF;
24335 	iocb.ic_timout = 15;
24336 	iocb.ic_len = sizeof (lifreq);
24337 	iocb.ic_dp = (char *)&lifreq;
24338 
24339 	error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval);
24340 	/* LINTED - statement has no consequent */
24341 	if (error) {
24342 #ifdef NS_DEBUG
24343 		printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on "
24344 		    "UDP error %d\n", error);
24345 #endif
24346 	}
24347 	(void) ldi_close(lh, FREAD|FWRITE, cr);
24348 	lh = NULL;
24349 
24350 out:
24351 	/* Close layered handles */
24352 	if (lh)
24353 		(void) ldi_close(lh, FREAD|FWRITE, cr);
24354 	if (li)
24355 		ldi_ident_release(li);
24356 
24357 	crfree(cr);
24358 }
24359 
24360 /*
24361  * This needs to be in-sync with nic_event_t definition
24362  */
24363 static const char *
24364 ill_hook_event2str(nic_event_t event)
24365 {
24366 	switch (event) {
24367 	case NE_PLUMB:
24368 		return ("PLUMB");
24369 	case NE_UNPLUMB:
24370 		return ("UNPLUMB");
24371 	case NE_UP:
24372 		return ("UP");
24373 	case NE_DOWN:
24374 		return ("DOWN");
24375 	case NE_ADDRESS_CHANGE:
24376 		return ("ADDRESS_CHANGE");
24377 	default:
24378 		return ("UNKNOWN");
24379 	}
24380 }
24381 
24382 static void
24383 ill_hook_event_destroy(ill_t *ill)
24384 {
24385 	hook_nic_event_t	*info;
24386 
24387 	if ((info = ill->ill_nic_event_info) != NULL) {
24388 		if (info->hne_data != NULL)
24389 			kmem_free(info->hne_data, info->hne_datalen);
24390 		kmem_free(info, sizeof (hook_nic_event_t));
24391 
24392 		ill->ill_nic_event_info = NULL;
24393 	}
24394 
24395 }
24396 
24397 boolean_t
24398 ill_hook_event_create(ill_t *ill, lif_if_t lif, nic_event_t event,
24399     nic_event_data_t data, size_t datalen)
24400 {
24401 	ip_stack_t		*ipst = ill->ill_ipst;
24402 	hook_nic_event_t	*info;
24403 	const char		*str = NULL;
24404 
24405 	/* destroy nic event info if it exists */
24406 	if ((info = ill->ill_nic_event_info) != NULL) {
24407 		str = ill_hook_event2str(info->hne_event);
24408 		ip2dbg(("ill_hook_event_create: unexpected nic event %s "
24409 		    "attached for %s\n", str, ill->ill_name));
24410 		ill_hook_event_destroy(ill);
24411 	}
24412 
24413 	/* create a new nic event info */
24414 	if ((info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP)) == NULL)
24415 		goto fail;
24416 
24417 	ill->ill_nic_event_info = info;
24418 
24419 	if (event == NE_UNPLUMB)
24420 		info->hne_nic = ill->ill_phyint->phyint_ifindex;
24421 	else
24422 		info->hne_nic = ill->ill_phyint->phyint_hook_ifindex;
24423 	info->hne_lif = lif;
24424 	info->hne_event = event;
24425 	info->hne_family = ill->ill_isv6 ?
24426 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
24427 	info->hne_data = NULL;
24428 	info->hne_datalen = 0;
24429 
24430 	if (data != NULL && datalen != 0) {
24431 		info->hne_data = kmem_alloc(datalen, KM_NOSLEEP);
24432 		if (info->hne_data != NULL) {
24433 			bcopy(data, info->hne_data, datalen);
24434 			info->hne_datalen = datalen;
24435 		} else {
24436 			ill_hook_event_destroy(ill);
24437 			goto fail;
24438 		}
24439 	}
24440 
24441 	return (B_TRUE);
24442 fail:
24443 	str = ill_hook_event2str(event);
24444 	ip2dbg(("ill_hook_event_create: could not attach %s nic event "
24445 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
24446 	return (B_FALSE);
24447 }
24448