xref: /titanic_52/usr/src/uts/common/inet/ip/ip_if.c (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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 2006 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 
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/isa_defs.h>
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <sys/sockio.h>
58 #include <netinet/in.h>
59 #include <netinet/ip6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet/igmp_var.h>
62 #include <sys/strsun.h>
63 #include <sys/policy.h>
64 #include <sys/ethernet.h>
65 
66 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
67 #include <inet/mi.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/mib2.h>
71 #include <inet/ip.h>
72 #include <inet/ip6.h>
73 #include <inet/ip6_asp.h>
74 #include <inet/tcp.h>
75 #include <inet/ip_multi.h>
76 #include <inet/ip_ire.h>
77 #include <inet/ip_rts.h>
78 #include <inet/ip_ndp.h>
79 #include <inet/ip_if.h>
80 #include <inet/ip_impl.h>
81 #include <inet/tun.h>
82 #include <inet/sctp_ip.h>
83 
84 #include <net/pfkeyv2.h>
85 #include <inet/ipsec_info.h>
86 #include <inet/sadb.h>
87 #include <inet/ipsec_impl.h>
88 #include <sys/iphada.h>
89 
90 
91 #include <netinet/igmp.h>
92 #include <inet/ip_listutils.h>
93 #include <inet/ipclassifier.h>
94 #include <sys/mac.h>
95 
96 #include <sys/systeminfo.h>
97 #include <sys/bootconf.h>
98 
99 #include <sys/tsol/tndb.h>
100 #include <sys/tsol/tnet.h>
101 
102 /* The character which tells where the ill_name ends */
103 #define	IPIF_SEPARATOR_CHAR	':'
104 
105 /* IP ioctl function table entry */
106 typedef struct ipft_s {
107 	int	ipft_cmd;
108 	pfi_t	ipft_pfi;
109 	int	ipft_min_size;
110 	int	ipft_flags;
111 } ipft_t;
112 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
113 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
114 
115 typedef struct ip_sock_ar_s {
116 	union {
117 		area_t	ip_sock_area;
118 		ared_t	ip_sock_ared;
119 		areq_t	ip_sock_areq;
120 	} ip_sock_ar_u;
121 	queue_t	*ip_sock_ar_q;
122 } ip_sock_ar_t;
123 
124 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
125 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
126 		    char *value, caddr_t cp, cred_t *ioc_cr);
127 
128 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
129 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
130 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
131     mblk_t *mp, boolean_t need_up);
132 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp, boolean_t need_up);
134 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
135     queue_t *q, mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
137     mblk_t *mp, boolean_t need_up);
138 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139     mblk_t *mp);
140 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
141     queue_t *q, mblk_t *mp, boolean_t need_up);
142 static int	ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp,
143     sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl);
144 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **);
145 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
146 static void	ipsq_flush(ill_t *ill);
147 static void	ipsq_clean_all(ill_t *ill);
148 static void	ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring);
149 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
150     queue_t *q, mblk_t *mp, boolean_t need_up);
151 static void	ipsq_delete(ipsq_t *);
152 
153 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
154 		    boolean_t initialize);
155 static void	ipif_check_bcast_ires(ipif_t *test_ipif);
156 static void	ipif_down_delete_ire(ire_t *ire, char *ipif);
157 static void	ipif_delete_cache_ire(ire_t *, char *);
158 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
159 static void	ipif_down_tail(ipif_t *ipif);
160 static void	ipif_free(ipif_t *ipif);
161 static void	ipif_free_tail(ipif_t *ipif);
162 static void	ipif_mask_reply(ipif_t *);
163 static void	ipif_mtu_change(ire_t *ire, char *ipif_arg);
164 static void	ipif_multicast_down(ipif_t *ipif);
165 static void	ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif);
166 static void	ipif_set_default(ipif_t *ipif);
167 static int	ipif_set_values(queue_t *q, mblk_t *mp,
168     char *interf_name, uint_t *ppa);
169 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
170     queue_t *q);
171 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
172     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
173     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error);
174 static int	ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp);
175 static void	ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp);
176 
177 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
178 static int	ill_arp_off(ill_t *ill);
179 static int	ill_arp_on(ill_t *ill);
180 static void	ill_delete_interface_type(ill_if_t *);
181 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
182 static void	ill_down(ill_t *ill);
183 static void	ill_downi(ire_t *ire, char *ill_arg);
184 static void	ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg);
185 static void	ill_down_tail(ill_t *ill);
186 static void	ill_free_mib(ill_t *ill);
187 static void	ill_glist_delete(ill_t *);
188 static boolean_t ill_has_usable_ipif(ill_t *);
189 static int	ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int);
190 static void	ill_nominate_bcast_rcv(ill_group_t *illgrp);
191 static void	ill_phyint_free(ill_t *ill);
192 static void	ill_phyint_reinit(ill_t *ill);
193 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
194 static void	ill_signal_ipsq_ills(ipsq_t *, boolean_t);
195 static boolean_t ill_split_ipsq(ipsq_t *cur_sq);
196 static void	ill_stq_cache_delete(ire_t *, char *);
197 
198 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *);
199 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *);
200 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
201     in6_addr_t *);
202 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
203     ipaddr_t *);
204 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *);
205 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
206     in6_addr_t *);
207 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *,
208     ipaddr_t *);
209 
210 static void	ipif_save_ire(ipif_t *, ire_t *);
211 static void	ipif_remove_ire(ipif_t *, ire_t *);
212 static void 	ip_cgtp_bcast_add(ire_t *, ire_t *);
213 static void 	ip_cgtp_bcast_delete(ire_t *);
214 
215 /*
216  * Per-ill IPsec capabilities management.
217  */
218 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void);
219 static void	ill_ipsec_capab_free(ill_ipsec_capab_t *);
220 static void	ill_ipsec_capab_add(ill_t *, uint_t, boolean_t);
221 static void	ill_ipsec_capab_delete(ill_t *, uint_t);
222 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int);
223 static void ill_capability_proto(ill_t *, int, mblk_t *);
224 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *,
225     boolean_t);
226 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
227 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
228 static void ill_capability_mdt_reset(ill_t *, mblk_t **);
229 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
230 static void ill_capability_ipsec_reset(ill_t *, mblk_t **);
231 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
232 static void ill_capability_hcksum_reset(ill_t *, mblk_t **);
233 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
234     dl_capability_sub_t *);
235 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **);
236 
237 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
238 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *);
239 static void ill_capability_dls_reset(ill_t *, mblk_t **);
240 static void ill_capability_dls_disable(ill_t *);
241 
242 static void	illgrp_cache_delete(ire_t *, char *);
243 static void	illgrp_delete(ill_t *ill);
244 static void	illgrp_reset_schednext(ill_t *ill);
245 
246 static ill_t	*ill_prev_usesrc(ill_t *);
247 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
248 static void	ill_disband_usesrc_group(ill_t *);
249 
250 static void	conn_cleanup_stale_ire(conn_t *, caddr_t);
251 
252 /*
253  * if we go over the memory footprint limit more than once in this msec
254  * interval, we'll start pruning aggressively.
255  */
256 int ip_min_frag_prune_time = 0;
257 
258 /*
259  * max # of IPsec algorithms supported.  Limited to 1 byte by PF_KEY
260  * and the IPsec DOI
261  */
262 #define	MAX_IPSEC_ALGS	256
263 
264 #define	BITSPERBYTE	8
265 #define	BITS(type)	(BITSPERBYTE * (long)sizeof (type))
266 
267 #define	IPSEC_ALG_ENABLE(algs, algid) \
268 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \
269 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
270 
271 #define	IPSEC_ALG_IS_ENABLED(algid, algs) \
272 		((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \
273 		(1 << ((algid) % BITS(ipsec_capab_elem_t))))
274 
275 typedef uint8_t ipsec_capab_elem_t;
276 
277 /*
278  * Per-algorithm parameters.  Note that at present, only encryption
279  * algorithms have variable keysize (IKE does not provide a way to negotiate
280  * auth algorithm keysize).
281  *
282  * All sizes here are in bits.
283  */
284 typedef struct
285 {
286 	uint16_t	minkeylen;
287 	uint16_t	maxkeylen;
288 } ipsec_capab_algparm_t;
289 
290 /*
291  * Per-ill capabilities.
292  */
293 struct ill_ipsec_capab_s {
294 	ipsec_capab_elem_t *encr_hw_algs;
295 	ipsec_capab_elem_t *auth_hw_algs;
296 	uint32_t algs_size;	/* size of _hw_algs in bytes */
297 	/* algorithm key lengths */
298 	ipsec_capab_algparm_t *encr_algparm;
299 	uint32_t encr_algparm_size;
300 	uint32_t encr_algparm_end;
301 };
302 
303 /*
304  * List of AH and ESP IPsec acceleration capable ills
305  */
306 typedef struct ipsec_capab_ill_s {
307 	uint_t ill_index;
308 	boolean_t ill_isv6;
309 	struct ipsec_capab_ill_s *next;
310 } ipsec_capab_ill_t;
311 
312 static ipsec_capab_ill_t *ipsec_capab_ills_ah;
313 static ipsec_capab_ill_t *ipsec_capab_ills_esp;
314 krwlock_t ipsec_capab_ills_lock;
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 	6,				/* xmit_count */
394 	1000,				/* (re)xmit_interval in milliseconds */
395 	4				/* 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_MIPRUNNING,	"MIP" },
487 	{ IPIF_NOFAILOVER,	"NOFAILOVER" },
488 	{ PHYI_FAILED,		"FAILED" },
489 	{ PHYI_STANDBY,		"STANDBY" },
490 	{ PHYI_INACTIVE,	"INACTIVE" },
491 	{ PHYI_OFFLINE,		"OFFLINE" },
492 };
493 
494 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
495 
496 static ip_m_t	ip_m_tbl[] = {
497 	{ DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
498 	    ip_ether_v6intfid },
499 	{ DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
500 	    ip_nodef_v6intfid },
501 	{ DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
502 	    ip_nodef_v6intfid },
503 	{ DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
504 	    ip_nodef_v6intfid },
505 	{ DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
506 	    ip_ether_v6intfid },
507 	{ DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo,
508 	    ip_ib_v6intfid },
509 	{ SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL},
510 	{ DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo,
511 	    ip_nodef_v6intfid }
512 };
513 
514 static ill_t	ill_null;		/* Empty ILL for init. */
515 char	ipif_loopback_name[] = "lo0";
516 static char *ipv4_forward_suffix = ":ip_forwarding";
517 static char *ipv6_forward_suffix = ":ip6_forwarding";
518 static kstat_t *loopback_ksp = NULL;
519 static	sin6_t	sin6_null;	/* Zero address for quick clears */
520 static	sin_t	sin_null;	/* Zero address for quick clears */
521 static	uint_t	ill_index = 1;	/* Used to assign interface indicies */
522 /* When set search for unused index */
523 static boolean_t ill_index_wrap = B_FALSE;
524 /* When set search for unused ipif_seqid */
525 static ipif_t	ipif_zero;
526 uint_t	ipif_src_random;
527 
528 /*
529  * For details on the protection offered by these locks please refer
530  * to the notes under the Synchronization section at the start of ip.c
531  */
532 krwlock_t	ill_g_lock;		/* The global ill_g_lock */
533 kmutex_t	ip_addr_avail_lock;	/* Address availability check lock */
534 ipsq_t		*ipsq_g_head;		/* List of all ipsq's on the system */
535 
536 krwlock_t	ill_g_usesrc_lock;	/* Protects usesrc related fields */
537 
538 /*
539  * illgrp_head/ifgrp_head is protected by IP's perimeter.
540  */
541 static  ill_group_t *illgrp_head_v4;	/* Head of IPv4 ill groups */
542 ill_group_t *illgrp_head_v6;		/* Head of IPv6 ill groups */
543 
544 ill_g_head_t	ill_g_heads[MAX_G_HEADS];   /* ILL List Head */
545 
546 /*
547  * ppa arena is created after these many
548  * interfaces have been plumbed.
549  */
550 uint_t	ill_no_arena = 12;
551 
552 #pragma align CACHE_ALIGN_SIZE(phyint_g_list)
553 static phyint_list_t phyint_g_list;	/* start of phyint list */
554 
555 /*
556  * Reflects value of FAILBACK variable in IPMP config file
557  * /etc/default/mpathd. Default value is B_TRUE.
558  * Set to B_FALSE if user disabled failback by configuring "FAILBACK=no"
559  * in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this information to kernel.
560  */
561 static boolean_t ipmp_enable_failback = B_TRUE;
562 
563 /*
564  * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout
565  * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is
566  * set through platform specific code (Niagara/Ontario).
567  */
568 #define	SOFT_RINGS_ENABLED()	(ip_soft_rings_cnt ? \
569 		(ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE)
570 
571 #define	ILL_CAPAB_DLS	(ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL)
572 
573 static uint_t
574 ipif_rand(void)
575 {
576 	ipif_src_random = ipif_src_random * 1103515245 + 12345;
577 	return ((ipif_src_random >> 16) & 0x7fff);
578 }
579 
580 /*
581  * Allocate per-interface mibs. Only used for ipv6.
582  * Returns true if ok. False otherwise.
583  *  ipsq  may not yet be allocated (loopback case ).
584  */
585 static boolean_t
586 ill_allocate_mibs(ill_t *ill)
587 {
588 	ASSERT(ill->ill_isv6);
589 
590 	/* Already allocated? */
591 	if (ill->ill_ip6_mib != NULL) {
592 		ASSERT(ill->ill_icmp6_mib != NULL);
593 		return (B_TRUE);
594 	}
595 
596 	ill->ill_ip6_mib = kmem_zalloc(sizeof (*ill->ill_ip6_mib),
597 	    KM_NOSLEEP);
598 	if (ill->ill_ip6_mib == NULL) {
599 		return (B_FALSE);
600 	}
601 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
602 	    KM_NOSLEEP);
603 	if (ill->ill_icmp6_mib == NULL) {
604 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
605 		ill->ill_ip6_mib = NULL;
606 		return (B_FALSE);
607 	}
608 	/*
609 	 * The ipv6Ifindex and ipv6IfIcmpIndex will be assigned later
610 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
611 	 * -> ill_phyint_reinit
612 	 */
613 	return (B_TRUE);
614 }
615 
616 /*
617  * Common code for preparation of ARP commands.  Two points to remember:
618  * 	1) The ill_name is tacked on at the end of the allocated space so
619  *	   the templates name_offset field must contain the total space
620  *	   to allocate less the name length.
621  *
622  *	2) The templates name_length field should contain the *template*
623  *	   length.  We use it as a parameter to bcopy() and then write
624  *	   the real ill_name_length into the name_length field of the copy.
625  * (Always called as writer.)
626  */
627 mblk_t *
628 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr)
629 {
630 	arc_t	*arc = (arc_t *)template;
631 	char	*cp;
632 	int	len;
633 	mblk_t	*mp;
634 	uint_t	name_length = ill->ill_name_length;
635 	uint_t	template_len = arc->arc_name_length;
636 
637 	len = arc->arc_name_offset + name_length;
638 	mp = allocb(len, BPRI_HI);
639 	if (mp == NULL)
640 		return (NULL);
641 	cp = (char *)mp->b_rptr;
642 	mp->b_wptr = (uchar_t *)&cp[len];
643 	if (template_len)
644 		bcopy(template, cp, template_len);
645 	if (len > template_len)
646 		bzero(&cp[template_len], len - template_len);
647 	mp->b_datap->db_type = M_PROTO;
648 
649 	arc = (arc_t *)cp;
650 	arc->arc_name_length = name_length;
651 	cp = (char *)arc + arc->arc_name_offset;
652 	bcopy(ill->ill_name, cp, name_length);
653 
654 	if (addr) {
655 		area_t	*area = (area_t *)mp->b_rptr;
656 
657 		cp = (char *)area + area->area_proto_addr_offset;
658 		bcopy(addr, cp, area->area_proto_addr_length);
659 		if (area->area_cmd == AR_ENTRY_ADD) {
660 			cp = (char *)area;
661 			len = area->area_proto_addr_length;
662 			if (area->area_proto_mask_offset)
663 				cp += area->area_proto_mask_offset;
664 			else
665 				cp += area->area_proto_addr_offset + len;
666 			while (len-- > 0)
667 				*cp++ = (char)~0;
668 		}
669 	}
670 	return (mp);
671 }
672 
673 /*
674  * Completely vaporize a lower level tap and all associated interfaces.
675  * ill_delete is called only out of ip_close when the device control
676  * stream is being closed.
677  */
678 void
679 ill_delete(ill_t *ill)
680 {
681 	ipif_t	*ipif;
682 	ill_t	*prev_ill;
683 
684 	/*
685 	 * ill_delete may be forcibly entering the ipsq. The previous
686 	 * ioctl may not have completed and may need to be aborted.
687 	 * ipsq_flush takes care of it. If we don't need to enter the
688 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
689 	 * ill_delete_tail is sufficient.
690 	 */
691 	ipsq_flush(ill);
692 
693 	/*
694 	 * Nuke all interfaces.  ipif_free will take down the interface,
695 	 * remove it from the list, and free the data structure.
696 	 * Walk down the ipif list and remove the logical interfaces
697 	 * first before removing the main ipif. We can't unplumb
698 	 * zeroth interface first in the case of IPv6 as reset_conn_ill
699 	 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking
700 	 * POINTOPOINT.
701 	 *
702 	 * If ill_ipif was not properly initialized (i.e low on memory),
703 	 * then no interfaces to clean up. In this case just clean up the
704 	 * ill.
705 	 */
706 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
707 		ipif_free(ipif);
708 
709 	/*
710 	 * Used only by ill_arp_on and ill_arp_off, which are writers.
711 	 * So nobody can be using this mp now. Free the mp allocated for
712 	 * honoring ILLF_NOARP
713 	 */
714 	freemsg(ill->ill_arp_on_mp);
715 	ill->ill_arp_on_mp = NULL;
716 
717 	/* Clean up msgs on pending upcalls for mrouted */
718 	reset_mrt_ill(ill);
719 
720 	/*
721 	 * ipif_free -> reset_conn_ipif will remove all multicast
722 	 * references for IPv4. For IPv6, we need to do it here as
723 	 * it points only at ills.
724 	 */
725 	reset_conn_ill(ill);
726 
727 	/*
728 	 * ill_down will arrange to blow off any IRE's dependent on this
729 	 * ILL, and shut down fragmentation reassembly.
730 	 */
731 	ill_down(ill);
732 
733 	/* Let SCTP know, so that it can remove this from its list. */
734 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
735 
736 	/*
737 	 * If an address on this ILL is being used as a source address then
738 	 * clear out the pointers in other ILLs that point to this ILL.
739 	 */
740 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
741 	if (ill->ill_usesrc_grp_next != NULL) {
742 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
743 			ill_disband_usesrc_group(ill);
744 		} else {	/* consumer of the usesrc ILL */
745 			prev_ill = ill_prev_usesrc(ill);
746 			prev_ill->ill_usesrc_grp_next =
747 			    ill->ill_usesrc_grp_next;
748 		}
749 	}
750 	rw_exit(&ill_g_usesrc_lock);
751 }
752 
753 /*
754  * ill_delete_tail is called from ip_modclose after all references
755  * to the closing ill are gone. The wait is done in ip_modclose
756  */
757 void
758 ill_delete_tail(ill_t *ill)
759 {
760 	mblk_t	**mpp;
761 	ipif_t	*ipif;
762 
763 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
764 		ipif_down_tail(ipif);
765 
766 	/*
767 	 * If polling capability is enabled (which signifies direct
768 	 * upcall into IP and driver has ill saved as a handle),
769 	 * we need to make sure that unbind has completed before we
770 	 * let the ill disappear and driver no longer has any reference
771 	 * to this ill.
772 	 */
773 	mutex_enter(&ill->ill_lock);
774 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
775 		cv_wait(&ill->ill_cv, &ill->ill_lock);
776 	mutex_exit(&ill->ill_lock);
777 
778 	/*
779 	 * Clean up polling and soft ring capabilities
780 	 */
781 	if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING))
782 		ill_capability_dls_disable(ill);
783 
784 	/*
785 	 * Send the detach if there's one to send (i.e., if we're above a
786 	 * style 2 DLPI driver).
787 	 */
788 	if (ill->ill_detach_mp != NULL) {
789 		ill_dlpi_send(ill, ill->ill_detach_mp);
790 		ill->ill_detach_mp = NULL;
791 	}
792 
793 	if (ill->ill_net_type != IRE_LOOPBACK)
794 		qprocsoff(ill->ill_rq);
795 
796 	/*
797 	 * We do an ipsq_flush once again now. New messages could have
798 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
799 	 * could also have landed up if an ioctl thread had looked up
800 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
801 	 * enqueued the ioctl when we did the ipsq_flush last time.
802 	 */
803 	ipsq_flush(ill);
804 
805 	/*
806 	 * Free capabilities.
807 	 */
808 	if (ill->ill_ipsec_capab_ah != NULL) {
809 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH);
810 		ill_ipsec_capab_free(ill->ill_ipsec_capab_ah);
811 		ill->ill_ipsec_capab_ah = NULL;
812 	}
813 
814 	if (ill->ill_ipsec_capab_esp != NULL) {
815 		ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP);
816 		ill_ipsec_capab_free(ill->ill_ipsec_capab_esp);
817 		ill->ill_ipsec_capab_esp = NULL;
818 	}
819 
820 	if (ill->ill_mdt_capab != NULL) {
821 		kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t));
822 		ill->ill_mdt_capab = NULL;
823 	}
824 
825 	if (ill->ill_hcksum_capab != NULL) {
826 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
827 		ill->ill_hcksum_capab = NULL;
828 	}
829 
830 	if (ill->ill_zerocopy_capab != NULL) {
831 		kmem_free(ill->ill_zerocopy_capab,
832 		    sizeof (ill_zerocopy_capab_t));
833 		ill->ill_zerocopy_capab = NULL;
834 	}
835 
836 	if (ill->ill_dls_capab != NULL) {
837 		CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn);
838 		ill->ill_dls_capab->ill_unbind_conn = NULL;
839 		kmem_free(ill->ill_dls_capab,
840 		    sizeof (ill_dls_capab_t) +
841 		    (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS));
842 		ill->ill_dls_capab = NULL;
843 	}
844 
845 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL));
846 
847 	while (ill->ill_ipif != NULL)
848 		ipif_free_tail(ill->ill_ipif);
849 
850 	ill_down_tail(ill);
851 
852 	/*
853 	 * We have removed all references to ilm from conn and the ones joined
854 	 * within the kernel.
855 	 *
856 	 * We don't walk conns, mrts and ires because
857 	 *
858 	 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts.
859 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
860 	 *    ill references.
861 	 */
862 	ASSERT(ilm_walk_ill(ill) == 0);
863 	/*
864 	 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free
865 	 * could free the phyint. No more reference to the phyint after this
866 	 * point.
867 	 */
868 	(void) ill_glist_delete(ill);
869 
870 	rw_enter(&ip_g_nd_lock, RW_WRITER);
871 	if (ill->ill_ndd_name != NULL)
872 		nd_unload(&ip_g_nd, ill->ill_ndd_name);
873 	rw_exit(&ip_g_nd_lock);
874 
875 
876 	if (ill->ill_frag_ptr != NULL) {
877 		uint_t count;
878 
879 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
880 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
881 		}
882 		mi_free(ill->ill_frag_ptr);
883 		ill->ill_frag_ptr = NULL;
884 		ill->ill_frag_hash_tbl = NULL;
885 	}
886 	if (ill->ill_nd_lla_mp != NULL)
887 		freemsg(ill->ill_nd_lla_mp);
888 	/* Free all retained control messages. */
889 	mpp = &ill->ill_first_mp_to_free;
890 	do {
891 		while (mpp[0]) {
892 			mblk_t  *mp;
893 			mblk_t  *mp1;
894 
895 			mp = mpp[0];
896 			mpp[0] = mp->b_next;
897 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
898 				mp1->b_next = NULL;
899 				mp1->b_prev = NULL;
900 			}
901 			freemsg(mp);
902 		}
903 	} while (mpp++ != &ill->ill_last_mp_to_free);
904 
905 	ill_free_mib(ill);
906 	ILL_TRACE_CLEANUP(ill);
907 }
908 
909 static void
910 ill_free_mib(ill_t *ill)
911 {
912 	if (ill->ill_ip6_mib != NULL) {
913 		kmem_free(ill->ill_ip6_mib, sizeof (*ill->ill_ip6_mib));
914 		ill->ill_ip6_mib = NULL;
915 	}
916 	if (ill->ill_icmp6_mib != NULL) {
917 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
918 		ill->ill_icmp6_mib = NULL;
919 	}
920 }
921 
922 /*
923  * Concatenate together a physical address and a sap.
924  *
925  * Sap_lengths are interpreted as follows:
926  *   sap_length == 0	==>	no sap
927  *   sap_length > 0	==>	sap is at the head of the dlpi address
928  *   sap_length < 0	==>	sap is at the tail of the dlpi address
929  */
930 static void
931 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
932     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
933 {
934 	uint16_t sap_addr = (uint16_t)sap_src;
935 
936 	if (sap_length == 0) {
937 		if (phys_src == NULL)
938 			bzero(dst, phys_length);
939 		else
940 			bcopy(phys_src, dst, phys_length);
941 	} else if (sap_length < 0) {
942 		if (phys_src == NULL)
943 			bzero(dst, phys_length);
944 		else
945 			bcopy(phys_src, dst, phys_length);
946 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
947 	} else {
948 		bcopy(&sap_addr, dst, sizeof (sap_addr));
949 		if (phys_src == NULL)
950 			bzero((char *)dst + sap_length, phys_length);
951 		else
952 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
953 	}
954 }
955 
956 /*
957  * Generate a dl_unitdata_req mblk for the device and address given.
958  * addr_length is the length of the physical portion of the address.
959  * If addr is NULL include an all zero address of the specified length.
960  * TRUE? In any case, addr_length is taken to be the entire length of the
961  * dlpi address, including the absolute value of sap_length.
962  */
963 mblk_t *
964 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
965 		t_scalar_t sap_length)
966 {
967 	dl_unitdata_req_t *dlur;
968 	mblk_t	*mp;
969 	t_scalar_t	abs_sap_length;		/* absolute value */
970 
971 	abs_sap_length = ABS(sap_length);
972 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
973 		DL_UNITDATA_REQ);
974 	if (mp == NULL)
975 		return (NULL);
976 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
977 	/* HACK: accomodate incompatible DLPI drivers */
978 	if (addr_length == 8)
979 		addr_length = 6;
980 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
981 	dlur->dl_dest_addr_offset = sizeof (*dlur);
982 	dlur->dl_priority.dl_min = 0;
983 	dlur->dl_priority.dl_max = 0;
984 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
985 	    (uchar_t *)&dlur[1]);
986 	return (mp);
987 }
988 
989 /*
990  * Add the 'mp' to the list of pending mp's headed by ill_pending_mp
991  * Return an error if we already have 1 or more ioctls in progress.
992  * This is used only for non-exclusive ioctls. Currently this is used
993  * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive
994  * and thus need to use ipsq_pending_mp_add.
995  */
996 boolean_t
997 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp)
998 {
999 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1000 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1001 	/*
1002 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls.
1003 	 */
1004 	ASSERT((add_mp->b_datap->db_type == M_IOCDATA) ||
1005 	    (add_mp->b_datap->db_type == M_IOCTL));
1006 
1007 	ASSERT(MUTEX_HELD(&connp->conn_lock));
1008 	/*
1009 	 * Return error if the conn has started closing. The conn
1010 	 * could have finished cleaning up the pending mp list,
1011 	 * If so we should not add another mp to the list negating
1012 	 * the cleanup.
1013 	 */
1014 	if (connp->conn_state_flags & CONN_CLOSING)
1015 		return (B_FALSE);
1016 	/*
1017 	 * Add the pending mp to the head of the list, chained by b_next.
1018 	 * Note down the conn on which the ioctl request came, in b_prev.
1019 	 * This will be used to later get the conn, when we get a response
1020 	 * on the ill queue, from some other module (typically arp)
1021 	 */
1022 	add_mp->b_next = (void *)ill->ill_pending_mp;
1023 	add_mp->b_queue = CONNP_TO_WQ(connp);
1024 	ill->ill_pending_mp = add_mp;
1025 	if (connp != NULL)
1026 		connp->conn_oper_pending_ill = ill;
1027 	return (B_TRUE);
1028 }
1029 
1030 /*
1031  * Retrieve the ill_pending_mp and return it. We have to walk the list
1032  * of mblks starting at ill_pending_mp, and match based on the ioc_id.
1033  */
1034 mblk_t *
1035 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id)
1036 {
1037 	mblk_t	*prev = NULL;
1038 	mblk_t	*curr = NULL;
1039 	uint_t	id;
1040 	conn_t	*connp;
1041 
1042 	/*
1043 	 * When the conn closes, conn_ioctl_cleanup needs to clean
1044 	 * up the pending mp, but it does not know the ioc_id and
1045 	 * passes in a zero for it.
1046 	 */
1047 	mutex_enter(&ill->ill_lock);
1048 	if (ioc_id != 0)
1049 		*connpp = NULL;
1050 
1051 	/* Search the list for the appropriate ioctl based on ioc_id */
1052 	for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL;
1053 	    prev = curr, curr = curr->b_next) {
1054 		id = ((struct iocblk *)curr->b_rptr)->ioc_id;
1055 		connp = Q_TO_CONN(curr->b_queue);
1056 		/* Match based on the ioc_id or based on the conn */
1057 		if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp))
1058 			break;
1059 	}
1060 
1061 	if (curr != NULL) {
1062 		/* Unlink the mblk from the pending mp list */
1063 		if (prev != NULL) {
1064 			prev->b_next = curr->b_next;
1065 		} else {
1066 			ASSERT(ill->ill_pending_mp == curr);
1067 			ill->ill_pending_mp = curr->b_next;
1068 		}
1069 
1070 		/*
1071 		 * conn refcnt must have been bumped up at the start of
1072 		 * the ioctl. So we can safely access the conn.
1073 		 */
1074 		ASSERT(CONN_Q(curr->b_queue));
1075 		*connpp = Q_TO_CONN(curr->b_queue);
1076 		curr->b_next = NULL;
1077 		curr->b_queue = NULL;
1078 	}
1079 
1080 	mutex_exit(&ill->ill_lock);
1081 
1082 	return (curr);
1083 }
1084 
1085 /*
1086  * Add the pending mp to the list. There can be only 1 pending mp
1087  * in the list. Any exclusive ioctl that needs to wait for a response
1088  * from another module or driver needs to use this function to set
1089  * the ipsq_pending_mp to the ioctl mblk and wait for the response from
1090  * the other module/driver. This is also used while waiting for the
1091  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
1092  */
1093 boolean_t
1094 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
1095     int waitfor)
1096 {
1097 	ipsq_t	*ipsq;
1098 
1099 	ASSERT(IAM_WRITER_IPIF(ipif));
1100 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
1101 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
1102 	/*
1103 	 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls,
1104 	 * M_ERROR/M_HANGUP from driver
1105 	 */
1106 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) ||
1107 	    (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP));
1108 
1109 	ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
1110 	if (connp != NULL) {
1111 		ASSERT(MUTEX_HELD(&connp->conn_lock));
1112 		/*
1113 		 * Return error if the conn has started closing. The conn
1114 		 * could have finished cleaning up the pending mp list,
1115 		 * If so we should not add another mp to the list negating
1116 		 * the cleanup.
1117 		 */
1118 		if (connp->conn_state_flags & CONN_CLOSING)
1119 			return (B_FALSE);
1120 	}
1121 	mutex_enter(&ipsq->ipsq_lock);
1122 	ipsq->ipsq_pending_ipif = ipif;
1123 	/*
1124 	 * Note down the queue in b_queue. This will be returned by
1125 	 * ipsq_pending_mp_get. Caller will then use these values to restart
1126 	 * the processing
1127 	 */
1128 	add_mp->b_next = NULL;
1129 	add_mp->b_queue = q;
1130 	ipsq->ipsq_pending_mp = add_mp;
1131 	ipsq->ipsq_waitfor = waitfor;
1132 	/*
1133 	 * ipsq_current_ipif is needed to restart the operation from
1134 	 * ipif_ill_refrele_tail when the last reference to the ipi/ill
1135 	 * is gone. Since this is not an ioctl ipsq_current_ipif has not
1136 	 * been set until now.
1137 	 */
1138 	if (DB_TYPE(add_mp) == M_ERROR || DB_TYPE(add_mp) == M_HANGUP) {
1139 		ASSERT(ipsq->ipsq_current_ipif == NULL);
1140 		ipsq->ipsq_current_ipif = ipif;
1141 		ipsq->ipsq_last_cmd = DB_TYPE(add_mp);
1142 	}
1143 	if (connp != NULL)
1144 		connp->conn_oper_pending_ill = ipif->ipif_ill;
1145 	mutex_exit(&ipsq->ipsq_lock);
1146 	return (B_TRUE);
1147 }
1148 
1149 /*
1150  * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp
1151  * queued in the list.
1152  */
1153 mblk_t *
1154 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
1155 {
1156 	mblk_t	*curr = NULL;
1157 
1158 	mutex_enter(&ipsq->ipsq_lock);
1159 	*connpp = NULL;
1160 	if (ipsq->ipsq_pending_mp == NULL) {
1161 		mutex_exit(&ipsq->ipsq_lock);
1162 		return (NULL);
1163 	}
1164 
1165 	/* There can be only 1 such excl message */
1166 	curr = ipsq->ipsq_pending_mp;
1167 	ASSERT(curr != NULL && curr->b_next == NULL);
1168 	ipsq->ipsq_pending_ipif = NULL;
1169 	ipsq->ipsq_pending_mp = NULL;
1170 	ipsq->ipsq_waitfor = 0;
1171 	mutex_exit(&ipsq->ipsq_lock);
1172 
1173 	if (CONN_Q(curr->b_queue)) {
1174 		/*
1175 		 * This mp did a refhold on the conn, at the start of the ioctl.
1176 		 * So we can safely return a pointer to the conn to the caller.
1177 		 */
1178 		*connpp = Q_TO_CONN(curr->b_queue);
1179 	} else {
1180 		*connpp = NULL;
1181 	}
1182 	curr->b_next = NULL;
1183 	curr->b_prev = NULL;
1184 	return (curr);
1185 }
1186 
1187 /*
1188  * Cleanup the ioctl mp queued in ipsq_pending_mp
1189  * - Called in the ill_delete path
1190  * - Called in the M_ERROR or M_HANGUP path on the ill.
1191  * - Called in the conn close path.
1192  */
1193 boolean_t
1194 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
1195 {
1196 	mblk_t	*mp;
1197 	ipsq_t	*ipsq;
1198 	queue_t	*q;
1199 	ipif_t	*ipif;
1200 
1201 	ASSERT(IAM_WRITER_ILL(ill));
1202 	ipsq = ill->ill_phyint->phyint_ipsq;
1203 	mutex_enter(&ipsq->ipsq_lock);
1204 	/*
1205 	 * If connp is null, unconditionally clean up the ipsq_pending_mp.
1206 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
1207 	 * even if it is meant for another ill, since we have to enqueue
1208 	 * a new mp now in ipsq_pending_mp to complete the ipif_down.
1209 	 * If connp is non-null we are called from the conn close path.
1210 	 */
1211 	mp = ipsq->ipsq_pending_mp;
1212 	if (mp == NULL || (connp != NULL &&
1213 	    mp->b_queue != CONNP_TO_WQ(connp))) {
1214 		mutex_exit(&ipsq->ipsq_lock);
1215 		return (B_FALSE);
1216 	}
1217 	/* Now remove from the ipsq_pending_mp */
1218 	ipsq->ipsq_pending_mp = NULL;
1219 	q = mp->b_queue;
1220 	mp->b_next = NULL;
1221 	mp->b_prev = NULL;
1222 	mp->b_queue = NULL;
1223 
1224 	/* If MOVE was in progress, clear the move_in_progress fields also. */
1225 	ill = ipsq->ipsq_pending_ipif->ipif_ill;
1226 	if (ill->ill_move_in_progress) {
1227 		ILL_CLEAR_MOVE(ill);
1228 	} else if (ill->ill_up_ipifs) {
1229 		ill_group_cleanup(ill);
1230 	}
1231 
1232 	ipif = ipsq->ipsq_pending_ipif;
1233 	ipsq->ipsq_pending_ipif = NULL;
1234 	ipsq->ipsq_waitfor = 0;
1235 	ipsq->ipsq_current_ipif = NULL;
1236 	mutex_exit(&ipsq->ipsq_lock);
1237 
1238 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
1239 		ip_ioctl_finish(q, mp, ENXIO, connp != NULL ? CONN_CLOSE :
1240 		    NO_COPYOUT, connp != NULL ? ipif : NULL, NULL);
1241 	} else {
1242 		/*
1243 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
1244 		 * be just inet_freemsg. we have to restart it
1245 		 * otherwise the thread will be stuck.
1246 		 */
1247 		inet_freemsg(mp);
1248 	}
1249 	return (B_TRUE);
1250 }
1251 
1252 /*
1253  * The ill is closing. Cleanup all the pending mps. Called exclusively
1254  * towards the end of ill_delete. The refcount has gone to 0. So nobody
1255  * knows this ill, and hence nobody can add an mp to this list
1256  */
1257 static void
1258 ill_pending_mp_cleanup(ill_t *ill)
1259 {
1260 	mblk_t	*mp;
1261 	queue_t	*q;
1262 
1263 	ASSERT(IAM_WRITER_ILL(ill));
1264 
1265 	mutex_enter(&ill->ill_lock);
1266 	/*
1267 	 * Every mp on the pending mp list originating from an ioctl
1268 	 * added 1 to the conn refcnt, at the start of the ioctl.
1269 	 * So bump it down now.  See comments in ip_wput_nondata()
1270 	 */
1271 	while (ill->ill_pending_mp != NULL) {
1272 		mp = ill->ill_pending_mp;
1273 		ill->ill_pending_mp = mp->b_next;
1274 		mutex_exit(&ill->ill_lock);
1275 
1276 		q = mp->b_queue;
1277 		ASSERT(CONN_Q(q));
1278 		mp->b_next = NULL;
1279 		mp->b_prev = NULL;
1280 		mp->b_queue = NULL;
1281 		ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL, NULL);
1282 		mutex_enter(&ill->ill_lock);
1283 	}
1284 	ill->ill_pending_ipif = NULL;
1285 
1286 	mutex_exit(&ill->ill_lock);
1287 }
1288 
1289 /*
1290  * Called in the conn close path and ill delete path
1291  */
1292 static void
1293 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
1294 {
1295 	ipsq_t	*ipsq;
1296 	mblk_t	*prev;
1297 	mblk_t	*curr;
1298 	mblk_t	*next;
1299 	queue_t	*q;
1300 	mblk_t	*tmp_list = NULL;
1301 
1302 	ASSERT(IAM_WRITER_ILL(ill));
1303 	if (connp != NULL)
1304 		q = CONNP_TO_WQ(connp);
1305 	else
1306 		q = ill->ill_wq;
1307 
1308 	ipsq = ill->ill_phyint->phyint_ipsq;
1309 	/*
1310 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
1311 	 * In the case of ioctl from a conn, there can be only 1 mp
1312 	 * queued on the ipsq. If an ill is being unplumbed, only messages
1313 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
1314 	 * ioctls meant for this ill form conn's are not flushed. They will
1315 	 * be processed during ipsq_exit and will not find the ill and will
1316 	 * return error.
1317 	 */
1318 	mutex_enter(&ipsq->ipsq_lock);
1319 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
1320 	    curr = next) {
1321 		next = curr->b_next;
1322 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
1323 			/* Unlink the mblk from the pending mp list */
1324 			if (prev != NULL) {
1325 				prev->b_next = curr->b_next;
1326 			} else {
1327 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
1328 				ipsq->ipsq_xopq_mphead = curr->b_next;
1329 			}
1330 			if (ipsq->ipsq_xopq_mptail == curr)
1331 				ipsq->ipsq_xopq_mptail = prev;
1332 			/*
1333 			 * Create a temporary list and release the ipsq lock
1334 			 * New elements are added to the head of the tmp_list
1335 			 */
1336 			curr->b_next = tmp_list;
1337 			tmp_list = curr;
1338 		} else {
1339 			prev = curr;
1340 		}
1341 	}
1342 	mutex_exit(&ipsq->ipsq_lock);
1343 
1344 	while (tmp_list != NULL) {
1345 		curr = tmp_list;
1346 		tmp_list = curr->b_next;
1347 		curr->b_next = NULL;
1348 		curr->b_prev = NULL;
1349 		curr->b_queue = NULL;
1350 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1351 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
1352 			    CONN_CLOSE : NO_COPYOUT, NULL, NULL);
1353 		} else {
1354 			/*
1355 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1356 			 * this can't be just inet_freemsg. we have to
1357 			 * restart it otherwise the thread will be stuck.
1358 			 */
1359 			inet_freemsg(curr);
1360 		}
1361 	}
1362 }
1363 
1364 /*
1365  * This conn has started closing. Cleanup any pending ioctl from this conn.
1366  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
1367  */
1368 void
1369 conn_ioctl_cleanup(conn_t *connp)
1370 {
1371 	mblk_t *curr;
1372 	ipsq_t	*ipsq;
1373 	ill_t	*ill;
1374 	boolean_t refheld;
1375 
1376 	/*
1377 	 * Is any exclusive ioctl pending ? If so clean it up. If the
1378 	 * ioctl has not yet started, the mp is pending in the list headed by
1379 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
1380 	 * ipsq_pending_mp. If the ioctl timed out in the streamhead but
1381 	 * is currently executing now the mp is not queued anywhere but
1382 	 * conn_oper_pending_ill is null. The conn close will wait
1383 	 * till the conn_ref drops to zero.
1384 	 */
1385 	mutex_enter(&connp->conn_lock);
1386 	ill = connp->conn_oper_pending_ill;
1387 	if (ill == NULL) {
1388 		mutex_exit(&connp->conn_lock);
1389 		return;
1390 	}
1391 
1392 	curr = ill_pending_mp_get(ill, &connp, 0);
1393 	if (curr != NULL) {
1394 		mutex_exit(&connp->conn_lock);
1395 		CONN_DEC_REF(connp);
1396 		inet_freemsg(curr);
1397 		return;
1398 	}
1399 	/*
1400 	 * We may not be able to refhold the ill if the ill/ipif
1401 	 * is changing. But we need to make sure that the ill will
1402 	 * not vanish. So we just bump up the ill_waiter count.
1403 	 */
1404 	refheld = ill_waiter_inc(ill);
1405 	mutex_exit(&connp->conn_lock);
1406 	if (refheld) {
1407 		if (ipsq_enter(ill, B_TRUE)) {
1408 			ill_waiter_dcr(ill);
1409 			/*
1410 			 * Check whether this ioctl has started and is
1411 			 * pending now in ipsq_pending_mp. If it is not
1412 			 * found there then check whether this ioctl has
1413 			 * not even started and is in the ipsq_xopq list.
1414 			 */
1415 			if (!ipsq_pending_mp_cleanup(ill, connp))
1416 				ipsq_xopq_mp_cleanup(ill, connp);
1417 			ipsq = ill->ill_phyint->phyint_ipsq;
1418 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
1419 			return;
1420 		}
1421 	}
1422 
1423 	/*
1424 	 * The ill is also closing and we could not bump up the
1425 	 * ill_waiter_count or we could not enter the ipsq. Leave
1426 	 * the cleanup to ill_delete
1427 	 */
1428 	mutex_enter(&connp->conn_lock);
1429 	while (connp->conn_oper_pending_ill != NULL)
1430 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1431 	mutex_exit(&connp->conn_lock);
1432 	if (refheld)
1433 		ill_waiter_dcr(ill);
1434 }
1435 
1436 /*
1437  * ipcl_walk function for cleaning up conn_*_ill fields.
1438  */
1439 static void
1440 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1441 {
1442 	ill_t	*ill = (ill_t *)arg;
1443 	ire_t	*ire;
1444 
1445 	mutex_enter(&connp->conn_lock);
1446 	if (connp->conn_multicast_ill == ill) {
1447 		/* Revert to late binding */
1448 		connp->conn_multicast_ill = NULL;
1449 		connp->conn_orig_multicast_ifindex = 0;
1450 	}
1451 	if (connp->conn_incoming_ill == ill)
1452 		connp->conn_incoming_ill = NULL;
1453 	if (connp->conn_outgoing_ill == ill)
1454 		connp->conn_outgoing_ill = NULL;
1455 	if (connp->conn_outgoing_pill == ill)
1456 		connp->conn_outgoing_pill = NULL;
1457 	if (connp->conn_nofailover_ill == ill)
1458 		connp->conn_nofailover_ill = NULL;
1459 	if (connp->conn_xmit_if_ill == ill)
1460 		connp->conn_xmit_if_ill = NULL;
1461 	if (connp->conn_ire_cache != NULL) {
1462 		ire = connp->conn_ire_cache;
1463 		/*
1464 		 * ip_newroute creates IRE_CACHE with ire_stq coming from
1465 		 * interface X and ipif coming from interface Y, if interface
1466 		 * X and Y are part of the same IPMPgroup. Thus whenever
1467 		 * interface X goes down, remove all references to it by
1468 		 * checking both on ire_ipif and ire_stq.
1469 		 */
1470 		if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1471 		    (ire->ire_type == IRE_CACHE &&
1472 		    ire->ire_stq == ill->ill_wq)) {
1473 			connp->conn_ire_cache = NULL;
1474 			mutex_exit(&connp->conn_lock);
1475 			ire_refrele_notr(ire);
1476 			return;
1477 		}
1478 	}
1479 	mutex_exit(&connp->conn_lock);
1480 
1481 }
1482 
1483 /* ARGSUSED */
1484 void
1485 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1486 {
1487 	ill_t	*ill = q->q_ptr;
1488 	ipif_t	*ipif;
1489 
1490 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1491 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1492 		ipif_down_tail(ipif);
1493 	ill_down_tail(ill);
1494 	freemsg(mp);
1495 	ipsq->ipsq_current_ipif = NULL;
1496 }
1497 
1498 /*
1499  * ill_down_start is called when we want to down this ill and bring it up again
1500  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1501  * all interfaces, but don't tear down any plumbing.
1502  */
1503 boolean_t
1504 ill_down_start(queue_t *q, mblk_t *mp)
1505 {
1506 	ill_t	*ill;
1507 	ipif_t	*ipif;
1508 
1509 	ill = q->q_ptr;
1510 
1511 	ASSERT(IAM_WRITER_ILL(ill));
1512 
1513 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1514 		(void) ipif_down(ipif, NULL, NULL);
1515 
1516 	ill_down(ill);
1517 
1518 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1519 	mutex_enter(&ill->ill_lock);
1520 	/*
1521 	 * Atomically test and add the pending mp if references are
1522 	 * still active.
1523 	 */
1524 	if (!ill_is_quiescent(ill)) {
1525 		/*
1526 		 * Get rid of any pending mps and cleanup. Call will
1527 		 * not fail since we are passing a null connp.
1528 		 */
1529 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1530 		    mp, ILL_DOWN);
1531 		mutex_exit(&ill->ill_lock);
1532 		return (B_FALSE);
1533 	}
1534 	mutex_exit(&ill->ill_lock);
1535 	return (B_TRUE);
1536 }
1537 
1538 static void
1539 ill_down(ill_t *ill)
1540 {
1541 	/* Blow off any IREs dependent on this ILL. */
1542 	ire_walk(ill_downi, (char *)ill);
1543 
1544 	mutex_enter(&ire_mrtun_lock);
1545 	if (ire_mrtun_count != 0) {
1546 		mutex_exit(&ire_mrtun_lock);
1547 		ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif,
1548 		    (char *)ill, NULL);
1549 	} else {
1550 		mutex_exit(&ire_mrtun_lock);
1551 	}
1552 
1553 	/*
1554 	 * If any interface based forwarding table exists
1555 	 * Blow off the ires there dependent on this ill
1556 	 */
1557 	mutex_enter(&ire_srcif_table_lock);
1558 	if (ire_srcif_table_count > 0) {
1559 		mutex_exit(&ire_srcif_table_lock);
1560 		ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill);
1561 	} else {
1562 		mutex_exit(&ire_srcif_table_lock);
1563 	}
1564 
1565 	/* Remove any conn_*_ill depending on this ill */
1566 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill);
1567 
1568 	if (ill->ill_group != NULL) {
1569 		illgrp_delete(ill);
1570 	}
1571 
1572 }
1573 
1574 static void
1575 ill_down_tail(ill_t *ill)
1576 {
1577 	int	i;
1578 
1579 	/* Destroy ill_srcif_table if it exists */
1580 	/* Lock not reqd really because nobody should be able to access */
1581 	mutex_enter(&ill->ill_lock);
1582 	if (ill->ill_srcif_table != NULL) {
1583 		ill->ill_srcif_refcnt = 0;
1584 		for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) {
1585 			rw_destroy(&ill->ill_srcif_table[i].irb_lock);
1586 		}
1587 		kmem_free(ill->ill_srcif_table,
1588 		    IP_SRCIF_TABLE_SIZE * sizeof (irb_t));
1589 		ill->ill_srcif_table = NULL;
1590 		ill->ill_srcif_refcnt = 0;
1591 		ill->ill_mrtun_refcnt = 0;
1592 	}
1593 	mutex_exit(&ill->ill_lock);
1594 }
1595 
1596 /*
1597  * ire_walk routine used to delete every IRE that depends on queues
1598  * associated with 'ill'.  (Always called as writer.)
1599  */
1600 static void
1601 ill_downi(ire_t *ire, char *ill_arg)
1602 {
1603 	ill_t	*ill = (ill_t *)ill_arg;
1604 
1605 	/*
1606 	 * ip_newroute creates IRE_CACHE with ire_stq coming from
1607 	 * interface X and ipif coming from interface Y, if interface
1608 	 * X and Y are part of the same IPMP group. Thus whenever interface
1609 	 * X goes down, remove all references to it by checking both
1610 	 * on ire_ipif and ire_stq.
1611 	 */
1612 	if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) ||
1613 	    (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) {
1614 		ire_delete(ire);
1615 	}
1616 }
1617 
1618 /*
1619  * A seperate routine for deleting revtun and srcif based routes
1620  * are needed because the ires only deleted when the interface
1621  * is unplumbed. Also these ires have ire_in_ill non-null as well.
1622  * we want to keep mobile IP specific code separate.
1623  */
1624 static void
1625 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg)
1626 {
1627 	ill_t   *ill = (ill_t *)ill_arg;
1628 
1629 	ASSERT(ire->ire_in_ill != NULL);
1630 
1631 	if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) ||
1632 	    (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) {
1633 		ire_delete(ire);
1634 	}
1635 }
1636 
1637 /*
1638  * Remove ire/nce from the fastpath list.
1639  */
1640 void
1641 ill_fastpath_nack(ill_t *ill)
1642 {
1643 	if (ill->ill_isv6) {
1644 		nce_fastpath_list_dispatch(ill, NULL, NULL);
1645 	} else {
1646 		ire_fastpath_list_dispatch(ill, NULL, NULL);
1647 	}
1648 }
1649 
1650 /* Consume an M_IOCACK of the fastpath probe. */
1651 void
1652 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1653 {
1654 	mblk_t	*mp1 = mp;
1655 
1656 	/*
1657 	 * If this was the first attempt turn on the fastpath probing.
1658 	 */
1659 	mutex_enter(&ill->ill_lock);
1660 	if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS)
1661 		ill->ill_dlpi_fastpath_state = IDMS_OK;
1662 	mutex_exit(&ill->ill_lock);
1663 
1664 	/* Free the M_IOCACK mblk, hold on to the data */
1665 	mp = mp->b_cont;
1666 	freeb(mp1);
1667 	if (mp == NULL)
1668 		return;
1669 	if (mp->b_cont != NULL) {
1670 		/*
1671 		 * Update all IRE's or NCE's that are waiting for
1672 		 * fastpath update.
1673 		 */
1674 		if (ill->ill_isv6) {
1675 			/*
1676 			 * update nce's in the fastpath list.
1677 			 */
1678 			nce_fastpath_list_dispatch(ill,
1679 			    ndp_fastpath_update, mp);
1680 		} else {
1681 
1682 			/*
1683 			 * update ire's in the fastpath list.
1684 			 */
1685 			ire_fastpath_list_dispatch(ill,
1686 			    ire_fastpath_update, mp);
1687 			/*
1688 			 * Check if we need to traverse reverse tunnel table.
1689 			 * Since there is only single ire_type (IRE_MIPRTUN)
1690 			 * in the table, we don't need to match on ire_type.
1691 			 * We have to check ire_mrtun_count and not the
1692 			 * ill_mrtun_refcnt since ill_mrtun_refcnt is set
1693 			 * on the incoming ill and here we are dealing with
1694 			 * outgoing ill.
1695 			 */
1696 			mutex_enter(&ire_mrtun_lock);
1697 			if (ire_mrtun_count != 0) {
1698 				mutex_exit(&ire_mrtun_lock);
1699 				ire_walk_ill_mrtun(MATCH_IRE_WQ, IRE_MIPRTUN,
1700 				    (void (*)(ire_t *, void *))
1701 					ire_fastpath_update, mp, ill);
1702 			} else {
1703 				mutex_exit(&ire_mrtun_lock);
1704 			}
1705 		}
1706 		mp1 = mp->b_cont;
1707 		freeb(mp);
1708 		mp = mp1;
1709 	} else {
1710 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1711 	}
1712 
1713 	freeb(mp);
1714 }
1715 
1716 /*
1717  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1718  * The data portion of the request is a dl_unitdata_req_t template for
1719  * what we would send downstream in the absence of a fastpath confirmation.
1720  */
1721 int
1722 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1723 {
1724 	struct iocblk	*ioc;
1725 	mblk_t	*mp;
1726 
1727 	if (dlur_mp == NULL)
1728 		return (EINVAL);
1729 
1730 	mutex_enter(&ill->ill_lock);
1731 	switch (ill->ill_dlpi_fastpath_state) {
1732 	case IDMS_FAILED:
1733 		/*
1734 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1735 		 * support it.
1736 		 */
1737 		mutex_exit(&ill->ill_lock);
1738 		return (ENOTSUP);
1739 	case IDMS_UNKNOWN:
1740 		/* This is the first probe */
1741 		ill->ill_dlpi_fastpath_state = IDMS_INPROGRESS;
1742 		break;
1743 	default:
1744 		break;
1745 	}
1746 	mutex_exit(&ill->ill_lock);
1747 
1748 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1749 		return (EAGAIN);
1750 
1751 	mp->b_cont = copyb(dlur_mp);
1752 	if (mp->b_cont == NULL) {
1753 		freeb(mp);
1754 		return (EAGAIN);
1755 	}
1756 
1757 	ioc = (struct iocblk *)mp->b_rptr;
1758 	ioc->ioc_count = msgdsize(mp->b_cont);
1759 
1760 	putnext(ill->ill_wq, mp);
1761 	return (0);
1762 }
1763 
1764 void
1765 ill_capability_probe(ill_t *ill)
1766 {
1767 	/*
1768 	 * Do so only if negotiation is enabled, capabilities are unknown,
1769 	 * and a capability negotiation is not already in progress.
1770 	 */
1771 	if (ill->ill_capab_state != IDMS_UNKNOWN &&
1772 	    ill->ill_capab_state != IDMS_RENEG)
1773 		return;
1774 
1775 	ill->ill_capab_state = IDMS_INPROGRESS;
1776 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1777 	ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL);
1778 }
1779 
1780 void
1781 ill_capability_reset(ill_t *ill)
1782 {
1783 	mblk_t *sc_mp = NULL;
1784 	mblk_t *tmp;
1785 
1786 	/*
1787 	 * Note here that we reset the state to UNKNOWN, and later send
1788 	 * down the DL_CAPABILITY_REQ without first setting the state to
1789 	 * INPROGRESS.  We do this in order to distinguish the
1790 	 * DL_CAPABILITY_ACK response which may come back in response to
1791 	 * a "reset" apart from the "probe" DL_CAPABILITY_REQ.  This would
1792 	 * also handle the case where the driver doesn't send us back
1793 	 * a DL_CAPABILITY_ACK in response, since the "probe" routine
1794 	 * requires the state to be in UNKNOWN anyway.  In any case, all
1795 	 * features are turned off until the state reaches IDMS_OK.
1796 	 */
1797 	ill->ill_capab_state = IDMS_UNKNOWN;
1798 
1799 	/*
1800 	 * Disable sub-capabilities and request a list of sub-capability
1801 	 * messages which will be sent down to the driver.  Each handler
1802 	 * allocates the corresponding dl_capability_sub_t inside an
1803 	 * mblk, and links it to the existing sc_mp mblk, or return it
1804 	 * as sc_mp if it's the first sub-capability (the passed in
1805 	 * sc_mp is NULL).  Upon returning from all capability handlers,
1806 	 * sc_mp will be pulled-up, before passing it downstream.
1807 	 */
1808 	ill_capability_mdt_reset(ill, &sc_mp);
1809 	ill_capability_hcksum_reset(ill, &sc_mp);
1810 	ill_capability_zerocopy_reset(ill, &sc_mp);
1811 	ill_capability_ipsec_reset(ill, &sc_mp);
1812 	ill_capability_dls_reset(ill, &sc_mp);
1813 
1814 	/* Nothing to send down in order to disable the capabilities? */
1815 	if (sc_mp == NULL)
1816 		return;
1817 
1818 	tmp = msgpullup(sc_mp, -1);
1819 	freemsg(sc_mp);
1820 	if ((sc_mp = tmp) == NULL) {
1821 		cmn_err(CE_WARN, "ill_capability_reset: unable to send down "
1822 		    "DL_CAPABILITY_REQ (ENOMEM)\n");
1823 		return;
1824 	}
1825 
1826 	ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n"));
1827 	ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp);
1828 }
1829 
1830 /*
1831  * Request or set new-style hardware capabilities supported by DLS provider.
1832  */
1833 static void
1834 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp)
1835 {
1836 	mblk_t *mp;
1837 	dl_capability_req_t *capb;
1838 	size_t size = 0;
1839 	uint8_t *ptr;
1840 
1841 	if (reqp != NULL)
1842 		size = MBLKL(reqp);
1843 
1844 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type);
1845 	if (mp == NULL) {
1846 		freemsg(reqp);
1847 		return;
1848 	}
1849 	ptr = mp->b_rptr;
1850 
1851 	capb = (dl_capability_req_t *)ptr;
1852 	ptr += sizeof (dl_capability_req_t);
1853 
1854 	if (reqp != NULL) {
1855 		capb->dl_sub_offset = sizeof (dl_capability_req_t);
1856 		capb->dl_sub_length = size;
1857 		bcopy(reqp->b_rptr, ptr, size);
1858 		ptr += size;
1859 		mp->b_cont = reqp->b_cont;
1860 		freeb(reqp);
1861 	}
1862 	ASSERT(ptr == mp->b_wptr);
1863 
1864 	ill_dlpi_send(ill, mp);
1865 }
1866 
1867 static void
1868 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1869 {
1870 	dl_capab_id_t *id_ic;
1871 	uint_t sub_dl_cap = outers->dl_cap;
1872 	dl_capability_sub_t *inners;
1873 	uint8_t *capend;
1874 
1875 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1876 
1877 	/*
1878 	 * Note: range checks here are not absolutely sufficient to
1879 	 * make us robust against malformed messages sent by drivers;
1880 	 * this is in keeping with the rest of IP's dlpi handling.
1881 	 * (Remember, it's coming from something else in the kernel
1882 	 * address space)
1883 	 */
1884 
1885 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1886 	if (capend > mp->b_wptr) {
1887 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1888 		    "malformed sub-capability too long for mblk");
1889 		return;
1890 	}
1891 
1892 	id_ic = (dl_capab_id_t *)(outers + 1);
1893 
1894 	if (outers->dl_length < sizeof (*id_ic) ||
1895 	    (inners = &id_ic->id_subcap,
1896 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1897 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1898 		    "encapsulated capab type %d too long for mblk",
1899 		    inners->dl_cap);
1900 		return;
1901 	}
1902 
1903 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1904 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1905 		    "isn't as expected; pass-thru module(s) detected, "
1906 		    "discarding capability\n", inners->dl_cap));
1907 		return;
1908 	}
1909 
1910 	/* Process the encapsulated sub-capability */
1911 	ill_capability_dispatch(ill, mp, inners, B_TRUE);
1912 }
1913 
1914 /*
1915  * Process Multidata Transmit capability negotiation ack received from a
1916  * DLS Provider.  isub must point to the sub-capability (DL_CAPAB_MDT) of a
1917  * DL_CAPABILITY_ACK message.
1918  */
1919 static void
1920 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1921 {
1922 	mblk_t *nmp = NULL;
1923 	dl_capability_req_t *oc;
1924 	dl_capab_mdt_t *mdt_ic, *mdt_oc;
1925 	ill_mdt_capab_t **ill_mdt_capab;
1926 	uint_t sub_dl_cap = isub->dl_cap;
1927 	uint8_t *capend;
1928 
1929 	ASSERT(sub_dl_cap == DL_CAPAB_MDT);
1930 
1931 	ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab;
1932 
1933 	/*
1934 	 * Note: range checks here are not absolutely sufficient to
1935 	 * make us robust against malformed messages sent by drivers;
1936 	 * this is in keeping with the rest of IP's dlpi handling.
1937 	 * (Remember, it's coming from something else in the kernel
1938 	 * address space)
1939 	 */
1940 
1941 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1942 	if (capend > mp->b_wptr) {
1943 		cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1944 		    "malformed sub-capability too long for mblk");
1945 		return;
1946 	}
1947 
1948 	mdt_ic = (dl_capab_mdt_t *)(isub + 1);
1949 
1950 	if (mdt_ic->mdt_version != MDT_VERSION_2) {
1951 		cmn_err(CE_CONT, "ill_capability_mdt_ack: "
1952 		    "unsupported MDT sub-capability (version %d, expected %d)",
1953 		    mdt_ic->mdt_version, MDT_VERSION_2);
1954 		return;
1955 	}
1956 
1957 	if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) {
1958 		ip1dbg(("ill_capability_mdt_ack: mid token for MDT "
1959 		    "capability isn't as expected; pass-thru module(s) "
1960 		    "detected, discarding capability\n"));
1961 		return;
1962 	}
1963 
1964 	if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) {
1965 
1966 		if (*ill_mdt_capab == NULL) {
1967 			*ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t),
1968 			    KM_NOSLEEP);
1969 
1970 			if (*ill_mdt_capab == NULL) {
1971 				cmn_err(CE_WARN, "ill_capability_mdt_ack: "
1972 				    "could not enable MDT version %d "
1973 				    "for %s (ENOMEM)\n", MDT_VERSION_2,
1974 				    ill->ill_name);
1975 				return;
1976 			}
1977 		}
1978 
1979 		ip1dbg(("ill_capability_mdt_ack: interface %s supports "
1980 		    "MDT version %d (%d bytes leading, %d bytes trailing "
1981 		    "header spaces, %d max pld bufs, %d span limit)\n",
1982 		    ill->ill_name, MDT_VERSION_2,
1983 		    mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail,
1984 		    mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit));
1985 
1986 		(*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2;
1987 		(*ill_mdt_capab)->ill_mdt_on = 1;
1988 		/*
1989 		 * Round the following values to the nearest 32-bit; ULP
1990 		 * may further adjust them to accomodate for additional
1991 		 * protocol headers.  We pass these values to ULP during
1992 		 * bind time.
1993 		 */
1994 		(*ill_mdt_capab)->ill_mdt_hdr_head =
1995 		    roundup(mdt_ic->mdt_hdr_head, 4);
1996 		(*ill_mdt_capab)->ill_mdt_hdr_tail =
1997 		    roundup(mdt_ic->mdt_hdr_tail, 4);
1998 		(*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld;
1999 		(*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit;
2000 
2001 		ill->ill_capabilities |= ILL_CAPAB_MDT;
2002 	} else {
2003 		uint_t size;
2004 		uchar_t *rptr;
2005 
2006 		size = sizeof (dl_capability_req_t) +
2007 		    sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t);
2008 
2009 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2010 			cmn_err(CE_WARN, "ill_capability_mdt_ack: "
2011 			    "could not enable MDT for %s (ENOMEM)\n",
2012 			    ill->ill_name);
2013 			return;
2014 		}
2015 
2016 		rptr = nmp->b_rptr;
2017 		/* initialize dl_capability_req_t */
2018 		oc = (dl_capability_req_t *)nmp->b_rptr;
2019 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
2020 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
2021 		    sizeof (dl_capab_mdt_t);
2022 		nmp->b_rptr += sizeof (dl_capability_req_t);
2023 
2024 		/* initialize dl_capability_sub_t */
2025 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
2026 		nmp->b_rptr += sizeof (*isub);
2027 
2028 		/* initialize dl_capab_mdt_t */
2029 		mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr;
2030 		bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic));
2031 
2032 		nmp->b_rptr = rptr;
2033 
2034 		ip1dbg(("ill_capability_mdt_ack: asking interface %s "
2035 		    "to enable MDT version %d\n", ill->ill_name,
2036 		    MDT_VERSION_2));
2037 
2038 		/* set ENABLE flag */
2039 		mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE;
2040 
2041 		/* nmp points to a DL_CAPABILITY_REQ message to enable MDT */
2042 		ill_dlpi_send(ill, nmp);
2043 	}
2044 }
2045 
2046 static void
2047 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp)
2048 {
2049 	mblk_t *mp;
2050 	dl_capab_mdt_t *mdt_subcap;
2051 	dl_capability_sub_t *dl_subcap;
2052 	int size;
2053 
2054 	if (!ILL_MDT_CAPABLE(ill))
2055 		return;
2056 
2057 	ASSERT(ill->ill_mdt_capab != NULL);
2058 	/*
2059 	 * Clear the capability flag for MDT but retain the ill_mdt_capab
2060 	 * structure since it's possible that another thread is still
2061 	 * referring to it.  The structure only gets deallocated when
2062 	 * we destroy the ill.
2063 	 */
2064 	ill->ill_capabilities &= ~ILL_CAPAB_MDT;
2065 
2066 	size = sizeof (*dl_subcap) + sizeof (*mdt_subcap);
2067 
2068 	mp = allocb(size, BPRI_HI);
2069 	if (mp == NULL) {
2070 		ip1dbg(("ill_capability_mdt_reset: unable to allocate "
2071 		    "request to disable MDT\n"));
2072 		return;
2073 	}
2074 
2075 	mp->b_wptr = mp->b_rptr + size;
2076 
2077 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2078 	dl_subcap->dl_cap = DL_CAPAB_MDT;
2079 	dl_subcap->dl_length = sizeof (*mdt_subcap);
2080 
2081 	mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1);
2082 	mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version;
2083 	mdt_subcap->mdt_flags = 0;
2084 	mdt_subcap->mdt_hdr_head = 0;
2085 	mdt_subcap->mdt_hdr_tail = 0;
2086 
2087 	if (*sc_mp != NULL)
2088 		linkb(*sc_mp, mp);
2089 	else
2090 		*sc_mp = mp;
2091 }
2092 
2093 /*
2094  * Send a DL_NOTIFY_REQ to the specified ill to enable
2095  * DL_NOTE_PROMISC_ON/OFF_PHYS notifications.
2096  * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware
2097  * acceleration.
2098  * Returns B_TRUE on success, B_FALSE if the message could not be sent.
2099  */
2100 static boolean_t
2101 ill_enable_promisc_notify(ill_t *ill)
2102 {
2103 	mblk_t *mp;
2104 	dl_notify_req_t *req;
2105 
2106 	IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n"));
2107 
2108 	mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ);
2109 	if (mp == NULL)
2110 		return (B_FALSE);
2111 
2112 	req = (dl_notify_req_t *)mp->b_rptr;
2113 	req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS |
2114 	    DL_NOTE_PROMISC_OFF_PHYS;
2115 
2116 	ill_dlpi_send(ill, mp);
2117 
2118 	return (B_TRUE);
2119 }
2120 
2121 
2122 /*
2123  * Allocate an IPsec capability request which will be filled by our
2124  * caller to turn on support for one or more algorithms.
2125  */
2126 static mblk_t *
2127 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub)
2128 {
2129 	mblk_t *nmp;
2130 	dl_capability_req_t	*ocap;
2131 	dl_capab_ipsec_t	*ocip;
2132 	dl_capab_ipsec_t	*icip;
2133 	uint8_t			*ptr;
2134 	icip = (dl_capab_ipsec_t *)(isub + 1);
2135 
2136 	/*
2137 	 * The first time around, we send a DL_NOTIFY_REQ to enable
2138 	 * PROMISC_ON/OFF notification from the provider. We need to
2139 	 * do this before enabling the algorithms to avoid leakage of
2140 	 * cleartext packets.
2141 	 */
2142 
2143 	if (!ill_enable_promisc_notify(ill))
2144 		return (NULL);
2145 
2146 	/*
2147 	 * Allocate new mblk which will contain a new capability
2148 	 * request to enable the capabilities.
2149 	 */
2150 
2151 	nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) +
2152 	    sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ);
2153 	if (nmp == NULL)
2154 		return (NULL);
2155 
2156 	ptr = nmp->b_rptr;
2157 
2158 	/* initialize dl_capability_req_t */
2159 	ocap = (dl_capability_req_t *)ptr;
2160 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2161 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2162 	ptr += sizeof (dl_capability_req_t);
2163 
2164 	/* initialize dl_capability_sub_t */
2165 	bcopy(isub, ptr, sizeof (*isub));
2166 	ptr += sizeof (*isub);
2167 
2168 	/* initialize dl_capab_ipsec_t */
2169 	ocip = (dl_capab_ipsec_t *)ptr;
2170 	bcopy(icip, ocip, sizeof (*icip));
2171 
2172 	nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]);
2173 	return (nmp);
2174 }
2175 
2176 /*
2177  * Process an IPsec capability negotiation ack received from a DLS Provider.
2178  * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or
2179  * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message.
2180  */
2181 static void
2182 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2183 {
2184 	dl_capab_ipsec_t	*icip;
2185 	dl_capab_ipsec_alg_t	*ialg;	/* ptr to input alg spec. */
2186 	dl_capab_ipsec_alg_t	*oalg;	/* ptr to output alg spec. */
2187 	uint_t cipher, nciphers;
2188 	mblk_t *nmp;
2189 	uint_t alg_len;
2190 	boolean_t need_sadb_dump;
2191 	uint_t sub_dl_cap = isub->dl_cap;
2192 	ill_ipsec_capab_t **ill_capab;
2193 	uint64_t ill_capab_flag;
2194 	uint8_t *capend, *ciphend;
2195 	boolean_t sadb_resync;
2196 
2197 	ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH ||
2198 	    sub_dl_cap == DL_CAPAB_IPSEC_ESP);
2199 
2200 	if (sub_dl_cap == DL_CAPAB_IPSEC_AH) {
2201 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah;
2202 		ill_capab_flag = ILL_CAPAB_AH;
2203 	} else {
2204 		ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp;
2205 		ill_capab_flag = ILL_CAPAB_ESP;
2206 	}
2207 
2208 	/*
2209 	 * If the ill capability structure exists, then this incoming
2210 	 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle.
2211 	 * If this is so, then we'd need to resynchronize the SADB
2212 	 * after re-enabling the offloaded ciphers.
2213 	 */
2214 	sadb_resync = (*ill_capab != NULL);
2215 
2216 	/*
2217 	 * Note: range checks here are not absolutely sufficient to
2218 	 * make us robust against malformed messages sent by drivers;
2219 	 * this is in keeping with the rest of IP's dlpi handling.
2220 	 * (Remember, it's coming from something else in the kernel
2221 	 * address space)
2222 	 */
2223 
2224 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2225 	if (capend > mp->b_wptr) {
2226 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2227 		    "malformed sub-capability too long for mblk");
2228 		return;
2229 	}
2230 
2231 	/*
2232 	 * There are two types of acks we process here:
2233 	 * 1. acks in reply to a (first form) generic capability req
2234 	 *    (no ENABLE flag set)
2235 	 * 2. acks in reply to a ENABLE capability req.
2236 	 *    (ENABLE flag set)
2237 	 *
2238 	 * We process the subcapability passed as argument as follows:
2239 	 * 1 do initializations
2240 	 *   1.1 initialize nmp = NULL
2241 	 *   1.2 set need_sadb_dump to B_FALSE
2242 	 * 2 for each cipher in subcapability:
2243 	 *   2.1 if ENABLE flag is set:
2244 	 *	2.1.1 update per-ill ipsec capabilities info
2245 	 *	2.1.2 set need_sadb_dump to B_TRUE
2246 	 *   2.2 if ENABLE flag is not set:
2247 	 *	2.2.1 if nmp is NULL:
2248 	 *		2.2.1.1 allocate and initialize nmp
2249 	 *		2.2.1.2 init current pos in nmp
2250 	 *	2.2.2 copy current cipher to current pos in nmp
2251 	 *	2.2.3 set ENABLE flag in nmp
2252 	 *	2.2.4 update current pos
2253 	 * 3 if nmp is not equal to NULL, send enable request
2254 	 *   3.1 send capability request
2255 	 * 4 if need_sadb_dump is B_TRUE
2256 	 *   4.1 enable promiscuous on/off notifications
2257 	 *   4.2 call ill_dlpi_send(isub->dlcap) to send all
2258 	 *	AH or ESP SA's to interface.
2259 	 */
2260 
2261 	nmp = NULL;
2262 	oalg = NULL;
2263 	need_sadb_dump = B_FALSE;
2264 	icip = (dl_capab_ipsec_t *)(isub + 1);
2265 	ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]);
2266 
2267 	nciphers = icip->cip_nciphers;
2268 	ciphend = (uint8_t *)(ialg + icip->cip_nciphers);
2269 
2270 	if (ciphend > capend) {
2271 		cmn_err(CE_WARN, "ill_capability_ipsec_ack: "
2272 		    "too many ciphers for sub-capability len");
2273 		return;
2274 	}
2275 
2276 	for (cipher = 0; cipher < nciphers; cipher++) {
2277 		alg_len = sizeof (dl_capab_ipsec_alg_t);
2278 
2279 		if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) {
2280 			/*
2281 			 * TBD: when we provide a way to disable capabilities
2282 			 * from above, need to manage the request-pending state
2283 			 * and fail if we were not expecting this ACK.
2284 			 */
2285 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2286 			    ("ill_capability_ipsec_ack: got ENABLE ACK\n"));
2287 
2288 			/*
2289 			 * Update IPsec capabilities for this ill
2290 			 */
2291 
2292 			if (*ill_capab == NULL) {
2293 				IPSECHW_DEBUG(IPSECHW_CAPAB,
2294 				    ("ill_capability_ipsec_ack: "
2295 					"allocating ipsec_capab for ill\n"));
2296 				*ill_capab = ill_ipsec_capab_alloc();
2297 
2298 				if (*ill_capab == NULL) {
2299 					cmn_err(CE_WARN,
2300 					    "ill_capability_ipsec_ack: "
2301 					    "could not enable IPsec Hardware "
2302 					    "acceleration for %s (ENOMEM)\n",
2303 					    ill->ill_name);
2304 					return;
2305 				}
2306 			}
2307 
2308 			ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH ||
2309 			    ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR);
2310 
2311 			if (ialg->alg_prim >= MAX_IPSEC_ALGS) {
2312 				cmn_err(CE_WARN,
2313 				    "ill_capability_ipsec_ack: "
2314 				    "malformed IPsec algorithm id %d",
2315 				    ialg->alg_prim);
2316 				continue;
2317 			}
2318 
2319 			if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) {
2320 				IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs,
2321 				    ialg->alg_prim);
2322 			} else {
2323 				ipsec_capab_algparm_t *alp;
2324 
2325 				IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs,
2326 				    ialg->alg_prim);
2327 				if (!ill_ipsec_capab_resize_algparm(*ill_capab,
2328 				    ialg->alg_prim)) {
2329 					cmn_err(CE_WARN,
2330 					    "ill_capability_ipsec_ack: "
2331 					    "no space for IPsec alg id %d",
2332 					    ialg->alg_prim);
2333 					continue;
2334 				}
2335 				alp = &((*ill_capab)->encr_algparm[
2336 						ialg->alg_prim]);
2337 				alp->minkeylen = ialg->alg_minbits;
2338 				alp->maxkeylen = ialg->alg_maxbits;
2339 			}
2340 			ill->ill_capabilities |= ill_capab_flag;
2341 			/*
2342 			 * indicate that a capability was enabled, which
2343 			 * will be used below to kick off a SADB dump
2344 			 * to the ill.
2345 			 */
2346 			need_sadb_dump = B_TRUE;
2347 		} else {
2348 			IPSECHW_DEBUG(IPSECHW_CAPAB,
2349 			    ("ill_capability_ipsec_ack: enabling alg 0x%x\n",
2350 				ialg->alg_prim));
2351 
2352 			if (nmp == NULL) {
2353 				nmp = ill_alloc_ipsec_cap_req(ill, isub);
2354 				if (nmp == NULL) {
2355 					/*
2356 					 * Sending the PROMISC_ON/OFF
2357 					 * notification request failed.
2358 					 * We cannot enable the algorithms
2359 					 * since the Provider will not
2360 					 * notify IP of promiscous mode
2361 					 * changes, which could lead
2362 					 * to leakage of packets.
2363 					 */
2364 					cmn_err(CE_WARN,
2365 					    "ill_capability_ipsec_ack: "
2366 					    "could not enable IPsec Hardware "
2367 					    "acceleration for %s (ENOMEM)\n",
2368 					    ill->ill_name);
2369 					return;
2370 				}
2371 				/* ptr to current output alg specifier */
2372 				oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2373 			}
2374 
2375 			/*
2376 			 * Copy current alg specifier, set ENABLE
2377 			 * flag, and advance to next output alg.
2378 			 * For now we enable all IPsec capabilities.
2379 			 */
2380 			ASSERT(oalg != NULL);
2381 			bcopy(ialg, oalg, alg_len);
2382 			oalg->alg_flag |= DL_CAPAB_ALG_ENABLE;
2383 			nmp->b_wptr += alg_len;
2384 			oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr;
2385 		}
2386 
2387 		/* move to next input algorithm specifier */
2388 		ialg = (dl_capab_ipsec_alg_t *)
2389 		    ((char *)ialg + alg_len);
2390 	}
2391 
2392 	if (nmp != NULL)
2393 		/*
2394 		 * nmp points to a DL_CAPABILITY_REQ message to enable
2395 		 * IPsec hardware acceleration.
2396 		 */
2397 		ill_dlpi_send(ill, nmp);
2398 
2399 	if (need_sadb_dump)
2400 		/*
2401 		 * An acknowledgement corresponding to a request to
2402 		 * enable acceleration was received, notify SADB.
2403 		 */
2404 		ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync);
2405 }
2406 
2407 /*
2408  * Given an mblk with enough space in it, create sub-capability entries for
2409  * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised
2410  * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared,
2411  * in preparation for the reset the DL_CAPABILITY_REQ message.
2412  */
2413 static void
2414 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen,
2415     ill_ipsec_capab_t *ill_cap, mblk_t *mp)
2416 {
2417 	dl_capab_ipsec_t *oipsec;
2418 	dl_capab_ipsec_alg_t *oalg;
2419 	dl_capability_sub_t *dl_subcap;
2420 	int i, k;
2421 
2422 	ASSERT(nciphers > 0);
2423 	ASSERT(ill_cap != NULL);
2424 	ASSERT(mp != NULL);
2425 	ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen);
2426 
2427 	/* dl_capability_sub_t for "stype" */
2428 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
2429 	dl_subcap->dl_cap = stype;
2430 	dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen;
2431 	mp->b_wptr += sizeof (dl_capability_sub_t);
2432 
2433 	/* dl_capab_ipsec_t for "stype" */
2434 	oipsec = (dl_capab_ipsec_t *)mp->b_wptr;
2435 	oipsec->cip_version = 1;
2436 	oipsec->cip_nciphers = nciphers;
2437 	mp->b_wptr = (uchar_t *)&oipsec->cip_data[0];
2438 
2439 	/* create entries for "stype" AUTH ciphers */
2440 	for (i = 0; i < ill_cap->algs_size; i++) {
2441 		for (k = 0; k < BITSPERBYTE; k++) {
2442 			if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0)
2443 				continue;
2444 
2445 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2446 			bzero((void *)oalg, sizeof (*oalg));
2447 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH;
2448 			oalg->alg_prim = k + (BITSPERBYTE * i);
2449 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2450 		}
2451 	}
2452 	/* create entries for "stype" ENCR ciphers */
2453 	for (i = 0; i < ill_cap->algs_size; i++) {
2454 		for (k = 0; k < BITSPERBYTE; k++) {
2455 			if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0)
2456 				continue;
2457 
2458 			oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr;
2459 			bzero((void *)oalg, sizeof (*oalg));
2460 			oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR;
2461 			oalg->alg_prim = k + (BITSPERBYTE * i);
2462 			mp->b_wptr += sizeof (dl_capab_ipsec_alg_t);
2463 		}
2464 	}
2465 }
2466 
2467 /*
2468  * Macro to count number of 1s in a byte (8-bit word).  The total count is
2469  * accumulated into the passed-in argument (sum).  We could use SPARCv9's
2470  * POPC instruction, but our macro is more flexible for an arbitrary length
2471  * of bytes, such as {auth,encr}_hw_algs.  These variables are currently
2472  * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length
2473  * stays that way, we can reduce the number of iterations required.
2474  */
2475 #define	COUNT_1S(val, sum) {					\
2476 	uint8_t x = val & 0xff;					\
2477 	x = (x & 0x55) + ((x >> 1) & 0x55);			\
2478 	x = (x & 0x33) + ((x >> 2) & 0x33);			\
2479 	sum += (x & 0xf) + ((x >> 4) & 0xf);			\
2480 }
2481 
2482 /* ARGSUSED */
2483 static void
2484 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp)
2485 {
2486 	mblk_t *mp;
2487 	ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah;
2488 	ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp;
2489 	uint64_t ill_capabilities = ill->ill_capabilities;
2490 	int ah_cnt = 0, esp_cnt = 0;
2491 	int ah_len = 0, esp_len = 0;
2492 	int i, size = 0;
2493 
2494 	if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)))
2495 		return;
2496 
2497 	ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH));
2498 	ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP));
2499 
2500 	/* Find out the number of ciphers for AH */
2501 	if (cap_ah != NULL) {
2502 		for (i = 0; i < cap_ah->algs_size; i++) {
2503 			COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt);
2504 			COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt);
2505 		}
2506 		if (ah_cnt > 0) {
2507 			size += sizeof (dl_capability_sub_t) +
2508 			    sizeof (dl_capab_ipsec_t);
2509 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2510 			ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2511 			size += ah_len;
2512 		}
2513 	}
2514 
2515 	/* Find out the number of ciphers for ESP */
2516 	if (cap_esp != NULL) {
2517 		for (i = 0; i < cap_esp->algs_size; i++) {
2518 			COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt);
2519 			COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt);
2520 		}
2521 		if (esp_cnt > 0) {
2522 			size += sizeof (dl_capability_sub_t) +
2523 			    sizeof (dl_capab_ipsec_t);
2524 			/* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */
2525 			esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t);
2526 			size += esp_len;
2527 		}
2528 	}
2529 
2530 	if (size == 0) {
2531 		ip1dbg(("ill_capability_ipsec_reset: capabilities exist but "
2532 		    "there's nothing to reset\n"));
2533 		return;
2534 	}
2535 
2536 	mp = allocb(size, BPRI_HI);
2537 	if (mp == NULL) {
2538 		ip1dbg(("ill_capability_ipsec_reset: unable to allocate "
2539 		    "request to disable IPSEC Hardware Acceleration\n"));
2540 		return;
2541 	}
2542 
2543 	/*
2544 	 * Clear the capability flags for IPSec HA but retain the ill
2545 	 * capability structures since it's possible that another thread
2546 	 * is still referring to them.  The structures only get deallocated
2547 	 * when we destroy the ill.
2548 	 *
2549 	 * Various places check the flags to see if the ill is capable of
2550 	 * hardware acceleration, and by clearing them we ensure that new
2551 	 * outbound IPSec packets are sent down encrypted.
2552 	 */
2553 	ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP);
2554 
2555 	/* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */
2556 	if (ah_cnt > 0) {
2557 		ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len,
2558 		    cap_ah, mp);
2559 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2560 	}
2561 
2562 	/* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */
2563 	if (esp_cnt > 0) {
2564 		ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len,
2565 		    cap_esp, mp);
2566 		ASSERT(mp->b_rptr + size >= mp->b_wptr);
2567 	}
2568 
2569 	/*
2570 	 * At this point we've composed a bunch of sub-capabilities to be
2571 	 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream
2572 	 * by the caller.  Upon receiving this reset message, the driver
2573 	 * must stop inbound decryption (by destroying all inbound SAs)
2574 	 * and let the corresponding packets come in encrypted.
2575 	 */
2576 
2577 	if (*sc_mp != NULL)
2578 		linkb(*sc_mp, mp);
2579 	else
2580 		*sc_mp = mp;
2581 }
2582 
2583 static void
2584 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp,
2585     boolean_t encapsulated)
2586 {
2587 	boolean_t legacy = B_FALSE;
2588 
2589 	/*
2590 	 * If this DL_CAPABILITY_ACK came in as a response to our "reset"
2591 	 * DL_CAPABILITY_REQ, ignore it during this cycle.  We've just
2592 	 * instructed the driver to disable its advertised capabilities,
2593 	 * so there's no point in accepting any response at this moment.
2594 	 */
2595 	if (ill->ill_capab_state == IDMS_UNKNOWN)
2596 		return;
2597 
2598 	/*
2599 	 * Note that only the following two sub-capabilities may be
2600 	 * considered as "legacy", since their original definitions
2601 	 * do not incorporate the dl_mid_t module ID token, and hence
2602 	 * may require the use of the wrapper sub-capability.
2603 	 */
2604 	switch (subp->dl_cap) {
2605 	case DL_CAPAB_IPSEC_AH:
2606 	case DL_CAPAB_IPSEC_ESP:
2607 		legacy = B_TRUE;
2608 		break;
2609 	}
2610 
2611 	/*
2612 	 * For legacy sub-capabilities which don't incorporate a queue_t
2613 	 * pointer in their structures, discard them if we detect that
2614 	 * there are intermediate modules in between IP and the driver.
2615 	 */
2616 	if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) {
2617 		ip1dbg(("ill_capability_dispatch: unencapsulated capab type "
2618 		    "%d discarded; %d module(s) present below IP\n",
2619 		    subp->dl_cap, ill->ill_lmod_cnt));
2620 		return;
2621 	}
2622 
2623 	switch (subp->dl_cap) {
2624 	case DL_CAPAB_IPSEC_AH:
2625 	case DL_CAPAB_IPSEC_ESP:
2626 		ill_capability_ipsec_ack(ill, mp, subp);
2627 		break;
2628 	case DL_CAPAB_MDT:
2629 		ill_capability_mdt_ack(ill, mp, subp);
2630 		break;
2631 	case DL_CAPAB_HCKSUM:
2632 		ill_capability_hcksum_ack(ill, mp, subp);
2633 		break;
2634 	case DL_CAPAB_ZEROCOPY:
2635 		ill_capability_zerocopy_ack(ill, mp, subp);
2636 		break;
2637 	case DL_CAPAB_POLL:
2638 		if (!SOFT_RINGS_ENABLED())
2639 			ill_capability_dls_ack(ill, mp, subp);
2640 		break;
2641 	case DL_CAPAB_SOFT_RING:
2642 		if (SOFT_RINGS_ENABLED())
2643 			ill_capability_dls_ack(ill, mp, subp);
2644 		break;
2645 	default:
2646 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
2647 		    subp->dl_cap));
2648 	}
2649 }
2650 
2651 /*
2652  * As part of negotiating polling capability, the driver tells us
2653  * the default (or normal) blanking interval and packet threshold
2654  * (the receive timer fires if blanking interval is reached or
2655  * the packet threshold is reached).
2656  *
2657  * As part of manipulating the polling interval, we always use our
2658  * estimated interval (avg service time * number of packets queued
2659  * on the squeue) but we try to blank for a minimum of
2660  * rr_normal_blank_time * rr_max_blank_ratio. We disable the
2661  * packet threshold during this time. When we are not in polling mode
2662  * we set the blank interval typically lower, rr_normal_pkt_cnt *
2663  * rr_min_blank_ratio but up the packet cnt by a ratio of
2664  * rr_min_pkt_cnt_ratio so that we are still getting chains if
2665  * possible although for a shorter interval.
2666  */
2667 #define	RR_MAX_BLANK_RATIO	20
2668 #define	RR_MIN_BLANK_RATIO	10
2669 #define	RR_MAX_PKT_CNT_RATIO	3
2670 #define	RR_MIN_PKT_CNT_RATIO	3
2671 
2672 /*
2673  * These can be tuned via /etc/system.
2674  */
2675 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO;
2676 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO;
2677 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO;
2678 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO;
2679 
2680 static mac_resource_handle_t
2681 ill_ring_add(void *arg, mac_resource_t *mrp)
2682 {
2683 	ill_t			*ill = (ill_t *)arg;
2684 	mac_rx_fifo_t		*mrfp = (mac_rx_fifo_t *)mrp;
2685 	ill_rx_ring_t		*rx_ring;
2686 	int			ip_rx_index;
2687 
2688 	ASSERT(mrp != NULL);
2689 	if (mrp->mr_type != MAC_RX_FIFO) {
2690 		return (NULL);
2691 	}
2692 	ASSERT(ill != NULL);
2693 	ASSERT(ill->ill_dls_capab != NULL);
2694 
2695 	mutex_enter(&ill->ill_lock);
2696 	for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) {
2697 		rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index];
2698 		ASSERT(rx_ring != NULL);
2699 
2700 		if (rx_ring->rr_ring_state == ILL_RING_FREE) {
2701 			time_t normal_blank_time =
2702 			    mrfp->mrf_normal_blank_time;
2703 			uint_t normal_pkt_cnt =
2704 			    mrfp->mrf_normal_pkt_count;
2705 
2706 			bzero(rx_ring, sizeof (ill_rx_ring_t));
2707 
2708 			rx_ring->rr_blank = mrfp->mrf_blank;
2709 			rx_ring->rr_handle = mrfp->mrf_arg;
2710 			rx_ring->rr_ill = ill;
2711 			rx_ring->rr_normal_blank_time = normal_blank_time;
2712 			rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt;
2713 
2714 			rx_ring->rr_max_blank_time =
2715 			    normal_blank_time * rr_max_blank_ratio;
2716 			rx_ring->rr_min_blank_time =
2717 			    normal_blank_time * rr_min_blank_ratio;
2718 			rx_ring->rr_max_pkt_cnt =
2719 			    normal_pkt_cnt * rr_max_pkt_cnt_ratio;
2720 			rx_ring->rr_min_pkt_cnt =
2721 			    normal_pkt_cnt * rr_min_pkt_cnt_ratio;
2722 
2723 			rx_ring->rr_ring_state = ILL_RING_INUSE;
2724 			mutex_exit(&ill->ill_lock);
2725 
2726 			DTRACE_PROBE2(ill__ring__add, (void *), ill,
2727 			    (int), ip_rx_index);
2728 			return ((mac_resource_handle_t)rx_ring);
2729 		}
2730 	}
2731 
2732 	/*
2733 	 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If
2734 	 * we have devices which can overwhelm this limit, ILL_MAX_RING
2735 	 * should be made configurable. Meanwhile it cause no panic because
2736 	 * driver will pass ip_input a NULL handle which will make
2737 	 * IP allocate the default squeue and Polling mode will not
2738 	 * be used for this ring.
2739 	 */
2740 	cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) "
2741 	    "for %s\n", ILL_MAX_RINGS, ill->ill_name);
2742 
2743 	mutex_exit(&ill->ill_lock);
2744 	return (NULL);
2745 }
2746 
2747 static boolean_t
2748 ill_capability_dls_init(ill_t *ill)
2749 {
2750 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2751 	conn_t 			*connp;
2752 	size_t			sz;
2753 
2754 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) {
2755 		if (ill_dls == NULL) {
2756 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2757 			    "soft_ring enabled for ill=%s (%p) but data "
2758 			    "structs uninitialized\n", ill->ill_name,
2759 			    (void *)ill);
2760 		}
2761 		return (B_TRUE);
2762 	} else if (ill->ill_capabilities & ILL_CAPAB_POLL) {
2763 		if (ill_dls == NULL) {
2764 			cmn_err(CE_PANIC, "ill_capability_dls_init: "
2765 			    "polling enabled for ill=%s (%p) but data "
2766 			    "structs uninitialized\n", ill->ill_name,
2767 			(void *)ill);
2768 		}
2769 		return (B_TRUE);
2770 	}
2771 
2772 	if (ill_dls != NULL) {
2773 		ill_rx_ring_t 	*rx_ring = ill_dls->ill_ring_tbl;
2774 		/* Soft_Ring or polling is being re-enabled */
2775 
2776 		connp = ill_dls->ill_unbind_conn;
2777 		ASSERT(rx_ring != NULL);
2778 		bzero((void *)ill_dls, sizeof (ill_dls_capab_t));
2779 		bzero((void *)rx_ring,
2780 		    sizeof (ill_rx_ring_t) * ILL_MAX_RINGS);
2781 		ill_dls->ill_ring_tbl = rx_ring;
2782 		ill_dls->ill_unbind_conn = connp;
2783 		return (B_TRUE);
2784 	}
2785 
2786 	if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL)
2787 		return (B_FALSE);
2788 
2789 	sz = sizeof (ill_dls_capab_t);
2790 	sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS;
2791 
2792 	ill_dls = kmem_zalloc(sz, KM_NOSLEEP);
2793 	if (ill_dls == NULL) {
2794 		cmn_err(CE_WARN, "ill_capability_dls_init: could not "
2795 		    "allocate dls_capab for %s (%p)\n", ill->ill_name,
2796 		    (void *)ill);
2797 		CONN_DEC_REF(connp);
2798 		return (B_FALSE);
2799 	}
2800 
2801 	/* Allocate space to hold ring table */
2802 	ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1];
2803 	ill->ill_dls_capab = ill_dls;
2804 	ill_dls->ill_unbind_conn = connp;
2805 	return (B_TRUE);
2806 }
2807 
2808 /*
2809  * ill_capability_dls_disable: disable soft_ring and/or polling
2810  * capability. Since any of the rings might already be in use, need
2811  * to call ipsq_clean_all() which gets behind the squeue to disable
2812  * direct calls if necessary.
2813  */
2814 static void
2815 ill_capability_dls_disable(ill_t *ill)
2816 {
2817 	ill_dls_capab_t	*ill_dls = ill->ill_dls_capab;
2818 
2819 	if (ill->ill_capabilities & ILL_CAPAB_DLS) {
2820 		ipsq_clean_all(ill);
2821 		ill_dls->ill_tx = NULL;
2822 		ill_dls->ill_tx_handle = NULL;
2823 		ill_dls->ill_dls_change_status = NULL;
2824 		ill_dls->ill_dls_bind = NULL;
2825 		ill_dls->ill_dls_unbind = NULL;
2826 	}
2827 
2828 	ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS));
2829 }
2830 
2831 static void
2832 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls,
2833     dl_capability_sub_t *isub)
2834 {
2835 	uint_t			size;
2836 	uchar_t			*rptr;
2837 	dl_capab_dls_t	dls, *odls;
2838 	ill_dls_capab_t	*ill_dls;
2839 	mblk_t			*nmp = NULL;
2840 	dl_capability_req_t	*ocap;
2841 	uint_t			sub_dl_cap = isub->dl_cap;
2842 
2843 	if (!ill_capability_dls_init(ill))
2844 		return;
2845 	ill_dls = ill->ill_dls_capab;
2846 
2847 	/* Copy locally to get the members aligned */
2848 	bcopy((void *)idls, (void *)&dls,
2849 	    sizeof (dl_capab_dls_t));
2850 
2851 	/* Get the tx function and handle from dld */
2852 	ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx;
2853 	ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle;
2854 
2855 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2856 		ill_dls->ill_dls_change_status =
2857 		    (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status;
2858 		ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind;
2859 		ill_dls->ill_dls_unbind =
2860 		    (ip_dls_unbind_t)dls.dls_ring_unbind;
2861 		ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt;
2862 	}
2863 
2864 	size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) +
2865 	    isub->dl_length;
2866 
2867 	if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
2868 		cmn_err(CE_WARN, "ill_capability_dls_capable: could "
2869 		    "not allocate memory for CAPAB_REQ for %s (%p)\n",
2870 		    ill->ill_name, (void *)ill);
2871 		return;
2872 	}
2873 
2874 	/* initialize dl_capability_req_t */
2875 	rptr = nmp->b_rptr;
2876 	ocap = (dl_capability_req_t *)rptr;
2877 	ocap->dl_sub_offset = sizeof (dl_capability_req_t);
2878 	ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length;
2879 	rptr += sizeof (dl_capability_req_t);
2880 
2881 	/* initialize dl_capability_sub_t */
2882 	bcopy(isub, rptr, sizeof (*isub));
2883 	rptr += sizeof (*isub);
2884 
2885 	odls = (dl_capab_dls_t *)rptr;
2886 	rptr += sizeof (dl_capab_dls_t);
2887 
2888 	/* initialize dl_capab_dls_t to be sent down */
2889 	dls.dls_rx_handle = (uintptr_t)ill;
2890 	dls.dls_rx = (uintptr_t)ip_input;
2891 	dls.dls_ring_add = (uintptr_t)ill_ring_add;
2892 
2893 	if (sub_dl_cap == DL_CAPAB_SOFT_RING) {
2894 		dls.dls_ring_cnt = ip_soft_rings_cnt;
2895 		dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment;
2896 		dls.dls_flags = SOFT_RING_ENABLE;
2897 	} else {
2898 		dls.dls_flags = POLL_ENABLE;
2899 		ip1dbg(("ill_capability_dls_capable: asking interface %s "
2900 		    "to enable polling\n", ill->ill_name));
2901 	}
2902 	bcopy((void *)&dls, (void *)odls,
2903 	    sizeof (dl_capab_dls_t));
2904 	ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
2905 	/*
2906 	 * nmp points to a DL_CAPABILITY_REQ message to
2907 	 * enable either soft_ring or polling
2908 	 */
2909 	ill_dlpi_send(ill, nmp);
2910 }
2911 
2912 static void
2913 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp)
2914 {
2915 	mblk_t *mp;
2916 	dl_capab_dls_t *idls;
2917 	dl_capability_sub_t *dl_subcap;
2918 	int size;
2919 
2920 	if (!(ill->ill_capabilities & ILL_CAPAB_DLS))
2921 		return;
2922 
2923 	ASSERT(ill->ill_dls_capab != NULL);
2924 
2925 	size = sizeof (*dl_subcap) + sizeof (*idls);
2926 
2927 	mp = allocb(size, BPRI_HI);
2928 	if (mp == NULL) {
2929 		ip1dbg(("ill_capability_dls_reset: unable to allocate "
2930 		    "request to disable soft_ring\n"));
2931 		return;
2932 	}
2933 
2934 	mp->b_wptr = mp->b_rptr + size;
2935 
2936 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
2937 	dl_subcap->dl_length = sizeof (*idls);
2938 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2939 		dl_subcap->dl_cap = DL_CAPAB_SOFT_RING;
2940 	else
2941 		dl_subcap->dl_cap = DL_CAPAB_POLL;
2942 
2943 	idls = (dl_capab_dls_t *)(dl_subcap + 1);
2944 	if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING)
2945 		idls->dls_flags = SOFT_RING_DISABLE;
2946 	else
2947 		idls->dls_flags = POLL_DISABLE;
2948 
2949 	if (*sc_mp != NULL)
2950 		linkb(*sc_mp, mp);
2951 	else
2952 		*sc_mp = mp;
2953 }
2954 
2955 /*
2956  * Process a soft_ring/poll capability negotiation ack received
2957  * from a DLS Provider.isub must point to the sub-capability
2958  * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message.
2959  */
2960 static void
2961 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
2962 {
2963 	dl_capab_dls_t		*idls;
2964 	uint_t			sub_dl_cap = isub->dl_cap;
2965 	uint8_t			*capend;
2966 
2967 	ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING ||
2968 	    sub_dl_cap == DL_CAPAB_POLL);
2969 
2970 	if (ill->ill_isv6)
2971 		return;
2972 
2973 	/*
2974 	 * Note: range checks here are not absolutely sufficient to
2975 	 * make us robust against malformed messages sent by drivers;
2976 	 * this is in keeping with the rest of IP's dlpi handling.
2977 	 * (Remember, it's coming from something else in the kernel
2978 	 * address space)
2979 	 */
2980 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
2981 	if (capend > mp->b_wptr) {
2982 		cmn_err(CE_WARN, "ill_capability_dls_ack: "
2983 		    "malformed sub-capability too long for mblk");
2984 		return;
2985 	}
2986 
2987 	/*
2988 	 * There are two types of acks we process here:
2989 	 * 1. acks in reply to a (first form) generic capability req
2990 	 *    (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE)
2991 	 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE
2992 	 *    capability req.
2993 	 */
2994 	idls = (dl_capab_dls_t *)(isub + 1);
2995 
2996 	if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) {
2997 		ip1dbg(("ill_capability_dls_ack: mid token for dls "
2998 		    "capability isn't as expected; pass-thru "
2999 		    "module(s) detected, discarding capability\n"));
3000 		if (ill->ill_capabilities & ILL_CAPAB_DLS) {
3001 			/*
3002 			 * This is a capability renegotitation case.
3003 			 * The interface better be unusable at this
3004 			 * point other wise bad things will happen
3005 			 * if we disable direct calls on a running
3006 			 * and up interface.
3007 			 */
3008 			ill_capability_dls_disable(ill);
3009 		}
3010 		return;
3011 	}
3012 
3013 	switch (idls->dls_flags) {
3014 	default:
3015 		/* Disable if unknown flag */
3016 	case SOFT_RING_DISABLE:
3017 	case POLL_DISABLE:
3018 		ill_capability_dls_disable(ill);
3019 		break;
3020 	case SOFT_RING_CAPABLE:
3021 	case POLL_CAPABLE:
3022 		/*
3023 		 * If the capability was already enabled, its safe
3024 		 * to disable it first to get rid of stale information
3025 		 * and then start enabling it again.
3026 		 */
3027 		ill_capability_dls_disable(ill);
3028 		ill_capability_dls_capable(ill, idls, isub);
3029 		break;
3030 	case SOFT_RING_ENABLE:
3031 	case POLL_ENABLE:
3032 		mutex_enter(&ill->ill_lock);
3033 		if (sub_dl_cap == DL_CAPAB_SOFT_RING &&
3034 		    !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) {
3035 			ASSERT(ill->ill_dls_capab != NULL);
3036 			ill->ill_capabilities |= ILL_CAPAB_SOFT_RING;
3037 		}
3038 		if (sub_dl_cap == DL_CAPAB_POLL &&
3039 		    !(ill->ill_capabilities & ILL_CAPAB_POLL)) {
3040 			ASSERT(ill->ill_dls_capab != NULL);
3041 			ill->ill_capabilities |= ILL_CAPAB_POLL;
3042 			ip1dbg(("ill_capability_dls_ack: interface %s "
3043 			    "has enabled polling\n", ill->ill_name));
3044 		}
3045 		mutex_exit(&ill->ill_lock);
3046 		break;
3047 	}
3048 }
3049 
3050 /*
3051  * Process a hardware checksum offload capability negotiation ack received
3052  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
3053  * of a DL_CAPABILITY_ACK message.
3054  */
3055 static void
3056 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3057 {
3058 	dl_capability_req_t	*ocap;
3059 	dl_capab_hcksum_t	*ihck, *ohck;
3060 	ill_hcksum_capab_t	**ill_hcksum;
3061 	mblk_t			*nmp = NULL;
3062 	uint_t			sub_dl_cap = isub->dl_cap;
3063 	uint8_t			*capend;
3064 
3065 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
3066 
3067 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
3068 
3069 	/*
3070 	 * Note: range checks here are not absolutely sufficient to
3071 	 * make us robust against malformed messages sent by drivers;
3072 	 * this is in keeping with the rest of IP's dlpi handling.
3073 	 * (Remember, it's coming from something else in the kernel
3074 	 * address space)
3075 	 */
3076 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3077 	if (capend > mp->b_wptr) {
3078 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3079 		    "malformed sub-capability too long for mblk");
3080 		return;
3081 	}
3082 
3083 	/*
3084 	 * There are two types of acks we process here:
3085 	 * 1. acks in reply to a (first form) generic capability req
3086 	 *    (no ENABLE flag set)
3087 	 * 2. acks in reply to a ENABLE capability req.
3088 	 *    (ENABLE flag set)
3089 	 */
3090 	ihck = (dl_capab_hcksum_t *)(isub + 1);
3091 
3092 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
3093 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
3094 		    "unsupported hardware checksum "
3095 		    "sub-capability (version %d, expected %d)",
3096 		    ihck->hcksum_version, HCKSUM_VERSION_1);
3097 		return;
3098 	}
3099 
3100 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
3101 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
3102 		    "checksum capability isn't as expected; pass-thru "
3103 		    "module(s) detected, discarding capability\n"));
3104 		return;
3105 	}
3106 
3107 #define	CURR_HCKSUM_CAPAB				\
3108 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
3109 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
3110 
3111 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
3112 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
3113 		/* do ENABLE processing */
3114 		if (*ill_hcksum == NULL) {
3115 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
3116 			    KM_NOSLEEP);
3117 
3118 			if (*ill_hcksum == NULL) {
3119 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3120 				    "could not enable hcksum version %d "
3121 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
3122 				    ill->ill_name);
3123 				return;
3124 			}
3125 		}
3126 
3127 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
3128 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
3129 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
3130 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
3131 		    "has enabled hardware checksumming\n ",
3132 		    ill->ill_name));
3133 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
3134 		/*
3135 		 * Enabling hardware checksum offload
3136 		 * Currently IP supports {TCP,UDP}/IPv4
3137 		 * partial and full cksum offload and
3138 		 * IPv4 header checksum offload.
3139 		 * Allocate new mblk which will
3140 		 * contain a new capability request
3141 		 * to enable hardware checksum offload.
3142 		 */
3143 		uint_t	size;
3144 		uchar_t	*rptr;
3145 
3146 		size = sizeof (dl_capability_req_t) +
3147 		    sizeof (dl_capability_sub_t) + isub->dl_length;
3148 
3149 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3150 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
3151 			    "could not enable hardware cksum for %s (ENOMEM)\n",
3152 			    ill->ill_name);
3153 			return;
3154 		}
3155 
3156 		rptr = nmp->b_rptr;
3157 		/* initialize dl_capability_req_t */
3158 		ocap = (dl_capability_req_t *)nmp->b_rptr;
3159 		ocap->dl_sub_offset =
3160 		    sizeof (dl_capability_req_t);
3161 		ocap->dl_sub_length =
3162 		    sizeof (dl_capability_sub_t) +
3163 		    isub->dl_length;
3164 		nmp->b_rptr += sizeof (dl_capability_req_t);
3165 
3166 		/* initialize dl_capability_sub_t */
3167 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
3168 		nmp->b_rptr += sizeof (*isub);
3169 
3170 		/* initialize dl_capab_hcksum_t */
3171 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
3172 		bcopy(ihck, ohck, sizeof (*ihck));
3173 
3174 		nmp->b_rptr = rptr;
3175 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
3176 
3177 		/* Set ENABLE flag */
3178 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
3179 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
3180 
3181 		/*
3182 		 * nmp points to a DL_CAPABILITY_REQ message to enable
3183 		 * hardware checksum acceleration.
3184 		 */
3185 		ill_dlpi_send(ill, nmp);
3186 	} else {
3187 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
3188 		    "advertised %x hardware checksum capability flags\n",
3189 		    ill->ill_name, ihck->hcksum_txflags));
3190 	}
3191 }
3192 
3193 static void
3194 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp)
3195 {
3196 	mblk_t *mp;
3197 	dl_capab_hcksum_t *hck_subcap;
3198 	dl_capability_sub_t *dl_subcap;
3199 	int size;
3200 
3201 	if (!ILL_HCKSUM_CAPABLE(ill))
3202 		return;
3203 
3204 	ASSERT(ill->ill_hcksum_capab != NULL);
3205 	/*
3206 	 * Clear the capability flag for hardware checksum offload but
3207 	 * retain the ill_hcksum_capab structure since it's possible that
3208 	 * another thread is still referring to it.  The structure only
3209 	 * gets deallocated when we destroy the ill.
3210 	 */
3211 	ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM;
3212 
3213 	size = sizeof (*dl_subcap) + sizeof (*hck_subcap);
3214 
3215 	mp = allocb(size, BPRI_HI);
3216 	if (mp == NULL) {
3217 		ip1dbg(("ill_capability_hcksum_reset: unable to allocate "
3218 		    "request to disable hardware checksum offload\n"));
3219 		return;
3220 	}
3221 
3222 	mp->b_wptr = mp->b_rptr + size;
3223 
3224 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3225 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
3226 	dl_subcap->dl_length = sizeof (*hck_subcap);
3227 
3228 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
3229 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
3230 	hck_subcap->hcksum_txflags = 0;
3231 
3232 	if (*sc_mp != NULL)
3233 		linkb(*sc_mp, mp);
3234 	else
3235 		*sc_mp = mp;
3236 }
3237 
3238 static void
3239 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
3240 {
3241 	mblk_t *nmp = NULL;
3242 	dl_capability_req_t *oc;
3243 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
3244 	ill_zerocopy_capab_t **ill_zerocopy_capab;
3245 	uint_t sub_dl_cap = isub->dl_cap;
3246 	uint8_t *capend;
3247 
3248 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
3249 
3250 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
3251 
3252 	/*
3253 	 * Note: range checks here are not absolutely sufficient to
3254 	 * make us robust against malformed messages sent by drivers;
3255 	 * this is in keeping with the rest of IP's dlpi handling.
3256 	 * (Remember, it's coming from something else in the kernel
3257 	 * address space)
3258 	 */
3259 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
3260 	if (capend > mp->b_wptr) {
3261 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3262 		    "malformed sub-capability too long for mblk");
3263 		return;
3264 	}
3265 
3266 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
3267 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
3268 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
3269 		    "unsupported ZEROCOPY sub-capability (version %d, "
3270 		    "expected %d)", zc_ic->zerocopy_version,
3271 		    ZEROCOPY_VERSION_1);
3272 		return;
3273 	}
3274 
3275 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
3276 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
3277 		    "capability isn't as expected; pass-thru module(s) "
3278 		    "detected, discarding capability\n"));
3279 		return;
3280 	}
3281 
3282 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
3283 		if (*ill_zerocopy_capab == NULL) {
3284 			*ill_zerocopy_capab =
3285 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
3286 			    KM_NOSLEEP);
3287 
3288 			if (*ill_zerocopy_capab == NULL) {
3289 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3290 				    "could not enable Zero-copy version %d "
3291 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
3292 				    ill->ill_name);
3293 				return;
3294 			}
3295 		}
3296 
3297 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
3298 		    "supports Zero-copy version %d\n", ill->ill_name,
3299 		    ZEROCOPY_VERSION_1));
3300 
3301 		(*ill_zerocopy_capab)->ill_zerocopy_version =
3302 		    zc_ic->zerocopy_version;
3303 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
3304 		    zc_ic->zerocopy_flags;
3305 
3306 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
3307 	} else {
3308 		uint_t size;
3309 		uchar_t *rptr;
3310 
3311 		size = sizeof (dl_capability_req_t) +
3312 		    sizeof (dl_capability_sub_t) +
3313 		    sizeof (dl_capab_zerocopy_t);
3314 
3315 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
3316 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
3317 			    "could not enable zerocopy for %s (ENOMEM)\n",
3318 			    ill->ill_name);
3319 			return;
3320 		}
3321 
3322 		rptr = nmp->b_rptr;
3323 		/* initialize dl_capability_req_t */
3324 		oc = (dl_capability_req_t *)rptr;
3325 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
3326 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
3327 		    sizeof (dl_capab_zerocopy_t);
3328 		rptr += sizeof (dl_capability_req_t);
3329 
3330 		/* initialize dl_capability_sub_t */
3331 		bcopy(isub, rptr, sizeof (*isub));
3332 		rptr += sizeof (*isub);
3333 
3334 		/* initialize dl_capab_zerocopy_t */
3335 		zc_oc = (dl_capab_zerocopy_t *)rptr;
3336 		*zc_oc = *zc_ic;
3337 
3338 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
3339 		    "to enable zero-copy version %d\n", ill->ill_name,
3340 		    ZEROCOPY_VERSION_1));
3341 
3342 		/* set VMSAFE_MEM flag */
3343 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
3344 
3345 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
3346 		ill_dlpi_send(ill, nmp);
3347 	}
3348 }
3349 
3350 static void
3351 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp)
3352 {
3353 	mblk_t *mp;
3354 	dl_capab_zerocopy_t *zerocopy_subcap;
3355 	dl_capability_sub_t *dl_subcap;
3356 	int size;
3357 
3358 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
3359 		return;
3360 
3361 	ASSERT(ill->ill_zerocopy_capab != NULL);
3362 	/*
3363 	 * Clear the capability flag for Zero-copy but retain the
3364 	 * ill_zerocopy_capab structure since it's possible that another
3365 	 * thread is still referring to it.  The structure only gets
3366 	 * deallocated when we destroy the ill.
3367 	 */
3368 	ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY;
3369 
3370 	size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
3371 
3372 	mp = allocb(size, BPRI_HI);
3373 	if (mp == NULL) {
3374 		ip1dbg(("ill_capability_zerocopy_reset: unable to allocate "
3375 		    "request to disable Zero-copy\n"));
3376 		return;
3377 	}
3378 
3379 	mp->b_wptr = mp->b_rptr + size;
3380 
3381 	dl_subcap = (dl_capability_sub_t *)mp->b_rptr;
3382 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
3383 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
3384 
3385 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
3386 	zerocopy_subcap->zerocopy_version =
3387 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
3388 	zerocopy_subcap->zerocopy_flags = 0;
3389 
3390 	if (*sc_mp != NULL)
3391 		linkb(*sc_mp, mp);
3392 	else
3393 		*sc_mp = mp;
3394 }
3395 
3396 /*
3397  * Consume a new-style hardware capabilities negotiation ack.
3398  * Called from ip_rput_dlpi_writer().
3399  */
3400 void
3401 ill_capability_ack(ill_t *ill, mblk_t *mp)
3402 {
3403 	dl_capability_ack_t *capp;
3404 	dl_capability_sub_t *subp, *endp;
3405 
3406 	if (ill->ill_capab_state == IDMS_INPROGRESS)
3407 		ill->ill_capab_state = IDMS_OK;
3408 
3409 	capp = (dl_capability_ack_t *)mp->b_rptr;
3410 
3411 	if (capp->dl_sub_length == 0)
3412 		/* no new-style capabilities */
3413 		return;
3414 
3415 	/* make sure the driver supplied correct dl_sub_length */
3416 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
3417 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
3418 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
3419 		return;
3420 	}
3421 
3422 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
3423 	/*
3424 	 * There are sub-capabilities. Process the ones we know about.
3425 	 * Loop until we don't have room for another sub-cap header..
3426 	 */
3427 	for (subp = SC(capp, capp->dl_sub_offset),
3428 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
3429 	    subp <= endp;
3430 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
3431 
3432 		switch (subp->dl_cap) {
3433 		case DL_CAPAB_ID_WRAPPER:
3434 			ill_capability_id_ack(ill, mp, subp);
3435 			break;
3436 		default:
3437 			ill_capability_dispatch(ill, mp, subp, B_FALSE);
3438 			break;
3439 		}
3440 	}
3441 #undef SC
3442 }
3443 
3444 /*
3445  * This routine is called to scan the fragmentation reassembly table for
3446  * the specified ILL for any packets that are starting to smell.
3447  * dead_interval is the maximum time in seconds that will be tolerated.  It
3448  * will either be the value specified in ip_g_frag_timeout, or zero if the
3449  * ILL is shutting down and it is time to blow everything off.
3450  *
3451  * It returns the number of seconds (as a time_t) that the next frag timer
3452  * should be scheduled for, 0 meaning that the timer doesn't need to be
3453  * re-started.  Note that the method of calculating next_timeout isn't
3454  * entirely accurate since time will flow between the time we grab
3455  * current_time and the time we schedule the next timeout.  This isn't a
3456  * big problem since this is the timer for sending an ICMP reassembly time
3457  * exceeded messages, and it doesn't have to be exactly accurate.
3458  *
3459  * This function is
3460  * sometimes called as writer, although this is not required.
3461  */
3462 time_t
3463 ill_frag_timeout(ill_t *ill, time_t dead_interval)
3464 {
3465 	ipfb_t	*ipfb;
3466 	ipfb_t	*endp;
3467 	ipf_t	*ipf;
3468 	ipf_t	*ipfnext;
3469 	mblk_t	*mp;
3470 	time_t	current_time = gethrestime_sec();
3471 	time_t	next_timeout = 0;
3472 	uint32_t	hdr_length;
3473 	mblk_t	*send_icmp_head;
3474 	mblk_t	*send_icmp_head_v6;
3475 
3476 	ipfb = ill->ill_frag_hash_tbl;
3477 	if (ipfb == NULL)
3478 		return (B_FALSE);
3479 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
3480 	/* Walk the frag hash table. */
3481 	for (; ipfb < endp; ipfb++) {
3482 		send_icmp_head = NULL;
3483 		send_icmp_head_v6 = NULL;
3484 		mutex_enter(&ipfb->ipfb_lock);
3485 		while ((ipf = ipfb->ipfb_ipf) != 0) {
3486 			time_t frag_time = current_time - ipf->ipf_timestamp;
3487 			time_t frag_timeout;
3488 
3489 			if (frag_time < dead_interval) {
3490 				/*
3491 				 * There are some outstanding fragments
3492 				 * that will timeout later.  Make note of
3493 				 * the time so that we can reschedule the
3494 				 * next timeout appropriately.
3495 				 */
3496 				frag_timeout = dead_interval - frag_time;
3497 				if (next_timeout == 0 ||
3498 				    frag_timeout < next_timeout) {
3499 					next_timeout = frag_timeout;
3500 				}
3501 				break;
3502 			}
3503 			/* Time's up.  Get it out of here. */
3504 			hdr_length = ipf->ipf_nf_hdr_len;
3505 			ipfnext = ipf->ipf_hash_next;
3506 			if (ipfnext)
3507 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
3508 			*ipf->ipf_ptphn = ipfnext;
3509 			mp = ipf->ipf_mp->b_cont;
3510 			for (; mp; mp = mp->b_cont) {
3511 				/* Extra points for neatness. */
3512 				IP_REASS_SET_START(mp, 0);
3513 				IP_REASS_SET_END(mp, 0);
3514 			}
3515 			mp = ipf->ipf_mp->b_cont;
3516 			ill->ill_frag_count -= ipf->ipf_count;
3517 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
3518 			ipfb->ipfb_count -= ipf->ipf_count;
3519 			ASSERT(ipfb->ipfb_frag_pkts > 0);
3520 			ipfb->ipfb_frag_pkts--;
3521 			/*
3522 			 * We do not send any icmp message from here because
3523 			 * we currently are holding the ipfb_lock for this
3524 			 * hash chain. If we try and send any icmp messages
3525 			 * from here we may end up via a put back into ip
3526 			 * trying to get the same lock, causing a recursive
3527 			 * mutex panic. Instead we build a list and send all
3528 			 * the icmp messages after we have dropped the lock.
3529 			 */
3530 			if (ill->ill_isv6) {
3531 				BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmFails);
3532 				if (hdr_length != 0) {
3533 					mp->b_next = send_icmp_head_v6;
3534 					send_icmp_head_v6 = mp;
3535 				} else {
3536 					freemsg(mp);
3537 				}
3538 			} else {
3539 				BUMP_MIB(&ip_mib, ipReasmFails);
3540 				if (hdr_length != 0) {
3541 					mp->b_next = send_icmp_head;
3542 					send_icmp_head = mp;
3543 				} else {
3544 					freemsg(mp);
3545 				}
3546 			}
3547 			freeb(ipf->ipf_mp);
3548 		}
3549 		mutex_exit(&ipfb->ipfb_lock);
3550 		/*
3551 		 * Now need to send any icmp messages that we delayed from
3552 		 * above.
3553 		 */
3554 		while (send_icmp_head_v6 != NULL) {
3555 			mp = send_icmp_head_v6;
3556 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
3557 			mp->b_next = NULL;
3558 			icmp_time_exceeded_v6(ill->ill_wq, mp,
3559 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, B_FALSE);
3560 		}
3561 		while (send_icmp_head != NULL) {
3562 			mp = send_icmp_head;
3563 			send_icmp_head = send_icmp_head->b_next;
3564 			mp->b_next = NULL;
3565 			icmp_time_exceeded(ill->ill_wq, mp,
3566 			    ICMP_REASSEMBLY_TIME_EXCEEDED);
3567 		}
3568 	}
3569 	/*
3570 	 * A non-dying ILL will use the return value to decide whether to
3571 	 * restart the frag timer, and for how long.
3572 	 */
3573 	return (next_timeout);
3574 }
3575 
3576 /*
3577  * This routine is called when the approximate count of mblk memory used
3578  * for the specified ILL has exceeded max_count.
3579  */
3580 void
3581 ill_frag_prune(ill_t *ill, uint_t max_count)
3582 {
3583 	ipfb_t	*ipfb;
3584 	ipf_t	*ipf;
3585 	size_t	count;
3586 
3587 	/*
3588 	 * If we are here within ip_min_frag_prune_time msecs remove
3589 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
3590 	 * ill_frag_free_num_pkts.
3591 	 */
3592 	mutex_enter(&ill->ill_lock);
3593 	if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <=
3594 	    (ip_min_frag_prune_time != 0 ?
3595 	    ip_min_frag_prune_time : msec_per_tick)) {
3596 
3597 		ill->ill_frag_free_num_pkts++;
3598 
3599 	} else {
3600 		ill->ill_frag_free_num_pkts = 0;
3601 	}
3602 	ill->ill_last_frag_clean_time = lbolt;
3603 	mutex_exit(&ill->ill_lock);
3604 
3605 	/*
3606 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
3607 	 */
3608 	if (ill->ill_frag_free_num_pkts != 0) {
3609 		int ix;
3610 
3611 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3612 			ipfb = &ill->ill_frag_hash_tbl[ix];
3613 			mutex_enter(&ipfb->ipfb_lock);
3614 			if (ipfb->ipfb_ipf != NULL) {
3615 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
3616 				    ill->ill_frag_free_num_pkts);
3617 			}
3618 			mutex_exit(&ipfb->ipfb_lock);
3619 		}
3620 	}
3621 	/*
3622 	 * While the reassembly list for this ILL is too big, prune a fragment
3623 	 * queue by age, oldest first.  Note that the per ILL count is
3624 	 * approximate, while the per frag hash bucket counts are accurate.
3625 	 */
3626 	while (ill->ill_frag_count > max_count) {
3627 		int	ix;
3628 		ipfb_t	*oipfb = NULL;
3629 		uint_t	oldest = UINT_MAX;
3630 
3631 		count = 0;
3632 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
3633 			ipfb = &ill->ill_frag_hash_tbl[ix];
3634 			mutex_enter(&ipfb->ipfb_lock);
3635 			ipf = ipfb->ipfb_ipf;
3636 			if (ipf != NULL && ipf->ipf_gen < oldest) {
3637 				oldest = ipf->ipf_gen;
3638 				oipfb = ipfb;
3639 			}
3640 			count += ipfb->ipfb_count;
3641 			mutex_exit(&ipfb->ipfb_lock);
3642 		}
3643 		/* Refresh the per ILL count */
3644 		ill->ill_frag_count = count;
3645 		if (oipfb == NULL) {
3646 			ill->ill_frag_count = 0;
3647 			break;
3648 		}
3649 		if (count <= max_count)
3650 			return;	/* Somebody beat us to it, nothing to do */
3651 		mutex_enter(&oipfb->ipfb_lock);
3652 		ipf = oipfb->ipfb_ipf;
3653 		if (ipf != NULL) {
3654 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
3655 		}
3656 		mutex_exit(&oipfb->ipfb_lock);
3657 	}
3658 }
3659 
3660 /*
3661  * free 'free_cnt' fragmented packets starting at ipf.
3662  */
3663 void
3664 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
3665 {
3666 	size_t	count;
3667 	mblk_t	*mp;
3668 	mblk_t	*tmp;
3669 	ipf_t **ipfp = ipf->ipf_ptphn;
3670 
3671 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
3672 	ASSERT(ipfp != NULL);
3673 	ASSERT(ipf != NULL);
3674 
3675 	while (ipf != NULL && free_cnt-- > 0) {
3676 		count = ipf->ipf_count;
3677 		mp = ipf->ipf_mp;
3678 		ipf = ipf->ipf_hash_next;
3679 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
3680 			IP_REASS_SET_START(tmp, 0);
3681 			IP_REASS_SET_END(tmp, 0);
3682 		}
3683 		ill->ill_frag_count -= count;
3684 		ASSERT(ipfb->ipfb_count >= count);
3685 		ipfb->ipfb_count -= count;
3686 		ASSERT(ipfb->ipfb_frag_pkts > 0);
3687 		ipfb->ipfb_frag_pkts--;
3688 		freemsg(mp);
3689 		BUMP_MIB(&ip_mib, ipReasmFails);
3690 	}
3691 
3692 	if (ipf)
3693 		ipf->ipf_ptphn = ipfp;
3694 	ipfp[0] = ipf;
3695 }
3696 
3697 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
3698 	"obsolete and may be removed in a future release of Solaris.  Use " \
3699 	"ifconfig(1M) to manipulate the forwarding status of an interface."
3700 
3701 /*
3702  * For obsolete per-interface forwarding configuration;
3703  * called in response to ND_GET.
3704  */
3705 /* ARGSUSED */
3706 static int
3707 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
3708 {
3709 	ill_t *ill = (ill_t *)cp;
3710 
3711 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3712 
3713 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
3714 	return (0);
3715 }
3716 
3717 /*
3718  * For obsolete per-interface forwarding configuration;
3719  * called in response to ND_SET.
3720  */
3721 /* ARGSUSED */
3722 static int
3723 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
3724     cred_t *ioc_cr)
3725 {
3726 	long value;
3727 	int retval;
3728 
3729 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
3730 
3731 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
3732 	    value < 0 || value > 1) {
3733 		return (EINVAL);
3734 	}
3735 
3736 	rw_enter(&ill_g_lock, RW_READER);
3737 	retval = ill_forward_set(q, mp, (value != 0), cp);
3738 	rw_exit(&ill_g_lock);
3739 	return (retval);
3740 }
3741 
3742 /*
3743  * Set an ill's ILLF_ROUTER flag appropriately.  If the ill is part of an
3744  * IPMP group, make sure all ill's in the group adopt the new policy.  Send
3745  * up RTS_IFINFO routing socket messages for each interface whose flags we
3746  * change.
3747  */
3748 /* ARGSUSED */
3749 int
3750 ill_forward_set(queue_t *q, mblk_t *mp, boolean_t enable, caddr_t cp)
3751 {
3752 	ill_t *ill = (ill_t *)cp;
3753 	ill_group_t *illgrp;
3754 
3755 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ill_g_lock));
3756 
3757 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
3758 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)) ||
3759 	    (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK))
3760 		return (EINVAL);
3761 
3762 	/*
3763 	 * If the ill is in an IPMP group, set the forwarding policy on all
3764 	 * members of the group to the same value.
3765 	 */
3766 	illgrp = ill->ill_group;
3767 	if (illgrp != NULL) {
3768 		ill_t *tmp_ill;
3769 
3770 		for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL;
3771 		    tmp_ill = tmp_ill->ill_group_next) {
3772 			ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3773 			    (enable ? "Enabling" : "Disabling"),
3774 			    (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"),
3775 			    tmp_ill->ill_name));
3776 			mutex_enter(&tmp_ill->ill_lock);
3777 			if (enable)
3778 				tmp_ill->ill_flags |= ILLF_ROUTER;
3779 			else
3780 				tmp_ill->ill_flags &= ~ILLF_ROUTER;
3781 			mutex_exit(&tmp_ill->ill_lock);
3782 			if (tmp_ill->ill_isv6)
3783 				ill_set_nce_router_flags(tmp_ill, enable);
3784 			/* Notify routing socket listeners of this change. */
3785 			ip_rts_ifmsg(tmp_ill->ill_ipif);
3786 		}
3787 	} else {
3788 		ip1dbg(("ill_forward_set: %s %s forwarding on %s",
3789 		    (enable ? "Enabling" : "Disabling"),
3790 		    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
3791 		mutex_enter(&ill->ill_lock);
3792 		if (enable)
3793 			ill->ill_flags |= ILLF_ROUTER;
3794 		else
3795 			ill->ill_flags &= ~ILLF_ROUTER;
3796 		mutex_exit(&ill->ill_lock);
3797 		if (ill->ill_isv6)
3798 			ill_set_nce_router_flags(ill, enable);
3799 		/* Notify routing socket listeners of this change. */
3800 		ip_rts_ifmsg(ill->ill_ipif);
3801 	}
3802 
3803 	return (0);
3804 }
3805 
3806 /*
3807  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
3808  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
3809  * set or clear.
3810  */
3811 static void
3812 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
3813 {
3814 	ipif_t *ipif;
3815 	nce_t *nce;
3816 
3817 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3818 		nce = ndp_lookup(ill, &ipif->ipif_v6lcl_addr, B_FALSE);
3819 		if (nce != NULL) {
3820 			mutex_enter(&nce->nce_lock);
3821 			if (enable)
3822 				nce->nce_flags |= NCE_F_ISROUTER;
3823 			else
3824 				nce->nce_flags &= ~NCE_F_ISROUTER;
3825 			mutex_exit(&nce->nce_lock);
3826 			NCE_REFRELE(nce);
3827 		}
3828 	}
3829 }
3830 
3831 /*
3832  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
3833  * for this ill.  Make sure the v6/v4 question has been answered about this
3834  * ill.  The creation of this ndd variable is only for backwards compatibility.
3835  * The preferred way to control per-interface IP forwarding is through the
3836  * ILLF_ROUTER interface flag.
3837  */
3838 static int
3839 ill_set_ndd_name(ill_t *ill)
3840 {
3841 	char *suffix;
3842 
3843 	ASSERT(IAM_WRITER_ILL(ill));
3844 
3845 	if (ill->ill_isv6)
3846 		suffix = ipv6_forward_suffix;
3847 	else
3848 		suffix = ipv4_forward_suffix;
3849 
3850 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
3851 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
3852 	/*
3853 	 * Copies over the '\0'.
3854 	 * Note that strlen(suffix) is always bounded.
3855 	 */
3856 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
3857 	    strlen(suffix) + 1);
3858 
3859 	/*
3860 	 * Use of the nd table requires holding the reader lock.
3861 	 * Modifying the nd table thru nd_load/nd_unload requires
3862 	 * the writer lock.
3863 	 */
3864 	rw_enter(&ip_g_nd_lock, RW_WRITER);
3865 	if (!nd_load(&ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
3866 	    nd_ill_forward_set, (caddr_t)ill)) {
3867 		/*
3868 		 * If the nd_load failed, it only meant that it could not
3869 		 * allocate a new bunch of room for further NDD expansion.
3870 		 * Because of that, the ill_ndd_name will be set to 0, and
3871 		 * this interface is at the mercy of the global ip_forwarding
3872 		 * variable.
3873 		 */
3874 		rw_exit(&ip_g_nd_lock);
3875 		ill->ill_ndd_name = NULL;
3876 		return (ENOMEM);
3877 	}
3878 	rw_exit(&ip_g_nd_lock);
3879 	return (0);
3880 }
3881 
3882 /*
3883  * Intializes the context structure and returns the first ill in the list
3884  * cuurently start_list and end_list can have values:
3885  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
3886  * IP_V4_G_HEAD		Traverse IPV4 list only.
3887  * IP_V6_G_HEAD		Traverse IPV6 list only.
3888  */
3889 
3890 /*
3891  * We don't check for CONDEMNED ills here. Caller must do that if
3892  * necessary under the ill lock.
3893  */
3894 ill_t *
3895 ill_first(int start_list, int end_list, ill_walk_context_t *ctx)
3896 {
3897 	ill_if_t *ifp;
3898 	ill_t *ill;
3899 	avl_tree_t *avl_tree;
3900 
3901 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3902 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
3903 
3904 	/*
3905 	 * setup the lists to search
3906 	 */
3907 	if (end_list != MAX_G_HEADS) {
3908 		ctx->ctx_current_list = start_list;
3909 		ctx->ctx_last_list = end_list;
3910 	} else {
3911 		ctx->ctx_last_list = MAX_G_HEADS - 1;
3912 		ctx->ctx_current_list = 0;
3913 	}
3914 
3915 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
3916 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3917 		if (ifp != (ill_if_t *)
3918 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3919 			avl_tree = &ifp->illif_avl_by_ppa;
3920 			ill = avl_first(avl_tree);
3921 			/*
3922 			 * ill is guaranteed to be non NULL or ifp should have
3923 			 * not existed.
3924 			 */
3925 			ASSERT(ill != NULL);
3926 			return (ill);
3927 		}
3928 		ctx->ctx_current_list++;
3929 	}
3930 
3931 	return (NULL);
3932 }
3933 
3934 /*
3935  * returns the next ill in the list. ill_first() must have been called
3936  * before calling ill_next() or bad things will happen.
3937  */
3938 
3939 /*
3940  * We don't check for CONDEMNED ills here. Caller must do that if
3941  * necessary under the ill lock.
3942  */
3943 ill_t *
3944 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
3945 {
3946 	ill_if_t *ifp;
3947 	ill_t *ill;
3948 
3949 
3950 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
3951 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
3952 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list));
3953 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
3954 	    AVL_AFTER)) != NULL) {
3955 		return (ill);
3956 	}
3957 
3958 	/* goto next ill_ifp in the list. */
3959 	ifp = lastill->ill_ifptr->illif_next;
3960 
3961 	/* make sure not at end of circular list */
3962 	while (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list)) {
3963 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
3964 			return (NULL);
3965 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list);
3966 	}
3967 
3968 	return (avl_first(&ifp->illif_avl_by_ppa));
3969 }
3970 
3971 /*
3972  * Check interface name for correct format which is name+ppa.
3973  * name can contain characters and digits, the right most digits
3974  * make up the ppa number. use of octal is not allowed, name must contain
3975  * a ppa, return pointer to the start of ppa.
3976  * In case of error return NULL.
3977  */
3978 static char *
3979 ill_get_ppa_ptr(char *name)
3980 {
3981 	int namelen = mi_strlen(name);
3982 
3983 	int len = namelen;
3984 
3985 	name += len;
3986 	while (len > 0) {
3987 		name--;
3988 		if (*name < '0' || *name > '9')
3989 			break;
3990 		len--;
3991 	}
3992 
3993 	/* empty string, all digits, or no trailing digits */
3994 	if (len == 0 || len == (int)namelen)
3995 		return (NULL);
3996 
3997 	name++;
3998 	/* check for attempted use of octal */
3999 	if (*name == '0' && len != (int)namelen - 1)
4000 		return (NULL);
4001 	return (name);
4002 }
4003 
4004 /*
4005  * use avl tree to locate the ill.
4006  */
4007 static ill_t *
4008 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp,
4009     ipsq_func_t func, int *error)
4010 {
4011 	char *ppa_ptr = NULL;
4012 	int len;
4013 	uint_t ppa;
4014 	ill_t *ill = NULL;
4015 	ill_if_t *ifp;
4016 	int list;
4017 	ipsq_t *ipsq;
4018 
4019 	if (error != NULL)
4020 		*error = 0;
4021 
4022 	/*
4023 	 * get ppa ptr
4024 	 */
4025 	if (isv6)
4026 		list = IP_V6_G_HEAD;
4027 	else
4028 		list = IP_V4_G_HEAD;
4029 
4030 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
4031 		if (error != NULL)
4032 			*error = ENXIO;
4033 		return (NULL);
4034 	}
4035 
4036 	len = ppa_ptr - name + 1;
4037 
4038 	ppa = stoi(&ppa_ptr);
4039 
4040 	ifp = IP_VX_ILL_G_LIST(list);
4041 
4042 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4043 		/*
4044 		 * match is done on len - 1 as the name is not null
4045 		 * terminated it contains ppa in addition to the interface
4046 		 * name.
4047 		 */
4048 		if ((ifp->illif_name_len == len) &&
4049 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
4050 			break;
4051 		} else {
4052 			ifp = ifp->illif_next;
4053 		}
4054 	}
4055 
4056 
4057 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list)) {
4058 		/*
4059 		 * Even the interface type does not exist.
4060 		 */
4061 		if (error != NULL)
4062 			*error = ENXIO;
4063 		return (NULL);
4064 	}
4065 
4066 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
4067 	if (ill != NULL) {
4068 		/*
4069 		 * The block comment at the start of ipif_down
4070 		 * explains the use of the macros used below
4071 		 */
4072 		GRAB_CONN_LOCK(q);
4073 		mutex_enter(&ill->ill_lock);
4074 		if (ILL_CAN_LOOKUP(ill)) {
4075 			ill_refhold_locked(ill);
4076 			mutex_exit(&ill->ill_lock);
4077 			RELEASE_CONN_LOCK(q);
4078 			return (ill);
4079 		} else if (ILL_CAN_WAIT(ill, q)) {
4080 			ipsq = ill->ill_phyint->phyint_ipsq;
4081 			mutex_enter(&ipsq->ipsq_lock);
4082 			mutex_exit(&ill->ill_lock);
4083 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4084 			mutex_exit(&ipsq->ipsq_lock);
4085 			RELEASE_CONN_LOCK(q);
4086 			*error = EINPROGRESS;
4087 			return (NULL);
4088 		}
4089 		mutex_exit(&ill->ill_lock);
4090 		RELEASE_CONN_LOCK(q);
4091 	}
4092 	if (error != NULL)
4093 		*error = ENXIO;
4094 	return (NULL);
4095 }
4096 
4097 /*
4098  * comparison function for use with avl.
4099  */
4100 static int
4101 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
4102 {
4103 	uint_t ppa;
4104 	uint_t ill_ppa;
4105 
4106 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
4107 
4108 	ppa = *((uint_t *)ppa_ptr);
4109 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
4110 	/*
4111 	 * We want the ill with the lowest ppa to be on the
4112 	 * top.
4113 	 */
4114 	if (ill_ppa < ppa)
4115 		return (1);
4116 	if (ill_ppa > ppa)
4117 		return (-1);
4118 	return (0);
4119 }
4120 
4121 /*
4122  * remove an interface type from the global list.
4123  */
4124 static void
4125 ill_delete_interface_type(ill_if_t *interface)
4126 {
4127 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4128 
4129 	ASSERT(interface != NULL);
4130 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
4131 
4132 	avl_destroy(&interface->illif_avl_by_ppa);
4133 	if (interface->illif_ppa_arena != NULL)
4134 		vmem_destroy(interface->illif_ppa_arena);
4135 
4136 	remque(interface);
4137 
4138 	mi_free(interface);
4139 }
4140 
4141 /*
4142  * remove ill from the global list.
4143  */
4144 static void
4145 ill_glist_delete(ill_t *ill)
4146 {
4147 	if (ill == NULL)
4148 		return;
4149 
4150 	rw_enter(&ill_g_lock, RW_WRITER);
4151 	/*
4152 	 * If the ill was never inserted into the AVL tree
4153 	 * we skip the if branch.
4154 	 */
4155 	if (ill->ill_ifptr != NULL) {
4156 		/*
4157 		 * remove from AVL tree and free ppa number
4158 		 */
4159 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
4160 
4161 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
4162 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
4163 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4164 		}
4165 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
4166 			ill_delete_interface_type(ill->ill_ifptr);
4167 		}
4168 
4169 		/*
4170 		 * Indicate ill is no longer in the list.
4171 		 */
4172 		ill->ill_ifptr = NULL;
4173 		ill->ill_name_length = 0;
4174 		ill->ill_name[0] = '\0';
4175 		ill->ill_ppa = UINT_MAX;
4176 	}
4177 	ill_phyint_free(ill);
4178 	rw_exit(&ill_g_lock);
4179 }
4180 
4181 /*
4182  * allocate a ppa, if the number of plumbed interfaces of this type are
4183  * less than ill_no_arena do a linear search to find a unused ppa.
4184  * When the number goes beyond ill_no_arena switch to using an arena.
4185  * Note: ppa value of zero cannot be allocated from vmem_arena as it
4186  * is the return value for an error condition, so allocation starts at one
4187  * and is decremented by one.
4188  */
4189 static int
4190 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
4191 {
4192 	ill_t *tmp_ill;
4193 	uint_t start, end;
4194 	int ppa;
4195 
4196 	if (ifp->illif_ppa_arena == NULL &&
4197 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
4198 		/*
4199 		 * Create an arena.
4200 		 */
4201 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
4202 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
4203 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
4204 			/* allocate what has already been assigned */
4205 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
4206 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
4207 		    tmp_ill, AVL_AFTER)) {
4208 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4209 			    1,		/* size */
4210 			    1,		/* align/quantum */
4211 			    0,		/* phase */
4212 			    0,		/* nocross */
4213 		(void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */
4214 		(void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */
4215 			    VM_NOSLEEP|VM_FIRSTFIT);
4216 			if (ppa == 0) {
4217 				ip1dbg(("ill_alloc_ppa: ppa allocation"
4218 				    " failed while switching"));
4219 				vmem_destroy(ifp->illif_ppa_arena);
4220 				ifp->illif_ppa_arena = NULL;
4221 				break;
4222 			}
4223 		}
4224 	}
4225 
4226 	if (ifp->illif_ppa_arena != NULL) {
4227 		if (ill->ill_ppa == UINT_MAX) {
4228 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
4229 			    1, VM_NOSLEEP|VM_FIRSTFIT);
4230 			if (ppa == 0)
4231 				return (EAGAIN);
4232 			ill->ill_ppa = --ppa;
4233 		} else {
4234 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
4235 			    1, 		/* size */
4236 			    1, 		/* align/quantum */
4237 			    0, 		/* phase */
4238 			    0, 		/* nocross */
4239 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
4240 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
4241 			    VM_NOSLEEP|VM_FIRSTFIT);
4242 			/*
4243 			 * Most likely the allocation failed because
4244 			 * the requested ppa was in use.
4245 			 */
4246 			if (ppa == 0)
4247 				return (EEXIST);
4248 		}
4249 		return (0);
4250 	}
4251 
4252 	/*
4253 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
4254 	 * been plumbed to create one. Do a linear search to get a unused ppa.
4255 	 */
4256 	if (ill->ill_ppa == UINT_MAX) {
4257 		end = UINT_MAX - 1;
4258 		start = 0;
4259 	} else {
4260 		end = start = ill->ill_ppa;
4261 	}
4262 
4263 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
4264 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
4265 		if (start++ >= end) {
4266 			if (ill->ill_ppa == UINT_MAX)
4267 				return (EAGAIN);
4268 			else
4269 				return (EEXIST);
4270 		}
4271 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
4272 	}
4273 	ill->ill_ppa = start;
4274 	return (0);
4275 }
4276 
4277 /*
4278  * Insert ill into the list of configured ill's. Once this function completes,
4279  * the ill is globally visible and is available through lookups. More precisely
4280  * this happens after the caller drops the ill_g_lock.
4281  */
4282 static int
4283 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
4284 {
4285 	ill_if_t *ill_interface;
4286 	avl_index_t where = 0;
4287 	int error;
4288 	int name_length;
4289 	int index;
4290 	boolean_t check_length = B_FALSE;
4291 
4292 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
4293 
4294 	name_length = mi_strlen(name) + 1;
4295 
4296 	if (isv6)
4297 		index = IP_V6_G_HEAD;
4298 	else
4299 		index = IP_V4_G_HEAD;
4300 
4301 	ill_interface = IP_VX_ILL_G_LIST(index);
4302 	/*
4303 	 * Search for interface type based on name
4304 	 */
4305 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4306 		if ((ill_interface->illif_name_len == name_length) &&
4307 		    (strcmp(ill_interface->illif_name, name) == 0)) {
4308 			break;
4309 		}
4310 		ill_interface = ill_interface->illif_next;
4311 	}
4312 
4313 	/*
4314 	 * Interface type not found, create one.
4315 	 */
4316 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index)) {
4317 
4318 		ill_g_head_t ghead;
4319 
4320 		/*
4321 		 * allocate ill_if_t structure
4322 		 */
4323 
4324 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
4325 		if (ill_interface == NULL) {
4326 			return (ENOMEM);
4327 		}
4328 
4329 
4330 
4331 		(void) strcpy(ill_interface->illif_name, name);
4332 		ill_interface->illif_name_len = name_length;
4333 
4334 		avl_create(&ill_interface->illif_avl_by_ppa,
4335 		    ill_compare_ppa, sizeof (ill_t),
4336 		    offsetof(struct ill_s, ill_avl_byppa));
4337 
4338 		/*
4339 		 * link the structure in the back to maintain order
4340 		 * of configuration for ifconfig output.
4341 		 */
4342 		ghead = ill_g_heads[index];
4343 		insque(ill_interface, ghead.ill_g_list_tail);
4344 
4345 	}
4346 
4347 	if (ill->ill_ppa == UINT_MAX)
4348 		check_length = B_TRUE;
4349 
4350 	error = ill_alloc_ppa(ill_interface, ill);
4351 	if (error != 0) {
4352 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
4353 			ill_delete_interface_type(ill->ill_ifptr);
4354 		return (error);
4355 	}
4356 
4357 	/*
4358 	 * When the ppa is choosen by the system, check that there is
4359 	 * enough space to insert ppa. if a specific ppa was passed in this
4360 	 * check is not required as the interface name passed in will have
4361 	 * the right ppa in it.
4362 	 */
4363 	if (check_length) {
4364 		/*
4365 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
4366 		 */
4367 		char buf[sizeof (uint_t) * 3];
4368 
4369 		/*
4370 		 * convert ppa to string to calculate the amount of space
4371 		 * required for it in the name.
4372 		 */
4373 		numtos(ill->ill_ppa, buf);
4374 
4375 		/* Do we have enough space to insert ppa ? */
4376 
4377 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
4378 			/* Free ppa and interface type struct */
4379 			if (ill_interface->illif_ppa_arena != NULL) {
4380 				vmem_free(ill_interface->illif_ppa_arena,
4381 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
4382 			}
4383 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) ==
4384 			    0) {
4385 				ill_delete_interface_type(ill->ill_ifptr);
4386 			}
4387 
4388 			return (EINVAL);
4389 		}
4390 	}
4391 
4392 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
4393 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
4394 
4395 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
4396 	    &where);
4397 	ill->ill_ifptr = ill_interface;
4398 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
4399 
4400 	ill_phyint_reinit(ill);
4401 	return (0);
4402 }
4403 
4404 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */
4405 static boolean_t
4406 ipsq_init(ill_t *ill)
4407 {
4408 	ipsq_t  *ipsq;
4409 
4410 	/* Init the ipsq and impicitly enter as writer */
4411 	ill->ill_phyint->phyint_ipsq =
4412 	    kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
4413 	if (ill->ill_phyint->phyint_ipsq == NULL)
4414 		return (B_FALSE);
4415 	ipsq = ill->ill_phyint->phyint_ipsq;
4416 	ipsq->ipsq_phyint_list = ill->ill_phyint;
4417 	ill->ill_phyint->phyint_ipsq_next = NULL;
4418 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
4419 	ipsq->ipsq_refs = 1;
4420 	ipsq->ipsq_writer = curthread;
4421 	ipsq->ipsq_reentry_cnt = 1;
4422 #ifdef ILL_DEBUG
4423 	ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH);
4424 #endif
4425 	(void) strcpy(ipsq->ipsq_name, ill->ill_name);
4426 	return (B_TRUE);
4427 }
4428 
4429 /*
4430  * ill_init is called by ip_open when a device control stream is opened.
4431  * It does a few initializations, and shoots a DL_INFO_REQ message down
4432  * to the driver.  The response is later picked up in ip_rput_dlpi and
4433  * used to set up default mechanisms for talking to the driver.  (Always
4434  * called as writer.)
4435  *
4436  * If this function returns error, ip_open will call ip_close which in
4437  * turn will call ill_delete to clean up any memory allocated here that
4438  * is not yet freed.
4439  */
4440 int
4441 ill_init(queue_t *q, ill_t *ill)
4442 {
4443 	int	count;
4444 	dl_info_req_t	*dlir;
4445 	mblk_t	*info_mp;
4446 	uchar_t *frag_ptr;
4447 
4448 	/*
4449 	 * The ill is initialized to zero by mi_alloc*(). In addition
4450 	 * some fields already contain valid values, initialized in
4451 	 * ip_open(), before we reach here.
4452 	 */
4453 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
4454 
4455 	ill->ill_rq = q;
4456 	ill->ill_wq = WR(q);
4457 
4458 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
4459 	    BPRI_HI);
4460 	if (info_mp == NULL)
4461 		return (ENOMEM);
4462 
4463 	/*
4464 	 * Allocate sufficient space to contain our fragment hash table and
4465 	 * the device name.
4466 	 */
4467 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
4468 	    2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix));
4469 	if (frag_ptr == NULL) {
4470 		freemsg(info_mp);
4471 		return (ENOMEM);
4472 	}
4473 	ill->ill_frag_ptr = frag_ptr;
4474 	ill->ill_frag_free_num_pkts = 0;
4475 	ill->ill_last_frag_clean_time = 0;
4476 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
4477 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
4478 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
4479 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
4480 		    NULL, MUTEX_DEFAULT, NULL);
4481 	}
4482 
4483 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4484 	if (ill->ill_phyint == NULL) {
4485 		freemsg(info_mp);
4486 		mi_free(frag_ptr);
4487 		return (ENOMEM);
4488 	}
4489 
4490 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4491 	/*
4492 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
4493 	 * at this point because of the following reason. If we can't
4494 	 * enter the ipsq at some point and cv_wait, the writer that
4495 	 * wakes us up tries to locate us using the list of all phyints
4496 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
4497 	 * If we don't set it now, we risk a missed wakeup.
4498 	 */
4499 	ill->ill_phyint->phyint_illv4 = ill;
4500 	ill->ill_ppa = UINT_MAX;
4501 	ill->ill_fastpath_list = &ill->ill_fastpath_list;
4502 
4503 	if (!ipsq_init(ill)) {
4504 		freemsg(info_mp);
4505 		mi_free(frag_ptr);
4506 		mi_free(ill->ill_phyint);
4507 		return (ENOMEM);
4508 	}
4509 
4510 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
4511 
4512 
4513 	/* Frag queue limit stuff */
4514 	ill->ill_frag_count = 0;
4515 	ill->ill_ipf_gen = 0;
4516 
4517 	ill->ill_global_timer = INFINITY;
4518 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4519 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4520 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4521 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4522 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4523 
4524 	/*
4525 	 * Initialize IPv6 configuration variables.  The IP module is always
4526 	 * opened as an IPv4 module.  Instead tracking down the cases where
4527 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
4528 	 * here for convenience, this has no effect until the ill is set to do
4529 	 * IPv6.
4530 	 */
4531 	ill->ill_reachable_time = ND_REACHABLE_TIME;
4532 	ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
4533 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
4534 	ill->ill_max_buf = ND_MAX_Q;
4535 	ill->ill_refcnt = 0;
4536 
4537 	/* Send down the Info Request to the driver. */
4538 	info_mp->b_datap->db_type = M_PCPROTO;
4539 	dlir = (dl_info_req_t *)info_mp->b_rptr;
4540 	info_mp->b_wptr = (uchar_t *)&dlir[1];
4541 	dlir->dl_primitive = DL_INFO_REQ;
4542 
4543 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
4544 
4545 	qprocson(q);
4546 	ill_dlpi_send(ill, info_mp);
4547 
4548 	return (0);
4549 }
4550 
4551 /*
4552  * ill_dls_info
4553  * creates datalink socket info from the device.
4554  */
4555 int
4556 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif)
4557 {
4558 	size_t	length;
4559 	ill_t	*ill = ipif->ipif_ill;
4560 
4561 	sdl->sdl_family = AF_LINK;
4562 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4563 	sdl->sdl_type = ipif->ipif_type;
4564 	(void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data));
4565 	length = mi_strlen(sdl->sdl_data);
4566 	ASSERT(length < 256);
4567 	sdl->sdl_nlen = (uchar_t)length;
4568 	sdl->sdl_alen = ill->ill_phys_addr_length;
4569 	mutex_enter(&ill->ill_lock);
4570 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) {
4571 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[length],
4572 		    ill->ill_phys_addr_length);
4573 	}
4574 	mutex_exit(&ill->ill_lock);
4575 	sdl->sdl_slen = 0;
4576 	return (sizeof (struct sockaddr_dl));
4577 }
4578 
4579 /*
4580  * ill_xarp_info
4581  * creates xarp info from the device.
4582  */
4583 static int
4584 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
4585 {
4586 	sdl->sdl_family = AF_LINK;
4587 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
4588 	sdl->sdl_type = ill->ill_type;
4589 	(void) ipif_get_name(ill->ill_ipif, sdl->sdl_data,
4590 	    sizeof (sdl->sdl_data));
4591 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
4592 	sdl->sdl_alen = ill->ill_phys_addr_length;
4593 	sdl->sdl_slen = 0;
4594 	return (sdl->sdl_nlen);
4595 }
4596 
4597 static int
4598 loopback_kstat_update(kstat_t *ksp, int rw)
4599 {
4600 	kstat_named_t *kn = KSTAT_NAMED_PTR(ksp);
4601 
4602 	if (rw == KSTAT_WRITE)
4603 		return (EACCES);
4604 	kn[0].value.ui32 = loopback_packets;
4605 	kn[1].value.ui32 = loopback_packets;
4606 	return (0);
4607 }
4608 
4609 
4610 /*
4611  * Has ifindex been plumbed already.
4612  */
4613 static boolean_t
4614 phyint_exists(uint_t index)
4615 {
4616 	phyint_t *phyi;
4617 
4618 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
4619 	/*
4620 	 * Indexes are stored in the phyint - a common structure
4621 	 * to both IPv4 and IPv6.
4622 	 */
4623 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4624 	    (void *) &index, NULL);
4625 	return (phyi != NULL);
4626 }
4627 
4628 /*
4629  * Assign a unique interface index for the phyint.
4630  */
4631 static boolean_t
4632 phyint_assign_ifindex(phyint_t *phyi)
4633 {
4634 	uint_t starting_index;
4635 
4636 	ASSERT(phyi->phyint_ifindex == 0);
4637 	if (!ill_index_wrap) {
4638 		phyi->phyint_ifindex = ill_index++;
4639 		if (ill_index == 0) {
4640 			/* Reached the uint_t limit Next time wrap  */
4641 			ill_index_wrap = B_TRUE;
4642 		}
4643 		return (B_TRUE);
4644 	}
4645 
4646 	/*
4647 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
4648 	 * at this point and don't want to call any function that attempts
4649 	 * to get the lock again.
4650 	 */
4651 	starting_index = ill_index++;
4652 	for (; ill_index != starting_index; ill_index++) {
4653 		if (ill_index != 0 && !phyint_exists(ill_index)) {
4654 			/* found unused index - use it */
4655 			phyi->phyint_ifindex = ill_index;
4656 			return (B_TRUE);
4657 		}
4658 	}
4659 
4660 	/*
4661 	 * all interface indicies are inuse.
4662 	 */
4663 	return (B_FALSE);
4664 }
4665 
4666 /*
4667  * Return a pointer to the ill which matches the supplied name.  Note that
4668  * the ill name length includes the null termination character.  (May be
4669  * called as writer.)
4670  * If do_alloc and the interface is "lo0" it will be automatically created.
4671  * Cannot bump up reference on condemned ills. So dup detect can't be done
4672  * using this func.
4673  */
4674 ill_t *
4675 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
4676     queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc)
4677 {
4678 	ill_t	*ill;
4679 	ipif_t	*ipif;
4680 	kstat_named_t	*kn;
4681 	boolean_t isloopback;
4682 	ipsq_t *old_ipsq;
4683 
4684 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
4685 
4686 	rw_enter(&ill_g_lock, RW_READER);
4687 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4688 	rw_exit(&ill_g_lock);
4689 	if (ill != NULL || (error != NULL && *error == EINPROGRESS))
4690 		return (ill);
4691 
4692 	/*
4693 	 * Couldn't find it.  Does this happen to be a lookup for the
4694 	 * loopback device and are we allowed to allocate it?
4695 	 */
4696 	if (!isloopback || !do_alloc)
4697 		return (NULL);
4698 
4699 	rw_enter(&ill_g_lock, RW_WRITER);
4700 
4701 	ill = ill_find_by_name(name, isv6, q, mp, func, error);
4702 	if (ill != NULL || (error != NULL && *error == EINPROGRESS)) {
4703 		rw_exit(&ill_g_lock);
4704 		return (ill);
4705 	}
4706 
4707 	/* Create the loopback device on demand */
4708 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
4709 	    sizeof (ipif_loopback_name), BPRI_MED));
4710 	if (ill == NULL)
4711 		goto done;
4712 
4713 	*ill = ill_null;
4714 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
4715 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
4716 	if (ill->ill_phyint == NULL)
4717 		goto done;
4718 
4719 	if (isv6)
4720 		ill->ill_phyint->phyint_illv6 = ill;
4721 	else
4722 		ill->ill_phyint->phyint_illv4 = ill;
4723 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
4724 	ill->ill_max_frag = IP_LOOPBACK_MTU;
4725 	/* Add room for tcp+ip headers */
4726 	if (isv6) {
4727 		ill->ill_isv6 = B_TRUE;
4728 		ill->ill_max_frag += IPV6_HDR_LEN + 20;	/* for TCP */
4729 		if (!ill_allocate_mibs(ill))
4730 			goto done;
4731 	} else {
4732 		ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20;
4733 	}
4734 	ill->ill_max_mtu = ill->ill_max_frag;
4735 	/*
4736 	 * ipif_loopback_name can't be pointed at directly because its used
4737 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
4738 	 * from the glist, ill_glist_delete() sets the first character of
4739 	 * ill_name to '\0'.
4740 	 */
4741 	ill->ill_name = (char *)ill + sizeof (*ill);
4742 	(void) strcpy(ill->ill_name, ipif_loopback_name);
4743 	ill->ill_name_length = sizeof (ipif_loopback_name);
4744 	/* Set ill_name_set for ill_phyint_reinit to work properly */
4745 
4746 	ill->ill_global_timer = INFINITY;
4747 	ill->ill_mcast_type = IGMP_V3_ROUTER;	/* == MLD_V2_ROUTER */
4748 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
4749 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
4750 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
4751 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
4752 
4753 	/* No resolver here. */
4754 	ill->ill_net_type = IRE_LOOPBACK;
4755 
4756 	/* Initialize the ipsq */
4757 	if (!ipsq_init(ill))
4758 		goto done;
4759 
4760 	ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL;
4761 	ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--;
4762 	ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0);
4763 #ifdef ILL_DEBUG
4764 	ill->ill_phyint->phyint_ipsq->ipsq_depth = 0;
4765 #endif
4766 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE);
4767 	if (ipif == NULL)
4768 		goto done;
4769 
4770 	ill->ill_flags = ILLF_MULTICAST;
4771 
4772 	/* Set up default loopback address and mask. */
4773 	if (!isv6) {
4774 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
4775 
4776 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
4777 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4778 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
4779 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4780 		    ipif->ipif_v6subnet);
4781 		ill->ill_flags |= ILLF_IPV4;
4782 	} else {
4783 		ipif->ipif_v6lcl_addr = ipv6_loopback;
4784 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
4785 		ipif->ipif_v6net_mask = ipv6_all_ones;
4786 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
4787 		    ipif->ipif_v6subnet);
4788 		ill->ill_flags |= ILLF_IPV6;
4789 	}
4790 
4791 	/*
4792 	 * Chain us in at the end of the ill list. hold the ill
4793 	 * before we make it globally visible. 1 for the lookup.
4794 	 */
4795 	ill->ill_refcnt = 0;
4796 	ill_refhold(ill);
4797 
4798 	ill->ill_frag_count = 0;
4799 	ill->ill_frag_free_num_pkts = 0;
4800 	ill->ill_last_frag_clean_time = 0;
4801 
4802 	old_ipsq = ill->ill_phyint->phyint_ipsq;
4803 
4804 	if (ill_glist_insert(ill, "lo", isv6) != 0)
4805 		cmn_err(CE_PANIC, "cannot insert loopback interface");
4806 
4807 	/* Let SCTP know so that it can add this to its list */
4808 	sctp_update_ill(ill, SCTP_ILL_INSERT);
4809 
4810 	/* Let SCTP know about this IPIF, so that it can add it to its list */
4811 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
4812 
4813 	/*
4814 	 * If the ipsq was changed in ill_phyint_reinit free the old ipsq.
4815 	 */
4816 	if (old_ipsq != ill->ill_phyint->phyint_ipsq) {
4817 		/* Loopback ills aren't in any IPMP group */
4818 		ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP));
4819 		ipsq_delete(old_ipsq);
4820 	}
4821 
4822 	/*
4823 	 * Delay this till the ipif is allocated as ipif_allocate
4824 	 * de-references ill_phyint for getting the ifindex. We
4825 	 * can't do this before ipif_allocate because ill_phyint_reinit
4826 	 * -> phyint_assign_ifindex expects ipif to be present.
4827 	 */
4828 	mutex_enter(&ill->ill_phyint->phyint_lock);
4829 	ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL;
4830 	mutex_exit(&ill->ill_phyint->phyint_lock);
4831 
4832 	if (loopback_ksp == NULL) {
4833 		/* Export loopback interface statistics */
4834 		loopback_ksp = kstat_create("lo", 0, ipif_loopback_name, "net",
4835 		    KSTAT_TYPE_NAMED, 2, 0);
4836 		if (loopback_ksp != NULL) {
4837 			loopback_ksp->ks_update = loopback_kstat_update;
4838 			kn = KSTAT_NAMED_PTR(loopback_ksp);
4839 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
4840 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
4841 			kstat_install(loopback_ksp);
4842 		}
4843 	}
4844 
4845 	if (error != NULL)
4846 		*error = 0;
4847 	*did_alloc = B_TRUE;
4848 	rw_exit(&ill_g_lock);
4849 	return (ill);
4850 done:
4851 	if (ill != NULL) {
4852 		if (ill->ill_phyint != NULL) {
4853 			ipsq_t	*ipsq;
4854 
4855 			ipsq = ill->ill_phyint->phyint_ipsq;
4856 			if (ipsq != NULL)
4857 				kmem_free(ipsq, sizeof (ipsq_t));
4858 			mi_free(ill->ill_phyint);
4859 		}
4860 		ill_free_mib(ill);
4861 		mi_free(ill);
4862 	}
4863 	rw_exit(&ill_g_lock);
4864 	if (error != NULL)
4865 		*error = ENOMEM;
4866 	return (NULL);
4867 }
4868 
4869 /*
4870  * Return a pointer to the ill which matches the index and IP version type.
4871  */
4872 ill_t *
4873 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp,
4874     ipsq_func_t func, int *err)
4875 {
4876 	ill_t	*ill;
4877 	ipsq_t  *ipsq;
4878 	phyint_t *phyi;
4879 
4880 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
4881 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
4882 
4883 	if (err != NULL)
4884 		*err = 0;
4885 
4886 	/*
4887 	 * Indexes are stored in the phyint - a common structure
4888 	 * to both IPv4 and IPv6.
4889 	 */
4890 	rw_enter(&ill_g_lock, RW_READER);
4891 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
4892 	    (void *) &index, NULL);
4893 	if (phyi != NULL) {
4894 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
4895 		if (ill != NULL) {
4896 			/*
4897 			 * The block comment at the start of ipif_down
4898 			 * explains the use of the macros used below
4899 			 */
4900 			GRAB_CONN_LOCK(q);
4901 			mutex_enter(&ill->ill_lock);
4902 			if (ILL_CAN_LOOKUP(ill)) {
4903 				ill_refhold_locked(ill);
4904 				mutex_exit(&ill->ill_lock);
4905 				RELEASE_CONN_LOCK(q);
4906 				rw_exit(&ill_g_lock);
4907 				return (ill);
4908 			} else if (ILL_CAN_WAIT(ill, q)) {
4909 				ipsq = ill->ill_phyint->phyint_ipsq;
4910 				mutex_enter(&ipsq->ipsq_lock);
4911 				rw_exit(&ill_g_lock);
4912 				mutex_exit(&ill->ill_lock);
4913 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
4914 				mutex_exit(&ipsq->ipsq_lock);
4915 				RELEASE_CONN_LOCK(q);
4916 				*err = EINPROGRESS;
4917 				return (NULL);
4918 			}
4919 			RELEASE_CONN_LOCK(q);
4920 			mutex_exit(&ill->ill_lock);
4921 		}
4922 	}
4923 	rw_exit(&ill_g_lock);
4924 	if (err != NULL)
4925 		*err = ENXIO;
4926 	return (NULL);
4927 }
4928 
4929 /*
4930  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4931  * that gives a running thread a reference to the ill. This reference must be
4932  * released by the thread when it is done accessing the ill and related
4933  * objects. ill_refcnt can not be used to account for static references
4934  * such as other structures pointing to an ill. Callers must generally
4935  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4936  * or be sure that the ill is not being deleted or changing state before
4937  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4938  * ill won't change any of its critical state such as address, netmask etc.
4939  */
4940 void
4941 ill_refhold(ill_t *ill)
4942 {
4943 	mutex_enter(&ill->ill_lock);
4944 	ill->ill_refcnt++;
4945 	ILL_TRACE_REF(ill);
4946 	mutex_exit(&ill->ill_lock);
4947 }
4948 
4949 void
4950 ill_refhold_locked(ill_t *ill)
4951 {
4952 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4953 	ill->ill_refcnt++;
4954 	ILL_TRACE_REF(ill);
4955 }
4956 
4957 int
4958 ill_check_and_refhold(ill_t *ill)
4959 {
4960 	mutex_enter(&ill->ill_lock);
4961 	if (ILL_CAN_LOOKUP(ill)) {
4962 		ill_refhold_locked(ill);
4963 		mutex_exit(&ill->ill_lock);
4964 		return (0);
4965 	}
4966 	mutex_exit(&ill->ill_lock);
4967 	return (ILL_LOOKUP_FAILED);
4968 }
4969 
4970 /*
4971  * Must not be called while holding any locks. Otherwise if this is
4972  * the last reference to be released, there is a chance of recursive mutex
4973  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4974  * to restart an ioctl.
4975  */
4976 void
4977 ill_refrele(ill_t *ill)
4978 {
4979 	mutex_enter(&ill->ill_lock);
4980 	ASSERT(ill->ill_refcnt != 0);
4981 	ill->ill_refcnt--;
4982 	ILL_UNTRACE_REF(ill);
4983 	if (ill->ill_refcnt != 0) {
4984 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4985 		mutex_exit(&ill->ill_lock);
4986 		return;
4987 	}
4988 
4989 	/* Drops the ill_lock */
4990 	ipif_ill_refrele_tail(ill);
4991 }
4992 
4993 /*
4994  * Obtain a weak reference count on the ill. This reference ensures the
4995  * ill won't be freed, but the ill may change any of its critical state
4996  * such as netmask, address etc. Returns an error if the ill has started
4997  * closing.
4998  */
4999 boolean_t
5000 ill_waiter_inc(ill_t *ill)
5001 {
5002 	mutex_enter(&ill->ill_lock);
5003 	if (ill->ill_state_flags & ILL_CONDEMNED) {
5004 		mutex_exit(&ill->ill_lock);
5005 		return (B_FALSE);
5006 	}
5007 	ill->ill_waiters++;
5008 	mutex_exit(&ill->ill_lock);
5009 	return (B_TRUE);
5010 }
5011 
5012 void
5013 ill_waiter_dcr(ill_t *ill)
5014 {
5015 	mutex_enter(&ill->ill_lock);
5016 	ill->ill_waiters--;
5017 	if (ill->ill_waiters == 0)
5018 		cv_broadcast(&ill->ill_cv);
5019 	mutex_exit(&ill->ill_lock);
5020 }
5021 
5022 /*
5023  * Named Dispatch routine to produce a formatted report on all ILLs.
5024  * This report is accessed by using the ndd utility to "get" ND variable
5025  * "ip_ill_status".
5026  */
5027 /* ARGSUSED */
5028 int
5029 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5030 {
5031 	ill_t		*ill;
5032 	ill_walk_context_t ctx;
5033 
5034 	(void) mi_mpprintf(mp,
5035 	    "ILL      " MI_COL_HDRPAD_STR
5036 	/*   01234567[89ABCDEF] */
5037 	    "rq       " MI_COL_HDRPAD_STR
5038 	/*   01234567[89ABCDEF] */
5039 	    "wq       " MI_COL_HDRPAD_STR
5040 	/*   01234567[89ABCDEF] */
5041 	    "upcnt mxfrg err name");
5042 	/*   12345 12345 123 xxxxxxxx  */
5043 
5044 	rw_enter(&ill_g_lock, RW_READER);
5045 	ill = ILL_START_WALK_ALL(&ctx);
5046 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5047 		(void) mi_mpprintf(mp,
5048 		    MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR
5049 		    "%05u %05u %03d %s",
5050 		    (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq,
5051 		    ill->ill_ipif_up_count,
5052 		    ill->ill_max_frag, ill->ill_error, ill->ill_name);
5053 	}
5054 	rw_exit(&ill_g_lock);
5055 
5056 	return (0);
5057 }
5058 
5059 /*
5060  * Named Dispatch routine to produce a formatted report on all IPIFs.
5061  * This report is accessed by using the ndd utility to "get" ND variable
5062  * "ip_ipif_status".
5063  */
5064 /* ARGSUSED */
5065 int
5066 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
5067 {
5068 	char	buf1[INET6_ADDRSTRLEN];
5069 	char	buf2[INET6_ADDRSTRLEN];
5070 	char	buf3[INET6_ADDRSTRLEN];
5071 	char	buf4[INET6_ADDRSTRLEN];
5072 	char	buf5[INET6_ADDRSTRLEN];
5073 	char	buf6[INET6_ADDRSTRLEN];
5074 	char	buf[LIFNAMSIZ];
5075 	ill_t	*ill;
5076 	ipif_t	*ipif;
5077 	nv_t	*nvp;
5078 	uint64_t flags;
5079 	zoneid_t zoneid;
5080 	ill_walk_context_t ctx;
5081 
5082 	(void) mi_mpprintf(mp,
5083 	    "IPIF metric mtu in/out/forward name zone flags...\n"
5084 	    "\tlocal address\n"
5085 	    "\tsrc address\n"
5086 	    "\tsubnet\n"
5087 	    "\tmask\n"
5088 	    "\tbroadcast\n"
5089 	    "\tp-p-dst");
5090 
5091 	ASSERT(q->q_next == NULL);
5092 	zoneid = Q_TO_CONN(q)->conn_zoneid;	/* IP is a driver */
5093 
5094 	rw_enter(&ill_g_lock, RW_READER);
5095 	ill = ILL_START_WALK_ALL(&ctx);
5096 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5097 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
5098 			if (zoneid != GLOBAL_ZONEID &&
5099 			    zoneid != ipif->ipif_zoneid &&
5100 			    ipif->ipif_zoneid != ALL_ZONES)
5101 				continue;
5102 			(void) mi_mpprintf(mp,
5103 			    MI_COL_PTRFMT_STR
5104 			    "%04u %05u %u/%u/%u %s %d",
5105 			    (void *)ipif,
5106 			    ipif->ipif_metric, ipif->ipif_mtu,
5107 			    ipif->ipif_ib_pkt_count,
5108 			    ipif->ipif_ob_pkt_count,
5109 			    ipif->ipif_fo_pkt_count,
5110 			    ipif_get_name(ipif, buf, sizeof (buf)),
5111 			    ipif->ipif_zoneid);
5112 
5113 		flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
5114 		    ipif->ipif_ill->ill_phyint->phyint_flags;
5115 
5116 		/* Tack on text strings for any flags. */
5117 		nvp = ipif_nv_tbl;
5118 		for (; nvp < A_END(ipif_nv_tbl); nvp++) {
5119 			if (nvp->nv_value & flags)
5120 				(void) mi_mpprintf_nr(mp, " %s",
5121 				    nvp->nv_name);
5122 		}
5123 		(void) mi_mpprintf(mp,
5124 		    "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
5125 		    inet_ntop(AF_INET6,
5126 			&ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)),
5127 		    inet_ntop(AF_INET6,
5128 			&ipif->ipif_v6src_addr, buf2, sizeof (buf2)),
5129 		    inet_ntop(AF_INET6,
5130 			&ipif->ipif_v6subnet, buf3, sizeof (buf3)),
5131 		    inet_ntop(AF_INET6,
5132 			&ipif->ipif_v6net_mask, buf4, sizeof (buf4)),
5133 		    inet_ntop(AF_INET6,
5134 			&ipif->ipif_v6brd_addr, buf5, sizeof (buf5)),
5135 		    inet_ntop(AF_INET6,
5136 			&ipif->ipif_v6pp_dst_addr,
5137 			buf6, sizeof (buf6)));
5138 		}
5139 	}
5140 	rw_exit(&ill_g_lock);
5141 	return (0);
5142 }
5143 
5144 /*
5145  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
5146  * driver.  We construct best guess defaults for lower level information that
5147  * we need.  If an interface is brought up without injection of any overriding
5148  * information from outside, we have to be ready to go with these defaults.
5149  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
5150  * we primarely want the dl_provider_style.
5151  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
5152  * at which point we assume the other part of the information is valid.
5153  */
5154 void
5155 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
5156 {
5157 	uchar_t		*brdcst_addr;
5158 	uint_t		brdcst_addr_length, phys_addr_length;
5159 	t_scalar_t	sap_length;
5160 	dl_info_ack_t	*dlia;
5161 	ip_m_t		*ipm;
5162 	dl_qos_cl_sel1_t *sel1;
5163 
5164 	ASSERT(IAM_WRITER_ILL(ill));
5165 
5166 	/*
5167 	 * Till the ill is fully up ILL_CHANGING will be set and
5168 	 * the ill is not globally visible. So no need for a lock.
5169 	 */
5170 	dlia = (dl_info_ack_t *)mp->b_rptr;
5171 	ill->ill_mactype = dlia->dl_mac_type;
5172 
5173 	ipm = ip_m_lookup(dlia->dl_mac_type);
5174 	if (ipm == NULL) {
5175 		ipm = ip_m_lookup(DL_OTHER);
5176 		ASSERT(ipm != NULL);
5177 	}
5178 	ill->ill_media = ipm;
5179 
5180 	/*
5181 	 * When the new DLPI stuff is ready we'll pull lengths
5182 	 * from dlia.
5183 	 */
5184 	if (dlia->dl_version == DL_VERSION_2) {
5185 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
5186 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
5187 		    brdcst_addr_length);
5188 		if (brdcst_addr == NULL) {
5189 			brdcst_addr_length = 0;
5190 		}
5191 		sap_length = dlia->dl_sap_length;
5192 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
5193 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
5194 		    brdcst_addr_length, sap_length, phys_addr_length));
5195 	} else {
5196 		brdcst_addr_length = 6;
5197 		brdcst_addr = ip_six_byte_all_ones;
5198 		sap_length = -2;
5199 		phys_addr_length = brdcst_addr_length;
5200 	}
5201 
5202 	ill->ill_bcast_addr_length = brdcst_addr_length;
5203 	ill->ill_phys_addr_length = phys_addr_length;
5204 	ill->ill_sap_length = sap_length;
5205 	ill->ill_max_frag = dlia->dl_max_sdu;
5206 	ill->ill_max_mtu = ill->ill_max_frag;
5207 
5208 	ill->ill_type = ipm->ip_m_type;
5209 
5210 	if (!ill->ill_dlpi_style_set) {
5211 		if (dlia->dl_provider_style == DL_STYLE2)
5212 			ill->ill_needs_attach = 1;
5213 
5214 		/*
5215 		 * Allocate the first ipif on this ill. We don't delay it
5216 		 * further as ioctl handling assumes atleast one ipif to
5217 		 * be present.
5218 		 *
5219 		 * At this point we don't know whether the ill is v4 or v6.
5220 		 * We will know this whan the SIOCSLIFNAME happens and
5221 		 * the correct value for ill_isv6 will be assigned in
5222 		 * ipif_set_values(). We need to hold the ill lock and
5223 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
5224 		 * the wakeup.
5225 		 */
5226 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
5227 		    dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE);
5228 		mutex_enter(&ill->ill_lock);
5229 		ASSERT(ill->ill_dlpi_style_set == 0);
5230 		ill->ill_dlpi_style_set = 1;
5231 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
5232 		cv_broadcast(&ill->ill_cv);
5233 		mutex_exit(&ill->ill_lock);
5234 		freemsg(mp);
5235 		return;
5236 	}
5237 	ASSERT(ill->ill_ipif != NULL);
5238 	/*
5239 	 * We know whether it is IPv4 or IPv6 now, as this is the
5240 	 * second DL_INFO_ACK we are recieving in response to the
5241 	 * DL_INFO_REQ sent in ipif_set_values.
5242 	 */
5243 	if (ill->ill_isv6)
5244 		ill->ill_sap = IP6_DL_SAP;
5245 	else
5246 		ill->ill_sap = IP_DL_SAP;
5247 	/*
5248 	 * Set ipif_mtu which is used to set the IRE's
5249 	 * ire_max_frag value. The driver could have sent
5250 	 * a different mtu from what it sent last time. No
5251 	 * need to call ipif_mtu_change because IREs have
5252 	 * not yet been created.
5253 	 */
5254 	ill->ill_ipif->ipif_mtu = ill->ill_max_mtu;
5255 	/*
5256 	 * Clear all the flags that were set based on ill_bcast_addr_length
5257 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
5258 	 * changed now and we need to re-evaluate.
5259 	 */
5260 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
5261 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
5262 
5263 	/*
5264 	 * Free ill_resolver_mp and ill_bcast_mp as things could have
5265 	 * changed now.
5266 	 */
5267 	if (ill->ill_bcast_addr_length == 0) {
5268 		if (ill->ill_resolver_mp != NULL)
5269 			freemsg(ill->ill_resolver_mp);
5270 		if (ill->ill_bcast_mp != NULL)
5271 			freemsg(ill->ill_bcast_mp);
5272 		if (ill->ill_flags & ILLF_XRESOLV)
5273 			ill->ill_net_type = IRE_IF_RESOLVER;
5274 		else
5275 			ill->ill_net_type = IRE_IF_NORESOLVER;
5276 		ill->ill_resolver_mp = ill_dlur_gen(NULL,
5277 		    ill->ill_phys_addr_length,
5278 		    ill->ill_sap,
5279 		    ill->ill_sap_length);
5280 		ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp);
5281 
5282 		if (ill->ill_isv6)
5283 			/*
5284 			 * Note: xresolv interfaces will eventually need NOARP
5285 			 * set here as well, but that will require those
5286 			 * external resolvers to have some knowledge of
5287 			 * that flag and act appropriately. Not to be changed
5288 			 * at present.
5289 			 */
5290 			ill->ill_flags |= ILLF_NONUD;
5291 		else
5292 			ill->ill_flags |= ILLF_NOARP;
5293 
5294 		if (ill->ill_phys_addr_length == 0) {
5295 			if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
5296 				ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
5297 				ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL;
5298 			} else {
5299 				/* pt-pt supports multicast. */
5300 				ill->ill_flags |= ILLF_MULTICAST;
5301 				ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
5302 			}
5303 		}
5304 	} else {
5305 		ill->ill_net_type = IRE_IF_RESOLVER;
5306 		if (ill->ill_bcast_mp != NULL)
5307 			freemsg(ill->ill_bcast_mp);
5308 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
5309 		    ill->ill_bcast_addr_length, ill->ill_sap,
5310 		    ill->ill_sap_length);
5311 		/*
5312 		 * Later detect lack of DLPI driver multicast
5313 		 * capability by catching DL_ENABMULTI errors in
5314 		 * ip_rput_dlpi.
5315 		 */
5316 		ill->ill_flags |= ILLF_MULTICAST;
5317 		if (!ill->ill_isv6)
5318 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
5319 	}
5320 	/* By default an interface does not support any CoS marking */
5321 	ill->ill_flags &= ~ILLF_COS_ENABLED;
5322 
5323 	/*
5324 	 * If we get QoS information in DL_INFO_ACK, the device supports
5325 	 * some form of CoS marking, set ILLF_COS_ENABLED.
5326 	 */
5327 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
5328 	    dlia->dl_qos_length);
5329 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
5330 		ill->ill_flags |= ILLF_COS_ENABLED;
5331 	}
5332 
5333 	/* Clear any previous error indication. */
5334 	ill->ill_error = 0;
5335 	freemsg(mp);
5336 }
5337 
5338 /*
5339  * Perform various checks to verify that an address would make sense as a
5340  * local, remote, or subnet interface address.
5341  */
5342 static boolean_t
5343 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
5344 {
5345 	ipaddr_t	net_mask;
5346 
5347 	/*
5348 	 * Don't allow all zeroes, all ones or experimental address, but allow
5349 	 * all ones netmask.
5350 	 */
5351 	if ((net_mask = ip_net_mask(addr)) == 0)
5352 		return (B_FALSE);
5353 	/* A given netmask overrides the "guess" netmask */
5354 	if (subnet_mask != 0)
5355 		net_mask = subnet_mask;
5356 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
5357 	    (addr == (addr | ~net_mask)))) {
5358 		return (B_FALSE);
5359 	}
5360 	if (CLASSD(addr))
5361 		return (B_FALSE);
5362 
5363 	return (B_TRUE);
5364 }
5365 
5366 /*
5367  * ipif_lookup_group
5368  * Returns held ipif
5369  */
5370 ipif_t *
5371 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid)
5372 {
5373 	ire_t	*ire;
5374 	ipif_t	*ipif;
5375 
5376 	ire = ire_lookup_multi(group, zoneid);
5377 	if (ire == NULL)
5378 		return (NULL);
5379 	ipif = ire->ire_ipif;
5380 	ipif_refhold(ipif);
5381 	ire_refrele(ire);
5382 	return (ipif);
5383 }
5384 
5385 /*
5386  * Look for an ipif with the specified interface address and destination.
5387  * The destination address is used only for matching point-to-point interfaces.
5388  */
5389 ipif_t *
5390 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp,
5391     ipsq_func_t func, int *error)
5392 {
5393 	ipif_t	*ipif;
5394 	ill_t	*ill;
5395 	ill_walk_context_t ctx;
5396 	ipsq_t	*ipsq;
5397 
5398 	if (error != NULL)
5399 		*error = 0;
5400 
5401 	/*
5402 	 * First match all the point-to-point interfaces
5403 	 * before looking at non-point-to-point interfaces.
5404 	 * This is done to avoid returning non-point-to-point
5405 	 * ipif instead of unnumbered point-to-point ipif.
5406 	 */
5407 	rw_enter(&ill_g_lock, RW_READER);
5408 	ill = ILL_START_WALK_V4(&ctx);
5409 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5410 		GRAB_CONN_LOCK(q);
5411 		mutex_enter(&ill->ill_lock);
5412 		for (ipif = ill->ill_ipif; ipif != NULL;
5413 		    ipif = ipif->ipif_next) {
5414 			/* Allow the ipif to be down */
5415 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
5416 			    (ipif->ipif_lcl_addr == if_addr) &&
5417 			    (ipif->ipif_pp_dst_addr == dst)) {
5418 				/*
5419 				 * The block comment at the start of ipif_down
5420 				 * explains the use of the macros used below
5421 				 */
5422 				if (IPIF_CAN_LOOKUP(ipif)) {
5423 					ipif_refhold_locked(ipif);
5424 					mutex_exit(&ill->ill_lock);
5425 					RELEASE_CONN_LOCK(q);
5426 					rw_exit(&ill_g_lock);
5427 					return (ipif);
5428 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5429 					ipsq = ill->ill_phyint->phyint_ipsq;
5430 					mutex_enter(&ipsq->ipsq_lock);
5431 					mutex_exit(&ill->ill_lock);
5432 					rw_exit(&ill_g_lock);
5433 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5434 						ill);
5435 					mutex_exit(&ipsq->ipsq_lock);
5436 					RELEASE_CONN_LOCK(q);
5437 					*error = EINPROGRESS;
5438 					return (NULL);
5439 				}
5440 			}
5441 		}
5442 		mutex_exit(&ill->ill_lock);
5443 		RELEASE_CONN_LOCK(q);
5444 	}
5445 	rw_exit(&ill_g_lock);
5446 
5447 	/* lookup the ipif based on interface address */
5448 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error);
5449 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
5450 	return (ipif);
5451 }
5452 
5453 /*
5454  * Look for an ipif with the specified address. For point-point links
5455  * we look for matches on either the destination address and the local
5456  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
5457  * is set.
5458  * Matches on a specific ill if match_ill is set.
5459  */
5460 ipif_t *
5461 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q,
5462     mblk_t *mp, ipsq_func_t func, int *error)
5463 {
5464 	ipif_t  *ipif;
5465 	ill_t   *ill;
5466 	boolean_t ptp = B_FALSE;
5467 	ipsq_t	*ipsq;
5468 	ill_walk_context_t	ctx;
5469 
5470 	if (error != NULL)
5471 		*error = 0;
5472 
5473 	rw_enter(&ill_g_lock, RW_READER);
5474 	/*
5475 	 * Repeat twice, first based on local addresses and
5476 	 * next time for pointopoint.
5477 	 */
5478 repeat:
5479 	ill = ILL_START_WALK_V4(&ctx);
5480 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
5481 		if (match_ill != NULL && ill != match_ill) {
5482 			continue;
5483 		}
5484 		GRAB_CONN_LOCK(q);
5485 		mutex_enter(&ill->ill_lock);
5486 		for (ipif = ill->ill_ipif; ipif != NULL;
5487 		    ipif = ipif->ipif_next) {
5488 			if (zoneid != ALL_ZONES &&
5489 			    zoneid != ipif->ipif_zoneid &&
5490 			    ipif->ipif_zoneid != ALL_ZONES)
5491 				continue;
5492 			/* Allow the ipif to be down */
5493 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
5494 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
5495 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
5496 			    (ipif->ipif_pp_dst_addr == addr))) {
5497 				/*
5498 				 * The block comment at the start of ipif_down
5499 				 * explains the use of the macros used below
5500 				 */
5501 				if (IPIF_CAN_LOOKUP(ipif)) {
5502 					ipif_refhold_locked(ipif);
5503 					mutex_exit(&ill->ill_lock);
5504 					RELEASE_CONN_LOCK(q);
5505 					rw_exit(&ill_g_lock);
5506 					return (ipif);
5507 				} else if (IPIF_CAN_WAIT(ipif, q)) {
5508 					ipsq = ill->ill_phyint->phyint_ipsq;
5509 					mutex_enter(&ipsq->ipsq_lock);
5510 					mutex_exit(&ill->ill_lock);
5511 					rw_exit(&ill_g_lock);
5512 					ipsq_enq(ipsq, q, mp, func, NEW_OP,
5513 						ill);
5514 					mutex_exit(&ipsq->ipsq_lock);
5515 					RELEASE_CONN_LOCK(q);
5516 					*error = EINPROGRESS;
5517 					return (NULL);
5518 				}
5519 			}
5520 		}
5521 		mutex_exit(&ill->ill_lock);
5522 		RELEASE_CONN_LOCK(q);
5523 	}
5524 
5525 	/* Now try the ptp case */
5526 	if (ptp) {
5527 		rw_exit(&ill_g_lock);
5528 		if (error != NULL)
5529 			*error = ENXIO;
5530 		return (NULL);
5531 	}
5532 	ptp = B_TRUE;
5533 	goto repeat;
5534 }
5535 
5536 /*
5537  * Look for an ipif that matches the specified remote address i.e. the
5538  * ipif that would receive the specified packet.
5539  * First look for directly connected interfaces and then do a recursive
5540  * IRE lookup and pick the first ipif corresponding to the source address in the
5541  * ire.
5542  * Returns: held ipif
5543  */
5544 ipif_t *
5545 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
5546 {
5547 	ipif_t	*ipif;
5548 	ire_t	*ire;
5549 
5550 	ASSERT(!ill->ill_isv6);
5551 
5552 	/*
5553 	 * Someone could be changing this ipif currently or change it
5554 	 * after we return this. Thus  a few packets could use the old
5555 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
5556 	 * will atomically be updated or cleaned up with the new value
5557 	 * Thus we don't need a lock to check the flags or other attrs below.
5558 	 */
5559 	mutex_enter(&ill->ill_lock);
5560 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5561 		if (!IPIF_CAN_LOOKUP(ipif))
5562 			continue;
5563 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
5564 		    ipif->ipif_zoneid != ALL_ZONES)
5565 			continue;
5566 		/* Allow the ipif to be down */
5567 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
5568 			if ((ipif->ipif_pp_dst_addr == addr) ||
5569 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
5570 			    ipif->ipif_lcl_addr == addr)) {
5571 				ipif_refhold_locked(ipif);
5572 				mutex_exit(&ill->ill_lock);
5573 				return (ipif);
5574 			}
5575 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
5576 			ipif_refhold_locked(ipif);
5577 			mutex_exit(&ill->ill_lock);
5578 			return (ipif);
5579 		}
5580 	}
5581 	mutex_exit(&ill->ill_lock);
5582 	ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid,
5583 	    NULL, MATCH_IRE_RECURSIVE);
5584 	if (ire != NULL) {
5585 		/*
5586 		 * The callers of this function wants to know the
5587 		 * interface on which they have to send the replies
5588 		 * back. For IRE_CACHES that have ire_stq and ire_ipif
5589 		 * derived from different ills, we really don't care
5590 		 * what we return here.
5591 		 */
5592 		ipif = ire->ire_ipif;
5593 		if (ipif != NULL) {
5594 			ipif_refhold(ipif);
5595 			ire_refrele(ire);
5596 			return (ipif);
5597 		}
5598 		ire_refrele(ire);
5599 	}
5600 	/* Pick the first interface */
5601 	ipif = ipif_get_next_ipif(NULL, ill);
5602 	return (ipif);
5603 }
5604 
5605 /*
5606  * This func does not prevent refcnt from increasing. But if
5607  * the caller has taken steps to that effect, then this func
5608  * can be used to determine whether the ill has become quiescent
5609  */
5610 boolean_t
5611 ill_is_quiescent(ill_t *ill)
5612 {
5613 	ipif_t	*ipif;
5614 
5615 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5616 
5617 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5618 		if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5619 			return (B_FALSE);
5620 	}
5621 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 ||
5622 	    ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 ||
5623 	    ill->ill_mrtun_refcnt != 0)
5624 		return (B_FALSE);
5625 	return (B_TRUE);
5626 }
5627 
5628 /*
5629  * This func does not prevent refcnt from increasing. But if
5630  * the caller has taken steps to that effect, then this func
5631  * can be used to determine whether the ipif has become quiescent
5632  */
5633 static boolean_t
5634 ipif_is_quiescent(ipif_t *ipif)
5635 {
5636 	ill_t *ill;
5637 
5638 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5639 
5640 	if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0)
5641 		return (B_FALSE);
5642 
5643 	ill = ipif->ipif_ill;
5644 	if (ill->ill_ipif_up_count != 0 || ill->ill_logical_down)
5645 		return (B_TRUE);
5646 
5647 	/* This is the last ipif going down or being deleted on this ill */
5648 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0)
5649 		return (B_FALSE);
5650 
5651 	return (B_TRUE);
5652 }
5653 
5654 /*
5655  * This func does not prevent refcnt from increasing. But if
5656  * the caller has taken steps to that effect, then this func
5657  * can be used to determine whether the ipifs marked with IPIF_MOVING
5658  * have become quiescent and can be moved in a failover/failback.
5659  */
5660 static ipif_t *
5661 ill_quiescent_to_move(ill_t *ill)
5662 {
5663 	ipif_t  *ipif;
5664 
5665 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5666 
5667 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
5668 		if (ipif->ipif_state_flags & IPIF_MOVING) {
5669 			if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) {
5670 				return (ipif);
5671 			}
5672 		}
5673 	}
5674 	return (NULL);
5675 }
5676 
5677 /*
5678  * The ipif/ill/ire has been refreled. Do the tail processing.
5679  * Determine if the ipif or ill in question has become quiescent and if so
5680  * wakeup close and/or restart any queued pending ioctl that is waiting
5681  * for the ipif_down (or ill_down)
5682  */
5683 void
5684 ipif_ill_refrele_tail(ill_t *ill)
5685 {
5686 	mblk_t	*mp;
5687 	conn_t	*connp;
5688 	ipsq_t	*ipsq;
5689 	ipif_t	*ipif;
5690 
5691 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5692 
5693 	if ((ill->ill_state_flags & ILL_CONDEMNED) &&
5694 	    ill_is_quiescent(ill)) {
5695 		/* ill_close may be waiting */
5696 		cv_broadcast(&ill->ill_cv);
5697 	}
5698 
5699 	/* ipsq can't change because ill_lock  is held */
5700 	ipsq = ill->ill_phyint->phyint_ipsq;
5701 	if (ipsq->ipsq_waitfor == 0) {
5702 		/* Not waiting for anything, just return. */
5703 		mutex_exit(&ill->ill_lock);
5704 		return;
5705 	}
5706 	ASSERT(ipsq->ipsq_pending_mp != NULL &&
5707 		ipsq->ipsq_pending_ipif != NULL);
5708 	/*
5709 	 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF.
5710 	 * Last ipif going down needs to down the ill, so ill_ire_cnt must
5711 	 * be zero for restarting an ioctl that ends up downing the ill.
5712 	 */
5713 	ipif = ipsq->ipsq_pending_ipif;
5714 	if (ipif->ipif_ill != ill) {
5715 		/* The ioctl is pending on some other ill. */
5716 		mutex_exit(&ill->ill_lock);
5717 		return;
5718 	}
5719 
5720 	switch (ipsq->ipsq_waitfor) {
5721 	case IPIF_DOWN:
5722 	case IPIF_FREE:
5723 		if (!ipif_is_quiescent(ipif)) {
5724 			mutex_exit(&ill->ill_lock);
5725 			return;
5726 		}
5727 		break;
5728 
5729 	case ILL_DOWN:
5730 	case ILL_FREE:
5731 		/*
5732 		 * case ILL_FREE arises only for loopback. otherwise ill_delete
5733 		 * waits synchronously in ip_close, and no message is queued in
5734 		 * ipsq_pending_mp at all in this case
5735 		 */
5736 		if (!ill_is_quiescent(ill)) {
5737 			mutex_exit(&ill->ill_lock);
5738 			return;
5739 		}
5740 
5741 		break;
5742 
5743 	case ILL_MOVE_OK:
5744 		if (ill_quiescent_to_move(ill) != NULL) {
5745 			mutex_exit(&ill->ill_lock);
5746 			return;
5747 		}
5748 
5749 		break;
5750 	default:
5751 		cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n",
5752 		    (void *)ipsq, ipsq->ipsq_waitfor);
5753 	}
5754 
5755 	/*
5756 	 * Incr refcnt for the qwriter_ip call below which
5757 	 * does a refrele
5758 	 */
5759 	ill_refhold_locked(ill);
5760 	mutex_exit(&ill->ill_lock);
5761 
5762 	mp = ipsq_pending_mp_get(ipsq, &connp);
5763 	ASSERT(mp != NULL);
5764 
5765 	switch (mp->b_datap->db_type) {
5766 	case M_ERROR:
5767 	case M_HANGUP:
5768 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp,
5769 		    ipif_all_down_tail, CUR_OP, B_TRUE);
5770 		return;
5771 
5772 	case M_IOCTL:
5773 	case M_IOCDATA:
5774 		(void) qwriter_ip(NULL, ill,
5775 		    (connp != NULL ? CONNP_TO_WQ(connp) : ill->ill_wq), mp,
5776 		    ip_reprocess_ioctl, CUR_OP, B_TRUE);
5777 		return;
5778 
5779 	default:
5780 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5781 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5782 	}
5783 }
5784 
5785 #ifdef ILL_DEBUG
5786 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5787 void
5788 th_trace_rrecord(th_trace_t *th_trace)
5789 {
5790 	tr_buf_t *tr_buf;
5791 	uint_t lastref;
5792 
5793 	lastref = th_trace->th_trace_lastref;
5794 	lastref++;
5795 	if (lastref == TR_BUF_MAX)
5796 		lastref = 0;
5797 	th_trace->th_trace_lastref = lastref;
5798 	tr_buf = &th_trace->th_trbuf[lastref];
5799 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH);
5800 }
5801 
5802 th_trace_t *
5803 th_trace_ipif_lookup(ipif_t *ipif)
5804 {
5805 	int bucket_id;
5806 	th_trace_t *th_trace;
5807 
5808 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5809 
5810 	bucket_id = IP_TR_HASH(curthread);
5811 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5812 
5813 	for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL;
5814 	    th_trace = th_trace->th_next) {
5815 		if (th_trace->th_id == curthread)
5816 			return (th_trace);
5817 	}
5818 	return (NULL);
5819 }
5820 
5821 void
5822 ipif_trace_ref(ipif_t *ipif)
5823 {
5824 	int bucket_id;
5825 	th_trace_t *th_trace;
5826 
5827 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5828 
5829 	if (ipif->ipif_trace_disable)
5830 		return;
5831 
5832 	/*
5833 	 * Attempt to locate the trace buffer for the curthread.
5834 	 * If it does not exist, then allocate a new trace buffer
5835 	 * and link it in list of trace bufs for this ipif, at the head
5836 	 */
5837 	th_trace = th_trace_ipif_lookup(ipif);
5838 	if (th_trace == NULL) {
5839 		bucket_id = IP_TR_HASH(curthread);
5840 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5841 		    KM_NOSLEEP);
5842 		if (th_trace == NULL) {
5843 			ipif->ipif_trace_disable = B_TRUE;
5844 			ipif_trace_cleanup(ipif);
5845 			return;
5846 		}
5847 		th_trace->th_id = curthread;
5848 		th_trace->th_next = ipif->ipif_trace[bucket_id];
5849 		th_trace->th_prev = &ipif->ipif_trace[bucket_id];
5850 		if (th_trace->th_next != NULL)
5851 			th_trace->th_next->th_prev = &th_trace->th_next;
5852 		ipif->ipif_trace[bucket_id] = th_trace;
5853 	}
5854 	ASSERT(th_trace->th_refcnt >= 0 &&
5855 		th_trace->th_refcnt < TR_BUF_MAX -1);
5856 	th_trace->th_refcnt++;
5857 	th_trace_rrecord(th_trace);
5858 }
5859 
5860 void
5861 ipif_untrace_ref(ipif_t *ipif)
5862 {
5863 	th_trace_t *th_trace;
5864 
5865 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5866 
5867 	if (ipif->ipif_trace_disable)
5868 		return;
5869 	th_trace = th_trace_ipif_lookup(ipif);
5870 	ASSERT(th_trace != NULL);
5871 	ASSERT(th_trace->th_refcnt > 0);
5872 
5873 	th_trace->th_refcnt--;
5874 	th_trace_rrecord(th_trace);
5875 }
5876 
5877 th_trace_t *
5878 th_trace_ill_lookup(ill_t *ill)
5879 {
5880 	th_trace_t *th_trace;
5881 	int bucket_id;
5882 
5883 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5884 
5885 	bucket_id = IP_TR_HASH(curthread);
5886 	ASSERT(bucket_id < IP_TR_HASH_MAX);
5887 
5888 	for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL;
5889 	    th_trace = th_trace->th_next) {
5890 		if (th_trace->th_id == curthread)
5891 			return (th_trace);
5892 	}
5893 	return (NULL);
5894 }
5895 
5896 void
5897 ill_trace_ref(ill_t *ill)
5898 {
5899 	int bucket_id;
5900 	th_trace_t *th_trace;
5901 
5902 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5903 	if (ill->ill_trace_disable)
5904 		return;
5905 	/*
5906 	 * Attempt to locate the trace buffer for the curthread.
5907 	 * If it does not exist, then allocate a new trace buffer
5908 	 * and link it in list of trace bufs for this ill, at the head
5909 	 */
5910 	th_trace = th_trace_ill_lookup(ill);
5911 	if (th_trace == NULL) {
5912 		bucket_id = IP_TR_HASH(curthread);
5913 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
5914 		    KM_NOSLEEP);
5915 		if (th_trace == NULL) {
5916 			ill->ill_trace_disable = B_TRUE;
5917 			ill_trace_cleanup(ill);
5918 			return;
5919 		}
5920 		th_trace->th_id = curthread;
5921 		th_trace->th_next = ill->ill_trace[bucket_id];
5922 		th_trace->th_prev = &ill->ill_trace[bucket_id];
5923 		if (th_trace->th_next != NULL)
5924 			th_trace->th_next->th_prev = &th_trace->th_next;
5925 		ill->ill_trace[bucket_id] = th_trace;
5926 	}
5927 	ASSERT(th_trace->th_refcnt >= 0 &&
5928 		th_trace->th_refcnt < TR_BUF_MAX - 1);
5929 
5930 	th_trace->th_refcnt++;
5931 	th_trace_rrecord(th_trace);
5932 }
5933 
5934 void
5935 ill_untrace_ref(ill_t *ill)
5936 {
5937 	th_trace_t *th_trace;
5938 
5939 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5940 
5941 	if (ill->ill_trace_disable)
5942 		return;
5943 	th_trace = th_trace_ill_lookup(ill);
5944 	ASSERT(th_trace != NULL);
5945 	ASSERT(th_trace->th_refcnt > 0);
5946 
5947 	th_trace->th_refcnt--;
5948 	th_trace_rrecord(th_trace);
5949 }
5950 
5951 /*
5952  * Verify that this thread has no refs to the ipif and free
5953  * the trace buffers
5954  */
5955 /* ARGSUSED */
5956 void
5957 ipif_thread_exit(ipif_t *ipif, void *dummy)
5958 {
5959 	th_trace_t *th_trace;
5960 
5961 	mutex_enter(&ipif->ipif_ill->ill_lock);
5962 
5963 	th_trace = th_trace_ipif_lookup(ipif);
5964 	if (th_trace == NULL) {
5965 		mutex_exit(&ipif->ipif_ill->ill_lock);
5966 		return;
5967 	}
5968 	ASSERT(th_trace->th_refcnt == 0);
5969 	/* unlink th_trace and free it */
5970 	*th_trace->th_prev = th_trace->th_next;
5971 	if (th_trace->th_next != NULL)
5972 		th_trace->th_next->th_prev = th_trace->th_prev;
5973 	th_trace->th_next = NULL;
5974 	th_trace->th_prev = NULL;
5975 	kmem_free(th_trace, sizeof (th_trace_t));
5976 
5977 	mutex_exit(&ipif->ipif_ill->ill_lock);
5978 }
5979 
5980 /*
5981  * Verify that this thread has no refs to the ill and free
5982  * the trace buffers
5983  */
5984 /* ARGSUSED */
5985 void
5986 ill_thread_exit(ill_t *ill, void *dummy)
5987 {
5988 	th_trace_t *th_trace;
5989 
5990 	mutex_enter(&ill->ill_lock);
5991 
5992 	th_trace = th_trace_ill_lookup(ill);
5993 	if (th_trace == NULL) {
5994 		mutex_exit(&ill->ill_lock);
5995 		return;
5996 	}
5997 	ASSERT(th_trace->th_refcnt == 0);
5998 	/* unlink th_trace and free it */
5999 	*th_trace->th_prev = th_trace->th_next;
6000 	if (th_trace->th_next != NULL)
6001 		th_trace->th_next->th_prev = th_trace->th_prev;
6002 	th_trace->th_next = NULL;
6003 	th_trace->th_prev = NULL;
6004 	kmem_free(th_trace, sizeof (th_trace_t));
6005 
6006 	mutex_exit(&ill->ill_lock);
6007 }
6008 #endif
6009 
6010 #ifdef ILL_DEBUG
6011 void
6012 ip_thread_exit(void)
6013 {
6014 	ill_t	*ill;
6015 	ipif_t	*ipif;
6016 	ill_walk_context_t	ctx;
6017 
6018 	rw_enter(&ill_g_lock, RW_READER);
6019 	ill = ILL_START_WALK_ALL(&ctx);
6020 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
6021 		for (ipif = ill->ill_ipif; ipif != NULL;
6022 		    ipif = ipif->ipif_next) {
6023 			ipif_thread_exit(ipif, NULL);
6024 		}
6025 		ill_thread_exit(ill, NULL);
6026 	}
6027 	rw_exit(&ill_g_lock);
6028 
6029 	ire_walk(ire_thread_exit, NULL);
6030 	ndp_walk_impl(NULL, nce_thread_exit, NULL, B_FALSE);
6031 }
6032 
6033 /*
6034  * Called when ipif is unplumbed or when memory alloc fails
6035  */
6036 void
6037 ipif_trace_cleanup(ipif_t *ipif)
6038 {
6039 	int	i;
6040 	th_trace_t	*th_trace;
6041 	th_trace_t	*th_trace_next;
6042 
6043 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6044 		for (th_trace = ipif->ipif_trace[i]; th_trace != NULL;
6045 		    th_trace = th_trace_next) {
6046 			th_trace_next = th_trace->th_next;
6047 			kmem_free(th_trace, sizeof (th_trace_t));
6048 		}
6049 		ipif->ipif_trace[i] = NULL;
6050 	}
6051 }
6052 
6053 /*
6054  * Called when ill is unplumbed or when memory alloc fails
6055  */
6056 void
6057 ill_trace_cleanup(ill_t *ill)
6058 {
6059 	int	i;
6060 	th_trace_t	*th_trace;
6061 	th_trace_t	*th_trace_next;
6062 
6063 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
6064 		for (th_trace = ill->ill_trace[i]; th_trace != NULL;
6065 		    th_trace = th_trace_next) {
6066 			th_trace_next = th_trace->th_next;
6067 			kmem_free(th_trace, sizeof (th_trace_t));
6068 		}
6069 		ill->ill_trace[i] = NULL;
6070 	}
6071 }
6072 
6073 #else
6074 void ip_thread_exit(void) {}
6075 #endif
6076 
6077 void
6078 ipif_refhold_locked(ipif_t *ipif)
6079 {
6080 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6081 	ipif->ipif_refcnt++;
6082 	IPIF_TRACE_REF(ipif);
6083 }
6084 
6085 void
6086 ipif_refhold(ipif_t *ipif)
6087 {
6088 	ill_t	*ill;
6089 
6090 	ill = ipif->ipif_ill;
6091 	mutex_enter(&ill->ill_lock);
6092 	ipif->ipif_refcnt++;
6093 	IPIF_TRACE_REF(ipif);
6094 	mutex_exit(&ill->ill_lock);
6095 }
6096 
6097 /*
6098  * Must not be called while holding any locks. Otherwise if this is
6099  * the last reference to be released there is a chance of recursive mutex
6100  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
6101  * to restart an ioctl.
6102  */
6103 void
6104 ipif_refrele(ipif_t *ipif)
6105 {
6106 	ill_t	*ill;
6107 
6108 	ill = ipif->ipif_ill;
6109 
6110 	mutex_enter(&ill->ill_lock);
6111 	ASSERT(ipif->ipif_refcnt != 0);
6112 	ipif->ipif_refcnt--;
6113 	IPIF_UNTRACE_REF(ipif);
6114 	if (ipif->ipif_refcnt != 0) {
6115 		mutex_exit(&ill->ill_lock);
6116 		return;
6117 	}
6118 
6119 	/* Drops the ill_lock */
6120 	ipif_ill_refrele_tail(ill);
6121 }
6122 
6123 ipif_t *
6124 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
6125 {
6126 	ipif_t	*ipif;
6127 
6128 	mutex_enter(&ill->ill_lock);
6129 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
6130 	    ipif != NULL; ipif = ipif->ipif_next) {
6131 		if (!IPIF_CAN_LOOKUP(ipif))
6132 			continue;
6133 		ipif_refhold_locked(ipif);
6134 		mutex_exit(&ill->ill_lock);
6135 		return (ipif);
6136 	}
6137 	mutex_exit(&ill->ill_lock);
6138 	return (NULL);
6139 }
6140 
6141 /*
6142  * TODO: make this table extendible at run time
6143  * Return a pointer to the mac type info for 'mac_type'
6144  */
6145 static ip_m_t *
6146 ip_m_lookup(t_uscalar_t mac_type)
6147 {
6148 	ip_m_t	*ipm;
6149 
6150 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
6151 		if (ipm->ip_m_mac_type == mac_type)
6152 			return (ipm);
6153 	return (NULL);
6154 }
6155 
6156 /*
6157  * ip_rt_add is called to add an IPv4 route to the forwarding table.
6158  * ipif_arg is passed in to associate it with the correct interface.
6159  * We may need to restart this operation if the ipif cannot be looked up
6160  * due to an exclusive operation that is currently in progress. The restart
6161  * entry point is specified by 'func'
6162  */
6163 int
6164 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6165     ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6166     ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp,
6167     ipsq_func_t func, struct rtsa_s *sp)
6168 {
6169 	ire_t	*ire;
6170 	ire_t	*gw_ire = NULL;
6171 	ipif_t	*ipif = NULL;
6172 	boolean_t ipif_refheld = B_FALSE;
6173 	uint_t	type;
6174 	int	match_flags = MATCH_IRE_TYPE;
6175 	int	error;
6176 	tsol_gc_t *gc = NULL;
6177 	tsol_gcgrp_t *gcgrp = NULL;
6178 	boolean_t gcgrp_xtraref = B_FALSE;
6179 
6180 	ip1dbg(("ip_rt_add:"));
6181 
6182 	if (ire_arg != NULL)
6183 		*ire_arg = NULL;
6184 
6185 	/*
6186 	 * If this is the case of RTF_HOST being set, then we set the netmask
6187 	 * to all ones (regardless if one was supplied).
6188 	 */
6189 	if (flags & RTF_HOST)
6190 		mask = IP_HOST_MASK;
6191 
6192 	/*
6193 	 * Prevent routes with a zero gateway from being created (since
6194 	 * interfaces can currently be plumbed and brought up no assigned
6195 	 * address).
6196 	 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0.
6197 	 */
6198 	if (gw_addr == 0 && src_ipif == NULL)
6199 		return (ENETUNREACH);
6200 	/*
6201 	 * Get the ipif, if any, corresponding to the gw_addr
6202 	 */
6203 	if (gw_addr != 0) {
6204 		ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func,
6205 		    &error);
6206 		if (ipif != NULL) {
6207 			if (IS_VNI(ipif->ipif_ill)) {
6208 				ipif_refrele(ipif);
6209 				return (EINVAL);
6210 			}
6211 			ipif_refheld = B_TRUE;
6212 		} else if (error == EINPROGRESS) {
6213 			ip1dbg(("ip_rt_add: null and EINPROGRESS"));
6214 			return (EINPROGRESS);
6215 		} else {
6216 			error = 0;
6217 		}
6218 	}
6219 
6220 	if (ipif != NULL) {
6221 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull"));
6222 		ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
6223 	} else {
6224 		ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null"));
6225 	}
6226 
6227 	/*
6228 	 * GateD will attempt to create routes with a loopback interface
6229 	 * address as the gateway and with RTF_GATEWAY set.  We allow
6230 	 * these routes to be added, but create them as interface routes
6231 	 * since the gateway is an interface address.
6232 	 */
6233 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK))
6234 		flags &= ~RTF_GATEWAY;
6235 
6236 	/*
6237 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6238 	 * and the gateway address provided is one of the system's interface
6239 	 * addresses.  By using the routing socket interface and supplying an
6240 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6241 	 * specifying an interface route to be created is available which uses
6242 	 * the interface index that specifies the outgoing interface rather than
6243 	 * the address of an outgoing interface (which may not be able to
6244 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6245 	 * flag, routes can be specified which not only specify the next-hop to
6246 	 * be used when routing to a certain prefix, but also which outgoing
6247 	 * interface should be used.
6248 	 *
6249 	 * Previously, interfaces would have unique addresses assigned to them
6250 	 * and so the address assigned to a particular interface could be used
6251 	 * to identify a particular interface.  One exception to this was the
6252 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6253 	 *
6254 	 * With the advent of IPv6 and its link-local addresses, this
6255 	 * restriction was relaxed and interfaces could share addresses between
6256 	 * themselves.  In fact, typically all of the link-local interfaces on
6257 	 * an IPv6 node or router will have the same link-local address.  In
6258 	 * order to differentiate between these interfaces, the use of an
6259 	 * interface index is necessary and this index can be carried inside a
6260 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6261 	 * of using the interface index, however, is that all of the ipif's that
6262 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6263 	 * cannot be used to differentiate between ipif's (or logical
6264 	 * interfaces) that belong to the same ill (physical interface).
6265 	 *
6266 	 * For example, in the following case involving IPv4 interfaces and
6267 	 * logical interfaces
6268 	 *
6269 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6270 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6271 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6272 	 *
6273 	 * the ipif's corresponding to each of these interface routes can be
6274 	 * uniquely identified by the "gateway" (actually interface address).
6275 	 *
6276 	 * In this case involving multiple IPv6 default routes to a particular
6277 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6278 	 * default route is of interest:
6279 	 *
6280 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6281 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6282 	 */
6283 
6284 	/* RTF_GATEWAY not set */
6285 	if (!(flags & RTF_GATEWAY)) {
6286 		queue_t	*stq;
6287 		queue_t	*rfq = NULL;
6288 		ill_t	*in_ill = NULL;
6289 
6290 		if (sp != NULL) {
6291 			ip2dbg(("ip_rt_add: gateway security attributes "
6292 			    "cannot be set with interface route\n"));
6293 			if (ipif_refheld)
6294 				ipif_refrele(ipif);
6295 			return (EINVAL);
6296 		}
6297 
6298 		/*
6299 		 * As the interface index specified with the RTA_IFP sockaddr is
6300 		 * the same for all ipif's off of an ill, the matching logic
6301 		 * below uses MATCH_IRE_ILL if such an index was specified.
6302 		 * This means that routes sharing the same prefix when added
6303 		 * using a RTA_IFP sockaddr must have distinct interface
6304 		 * indices (namely, they must be on distinct ill's).
6305 		 *
6306 		 * On the other hand, since the gateway address will usually be
6307 		 * different for each ipif on the system, the matching logic
6308 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6309 		 * route.  This means that interface routes for the same prefix
6310 		 * can be created if they belong to distinct ipif's and if a
6311 		 * RTA_IFP sockaddr is not present.
6312 		 */
6313 		if (ipif_arg != NULL) {
6314 			if (ipif_refheld)  {
6315 				ipif_refrele(ipif);
6316 				ipif_refheld = B_FALSE;
6317 			}
6318 			ipif = ipif_arg;
6319 			match_flags |= MATCH_IRE_ILL;
6320 		} else {
6321 			/*
6322 			 * Check the ipif corresponding to the gw_addr
6323 			 */
6324 			if (ipif == NULL)
6325 				return (ENETUNREACH);
6326 			match_flags |= MATCH_IRE_IPIF;
6327 		}
6328 		ASSERT(ipif != NULL);
6329 		/*
6330 		 * If src_ipif is not NULL, we have to create
6331 		 * an ire with non-null ire_in_ill value
6332 		 */
6333 		if (src_ipif != NULL) {
6334 			in_ill = src_ipif->ipif_ill;
6335 		}
6336 
6337 		/*
6338 		 * We check for an existing entry at this point.
6339 		 *
6340 		 * Since a netmask isn't passed in via the ioctl interface
6341 		 * (SIOCADDRT), we don't check for a matching netmask in that
6342 		 * case.
6343 		 */
6344 		if (!ioctl_msg)
6345 			match_flags |= MATCH_IRE_MASK;
6346 		if (src_ipif != NULL) {
6347 			/* Look up in the special table */
6348 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6349 			    ipif, src_ipif->ipif_ill, match_flags);
6350 		} else {
6351 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6352 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6353 			    NULL, match_flags);
6354 		}
6355 		if (ire != NULL) {
6356 			ire_refrele(ire);
6357 			if (ipif_refheld)
6358 				ipif_refrele(ipif);
6359 			return (EEXIST);
6360 		}
6361 
6362 		if (src_ipif != NULL) {
6363 			/*
6364 			 * Create the special ire for the IRE table
6365 			 * which hangs out of ire_in_ill. This ire
6366 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6367 			 * Thus rfq is non-NULL.
6368 			 */
6369 			rfq = ipif->ipif_rq;
6370 		}
6371 		/* Create the usual interface ires */
6372 
6373 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6374 		    ? ipif->ipif_rq : ipif->ipif_wq;
6375 
6376 		/*
6377 		 * Create a copy of the IRE_LOOPBACK,
6378 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6379 		 * the modified address and netmask.
6380 		 */
6381 		ire = ire_create(
6382 		    (uchar_t *)&dst_addr,
6383 		    (uint8_t *)&mask,
6384 		    (uint8_t *)&ipif->ipif_src_addr,
6385 		    NULL,
6386 		    NULL,
6387 		    &ipif->ipif_mtu,
6388 		    NULL,
6389 		    rfq,
6390 		    stq,
6391 		    ipif->ipif_net_type,
6392 		    ipif->ipif_resolver_mp,
6393 		    ipif,
6394 		    in_ill,
6395 		    0,
6396 		    0,
6397 		    0,
6398 		    flags,
6399 		    &ire_uinfo_null,
6400 		    NULL,
6401 		    NULL);
6402 		if (ire == NULL) {
6403 			if (ipif_refheld)
6404 				ipif_refrele(ipif);
6405 			return (ENOMEM);
6406 		}
6407 
6408 		/*
6409 		 * Some software (for example, GateD and Sun Cluster) attempts
6410 		 * to create (what amount to) IRE_PREFIX routes with the
6411 		 * loopback address as the gateway.  This is primarily done to
6412 		 * set up prefixes with the RTF_REJECT flag set (for example,
6413 		 * when generating aggregate routes.)
6414 		 *
6415 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6416 		 * IRE_LOOPBACK, then we map the request into a
6417 		 * IRE_IF_NORESOLVER.
6418 		 *
6419 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6420 		 * routine, but rather using ire_create() directly.
6421 		 */
6422 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6423 			ire->ire_type = IRE_IF_NORESOLVER;
6424 		error = ire_add(&ire, q, mp, func);
6425 		if (error == 0)
6426 			goto save_ire;
6427 
6428 		/*
6429 		 * In the result of failure, ire_add() will have already
6430 		 * deleted the ire in question, so there is no need to
6431 		 * do that here.
6432 		 */
6433 		if (ipif_refheld)
6434 			ipif_refrele(ipif);
6435 		return (error);
6436 	}
6437 	if (ipif_refheld) {
6438 		ipif_refrele(ipif);
6439 		ipif_refheld = B_FALSE;
6440 	}
6441 
6442 	if (src_ipif != NULL) {
6443 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6444 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6445 		return (EINVAL);
6446 	}
6447 	/*
6448 	 * Get an interface IRE for the specified gateway.
6449 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6450 	 * gateway, it is currently unreachable and we fail the request
6451 	 * accordingly.
6452 	 */
6453 	ipif = ipif_arg;
6454 	if (ipif_arg != NULL)
6455 		match_flags |= MATCH_IRE_ILL;
6456 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6457 	    ALL_ZONES, 0, NULL, match_flags);
6458 	if (gw_ire == NULL)
6459 		return (ENETUNREACH);
6460 
6461 	/*
6462 	 * We create one of three types of IREs as a result of this request
6463 	 * based on the netmask.  A netmask of all ones (which is automatically
6464 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6465 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6466 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6467 	 * destination prefix.
6468 	 */
6469 	if (mask == IP_HOST_MASK)
6470 		type = IRE_HOST;
6471 	else if (mask == 0)
6472 		type = IRE_DEFAULT;
6473 	else
6474 		type = IRE_PREFIX;
6475 
6476 	/* check for a duplicate entry */
6477 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6478 	    NULL, ALL_ZONES, 0, NULL,
6479 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6480 	if (ire != NULL) {
6481 		ire_refrele(gw_ire);
6482 		ire_refrele(ire);
6483 		return (EEXIST);
6484 	}
6485 
6486 	/* Security attribute exists */
6487 	if (sp != NULL) {
6488 		tsol_gcgrp_addr_t ga;
6489 
6490 		/* find or create the gateway credentials group */
6491 		ga.ga_af = AF_INET;
6492 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6493 
6494 		/* we hold reference to it upon success */
6495 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6496 		if (gcgrp == NULL) {
6497 			ire_refrele(gw_ire);
6498 			return (ENOMEM);
6499 		}
6500 
6501 		/*
6502 		 * Create and add the security attribute to the group; a
6503 		 * reference to the group is made upon allocating a new
6504 		 * entry successfully.  If it finds an already-existing
6505 		 * entry for the security attribute in the group, it simply
6506 		 * returns it and no new reference is made to the group.
6507 		 */
6508 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6509 		if (gc == NULL) {
6510 			/* release reference held by gcgrp_lookup */
6511 			GCGRP_REFRELE(gcgrp);
6512 			ire_refrele(gw_ire);
6513 			return (ENOMEM);
6514 		}
6515 	}
6516 
6517 	/* Create the IRE. */
6518 	ire = ire_create(
6519 	    (uchar_t *)&dst_addr,		/* dest address */
6520 	    (uchar_t *)&mask,			/* mask */
6521 	    /* src address assigned by the caller? */
6522 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6523 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6524 	    (uchar_t *)&gw_addr,		/* gateway address */
6525 	    NULL,				/* no in-srcaddress */
6526 	    &gw_ire->ire_max_frag,
6527 	    NULL,				/* no Fast Path header */
6528 	    NULL,				/* no recv-from queue */
6529 	    NULL,				/* no send-to queue */
6530 	    (ushort_t)type,			/* IRE type */
6531 	    NULL,
6532 	    ipif_arg,
6533 	    NULL,
6534 	    0,
6535 	    0,
6536 	    0,
6537 	    flags,
6538 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6539 	    gc,					/* security attribute */
6540 	    NULL);
6541 	/*
6542 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6543 	 * reference to the 'gcgrp'. We can now release the extra reference
6544 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6545 	 */
6546 	if (gcgrp_xtraref)
6547 		GCGRP_REFRELE(gcgrp);
6548 	if (ire == NULL) {
6549 		if (gc != NULL)
6550 			GC_REFRELE(gc);
6551 		ire_refrele(gw_ire);
6552 		return (ENOMEM);
6553 	}
6554 
6555 	/*
6556 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6557 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6558 	 */
6559 
6560 	/* Add the new IRE. */
6561 	error = ire_add(&ire, q, mp, func);
6562 	if (error != 0) {
6563 		/*
6564 		 * In the result of failure, ire_add() will have already
6565 		 * deleted the ire in question, so there is no need to
6566 		 * do that here.
6567 		 */
6568 		ire_refrele(gw_ire);
6569 		return (error);
6570 	}
6571 
6572 	if (flags & RTF_MULTIRT) {
6573 		/*
6574 		 * Invoke the CGTP (multirouting) filtering module
6575 		 * to add the dst address in the filtering database.
6576 		 * Replicated inbound packets coming from that address
6577 		 * will be filtered to discard the duplicates.
6578 		 * It is not necessary to call the CGTP filter hook
6579 		 * when the dst address is a broadcast or multicast,
6580 		 * because an IP source address cannot be a broadcast
6581 		 * or a multicast.
6582 		 */
6583 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6584 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
6585 		if (ire_dst != NULL) {
6586 			ip_cgtp_bcast_add(ire, ire_dst);
6587 			ire_refrele(ire_dst);
6588 			goto save_ire;
6589 		}
6590 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6591 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6592 			    ire->ire_addr,
6593 			    ire->ire_gateway_addr,
6594 			    ire->ire_src_addr,
6595 			    gw_ire->ire_src_addr);
6596 			if (res != 0) {
6597 				ire_refrele(gw_ire);
6598 				ire_delete(ire);
6599 				return (res);
6600 			}
6601 		}
6602 	}
6603 
6604 	/*
6605 	 * Now that the prefix IRE entry has been created, delete any
6606 	 * existing gateway IRE cache entries as well as any IRE caches
6607 	 * using the gateway, and force them to be created through
6608 	 * ip_newroute.
6609 	 */
6610 	if (gc != NULL) {
6611 		ASSERT(gcgrp != NULL);
6612 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
6613 	}
6614 
6615 save_ire:
6616 	if (gw_ire != NULL) {
6617 		ire_refrele(gw_ire);
6618 	}
6619 	/*
6620 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6621 	 * flag. This route is only added and deleted by mipagent.
6622 	 * So, for simplicity of design, we refrain from saving
6623 	 * ires that are created with srcif value. This may change
6624 	 * in future if we find more usage of srcifp feature.
6625 	 */
6626 	if (ipif != NULL && src_ipif == NULL) {
6627 		/*
6628 		 * Save enough information so that we can recreate the IRE if
6629 		 * the interface goes down and then up.  The metrics associated
6630 		 * with the route will be saved as well when rts_setmetrics() is
6631 		 * called after the IRE has been created.  In the case where
6632 		 * memory cannot be allocated, none of this information will be
6633 		 * saved.
6634 		 */
6635 		ipif_save_ire(ipif, ire);
6636 	}
6637 	if (ioctl_msg)
6638 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6639 	if (ire_arg != NULL) {
6640 		/*
6641 		 * Store the ire that was successfully added into where ire_arg
6642 		 * points to so that callers don't have to look it up
6643 		 * themselves (but they are responsible for ire_refrele()ing
6644 		 * the ire when they are finished with it).
6645 		 */
6646 		*ire_arg = ire;
6647 	} else {
6648 		ire_refrele(ire);		/* Held in ire_add */
6649 	}
6650 	if (ipif_refheld)
6651 		ipif_refrele(ipif);
6652 	return (0);
6653 }
6654 
6655 /*
6656  * ip_rt_delete is called to delete an IPv4 route.
6657  * ipif_arg is passed in to associate it with the correct interface.
6658  * src_ipif is passed to associate the incoming interface of the packet.
6659  * We may need to restart this operation if the ipif cannot be looked up
6660  * due to an exclusive operation that is currently in progress. The restart
6661  * entry point is specified by 'func'
6662  */
6663 /* ARGSUSED4 */
6664 int
6665 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6666     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6667     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6668 {
6669 	ire_t	*ire = NULL;
6670 	ipif_t	*ipif;
6671 	boolean_t ipif_refheld = B_FALSE;
6672 	uint_t	type;
6673 	uint_t	match_flags = MATCH_IRE_TYPE;
6674 	int	err = 0;
6675 
6676 	ip1dbg(("ip_rt_delete:"));
6677 	/*
6678 	 * If this is the case of RTF_HOST being set, then we set the netmask
6679 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6680 	 */
6681 	if (flags & RTF_HOST) {
6682 		mask = IP_HOST_MASK;
6683 		match_flags |= MATCH_IRE_MASK;
6684 	} else if (rtm_addrs & RTA_NETMASK) {
6685 		match_flags |= MATCH_IRE_MASK;
6686 	}
6687 
6688 	/*
6689 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6690 	 * we check if the gateway address is one of our interfaces first,
6691 	 * and fall back on RTF_GATEWAY routes.
6692 	 *
6693 	 * This makes it possible to delete an original
6694 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6695 	 *
6696 	 * As the interface index specified with the RTA_IFP sockaddr is the
6697 	 * same for all ipif's off of an ill, the matching logic below uses
6698 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6699 	 * sharing the same prefix and interface index as the the route
6700 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6701 	 * is specified in the request.
6702 	 *
6703 	 * On the other hand, since the gateway address will usually be
6704 	 * different for each ipif on the system, the matching logic
6705 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6706 	 * route.  This means that interface routes for the same prefix can be
6707 	 * uniquely identified if they belong to distinct ipif's and if a
6708 	 * RTA_IFP sockaddr is not present.
6709 	 *
6710 	 * For more detail on specifying routes by gateway address and by
6711 	 * interface index, see the comments in ip_rt_add().
6712 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6713 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6714 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6715 	 * succeed.
6716 	 */
6717 	if (src_ipif != NULL) {
6718 		if (ipif_arg == NULL && gw_addr != 0) {
6719 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6720 			    q, mp, func, &err);
6721 			if (ipif_arg != NULL)
6722 				ipif_refheld = B_TRUE;
6723 		}
6724 		if (ipif_arg == NULL) {
6725 			err = (err == EINPROGRESS) ? err : ESRCH;
6726 			return (err);
6727 		}
6728 		ipif = ipif_arg;
6729 	} else {
6730 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6731 			    q, mp, func, &err);
6732 		if (ipif != NULL)
6733 			ipif_refheld = B_TRUE;
6734 		else if (err == EINPROGRESS)
6735 			return (err);
6736 		else
6737 			err = 0;
6738 	}
6739 	if (ipif != NULL) {
6740 		if (ipif_arg != NULL) {
6741 			if (ipif_refheld) {
6742 				ipif_refrele(ipif);
6743 				ipif_refheld = B_FALSE;
6744 			}
6745 			ipif = ipif_arg;
6746 			match_flags |= MATCH_IRE_ILL;
6747 		} else {
6748 			match_flags |= MATCH_IRE_IPIF;
6749 		}
6750 		if (src_ipif != NULL) {
6751 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6752 			    ipif, src_ipif->ipif_ill, match_flags);
6753 		} else {
6754 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6755 				ire = ire_ctable_lookup(dst_addr, 0,
6756 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
6757 				    match_flags);
6758 			}
6759 			if (ire == NULL) {
6760 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6761 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6762 				    NULL, match_flags);
6763 			}
6764 		}
6765 	}
6766 
6767 	if (ire == NULL) {
6768 		/*
6769 		 * At this point, the gateway address is not one of our own
6770 		 * addresses or a matching interface route was not found.  We
6771 		 * set the IRE type to lookup based on whether
6772 		 * this is a host route, a default route or just a prefix.
6773 		 *
6774 		 * If an ipif_arg was passed in, then the lookup is based on an
6775 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6776 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6777 		 * set as the route being looked up is not a traditional
6778 		 * interface route.
6779 		 * Since we do not add gateway route with srcipif, we don't
6780 		 * expect to find it either.
6781 		 */
6782 		if (src_ipif != NULL) {
6783 			if (ipif_refheld)
6784 				ipif_refrele(ipif);
6785 			return (ESRCH);
6786 		} else {
6787 			match_flags &= ~MATCH_IRE_IPIF;
6788 			match_flags |= MATCH_IRE_GW;
6789 			if (ipif_arg != NULL)
6790 				match_flags |= MATCH_IRE_ILL;
6791 			if (mask == IP_HOST_MASK)
6792 				type = IRE_HOST;
6793 			else if (mask == 0)
6794 				type = IRE_DEFAULT;
6795 			else
6796 				type = IRE_PREFIX;
6797 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6798 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
6799 			if (ire == NULL && type == IRE_HOST) {
6800 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6801 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6802 				    ALL_ZONES, 0, NULL, match_flags);
6803 			}
6804 		}
6805 	}
6806 
6807 	if (ipif_refheld)
6808 		ipif_refrele(ipif);
6809 
6810 	/* ipif is not refheld anymore */
6811 	if (ire == NULL)
6812 		return (ESRCH);
6813 
6814 	if (ire->ire_flags & RTF_MULTIRT) {
6815 		/*
6816 		 * Invoke the CGTP (multirouting) filtering module
6817 		 * to remove the dst address from the filtering database.
6818 		 * Packets coming from that address will no longer be
6819 		 * filtered to remove duplicates.
6820 		 */
6821 		if (ip_cgtp_filter_ops != NULL) {
6822 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6823 			    ire->ire_gateway_addr);
6824 		}
6825 		ip_cgtp_bcast_delete(ire);
6826 	}
6827 
6828 	ipif = ire->ire_ipif;
6829 	/*
6830 	 * Removing from ipif_saved_ire_mp is not necessary
6831 	 * when src_ipif being non-NULL. ip_rt_add does not
6832 	 * save the ires which src_ipif being non-NULL.
6833 	 */
6834 	if (ipif != NULL && src_ipif == NULL) {
6835 		ipif_remove_ire(ipif, ire);
6836 	}
6837 	if (ioctl_msg)
6838 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6839 	ire_delete(ire);
6840 	ire_refrele(ire);
6841 	return (err);
6842 }
6843 
6844 /*
6845  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6846  */
6847 /* ARGSUSED */
6848 int
6849 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6850     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6851 {
6852 	ipaddr_t dst_addr;
6853 	ipaddr_t gw_addr;
6854 	ipaddr_t mask;
6855 	int error = 0;
6856 	mblk_t *mp1;
6857 	struct rtentry *rt;
6858 	ipif_t *ipif = NULL;
6859 
6860 	ip1dbg(("ip_siocaddrt:"));
6861 	/* Existence of mp1 verified in ip_wput_nondata */
6862 	mp1 = mp->b_cont->b_cont;
6863 	rt = (struct rtentry *)mp1->b_rptr;
6864 
6865 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6866 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6867 
6868 	/*
6869 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6870 	 * to a particular host address.  In this case, we set the netmask to
6871 	 * all ones for the particular destination address.  Otherwise,
6872 	 * determine the netmask to be used based on dst_addr and the interfaces
6873 	 * in use.
6874 	 */
6875 	if (rt->rt_flags & RTF_HOST) {
6876 		mask = IP_HOST_MASK;
6877 	} else {
6878 		/*
6879 		 * Note that ip_subnet_mask returns a zero mask in the case of
6880 		 * default (an all-zeroes address).
6881 		 */
6882 		mask = ip_subnet_mask(dst_addr, &ipif);
6883 	}
6884 
6885 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6886 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
6887 	if (ipif != NULL)
6888 		ipif_refrele(ipif);
6889 	return (error);
6890 }
6891 
6892 /*
6893  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6894  */
6895 /* ARGSUSED */
6896 int
6897 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6898     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6899 {
6900 	ipaddr_t dst_addr;
6901 	ipaddr_t gw_addr;
6902 	ipaddr_t mask;
6903 	int error;
6904 	mblk_t *mp1;
6905 	struct rtentry *rt;
6906 	ipif_t *ipif = NULL;
6907 
6908 	ip1dbg(("ip_siocdelrt:"));
6909 	/* Existence of mp1 verified in ip_wput_nondata */
6910 	mp1 = mp->b_cont->b_cont;
6911 	rt = (struct rtentry *)mp1->b_rptr;
6912 
6913 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6914 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6915 
6916 	/*
6917 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6918 	 * to a particular host address.  In this case, we set the netmask to
6919 	 * all ones for the particular destination address.  Otherwise,
6920 	 * determine the netmask to be used based on dst_addr and the interfaces
6921 	 * in use.
6922 	 */
6923 	if (rt->rt_flags & RTF_HOST) {
6924 		mask = IP_HOST_MASK;
6925 	} else {
6926 		/*
6927 		 * Note that ip_subnet_mask returns a zero mask in the case of
6928 		 * default (an all-zeroes address).
6929 		 */
6930 		mask = ip_subnet_mask(dst_addr, &ipif);
6931 	}
6932 
6933 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6934 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6935 	    B_TRUE, q, mp, ip_process_ioctl);
6936 	if (ipif != NULL)
6937 		ipif_refrele(ipif);
6938 	return (error);
6939 }
6940 
6941 /*
6942  * Enqueue the mp onto the ipsq, chained by b_next.
6943  * b_prev stores the function to be executed later, and b_queue the queue
6944  * where this mp originated.
6945  */
6946 void
6947 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6948     ill_t *pending_ill)
6949 {
6950 	conn_t	*connp = NULL;
6951 
6952 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6953 	ASSERT(func != NULL);
6954 
6955 	mp->b_queue = q;
6956 	mp->b_prev = (void *)func;
6957 	mp->b_next = NULL;
6958 
6959 	switch (type) {
6960 	case CUR_OP:
6961 		if (ipsq->ipsq_mptail != NULL) {
6962 			ASSERT(ipsq->ipsq_mphead != NULL);
6963 			ipsq->ipsq_mptail->b_next = mp;
6964 		} else {
6965 			ASSERT(ipsq->ipsq_mphead == NULL);
6966 			ipsq->ipsq_mphead = mp;
6967 		}
6968 		ipsq->ipsq_mptail = mp;
6969 		break;
6970 
6971 	case NEW_OP:
6972 		if (ipsq->ipsq_xopq_mptail != NULL) {
6973 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6974 			ipsq->ipsq_xopq_mptail->b_next = mp;
6975 		} else {
6976 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6977 			ipsq->ipsq_xopq_mphead = mp;
6978 		}
6979 		ipsq->ipsq_xopq_mptail = mp;
6980 		break;
6981 	default:
6982 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6983 	}
6984 
6985 	if (CONN_Q(q) && pending_ill != NULL) {
6986 		connp = Q_TO_CONN(q);
6987 
6988 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6989 		connp->conn_oper_pending_ill = pending_ill;
6990 	}
6991 }
6992 
6993 /*
6994  * Return the mp at the head of the ipsq. After emptying the ipsq
6995  * look at the next ioctl, if this ioctl is complete. Otherwise
6996  * return, we will resume when we complete the current ioctl.
6997  * The current ioctl will wait till it gets a response from the
6998  * driver below.
6999  */
7000 static mblk_t *
7001 ipsq_dq(ipsq_t *ipsq)
7002 {
7003 	mblk_t	*mp;
7004 
7005 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7006 
7007 	mp = ipsq->ipsq_mphead;
7008 	if (mp != NULL) {
7009 		ipsq->ipsq_mphead = mp->b_next;
7010 		if (ipsq->ipsq_mphead == NULL)
7011 			ipsq->ipsq_mptail = NULL;
7012 		mp->b_next = NULL;
7013 		return (mp);
7014 	}
7015 	if (ipsq->ipsq_current_ipif != NULL)
7016 		return (NULL);
7017 	mp = ipsq->ipsq_xopq_mphead;
7018 	if (mp != NULL) {
7019 		ipsq->ipsq_xopq_mphead = mp->b_next;
7020 		if (ipsq->ipsq_xopq_mphead == NULL)
7021 			ipsq->ipsq_xopq_mptail = NULL;
7022 		mp->b_next = NULL;
7023 		return (mp);
7024 	}
7025 	return (NULL);
7026 }
7027 
7028 /*
7029  * Enter the ipsq corresponding to ill, by waiting synchronously till
7030  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7031  * will have to drain completely before ipsq_enter returns success.
7032  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7033  * and the ipsq_exit logic will start the next enqueued ioctl after
7034  * completion of the current ioctl. If 'force' is used, we don't wait
7035  * for the enqueued ioctls. This is needed when a conn_close wants to
7036  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7037  * of an ill can also use this option. But we dont' use it currently.
7038  */
7039 #define	ENTER_SQ_WAIT_TICKS 100
7040 boolean_t
7041 ipsq_enter(ill_t *ill, boolean_t force)
7042 {
7043 	ipsq_t	*ipsq;
7044 	boolean_t waited_enough = B_FALSE;
7045 
7046 	/*
7047 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7048 	 * Since the <ill-ipsq> assocs could change while we wait for the
7049 	 * writer, it is easier to wait on a fixed global rather than try to
7050 	 * cv_wait on a changing ipsq.
7051 	 */
7052 	mutex_enter(&ill->ill_lock);
7053 	for (;;) {
7054 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7055 			mutex_exit(&ill->ill_lock);
7056 			return (B_FALSE);
7057 		}
7058 
7059 		ipsq = ill->ill_phyint->phyint_ipsq;
7060 		mutex_enter(&ipsq->ipsq_lock);
7061 		if (ipsq->ipsq_writer == NULL &&
7062 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7063 			break;
7064 		} else if (ipsq->ipsq_writer != NULL) {
7065 			mutex_exit(&ipsq->ipsq_lock);
7066 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7067 		} else {
7068 			mutex_exit(&ipsq->ipsq_lock);
7069 			if (force) {
7070 				(void) cv_timedwait(&ill->ill_cv,
7071 				    &ill->ill_lock,
7072 				    lbolt + ENTER_SQ_WAIT_TICKS);
7073 				waited_enough = B_TRUE;
7074 				continue;
7075 			} else {
7076 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7077 			}
7078 		}
7079 	}
7080 
7081 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7082 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7083 	ipsq->ipsq_writer = curthread;
7084 	ipsq->ipsq_reentry_cnt++;
7085 #ifdef ILL_DEBUG
7086 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7087 #endif
7088 	mutex_exit(&ipsq->ipsq_lock);
7089 	mutex_exit(&ill->ill_lock);
7090 	return (B_TRUE);
7091 }
7092 
7093 /*
7094  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7095  * certain critical operations like plumbing (i.e. most set ioctls),
7096  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7097  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7098  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7099  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7100  * threads executing in the ipsq. Responses from the driver pertain to the
7101  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7102  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7103  *
7104  * If a thread does not want to reenter the ipsq when it is already writer,
7105  * it must make sure that the specified reentry point to be called later
7106  * when the ipsq is empty, nor any code path starting from the specified reentry
7107  * point must never ever try to enter the ipsq again. Otherwise it can lead
7108  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7109  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7110  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7111  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7112  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7113  * ioctl if the current ioctl has completed. If the current ioctl is still
7114  * in progress it simply returns. The current ioctl could be waiting for
7115  * a response from another module (arp_ or the driver or could be waiting for
7116  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7117  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7118  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7119  * ipsq_current_ipif is clear which happens only on ioctl completion.
7120  */
7121 
7122 /*
7123  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7124  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7125  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7126  * completion.
7127  */
7128 ipsq_t *
7129 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7130     ipsq_func_t func, int type, boolean_t reentry_ok)
7131 {
7132 	ipsq_t	*ipsq;
7133 
7134 	/* Only 1 of ipif or ill can be specified */
7135 	ASSERT((ipif != NULL) ^ (ill != NULL));
7136 	if (ipif != NULL)
7137 		ill = ipif->ipif_ill;
7138 
7139 	/*
7140 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7141 	 * ipsq of an ill can't change when ill_lock is held.
7142 	 */
7143 	GRAB_CONN_LOCK(q);
7144 	mutex_enter(&ill->ill_lock);
7145 	ipsq = ill->ill_phyint->phyint_ipsq;
7146 	mutex_enter(&ipsq->ipsq_lock);
7147 
7148 	/*
7149 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7150 	 *    (Note: If the caller does not specify reentry_ok then neither
7151 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7152 	 *    again. Otherwise it can lead to an infinite loop
7153 	 * 2. Enter the ipsq if there is no current writer and this attempted
7154 	 *    entry is part of the current ioctl or operation
7155 	 * 3. Enter the ipsq if there is no current writer and this is a new
7156 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7157 	 *    empty and there is no ioctl (or operation) currently in progress
7158 	 */
7159 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7160 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7161 	    ipsq->ipsq_current_ipif == NULL))) ||
7162 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7163 		/* Success. */
7164 		ipsq->ipsq_reentry_cnt++;
7165 		ipsq->ipsq_writer = curthread;
7166 		mutex_exit(&ipsq->ipsq_lock);
7167 		mutex_exit(&ill->ill_lock);
7168 		RELEASE_CONN_LOCK(q);
7169 #ifdef ILL_DEBUG
7170 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7171 #endif
7172 		return (ipsq);
7173 	}
7174 
7175 	ipsq_enq(ipsq, q, mp, func, type, ill);
7176 
7177 	mutex_exit(&ipsq->ipsq_lock);
7178 	mutex_exit(&ill->ill_lock);
7179 	RELEASE_CONN_LOCK(q);
7180 	return (NULL);
7181 }
7182 
7183 /*
7184  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7185  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7186  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7187  * completion.
7188  *
7189  * This function does a refrele on the ipif/ill.
7190  */
7191 void
7192 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7193     ipsq_func_t func, int type, boolean_t reentry_ok)
7194 {
7195 	ipsq_t	*ipsq;
7196 
7197 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7198 	/*
7199 	 * Caller must have done a refhold on the ipif. ipif_refrele
7200 	 * happens on the passed ipif. We can do this since we are
7201 	 * already exclusive, or we won't access ipif henceforth, Both
7202 	 * this func and caller will just return if we ipsq_try_enter
7203 	 * fails above. This is needed because func needs to
7204 	 * see the correct refcount. Eg. removeif can work only then.
7205 	 */
7206 	if (ipif != NULL)
7207 		ipif_refrele(ipif);
7208 	else
7209 		ill_refrele(ill);
7210 	if (ipsq != NULL) {
7211 		(*func)(ipsq, q, mp, NULL);
7212 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7213 	}
7214 }
7215 
7216 /*
7217  * If there are more than ILL_GRP_CNT ills in a group,
7218  * we use kmem alloc'd buffers, else use the stack
7219  */
7220 #define	ILL_GRP_CNT	14
7221 /*
7222  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7223  * Called by a thread that is currently exclusive on this ipsq.
7224  */
7225 void
7226 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7227 {
7228 	queue_t	*q;
7229 	mblk_t	*mp;
7230 	ipsq_func_t	func;
7231 	int	next;
7232 	ill_t	**ill_list = NULL;
7233 	size_t	ill_list_size = 0;
7234 	int	cnt = 0;
7235 	boolean_t need_ipsq_free = B_FALSE;
7236 
7237 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7238 	mutex_enter(&ipsq->ipsq_lock);
7239 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7240 	if (ipsq->ipsq_reentry_cnt != 1) {
7241 		ipsq->ipsq_reentry_cnt--;
7242 		mutex_exit(&ipsq->ipsq_lock);
7243 		return;
7244 	}
7245 
7246 	mp = ipsq_dq(ipsq);
7247 	while (mp != NULL) {
7248 again:
7249 		mutex_exit(&ipsq->ipsq_lock);
7250 		func = (ipsq_func_t)mp->b_prev;
7251 		q = (queue_t *)mp->b_queue;
7252 		mp->b_prev = NULL;
7253 		mp->b_queue = NULL;
7254 
7255 		/*
7256 		 * If 'q' is an conn queue, it is valid, since we did a
7257 		 * a refhold on the connp, at the start of the ioctl.
7258 		 * If 'q' is an ill queue, it is valid, since close of an
7259 		 * ill will clean up the 'ipsq'.
7260 		 */
7261 		(*func)(ipsq, q, mp, NULL);
7262 
7263 		mutex_enter(&ipsq->ipsq_lock);
7264 		mp = ipsq_dq(ipsq);
7265 	}
7266 
7267 	mutex_exit(&ipsq->ipsq_lock);
7268 
7269 	/*
7270 	 * Need to grab the locks in the right order. Need to
7271 	 * atomically check (under ipsq_lock) that there are no
7272 	 * messages before relinquishing the ipsq. Also need to
7273 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7274 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7275 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7276 	 * to grab ill_g_lock as writer.
7277 	 */
7278 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7279 
7280 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7281 	if (ipsq->ipsq_refs != 0) {
7282 		/* At most 2 ills v4/v6 per phyint */
7283 		cnt = ipsq->ipsq_refs << 1;
7284 		ill_list_size = cnt * sizeof (ill_t *);
7285 		/*
7286 		 * If memory allocation fails, we will do the split
7287 		 * the next time ipsq_exit is called for whatever reason.
7288 		 * As long as the ipsq_split flag is set the need to
7289 		 * split is remembered.
7290 		 */
7291 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7292 		if (ill_list != NULL)
7293 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7294 	}
7295 	mutex_enter(&ipsq->ipsq_lock);
7296 	mp = ipsq_dq(ipsq);
7297 	if (mp != NULL) {
7298 		/* oops, some message has landed up, we can't get out */
7299 		if (ill_list != NULL)
7300 			ill_unlock_ills(ill_list, cnt);
7301 		rw_exit(&ill_g_lock);
7302 		if (ill_list != NULL)
7303 			kmem_free(ill_list, ill_list_size);
7304 		ill_list = NULL;
7305 		ill_list_size = 0;
7306 		cnt = 0;
7307 		goto again;
7308 	}
7309 
7310 	/*
7311 	 * Split only if no ioctl is pending and if memory alloc succeeded
7312 	 * above.
7313 	 */
7314 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7315 		ill_list != NULL) {
7316 		/*
7317 		 * No new ill can join this ipsq since we are holding the
7318 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7319 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7320 		 * If so we will retry on the next ipsq_exit.
7321 		 */
7322 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7323 	}
7324 
7325 	/*
7326 	 * We are holding the ipsq lock, hence no new messages can
7327 	 * land up on the ipsq, and there are no messages currently.
7328 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7329 	 * atomically while holding ill locks.
7330 	 */
7331 	ipsq->ipsq_writer = NULL;
7332 	ipsq->ipsq_reentry_cnt--;
7333 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7334 #ifdef ILL_DEBUG
7335 	ipsq->ipsq_depth = 0;
7336 #endif
7337 	mutex_exit(&ipsq->ipsq_lock);
7338 	/*
7339 	 * For IPMP this should wake up all ills in this ipsq.
7340 	 * We need to hold the ill_lock while waking up waiters to
7341 	 * avoid missed wakeups. But there is no need to acquire all
7342 	 * the ill locks and then wakeup. If we have not acquired all
7343 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7344 	 * wakes up ills one at a time after getting the right ill_lock
7345 	 */
7346 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7347 	if (ill_list != NULL)
7348 		ill_unlock_ills(ill_list, cnt);
7349 	if (ipsq->ipsq_refs == 0)
7350 		need_ipsq_free = B_TRUE;
7351 	rw_exit(&ill_g_lock);
7352 	if (ill_list != 0)
7353 		kmem_free(ill_list, ill_list_size);
7354 
7355 	if (need_ipsq_free) {
7356 		/*
7357 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7358 		 * looked up. ipsq can be looked up only thru ill or phyint
7359 		 * and there are no ills/phyint on this ipsq.
7360 		 */
7361 		ipsq_delete(ipsq);
7362 	}
7363 	/*
7364 	 * Now start any igmp or mld timers that could not be started
7365 	 * while inside the ipsq. The timers can't be started while inside
7366 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7367 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7368 	 * there could be a deadlock since the timeout handlers
7369 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7370 	 * wait in ipsq_enter() trying to get the ipsq.
7371 	 *
7372 	 * However there is one exception to the above. If this thread is
7373 	 * itself the igmp/mld timeout handler thread, then we don't want
7374 	 * to start any new timer until the current handler is done. The
7375 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7376 	 * all others pass B_TRUE.
7377 	 */
7378 	if (start_igmp_timer) {
7379 		mutex_enter(&igmp_timer_lock);
7380 		next = igmp_deferred_next;
7381 		igmp_deferred_next = INFINITY;
7382 		mutex_exit(&igmp_timer_lock);
7383 
7384 		if (next != INFINITY)
7385 			igmp_start_timers(next);
7386 	}
7387 
7388 	if (start_mld_timer) {
7389 		mutex_enter(&mld_timer_lock);
7390 		next = mld_deferred_next;
7391 		mld_deferred_next = INFINITY;
7392 		mutex_exit(&mld_timer_lock);
7393 
7394 		if (next != INFINITY)
7395 			mld_start_timers(next);
7396 	}
7397 }
7398 
7399 /*
7400  * The ill is closing. Flush all messages on the ipsq that originated
7401  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7402  * for this ill since ipsq_enter could not have entered until then.
7403  * New messages can't be queued since the CONDEMNED flag is set.
7404  */
7405 static void
7406 ipsq_flush(ill_t *ill)
7407 {
7408 	queue_t	*q;
7409 	mblk_t	*prev;
7410 	mblk_t	*mp;
7411 	mblk_t	*mp_next;
7412 	ipsq_t	*ipsq;
7413 
7414 	ASSERT(IAM_WRITER_ILL(ill));
7415 	ipsq = ill->ill_phyint->phyint_ipsq;
7416 	/*
7417 	 * Flush any messages sent up by the driver.
7418 	 */
7419 	mutex_enter(&ipsq->ipsq_lock);
7420 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7421 		mp_next = mp->b_next;
7422 		q = mp->b_queue;
7423 		if (q == ill->ill_rq || q == ill->ill_wq) {
7424 			/* Remove the mp from the ipsq */
7425 			if (prev == NULL)
7426 				ipsq->ipsq_mphead = mp->b_next;
7427 			else
7428 				prev->b_next = mp->b_next;
7429 			if (ipsq->ipsq_mptail == mp) {
7430 				ASSERT(mp_next == NULL);
7431 				ipsq->ipsq_mptail = prev;
7432 			}
7433 			inet_freemsg(mp);
7434 		} else {
7435 			prev = mp;
7436 		}
7437 	}
7438 	mutex_exit(&ipsq->ipsq_lock);
7439 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7440 	ipsq_xopq_mp_cleanup(ill, NULL);
7441 	ill_pending_mp_cleanup(ill);
7442 }
7443 
7444 /*
7445  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7446  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7447  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7448  * time (possible with one port going down for aggr and someone tearing down the
7449  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7450  * to indicate when the cleanup has started (1 ref) and when the cleanup
7451  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7452  * putting 2 ref on ill_inuse_ref.
7453  */
7454 static void
7455 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7456 {
7457 	conn_t *connp;
7458 	squeue_t *sqp;
7459 	mblk_t *mp;
7460 
7461 	ASSERT(rx_ring != NULL);
7462 
7463 	/* Just clean one squeue */
7464 	mutex_enter(&ill->ill_lock);
7465 	/*
7466 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
7467 	 * ip_squeue_soft_ring_affinty() will not go
7468 	 * ahead with assigning rings.
7469 	 */
7470 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
7471 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7472 		/* Some operations pending on the ring. Wait */
7473 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7474 
7475 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7476 		/*
7477 		 * Someone already trying to clean
7478 		 * this squeue or its already been cleaned.
7479 		 */
7480 		mutex_exit(&ill->ill_lock);
7481 		return;
7482 	}
7483 	sqp = rx_ring->rr_sqp;
7484 
7485 	if (sqp == NULL) {
7486 		/*
7487 		 * The rx_ring never had a squeue assigned to it.
7488 		 * We are under ill_lock so we can clean it up
7489 		 * here itself since no one can get to it.
7490 		 */
7491 		rx_ring->rr_blank = NULL;
7492 		rx_ring->rr_handle = NULL;
7493 		rx_ring->rr_sqp = NULL;
7494 		rx_ring->rr_ring_state = ILL_RING_FREE;
7495 		mutex_exit(&ill->ill_lock);
7496 		return;
7497 	}
7498 
7499 	/* Set the state that its being cleaned */
7500 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7501 	ASSERT(sqp != NULL);
7502 	mutex_exit(&ill->ill_lock);
7503 
7504 	/*
7505 	 * Use the preallocated ill_unbind_conn for this purpose
7506 	 */
7507 	connp = ill->ill_dls_capab->ill_unbind_conn;
7508 	mp = &connp->conn_tcp->tcp_closemp;
7509 	CONN_INC_REF(connp);
7510 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7511 
7512 	mutex_enter(&ill->ill_lock);
7513 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7514 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7515 
7516 	mutex_exit(&ill->ill_lock);
7517 }
7518 
7519 static void
7520 ipsq_clean_all(ill_t *ill)
7521 {
7522 	int idx;
7523 
7524 	/*
7525 	 * No need to clean if poll_capab isn't set for this ill
7526 	 */
7527 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
7528 		return;
7529 
7530 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7531 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
7532 		ipsq_clean_ring(ill, ipr);
7533 	}
7534 
7535 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
7536 }
7537 
7538 /* ARGSUSED */
7539 int
7540 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7541     ip_ioctl_cmd_t *ipip, void *ifreq)
7542 {
7543 	ill_t	*ill;
7544 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7545 	boolean_t isv6;
7546 	conn_t	*connp;
7547 
7548 	connp = Q_TO_CONN(q);
7549 	isv6 = connp->conn_af_isv6;
7550 	/*
7551 	 * Set original index.
7552 	 * Failover and failback move logical interfaces
7553 	 * from one physical interface to another.  The
7554 	 * original index indicates the parent of a logical
7555 	 * interface, in other words, the physical interface
7556 	 * the logical interface will be moved back to on
7557 	 * failback.
7558 	 */
7559 
7560 	/*
7561 	 * Don't allow the original index to be changed
7562 	 * for non-failover addresses, autoconfigured
7563 	 * addresses, or IPv6 link local addresses.
7564 	 */
7565 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7566 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7567 		return (EINVAL);
7568 	}
7569 	/*
7570 	 * The new original index must be in use by some
7571 	 * physical interface.
7572 	 */
7573 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7574 	    NULL, NULL);
7575 	if (ill == NULL)
7576 		return (ENXIO);
7577 	ill_refrele(ill);
7578 
7579 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7580 	/*
7581 	 * When this ipif gets failed back, don't
7582 	 * preserve the original id, as it is no
7583 	 * longer applicable.
7584 	 */
7585 	ipif->ipif_orig_ipifid = 0;
7586 	/*
7587 	 * For IPv4, change the original index of any
7588 	 * multicast addresses associated with the
7589 	 * ipif to the new value.
7590 	 */
7591 	if (!isv6) {
7592 		ilm_t *ilm;
7593 
7594 		mutex_enter(&ipif->ipif_ill->ill_lock);
7595 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7596 		    ilm = ilm->ilm_next) {
7597 			if (ilm->ilm_ipif == ipif) {
7598 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7599 			}
7600 		}
7601 		mutex_exit(&ipif->ipif_ill->ill_lock);
7602 	}
7603 	return (0);
7604 }
7605 
7606 /* ARGSUSED */
7607 int
7608 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7609     ip_ioctl_cmd_t *ipip, void *ifreq)
7610 {
7611 	struct lifreq *lifr = (struct lifreq *)ifreq;
7612 
7613 	/*
7614 	 * Get the original interface index i.e the one
7615 	 * before FAILOVER if it ever happened.
7616 	 */
7617 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7618 	return (0);
7619 }
7620 
7621 /*
7622  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7623  * refhold and return the associated ipif
7624  */
7625 int
7626 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7627 {
7628 	boolean_t exists;
7629 	struct iftun_req *ta;
7630 	ipif_t	*ipif;
7631 	ill_t	*ill;
7632 	boolean_t isv6;
7633 	mblk_t	*mp1;
7634 	int	error;
7635 	conn_t	*connp;
7636 
7637 	/* Existence verified in ip_wput_nondata */
7638 	mp1 = mp->b_cont->b_cont;
7639 	ta = (struct iftun_req *)mp1->b_rptr;
7640 	/*
7641 	 * Null terminate the string to protect against buffer
7642 	 * overrun. String was generated by user code and may not
7643 	 * be trusted.
7644 	 */
7645 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7646 
7647 	connp = Q_TO_CONN(q);
7648 	isv6 = connp->conn_af_isv6;
7649 
7650 	/* Disallows implicit create */
7651 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7652 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7653 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7654 	if (ipif == NULL)
7655 		return (error);
7656 
7657 	if (ipif->ipif_id != 0) {
7658 		/*
7659 		 * We really don't want to set/get tunnel parameters
7660 		 * on virtual tunnel interfaces.  Only allow the
7661 		 * base tunnel to do these.
7662 		 */
7663 		ipif_refrele(ipif);
7664 		return (EINVAL);
7665 	}
7666 
7667 	/*
7668 	 * Send down to tunnel mod for ioctl processing.
7669 	 * Will finish ioctl in ip_rput_other().
7670 	 */
7671 	ill = ipif->ipif_ill;
7672 	if (ill->ill_net_type == IRE_LOOPBACK) {
7673 		ipif_refrele(ipif);
7674 		return (EOPNOTSUPP);
7675 	}
7676 
7677 	if (ill->ill_wq == NULL) {
7678 		ipif_refrele(ipif);
7679 		return (ENXIO);
7680 	}
7681 	/*
7682 	 * Mark the ioctl as coming from an IPv6 interface for
7683 	 * tun's convenience.
7684 	 */
7685 	if (ill->ill_isv6)
7686 		ta->ifta_flags |= 0x80000000;
7687 	*ipifp = ipif;
7688 	return (0);
7689 }
7690 
7691 /*
7692  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7693  * and return the associated ipif.
7694  * Return value:
7695  *	Non zero: An error has occurred. ci may not be filled out.
7696  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7697  *	a held ipif in ci.ci_ipif.
7698  */
7699 int
7700 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7701     cmd_info_t *ci, ipsq_func_t func)
7702 {
7703 	sin_t		*sin;
7704 	sin6_t		*sin6;
7705 	char		*name;
7706 	struct ifreq    *ifr;
7707 	struct lifreq    *lifr;
7708 	ipif_t		*ipif = NULL;
7709 	ill_t		*ill;
7710 	conn_t		*connp;
7711 	boolean_t	isv6;
7712 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7713 	boolean_t	exists;
7714 	int		err;
7715 	mblk_t		*mp1;
7716 	zoneid_t	zoneid;
7717 
7718 	if (q->q_next != NULL) {
7719 		ill = (ill_t *)q->q_ptr;
7720 		isv6 = ill->ill_isv6;
7721 		connp = NULL;
7722 		zoneid = ALL_ZONES;
7723 	} else {
7724 		ill = NULL;
7725 		connp = Q_TO_CONN(q);
7726 		isv6 = connp->conn_af_isv6;
7727 		zoneid = connp->conn_zoneid;
7728 		if (zoneid == GLOBAL_ZONEID) {
7729 			/* global zone can access ipifs in all zones */
7730 			zoneid = ALL_ZONES;
7731 		}
7732 	}
7733 
7734 	/* Has been checked in ip_wput_nondata */
7735 	mp1 = mp->b_cont->b_cont;
7736 
7737 
7738 	if (cmd_type == IF_CMD) {
7739 		/* This a old style SIOC[GS]IF* command */
7740 		ifr = (struct ifreq *)mp1->b_rptr;
7741 		/*
7742 		 * Null terminate the string to protect against buffer
7743 		 * overrun. String was generated by user code and may not
7744 		 * be trusted.
7745 		 */
7746 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7747 		sin = (sin_t *)&ifr->ifr_addr;
7748 		name = ifr->ifr_name;
7749 		ci->ci_sin = sin;
7750 		ci->ci_sin6 = NULL;
7751 		ci->ci_lifr = (struct lifreq *)ifr;
7752 	} else {
7753 		/* This a new style SIOC[GS]LIF* command */
7754 		ASSERT(cmd_type == LIF_CMD);
7755 		lifr = (struct lifreq *)mp1->b_rptr;
7756 		/*
7757 		 * Null terminate the string to protect against buffer
7758 		 * overrun. String was generated by user code and may not
7759 		 * be trusted.
7760 		 */
7761 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7762 		name = lifr->lifr_name;
7763 		sin = (sin_t *)&lifr->lifr_addr;
7764 		sin6 = (sin6_t *)&lifr->lifr_addr;
7765 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7766 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7767 			    LIFNAMSIZ);
7768 		}
7769 		ci->ci_sin = sin;
7770 		ci->ci_sin6 = sin6;
7771 		ci->ci_lifr = lifr;
7772 	}
7773 
7774 
7775 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7776 		/*
7777 		 * The ioctl will be failed if the ioctl comes down
7778 		 * an conn stream
7779 		 */
7780 		if (ill == NULL) {
7781 			/*
7782 			 * Not an ill queue, return EINVAL same as the
7783 			 * old error code.
7784 			 */
7785 			return (ENXIO);
7786 		}
7787 		ipif = ill->ill_ipif;
7788 		ipif_refhold(ipif);
7789 	} else {
7790 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7791 		    &exists, isv6, zoneid,
7792 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7793 		if (ipif == NULL) {
7794 			if (err == EINPROGRESS)
7795 				return (err);
7796 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7797 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7798 				/*
7799 				 * Need to try both v4 and v6 since this
7800 				 * ioctl can come down either v4 or v6
7801 				 * socket. The lifreq.lifr_family passed
7802 				 * down by this ioctl is AF_UNSPEC.
7803 				 */
7804 				ipif = ipif_lookup_on_name(name,
7805 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7806 				    zoneid, (connp == NULL) ? q :
7807 				    CONNP_TO_WQ(connp), mp, func, &err);
7808 				if (err == EINPROGRESS)
7809 					return (err);
7810 			}
7811 			err = 0;	/* Ensure we don't use it below */
7812 		}
7813 	}
7814 
7815 	/*
7816 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7817 	 */
7818 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7819 		ipif_refrele(ipif);
7820 		return (ENXIO);
7821 	}
7822 
7823 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7824 	    name[0] == '\0') {
7825 		/*
7826 		 * Handle a or a SIOC?IF* with a null name
7827 		 * during plumb (on the ill queue before the I_PLINK).
7828 		 */
7829 		ipif = ill->ill_ipif;
7830 		ipif_refhold(ipif);
7831 	}
7832 
7833 	if (ipif == NULL)
7834 		return (ENXIO);
7835 
7836 	/*
7837 	 * Allow only GET operations if this ipif has been created
7838 	 * temporarily due to a MOVE operation.
7839 	 */
7840 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7841 		ipif_refrele(ipif);
7842 		return (EINVAL);
7843 	}
7844 
7845 	ci->ci_ipif = ipif;
7846 	return (0);
7847 }
7848 
7849 /*
7850  * Return the total number of ipifs.
7851  */
7852 static uint_t
7853 ip_get_numifs(zoneid_t zoneid)
7854 {
7855 	uint_t numifs = 0;
7856 	ill_t	*ill;
7857 	ill_walk_context_t	ctx;
7858 	ipif_t	*ipif;
7859 
7860 	rw_enter(&ill_g_lock, RW_READER);
7861 	ill = ILL_START_WALK_V4(&ctx);
7862 
7863 	while (ill != NULL) {
7864 		for (ipif = ill->ill_ipif; ipif != NULL;
7865 		    ipif = ipif->ipif_next) {
7866 			if (ipif->ipif_zoneid == zoneid ||
7867 			    ipif->ipif_zoneid == ALL_ZONES)
7868 				numifs++;
7869 		}
7870 		ill = ill_next(&ctx, ill);
7871 	}
7872 	rw_exit(&ill_g_lock);
7873 	return (numifs);
7874 }
7875 
7876 /*
7877  * Return the total number of ipifs.
7878  */
7879 static uint_t
7880 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7881 {
7882 	uint_t numifs = 0;
7883 	ill_t	*ill;
7884 	ipif_t	*ipif;
7885 	ill_walk_context_t	ctx;
7886 
7887 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7888 
7889 	rw_enter(&ill_g_lock, RW_READER);
7890 	if (family == AF_INET)
7891 		ill = ILL_START_WALK_V4(&ctx);
7892 	else if (family == AF_INET6)
7893 		ill = ILL_START_WALK_V6(&ctx);
7894 	else
7895 		ill = ILL_START_WALK_ALL(&ctx);
7896 
7897 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7898 		for (ipif = ill->ill_ipif; ipif != NULL;
7899 		    ipif = ipif->ipif_next) {
7900 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7901 			    !(lifn_flags & LIFC_NOXMIT))
7902 				continue;
7903 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7904 			    !(lifn_flags & LIFC_TEMPORARY))
7905 				continue;
7906 			if (((ipif->ipif_flags &
7907 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7908 			    IPIF_DEPRECATED)) ||
7909 			    (ill->ill_phyint->phyint_flags &
7910 			    PHYI_LOOPBACK) ||
7911 			    !(ipif->ipif_flags & IPIF_UP)) &&
7912 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7913 				continue;
7914 
7915 			if (zoneid != ipif->ipif_zoneid &&
7916 			    ipif->ipif_zoneid != ALL_ZONES &&
7917 			    (zoneid != GLOBAL_ZONEID ||
7918 			    !(lifn_flags & LIFC_ALLZONES)))
7919 				continue;
7920 
7921 			numifs++;
7922 		}
7923 	}
7924 	rw_exit(&ill_g_lock);
7925 	return (numifs);
7926 }
7927 
7928 uint_t
7929 ip_get_lifsrcofnum(ill_t *ill)
7930 {
7931 	uint_t numifs = 0;
7932 	ill_t	*ill_head = ill;
7933 
7934 	/*
7935 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7936 	 * other thread may be trying to relink the ILLs in this usesrc group
7937 	 * and adjusting the ill_usesrc_grp_next pointers
7938 	 */
7939 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7940 	if ((ill->ill_usesrc_ifindex == 0) &&
7941 	    (ill->ill_usesrc_grp_next != NULL)) {
7942 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7943 		    ill = ill->ill_usesrc_grp_next)
7944 			numifs++;
7945 	}
7946 	rw_exit(&ill_g_usesrc_lock);
7947 
7948 	return (numifs);
7949 }
7950 
7951 /* Null values are passed in for ipif, sin, and ifreq */
7952 /* ARGSUSED */
7953 int
7954 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7955     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7956 {
7957 	int *nump;
7958 
7959 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7960 
7961 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7962 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7963 
7964 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
7965 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7966 	return (0);
7967 }
7968 
7969 /* Null values are passed in for ipif, sin, and ifreq */
7970 /* ARGSUSED */
7971 int
7972 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7973     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7974 {
7975 	struct lifnum *lifn;
7976 	mblk_t	*mp1;
7977 
7978 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7979 
7980 	/* Existence checked in ip_wput_nondata */
7981 	mp1 = mp->b_cont->b_cont;
7982 
7983 	lifn = (struct lifnum *)mp1->b_rptr;
7984 	switch (lifn->lifn_family) {
7985 	case AF_UNSPEC:
7986 	case AF_INET:
7987 	case AF_INET6:
7988 		break;
7989 	default:
7990 		return (EAFNOSUPPORT);
7991 	}
7992 
7993 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7994 	    Q_TO_CONN(q)->conn_zoneid);
7995 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7996 	return (0);
7997 }
7998 
7999 /* ARGSUSED */
8000 int
8001 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8002     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8003 {
8004 	STRUCT_HANDLE(ifconf, ifc);
8005 	mblk_t *mp1;
8006 	struct iocblk *iocp;
8007 	struct ifreq *ifr;
8008 	ill_walk_context_t	ctx;
8009 	ill_t	*ill;
8010 	ipif_t	*ipif;
8011 	struct sockaddr_in *sin;
8012 	int32_t	ifclen;
8013 	zoneid_t zoneid;
8014 
8015 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8016 
8017 	ip1dbg(("ip_sioctl_get_ifconf"));
8018 	/* Existence verified in ip_wput_nondata */
8019 	mp1 = mp->b_cont->b_cont;
8020 	iocp = (struct iocblk *)mp->b_rptr;
8021 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8022 
8023 	/*
8024 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8025 	 * the user buffer address and length into which the list of struct
8026 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8027 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8028 	 * the SIOCGIFCONF operation was redefined to simply provide
8029 	 * a large output buffer into which we are supposed to jam the ifreq
8030 	 * array.  The same ioctl command code was used, despite the fact that
8031 	 * both the applications and the kernel code had to change, thus making
8032 	 * it impossible to support both interfaces.
8033 	 *
8034 	 * For reasons not good enough to try to explain, the following
8035 	 * algorithm is used for deciding what to do with one of these:
8036 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8037 	 * form with the output buffer coming down as the continuation message.
8038 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8039 	 * and we have to copy in the ifconf structure to find out how big the
8040 	 * output buffer is and where to copy out to.  Sure no problem...
8041 	 *
8042 	 */
8043 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8044 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8045 		int numifs = 0;
8046 		size_t ifc_bufsize;
8047 
8048 		/*
8049 		 * Must be (better be!) continuation of a TRANSPARENT
8050 		 * IOCTL.  We just copied in the ifconf structure.
8051 		 */
8052 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8053 		    (struct ifconf *)mp1->b_rptr);
8054 
8055 		/*
8056 		 * Allocate a buffer to hold requested information.
8057 		 *
8058 		 * If ifc_len is larger than what is needed, we only
8059 		 * allocate what we will use.
8060 		 *
8061 		 * If ifc_len is smaller than what is needed, return
8062 		 * EINVAL.
8063 		 *
8064 		 * XXX: the ill_t structure can hava 2 counters, for
8065 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8066 		 * number of interfaces for a device, so we don't need
8067 		 * to count them here...
8068 		 */
8069 		numifs = ip_get_numifs(zoneid);
8070 
8071 		ifclen = STRUCT_FGET(ifc, ifc_len);
8072 		ifc_bufsize = numifs * sizeof (struct ifreq);
8073 		if (ifc_bufsize > ifclen) {
8074 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8075 				/* old behaviour */
8076 				return (EINVAL);
8077 			} else {
8078 				ifc_bufsize = ifclen;
8079 			}
8080 		}
8081 
8082 		mp1 = mi_copyout_alloc(q, mp,
8083 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8084 		if (mp1 == NULL)
8085 			return (ENOMEM);
8086 
8087 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8088 	}
8089 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8090 	/*
8091 	 * the SIOCGIFCONF ioctl only knows about
8092 	 * IPv4 addresses, so don't try to tell
8093 	 * it about interfaces with IPv6-only
8094 	 * addresses. (Last parm 'isv6' is B_FALSE)
8095 	 */
8096 
8097 	ifr = (struct ifreq *)mp1->b_rptr;
8098 
8099 	rw_enter(&ill_g_lock, RW_READER);
8100 	ill = ILL_START_WALK_V4(&ctx);
8101 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8102 		for (ipif = ill->ill_ipif; ipif;
8103 		    ipif = ipif->ipif_next) {
8104 			if (zoneid != ipif->ipif_zoneid &&
8105 			    ipif->ipif_zoneid != ALL_ZONES)
8106 				continue;
8107 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8108 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8109 					/* old behaviour */
8110 					rw_exit(&ill_g_lock);
8111 					return (EINVAL);
8112 				} else {
8113 					goto if_copydone;
8114 				}
8115 			}
8116 			(void) ipif_get_name(ipif,
8117 			    ifr->ifr_name,
8118 			    sizeof (ifr->ifr_name));
8119 			sin = (sin_t *)&ifr->ifr_addr;
8120 			*sin = sin_null;
8121 			sin->sin_family = AF_INET;
8122 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8123 			ifr++;
8124 		}
8125 	}
8126 if_copydone:
8127 	rw_exit(&ill_g_lock);
8128 	mp1->b_wptr = (uchar_t *)ifr;
8129 
8130 	if (STRUCT_BUF(ifc) != NULL) {
8131 		STRUCT_FSET(ifc, ifc_len,
8132 			(int)((uchar_t *)ifr - mp1->b_rptr));
8133 	}
8134 	return (0);
8135 }
8136 
8137 /*
8138  * Get the interfaces using the address hosted on the interface passed in,
8139  * as a source adddress
8140  */
8141 /* ARGSUSED */
8142 int
8143 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8144     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8145 {
8146 	mblk_t *mp1;
8147 	ill_t	*ill, *ill_head;
8148 	ipif_t	*ipif, *orig_ipif;
8149 	int	numlifs = 0;
8150 	size_t	lifs_bufsize, lifsmaxlen;
8151 	struct	lifreq *lifr;
8152 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8153 	uint_t	ifindex;
8154 	zoneid_t zoneid;
8155 	int err = 0;
8156 	boolean_t isv6 = B_FALSE;
8157 	struct	sockaddr_in	*sin;
8158 	struct	sockaddr_in6	*sin6;
8159 
8160 	STRUCT_HANDLE(lifsrcof, lifs);
8161 
8162 	ASSERT(q->q_next == NULL);
8163 
8164 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8165 
8166 	/* Existence verified in ip_wput_nondata */
8167 	mp1 = mp->b_cont->b_cont;
8168 
8169 	/*
8170 	 * Must be (better be!) continuation of a TRANSPARENT
8171 	 * IOCTL.  We just copied in the lifsrcof structure.
8172 	 */
8173 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8174 	    (struct lifsrcof *)mp1->b_rptr);
8175 
8176 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8177 		return (EINVAL);
8178 
8179 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8180 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8181 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8182 	    ip_process_ioctl, &err);
8183 	if (ipif == NULL) {
8184 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8185 		    ifindex));
8186 		return (err);
8187 	}
8188 
8189 
8190 	/* Allocate a buffer to hold requested information */
8191 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8192 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8193 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8194 	/* The actual size needed is always returned in lifs_len */
8195 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8196 
8197 	/* If the amount we need is more than what is passed in, abort */
8198 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8199 		ipif_refrele(ipif);
8200 		return (0);
8201 	}
8202 
8203 	mp1 = mi_copyout_alloc(q, mp,
8204 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8205 	if (mp1 == NULL) {
8206 		ipif_refrele(ipif);
8207 		return (ENOMEM);
8208 	}
8209 
8210 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8211 	bzero(mp1->b_rptr, lifs_bufsize);
8212 
8213 	lifr = (struct lifreq *)mp1->b_rptr;
8214 
8215 	ill = ill_head = ipif->ipif_ill;
8216 	orig_ipif = ipif;
8217 
8218 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8219 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8220 	rw_enter(&ill_g_lock, RW_READER);
8221 
8222 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8223 	for (; (ill != NULL) && (ill != ill_head);
8224 	    ill = ill->ill_usesrc_grp_next) {
8225 
8226 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8227 			break;
8228 
8229 		ipif = ill->ill_ipif;
8230 		(void) ipif_get_name(ipif,
8231 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8232 		if (ipif->ipif_isv6) {
8233 			sin6 = (sin6_t *)&lifr->lifr_addr;
8234 			*sin6 = sin6_null;
8235 			sin6->sin6_family = AF_INET6;
8236 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8237 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8238 			    &ipif->ipif_v6net_mask);
8239 		} else {
8240 			sin = (sin_t *)&lifr->lifr_addr;
8241 			*sin = sin_null;
8242 			sin->sin_family = AF_INET;
8243 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8244 			lifr->lifr_addrlen = ip_mask_to_plen(
8245 			    ipif->ipif_net_mask);
8246 		}
8247 		lifr++;
8248 	}
8249 	rw_exit(&ill_g_usesrc_lock);
8250 	rw_exit(&ill_g_lock);
8251 	ipif_refrele(orig_ipif);
8252 	mp1->b_wptr = (uchar_t *)lifr;
8253 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8254 
8255 	return (0);
8256 }
8257 
8258 /* ARGSUSED */
8259 int
8260 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8261     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8262 {
8263 	mblk_t *mp1;
8264 	int	list;
8265 	ill_t	*ill;
8266 	ipif_t	*ipif;
8267 	int	flags;
8268 	int	numlifs = 0;
8269 	size_t	lifc_bufsize;
8270 	struct	lifreq *lifr;
8271 	sa_family_t	family;
8272 	struct	sockaddr_in	*sin;
8273 	struct	sockaddr_in6	*sin6;
8274 	ill_walk_context_t	ctx;
8275 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8276 	int32_t	lifclen;
8277 	zoneid_t zoneid;
8278 	STRUCT_HANDLE(lifconf, lifc);
8279 
8280 	ip1dbg(("ip_sioctl_get_lifconf"));
8281 
8282 	ASSERT(q->q_next == NULL);
8283 
8284 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8285 
8286 	/* Existence verified in ip_wput_nondata */
8287 	mp1 = mp->b_cont->b_cont;
8288 
8289 	/*
8290 	 * An extended version of SIOCGIFCONF that takes an
8291 	 * additional address family and flags field.
8292 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8293 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8294 	 * interfaces are omitted.
8295 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8296 	 * unless LIFC_TEMPORARY is specified.
8297 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8298 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8299 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8300 	 * has priority over LIFC_NOXMIT.
8301 	 */
8302 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8303 
8304 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8305 		return (EINVAL);
8306 
8307 	/*
8308 	 * Must be (better be!) continuation of a TRANSPARENT
8309 	 * IOCTL.  We just copied in the lifconf structure.
8310 	 */
8311 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8312 
8313 	family = STRUCT_FGET(lifc, lifc_family);
8314 	flags = STRUCT_FGET(lifc, lifc_flags);
8315 
8316 	switch (family) {
8317 	case AF_UNSPEC:
8318 		/*
8319 		 * walk all ILL's.
8320 		 */
8321 		list = MAX_G_HEADS;
8322 		break;
8323 	case AF_INET:
8324 		/*
8325 		 * walk only IPV4 ILL's.
8326 		 */
8327 		list = IP_V4_G_HEAD;
8328 		break;
8329 	case AF_INET6:
8330 		/*
8331 		 * walk only IPV6 ILL's.
8332 		 */
8333 		list = IP_V6_G_HEAD;
8334 		break;
8335 	default:
8336 		return (EAFNOSUPPORT);
8337 	}
8338 
8339 	/*
8340 	 * Allocate a buffer to hold requested information.
8341 	 *
8342 	 * If lifc_len is larger than what is needed, we only
8343 	 * allocate what we will use.
8344 	 *
8345 	 * If lifc_len is smaller than what is needed, return
8346 	 * EINVAL.
8347 	 */
8348 	numlifs = ip_get_numlifs(family, flags, zoneid);
8349 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8350 	lifclen = STRUCT_FGET(lifc, lifc_len);
8351 	if (lifc_bufsize > lifclen) {
8352 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8353 			return (EINVAL);
8354 		else
8355 			lifc_bufsize = lifclen;
8356 	}
8357 
8358 	mp1 = mi_copyout_alloc(q, mp,
8359 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8360 	if (mp1 == NULL)
8361 		return (ENOMEM);
8362 
8363 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8364 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8365 
8366 	lifr = (struct lifreq *)mp1->b_rptr;
8367 
8368 	rw_enter(&ill_g_lock, RW_READER);
8369 	ill = ill_first(list, list, &ctx);
8370 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8371 		for (ipif = ill->ill_ipif; ipif != NULL;
8372 		    ipif = ipif->ipif_next) {
8373 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8374 			    !(flags & LIFC_NOXMIT))
8375 				continue;
8376 
8377 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8378 			    !(flags & LIFC_TEMPORARY))
8379 				continue;
8380 
8381 			if (((ipif->ipif_flags &
8382 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8383 			    IPIF_DEPRECATED)) ||
8384 			    (ill->ill_phyint->phyint_flags &
8385 			    PHYI_LOOPBACK) ||
8386 			    !(ipif->ipif_flags & IPIF_UP)) &&
8387 			    (flags & LIFC_EXTERNAL_SOURCE))
8388 				continue;
8389 
8390 			if (zoneid != ipif->ipif_zoneid &&
8391 			    ipif->ipif_zoneid != ALL_ZONES &&
8392 			    (zoneid != GLOBAL_ZONEID ||
8393 			    !(flags & LIFC_ALLZONES)))
8394 				continue;
8395 
8396 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8397 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8398 					rw_exit(&ill_g_lock);
8399 					return (EINVAL);
8400 				} else {
8401 					goto lif_copydone;
8402 				}
8403 			}
8404 
8405 			(void) ipif_get_name(ipif,
8406 				lifr->lifr_name,
8407 				sizeof (lifr->lifr_name));
8408 			if (ipif->ipif_isv6) {
8409 				sin6 = (sin6_t *)&lifr->lifr_addr;
8410 				*sin6 = sin6_null;
8411 				sin6->sin6_family = AF_INET6;
8412 				sin6->sin6_addr =
8413 				ipif->ipif_v6lcl_addr;
8414 				lifr->lifr_addrlen =
8415 				ip_mask_to_plen_v6(
8416 				    &ipif->ipif_v6net_mask);
8417 			} else {
8418 				sin = (sin_t *)&lifr->lifr_addr;
8419 				*sin = sin_null;
8420 				sin->sin_family = AF_INET;
8421 				sin->sin_addr.s_addr =
8422 				    ipif->ipif_lcl_addr;
8423 				lifr->lifr_addrlen =
8424 				    ip_mask_to_plen(
8425 				    ipif->ipif_net_mask);
8426 			}
8427 			lifr++;
8428 		}
8429 	}
8430 lif_copydone:
8431 	rw_exit(&ill_g_lock);
8432 
8433 	mp1->b_wptr = (uchar_t *)lifr;
8434 	if (STRUCT_BUF(lifc) != NULL) {
8435 		STRUCT_FSET(lifc, lifc_len,
8436 			(int)((uchar_t *)lifr - mp1->b_rptr));
8437 	}
8438 	return (0);
8439 }
8440 
8441 /* ARGSUSED */
8442 int
8443 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8444     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8445 {
8446 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8447 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8448 	return (0);
8449 }
8450 
8451 static void
8452 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8453 {
8454 	ip6_asp_t *table;
8455 	size_t table_size;
8456 	mblk_t *data_mp;
8457 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8458 
8459 	/* These two ioctls are I_STR only */
8460 	if (iocp->ioc_count == TRANSPARENT) {
8461 		miocnak(q, mp, 0, EINVAL);
8462 		return;
8463 	}
8464 
8465 	data_mp = mp->b_cont;
8466 	if (data_mp == NULL) {
8467 		/* The user passed us a NULL argument */
8468 		table = NULL;
8469 		table_size = iocp->ioc_count;
8470 	} else {
8471 		/*
8472 		 * The user provided a table.  The stream head
8473 		 * may have copied in the user data in chunks,
8474 		 * so make sure everything is pulled up
8475 		 * properly.
8476 		 */
8477 		if (MBLKL(data_mp) < iocp->ioc_count) {
8478 			mblk_t *new_data_mp;
8479 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8480 			    NULL) {
8481 				miocnak(q, mp, 0, ENOMEM);
8482 				return;
8483 			}
8484 			freemsg(data_mp);
8485 			data_mp = new_data_mp;
8486 			mp->b_cont = data_mp;
8487 		}
8488 		table = (ip6_asp_t *)data_mp->b_rptr;
8489 		table_size = iocp->ioc_count;
8490 	}
8491 
8492 	switch (iocp->ioc_cmd) {
8493 	case SIOCGIP6ADDRPOLICY:
8494 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8495 		if (iocp->ioc_rval == -1)
8496 			iocp->ioc_error = EINVAL;
8497 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8498 		else if (table != NULL &&
8499 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8500 			ip6_asp_t *src = table;
8501 			ip6_asp32_t *dst = (void *)table;
8502 			int count = table_size / sizeof (ip6_asp_t);
8503 			int i;
8504 
8505 			/*
8506 			 * We need to do an in-place shrink of the array
8507 			 * to match the alignment attributes of the
8508 			 * 32-bit ABI looking at it.
8509 			 */
8510 			/* LINTED: logical expression always true: op "||" */
8511 			ASSERT(sizeof (*src) > sizeof (*dst));
8512 			for (i = 1; i < count; i++)
8513 				bcopy(src + i, dst + i, sizeof (*dst));
8514 		}
8515 #endif
8516 		break;
8517 
8518 	case SIOCSIP6ADDRPOLICY:
8519 		ASSERT(mp->b_prev == NULL);
8520 		mp->b_prev = (void *)q;
8521 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8522 		/*
8523 		 * We pass in the datamodel here so that the ip6_asp_replace()
8524 		 * routine can handle converting from 32-bit to native formats
8525 		 * where necessary.
8526 		 *
8527 		 * A better way to handle this might be to convert the inbound
8528 		 * data structure here, and hang it off a new 'mp'; thus the
8529 		 * ip6_asp_replace() logic would always be dealing with native
8530 		 * format data structures..
8531 		 *
8532 		 * (An even simpler way to handle these ioctls is to just
8533 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8534 		 * and just recompile everything that depends on it.)
8535 		 */
8536 #endif
8537 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8538 		    iocp->ioc_flag & IOC_MODELS);
8539 		return;
8540 	}
8541 
8542 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8543 	qreply(q, mp);
8544 }
8545 
8546 static void
8547 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8548 {
8549 	mblk_t 		*data_mp;
8550 	struct dstinforeq	*dir;
8551 	uint8_t		*end, *cur;
8552 	in6_addr_t	*daddr, *saddr;
8553 	ipaddr_t	v4daddr;
8554 	ire_t		*ire;
8555 	char		*slabel, *dlabel;
8556 	boolean_t	isipv4;
8557 	int		match_ire;
8558 	ill_t		*dst_ill;
8559 	ipif_t		*src_ipif, *ire_ipif;
8560 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8561 	zoneid_t	zoneid;
8562 
8563 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8564 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8565 
8566 	/*
8567 	 * This ioctl is I_STR only, and must have a
8568 	 * data mblk following the M_IOCTL mblk.
8569 	 */
8570 	data_mp = mp->b_cont;
8571 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8572 		miocnak(q, mp, 0, EINVAL);
8573 		return;
8574 	}
8575 
8576 	if (MBLKL(data_mp) < iocp->ioc_count) {
8577 		mblk_t *new_data_mp;
8578 
8579 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8580 			miocnak(q, mp, 0, ENOMEM);
8581 			return;
8582 		}
8583 		freemsg(data_mp);
8584 		data_mp = new_data_mp;
8585 		mp->b_cont = data_mp;
8586 	}
8587 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8588 
8589 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8590 	    end - cur >= sizeof (struct dstinforeq);
8591 	    cur += sizeof (struct dstinforeq)) {
8592 		dir = (struct dstinforeq *)cur;
8593 		daddr = &dir->dir_daddr;
8594 		saddr = &dir->dir_saddr;
8595 
8596 		/*
8597 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8598 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8599 		 * and ipif_select_source[_v6]() do not.
8600 		 */
8601 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8602 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8603 
8604 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8605 		if (isipv4) {
8606 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8607 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8608 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8609 		} else {
8610 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8611 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8612 		}
8613 		if (ire == NULL) {
8614 			dir->dir_dreachable = 0;
8615 
8616 			/* move on to next dst addr */
8617 			continue;
8618 		}
8619 		dir->dir_dreachable = 1;
8620 
8621 		ire_ipif = ire->ire_ipif;
8622 		if (ire_ipif == NULL)
8623 			goto next_dst;
8624 
8625 		/*
8626 		 * We expect to get back an interface ire or a
8627 		 * gateway ire cache entry.  For both types, the
8628 		 * output interface is ire_ipif->ipif_ill.
8629 		 */
8630 		dst_ill = ire_ipif->ipif_ill;
8631 		dir->dir_dmactype = dst_ill->ill_mactype;
8632 
8633 		if (isipv4) {
8634 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8635 		} else {
8636 			src_ipif = ipif_select_source_v6(dst_ill,
8637 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8638 			    zoneid);
8639 		}
8640 		if (src_ipif == NULL)
8641 			goto next_dst;
8642 
8643 		*saddr = src_ipif->ipif_v6lcl_addr;
8644 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8645 		slabel = ip6_asp_lookup(saddr, NULL);
8646 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8647 		dir->dir_sdeprecated =
8648 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8649 		ipif_refrele(src_ipif);
8650 next_dst:
8651 		ire_refrele(ire);
8652 	}
8653 	miocack(q, mp, iocp->ioc_count, 0);
8654 }
8655 
8656 
8657 /*
8658  * Check if this is an address assigned to this machine.
8659  * Skips interfaces that are down by using ire checks.
8660  * Translates mapped addresses to v4 addresses and then
8661  * treats them as such, returning true if the v4 address
8662  * associated with this mapped address is configured.
8663  * Note: Applications will have to be careful what they do
8664  * with the response; use of mapped addresses limits
8665  * what can be done with the socket, especially with
8666  * respect to socket options and ioctls - neither IPv4
8667  * options nor IPv6 sticky options/ancillary data options
8668  * may be used.
8669  */
8670 /* ARGSUSED */
8671 int
8672 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8673     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8674 {
8675 	struct sioc_addrreq *sia;
8676 	sin_t *sin;
8677 	ire_t *ire;
8678 	mblk_t *mp1;
8679 	zoneid_t zoneid;
8680 
8681 	ip1dbg(("ip_sioctl_tmyaddr"));
8682 
8683 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8684 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8685 
8686 	/* Existence verified in ip_wput_nondata */
8687 	mp1 = mp->b_cont->b_cont;
8688 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8689 	sin = (sin_t *)&sia->sa_addr;
8690 	switch (sin->sin_family) {
8691 	case AF_INET6: {
8692 		sin6_t *sin6 = (sin6_t *)sin;
8693 
8694 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8695 			ipaddr_t v4_addr;
8696 
8697 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8698 			    v4_addr);
8699 			ire = ire_ctable_lookup(v4_addr, 0,
8700 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8701 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8702 		} else {
8703 			in6_addr_t v6addr;
8704 
8705 			v6addr = sin6->sin6_addr;
8706 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8707 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8708 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8709 		}
8710 		break;
8711 	}
8712 	case AF_INET: {
8713 		ipaddr_t v4addr;
8714 
8715 		v4addr = sin->sin_addr.s_addr;
8716 		ire = ire_ctable_lookup(v4addr, 0,
8717 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8718 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8719 		break;
8720 	}
8721 	default:
8722 		return (EAFNOSUPPORT);
8723 	}
8724 	if (ire != NULL) {
8725 		sia->sa_res = 1;
8726 		ire_refrele(ire);
8727 	} else {
8728 		sia->sa_res = 0;
8729 	}
8730 	return (0);
8731 }
8732 
8733 /*
8734  * Check if this is an address assigned on-link i.e. neighbor,
8735  * and makes sure it's reachable from the current zone.
8736  * Returns true for my addresses as well.
8737  * Translates mapped addresses to v4 addresses and then
8738  * treats them as such, returning true if the v4 address
8739  * associated with this mapped address is configured.
8740  * Note: Applications will have to be careful what they do
8741  * with the response; use of mapped addresses limits
8742  * what can be done with the socket, especially with
8743  * respect to socket options and ioctls - neither IPv4
8744  * options nor IPv6 sticky options/ancillary data options
8745  * may be used.
8746  */
8747 /* ARGSUSED */
8748 int
8749 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8750     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8751 {
8752 	struct sioc_addrreq *sia;
8753 	sin_t *sin;
8754 	mblk_t	*mp1;
8755 	ire_t *ire = NULL;
8756 	zoneid_t zoneid;
8757 
8758 	ip1dbg(("ip_sioctl_tonlink"));
8759 
8760 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8761 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8762 
8763 	/* Existence verified in ip_wput_nondata */
8764 	mp1 = mp->b_cont->b_cont;
8765 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8766 	sin = (sin_t *)&sia->sa_addr;
8767 
8768 	/*
8769 	 * Match addresses with a zero gateway field to avoid
8770 	 * routes going through a router.
8771 	 * Exclude broadcast and multicast addresses.
8772 	 */
8773 	switch (sin->sin_family) {
8774 	case AF_INET6: {
8775 		sin6_t *sin6 = (sin6_t *)sin;
8776 
8777 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8778 			ipaddr_t v4_addr;
8779 
8780 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8781 			    v4_addr);
8782 			if (!CLASSD(v4_addr)) {
8783 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8784 				    NULL, NULL, zoneid, NULL,
8785 				    MATCH_IRE_GW);
8786 			}
8787 		} else {
8788 			in6_addr_t v6addr;
8789 			in6_addr_t v6gw;
8790 
8791 			v6addr = sin6->sin6_addr;
8792 			v6gw = ipv6_all_zeros;
8793 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8794 				ire = ire_route_lookup_v6(&v6addr, 0,
8795 				    &v6gw, 0, NULL, NULL, zoneid,
8796 				    NULL, MATCH_IRE_GW);
8797 			}
8798 		}
8799 		break;
8800 	}
8801 	case AF_INET: {
8802 		ipaddr_t v4addr;
8803 
8804 		v4addr = sin->sin_addr.s_addr;
8805 		if (!CLASSD(v4addr)) {
8806 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8807 			    NULL, NULL, zoneid, NULL,
8808 			    MATCH_IRE_GW);
8809 		}
8810 		break;
8811 	}
8812 	default:
8813 		return (EAFNOSUPPORT);
8814 	}
8815 	sia->sa_res = 0;
8816 	if (ire != NULL) {
8817 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8818 		    IRE_LOCAL|IRE_LOOPBACK)) {
8819 			sia->sa_res = 1;
8820 		}
8821 		ire_refrele(ire);
8822 	}
8823 	return (0);
8824 }
8825 
8826 /*
8827  * TBD: implement when kernel maintaines a list of site prefixes.
8828  */
8829 /* ARGSUSED */
8830 int
8831 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8832     ip_ioctl_cmd_t *ipip, void *ifreq)
8833 {
8834 	return (ENXIO);
8835 }
8836 
8837 /* ARGSUSED */
8838 int
8839 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8840     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8841 {
8842 	ill_t  		*ill;
8843 	mblk_t		*mp1;
8844 	conn_t		*connp;
8845 	boolean_t	success;
8846 
8847 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8848 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8849 	/* ioctl comes down on an conn */
8850 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8851 	connp = Q_TO_CONN(q);
8852 
8853 	mp->b_datap->db_type = M_IOCTL;
8854 
8855 	/*
8856 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8857 	 * The original mp contains contaminated b_next values due to 'mi',
8858 	 * which is needed to do the mi_copy_done. Unfortunately if we
8859 	 * send down the original mblk itself and if we are popped due to an
8860 	 * an unplumb before the response comes back from tunnel,
8861 	 * the streamhead (which does a freemsg) will see this contaminated
8862 	 * message and the assertion in freemsg about non-null b_next/b_prev
8863 	 * will panic a DEBUG kernel.
8864 	 */
8865 	mp1 = copymsg(mp);
8866 	if (mp1 == NULL)
8867 		return (ENOMEM);
8868 
8869 	ill = ipif->ipif_ill;
8870 	mutex_enter(&connp->conn_lock);
8871 	mutex_enter(&ill->ill_lock);
8872 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8873 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8874 		    mp, 0);
8875 	} else {
8876 		success = ill_pending_mp_add(ill, connp, mp);
8877 	}
8878 	mutex_exit(&ill->ill_lock);
8879 	mutex_exit(&connp->conn_lock);
8880 
8881 	if (success) {
8882 		ip1dbg(("sending down tunparam request "));
8883 		putnext(ill->ill_wq, mp1);
8884 		return (EINPROGRESS);
8885 	} else {
8886 		/* The conn has started closing */
8887 		freemsg(mp1);
8888 		return (EINTR);
8889 	}
8890 }
8891 
8892 static int
8893 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8894     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8895 {
8896 	mblk_t *mp1;
8897 	mblk_t *mp2;
8898 	mblk_t *pending_mp;
8899 	ipaddr_t ipaddr;
8900 	area_t *area;
8901 	struct iocblk *iocp;
8902 	conn_t *connp;
8903 	struct arpreq *ar;
8904 	struct xarpreq *xar;
8905 	boolean_t success;
8906 	int flags, alength;
8907 	char *lladdr;
8908 
8909 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8910 	connp = Q_TO_CONN(q);
8911 
8912 	iocp = (struct iocblk *)mp->b_rptr;
8913 	/*
8914 	 * ill has already been set depending on whether
8915 	 * bsd style or interface style ioctl.
8916 	 */
8917 	ASSERT(ill != NULL);
8918 
8919 	/*
8920 	 * Is this one of the new SIOC*XARP ioctls?
8921 	 */
8922 	if (x_arp_ioctl) {
8923 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8924 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8925 		ar = NULL;
8926 
8927 		flags = xar->xarp_flags;
8928 		lladdr = LLADDR(&xar->xarp_ha);
8929 		/*
8930 		 * Validate against user's link layer address length
8931 		 * input and name and addr length limits.
8932 		 */
8933 		alength = ill->ill_phys_addr_length;
8934 		if (iocp->ioc_cmd == SIOCSXARP) {
8935 			if (alength != xar->xarp_ha.sdl_alen ||
8936 			    (alength + xar->xarp_ha.sdl_nlen >
8937 			    sizeof (xar->xarp_ha.sdl_data)))
8938 				return (EINVAL);
8939 		}
8940 	} else {
8941 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8942 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8943 		xar = NULL;
8944 
8945 		flags = ar->arp_flags;
8946 		lladdr = ar->arp_ha.sa_data;
8947 		/*
8948 		 * Theoretically, the sa_family could tell us what link
8949 		 * layer type this operation is trying to deal with. By
8950 		 * common usage AF_UNSPEC means ethernet. We'll assume
8951 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8952 		 * for now. Our new SIOC*XARP ioctls can be used more
8953 		 * generally.
8954 		 *
8955 		 * If the underlying media happens to have a non 6 byte
8956 		 * address, arp module will fail set/get, but the del
8957 		 * operation will succeed.
8958 		 */
8959 		alength = 6;
8960 		if ((iocp->ioc_cmd != SIOCDARP) &&
8961 		    (alength != ill->ill_phys_addr_length)) {
8962 			return (EINVAL);
8963 		}
8964 	}
8965 
8966 	/*
8967 	 * We are going to pass up to ARP a packet chain that looks
8968 	 * like:
8969 	 *
8970 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
8971 	 *
8972 	 * Get a copy of the original IOCTL mblk to head the chain,
8973 	 * to be sent up (in mp1). Also get another copy to store
8974 	 * in the ill_pending_mp list, for matching the response
8975 	 * when it comes back from ARP.
8976 	 */
8977 	mp1 = copyb(mp);
8978 	pending_mp = copymsg(mp);
8979 	if (mp1 == NULL || pending_mp == NULL) {
8980 		if (mp1 != NULL)
8981 			freeb(mp1);
8982 		if (pending_mp != NULL)
8983 			inet_freemsg(pending_mp);
8984 		return (ENOMEM);
8985 	}
8986 
8987 	ipaddr = sin->sin_addr.s_addr;
8988 
8989 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
8990 	    (caddr_t)&ipaddr);
8991 	if (mp2 == NULL) {
8992 		freeb(mp1);
8993 		inet_freemsg(pending_mp);
8994 		return (ENOMEM);
8995 	}
8996 	/* Put together the chain. */
8997 	mp1->b_cont = mp2;
8998 	mp1->b_datap->db_type = M_IOCTL;
8999 	mp2->b_cont = mp;
9000 	mp2->b_datap->db_type = M_DATA;
9001 
9002 	iocp = (struct iocblk *)mp1->b_rptr;
9003 
9004 	/*
9005 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9006 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9007 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9008 	 * ioc_count field; set ioc_count to be correct.
9009 	 */
9010 	iocp->ioc_count = MBLKL(mp1->b_cont);
9011 
9012 	/*
9013 	 * Set the proper command in the ARP message.
9014 	 * Convert the SIOC{G|S|D}ARP calls into our
9015 	 * AR_ENTRY_xxx calls.
9016 	 */
9017 	area = (area_t *)mp2->b_rptr;
9018 	switch (iocp->ioc_cmd) {
9019 	case SIOCDARP:
9020 	case SIOCDXARP:
9021 		/*
9022 		 * We defer deleting the corresponding IRE until
9023 		 * we return from arp.
9024 		 */
9025 		area->area_cmd = AR_ENTRY_DELETE;
9026 		area->area_proto_mask_offset = 0;
9027 		break;
9028 	case SIOCGARP:
9029 	case SIOCGXARP:
9030 		area->area_cmd = AR_ENTRY_SQUERY;
9031 		area->area_proto_mask_offset = 0;
9032 		break;
9033 	case SIOCSARP:
9034 	case SIOCSXARP: {
9035 		/*
9036 		 * Delete the corresponding ire to make sure IP will
9037 		 * pick up any change from arp.
9038 		 */
9039 		if (!if_arp_ioctl) {
9040 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9041 			break;
9042 		} else {
9043 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9044 			if (ipif != NULL) {
9045 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9046 				ipif_refrele(ipif);
9047 			}
9048 			break;
9049 		}
9050 	}
9051 	}
9052 	iocp->ioc_cmd = area->area_cmd;
9053 
9054 	/*
9055 	 * Before sending 'mp' to ARP, we have to clear the b_next
9056 	 * and b_prev. Otherwise if STREAMS encounters such a message
9057 	 * in freemsg(), (because ARP can close any time) it can cause
9058 	 * a panic. But mi code needs the b_next and b_prev values of
9059 	 * mp->b_cont, to complete the ioctl. So we store it here
9060 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9061 	 * when the response comes down from ARP.
9062 	 */
9063 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9064 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9065 	mp->b_cont->b_next = NULL;
9066 	mp->b_cont->b_prev = NULL;
9067 
9068 	mutex_enter(&connp->conn_lock);
9069 	mutex_enter(&ill->ill_lock);
9070 	/* conn has not yet started closing, hence this can't fail */
9071 	success = ill_pending_mp_add(ill, connp, pending_mp);
9072 	ASSERT(success);
9073 	mutex_exit(&ill->ill_lock);
9074 	mutex_exit(&connp->conn_lock);
9075 
9076 	/*
9077 	 * Fill in the rest of the ARP operation fields.
9078 	 */
9079 	area->area_hw_addr_length = alength;
9080 	bcopy(lladdr,
9081 	    (char *)area + area->area_hw_addr_offset,
9082 	    area->area_hw_addr_length);
9083 	/* Translate the flags. */
9084 	if (flags & ATF_PERM)
9085 		area->area_flags |= ACE_F_PERMANENT;
9086 	if (flags & ATF_PUBL)
9087 		area->area_flags |= ACE_F_PUBLISH;
9088 
9089 	/*
9090 	 * Up to ARP it goes.  The response will come
9091 	 * back in ip_wput as an M_IOCACK message, and
9092 	 * will be handed to ip_sioctl_iocack for
9093 	 * completion.
9094 	 */
9095 	putnext(ill->ill_rq, mp1);
9096 	return (EINPROGRESS);
9097 }
9098 
9099 /* ARGSUSED */
9100 int
9101 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9102     ip_ioctl_cmd_t *ipip, void *ifreq)
9103 {
9104 	struct xarpreq *xar;
9105 	boolean_t isv6;
9106 	mblk_t	*mp1;
9107 	int	err;
9108 	conn_t	*connp;
9109 	int ifnamelen;
9110 	ire_t	*ire = NULL;
9111 	ill_t	*ill = NULL;
9112 	struct sockaddr_in *sin;
9113 	boolean_t if_arp_ioctl = B_FALSE;
9114 
9115 	/* ioctl comes down on an conn */
9116 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9117 	connp = Q_TO_CONN(q);
9118 	isv6 = connp->conn_af_isv6;
9119 
9120 	/* Existance verified in ip_wput_nondata */
9121 	mp1 = mp->b_cont->b_cont;
9122 
9123 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9124 	xar = (struct xarpreq *)mp1->b_rptr;
9125 	sin = (sin_t *)&xar->xarp_pa;
9126 
9127 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9128 	    (xar->xarp_pa.ss_family != AF_INET))
9129 		return (ENXIO);
9130 
9131 	ifnamelen = xar->xarp_ha.sdl_nlen;
9132 	if (ifnamelen != 0) {
9133 		char	*cptr, cval;
9134 
9135 		if (ifnamelen >= LIFNAMSIZ)
9136 			return (EINVAL);
9137 
9138 		/*
9139 		 * Instead of bcopying a bunch of bytes,
9140 		 * null-terminate the string in-situ.
9141 		 */
9142 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9143 		cval = *cptr;
9144 		*cptr = '\0';
9145 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9146 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9147 		    &err, NULL);
9148 		*cptr = cval;
9149 		if (ill == NULL)
9150 			return (err);
9151 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9152 			ill_refrele(ill);
9153 			return (ENXIO);
9154 		}
9155 
9156 		if_arp_ioctl = B_TRUE;
9157 	} else {
9158 		/*
9159 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9160 		 * as an extended BSD ioctl. The kernel uses the IP address
9161 		 * to figure out the network interface.
9162 		 */
9163 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9164 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9165 		    ((ill = ire_to_ill(ire)) == NULL) ||
9166 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9167 			if (ire != NULL)
9168 				ire_refrele(ire);
9169 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9170 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9171 			    NULL, MATCH_IRE_TYPE);
9172 			if ((ire == NULL) ||
9173 			    ((ill = ire_to_ill(ire)) == NULL)) {
9174 				if (ire != NULL)
9175 					ire_refrele(ire);
9176 				return (ENXIO);
9177 			}
9178 		}
9179 		ASSERT(ire != NULL && ill != NULL);
9180 	}
9181 
9182 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9183 	if (if_arp_ioctl)
9184 		ill_refrele(ill);
9185 	if (ire != NULL)
9186 		ire_refrele(ire);
9187 
9188 	return (err);
9189 }
9190 
9191 /*
9192  * ARP IOCTLs.
9193  * How does IP get in the business of fronting ARP configuration/queries?
9194  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9195  * are by tradition passed in through a datagram socket.  That lands in IP.
9196  * As it happens, this is just as well since the interface is quite crude in
9197  * that it passes in no information about protocol or hardware types, or
9198  * interface association.  After making the protocol assumption, IP is in
9199  * the position to look up the name of the ILL, which ARP will need, and
9200  * format a request that can be handled by ARP.	 The request is passed up
9201  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9202  * back a response.  ARP supports its own set of more general IOCTLs, in
9203  * case anyone is interested.
9204  */
9205 /* ARGSUSED */
9206 int
9207 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9208     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9209 {
9210 	struct arpreq *ar;
9211 	struct sockaddr_in *sin;
9212 	ire_t	*ire;
9213 	boolean_t isv6;
9214 	mblk_t	*mp1;
9215 	int	err;
9216 	conn_t	*connp;
9217 	ill_t	*ill;
9218 
9219 	/* ioctl comes down on an conn */
9220 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9221 	connp = Q_TO_CONN(q);
9222 	isv6 = connp->conn_af_isv6;
9223 	if (isv6)
9224 		return (ENXIO);
9225 
9226 	/* Existance verified in ip_wput_nondata */
9227 	mp1 = mp->b_cont->b_cont;
9228 
9229 	ar = (struct arpreq *)mp1->b_rptr;
9230 	sin = (sin_t *)&ar->arp_pa;
9231 
9232 	/*
9233 	 * We need to let ARP know on which interface the IP
9234 	 * address has an ARP mapping. In the IPMP case, a
9235 	 * simple forwarding table lookup will return the
9236 	 * IRE_IF_RESOLVER for the first interface in the group,
9237 	 * which might not be the interface on which the
9238 	 * requested IP address was resolved due to the ill
9239 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9240 	 * So we do a cache table lookup first: if the IRE cache
9241 	 * entry for the IP address is still there, it will
9242 	 * contain the ill pointer for the right interface, so
9243 	 * we use that. If the cache entry has been flushed, we
9244 	 * fall back to the forwarding table lookup. This should
9245 	 * be rare enough since IRE cache entries have a longer
9246 	 * life expectancy than ARP cache entries.
9247 	 */
9248 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9249 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9250 	    ((ill = ire_to_ill(ire)) == NULL)) {
9251 		if (ire != NULL)
9252 			ire_refrele(ire);
9253 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9254 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9255 		    NULL, MATCH_IRE_TYPE);
9256 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9257 			if (ire != NULL)
9258 				ire_refrele(ire);
9259 			return (ENXIO);
9260 		}
9261 	}
9262 	ASSERT(ire != NULL && ill != NULL);
9263 
9264 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9265 	ire_refrele(ire);
9266 	return (err);
9267 }
9268 
9269 /*
9270  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9271  * atomically set/clear the muxids. Also complete the ioctl by acking or
9272  * naking it.  Note that the code is structured such that the link type,
9273  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9274  * its clones use the persistent link, while pppd(1M) and perhaps many
9275  * other daemons may use non-persistent link.  When combined with some
9276  * ill_t states, linking and unlinking lower streams may be used as
9277  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9278  */
9279 /* ARGSUSED */
9280 void
9281 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9282 {
9283 	mblk_t *mp1;
9284 	mblk_t *mp2;
9285 	struct linkblk *li;
9286 	queue_t	*ipwq;
9287 	char	*name;
9288 	struct qinit *qinfo;
9289 	struct ipmx_s *ipmxp;
9290 	ill_t	*ill = NULL;
9291 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9292 	int	err = 0;
9293 	boolean_t	entered_ipsq = B_FALSE;
9294 	boolean_t islink;
9295 	queue_t *dwq = NULL;
9296 
9297 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9298 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9299 
9300 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9301 	    B_TRUE : B_FALSE;
9302 
9303 	mp1 = mp->b_cont;	/* This is the linkblk info */
9304 	li = (struct linkblk *)mp1->b_rptr;
9305 
9306 	/*
9307 	 * ARP has added this special mblk, and the utility is asking us
9308 	 * to perform consistency checks, and also atomically set the
9309 	 * muxid. Ifconfig is an example.  It achieves this by using
9310 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9311 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9312 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9313 	 * and other comments in this routine for more details.
9314 	 */
9315 	mp2 = mp1->b_cont;	/* This is added by ARP */
9316 
9317 	/*
9318 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9319 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9320 	 * get the special mblk above.  For backward compatibility, we just
9321 	 * return success.  The utility will use SIOCSLIFMUXID to store
9322 	 * the muxids.  This is not atomic, and can leave the streams
9323 	 * unplumbable if the utility is interrrupted, before it does the
9324 	 * SIOCSLIFMUXID.
9325 	 */
9326 	if (mp2 == NULL) {
9327 		/*
9328 		 * At this point we don't know whether or not this is the
9329 		 * IP module stream or the ARP device stream.  We need to
9330 		 * walk the lower stream in order to find this out, since
9331 		 * the capability negotiation is done only on the IP module
9332 		 * stream.  IP module instance is identified by the module
9333 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9334 		 * STREAMS ensures that the lower stream (l_qbot) will not
9335 		 * vanish until this ioctl completes. So we can safely walk
9336 		 * the stream or refer to the q_ptr.
9337 		 */
9338 		ipwq = li->l_qbot;
9339 		while (ipwq != NULL) {
9340 			qinfo = ipwq->q_qinfo;
9341 			name = qinfo->qi_minfo->mi_idname;
9342 			if (name != NULL && name[0] != NULL &&
9343 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9344 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9345 			    (ipwq->q_next != NULL)) {
9346 				break;
9347 			}
9348 			ipwq = ipwq->q_next;
9349 		}
9350 		/*
9351 		 * This looks like an IP module stream, so trigger
9352 		 * the capability reset or re-negotiation if necessary.
9353 		 */
9354 		if (ipwq != NULL) {
9355 			ill = ipwq->q_ptr;
9356 			ASSERT(ill != NULL);
9357 
9358 			if (ipsq == NULL) {
9359 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9360 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9361 				if (ipsq == NULL)
9362 					return;
9363 				entered_ipsq = B_TRUE;
9364 			}
9365 			ASSERT(IAM_WRITER_ILL(ill));
9366 			/*
9367 			 * Store the upper read queue of the module
9368 			 * immediately below IP, and count the total
9369 			 * number of lower modules.  Do this only
9370 			 * for I_PLINK or I_LINK event.
9371 			 */
9372 			ill->ill_lmod_rq = NULL;
9373 			ill->ill_lmod_cnt = 0;
9374 			if (islink && (dwq = ipwq->q_next) != NULL) {
9375 				ill->ill_lmod_rq = RD(dwq);
9376 
9377 				while (dwq != NULL) {
9378 					ill->ill_lmod_cnt++;
9379 					dwq = dwq->q_next;
9380 				}
9381 			}
9382 			/*
9383 			 * There's no point in resetting or re-negotiating if
9384 			 * we are not bound to the driver, so only do this if
9385 			 * the DLPI state is idle (up); we assume such state
9386 			 * since ill_ipif_up_count gets incremented in
9387 			 * ipif_up_done(), which is after we are bound to the
9388 			 * driver.  Note that in the case of logical
9389 			 * interfaces, IP won't rebind to the driver unless
9390 			 * the ill_ipif_up_count is 0, meaning that all other
9391 			 * IP interfaces (including the main ipif) are in the
9392 			 * down state.  Because of this, we use such counter
9393 			 * as an indicator, instead of relying on the IPIF_UP
9394 			 * flag, which is per ipif instance.
9395 			 */
9396 			if (ill->ill_ipif_up_count > 0) {
9397 				if (islink)
9398 					ill_capability_probe(ill);
9399 				else
9400 					ill_capability_reset(ill);
9401 			}
9402 		}
9403 		goto done;
9404 	}
9405 
9406 	/*
9407 	 * This is an I_{P}LINK sent down by ifconfig on
9408 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9409 	 * giving more info. STREAMS ensures that the lower
9410 	 * stream (l_qbot) will not vanish until this ioctl
9411 	 * completes. So we can safely walk the stream or refer
9412 	 * to the q_ptr.
9413 	 */
9414 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9415 	if (ipmxp->ipmx_arpdev_stream) {
9416 		/*
9417 		 * The operation is occuring on the arp-device
9418 		 * stream.
9419 		 */
9420 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9421 		    q, mp, ip_sioctl_plink, &err, NULL);
9422 		if (ill == NULL) {
9423 			if (err == EINPROGRESS) {
9424 				return;
9425 			} else {
9426 				err = EINVAL;
9427 				goto done;
9428 			}
9429 		}
9430 
9431 		if (ipsq == NULL) {
9432 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9433 			    NEW_OP, B_TRUE);
9434 			if (ipsq == NULL) {
9435 				ill_refrele(ill);
9436 				return;
9437 			}
9438 			entered_ipsq = B_TRUE;
9439 		}
9440 		ASSERT(IAM_WRITER_ILL(ill));
9441 		ill_refrele(ill);
9442 		/*
9443 		 * To ensure consistency between IP and ARP,
9444 		 * the following LIFO scheme is used in
9445 		 * plink/punlink. (IP first, ARP last).
9446 		 * This is because the muxid's are stored
9447 		 * in the IP stream on the ill.
9448 		 *
9449 		 * I_{P}LINK: ifconfig plinks the IP stream before
9450 		 * plinking the ARP stream. On an arp-dev
9451 		 * stream, IP checks that it is not yet
9452 		 * plinked, and it also checks that the
9453 		 * corresponding IP stream is already plinked.
9454 		 *
9455 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9456 		 * before punlinking the IP stream. IP does
9457 		 * not allow punlink of the IP stream unless
9458 		 * the arp stream has been punlinked.
9459 		 *
9460 		 */
9461 		if ((islink &&
9462 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9463 		    (!islink &&
9464 		    ill->ill_arp_muxid != li->l_index)) {
9465 			err = EINVAL;
9466 			goto done;
9467 		}
9468 		if (islink) {
9469 			ill->ill_arp_muxid = li->l_index;
9470 		} else {
9471 			ill->ill_arp_muxid = 0;
9472 		}
9473 	} else {
9474 		/*
9475 		 * This must be the IP module stream with or
9476 		 * without arp. Walk the stream and locate the
9477 		 * IP module. An IP module instance is
9478 		 * identified by the module name IP, non-null
9479 		 * q_next, and it's wput not being ip_lwput.
9480 		 */
9481 		ipwq = li->l_qbot;
9482 		while (ipwq != NULL) {
9483 			qinfo = ipwq->q_qinfo;
9484 			name = qinfo->qi_minfo->mi_idname;
9485 			if (name != NULL && name[0] != NULL &&
9486 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9487 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9488 			    (ipwq->q_next != NULL)) {
9489 				break;
9490 			}
9491 			ipwq = ipwq->q_next;
9492 		}
9493 		if (ipwq != NULL) {
9494 			ill = ipwq->q_ptr;
9495 			ASSERT(ill != NULL);
9496 
9497 			if (ipsq == NULL) {
9498 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9499 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9500 				if (ipsq == NULL)
9501 					return;
9502 				entered_ipsq = B_TRUE;
9503 			}
9504 			ASSERT(IAM_WRITER_ILL(ill));
9505 			/*
9506 			 * Return error if the ip_mux_id is
9507 			 * non-zero and command is I_{P}LINK.
9508 			 * If command is I_{P}UNLINK, return
9509 			 * error if the arp-devstr is not
9510 			 * yet punlinked.
9511 			 */
9512 			if ((islink && ill->ill_ip_muxid != 0) ||
9513 			    (!islink && ill->ill_arp_muxid != 0)) {
9514 				err = EINVAL;
9515 				goto done;
9516 			}
9517 			ill->ill_lmod_rq = NULL;
9518 			ill->ill_lmod_cnt = 0;
9519 			if (islink) {
9520 				/*
9521 				 * Store the upper read queue of the module
9522 				 * immediately below IP, and count the total
9523 				 * number of lower modules.
9524 				 */
9525 				if ((dwq = ipwq->q_next) != NULL) {
9526 					ill->ill_lmod_rq = RD(dwq);
9527 
9528 					while (dwq != NULL) {
9529 						ill->ill_lmod_cnt++;
9530 						dwq = dwq->q_next;
9531 					}
9532 				}
9533 				ill->ill_ip_muxid = li->l_index;
9534 			} else {
9535 				ill->ill_ip_muxid = 0;
9536 			}
9537 
9538 			/*
9539 			 * See comments above about resetting/re-
9540 			 * negotiating driver sub-capabilities.
9541 			 */
9542 			if (ill->ill_ipif_up_count > 0) {
9543 				if (islink)
9544 					ill_capability_probe(ill);
9545 				else
9546 					ill_capability_reset(ill);
9547 			}
9548 		}
9549 	}
9550 done:
9551 	iocp->ioc_count = 0;
9552 	iocp->ioc_error = err;
9553 	if (err == 0)
9554 		mp->b_datap->db_type = M_IOCACK;
9555 	else
9556 		mp->b_datap->db_type = M_IOCNAK;
9557 	qreply(q, mp);
9558 
9559 	/* Conn was refheld in ip_sioctl_copyin_setup */
9560 	if (CONN_Q(q))
9561 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9562 	if (entered_ipsq)
9563 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9564 }
9565 
9566 /*
9567  * Search the ioctl command in the ioctl tables and return a pointer
9568  * to the ioctl command information. The ioctl command tables are
9569  * static and fully populated at compile time.
9570  */
9571 ip_ioctl_cmd_t *
9572 ip_sioctl_lookup(int ioc_cmd)
9573 {
9574 	int index;
9575 	ip_ioctl_cmd_t *ipip;
9576 	ip_ioctl_cmd_t *ipip_end;
9577 
9578 	if (ioc_cmd == IPI_DONTCARE)
9579 		return (NULL);
9580 
9581 	/*
9582 	 * Do a 2 step search. First search the indexed table
9583 	 * based on the least significant byte of the ioctl cmd.
9584 	 * If we don't find a match, then search the misc table
9585 	 * serially.
9586 	 */
9587 	index = ioc_cmd & 0xFF;
9588 	if (index < ip_ndx_ioctl_count) {
9589 		ipip = &ip_ndx_ioctl_table[index];
9590 		if (ipip->ipi_cmd == ioc_cmd) {
9591 			/* Found a match in the ndx table */
9592 			return (ipip);
9593 		}
9594 	}
9595 
9596 	/* Search the misc table */
9597 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9598 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9599 		if (ipip->ipi_cmd == ioc_cmd)
9600 			/* Found a match in the misc table */
9601 			return (ipip);
9602 	}
9603 
9604 	return (NULL);
9605 }
9606 
9607 /*
9608  * Wrapper function for resuming deferred ioctl processing
9609  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9610  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9611  */
9612 /* ARGSUSED */
9613 void
9614 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9615     void *dummy_arg)
9616 {
9617 	ip_sioctl_copyin_setup(q, mp);
9618 }
9619 
9620 /*
9621  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9622  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9623  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9624  * We establish here the size of the block to be copied in.  mi_copyin
9625  * arranges for this to happen, an processing continues in ip_wput with
9626  * an M_IOCDATA message.
9627  */
9628 void
9629 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9630 {
9631 	int	copyin_size;
9632 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9633 	ip_ioctl_cmd_t *ipip;
9634 	cred_t *cr;
9635 
9636 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9637 	if (ipip == NULL) {
9638 		/*
9639 		 * The ioctl is not one we understand or own.
9640 		 * Pass it along to be processed down stream,
9641 		 * if this is a module instance of IP, else nak
9642 		 * the ioctl.
9643 		 */
9644 		if (q->q_next == NULL) {
9645 			goto nak;
9646 		} else {
9647 			putnext(q, mp);
9648 			return;
9649 		}
9650 	}
9651 
9652 	/*
9653 	 * If this is deferred, then we will do all the checks when we
9654 	 * come back.
9655 	 */
9656 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9657 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9658 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9659 		return;
9660 	}
9661 
9662 	/*
9663 	 * Only allow a very small subset of IP ioctls on this stream if
9664 	 * IP is a module and not a driver. Allowing ioctls to be processed
9665 	 * in this case may cause assert failures or data corruption.
9666 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9667 	 * ioctls allowed on an IP module stream, after which this stream
9668 	 * normally becomes a multiplexor (at which time the stream head
9669 	 * will fail all ioctls).
9670 	 */
9671 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9672 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9673 			/*
9674 			 * Pass common Streams ioctls which the IP
9675 			 * module does not own or consume along to
9676 			 * be processed down stream.
9677 			 */
9678 			putnext(q, mp);
9679 			return;
9680 		} else {
9681 			goto nak;
9682 		}
9683 	}
9684 
9685 	/* Make sure we have ioctl data to process. */
9686 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9687 		goto nak;
9688 
9689 	/*
9690 	 * Prefer dblk credential over ioctl credential; some synthesized
9691 	 * ioctls have kcred set because there's no way to crhold()
9692 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9693 	 * the framework; the caller of ioctl needs to hold the reference
9694 	 * for the duration of the call).
9695 	 */
9696 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9697 
9698 	/* Make sure normal users don't send down privileged ioctls */
9699 	if ((ipip->ipi_flags & IPI_PRIV) &&
9700 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9701 		/* We checked the privilege earlier but log it here */
9702 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9703 		return;
9704 	}
9705 
9706 	/*
9707 	 * The ioctl command tables can only encode fixed length
9708 	 * ioctl data. If the length is variable, the table will
9709 	 * encode the length as zero. Such special cases are handled
9710 	 * below in the switch.
9711 	 */
9712 	if (ipip->ipi_copyin_size != 0) {
9713 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9714 		return;
9715 	}
9716 
9717 	switch (iocp->ioc_cmd) {
9718 	case O_SIOCGIFCONF:
9719 	case SIOCGIFCONF:
9720 		/*
9721 		 * This IOCTL is hilarious.  See comments in
9722 		 * ip_sioctl_get_ifconf for the story.
9723 		 */
9724 		if (iocp->ioc_count == TRANSPARENT)
9725 			copyin_size = SIZEOF_STRUCT(ifconf,
9726 			    iocp->ioc_flag);
9727 		else
9728 			copyin_size = iocp->ioc_count;
9729 		mi_copyin(q, mp, NULL, copyin_size);
9730 		return;
9731 
9732 	case O_SIOCGLIFCONF:
9733 	case SIOCGLIFCONF:
9734 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9735 		mi_copyin(q, mp, NULL, copyin_size);
9736 		return;
9737 
9738 	case SIOCGLIFSRCOF:
9739 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9740 		mi_copyin(q, mp, NULL, copyin_size);
9741 		return;
9742 	case SIOCGIP6ADDRPOLICY:
9743 		ip_sioctl_ip6addrpolicy(q, mp);
9744 		ip6_asp_table_refrele();
9745 		return;
9746 
9747 	case SIOCSIP6ADDRPOLICY:
9748 		ip_sioctl_ip6addrpolicy(q, mp);
9749 		return;
9750 
9751 	case SIOCGDSTINFO:
9752 		ip_sioctl_dstinfo(q, mp);
9753 		ip6_asp_table_refrele();
9754 		return;
9755 
9756 	case I_PLINK:
9757 	case I_PUNLINK:
9758 	case I_LINK:
9759 	case I_UNLINK:
9760 		/*
9761 		 * We treat non-persistent link similarly as the persistent
9762 		 * link case, in terms of plumbing/unplumbing, as well as
9763 		 * dynamic re-plumbing events indicator.  See comments
9764 		 * in ip_sioctl_plink() for more.
9765 		 *
9766 		 * Request can be enqueued in the 'ipsq' while waiting
9767 		 * to become exclusive. So bump up the conn ref.
9768 		 */
9769 		if (CONN_Q(q))
9770 			CONN_INC_REF(Q_TO_CONN(q));
9771 		ip_sioctl_plink(NULL, q, mp, NULL);
9772 		return;
9773 
9774 	case ND_GET:
9775 	case ND_SET:
9776 		/*
9777 		 * Use of the nd table requires holding the reader lock.
9778 		 * Modifying the nd table thru nd_load/nd_unload requires
9779 		 * the writer lock.
9780 		 */
9781 		rw_enter(&ip_g_nd_lock, RW_READER);
9782 		if (nd_getset(q, ip_g_nd, mp)) {
9783 			rw_exit(&ip_g_nd_lock);
9784 
9785 			if (iocp->ioc_error)
9786 				iocp->ioc_count = 0;
9787 			mp->b_datap->db_type = M_IOCACK;
9788 			qreply(q, mp);
9789 			return;
9790 		}
9791 		rw_exit(&ip_g_nd_lock);
9792 		/*
9793 		 * We don't understand this subioctl of ND_GET / ND_SET.
9794 		 * Maybe intended for some driver / module below us
9795 		 */
9796 		if (q->q_next) {
9797 			putnext(q, mp);
9798 		} else {
9799 			iocp->ioc_error = ENOENT;
9800 			mp->b_datap->db_type = M_IOCNAK;
9801 			iocp->ioc_count = 0;
9802 			qreply(q, mp);
9803 		}
9804 		return;
9805 
9806 	case IP_IOCTL:
9807 		ip_wput_ioctl(q, mp);
9808 		return;
9809 	default:
9810 		cmn_err(CE_PANIC, "should not happen ");
9811 	}
9812 nak:
9813 	if (mp->b_cont != NULL) {
9814 		freemsg(mp->b_cont);
9815 		mp->b_cont = NULL;
9816 	}
9817 	iocp->ioc_error = EINVAL;
9818 	mp->b_datap->db_type = M_IOCNAK;
9819 	iocp->ioc_count = 0;
9820 	qreply(q, mp);
9821 }
9822 
9823 /* ip_wput hands off ARP IOCTL responses to us */
9824 void
9825 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9826 {
9827 	struct arpreq *ar;
9828 	struct xarpreq *xar;
9829 	area_t	*area;
9830 	mblk_t	*area_mp;
9831 	struct iocblk *iocp;
9832 	mblk_t	*orig_ioc_mp, *tmp;
9833 	struct iocblk	*orig_iocp;
9834 	ill_t *ill;
9835 	conn_t *connp = NULL;
9836 	uint_t ioc_id;
9837 	mblk_t *pending_mp;
9838 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9839 	int *flagsp;
9840 	char *storage = NULL;
9841 	sin_t *sin;
9842 	ipaddr_t addr;
9843 	int err;
9844 
9845 	ill = q->q_ptr;
9846 	ASSERT(ill != NULL);
9847 
9848 	/*
9849 	 * We should get back from ARP a packet chain that looks like:
9850 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9851 	 */
9852 	if (!(area_mp = mp->b_cont) ||
9853 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9854 	    !(orig_ioc_mp = area_mp->b_cont) ||
9855 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9856 		freemsg(mp);
9857 		return;
9858 	}
9859 
9860 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9861 
9862 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9863 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9864 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9865 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9866 		x_arp_ioctl = B_TRUE;
9867 		xar = (struct xarpreq *)tmp->b_rptr;
9868 		sin = (sin_t *)&xar->xarp_pa;
9869 		flagsp = &xar->xarp_flags;
9870 		storage = xar->xarp_ha.sdl_data;
9871 		if (xar->xarp_ha.sdl_nlen != 0)
9872 			ifx_arp_ioctl = B_TRUE;
9873 	} else {
9874 		ar = (struct arpreq *)tmp->b_rptr;
9875 		sin = (sin_t *)&ar->arp_pa;
9876 		flagsp = &ar->arp_flags;
9877 		storage = ar->arp_ha.sa_data;
9878 	}
9879 
9880 	iocp = (struct iocblk *)mp->b_rptr;
9881 
9882 	/*
9883 	 * Pick out the originating queue based on the ioc_id.
9884 	 */
9885 	ioc_id = iocp->ioc_id;
9886 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9887 	if (pending_mp == NULL) {
9888 		ASSERT(connp == NULL);
9889 		inet_freemsg(mp);
9890 		return;
9891 	}
9892 	ASSERT(connp != NULL);
9893 	q = CONNP_TO_WQ(connp);
9894 
9895 	/* Uncouple the internally generated IOCTL from the original one */
9896 	area = (area_t *)area_mp->b_rptr;
9897 	area_mp->b_cont = NULL;
9898 
9899 	/*
9900 	 * Restore the b_next and b_prev used by mi code. This is needed
9901 	 * to complete the ioctl using mi* functions. We stored them in
9902 	 * the pending mp prior to sending the request to ARP.
9903 	 */
9904 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9905 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9906 	inet_freemsg(pending_mp);
9907 
9908 	/*
9909 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9910 	 * Catch the case where there is an IRE_CACHE by no entry in the
9911 	 * arp table.
9912 	 */
9913 	addr = sin->sin_addr.s_addr;
9914 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9915 		ire_t			*ire;
9916 		dl_unitdata_req_t	*dlup;
9917 		mblk_t			*llmp;
9918 		int			addr_len;
9919 		ill_t			*ipsqill = NULL;
9920 
9921 		if (ifx_arp_ioctl) {
9922 			/*
9923 			 * There's no need to lookup the ill, since
9924 			 * we've already done that when we started
9925 			 * processing the ioctl and sent the message
9926 			 * to ARP on that ill.  So use the ill that
9927 			 * is stored in q->q_ptr.
9928 			 */
9929 			ipsqill = ill;
9930 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9931 			    ipsqill->ill_ipif, ALL_ZONES,
9932 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
9933 		} else {
9934 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9935 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
9936 			if (ire != NULL)
9937 				ipsqill = ire_to_ill(ire);
9938 		}
9939 
9940 		if ((x_arp_ioctl) && (ipsqill != NULL))
9941 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9942 
9943 		if (ire != NULL) {
9944 			*flagsp = ATF_INUSE;
9945 			llmp = ire->ire_dlureq_mp;
9946 			if (llmp != NULL && ipsqill != NULL) {
9947 				uchar_t *macaddr;
9948 
9949 				addr_len = ipsqill->ill_phys_addr_length;
9950 				if (x_arp_ioctl && ((addr_len +
9951 				    ipsqill->ill_name_length) >
9952 				    sizeof (xar->xarp_ha.sdl_data))) {
9953 					ire_refrele(ire);
9954 					freemsg(mp);
9955 					ip_ioctl_finish(q, orig_ioc_mp,
9956 					    EINVAL, NO_COPYOUT, NULL, NULL);
9957 					return;
9958 				}
9959 				*flagsp |= ATF_COM;
9960 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
9961 				if (ipsqill->ill_sap_length < 0)
9962 					macaddr = llmp->b_rptr +
9963 					    dlup->dl_dest_addr_offset;
9964 				else
9965 					macaddr = llmp->b_rptr +
9966 					    dlup->dl_dest_addr_offset +
9967 					    ipsqill->ill_sap_length;
9968 				/*
9969 				 * For SIOCGARP, MAC address length
9970 				 * validation has already been done
9971 				 * before the ioctl was issued to ARP to
9972 				 * allow it to progress only on 6 byte
9973 				 * addressable (ethernet like) media. Thus
9974 				 * the mac address copying can not overwrite
9975 				 * the sa_data area below.
9976 				 */
9977 				bcopy(macaddr, storage, addr_len);
9978 			}
9979 			/* Ditch the internal IOCTL. */
9980 			freemsg(mp);
9981 			ire_refrele(ire);
9982 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
9983 			return;
9984 		}
9985 	}
9986 
9987 	/*
9988 	 * Delete the coresponding IRE_CACHE if any.
9989 	 * Reset the error if there was one (in case there was no entry
9990 	 * in arp.)
9991 	 */
9992 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
9993 		ipif_t *ipintf = NULL;
9994 
9995 		if (ifx_arp_ioctl) {
9996 			/*
9997 			 * There's no need to lookup the ill, since
9998 			 * we've already done that when we started
9999 			 * processing the ioctl and sent the message
10000 			 * to ARP on that ill.  So use the ill that
10001 			 * is stored in q->q_ptr.
10002 			 */
10003 			ipintf = ill->ill_ipif;
10004 		}
10005 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10006 			/*
10007 			 * The address in "addr" may be an entry for a
10008 			 * router. If that's true, then any off-net
10009 			 * IRE_CACHE entries that go through the router
10010 			 * with address "addr" must be clobbered. Use
10011 			 * ire_walk to achieve this goal.
10012 			 */
10013 			if (ifx_arp_ioctl)
10014 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10015 				    ire_delete_cache_gw, (char *)&addr, ill);
10016 			else
10017 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10018 				    ALL_ZONES);
10019 			iocp->ioc_error = 0;
10020 		}
10021 	}
10022 
10023 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10024 		err = iocp->ioc_error;
10025 		freemsg(mp);
10026 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
10027 		return;
10028 	}
10029 
10030 	/*
10031 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10032 	 * the area_t into the struct {x}arpreq.
10033 	 */
10034 	if (x_arp_ioctl) {
10035 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10036 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10037 		    sizeof (xar->xarp_ha.sdl_data)) {
10038 			freemsg(mp);
10039 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
10040 			    NO_COPYOUT, NULL, NULL);
10041 			return;
10042 		}
10043 	}
10044 	*flagsp = ATF_INUSE;
10045 	if (area->area_flags & ACE_F_PERMANENT)
10046 		*flagsp |= ATF_PERM;
10047 	if (area->area_flags & ACE_F_PUBLISH)
10048 		*flagsp |= ATF_PUBL;
10049 	if (area->area_hw_addr_length != 0) {
10050 		*flagsp |= ATF_COM;
10051 		/*
10052 		 * For SIOCGARP, MAC address length validation has
10053 		 * already been done before the ioctl was issued to ARP
10054 		 * to allow it to progress only on 6 byte addressable
10055 		 * (ethernet like) media. Thus the mac address copying
10056 		 * can not overwrite the sa_data area below.
10057 		 */
10058 		bcopy((char *)area + area->area_hw_addr_offset,
10059 		    storage, area->area_hw_addr_length);
10060 	}
10061 
10062 	/* Ditch the internal IOCTL. */
10063 	freemsg(mp);
10064 	/* Complete the original. */
10065 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10066 }
10067 
10068 /*
10069  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10070  * interface) create the next available logical interface for this
10071  * physical interface.
10072  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10073  * ipif with the specified name.
10074  *
10075  * If the address family is not AF_UNSPEC then set the address as well.
10076  *
10077  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10078  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10079  *
10080  * Executed as a writer on the ill or ill group.
10081  * So no lock is needed to traverse the ipif chain, or examine the
10082  * phyint flags.
10083  */
10084 /* ARGSUSED */
10085 int
10086 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10087     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10088 {
10089 	mblk_t	*mp1;
10090 	struct lifreq *lifr;
10091 	boolean_t	isv6;
10092 	boolean_t	exists;
10093 	char 	*name;
10094 	char	*endp;
10095 	char	*cp;
10096 	int	namelen;
10097 	ipif_t	*ipif;
10098 	long	id;
10099 	ipsq_t	*ipsq;
10100 	ill_t	*ill;
10101 	sin_t	*sin;
10102 	int	err = 0;
10103 	boolean_t found_sep = B_FALSE;
10104 	conn_t	*connp;
10105 	zoneid_t zoneid;
10106 	int	orig_ifindex = 0;
10107 
10108 	ip1dbg(("ip_sioctl_addif\n"));
10109 	/* Existence of mp1 has been checked in ip_wput_nondata */
10110 	mp1 = mp->b_cont->b_cont;
10111 	/*
10112 	 * Null terminate the string to protect against buffer
10113 	 * overrun. String was generated by user code and may not
10114 	 * be trusted.
10115 	 */
10116 	lifr = (struct lifreq *)mp1->b_rptr;
10117 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10118 	name = lifr->lifr_name;
10119 	ASSERT(CONN_Q(q));
10120 	connp = Q_TO_CONN(q);
10121 	isv6 = connp->conn_af_isv6;
10122 	zoneid = connp->conn_zoneid;
10123 	namelen = mi_strlen(name);
10124 	if (namelen == 0)
10125 		return (EINVAL);
10126 
10127 	exists = B_FALSE;
10128 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10129 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10130 		/*
10131 		 * Allow creating lo0 using SIOCLIFADDIF.
10132 		 * can't be any other writer thread. So can pass null below
10133 		 * for the last 4 args to ipif_lookup_name.
10134 		 */
10135 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10136 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10137 		/* Prevent any further action */
10138 		if (ipif == NULL) {
10139 			return (ENOBUFS);
10140 		} else if (!exists) {
10141 			/* We created the ipif now and as writer */
10142 			ipif_refrele(ipif);
10143 			return (0);
10144 		} else {
10145 			ill = ipif->ipif_ill;
10146 			ill_refhold(ill);
10147 			ipif_refrele(ipif);
10148 		}
10149 	} else {
10150 		/* Look for a colon in the name. */
10151 		endp = &name[namelen];
10152 		for (cp = endp; --cp > name; ) {
10153 			if (*cp == IPIF_SEPARATOR_CHAR) {
10154 				found_sep = B_TRUE;
10155 				/*
10156 				 * Reject any non-decimal aliases for plumbing
10157 				 * of logical interfaces. Aliases with leading
10158 				 * zeroes are also rejected as they introduce
10159 				 * ambiguity in the naming of the interfaces.
10160 				 * Comparing with "0" takes care of all such
10161 				 * cases.
10162 				 */
10163 				if ((strncmp("0", cp+1, 1)) == 0)
10164 					return (EINVAL);
10165 
10166 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10167 				    id <= 0 || *endp != '\0') {
10168 					return (EINVAL);
10169 				}
10170 				*cp = '\0';
10171 				break;
10172 			}
10173 		}
10174 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10175 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10176 		if (found_sep)
10177 			*cp = IPIF_SEPARATOR_CHAR;
10178 		if (ill == NULL)
10179 			return (err);
10180 	}
10181 
10182 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10183 	    B_TRUE);
10184 
10185 	/*
10186 	 * Release the refhold due to the lookup, now that we are excl
10187 	 * or we are just returning
10188 	 */
10189 	ill_refrele(ill);
10190 
10191 	if (ipsq == NULL)
10192 		return (EINPROGRESS);
10193 
10194 	/*
10195 	 * If the interface is failed, inactive or offlined, look for a working
10196 	 * interface in the ill group and create the ipif there. If we can't
10197 	 * find a good interface, create the ipif anyway so that in.mpathd can
10198 	 * move it to the first repaired interface.
10199 	 */
10200 	if ((ill->ill_phyint->phyint_flags &
10201 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10202 	    ill->ill_phyint->phyint_groupname_len != 0) {
10203 		phyint_t *phyi;
10204 		char *groupname = ill->ill_phyint->phyint_groupname;
10205 
10206 		/*
10207 		 * We're looking for a working interface, but it doesn't matter
10208 		 * if it's up or down; so instead of following the group lists,
10209 		 * we look at each physical interface and compare the groupname.
10210 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10211 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10212 		 * Otherwise we create the ipif on the failed interface.
10213 		 */
10214 		rw_enter(&ill_g_lock, RW_READER);
10215 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10216 		for (; phyi != NULL;
10217 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10218 		    phyi, AVL_AFTER)) {
10219 			if (phyi->phyint_groupname_len == 0)
10220 				continue;
10221 			ASSERT(phyi->phyint_groupname != NULL);
10222 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10223 			    !(phyi->phyint_flags &
10224 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10225 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10226 			    (phyi->phyint_illv4 != NULL))) {
10227 				break;
10228 			}
10229 		}
10230 		rw_exit(&ill_g_lock);
10231 
10232 		if (phyi != NULL) {
10233 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10234 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10235 			    phyi->phyint_illv4);
10236 		}
10237 	}
10238 
10239 	/*
10240 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10241 	 * before or after us.
10242 	 */
10243 	ASSERT(IAM_WRITER_ILL(ill));
10244 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10245 
10246 	if (found_sep && orig_ifindex == 0) {
10247 		/* Now see if there is an IPIF with this unit number. */
10248 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10249 			if (ipif->ipif_id == id) {
10250 				err = EEXIST;
10251 				goto done;
10252 			}
10253 		}
10254 	}
10255 
10256 	/*
10257 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10258 	 * of lo0. We never come here when we plumb lo0:0. It
10259 	 * happens in ipif_lookup_on_name.
10260 	 * The specified unit number is ignored when we create the ipif on a
10261 	 * different interface. However, we save it in ipif_orig_ipifid below so
10262 	 * that the ipif fails back to the right position.
10263 	 */
10264 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10265 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10266 		err = ENOBUFS;
10267 		goto done;
10268 	}
10269 
10270 	/* Return created name with ioctl */
10271 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10272 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10273 	ip1dbg(("created %s\n", lifr->lifr_name));
10274 
10275 	/* Set address */
10276 	sin = (sin_t *)&lifr->lifr_addr;
10277 	if (sin->sin_family != AF_UNSPEC) {
10278 		err = ip_sioctl_addr(ipif, sin, q, mp,
10279 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10280 	}
10281 
10282 	/* Set ifindex and unit number for failback */
10283 	if (err == 0 && orig_ifindex != 0) {
10284 		ipif->ipif_orig_ifindex = orig_ifindex;
10285 		if (found_sep) {
10286 			ipif->ipif_orig_ipifid = id;
10287 		}
10288 	}
10289 
10290 done:
10291 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10292 	return (err);
10293 }
10294 
10295 /*
10296  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10297  * interface) delete it based on the IP address (on this physical interface).
10298  * Otherwise delete it based on the ipif_id.
10299  * Also, special handling to allow a removeif of lo0.
10300  */
10301 /* ARGSUSED */
10302 int
10303 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10304     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10305 {
10306 	conn_t		*connp;
10307 	ill_t		*ill = ipif->ipif_ill;
10308 	boolean_t	 success;
10309 
10310 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10311 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10312 	ASSERT(IAM_WRITER_IPIF(ipif));
10313 
10314 	connp = Q_TO_CONN(q);
10315 	/*
10316 	 * Special case for unplumbing lo0 (the loopback physical interface).
10317 	 * If unplumbing lo0, the incoming address structure has been
10318 	 * initialized to all zeros. When unplumbing lo0, all its logical
10319 	 * interfaces must be removed too.
10320 	 *
10321 	 * Note that this interface may be called to remove a specific
10322 	 * loopback logical interface (eg, lo0:1). But in that case
10323 	 * ipif->ipif_id != 0 so that the code path for that case is the
10324 	 * same as any other interface (meaning it skips the code directly
10325 	 * below).
10326 	 */
10327 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10328 		if (sin->sin_family == AF_UNSPEC &&
10329 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10330 			/*
10331 			 * Mark it condemned. No new ref. will be made to ill.
10332 			 */
10333 			mutex_enter(&ill->ill_lock);
10334 			ill->ill_state_flags |= ILL_CONDEMNED;
10335 			for (ipif = ill->ill_ipif; ipif != NULL;
10336 			    ipif = ipif->ipif_next) {
10337 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10338 			}
10339 			mutex_exit(&ill->ill_lock);
10340 
10341 			ipif = ill->ill_ipif;
10342 			/* unplumb the loopback interface */
10343 			ill_delete(ill);
10344 			mutex_enter(&connp->conn_lock);
10345 			mutex_enter(&ill->ill_lock);
10346 			ASSERT(ill->ill_group == NULL);
10347 
10348 			/* Are any references to this ill active */
10349 			if (ill_is_quiescent(ill)) {
10350 				mutex_exit(&ill->ill_lock);
10351 				mutex_exit(&connp->conn_lock);
10352 				ill_delete_tail(ill);
10353 				mi_free(ill);
10354 				return (0);
10355 			}
10356 			success = ipsq_pending_mp_add(connp, ipif,
10357 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10358 			mutex_exit(&connp->conn_lock);
10359 			mutex_exit(&ill->ill_lock);
10360 			if (success)
10361 				return (EINPROGRESS);
10362 			else
10363 				return (EINTR);
10364 		}
10365 	}
10366 
10367 	/*
10368 	 * We are exclusive on the ipsq, so an ill move will be serialized
10369 	 * before or after us.
10370 	 */
10371 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10372 
10373 	if (ipif->ipif_id == 0) {
10374 		/* Find based on address */
10375 		if (ipif->ipif_isv6) {
10376 			sin6_t *sin6;
10377 
10378 			if (sin->sin_family != AF_INET6)
10379 				return (EAFNOSUPPORT);
10380 
10381 			sin6 = (sin6_t *)sin;
10382 			/* We are a writer, so we should be able to lookup */
10383 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10384 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10385 			if (ipif == NULL) {
10386 				/*
10387 				 * Maybe the address in on another interface in
10388 				 * the same IPMP group? We check this below.
10389 				 */
10390 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10391 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10392 			}
10393 		} else {
10394 			ipaddr_t addr;
10395 
10396 			if (sin->sin_family != AF_INET)
10397 				return (EAFNOSUPPORT);
10398 
10399 			addr = sin->sin_addr.s_addr;
10400 			/* We are a writer, so we should be able to lookup */
10401 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10402 			    NULL, NULL, NULL);
10403 			if (ipif == NULL) {
10404 				/*
10405 				 * Maybe the address in on another interface in
10406 				 * the same IPMP group? We check this below.
10407 				 */
10408 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10409 				    NULL, NULL, NULL, NULL);
10410 			}
10411 		}
10412 		if (ipif == NULL) {
10413 			return (EADDRNOTAVAIL);
10414 		}
10415 		/*
10416 		 * When the address to be removed is hosted on a different
10417 		 * interface, we check if the interface is in the same IPMP
10418 		 * group as the specified one; if so we proceed with the
10419 		 * removal.
10420 		 * ill->ill_group is NULL when the ill is down, so we have to
10421 		 * compare the group names instead.
10422 		 */
10423 		if (ipif->ipif_ill != ill &&
10424 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10425 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10426 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10427 		    ill->ill_phyint->phyint_groupname) != 0)) {
10428 			ipif_refrele(ipif);
10429 			return (EADDRNOTAVAIL);
10430 		}
10431 
10432 		/* This is a writer */
10433 		ipif_refrele(ipif);
10434 	}
10435 
10436 	/*
10437 	 * Can not delete instance zero since it is tied to the ill.
10438 	 */
10439 	if (ipif->ipif_id == 0)
10440 		return (EBUSY);
10441 
10442 	mutex_enter(&ill->ill_lock);
10443 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10444 	mutex_exit(&ill->ill_lock);
10445 
10446 	ipif_free(ipif);
10447 
10448 	mutex_enter(&connp->conn_lock);
10449 	mutex_enter(&ill->ill_lock);
10450 
10451 	/* Are any references to this ipif active */
10452 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10453 		mutex_exit(&ill->ill_lock);
10454 		mutex_exit(&connp->conn_lock);
10455 		ipif_down_tail(ipif);
10456 		ipif_free_tail(ipif);
10457 		return (0);
10458 	    }
10459 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10460 	    IPIF_FREE);
10461 	mutex_exit(&ill->ill_lock);
10462 	mutex_exit(&connp->conn_lock);
10463 	if (success)
10464 		return (EINPROGRESS);
10465 	else
10466 		return (EINTR);
10467 }
10468 
10469 /*
10470  * Restart the removeif ioctl. The refcnt has gone down to 0.
10471  * The ipif is already condemned. So can't find it thru lookups.
10472  */
10473 /* ARGSUSED */
10474 int
10475 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10476     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10477 {
10478 	ill_t *ill;
10479 
10480 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10481 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10482 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10483 		ill = ipif->ipif_ill;
10484 		ASSERT(IAM_WRITER_ILL(ill));
10485 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10486 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10487 		ill_delete_tail(ill);
10488 		mi_free(ill);
10489 		return (0);
10490 	}
10491 
10492 	ill = ipif->ipif_ill;
10493 	ASSERT(IAM_WRITER_IPIF(ipif));
10494 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10495 
10496 	ipif_down_tail(ipif);
10497 	ipif_free_tail(ipif);
10498 
10499 	ILL_UNMARK_CHANGING(ill);
10500 	return (0);
10501 }
10502 
10503 /*
10504  * Set the local interface address.
10505  * Allow an address of all zero when the interface is down.
10506  */
10507 /* ARGSUSED */
10508 int
10509 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10510     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10511 {
10512 	int err = 0;
10513 	in6_addr_t v6addr;
10514 	boolean_t need_up = B_FALSE;
10515 
10516 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10517 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10518 
10519 	ASSERT(IAM_WRITER_IPIF(ipif));
10520 
10521 	if (ipif->ipif_isv6) {
10522 		sin6_t *sin6;
10523 		ill_t *ill;
10524 		phyint_t *phyi;
10525 
10526 		if (sin->sin_family != AF_INET6)
10527 			return (EAFNOSUPPORT);
10528 
10529 		sin6 = (sin6_t *)sin;
10530 		v6addr = sin6->sin6_addr;
10531 		ill = ipif->ipif_ill;
10532 		phyi = ill->ill_phyint;
10533 
10534 		/*
10535 		 * Enforce that true multicast interfaces have a link-local
10536 		 * address for logical unit 0.
10537 		 */
10538 		if (ipif->ipif_id == 0 &&
10539 		    (ill->ill_flags & ILLF_MULTICAST) &&
10540 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10541 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10542 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10543 			return (EADDRNOTAVAIL);
10544 		}
10545 
10546 		/*
10547 		 * up interfaces shouldn't have the unspecified address
10548 		 * unless they also have the IPIF_NOLOCAL flags set and
10549 		 * have a subnet assigned.
10550 		 */
10551 		if ((ipif->ipif_flags & IPIF_UP) &&
10552 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10553 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10554 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10555 			return (EADDRNOTAVAIL);
10556 		}
10557 
10558 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10559 			return (EADDRNOTAVAIL);
10560 	} else {
10561 		ipaddr_t addr;
10562 
10563 		if (sin->sin_family != AF_INET)
10564 			return (EAFNOSUPPORT);
10565 
10566 		addr = sin->sin_addr.s_addr;
10567 
10568 		/* Allow 0 as the local address. */
10569 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10570 			return (EADDRNOTAVAIL);
10571 
10572 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10573 	}
10574 
10575 
10576 	/*
10577 	 * Even if there is no change we redo things just to rerun
10578 	 * ipif_set_default.
10579 	 */
10580 	if (ipif->ipif_flags & IPIF_UP) {
10581 		/*
10582 		 * Setting a new local address, make sure
10583 		 * we have net and subnet bcast ire's for
10584 		 * the old address if we need them.
10585 		 */
10586 		if (!ipif->ipif_isv6)
10587 			ipif_check_bcast_ires(ipif);
10588 		/*
10589 		 * If the interface is already marked up,
10590 		 * we call ipif_down which will take care
10591 		 * of ditching any IREs that have been set
10592 		 * up based on the old interface address.
10593 		 */
10594 		err = ipif_logical_down(ipif, q, mp);
10595 		if (err == EINPROGRESS)
10596 			return (err);
10597 		ipif_down_tail(ipif);
10598 		need_up = 1;
10599 	}
10600 
10601 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10602 	return (err);
10603 }
10604 
10605 int
10606 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10607     boolean_t need_up)
10608 {
10609 	in6_addr_t v6addr;
10610 	ipaddr_t addr;
10611 	sin6_t	*sin6;
10612 	int	err = 0;
10613 
10614 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10615 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10616 	ASSERT(IAM_WRITER_IPIF(ipif));
10617 	if (ipif->ipif_isv6) {
10618 		sin6 = (sin6_t *)sin;
10619 		v6addr = sin6->sin6_addr;
10620 	} else {
10621 		addr = sin->sin_addr.s_addr;
10622 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10623 	}
10624 	mutex_enter(&ipif->ipif_ill->ill_lock);
10625 	ipif->ipif_v6lcl_addr = v6addr;
10626 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10627 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10628 	} else {
10629 		ipif->ipif_v6src_addr = v6addr;
10630 	}
10631 
10632 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10633 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10634 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10635 
10636 		/*
10637 		 * The local address of this interface is a 6to4 address,
10638 		 * check if this interface is in fact a 6to4 tunnel or just
10639 		 * an interface configured with a 6to4 address.  We are only
10640 		 * interested in the former.
10641 		 */
10642 		if (wqp != NULL) {
10643 			while ((wqp->q_next != NULL) &&
10644 			    (wqp->q_next->q_qinfo != NULL) &&
10645 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10646 
10647 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10648 				    == TUN6TO4_MODID) {
10649 					/* set for use in IP */
10650 					ipif->ipif_ill->ill_is_6to4tun = 1;
10651 					break;
10652 				}
10653 				wqp = wqp->q_next;
10654 			}
10655 		}
10656 	}
10657 
10658 	ipif_set_default(ipif);
10659 	mutex_exit(&ipif->ipif_ill->ill_lock);
10660 
10661 	if (need_up) {
10662 		/*
10663 		 * Now bring the interface back up.  If this
10664 		 * is the only IPIF for the ILL, ipif_up
10665 		 * will have to re-bind to the device, so
10666 		 * we may get back EINPROGRESS, in which
10667 		 * case, this IOCTL will get completed in
10668 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10669 		 */
10670 		err = ipif_up(ipif, q, mp);
10671 	} else {
10672 		/*
10673 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10674 		 * if need_up is true.
10675 		 */
10676 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10677 	}
10678 
10679 	return (err);
10680 }
10681 
10682 
10683 /*
10684  * Restart entry point to restart the address set operation after the
10685  * refcounts have dropped to zero.
10686  */
10687 /* ARGSUSED */
10688 int
10689 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10690     ip_ioctl_cmd_t *ipip, void *ifreq)
10691 {
10692 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10693 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10694 	ASSERT(IAM_WRITER_IPIF(ipif));
10695 	ipif_down_tail(ipif);
10696 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10697 }
10698 
10699 /* ARGSUSED */
10700 int
10701 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10702     ip_ioctl_cmd_t *ipip, void *if_req)
10703 {
10704 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10705 	struct lifreq *lifr = (struct lifreq *)if_req;
10706 
10707 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10708 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10709 	/*
10710 	 * The net mask and address can't change since we have a
10711 	 * reference to the ipif. So no lock is necessary.
10712 	 */
10713 	if (ipif->ipif_isv6) {
10714 		*sin6 = sin6_null;
10715 		sin6->sin6_family = AF_INET6;
10716 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10717 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10718 		lifr->lifr_addrlen =
10719 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10720 	} else {
10721 		*sin = sin_null;
10722 		sin->sin_family = AF_INET;
10723 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10724 		if (ipip->ipi_cmd_type == LIF_CMD) {
10725 			lifr->lifr_addrlen =
10726 			    ip_mask_to_plen(ipif->ipif_net_mask);
10727 		}
10728 	}
10729 	return (0);
10730 }
10731 
10732 /*
10733  * Set the destination address for a pt-pt interface.
10734  */
10735 /* ARGSUSED */
10736 int
10737 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10738     ip_ioctl_cmd_t *ipip, void *if_req)
10739 {
10740 	int err = 0;
10741 	in6_addr_t v6addr;
10742 	boolean_t need_up = B_FALSE;
10743 
10744 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10745 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10746 	ASSERT(IAM_WRITER_IPIF(ipif));
10747 
10748 	if (ipif->ipif_isv6) {
10749 		sin6_t *sin6;
10750 
10751 		if (sin->sin_family != AF_INET6)
10752 			return (EAFNOSUPPORT);
10753 
10754 		sin6 = (sin6_t *)sin;
10755 		v6addr = sin6->sin6_addr;
10756 
10757 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10758 			return (EADDRNOTAVAIL);
10759 	} else {
10760 		ipaddr_t addr;
10761 
10762 		if (sin->sin_family != AF_INET)
10763 			return (EAFNOSUPPORT);
10764 
10765 		addr = sin->sin_addr.s_addr;
10766 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10767 			return (EADDRNOTAVAIL);
10768 
10769 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10770 	}
10771 
10772 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10773 		return (0);	/* No change */
10774 
10775 	if (ipif->ipif_flags & IPIF_UP) {
10776 		/*
10777 		 * If the interface is already marked up,
10778 		 * we call ipif_down which will take care
10779 		 * of ditching any IREs that have been set
10780 		 * up based on the old pp dst address.
10781 		 */
10782 		err = ipif_logical_down(ipif, q, mp);
10783 		if (err == EINPROGRESS)
10784 			return (err);
10785 		ipif_down_tail(ipif);
10786 		need_up = B_TRUE;
10787 	}
10788 	/*
10789 	 * could return EINPROGRESS. If so ioctl will complete in
10790 	 * ip_rput_dlpi_writer
10791 	 */
10792 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10793 	return (err);
10794 }
10795 
10796 static int
10797 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10798     boolean_t need_up)
10799 {
10800 	in6_addr_t v6addr;
10801 	ill_t	*ill = ipif->ipif_ill;
10802 	int	err = 0;
10803 
10804 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10805 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10806 	if (ipif->ipif_isv6) {
10807 		sin6_t *sin6;
10808 
10809 		sin6 = (sin6_t *)sin;
10810 		v6addr = sin6->sin6_addr;
10811 	} else {
10812 		ipaddr_t addr;
10813 
10814 		addr = sin->sin_addr.s_addr;
10815 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10816 	}
10817 	mutex_enter(&ill->ill_lock);
10818 	/* Set point to point destination address. */
10819 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10820 		/*
10821 		 * Allow this as a means of creating logical
10822 		 * pt-pt interfaces on top of e.g. an Ethernet.
10823 		 * XXX Undocumented HACK for testing.
10824 		 * pt-pt interfaces are created with NUD disabled.
10825 		 */
10826 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10827 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10828 		if (ipif->ipif_isv6)
10829 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10830 	}
10831 
10832 	/* Set the new address. */
10833 	ipif->ipif_v6pp_dst_addr = v6addr;
10834 	/* Make sure subnet tracks pp_dst */
10835 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10836 	mutex_exit(&ill->ill_lock);
10837 
10838 	if (need_up) {
10839 		/*
10840 		 * Now bring the interface back up.  If this
10841 		 * is the only IPIF for the ILL, ipif_up
10842 		 * will have to re-bind to the device, so
10843 		 * we may get back EINPROGRESS, in which
10844 		 * case, this IOCTL will get completed in
10845 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10846 		 */
10847 		err = ipif_up(ipif, q, mp);
10848 	}
10849 	return (err);
10850 }
10851 
10852 /*
10853  * Restart entry point to restart the dstaddress set operation after the
10854  * refcounts have dropped to zero.
10855  */
10856 /* ARGSUSED */
10857 int
10858 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10859     ip_ioctl_cmd_t *ipip, void *ifreq)
10860 {
10861 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10862 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10863 	ipif_down_tail(ipif);
10864 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10865 }
10866 
10867 /* ARGSUSED */
10868 int
10869 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10870     ip_ioctl_cmd_t *ipip, void *if_req)
10871 {
10872 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10873 
10874 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10875 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10876 	/*
10877 	 * Get point to point destination address. The addresses can't
10878 	 * change since we hold a reference to the ipif.
10879 	 */
10880 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10881 		return (EADDRNOTAVAIL);
10882 
10883 	if (ipif->ipif_isv6) {
10884 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10885 		*sin6 = sin6_null;
10886 		sin6->sin6_family = AF_INET6;
10887 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10888 	} else {
10889 		*sin = sin_null;
10890 		sin->sin_family = AF_INET;
10891 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10892 	}
10893 	return (0);
10894 }
10895 
10896 /*
10897  * part of ipmp, make this func return the active/inactive state and
10898  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10899  */
10900 /*
10901  * This function either sets or clears the IFF_INACTIVE flag.
10902  *
10903  * As long as there are some addresses or multicast memberships on the
10904  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10905  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10906  * will be used for outbound packets.
10907  *
10908  * Caller needs to verify the validity of setting IFF_INACTIVE.
10909  */
10910 static void
10911 phyint_inactive(phyint_t *phyi)
10912 {
10913 	ill_t *ill_v4;
10914 	ill_t *ill_v6;
10915 	ipif_t *ipif;
10916 	ilm_t *ilm;
10917 
10918 	ill_v4 = phyi->phyint_illv4;
10919 	ill_v6 = phyi->phyint_illv6;
10920 
10921 	/*
10922 	 * No need for a lock while traversing the list since iam
10923 	 * a writer
10924 	 */
10925 	if (ill_v4 != NULL) {
10926 		ASSERT(IAM_WRITER_ILL(ill_v4));
10927 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10928 		    ipif = ipif->ipif_next) {
10929 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10930 				mutex_enter(&phyi->phyint_lock);
10931 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10932 				mutex_exit(&phyi->phyint_lock);
10933 				return;
10934 			}
10935 		}
10936 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10937 		    ilm = ilm->ilm_next) {
10938 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10939 				mutex_enter(&phyi->phyint_lock);
10940 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10941 				mutex_exit(&phyi->phyint_lock);
10942 				return;
10943 			}
10944 		}
10945 	}
10946 	if (ill_v6 != NULL) {
10947 		ill_v6 = phyi->phyint_illv6;
10948 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
10949 		    ipif = ipif->ipif_next) {
10950 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10951 				mutex_enter(&phyi->phyint_lock);
10952 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10953 				mutex_exit(&phyi->phyint_lock);
10954 				return;
10955 			}
10956 		}
10957 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
10958 		    ilm = ilm->ilm_next) {
10959 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10960 				mutex_enter(&phyi->phyint_lock);
10961 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10962 				mutex_exit(&phyi->phyint_lock);
10963 				return;
10964 			}
10965 		}
10966 	}
10967 	mutex_enter(&phyi->phyint_lock);
10968 	phyi->phyint_flags |= PHYI_INACTIVE;
10969 	mutex_exit(&phyi->phyint_lock);
10970 }
10971 
10972 /*
10973  * This function is called only when the phyint flags change. Currently
10974  * called from ip_sioctl_flags. We re-do the broadcast nomination so
10975  * that we can select a good ill.
10976  */
10977 static void
10978 ip_redo_nomination(phyint_t *phyi)
10979 {
10980 	ill_t *ill_v4;
10981 
10982 	ill_v4 = phyi->phyint_illv4;
10983 
10984 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
10985 		ASSERT(IAM_WRITER_ILL(ill_v4));
10986 		if (ill_v4->ill_group->illgrp_ill_count > 1)
10987 			ill_nominate_bcast_rcv(ill_v4->ill_group);
10988 	}
10989 }
10990 
10991 /*
10992  * Heuristic to check if ill is INACTIVE.
10993  * Checks if ill has an ipif with an usable ip address.
10994  *
10995  * Return values:
10996  *	B_TRUE	- ill is INACTIVE; has no usable ipif
10997  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
10998  */
10999 static boolean_t
11000 ill_is_inactive(ill_t *ill)
11001 {
11002 	ipif_t *ipif;
11003 
11004 	/* Check whether it is in an IPMP group */
11005 	if (ill->ill_phyint->phyint_groupname == NULL)
11006 		return (B_FALSE);
11007 
11008 	if (ill->ill_ipif_up_count == 0)
11009 		return (B_TRUE);
11010 
11011 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11012 		uint64_t flags = ipif->ipif_flags;
11013 
11014 		/*
11015 		 * This ipif is usable if it is IPIF_UP and not a
11016 		 * dedicated test address.  A dedicated test address
11017 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11018 		 * (note in particular that V6 test addresses are
11019 		 * link-local data addresses and thus are marked
11020 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11021 		 */
11022 		if ((flags & IPIF_UP) &&
11023 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11024 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11025 			return (B_FALSE);
11026 	}
11027 	return (B_TRUE);
11028 }
11029 
11030 /*
11031  * Set interface flags.
11032  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11033  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11034  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11035  *
11036  * NOTE : We really don't enforce that ipif_id zero should be used
11037  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11038  *	  is because applications generally does SICGLIFFLAGS and
11039  *	  ORs in the new flags (that affects the logical) and does a
11040  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11041  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11042  *	  flags that will be turned on is correct with respect to
11043  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11044  */
11045 /* ARGSUSED */
11046 int
11047 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11048     ip_ioctl_cmd_t *ipip, void *if_req)
11049 {
11050 	uint64_t turn_on;
11051 	uint64_t turn_off;
11052 	int	err;
11053 	boolean_t need_up = B_FALSE;
11054 	phyint_t *phyi;
11055 	ill_t *ill;
11056 	uint64_t intf_flags;
11057 	boolean_t phyint_flags_modified = B_FALSE;
11058 	uint64_t flags;
11059 	struct ifreq *ifr;
11060 	struct lifreq *lifr;
11061 	boolean_t set_linklocal = B_FALSE;
11062 	boolean_t zero_source = B_FALSE;
11063 
11064 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11065 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11066 
11067 	ASSERT(IAM_WRITER_IPIF(ipif));
11068 
11069 	ill = ipif->ipif_ill;
11070 	phyi = ill->ill_phyint;
11071 
11072 	if (ipip->ipi_cmd_type == IF_CMD) {
11073 		ifr = (struct ifreq *)if_req;
11074 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11075 	} else {
11076 		lifr = (struct lifreq *)if_req;
11077 		flags = lifr->lifr_flags;
11078 	}
11079 
11080 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11081 
11082 	/*
11083 	 * Has the flags been set correctly till now ?
11084 	 */
11085 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11086 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11087 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11088 	/*
11089 	 * Compare the new flags to the old, and partition
11090 	 * into those coming on and those going off.
11091 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11092 	 */
11093 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11094 		flags |= intf_flags & ~0xFFFF;
11095 
11096 	/*
11097 	 * First check which bits will change and then which will
11098 	 * go on and off
11099 	 */
11100 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11101 	if (!turn_on)
11102 		return (0);	/* No change */
11103 
11104 	turn_off = intf_flags & turn_on;
11105 	turn_on ^= turn_off;
11106 	err = 0;
11107 
11108 	/*
11109 	 * Don't allow any bits belonging to the logical interface
11110 	 * to be set or cleared on the replacement ipif that was
11111 	 * created temporarily during a MOVE.
11112 	 */
11113 	if (ipif->ipif_replace_zero &&
11114 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11115 		return (EINVAL);
11116 	}
11117 
11118 	/*
11119 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11120 	 * IPv6 interfaces.
11121 	 */
11122 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11123 		return (EINVAL);
11124 
11125 	/*
11126 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11127 	 * interfaces.  It makes no sense in that context.
11128 	 */
11129 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11130 		return (EINVAL);
11131 
11132 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11133 		zero_source = B_TRUE;
11134 
11135 	/*
11136 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11137 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11138 	 * If the link local address isn't set, and can be set, it will get
11139 	 * set later on in this function.
11140 	 */
11141 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11142 	    (flags & IFF_UP) && !zero_source &&
11143 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11144 		if (ipif_cant_setlinklocal(ipif))
11145 			return (EINVAL);
11146 		set_linklocal = B_TRUE;
11147 	}
11148 
11149 	/*
11150 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11151 	 * same time. No need to grab ill_g_usesrc_lock here, see
11152 	 * synchronization notes in ip.c
11153 	 */
11154 	if (turn_on & PHYI_STANDBY &&
11155 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11156 		return (EINVAL);
11157 	}
11158 
11159 	/*
11160 	 * If we modify physical interface flags, we'll potentially need to
11161 	 * send up two routing socket messages for the changes (one for the
11162 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11163 	 */
11164 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11165 		phyint_flags_modified = B_TRUE;
11166 
11167 	/*
11168 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11169 	 * we need to flush the IRE_CACHES belonging to this ill.
11170 	 * We handle this case here without doing the DOWN/UP dance
11171 	 * like it is done for other flags. If some other flags are
11172 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11173 	 * below will handle it by bringing it down and then
11174 	 * bringing it UP.
11175 	 */
11176 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11177 		ill_t *ill_v4, *ill_v6;
11178 
11179 		ill_v4 = phyi->phyint_illv4;
11180 		ill_v6 = phyi->phyint_illv6;
11181 
11182 		/*
11183 		 * First set the INACTIVE flag if needed. Then delete the ires.
11184 		 * ire_add will atomically prevent creating new IRE_CACHEs
11185 		 * unless hidden flag is set.
11186 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11187 		 */
11188 		if ((turn_on & PHYI_FAILED) &&
11189 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11190 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11191 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11192 		}
11193 		if ((turn_off & PHYI_FAILED) &&
11194 		    ((intf_flags & PHYI_STANDBY) ||
11195 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11196 			phyint_inactive(phyi);
11197 		}
11198 
11199 		if (turn_on & PHYI_STANDBY) {
11200 			/*
11201 			 * We implicitly set INACTIVE only when STANDBY is set.
11202 			 * INACTIVE is also set on non-STANDBY phyint when user
11203 			 * disables FAILBACK using configuration file.
11204 			 * Do not allow STANDBY to be set on such INACTIVE
11205 			 * phyint
11206 			 */
11207 			if (phyi->phyint_flags & PHYI_INACTIVE)
11208 				return (EINVAL);
11209 			if (!(phyi->phyint_flags & PHYI_FAILED))
11210 				phyint_inactive(phyi);
11211 		}
11212 		if (turn_off & PHYI_STANDBY) {
11213 			if (ipmp_enable_failback) {
11214 				/*
11215 				 * Reset PHYI_INACTIVE.
11216 				 */
11217 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11218 			} else if (ill_is_inactive(ill) &&
11219 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11220 				/*
11221 				 * Need to set INACTIVE, when user sets
11222 				 * STANDBY on a non-STANDBY phyint and
11223 				 * later resets STANDBY
11224 				 */
11225 				phyint_inactive(phyi);
11226 			}
11227 		}
11228 		/*
11229 		 * We should always send up a message so that the
11230 		 * daemons come to know of it. Note that the zeroth
11231 		 * interface can be down and the check below for IPIF_UP
11232 		 * will not make sense as we are actually setting
11233 		 * a phyint flag here. We assume that the ipif used
11234 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11235 		 * send up any message for non-zero ipifs).
11236 		 */
11237 		phyint_flags_modified = B_TRUE;
11238 
11239 		if (ill_v4 != NULL) {
11240 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11241 			    IRE_CACHE, ill_stq_cache_delete,
11242 			    (char *)ill_v4, ill_v4);
11243 			illgrp_reset_schednext(ill_v4);
11244 		}
11245 		if (ill_v6 != NULL) {
11246 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11247 			    IRE_CACHE, ill_stq_cache_delete,
11248 			    (char *)ill_v6, ill_v6);
11249 			illgrp_reset_schednext(ill_v6);
11250 		}
11251 	}
11252 
11253 	/*
11254 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11255 	 * status of the interface and, if the interface is part of an IPMP
11256 	 * group, all other interfaces that are part of the same IPMP
11257 	 * group.
11258 	 */
11259 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11260 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11261 		    (caddr_t)ill);
11262 	}
11263 
11264 	/*
11265 	 * If the interface is not UP and we are not going to
11266 	 * bring it UP, record the flags and return. When the
11267 	 * interface comes UP later, the right actions will be
11268 	 * taken.
11269 	 */
11270 	if (!(ipif->ipif_flags & IPIF_UP) &&
11271 	    !(turn_on & IPIF_UP)) {
11272 		/* Record new flags in their respective places. */
11273 		mutex_enter(&ill->ill_lock);
11274 		mutex_enter(&ill->ill_phyint->phyint_lock);
11275 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11276 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11277 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11278 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11279 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11280 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11281 		mutex_exit(&ill->ill_lock);
11282 		mutex_exit(&ill->ill_phyint->phyint_lock);
11283 
11284 		/*
11285 		 * We do the broadcast and nomination here rather
11286 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11287 		 * the case of FAILBACK from INACTIVE standby to the
11288 		 * interface that has been repaired, PHYI_FAILED has not
11289 		 * been cleared yet. If there are only two interfaces in
11290 		 * that group, all we have is a FAILED and INACTIVE
11291 		 * interface. If we do the nomination soon after a failback,
11292 		 * the broadcast nomination code would select the
11293 		 * INACTIVE interface for receiving broadcasts as FAILED is
11294 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11295 		 * receive broadcast packets, we need to redo nomination
11296 		 * when the FAILED is cleared here. Thus, in general we
11297 		 * always do the nomination here for FAILED, STANDBY
11298 		 * and OFFLINE.
11299 		 */
11300 		if (((turn_on | turn_off) &
11301 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11302 			ip_redo_nomination(phyi);
11303 		}
11304 		if (phyint_flags_modified) {
11305 			if (phyi->phyint_illv4 != NULL) {
11306 				ip_rts_ifmsg(phyi->phyint_illv4->
11307 				    ill_ipif);
11308 			}
11309 			if (phyi->phyint_illv6 != NULL) {
11310 				ip_rts_ifmsg(phyi->phyint_illv6->
11311 				    ill_ipif);
11312 			}
11313 		}
11314 		return (0);
11315 	} else if (set_linklocal || zero_source) {
11316 		mutex_enter(&ill->ill_lock);
11317 		if (set_linklocal)
11318 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11319 		if (zero_source)
11320 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11321 		mutex_exit(&ill->ill_lock);
11322 	}
11323 
11324 	/*
11325 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11326 	 * or point-to-point interfaces with an unspecified destination. We do
11327 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11328 	 * have a subnet assigned, which is how in.ndpd currently manages its
11329 	 * onlink prefix list when no addresses are configured with those
11330 	 * prefixes.
11331 	 */
11332 	if (ipif->ipif_isv6 &&
11333 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11334 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11335 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11336 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11337 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11338 		return (EINVAL);
11339 	}
11340 
11341 	/*
11342 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11343 	 * from being brought up.
11344 	 */
11345 	if (!ipif->ipif_isv6 &&
11346 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11347 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11348 		return (EINVAL);
11349 	}
11350 
11351 	/*
11352 	 * The only flag changes that we currently take specific action on
11353 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11354 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11355 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11356 	 * the flags and bringing it back up again.
11357 	 */
11358 	if ((turn_on|turn_off) &
11359 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11360 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11361 		/*
11362 		 * Taking this ipif down, make sure we have
11363 		 * valid net and subnet bcast ire's for other
11364 		 * logical interfaces, if we need them.
11365 		 */
11366 		if (!ipif->ipif_isv6)
11367 			ipif_check_bcast_ires(ipif);
11368 
11369 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11370 		    !(turn_off & IPIF_UP)) {
11371 			need_up = B_TRUE;
11372 			if (ipif->ipif_flags & IPIF_UP)
11373 				ill->ill_logical_down = 1;
11374 			turn_on &= ~IPIF_UP;
11375 		}
11376 		err = ipif_down(ipif, q, mp);
11377 		ip1dbg(("ipif_down returns %d err ", err));
11378 		if (err == EINPROGRESS)
11379 			return (err);
11380 		ipif_down_tail(ipif);
11381 	}
11382 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11383 }
11384 
11385 static int
11386 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11387     boolean_t need_up)
11388 {
11389 	ill_t	*ill;
11390 	phyint_t *phyi;
11391 	uint64_t turn_on;
11392 	uint64_t turn_off;
11393 	uint64_t intf_flags;
11394 	boolean_t phyint_flags_modified = B_FALSE;
11395 	int	err = 0;
11396 	boolean_t set_linklocal = B_FALSE;
11397 	boolean_t zero_source = B_FALSE;
11398 
11399 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11400 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11401 
11402 	ASSERT(IAM_WRITER_IPIF(ipif));
11403 
11404 	ill = ipif->ipif_ill;
11405 	phyi = ill->ill_phyint;
11406 
11407 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11408 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11409 
11410 	turn_off = intf_flags & turn_on;
11411 	turn_on ^= turn_off;
11412 
11413 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11414 		phyint_flags_modified = B_TRUE;
11415 
11416 	/*
11417 	 * Now we change the flags. Track current value of
11418 	 * other flags in their respective places.
11419 	 */
11420 	mutex_enter(&ill->ill_lock);
11421 	mutex_enter(&phyi->phyint_lock);
11422 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11423 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11424 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11425 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11426 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11427 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11428 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11429 		set_linklocal = B_TRUE;
11430 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11431 	}
11432 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11433 		zero_source = B_TRUE;
11434 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11435 	}
11436 	mutex_exit(&ill->ill_lock);
11437 	mutex_exit(&phyi->phyint_lock);
11438 
11439 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11440 		ip_redo_nomination(phyi);
11441 
11442 	if (set_linklocal)
11443 		(void) ipif_setlinklocal(ipif);
11444 
11445 	if (zero_source)
11446 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11447 	else
11448 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11449 
11450 	if (need_up) {
11451 		/*
11452 		 * XXX ipif_up really does not know whether a phyint flags
11453 		 * was modified or not. So, it sends up information on
11454 		 * only one routing sockets message. As we don't bring up
11455 		 * the interface and also set STANDBY/FAILED simultaneously
11456 		 * it should be okay.
11457 		 */
11458 		err = ipif_up(ipif, q, mp);
11459 	} else {
11460 		/*
11461 		 * Make sure routing socket sees all changes to the flags.
11462 		 * ipif_up_done* handles this when we use ipif_up.
11463 		 */
11464 		if (phyint_flags_modified) {
11465 			if (phyi->phyint_illv4 != NULL) {
11466 				ip_rts_ifmsg(phyi->phyint_illv4->
11467 				    ill_ipif);
11468 			}
11469 			if (phyi->phyint_illv6 != NULL) {
11470 				ip_rts_ifmsg(phyi->phyint_illv6->
11471 				    ill_ipif);
11472 			}
11473 		} else {
11474 			ip_rts_ifmsg(ipif);
11475 		}
11476 	}
11477 	return (err);
11478 }
11479 
11480 /*
11481  * Restart entry point to restart the flags restart operation after the
11482  * refcounts have dropped to zero.
11483  */
11484 /* ARGSUSED */
11485 int
11486 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11487     ip_ioctl_cmd_t *ipip, void *if_req)
11488 {
11489 	int	err;
11490 	struct ifreq *ifr = (struct ifreq *)if_req;
11491 	struct lifreq *lifr = (struct lifreq *)if_req;
11492 
11493 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11494 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11495 
11496 	ipif_down_tail(ipif);
11497 	if (ipip->ipi_cmd_type == IF_CMD) {
11498 		/*
11499 		 * Since ip_sioctl_flags expects an int and ifr_flags
11500 		 * is a short we need to cast ifr_flags into an int
11501 		 * to avoid having sign extension cause bits to get
11502 		 * set that should not be.
11503 		 */
11504 		err = ip_sioctl_flags_tail(ipif,
11505 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11506 		    q, mp, B_TRUE);
11507 	} else {
11508 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11509 		    q, mp, B_TRUE);
11510 	}
11511 	return (err);
11512 }
11513 
11514 /* ARGSUSED */
11515 int
11516 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11517     ip_ioctl_cmd_t *ipip, void *if_req)
11518 {
11519 	/*
11520 	 * Has the flags been set correctly till now ?
11521 	 */
11522 	ill_t *ill = ipif->ipif_ill;
11523 	phyint_t *phyi = ill->ill_phyint;
11524 
11525 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11526 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11527 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11528 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11529 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11530 
11531 	/*
11532 	 * Need a lock since some flags can be set even when there are
11533 	 * references to the ipif.
11534 	 */
11535 	mutex_enter(&ill->ill_lock);
11536 	if (ipip->ipi_cmd_type == IF_CMD) {
11537 		struct ifreq *ifr = (struct ifreq *)if_req;
11538 
11539 		/* Get interface flags (low 16 only). */
11540 		ifr->ifr_flags = ((ipif->ipif_flags |
11541 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11542 	} else {
11543 		struct lifreq *lifr = (struct lifreq *)if_req;
11544 
11545 		/* Get interface flags. */
11546 		lifr->lifr_flags = ipif->ipif_flags |
11547 		    ill->ill_flags | phyi->phyint_flags;
11548 	}
11549 	mutex_exit(&ill->ill_lock);
11550 	return (0);
11551 }
11552 
11553 /* ARGSUSED */
11554 int
11555 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11556     ip_ioctl_cmd_t *ipip, void *if_req)
11557 {
11558 	int mtu;
11559 	int ip_min_mtu;
11560 	struct ifreq	*ifr;
11561 	struct lifreq *lifr;
11562 	ire_t	*ire;
11563 
11564 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11565 	    ipif->ipif_id, (void *)ipif));
11566 	if (ipip->ipi_cmd_type == IF_CMD) {
11567 		ifr = (struct ifreq *)if_req;
11568 		mtu = ifr->ifr_metric;
11569 	} else {
11570 		lifr = (struct lifreq *)if_req;
11571 		mtu = lifr->lifr_mtu;
11572 	}
11573 
11574 	if (ipif->ipif_isv6)
11575 		ip_min_mtu = IPV6_MIN_MTU;
11576 	else
11577 		ip_min_mtu = IP_MIN_MTU;
11578 
11579 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11580 		return (EINVAL);
11581 
11582 	/*
11583 	 * Change the MTU size in all relevant ire's.
11584 	 * Mtu change Vs. new ire creation - protocol below.
11585 	 * First change ipif_mtu and the ire_max_frag of the
11586 	 * interface ire. Then do an ire walk and change the
11587 	 * ire_max_frag of all affected ires. During ire_add
11588 	 * under the bucket lock, set the ire_max_frag of the
11589 	 * new ire being created from the ipif/ire from which
11590 	 * it is being derived. If an mtu change happens after
11591 	 * the ire is added, the new ire will be cleaned up.
11592 	 * Conversely if the mtu change happens before the ire
11593 	 * is added, ire_add will see the new value of the mtu.
11594 	 */
11595 	ipif->ipif_mtu = mtu;
11596 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11597 
11598 	if (ipif->ipif_isv6)
11599 		ire = ipif_to_ire_v6(ipif);
11600 	else
11601 		ire = ipif_to_ire(ipif);
11602 	if (ire != NULL) {
11603 		ire->ire_max_frag = ipif->ipif_mtu;
11604 		ire_refrele(ire);
11605 	}
11606 	if (ipif->ipif_flags & IPIF_UP) {
11607 		if (ipif->ipif_isv6)
11608 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11609 		else
11610 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11611 	}
11612 	/* Update the MTU in SCTP's list */
11613 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11614 	return (0);
11615 }
11616 
11617 /* Get interface MTU. */
11618 /* ARGSUSED */
11619 int
11620 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11621 	ip_ioctl_cmd_t *ipip, void *if_req)
11622 {
11623 	struct ifreq	*ifr;
11624 	struct lifreq	*lifr;
11625 
11626 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11627 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11628 	if (ipip->ipi_cmd_type == IF_CMD) {
11629 		ifr = (struct ifreq *)if_req;
11630 		ifr->ifr_metric = ipif->ipif_mtu;
11631 	} else {
11632 		lifr = (struct lifreq *)if_req;
11633 		lifr->lifr_mtu = ipif->ipif_mtu;
11634 	}
11635 	return (0);
11636 }
11637 
11638 /* Set interface broadcast address. */
11639 /* ARGSUSED2 */
11640 int
11641 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11642 	ip_ioctl_cmd_t *ipip, void *if_req)
11643 {
11644 	ipaddr_t addr;
11645 	ire_t	*ire;
11646 
11647 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11648 	    ipif->ipif_id));
11649 
11650 	ASSERT(IAM_WRITER_IPIF(ipif));
11651 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11652 		return (EADDRNOTAVAIL);
11653 
11654 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11655 
11656 	if (sin->sin_family != AF_INET)
11657 		return (EAFNOSUPPORT);
11658 
11659 	addr = sin->sin_addr.s_addr;
11660 	if (ipif->ipif_flags & IPIF_UP) {
11661 		/*
11662 		 * If we are already up, make sure the new
11663 		 * broadcast address makes sense.  If it does,
11664 		 * there should be an IRE for it already.
11665 		 * Don't match on ipif, only on the ill
11666 		 * since we are sharing these now. Don't use
11667 		 * MATCH_IRE_ILL_GROUP as we are looking for
11668 		 * the broadcast ire on this ill and each ill
11669 		 * in the group has its own broadcast ire.
11670 		 */
11671 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11672 		    ipif, ALL_ZONES, NULL,
11673 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11674 		if (ire == NULL) {
11675 			return (EINVAL);
11676 		} else {
11677 			ire_refrele(ire);
11678 		}
11679 	}
11680 	/*
11681 	 * Changing the broadcast addr for this ipif.
11682 	 * Make sure we have valid net and subnet bcast
11683 	 * ire's for other logical interfaces, if needed.
11684 	 */
11685 	if (addr != ipif->ipif_brd_addr)
11686 		ipif_check_bcast_ires(ipif);
11687 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11688 	return (0);
11689 }
11690 
11691 /* Get interface broadcast address. */
11692 /* ARGSUSED */
11693 int
11694 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11695     ip_ioctl_cmd_t *ipip, void *if_req)
11696 {
11697 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11698 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11699 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11700 		return (EADDRNOTAVAIL);
11701 
11702 	/* IPIF_BROADCAST not possible with IPv6 */
11703 	ASSERT(!ipif->ipif_isv6);
11704 	*sin = sin_null;
11705 	sin->sin_family = AF_INET;
11706 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11707 	return (0);
11708 }
11709 
11710 /*
11711  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11712  */
11713 /* ARGSUSED */
11714 int
11715 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11716     ip_ioctl_cmd_t *ipip, void *if_req)
11717 {
11718 	int err = 0;
11719 	in6_addr_t v6mask;
11720 
11721 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11722 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11723 
11724 	ASSERT(IAM_WRITER_IPIF(ipif));
11725 
11726 	if (ipif->ipif_isv6) {
11727 		sin6_t *sin6;
11728 
11729 		if (sin->sin_family != AF_INET6)
11730 			return (EAFNOSUPPORT);
11731 
11732 		sin6 = (sin6_t *)sin;
11733 		v6mask = sin6->sin6_addr;
11734 	} else {
11735 		ipaddr_t mask;
11736 
11737 		if (sin->sin_family != AF_INET)
11738 			return (EAFNOSUPPORT);
11739 
11740 		mask = sin->sin_addr.s_addr;
11741 		V4MASK_TO_V6(mask, v6mask);
11742 	}
11743 
11744 	/*
11745 	 * No big deal if the interface isn't already up, or the mask
11746 	 * isn't really changing, or this is pt-pt.
11747 	 */
11748 	if (!(ipif->ipif_flags & IPIF_UP) ||
11749 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11750 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11751 		ipif->ipif_v6net_mask = v6mask;
11752 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11753 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11754 			    ipif->ipif_v6net_mask,
11755 			    ipif->ipif_v6subnet);
11756 		}
11757 		return (0);
11758 	}
11759 	/*
11760 	 * Make sure we have valid net and subnet broadcast ire's
11761 	 * for the old netmask, if needed by other logical interfaces.
11762 	 */
11763 	if (!ipif->ipif_isv6)
11764 		ipif_check_bcast_ires(ipif);
11765 
11766 	err = ipif_logical_down(ipif, q, mp);
11767 	if (err == EINPROGRESS)
11768 		return (err);
11769 	ipif_down_tail(ipif);
11770 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11771 	return (err);
11772 }
11773 
11774 static int
11775 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11776 {
11777 	in6_addr_t v6mask;
11778 	int err = 0;
11779 
11780 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11781 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11782 
11783 	if (ipif->ipif_isv6) {
11784 		sin6_t *sin6;
11785 
11786 		sin6 = (sin6_t *)sin;
11787 		v6mask = sin6->sin6_addr;
11788 	} else {
11789 		ipaddr_t mask;
11790 
11791 		mask = sin->sin_addr.s_addr;
11792 		V4MASK_TO_V6(mask, v6mask);
11793 	}
11794 
11795 	ipif->ipif_v6net_mask = v6mask;
11796 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11797 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11798 		    ipif->ipif_v6subnet);
11799 	}
11800 	err = ipif_up(ipif, q, mp);
11801 
11802 	if (err == 0 || err == EINPROGRESS) {
11803 		/*
11804 		 * The interface must be DL_BOUND if this packet has to
11805 		 * go out on the wire. Since we only go through a logical
11806 		 * down and are bound with the driver during an internal
11807 		 * down/up that is satisfied.
11808 		 */
11809 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11810 			/* Potentially broadcast an address mask reply. */
11811 			ipif_mask_reply(ipif);
11812 		}
11813 	}
11814 	return (err);
11815 }
11816 
11817 /* ARGSUSED */
11818 int
11819 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11820     ip_ioctl_cmd_t *ipip, void *if_req)
11821 {
11822 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11823 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11824 	ipif_down_tail(ipif);
11825 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11826 }
11827 
11828 /* Get interface net mask. */
11829 /* ARGSUSED */
11830 int
11831 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11832     ip_ioctl_cmd_t *ipip, void *if_req)
11833 {
11834 	struct lifreq *lifr = (struct lifreq *)if_req;
11835 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11836 
11837 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11838 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11839 
11840 	/*
11841 	 * net mask can't change since we have a reference to the ipif.
11842 	 */
11843 	if (ipif->ipif_isv6) {
11844 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11845 		*sin6 = sin6_null;
11846 		sin6->sin6_family = AF_INET6;
11847 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11848 		lifr->lifr_addrlen =
11849 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11850 	} else {
11851 		*sin = sin_null;
11852 		sin->sin_family = AF_INET;
11853 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11854 		if (ipip->ipi_cmd_type == LIF_CMD) {
11855 			lifr->lifr_addrlen =
11856 			    ip_mask_to_plen(ipif->ipif_net_mask);
11857 		}
11858 	}
11859 	return (0);
11860 }
11861 
11862 /* ARGSUSED */
11863 int
11864 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11865     ip_ioctl_cmd_t *ipip, void *if_req)
11866 {
11867 
11868 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11869 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11870 	/*
11871 	 * Set interface metric.  We don't use this for
11872 	 * anything but we keep track of it in case it is
11873 	 * important to routing applications or such.
11874 	 */
11875 	if (ipip->ipi_cmd_type == IF_CMD) {
11876 		struct ifreq    *ifr;
11877 
11878 		ifr = (struct ifreq *)if_req;
11879 		ipif->ipif_metric = ifr->ifr_metric;
11880 	} else {
11881 		struct lifreq   *lifr;
11882 
11883 		lifr = (struct lifreq *)if_req;
11884 		ipif->ipif_metric = lifr->lifr_metric;
11885 	}
11886 	return (0);
11887 }
11888 
11889 
11890 /* ARGSUSED */
11891 int
11892 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11893     ip_ioctl_cmd_t *ipip, void *if_req)
11894 {
11895 
11896 	/* Get interface metric. */
11897 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11898 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11899 	if (ipip->ipi_cmd_type == IF_CMD) {
11900 		struct ifreq    *ifr;
11901 
11902 		ifr = (struct ifreq *)if_req;
11903 		ifr->ifr_metric = ipif->ipif_metric;
11904 	} else {
11905 		struct lifreq   *lifr;
11906 
11907 		lifr = (struct lifreq *)if_req;
11908 		lifr->lifr_metric = ipif->ipif_metric;
11909 	}
11910 
11911 	return (0);
11912 }
11913 
11914 /* ARGSUSED */
11915 int
11916 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11917     ip_ioctl_cmd_t *ipip, void *if_req)
11918 {
11919 
11920 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11921 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11922 	/*
11923 	 * Set the muxid returned from I_PLINK.
11924 	 */
11925 	if (ipip->ipi_cmd_type == IF_CMD) {
11926 		struct ifreq *ifr = (struct ifreq *)if_req;
11927 
11928 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11929 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11930 	} else {
11931 		struct lifreq *lifr = (struct lifreq *)if_req;
11932 
11933 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11934 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11935 	}
11936 	return (0);
11937 }
11938 
11939 /* ARGSUSED */
11940 int
11941 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11942     ip_ioctl_cmd_t *ipip, void *if_req)
11943 {
11944 
11945 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11946 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11947 	/*
11948 	 * Get the muxid saved in ill for I_PUNLINK.
11949 	 */
11950 	if (ipip->ipi_cmd_type == IF_CMD) {
11951 		struct ifreq *ifr = (struct ifreq *)if_req;
11952 
11953 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11954 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11955 	} else {
11956 		struct lifreq *lifr = (struct lifreq *)if_req;
11957 
11958 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
11959 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
11960 	}
11961 	return (0);
11962 }
11963 
11964 /*
11965  * Set the subnet prefix. Does not modify the broadcast address.
11966  */
11967 /* ARGSUSED */
11968 int
11969 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11970     ip_ioctl_cmd_t *ipip, void *if_req)
11971 {
11972 	int err = 0;
11973 	in6_addr_t v6addr;
11974 	in6_addr_t v6mask;
11975 	boolean_t need_up = B_FALSE;
11976 	int addrlen;
11977 
11978 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11979 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11980 
11981 	ASSERT(IAM_WRITER_IPIF(ipif));
11982 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11983 
11984 	if (ipif->ipif_isv6) {
11985 		sin6_t *sin6;
11986 
11987 		if (sin->sin_family != AF_INET6)
11988 			return (EAFNOSUPPORT);
11989 
11990 		sin6 = (sin6_t *)sin;
11991 		v6addr = sin6->sin6_addr;
11992 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11993 			return (EADDRNOTAVAIL);
11994 	} else {
11995 		ipaddr_t addr;
11996 
11997 		if (sin->sin_family != AF_INET)
11998 			return (EAFNOSUPPORT);
11999 
12000 		addr = sin->sin_addr.s_addr;
12001 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12002 			return (EADDRNOTAVAIL);
12003 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12004 		/* Add 96 bits */
12005 		addrlen += IPV6_ABITS - IP_ABITS;
12006 	}
12007 
12008 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12009 		return (EINVAL);
12010 
12011 	/* Check if bits in the address is set past the mask */
12012 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12013 		return (EINVAL);
12014 
12015 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12016 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12017 		return (0);	/* No change */
12018 
12019 	if (ipif->ipif_flags & IPIF_UP) {
12020 		/*
12021 		 * If the interface is already marked up,
12022 		 * we call ipif_down which will take care
12023 		 * of ditching any IREs that have been set
12024 		 * up based on the old interface address.
12025 		 */
12026 		err = ipif_logical_down(ipif, q, mp);
12027 		if (err == EINPROGRESS)
12028 			return (err);
12029 		ipif_down_tail(ipif);
12030 		need_up = B_TRUE;
12031 	}
12032 
12033 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12034 	return (err);
12035 }
12036 
12037 static int
12038 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12039     queue_t *q, mblk_t *mp, boolean_t need_up)
12040 {
12041 	ill_t	*ill = ipif->ipif_ill;
12042 	int	err = 0;
12043 
12044 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12045 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12046 
12047 	/* Set the new address. */
12048 	mutex_enter(&ill->ill_lock);
12049 	ipif->ipif_v6net_mask = v6mask;
12050 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12051 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12052 		    ipif->ipif_v6subnet);
12053 	}
12054 	mutex_exit(&ill->ill_lock);
12055 
12056 	if (need_up) {
12057 		/*
12058 		 * Now bring the interface back up.  If this
12059 		 * is the only IPIF for the ILL, ipif_up
12060 		 * will have to re-bind to the device, so
12061 		 * we may get back EINPROGRESS, in which
12062 		 * case, this IOCTL will get completed in
12063 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12064 		 */
12065 		err = ipif_up(ipif, q, mp);
12066 		if (err == EINPROGRESS)
12067 			return (err);
12068 	}
12069 	return (err);
12070 }
12071 
12072 /* ARGSUSED */
12073 int
12074 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12075     ip_ioctl_cmd_t *ipip, void *if_req)
12076 {
12077 	int	addrlen;
12078 	in6_addr_t v6addr;
12079 	in6_addr_t v6mask;
12080 	struct lifreq *lifr = (struct lifreq *)if_req;
12081 
12082 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12083 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12084 	ipif_down_tail(ipif);
12085 
12086 	addrlen = lifr->lifr_addrlen;
12087 	if (ipif->ipif_isv6) {
12088 		sin6_t *sin6;
12089 
12090 		sin6 = (sin6_t *)sin;
12091 		v6addr = sin6->sin6_addr;
12092 	} else {
12093 		ipaddr_t addr;
12094 
12095 		addr = sin->sin_addr.s_addr;
12096 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12097 		addrlen += IPV6_ABITS - IP_ABITS;
12098 	}
12099 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12100 
12101 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12102 }
12103 
12104 /* ARGSUSED */
12105 int
12106 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12107     ip_ioctl_cmd_t *ipip, void *if_req)
12108 {
12109 	struct lifreq *lifr = (struct lifreq *)if_req;
12110 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12111 
12112 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12113 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12114 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12115 
12116 	if (ipif->ipif_isv6) {
12117 		*sin6 = sin6_null;
12118 		sin6->sin6_family = AF_INET6;
12119 		sin6->sin6_addr = ipif->ipif_v6subnet;
12120 		lifr->lifr_addrlen =
12121 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12122 	} else {
12123 		*sin = sin_null;
12124 		sin->sin_family = AF_INET;
12125 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12126 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12127 	}
12128 	return (0);
12129 }
12130 
12131 /*
12132  * Set the IPv6 address token.
12133  */
12134 /* ARGSUSED */
12135 int
12136 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12137     ip_ioctl_cmd_t *ipi, void *if_req)
12138 {
12139 	ill_t *ill = ipif->ipif_ill;
12140 	int err;
12141 	in6_addr_t v6addr;
12142 	in6_addr_t v6mask;
12143 	boolean_t need_up = B_FALSE;
12144 	int i;
12145 	sin6_t *sin6 = (sin6_t *)sin;
12146 	struct lifreq *lifr = (struct lifreq *)if_req;
12147 	int addrlen;
12148 
12149 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12150 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12151 	ASSERT(IAM_WRITER_IPIF(ipif));
12152 
12153 	addrlen = lifr->lifr_addrlen;
12154 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12155 	if (ipif->ipif_id != 0)
12156 		return (EINVAL);
12157 
12158 	if (!ipif->ipif_isv6)
12159 		return (EINVAL);
12160 
12161 	if (addrlen > IPV6_ABITS)
12162 		return (EINVAL);
12163 
12164 	v6addr = sin6->sin6_addr;
12165 
12166 	/*
12167 	 * The length of the token is the length from the end.  To get
12168 	 * the proper mask for this, compute the mask of the bits not
12169 	 * in the token; ie. the prefix, and then xor to get the mask.
12170 	 */
12171 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12172 		return (EINVAL);
12173 	for (i = 0; i < 4; i++) {
12174 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12175 	}
12176 
12177 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12178 	    ill->ill_token_length == addrlen)
12179 		return (0);	/* No change */
12180 
12181 	if (ipif->ipif_flags & IPIF_UP) {
12182 		err = ipif_logical_down(ipif, q, mp);
12183 		if (err == EINPROGRESS)
12184 			return (err);
12185 		ipif_down_tail(ipif);
12186 		need_up = B_TRUE;
12187 	}
12188 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12189 	return (err);
12190 }
12191 
12192 static int
12193 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12194     mblk_t *mp, boolean_t need_up)
12195 {
12196 	in6_addr_t v6addr;
12197 	in6_addr_t v6mask;
12198 	ill_t	*ill = ipif->ipif_ill;
12199 	int	i;
12200 	int	err = 0;
12201 
12202 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12203 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12204 	v6addr = sin6->sin6_addr;
12205 	/*
12206 	 * The length of the token is the length from the end.  To get
12207 	 * the proper mask for this, compute the mask of the bits not
12208 	 * in the token; ie. the prefix, and then xor to get the mask.
12209 	 */
12210 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12211 	for (i = 0; i < 4; i++)
12212 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12213 
12214 	mutex_enter(&ill->ill_lock);
12215 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12216 	ill->ill_token_length = addrlen;
12217 	mutex_exit(&ill->ill_lock);
12218 
12219 	if (need_up) {
12220 		/*
12221 		 * Now bring the interface back up.  If this
12222 		 * is the only IPIF for the ILL, ipif_up
12223 		 * will have to re-bind to the device, so
12224 		 * we may get back EINPROGRESS, in which
12225 		 * case, this IOCTL will get completed in
12226 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12227 		 */
12228 		err = ipif_up(ipif, q, mp);
12229 		if (err == EINPROGRESS)
12230 			return (err);
12231 	}
12232 	return (err);
12233 }
12234 
12235 /* ARGSUSED */
12236 int
12237 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12238     ip_ioctl_cmd_t *ipi, void *if_req)
12239 {
12240 	ill_t *ill;
12241 	sin6_t *sin6 = (sin6_t *)sin;
12242 	struct lifreq *lifr = (struct lifreq *)if_req;
12243 
12244 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12245 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12246 	if (ipif->ipif_id != 0)
12247 		return (EINVAL);
12248 
12249 	ill = ipif->ipif_ill;
12250 	if (!ill->ill_isv6)
12251 		return (ENXIO);
12252 
12253 	*sin6 = sin6_null;
12254 	sin6->sin6_family = AF_INET6;
12255 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12256 	sin6->sin6_addr = ill->ill_token;
12257 	lifr->lifr_addrlen = ill->ill_token_length;
12258 	return (0);
12259 }
12260 
12261 /*
12262  * Set (hardware) link specific information that might override
12263  * what was acquired through the DL_INFO_ACK.
12264  * The logic is as follows.
12265  *
12266  * become exclusive
12267  * set CHANGING flag
12268  * change mtu on affected IREs
12269  * clear CHANGING flag
12270  *
12271  * An ire add that occurs before the CHANGING flag is set will have its mtu
12272  * changed by the ip_sioctl_lnkinfo.
12273  *
12274  * During the time the CHANGING flag is set, no new ires will be added to the
12275  * bucket, and ire add will fail (due the CHANGING flag).
12276  *
12277  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12278  * before it is added to the bucket.
12279  *
12280  * Obviously only 1 thread can set the CHANGING flag and we need to become
12281  * exclusive to set the flag.
12282  */
12283 /* ARGSUSED */
12284 int
12285 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12286     ip_ioctl_cmd_t *ipi, void *if_req)
12287 {
12288 	ill_t		*ill = ipif->ipif_ill;
12289 	ipif_t		*nipif;
12290 	int		ip_min_mtu;
12291 	boolean_t	mtu_walk = B_FALSE;
12292 	struct lifreq	*lifr = (struct lifreq *)if_req;
12293 	lif_ifinfo_req_t *lir;
12294 	ire_t		*ire;
12295 
12296 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12297 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12298 	lir = &lifr->lifr_ifinfo;
12299 	ASSERT(IAM_WRITER_IPIF(ipif));
12300 
12301 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12302 	if (ipif->ipif_id != 0)
12303 		return (EINVAL);
12304 
12305 	/* Set interface MTU. */
12306 	if (ipif->ipif_isv6)
12307 		ip_min_mtu = IPV6_MIN_MTU;
12308 	else
12309 		ip_min_mtu = IP_MIN_MTU;
12310 
12311 	/*
12312 	 * Verify values before we set anything. Allow zero to
12313 	 * mean unspecified.
12314 	 */
12315 	if (lir->lir_maxmtu != 0 &&
12316 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12317 	    lir->lir_maxmtu < ip_min_mtu))
12318 		return (EINVAL);
12319 	if (lir->lir_reachtime != 0 &&
12320 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12321 		return (EINVAL);
12322 	if (lir->lir_reachretrans != 0 &&
12323 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12324 		return (EINVAL);
12325 
12326 	mutex_enter(&ill->ill_lock);
12327 	ill->ill_state_flags |= ILL_CHANGING;
12328 	for (nipif = ill->ill_ipif; nipif != NULL;
12329 	    nipif = nipif->ipif_next) {
12330 		nipif->ipif_state_flags |= IPIF_CHANGING;
12331 	}
12332 
12333 	mutex_exit(&ill->ill_lock);
12334 
12335 	if (lir->lir_maxmtu != 0) {
12336 		ill->ill_max_mtu = lir->lir_maxmtu;
12337 		ill->ill_mtu_userspecified = 1;
12338 		mtu_walk = B_TRUE;
12339 	}
12340 
12341 	if (lir->lir_reachtime != 0)
12342 		ill->ill_reachable_time = lir->lir_reachtime;
12343 
12344 	if (lir->lir_reachretrans != 0)
12345 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12346 
12347 	ill->ill_max_hops = lir->lir_maxhops;
12348 
12349 	ill->ill_max_buf = ND_MAX_Q;
12350 
12351 	if (mtu_walk) {
12352 		/*
12353 		 * Set the MTU on all ipifs associated with this ill except
12354 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12355 		 */
12356 		for (nipif = ill->ill_ipif; nipif != NULL;
12357 		    nipif = nipif->ipif_next) {
12358 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12359 				continue;
12360 
12361 			nipif->ipif_mtu = ill->ill_max_mtu;
12362 
12363 			if (!(nipif->ipif_flags & IPIF_UP))
12364 				continue;
12365 
12366 			if (nipif->ipif_isv6)
12367 				ire = ipif_to_ire_v6(nipif);
12368 			else
12369 				ire = ipif_to_ire(nipif);
12370 			if (ire != NULL) {
12371 				ire->ire_max_frag = ipif->ipif_mtu;
12372 				ire_refrele(ire);
12373 			}
12374 			if (ill->ill_isv6) {
12375 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12376 				    ipif_mtu_change, (char *)nipif,
12377 				    ill);
12378 			} else {
12379 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12380 				    ipif_mtu_change, (char *)nipif,
12381 				    ill);
12382 			}
12383 		}
12384 	}
12385 
12386 	mutex_enter(&ill->ill_lock);
12387 	for (nipif = ill->ill_ipif; nipif != NULL;
12388 	    nipif = nipif->ipif_next) {
12389 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12390 	}
12391 	ILL_UNMARK_CHANGING(ill);
12392 	mutex_exit(&ill->ill_lock);
12393 
12394 	return (0);
12395 }
12396 
12397 /* ARGSUSED */
12398 int
12399 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12400     ip_ioctl_cmd_t *ipi, void *if_req)
12401 {
12402 	struct lif_ifinfo_req *lir;
12403 	ill_t *ill = ipif->ipif_ill;
12404 
12405 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12406 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12407 	if (ipif->ipif_id != 0)
12408 		return (EINVAL);
12409 
12410 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12411 	lir->lir_maxhops = ill->ill_max_hops;
12412 	lir->lir_reachtime = ill->ill_reachable_time;
12413 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12414 	lir->lir_maxmtu = ill->ill_max_mtu;
12415 
12416 	return (0);
12417 }
12418 
12419 /*
12420  * Return best guess as to the subnet mask for the specified address.
12421  * Based on the subnet masks for all the configured interfaces.
12422  *
12423  * We end up returning a zero mask in the case of default, multicast or
12424  * experimental.
12425  */
12426 static ipaddr_t
12427 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12428 {
12429 	ipaddr_t net_mask;
12430 	ill_t	*ill;
12431 	ipif_t	*ipif;
12432 	ill_walk_context_t ctx;
12433 	ipif_t	*fallback_ipif = NULL;
12434 
12435 	net_mask = ip_net_mask(addr);
12436 	if (net_mask == 0) {
12437 		*ipifp = NULL;
12438 		return (0);
12439 	}
12440 
12441 	/* Let's check to see if this is maybe a local subnet route. */
12442 	/* this function only applies to IPv4 interfaces */
12443 	rw_enter(&ill_g_lock, RW_READER);
12444 	ill = ILL_START_WALK_V4(&ctx);
12445 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12446 		mutex_enter(&ill->ill_lock);
12447 		for (ipif = ill->ill_ipif; ipif != NULL;
12448 		    ipif = ipif->ipif_next) {
12449 			if (!IPIF_CAN_LOOKUP(ipif))
12450 				continue;
12451 			if (!(ipif->ipif_flags & IPIF_UP))
12452 				continue;
12453 			if ((ipif->ipif_subnet & net_mask) ==
12454 			    (addr & net_mask)) {
12455 				/*
12456 				 * Don't trust pt-pt interfaces if there are
12457 				 * other interfaces.
12458 				 */
12459 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12460 					if (fallback_ipif == NULL) {
12461 						ipif_refhold_locked(ipif);
12462 						fallback_ipif = ipif;
12463 					}
12464 					continue;
12465 				}
12466 
12467 				/*
12468 				 * Fine. Just assume the same net mask as the
12469 				 * directly attached subnet interface is using.
12470 				 */
12471 				ipif_refhold_locked(ipif);
12472 				mutex_exit(&ill->ill_lock);
12473 				rw_exit(&ill_g_lock);
12474 				if (fallback_ipif != NULL)
12475 					ipif_refrele(fallback_ipif);
12476 				*ipifp = ipif;
12477 				return (ipif->ipif_net_mask);
12478 			}
12479 		}
12480 		mutex_exit(&ill->ill_lock);
12481 	}
12482 	rw_exit(&ill_g_lock);
12483 
12484 	*ipifp = fallback_ipif;
12485 	return ((fallback_ipif != NULL) ?
12486 	    fallback_ipif->ipif_net_mask : net_mask);
12487 }
12488 
12489 /*
12490  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12491  */
12492 static void
12493 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12494 {
12495 	IOCP	iocp;
12496 	ipft_t	*ipft;
12497 	ipllc_t	*ipllc;
12498 	mblk_t	*mp1;
12499 	cred_t	*cr;
12500 	int	error = 0;
12501 	conn_t	*connp;
12502 
12503 	ip1dbg(("ip_wput_ioctl"));
12504 	iocp = (IOCP)mp->b_rptr;
12505 	mp1 = mp->b_cont;
12506 	if (mp1 == NULL) {
12507 		iocp->ioc_error = EINVAL;
12508 		mp->b_datap->db_type = M_IOCNAK;
12509 		iocp->ioc_count = 0;
12510 		qreply(q, mp);
12511 		return;
12512 	}
12513 
12514 	/*
12515 	 * These IOCTLs provide various control capabilities to
12516 	 * upstream agents such as ULPs and processes.	There
12517 	 * are currently two such IOCTLs implemented.  They
12518 	 * are used by TCP to provide update information for
12519 	 * existing IREs and to forcibly delete an IRE for a
12520 	 * host that is not responding, thereby forcing an
12521 	 * attempt at a new route.
12522 	 */
12523 	iocp->ioc_error = EINVAL;
12524 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12525 		goto done;
12526 
12527 	ipllc = (ipllc_t *)mp1->b_rptr;
12528 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12529 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12530 			break;
12531 	}
12532 	/*
12533 	 * prefer credential from mblk over ioctl;
12534 	 * see ip_sioctl_copyin_setup
12535 	 */
12536 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12537 
12538 	/*
12539 	 * Refhold the conn in case the request gets queued up in some lookup
12540 	 */
12541 	ASSERT(CONN_Q(q));
12542 	connp = Q_TO_CONN(q);
12543 	CONN_INC_REF(connp);
12544 	if (ipft->ipft_pfi &&
12545 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12546 		pullupmsg(mp1, ipft->ipft_min_size))) {
12547 		error = (*ipft->ipft_pfi)(q,
12548 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12549 	}
12550 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12551 		/*
12552 		 * CONN_OPER_PENDING_DONE happens in the function called
12553 		 * through ipft_pfi above.
12554 		 */
12555 		return;
12556 	}
12557 
12558 	CONN_OPER_PENDING_DONE(connp);
12559 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12560 		freemsg(mp);
12561 		return;
12562 	}
12563 	iocp->ioc_error = error;
12564 
12565 done:
12566 	mp->b_datap->db_type = M_IOCACK;
12567 	if (iocp->ioc_error)
12568 		iocp->ioc_count = 0;
12569 	qreply(q, mp);
12570 }
12571 
12572 /*
12573  * Lookup an ipif using the sequence id (ipif_seqid)
12574  */
12575 ipif_t *
12576 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12577 {
12578 	ipif_t *ipif;
12579 
12580 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12581 
12582 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12583 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12584 			return (ipif);
12585 	}
12586 	return (NULL);
12587 }
12588 
12589 uint64_t ipif_g_seqid;
12590 
12591 /*
12592  * Assign a unique id for the ipif. This is used later when we send
12593  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12594  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12595  * IRE is added, we verify that ipif has not disappeared.
12596  */
12597 
12598 static void
12599 ipif_assign_seqid(ipif_t *ipif)
12600 {
12601 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12602 }
12603 
12604 /*
12605  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12606  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12607  * be inserted into the first space available in the list. The value of
12608  * ipif_id will then be set to the appropriate value for its position.
12609  */
12610 static int
12611 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12612 {
12613 	ill_t *ill;
12614 	ipif_t *tipif;
12615 	ipif_t **tipifp;
12616 	int id;
12617 
12618 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12619 	    IAM_WRITER_IPIF(ipif));
12620 
12621 	ill = ipif->ipif_ill;
12622 	ASSERT(ill != NULL);
12623 
12624 	/*
12625 	 * In the case of lo0:0 we already hold the ill_g_lock.
12626 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12627 	 * ipif_insert. Another such caller is ipif_move.
12628 	 */
12629 	if (acquire_g_lock)
12630 		rw_enter(&ill_g_lock, RW_WRITER);
12631 	if (acquire_ill_lock)
12632 		mutex_enter(&ill->ill_lock);
12633 	id = ipif->ipif_id;
12634 	tipifp = &(ill->ill_ipif);
12635 	if (id == -1) {	/* need to find a real id */
12636 		id = 0;
12637 		while ((tipif = *tipifp) != NULL) {
12638 			ASSERT(tipif->ipif_id >= id);
12639 			if (tipif->ipif_id != id)
12640 				break; /* non-consecutive id */
12641 			id++;
12642 			tipifp = &(tipif->ipif_next);
12643 		}
12644 		/* limit number of logical interfaces */
12645 		if (id >= ip_addrs_per_if) {
12646 			if (acquire_ill_lock)
12647 				mutex_exit(&ill->ill_lock);
12648 			if (acquire_g_lock)
12649 				rw_exit(&ill_g_lock);
12650 			return (-1);
12651 		}
12652 		ipif->ipif_id = id; /* assign new id */
12653 	} else if (id < ip_addrs_per_if) {
12654 		/* we have a real id; insert ipif in the right place */
12655 		while ((tipif = *tipifp) != NULL) {
12656 			ASSERT(tipif->ipif_id != id);
12657 			if (tipif->ipif_id > id)
12658 				break; /* found correct location */
12659 			tipifp = &(tipif->ipif_next);
12660 		}
12661 	} else {
12662 		if (acquire_ill_lock)
12663 			mutex_exit(&ill->ill_lock);
12664 		if (acquire_g_lock)
12665 			rw_exit(&ill_g_lock);
12666 		return (-1);
12667 	}
12668 
12669 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12670 
12671 	ipif->ipif_next = tipif;
12672 	*tipifp = ipif;
12673 	if (acquire_ill_lock)
12674 		mutex_exit(&ill->ill_lock);
12675 	if (acquire_g_lock)
12676 		rw_exit(&ill_g_lock);
12677 	return (0);
12678 }
12679 
12680 /*
12681  * Allocate and initialize a new interface control structure.  (Always
12682  * called as writer.)
12683  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12684  * is not part of the global linked list of ills. ipif_seqid is unique
12685  * in the system and to preserve the uniqueness, it is assigned only
12686  * when ill becomes part of the global list. At that point ill will
12687  * have a name. If it doesn't get assigned here, it will get assigned
12688  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12689  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12690  * the interface flags or any other information from the DL_INFO_ACK for
12691  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12692  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12693  * second DL_INFO_ACK comes in from the driver.
12694  */
12695 static ipif_t *
12696 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12697 {
12698 	ipif_t	*ipif;
12699 	phyint_t *phyi;
12700 
12701 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12702 	    ill->ill_name, id, (void *)ill));
12703 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12704 
12705 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12706 		return (NULL);
12707 	*ipif = ipif_zero;	/* start clean */
12708 
12709 	ipif->ipif_ill = ill;
12710 	ipif->ipif_id = id;	/* could be -1 */
12711 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12712 
12713 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12714 
12715 	ipif->ipif_refcnt = 0;
12716 	ipif->ipif_saved_ire_cnt = 0;
12717 
12718 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12719 		mi_free(ipif);
12720 		return (NULL);
12721 	}
12722 	/* -1 id should have been replaced by real id */
12723 	id = ipif->ipif_id;
12724 	ASSERT(id >= 0);
12725 
12726 	if (ill->ill_name[0] != '\0') {
12727 		ipif_assign_seqid(ipif);
12728 		if (ill->ill_phyint->phyint_ifindex != 0)
12729 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12730 	}
12731 	/*
12732 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12733 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12734 	 * ioctl sets ipif_orig_ipifid to zero.
12735 	 */
12736 	ipif->ipif_orig_ipifid = id;
12737 
12738 	/*
12739 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12740 	 * The ipif is still not up and can't be looked up until the
12741 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12742 	 */
12743 	mutex_enter(&ill->ill_lock);
12744 	mutex_enter(&ill->ill_phyint->phyint_lock);
12745 	/*
12746 	 * Set the running flag when logical interface zero is created.
12747 	 * For subsequent logical interfaces, a DLPI link down
12748 	 * notification message may have cleared the running flag to
12749 	 * indicate the link is down, so we shouldn't just blindly set it.
12750 	 */
12751 	if (id == 0)
12752 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12753 	ipif->ipif_ire_type = ire_type;
12754 	phyi = ill->ill_phyint;
12755 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12756 
12757 	if (ipif->ipif_isv6) {
12758 		ill->ill_flags |= ILLF_IPV6;
12759 	} else {
12760 		ipaddr_t inaddr_any = INADDR_ANY;
12761 
12762 		ill->ill_flags |= ILLF_IPV4;
12763 
12764 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12765 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12766 		    &ipif->ipif_v6lcl_addr);
12767 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12768 		    &ipif->ipif_v6src_addr);
12769 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12770 		    &ipif->ipif_v6subnet);
12771 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12772 		    &ipif->ipif_v6net_mask);
12773 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12774 		    &ipif->ipif_v6brd_addr);
12775 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12776 		    &ipif->ipif_v6pp_dst_addr);
12777 	}
12778 
12779 	/*
12780 	 * Don't set the interface flags etc. now, will do it in
12781 	 * ip_ll_subnet_defaults.
12782 	 */
12783 	if (!initialize) {
12784 		mutex_exit(&ill->ill_lock);
12785 		mutex_exit(&ill->ill_phyint->phyint_lock);
12786 		return (ipif);
12787 	}
12788 	ipif->ipif_mtu = ill->ill_max_mtu;
12789 
12790 	if (ill->ill_bcast_addr_length != 0) {
12791 		/*
12792 		 * Later detect lack of DLPI driver multicast
12793 		 * capability by catching DL_ENABMULTI errors in
12794 		 * ip_rput_dlpi.
12795 		 */
12796 		ill->ill_flags |= ILLF_MULTICAST;
12797 		if (!ipif->ipif_isv6)
12798 			ipif->ipif_flags |= IPIF_BROADCAST;
12799 	} else {
12800 		if (ill->ill_net_type != IRE_LOOPBACK) {
12801 			if (ipif->ipif_isv6)
12802 				/*
12803 				 * Note: xresolv interfaces will eventually need
12804 				 * NOARP set here as well, but that will require
12805 				 * those external resolvers to have some
12806 				 * knowledge of that flag and act appropriately.
12807 				 * Not to be changed at present.
12808 				 */
12809 				ill->ill_flags |= ILLF_NONUD;
12810 			else
12811 				ill->ill_flags |= ILLF_NOARP;
12812 		}
12813 		if (ill->ill_phys_addr_length == 0) {
12814 			if (ill->ill_media &&
12815 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12816 				ipif->ipif_flags |= IPIF_NOXMIT;
12817 				phyi->phyint_flags |= PHYI_VIRTUAL;
12818 			} else {
12819 				/* pt-pt supports multicast. */
12820 				ill->ill_flags |= ILLF_MULTICAST;
12821 				if (ill->ill_net_type == IRE_LOOPBACK) {
12822 					phyi->phyint_flags |=
12823 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12824 				} else {
12825 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12826 				}
12827 			}
12828 		}
12829 	}
12830 	mutex_exit(&ill->ill_lock);
12831 	mutex_exit(&ill->ill_phyint->phyint_lock);
12832 	return (ipif);
12833 }
12834 
12835 /*
12836  * If appropriate, send a message up to the resolver delete the entry
12837  * for the address of this interface which is going out of business.
12838  * (Always called as writer).
12839  *
12840  * NOTE : We need to check for NULL mps as some of the fields are
12841  *	  initialized only for some interface types. See ipif_resolver_up()
12842  *	  for details.
12843  */
12844 void
12845 ipif_arp_down(ipif_t *ipif)
12846 {
12847 	mblk_t	*mp;
12848 
12849 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12850 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12851 	ASSERT(IAM_WRITER_IPIF(ipif));
12852 
12853 	/* Delete the mapping for the local address */
12854 	mp = ipif->ipif_arp_del_mp;
12855 	if (mp != NULL) {
12856 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12857 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12858 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12859 		putnext(ipif->ipif_ill->ill_rq, mp);
12860 		ipif->ipif_arp_del_mp = NULL;
12861 	}
12862 
12863 	/*
12864 	 * If this is the last ipif that is going down, we need
12865 	 * to clean up ARP completely.
12866 	 */
12867 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12868 
12869 		/* Send up AR_INTERFACE_DOWN message */
12870 		mp = ipif->ipif_ill->ill_arp_down_mp;
12871 		if (mp != NULL) {
12872 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12873 			    dlpi_prim_str(*(int *)mp->b_rptr),
12874 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12875 			    ipif->ipif_id));
12876 			putnext(ipif->ipif_ill->ill_rq, mp);
12877 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12878 		}
12879 
12880 		/* Tell ARP to delete the multicast mappings */
12881 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12882 		if (mp != NULL) {
12883 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12884 			    dlpi_prim_str(*(int *)mp->b_rptr),
12885 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12886 			    ipif->ipif_id));
12887 			putnext(ipif->ipif_ill->ill_rq, mp);
12888 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12889 		}
12890 	}
12891 }
12892 
12893 /*
12894  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12895  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12896  * that it wants the add_mp allocated in this function to be returned
12897  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12898  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12899  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12900  * as it does a ipif_arp_down after calling this function - which will
12901  * remove what we add here.
12902  *
12903  * Returns -1 on failures and 0 on success.
12904  */
12905 int
12906 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12907 {
12908 	mblk_t	*del_mp = NULL;
12909 	mblk_t *add_mp = NULL;
12910 	mblk_t *mp;
12911 	ill_t	*ill = ipif->ipif_ill;
12912 	phyint_t *phyi = ill->ill_phyint;
12913 	ipaddr_t addr, mask, extract_mask = 0;
12914 	arma_t	*arma;
12915 	uint8_t *maddr, *bphys_addr;
12916 	uint32_t hw_start;
12917 	dl_unitdata_req_t *dlur;
12918 
12919 	ASSERT(IAM_WRITER_IPIF(ipif));
12920 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12921 		return (0);
12922 
12923 	/*
12924 	 * Delete the existing mapping from ARP. Normally ipif_down
12925 	 * -> ipif_arp_down should send this up to ARP. The only
12926 	 * reason we would find this when we are switching from
12927 	 * Multicast to Broadcast where we did not do a down.
12928 	 */
12929 	mp = ill->ill_arp_del_mapping_mp;
12930 	if (mp != NULL) {
12931 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12932 		    dlpi_prim_str(*(int *)mp->b_rptr),
12933 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12934 		putnext(ill->ill_rq, mp);
12935 		ill->ill_arp_del_mapping_mp = NULL;
12936 	}
12937 
12938 	if (arp_add_mapping_mp != NULL)
12939 		*arp_add_mapping_mp = NULL;
12940 
12941 	/*
12942 	 * Check that the address is not to long for the constant
12943 	 * length reserved in the template arma_t.
12944 	 */
12945 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12946 		return (-1);
12947 
12948 	/* Add mapping mblk */
12949 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
12950 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
12951 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
12952 	    (caddr_t)&addr);
12953 	if (add_mp == NULL)
12954 		return (-1);
12955 	arma = (arma_t *)add_mp->b_rptr;
12956 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
12957 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
12958 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
12959 
12960 	/*
12961 	 * Determine the broadcast address.
12962 	 */
12963 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
12964 	if (ill->ill_sap_length < 0)
12965 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
12966 	else
12967 		bphys_addr = (uchar_t *)dlur +
12968 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
12969 	/*
12970 	 * Check PHYI_MULTI_BCAST and length of physical
12971 	 * address to determine if we use the mapping or the
12972 	 * broadcast address.
12973 	 */
12974 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
12975 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
12976 		    bphys_addr, maddr, &hw_start, &extract_mask))
12977 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
12978 
12979 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
12980 	    (ill->ill_flags & ILLF_MULTICAST)) {
12981 		/* Make sure this will not match the "exact" entry. */
12982 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
12983 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
12984 		    (caddr_t)&addr);
12985 		if (del_mp == NULL) {
12986 			freemsg(add_mp);
12987 			return (-1);
12988 		}
12989 		bcopy(&extract_mask, (char *)arma +
12990 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
12991 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
12992 			/* Use link-layer broadcast address for MULTI_BCAST */
12993 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
12994 			ip2dbg(("ipif_arp_setup_multicast: adding"
12995 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
12996 		} else {
12997 			arma->arma_hw_mapping_start = hw_start;
12998 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
12999 			    " ARP setup for %s\n", ill->ill_name));
13000 		}
13001 	} else {
13002 		freemsg(add_mp);
13003 		ASSERT(del_mp == NULL);
13004 		/* It is neither MULTICAST nor MULTI_BCAST */
13005 		return (0);
13006 	}
13007 	ASSERT(add_mp != NULL && del_mp != NULL);
13008 	ill->ill_arp_del_mapping_mp = del_mp;
13009 	if (arp_add_mapping_mp != NULL) {
13010 		/* The caller just wants the mblks allocated */
13011 		*arp_add_mapping_mp = add_mp;
13012 	} else {
13013 		/* The caller wants us to send it to arp */
13014 		putnext(ill->ill_rq, add_mp);
13015 	}
13016 	return (0);
13017 }
13018 
13019 /*
13020  * Get the resolver set up for a new interface address.
13021  * (Always called as writer.)
13022  * Called both for IPv4 and IPv6 interfaces,
13023  * though it only sets up the resolver for v6
13024  * if it's an xresolv interface (one using an external resolver).
13025  * Honors ILLF_NOARP.
13026  * The boolean value arp_just_publish, if B_TRUE, indicates that
13027  * it only needs to send an AR_ENTRY_ADD message up to ARP for
13028  * IPv4 interfaces. Currently, B_TRUE is only set when this
13029  * function is called by ip_rput_dlpi_writer() to handle
13030  * asynchronous hardware address change notification.
13031  * Returns error on failure.
13032  */
13033 int
13034 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
13035 {
13036 	caddr_t	addr;
13037 	mblk_t	*arp_up_mp = NULL;
13038 	mblk_t	*arp_down_mp = NULL;
13039 	mblk_t	*arp_add_mp = NULL;
13040 	mblk_t	*arp_del_mp = NULL;
13041 	mblk_t	*arp_add_mapping_mp = NULL;
13042 	mblk_t	*arp_del_mapping_mp = NULL;
13043 	ill_t	*ill = ipif->ipif_ill;
13044 	uchar_t	*area_p = NULL;
13045 	uchar_t	*ared_p = NULL;
13046 	int	err = ENOMEM;
13047 
13048 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13049 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
13050 	    (uint_t)ipif->ipif_flags));
13051 	ASSERT(IAM_WRITER_IPIF(ipif));
13052 
13053 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
13054 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
13055 		return (0);
13056 	}
13057 
13058 	if (ill->ill_isv6) {
13059 		/*
13060 		 * External resolver for IPv6
13061 		 */
13062 		ASSERT(!arp_just_publish);
13063 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13064 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13065 			area_p = (uchar_t *)&ip6_area_template;
13066 			ared_p = (uchar_t *)&ip6_ared_template;
13067 		}
13068 	} else {
13069 		/*
13070 		 * IPv4 arp case. If the ARP stream has already started
13071 		 * closing, fail this request for ARP bringup. Else
13072 		 * record the fact that an ARP bringup is pending.
13073 		 */
13074 		mutex_enter(&ill->ill_lock);
13075 		if (ill->ill_arp_closing) {
13076 			mutex_exit(&ill->ill_lock);
13077 			err = EINVAL;
13078 			goto failed;
13079 		} else {
13080 			if (ill->ill_ipif_up_count == 0)
13081 				ill->ill_arp_bringup_pending = 1;
13082 			mutex_exit(&ill->ill_lock);
13083 		}
13084 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13085 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13086 			area_p = (uchar_t *)&ip_area_template;
13087 			ared_p = (uchar_t *)&ip_ared_template;
13088 		}
13089 	}
13090 
13091 	/*
13092 	 * Add an entry for the local address in ARP only if it
13093 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13094 	 */
13095 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
13096 		/* Now ask ARP to publish our address. */
13097 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13098 		if (arp_add_mp == NULL)
13099 			goto failed;
13100 		if (arp_just_publish) {
13101 			/*
13102 			 * Copy the new hardware address and length into
13103 			 * arp_add_mp to be sent to ARP.
13104 			 */
13105 			area_t *area = (area_t *)arp_add_mp->b_rptr;
13106 			area->area_hw_addr_length =
13107 			    ill->ill_phys_addr_length;
13108 			bcopy((char *)ill->ill_phys_addr,
13109 			    ((char *)area + area->area_hw_addr_offset),
13110 			    area->area_hw_addr_length);
13111 		}
13112 
13113 		((area_t *)arp_add_mp->b_rptr)->area_flags =
13114 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
13115 
13116 		if (arp_just_publish)
13117 			goto arp_setup_multicast;
13118 
13119 		/*
13120 		 * Allocate an ARP deletion message so we know we can tell ARP
13121 		 * when the interface goes down.
13122 		 */
13123 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13124 		if (arp_del_mp == NULL)
13125 			goto failed;
13126 
13127 	} else {
13128 		if (arp_just_publish)
13129 			goto done;
13130 	}
13131 	/*
13132 	 * Need to bring up ARP or setup multicast mapping only
13133 	 * when the first interface is coming UP.
13134 	 */
13135 	if (ill->ill_ipif_up_count != 0)
13136 		goto done;
13137 
13138 	/*
13139 	 * Allocate an ARP down message (to be saved) and an ARP up
13140 	 * message.
13141 	 */
13142 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13143 	if (arp_down_mp == NULL)
13144 		goto failed;
13145 
13146 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13147 	if (arp_up_mp == NULL)
13148 		goto failed;
13149 
13150 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13151 		goto done;
13152 
13153 arp_setup_multicast:
13154 	/*
13155 	 * Setup the multicast mappings. This function initializes
13156 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13157 	 * IPv6.
13158 	 */
13159 	if (!ill->ill_isv6) {
13160 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13161 		if (err != 0)
13162 			goto failed;
13163 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13164 		ASSERT(arp_add_mapping_mp != NULL);
13165 	}
13166 
13167 done:;
13168 	if (arp_del_mp != NULL) {
13169 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13170 		ipif->ipif_arp_del_mp = arp_del_mp;
13171 	}
13172 	if (arp_down_mp != NULL) {
13173 		ASSERT(ill->ill_arp_down_mp == NULL);
13174 		ill->ill_arp_down_mp = arp_down_mp;
13175 	}
13176 	if (arp_del_mapping_mp != NULL) {
13177 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13178 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13179 	}
13180 	if (arp_up_mp != NULL) {
13181 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13182 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13183 		putnext(ill->ill_rq, arp_up_mp);
13184 	}
13185 	if (arp_add_mp != NULL) {
13186 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13187 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13188 		putnext(ill->ill_rq, arp_add_mp);
13189 	}
13190 	if (arp_add_mapping_mp != NULL) {
13191 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13192 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13193 		putnext(ill->ill_rq, arp_add_mapping_mp);
13194 	}
13195 	if (arp_just_publish)
13196 		return (0);
13197 
13198 	if (ill->ill_flags & ILLF_NOARP)
13199 		err = ill_arp_off(ill);
13200 	else
13201 		err = ill_arp_on(ill);
13202 	if (err) {
13203 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13204 		freemsg(ipif->ipif_arp_del_mp);
13205 		if (arp_down_mp != NULL)
13206 			freemsg(ill->ill_arp_down_mp);
13207 		if (ill->ill_arp_del_mapping_mp != NULL)
13208 			freemsg(ill->ill_arp_del_mapping_mp);
13209 		ipif->ipif_arp_del_mp = NULL;
13210 		ill->ill_arp_down_mp = NULL;
13211 		ill->ill_arp_del_mapping_mp = NULL;
13212 		return (err);
13213 	}
13214 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
13215 
13216 failed:;
13217 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13218 	freemsg(arp_add_mp);
13219 	freemsg(arp_del_mp);
13220 	freemsg(arp_add_mapping_mp);
13221 	freemsg(arp_up_mp);
13222 	freemsg(arp_down_mp);
13223 	ill->ill_arp_bringup_pending = 0;
13224 	return (err);
13225 }
13226 
13227 /*
13228  * Wakeup all threads waiting to enter the ipsq, and sleeping
13229  * on any of the ills in this ipsq. The ill_lock of the ill
13230  * must be held so that waiters don't miss wakeups
13231  */
13232 static void
13233 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13234 {
13235 	phyint_t *phyint;
13236 
13237 	phyint = ipsq->ipsq_phyint_list;
13238 	while (phyint != NULL) {
13239 		if (phyint->phyint_illv4) {
13240 			if (!caller_holds_lock)
13241 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13242 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13243 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13244 			if (!caller_holds_lock)
13245 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13246 		}
13247 		if (phyint->phyint_illv6) {
13248 			if (!caller_holds_lock)
13249 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13250 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13251 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13252 			if (!caller_holds_lock)
13253 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13254 		}
13255 		phyint = phyint->phyint_ipsq_next;
13256 	}
13257 }
13258 
13259 static ipsq_t *
13260 ipsq_create(char *groupname)
13261 {
13262 	ipsq_t	*ipsq;
13263 
13264 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13265 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13266 	if (ipsq == NULL) {
13267 		return (NULL);
13268 	}
13269 
13270 	if (groupname != NULL)
13271 		(void) strcpy(ipsq->ipsq_name, groupname);
13272 	else
13273 		ipsq->ipsq_name[0] = '\0';
13274 
13275 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13276 	ipsq->ipsq_flags |= IPSQ_GROUP;
13277 	ipsq->ipsq_next = ipsq_g_head;
13278 	ipsq_g_head = ipsq;
13279 	return (ipsq);
13280 }
13281 
13282 /*
13283  * Return an ipsq correspoding to the groupname. If 'create' is true
13284  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13285  * uniquely with an IPMP group. However during IPMP groupname operations,
13286  * multiple IPMP groups may be associated with a single ipsq. But no
13287  * IPMP group can be associated with more than 1 ipsq at any time.
13288  * For example
13289  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13290  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13291  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13292  *
13293  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13294  * status shown below during the execution of the above command.
13295  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13296  *
13297  * After the completion of the above groupname command we return to the stable
13298  * state shown below.
13299  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13300  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13301  *
13302  * Because of the above, we don't search based on the ipsq_name since that
13303  * would miss the correct ipsq during certain windows as shown above.
13304  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13305  * natural state.
13306  */
13307 static ipsq_t *
13308 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13309 {
13310 	ipsq_t	*ipsq;
13311 	int	group_len;
13312 	phyint_t *phyint;
13313 
13314 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13315 
13316 	group_len = strlen(groupname);
13317 	ASSERT(group_len != 0);
13318 	group_len++;
13319 
13320 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13321 		/*
13322 		 * When an ipsq is being split, and ill_split_ipsq
13323 		 * calls this function, we exclude it from being considered.
13324 		 */
13325 		if (ipsq == exclude_ipsq)
13326 			continue;
13327 
13328 		/*
13329 		 * Compare against the ipsq_name. The groupname change happens
13330 		 * in 2 phases. The 1st phase merges the from group into
13331 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13332 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13333 		 * ipsq_name. At this point the phyint_groupname has not been
13334 		 * updated.
13335 		 */
13336 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13337 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13338 			/*
13339 			 * Verify that an ipmp groupname is exactly
13340 			 * part of 1 ipsq and is not found in any other
13341 			 * ipsq.
13342 			 */
13343 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13344 			    NULL);
13345 			return (ipsq);
13346 		}
13347 
13348 		/*
13349 		 * Comparison against ipsq_name alone is not sufficient.
13350 		 * In the case when groups are currently being
13351 		 * merged, the ipsq could hold other IPMP groups temporarily.
13352 		 * so we walk the phyint list and compare against the
13353 		 * phyint_groupname as well.
13354 		 */
13355 		phyint = ipsq->ipsq_phyint_list;
13356 		while (phyint != NULL) {
13357 			if ((group_len == phyint->phyint_groupname_len) &&
13358 			    (bcmp(phyint->phyint_groupname, groupname,
13359 			    group_len) == 0)) {
13360 				/*
13361 				 * Verify that an ipmp groupname is exactly
13362 				 * part of 1 ipsq and is not found in any other
13363 				 * ipsq.
13364 				 */
13365 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13366 					== NULL);
13367 				return (ipsq);
13368 			}
13369 			phyint = phyint->phyint_ipsq_next;
13370 		}
13371 	}
13372 	if (create)
13373 		ipsq = ipsq_create(groupname);
13374 	return (ipsq);
13375 }
13376 
13377 static void
13378 ipsq_delete(ipsq_t *ipsq)
13379 {
13380 	ipsq_t *nipsq;
13381 	ipsq_t *pipsq = NULL;
13382 
13383 	/*
13384 	 * We don't hold the ipsq lock, but we are sure no new
13385 	 * messages can land up, since the ipsq_refs is zero.
13386 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13387 	 * is associated with this ipsq. (Lookups are based on ill_name
13388 	 * or phyint_group_name)
13389 	 */
13390 	ASSERT(ipsq->ipsq_refs == 0);
13391 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13392 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13393 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13394 		/*
13395 		 * This is not the ipsq of an IPMP group.
13396 		 */
13397 		kmem_free(ipsq, sizeof (ipsq_t));
13398 		return;
13399 	}
13400 
13401 	rw_enter(&ill_g_lock, RW_WRITER);
13402 
13403 	/*
13404 	 * Locate the ipsq  before we can remove it from
13405 	 * the singly linked list of ipsq's.
13406 	 */
13407 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13408 		if (nipsq == ipsq) {
13409 			break;
13410 		}
13411 		pipsq = nipsq;
13412 	}
13413 
13414 	ASSERT(nipsq == ipsq);
13415 
13416 	/* unlink ipsq from the list */
13417 	if (pipsq != NULL)
13418 		pipsq->ipsq_next = ipsq->ipsq_next;
13419 	else
13420 		ipsq_g_head = ipsq->ipsq_next;
13421 	kmem_free(ipsq, sizeof (ipsq_t));
13422 	rw_exit(&ill_g_lock);
13423 }
13424 
13425 static void
13426 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13427     queue_t *q)
13428 
13429 {
13430 
13431 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13432 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13433 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13434 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13435 	ASSERT(current_mp != NULL);
13436 
13437 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13438 		NEW_OP, NULL);
13439 
13440 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13441 	    new_ipsq->ipsq_xopq_mphead != NULL);
13442 
13443 	/*
13444 	 * move from old ipsq to the new ipsq.
13445 	 */
13446 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13447 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13448 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13449 
13450 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13451 }
13452 
13453 void
13454 ill_group_cleanup(ill_t *ill)
13455 {
13456 	ill_t *ill_v4;
13457 	ill_t *ill_v6;
13458 	ipif_t *ipif;
13459 
13460 	ill_v4 = ill->ill_phyint->phyint_illv4;
13461 	ill_v6 = ill->ill_phyint->phyint_illv6;
13462 
13463 	if (ill_v4 != NULL) {
13464 		mutex_enter(&ill_v4->ill_lock);
13465 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13466 		    ipif = ipif->ipif_next) {
13467 			IPIF_UNMARK_MOVING(ipif);
13468 		}
13469 		ill_v4->ill_up_ipifs = B_FALSE;
13470 		mutex_exit(&ill_v4->ill_lock);
13471 	}
13472 
13473 	if (ill_v6 != NULL) {
13474 		mutex_enter(&ill_v6->ill_lock);
13475 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13476 		    ipif = ipif->ipif_next) {
13477 			IPIF_UNMARK_MOVING(ipif);
13478 		}
13479 		ill_v6->ill_up_ipifs = B_FALSE;
13480 		mutex_exit(&ill_v6->ill_lock);
13481 	}
13482 }
13483 /*
13484  * This function is called when an ill has had a change in its group status
13485  * to bring up all the ipifs that were up before the change.
13486  */
13487 int
13488 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13489 {
13490 	ipif_t *ipif;
13491 	ill_t *ill_v4;
13492 	ill_t *ill_v6;
13493 	ill_t *from_ill;
13494 	int err = 0;
13495 
13496 
13497 	ASSERT(IAM_WRITER_ILL(ill));
13498 
13499 	/*
13500 	 * Except for ipif_state_flags and ill_state_flags the other
13501 	 * fields of the ipif/ill that are modified below are protected
13502 	 * implicitly since we are a writer. We would have tried to down
13503 	 * even an ipif that was already down, in ill_down_ipifs. So we
13504 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13505 	 */
13506 	ill_v4 = ill->ill_phyint->phyint_illv4;
13507 	ill_v6 = ill->ill_phyint->phyint_illv6;
13508 	if (ill_v4 != NULL) {
13509 		ill_v4->ill_up_ipifs = B_TRUE;
13510 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13511 		    ipif = ipif->ipif_next) {
13512 			mutex_enter(&ill_v4->ill_lock);
13513 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13514 			IPIF_UNMARK_MOVING(ipif);
13515 			mutex_exit(&ill_v4->ill_lock);
13516 			if (ipif->ipif_was_up) {
13517 				if (!(ipif->ipif_flags & IPIF_UP))
13518 					err = ipif_up(ipif, q, mp);
13519 				ipif->ipif_was_up = B_FALSE;
13520 				if (err != 0) {
13521 					/*
13522 					 * Can there be any other error ?
13523 					 */
13524 					ASSERT(err == EINPROGRESS);
13525 					return (err);
13526 				}
13527 			}
13528 		}
13529 		mutex_enter(&ill_v4->ill_lock);
13530 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13531 		mutex_exit(&ill_v4->ill_lock);
13532 		ill_v4->ill_up_ipifs = B_FALSE;
13533 		if (ill_v4->ill_move_in_progress) {
13534 			ASSERT(ill_v4->ill_move_peer != NULL);
13535 			ill_v4->ill_move_in_progress = B_FALSE;
13536 			from_ill = ill_v4->ill_move_peer;
13537 			from_ill->ill_move_in_progress = B_FALSE;
13538 			from_ill->ill_move_peer = NULL;
13539 			mutex_enter(&from_ill->ill_lock);
13540 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13541 			mutex_exit(&from_ill->ill_lock);
13542 			if (ill_v6 == NULL) {
13543 				if (from_ill->ill_phyint->phyint_flags &
13544 				    PHYI_STANDBY) {
13545 					phyint_inactive(from_ill->ill_phyint);
13546 				}
13547 				if (ill_v4->ill_phyint->phyint_flags &
13548 				    PHYI_STANDBY) {
13549 					phyint_inactive(ill_v4->ill_phyint);
13550 				}
13551 			}
13552 			ill_v4->ill_move_peer = NULL;
13553 		}
13554 	}
13555 
13556 	if (ill_v6 != NULL) {
13557 		ill_v6->ill_up_ipifs = B_TRUE;
13558 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13559 		    ipif = ipif->ipif_next) {
13560 			mutex_enter(&ill_v6->ill_lock);
13561 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13562 			IPIF_UNMARK_MOVING(ipif);
13563 			mutex_exit(&ill_v6->ill_lock);
13564 			if (ipif->ipif_was_up) {
13565 				if (!(ipif->ipif_flags & IPIF_UP))
13566 					err = ipif_up(ipif, q, mp);
13567 				ipif->ipif_was_up = B_FALSE;
13568 				if (err != 0) {
13569 					/*
13570 					 * Can there be any other error ?
13571 					 */
13572 					ASSERT(err == EINPROGRESS);
13573 					return (err);
13574 				}
13575 			}
13576 		}
13577 		mutex_enter(&ill_v6->ill_lock);
13578 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13579 		mutex_exit(&ill_v6->ill_lock);
13580 		ill_v6->ill_up_ipifs = B_FALSE;
13581 		if (ill_v6->ill_move_in_progress) {
13582 			ASSERT(ill_v6->ill_move_peer != NULL);
13583 			ill_v6->ill_move_in_progress = B_FALSE;
13584 			from_ill = ill_v6->ill_move_peer;
13585 			from_ill->ill_move_in_progress = B_FALSE;
13586 			from_ill->ill_move_peer = NULL;
13587 			mutex_enter(&from_ill->ill_lock);
13588 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13589 			mutex_exit(&from_ill->ill_lock);
13590 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13591 				phyint_inactive(from_ill->ill_phyint);
13592 			}
13593 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13594 				phyint_inactive(ill_v6->ill_phyint);
13595 			}
13596 			ill_v6->ill_move_peer = NULL;
13597 		}
13598 	}
13599 	return (0);
13600 }
13601 
13602 /*
13603  * bring down all the approriate ipifs.
13604  */
13605 /* ARGSUSED */
13606 static void
13607 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13608 {
13609 	ipif_t *ipif;
13610 
13611 	ASSERT(IAM_WRITER_ILL(ill));
13612 
13613 	/*
13614 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13615 	 * are modified below are protected implicitly since we are a writer
13616 	 */
13617 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13618 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13619 			continue;
13620 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13621 			/*
13622 			 * We go through the ipif_down logic even if the ipif
13623 			 * is already down, since routes can be added based
13624 			 * on down ipifs. Going through ipif_down once again
13625 			 * will delete any IREs created based on these routes.
13626 			 */
13627 			if (ipif->ipif_flags & IPIF_UP)
13628 				ipif->ipif_was_up = B_TRUE;
13629 			/*
13630 			 * If called with chk_nofailover true ipif is moving.
13631 			 */
13632 			mutex_enter(&ill->ill_lock);
13633 			if (chk_nofailover) {
13634 				ipif->ipif_state_flags |=
13635 					IPIF_MOVING | IPIF_CHANGING;
13636 			} else {
13637 				ipif->ipif_state_flags |= IPIF_CHANGING;
13638 			}
13639 			mutex_exit(&ill->ill_lock);
13640 			/*
13641 			 * Need to re-create net/subnet bcast ires if
13642 			 * they are dependent on ipif.
13643 			 */
13644 			if (!ipif->ipif_isv6)
13645 				ipif_check_bcast_ires(ipif);
13646 			(void) ipif_logical_down(ipif, NULL, NULL);
13647 			ipif_down_tail(ipif);
13648 			/*
13649 			 * We don't do ipif_multicast_down for IPv4 in
13650 			 * ipif_down. We need to set this so that
13651 			 * ipif_multicast_up will join the
13652 			 * ALLHOSTS_GROUP on to_ill.
13653 			 */
13654 			ipif->ipif_multicast_up = B_FALSE;
13655 		}
13656 	}
13657 }
13658 
13659 #define	IPSQ_INC_REF(ipsq)	{			\
13660 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13661 	(ipsq)->ipsq_refs++;				\
13662 }
13663 
13664 #define	IPSQ_DEC_REF(ipsq)	{			\
13665 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13666 	(ipsq)->ipsq_refs--;				\
13667 	if ((ipsq)->ipsq_refs == 0)				\
13668 		(ipsq)->ipsq_name[0] = '\0'; 		\
13669 }
13670 
13671 /*
13672  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13673  * new_ipsq.
13674  */
13675 static void
13676 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13677 {
13678 	phyint_t *phyint;
13679 	phyint_t *next_phyint;
13680 
13681 	/*
13682 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13683 	 * writer and the ill_lock of the ill in question. Also the dest
13684 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13685 	 */
13686 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13687 
13688 	phyint = cur_ipsq->ipsq_phyint_list;
13689 	cur_ipsq->ipsq_phyint_list = NULL;
13690 	while (phyint != NULL) {
13691 		next_phyint = phyint->phyint_ipsq_next;
13692 		IPSQ_DEC_REF(cur_ipsq);
13693 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13694 		new_ipsq->ipsq_phyint_list = phyint;
13695 		IPSQ_INC_REF(new_ipsq);
13696 		phyint->phyint_ipsq = new_ipsq;
13697 		phyint = next_phyint;
13698 	}
13699 }
13700 
13701 #define	SPLIT_SUCCESS		0
13702 #define	SPLIT_NOT_NEEDED	1
13703 #define	SPLIT_FAILED		2
13704 
13705 int
13706 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13707 {
13708 	ipsq_t *newipsq = NULL;
13709 
13710 	/*
13711 	 * Assertions denote pre-requisites for changing the ipsq of
13712 	 * a phyint
13713 	 */
13714 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13715 	/*
13716 	 * <ill-phyint> assocs can't change while ill_g_lock
13717 	 * is held as writer. See ill_phyint_reinit()
13718 	 */
13719 	ASSERT(phyint->phyint_illv4 == NULL ||
13720 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13721 	ASSERT(phyint->phyint_illv6 == NULL ||
13722 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13723 
13724 	if ((phyint->phyint_groupname_len !=
13725 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13726 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13727 	    phyint->phyint_groupname_len) != 0)) {
13728 		/*
13729 		 * Once we fail in creating a new ipsq due to memory shortage,
13730 		 * don't attempt to create new ipsq again, based on another
13731 		 * phyint, since we want all phyints belonging to an IPMP group
13732 		 * to be in the same ipsq even in the event of mem alloc fails.
13733 		 */
13734 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13735 		    cur_ipsq);
13736 		if (newipsq == NULL) {
13737 			/* Memory allocation failure */
13738 			return (SPLIT_FAILED);
13739 		} else {
13740 			/* ipsq_refs protected by ill_g_lock (writer) */
13741 			IPSQ_DEC_REF(cur_ipsq);
13742 			phyint->phyint_ipsq = newipsq;
13743 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13744 			newipsq->ipsq_phyint_list = phyint;
13745 			IPSQ_INC_REF(newipsq);
13746 			return (SPLIT_SUCCESS);
13747 		}
13748 	}
13749 	return (SPLIT_NOT_NEEDED);
13750 }
13751 
13752 /*
13753  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13754  * to do this split
13755  */
13756 static int
13757 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13758 {
13759 	ipsq_t *newipsq;
13760 
13761 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13762 	/*
13763 	 * <ill-phyint> assocs can't change while ill_g_lock
13764 	 * is held as writer. See ill_phyint_reinit()
13765 	 */
13766 
13767 	ASSERT(phyint->phyint_illv4 == NULL ||
13768 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13769 	ASSERT(phyint->phyint_illv6 == NULL ||
13770 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13771 
13772 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13773 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13774 		/*
13775 		 * ipsq_init failed due to no memory
13776 		 * caller will use the same ipsq
13777 		 */
13778 		return (SPLIT_FAILED);
13779 	}
13780 
13781 	/* ipsq_ref is protected by ill_g_lock (writer) */
13782 	IPSQ_DEC_REF(cur_ipsq);
13783 
13784 	/*
13785 	 * This is a new ipsq that is unknown to the world.
13786 	 * So we don't need to hold ipsq_lock,
13787 	 */
13788 	newipsq = phyint->phyint_ipsq;
13789 	newipsq->ipsq_writer = NULL;
13790 	newipsq->ipsq_reentry_cnt--;
13791 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13792 #ifdef ILL_DEBUG
13793 	newipsq->ipsq_depth = 0;
13794 #endif
13795 
13796 	return (SPLIT_SUCCESS);
13797 }
13798 
13799 /*
13800  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13801  * ipsq's representing their individual groups or themselves. Return
13802  * whether split needs to be retried again later.
13803  */
13804 static boolean_t
13805 ill_split_ipsq(ipsq_t *cur_ipsq)
13806 {
13807 	phyint_t *phyint;
13808 	phyint_t *next_phyint;
13809 	int	error;
13810 	boolean_t need_retry = B_FALSE;
13811 
13812 	phyint = cur_ipsq->ipsq_phyint_list;
13813 	cur_ipsq->ipsq_phyint_list = NULL;
13814 	while (phyint != NULL) {
13815 		next_phyint = phyint->phyint_ipsq_next;
13816 		/*
13817 		 * 'created' will tell us whether the callee actually
13818 		 * created an ipsq. Lack of memory may force the callee
13819 		 * to return without creating an ipsq.
13820 		 */
13821 		if (phyint->phyint_groupname == NULL) {
13822 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13823 		} else {
13824 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13825 					need_retry);
13826 		}
13827 
13828 		switch (error) {
13829 		case SPLIT_FAILED:
13830 			need_retry = B_TRUE;
13831 			/* FALLTHRU */
13832 		case SPLIT_NOT_NEEDED:
13833 			/*
13834 			 * Keep it on the list.
13835 			 */
13836 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13837 			cur_ipsq->ipsq_phyint_list = phyint;
13838 			break;
13839 		case SPLIT_SUCCESS:
13840 			break;
13841 		default:
13842 			ASSERT(0);
13843 		}
13844 
13845 		phyint = next_phyint;
13846 	}
13847 	return (need_retry);
13848 }
13849 
13850 /*
13851  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13852  * and return the ills in the list. This list will be
13853  * needed to unlock all the ills later on by the caller.
13854  * The <ill-ipsq> associations could change between the
13855  * lock and unlock. Hence the unlock can't traverse the
13856  * ipsq to get the list of ills.
13857  */
13858 static int
13859 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13860 {
13861 	int	cnt = 0;
13862 	phyint_t	*phyint;
13863 
13864 	/*
13865 	 * The caller holds ill_g_lock to ensure that the ill memberships
13866 	 * of the ipsq don't change
13867 	 */
13868 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13869 
13870 	phyint = ipsq->ipsq_phyint_list;
13871 	while (phyint != NULL) {
13872 		if (phyint->phyint_illv4 != NULL) {
13873 			ASSERT(cnt < list_max);
13874 			list[cnt++] = phyint->phyint_illv4;
13875 		}
13876 		if (phyint->phyint_illv6 != NULL) {
13877 			ASSERT(cnt < list_max);
13878 			list[cnt++] = phyint->phyint_illv6;
13879 		}
13880 		phyint = phyint->phyint_ipsq_next;
13881 	}
13882 	ill_lock_ills(list, cnt);
13883 	return (cnt);
13884 }
13885 
13886 void
13887 ill_lock_ills(ill_t **list, int cnt)
13888 {
13889 	int	i;
13890 
13891 	if (cnt > 1) {
13892 		boolean_t try_again;
13893 		do {
13894 			try_again = B_FALSE;
13895 			for (i = 0; i < cnt - 1; i++) {
13896 				if (list[i] < list[i + 1]) {
13897 					ill_t	*tmp;
13898 
13899 					/* swap the elements */
13900 					tmp = list[i];
13901 					list[i] = list[i + 1];
13902 					list[i + 1] = tmp;
13903 					try_again = B_TRUE;
13904 				}
13905 			}
13906 		} while (try_again);
13907 	}
13908 
13909 	for (i = 0; i < cnt; i++) {
13910 		if (i == 0) {
13911 			if (list[i] != NULL)
13912 				mutex_enter(&list[i]->ill_lock);
13913 			else
13914 				return;
13915 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13916 			mutex_enter(&list[i]->ill_lock);
13917 		}
13918 	}
13919 }
13920 
13921 void
13922 ill_unlock_ills(ill_t **list, int cnt)
13923 {
13924 	int	i;
13925 
13926 	for (i = 0; i < cnt; i++) {
13927 		if ((i == 0) && (list[i] != NULL)) {
13928 			mutex_exit(&list[i]->ill_lock);
13929 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13930 			mutex_exit(&list[i]->ill_lock);
13931 		}
13932 	}
13933 }
13934 
13935 /*
13936  * Merge all the ills from 1 ipsq group into another ipsq group.
13937  * The source ipsq group is specified by the ipsq associated with
13938  * 'from_ill'. The destination ipsq group is specified by the ipsq
13939  * associated with 'to_ill' or 'groupname' respectively.
13940  * Note that ipsq itself does not have a reference count mechanism
13941  * and functions don't look up an ipsq and pass it around. Instead
13942  * functions pass around an ill or groupname, and the ipsq is looked
13943  * up from the ill or groupname and the required operation performed
13944  * atomically with the lookup on the ipsq.
13945  */
13946 static int
13947 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13948     queue_t *q)
13949 {
13950 	ipsq_t *old_ipsq;
13951 	ipsq_t *new_ipsq;
13952 	ill_t	**ill_list;
13953 	int	cnt;
13954 	size_t	ill_list_size;
13955 	boolean_t became_writer_on_new_sq = B_FALSE;
13956 
13957 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
13958 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
13959 
13960 	/*
13961 	 * Need to hold ill_g_lock as writer and also the ill_lock to
13962 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
13963 	 * ipsq_lock to prevent new messages from landing on an ipsq.
13964 	 */
13965 	rw_enter(&ill_g_lock, RW_WRITER);
13966 
13967 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
13968 	if (groupname != NULL)
13969 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
13970 	else {
13971 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
13972 	}
13973 
13974 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
13975 
13976 	/*
13977 	 * both groups are on the same ipsq.
13978 	 */
13979 	if (old_ipsq == new_ipsq) {
13980 		rw_exit(&ill_g_lock);
13981 		return (0);
13982 	}
13983 
13984 	cnt = old_ipsq->ipsq_refs << 1;
13985 	ill_list_size = cnt * sizeof (ill_t *);
13986 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
13987 	if (ill_list == NULL) {
13988 		rw_exit(&ill_g_lock);
13989 		return (ENOMEM);
13990 	}
13991 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
13992 
13993 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
13994 	mutex_enter(&new_ipsq->ipsq_lock);
13995 	if ((new_ipsq->ipsq_writer == NULL &&
13996 		new_ipsq->ipsq_current_ipif == NULL) ||
13997 	    (new_ipsq->ipsq_writer == curthread)) {
13998 		new_ipsq->ipsq_writer = curthread;
13999 		new_ipsq->ipsq_reentry_cnt++;
14000 		became_writer_on_new_sq = B_TRUE;
14001 	}
14002 
14003 	/*
14004 	 * We are holding ill_g_lock as writer and all the ill locks of
14005 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14006 	 * message can land up on the old ipsq even though we don't hold the
14007 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14008 	 */
14009 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14010 
14011 	/*
14012 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14013 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14014 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14015 	 */
14016 	ill_merge_ipsq(old_ipsq, new_ipsq);
14017 
14018 	/*
14019 	 * Mark the new ipsq as needing a split since it is currently
14020 	 * being shared by more than 1 IPMP group. The split will
14021 	 * occur at the end of ipsq_exit
14022 	 */
14023 	new_ipsq->ipsq_split = B_TRUE;
14024 
14025 	/* Now release all the locks */
14026 	mutex_exit(&new_ipsq->ipsq_lock);
14027 	ill_unlock_ills(ill_list, cnt);
14028 	rw_exit(&ill_g_lock);
14029 
14030 	kmem_free(ill_list, ill_list_size);
14031 
14032 	/*
14033 	 * If we succeeded in becoming writer on the new ipsq, then
14034 	 * drain the new ipsq and start processing  all enqueued messages
14035 	 * including the current ioctl we are processing which is either
14036 	 * a set groupname or failover/failback.
14037 	 */
14038 	if (became_writer_on_new_sq)
14039 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14040 
14041 	/*
14042 	 * syncq has been changed and all the messages have been moved.
14043 	 */
14044 	mutex_enter(&old_ipsq->ipsq_lock);
14045 	old_ipsq->ipsq_current_ipif = NULL;
14046 	mutex_exit(&old_ipsq->ipsq_lock);
14047 	return (EINPROGRESS);
14048 }
14049 
14050 /*
14051  * Delete and add the loopback copy and non-loopback copy of
14052  * the BROADCAST ire corresponding to ill and addr. Used to
14053  * group broadcast ires together when ill becomes part of
14054  * a group.
14055  *
14056  * This function is also called when ill is leaving the group
14057  * so that the ires belonging to the group gets re-grouped.
14058  */
14059 static void
14060 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14061 {
14062 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14063 	ire_t **ire_ptpn = &ire_head;
14064 
14065 	/*
14066 	 * The loopback and non-loopback IREs are inserted in the order in which
14067 	 * they're found, on the basis that they are correctly ordered (loopback
14068 	 * first).
14069 	 */
14070 	for (;;) {
14071 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14072 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14073 		if (ire == NULL)
14074 			break;
14075 
14076 		/*
14077 		 * we are passing in KM_SLEEP because it is not easy to
14078 		 * go back to a sane state in case of memory failure.
14079 		 */
14080 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14081 		ASSERT(nire != NULL);
14082 		bzero(nire, sizeof (ire_t));
14083 		/*
14084 		 * Don't use ire_max_frag directly since we don't
14085 		 * hold on to 'ire' until we add the new ire 'nire' and
14086 		 * we don't want the new ire to have a dangling reference
14087 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14088 		 * be in sync with the ipif_mtu of the associate ipif.
14089 		 * For eg. this happens as a result of SIOCSLIFNAME,
14090 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14091 		 * the driver. A change in ire_max_frag triggered as
14092 		 * as a result of path mtu discovery, or due to an
14093 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14094 		 * route change -mtu command does not apply to broadcast ires.
14095 		 *
14096 		 * XXX We need a recovery strategy here if ire_init fails
14097 		 */
14098 		if (ire_init(nire,
14099 		    (uchar_t *)&ire->ire_addr,
14100 		    (uchar_t *)&ire->ire_mask,
14101 		    (uchar_t *)&ire->ire_src_addr,
14102 		    (uchar_t *)&ire->ire_gateway_addr,
14103 		    (uchar_t *)&ire->ire_in_src_addr,
14104 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14105 			&ire->ire_ipif->ipif_mtu,
14106 		    ire->ire_fp_mp,
14107 		    ire->ire_rfq,
14108 		    ire->ire_stq,
14109 		    ire->ire_type,
14110 		    ire->ire_dlureq_mp,
14111 		    ire->ire_ipif,
14112 		    ire->ire_in_ill,
14113 		    ire->ire_cmask,
14114 		    ire->ire_phandle,
14115 		    ire->ire_ihandle,
14116 		    ire->ire_flags,
14117 		    &ire->ire_uinfo,
14118 		    NULL,
14119 		    NULL) == NULL) {
14120 			cmn_err(CE_PANIC, "ire_init() failed");
14121 		}
14122 		ire_delete(ire);
14123 		ire_refrele(ire);
14124 
14125 		/*
14126 		 * The newly created IREs are inserted at the tail of the list
14127 		 * starting with ire_head. As we've just allocated them no one
14128 		 * knows about them so it's safe.
14129 		 */
14130 		*ire_ptpn = nire;
14131 		ire_ptpn = &nire->ire_next;
14132 	}
14133 
14134 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14135 		int error;
14136 		ire_t *oire;
14137 		/* unlink the IRE from our list before calling ire_add() */
14138 		nire_next = nire->ire_next;
14139 		nire->ire_next = NULL;
14140 
14141 		/* ire_add adds the ire at the right place in the list */
14142 		oire = nire;
14143 		error = ire_add(&nire, NULL, NULL, NULL);
14144 		ASSERT(error == 0);
14145 		ASSERT(oire == nire);
14146 		ire_refrele(nire);	/* Held in ire_add */
14147 	}
14148 }
14149 
14150 /*
14151  * This function is usually called when an ill is inserted in
14152  * a group and all the ipifs are already UP. As all the ipifs
14153  * are already UP, the broadcast ires have already been created
14154  * and been inserted. But, ire_add_v4 would not have grouped properly.
14155  * We need to re-group for the benefit of ip_wput_ire which
14156  * expects BROADCAST ires to be grouped properly to avoid sending
14157  * more than one copy of the broadcast packet per group.
14158  *
14159  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14160  *	  because when ipif_up_done ends up calling this, ires have
14161  *        already been added before illgrp_insert i.e before ill_group
14162  *	  has been initialized.
14163  */
14164 static void
14165 ill_group_bcast_for_xmit(ill_t *ill)
14166 {
14167 	ill_group_t *illgrp;
14168 	ipif_t *ipif;
14169 	ipaddr_t addr;
14170 	ipaddr_t net_mask;
14171 	ipaddr_t subnet_netmask;
14172 
14173 	illgrp = ill->ill_group;
14174 
14175 	/*
14176 	 * This function is called even when an ill is deleted from
14177 	 * the group. Hence, illgrp could be null.
14178 	 */
14179 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14180 		return;
14181 
14182 	/*
14183 	 * Delete all the BROADCAST ires matching this ill and add
14184 	 * them back. This time, ire_add_v4 should take care of
14185 	 * grouping them with others because ill is part of the
14186 	 * group.
14187 	 */
14188 	ill_bcast_delete_and_add(ill, 0);
14189 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14190 
14191 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14192 
14193 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14194 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14195 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14196 		} else {
14197 			net_mask = htonl(IN_CLASSA_NET);
14198 		}
14199 		addr = net_mask & ipif->ipif_subnet;
14200 		ill_bcast_delete_and_add(ill, addr);
14201 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14202 
14203 		subnet_netmask = ipif->ipif_net_mask;
14204 		addr = ipif->ipif_subnet;
14205 		ill_bcast_delete_and_add(ill, addr);
14206 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14207 	}
14208 }
14209 
14210 /*
14211  * This function is called from illgrp_delete when ill is being deleted
14212  * from the group.
14213  *
14214  * As ill is not there in the group anymore, any address belonging
14215  * to this ill should be cleared of IRE_MARK_NORECV.
14216  */
14217 static void
14218 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14219 {
14220 	ire_t *ire;
14221 	irb_t *irb;
14222 
14223 	ASSERT(ill->ill_group == NULL);
14224 
14225 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14226 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14227 
14228 	if (ire != NULL) {
14229 		/*
14230 		 * IPMP and plumbing operations are serialized on the ipsq, so
14231 		 * no one will insert or delete a broadcast ire under our feet.
14232 		 */
14233 		irb = ire->ire_bucket;
14234 		rw_enter(&irb->irb_lock, RW_READER);
14235 		ire_refrele(ire);
14236 
14237 		for (; ire != NULL; ire = ire->ire_next) {
14238 			if (ire->ire_addr != addr)
14239 				break;
14240 			if (ire_to_ill(ire) != ill)
14241 				continue;
14242 
14243 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14244 			ire->ire_marks &= ~IRE_MARK_NORECV;
14245 		}
14246 		rw_exit(&irb->irb_lock);
14247 	}
14248 }
14249 
14250 /*
14251  * This function must be called only after the broadcast ires
14252  * have been grouped together. For a given address addr, nominate
14253  * only one of the ires whose interface is not FAILED or OFFLINE.
14254  *
14255  * This is also called when an ipif goes down, so that we can nominate
14256  * a different ire with the same address for receiving.
14257  */
14258 static void
14259 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14260 {
14261 	irb_t *irb;
14262 	ire_t *ire;
14263 	ire_t *ire1;
14264 	ire_t *save_ire;
14265 	ire_t **irep = NULL;
14266 	boolean_t first = B_TRUE;
14267 	ire_t *clear_ire = NULL;
14268 	ire_t *start_ire = NULL;
14269 	ire_t	*new_lb_ire;
14270 	ire_t	*new_nlb_ire;
14271 	boolean_t new_lb_ire_used = B_FALSE;
14272 	boolean_t new_nlb_ire_used = B_FALSE;
14273 	uint64_t match_flags;
14274 	uint64_t phyi_flags;
14275 	boolean_t fallback = B_FALSE;
14276 
14277 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14278 	    NULL, MATCH_IRE_TYPE);
14279 	/*
14280 	 * We may not be able to find some ires if a previous
14281 	 * ire_create failed. This happens when an ipif goes
14282 	 * down and we are unable to create BROADCAST ires due
14283 	 * to memory failure. Thus, we have to check for NULL
14284 	 * below. This should handle the case for LOOPBACK,
14285 	 * POINTOPOINT and interfaces with some POINTOPOINT
14286 	 * logicals for which there are no BROADCAST ires.
14287 	 */
14288 	if (ire == NULL)
14289 		return;
14290 	/*
14291 	 * Currently IRE_BROADCASTS are deleted when an ipif
14292 	 * goes down which runs exclusively. Thus, setting
14293 	 * IRE_MARK_RCVD should not race with ire_delete marking
14294 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14295 	 * be consistent with other parts of the code that walks
14296 	 * a given bucket.
14297 	 */
14298 	save_ire = ire;
14299 	irb = ire->ire_bucket;
14300 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14301 	if (new_lb_ire == NULL) {
14302 		ire_refrele(ire);
14303 		return;
14304 	}
14305 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14306 	if (new_nlb_ire == NULL) {
14307 		ire_refrele(ire);
14308 		kmem_cache_free(ire_cache, new_lb_ire);
14309 		return;
14310 	}
14311 	IRB_REFHOLD(irb);
14312 	rw_enter(&irb->irb_lock, RW_WRITER);
14313 	/*
14314 	 * Get to the first ire matching the address and the
14315 	 * group. If the address does not match we are done
14316 	 * as we could not find the IRE. If the address matches
14317 	 * we should get to the first one matching the group.
14318 	 */
14319 	while (ire != NULL) {
14320 		if (ire->ire_addr != addr ||
14321 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14322 			break;
14323 		}
14324 		ire = ire->ire_next;
14325 	}
14326 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14327 	start_ire = ire;
14328 redo:
14329 	while (ire != NULL && ire->ire_addr == addr &&
14330 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14331 		/*
14332 		 * The first ire for any address within a group
14333 		 * should always be the one with IRE_MARK_NORECV cleared
14334 		 * so that ip_wput_ire can avoid searching for one.
14335 		 * Note down the insertion point which will be used
14336 		 * later.
14337 		 */
14338 		if (first && (irep == NULL))
14339 			irep = ire->ire_ptpn;
14340 		/*
14341 		 * PHYI_FAILED is set when the interface fails.
14342 		 * This interface might have become good, but the
14343 		 * daemon has not yet detected. We should still
14344 		 * not receive on this. PHYI_OFFLINE should never
14345 		 * be picked as this has been offlined and soon
14346 		 * be removed.
14347 		 */
14348 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14349 		if (phyi_flags & PHYI_OFFLINE) {
14350 			ire->ire_marks |= IRE_MARK_NORECV;
14351 			ire = ire->ire_next;
14352 			continue;
14353 		}
14354 		if (phyi_flags & match_flags) {
14355 			ire->ire_marks |= IRE_MARK_NORECV;
14356 			ire = ire->ire_next;
14357 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14358 			    PHYI_INACTIVE) {
14359 				fallback = B_TRUE;
14360 			}
14361 			continue;
14362 		}
14363 		if (first) {
14364 			/*
14365 			 * We will move this to the front of the list later
14366 			 * on.
14367 			 */
14368 			clear_ire = ire;
14369 			ire->ire_marks &= ~IRE_MARK_NORECV;
14370 		} else {
14371 			ire->ire_marks |= IRE_MARK_NORECV;
14372 		}
14373 		first = B_FALSE;
14374 		ire = ire->ire_next;
14375 	}
14376 	/*
14377 	 * If we never nominated anybody, try nominating at least
14378 	 * an INACTIVE, if we found one. Do it only once though.
14379 	 */
14380 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14381 	    fallback) {
14382 		match_flags = PHYI_FAILED;
14383 		ire = start_ire;
14384 		irep = NULL;
14385 		goto redo;
14386 	}
14387 	ire_refrele(save_ire);
14388 
14389 	/*
14390 	 * irep non-NULL indicates that we entered the while loop
14391 	 * above. If clear_ire is at the insertion point, we don't
14392 	 * have to do anything. clear_ire will be NULL if all the
14393 	 * interfaces are failed.
14394 	 *
14395 	 * We cannot unlink and reinsert the ire at the right place
14396 	 * in the list since there can be other walkers of this bucket.
14397 	 * Instead we delete and recreate the ire
14398 	 */
14399 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14400 		ire_t *clear_ire_stq = NULL;
14401 		bzero(new_lb_ire, sizeof (ire_t));
14402 		/* XXX We need a recovery strategy here. */
14403 		if (ire_init(new_lb_ire,
14404 		    (uchar_t *)&clear_ire->ire_addr,
14405 		    (uchar_t *)&clear_ire->ire_mask,
14406 		    (uchar_t *)&clear_ire->ire_src_addr,
14407 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14408 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14409 		    &clear_ire->ire_max_frag,
14410 		    clear_ire->ire_fp_mp,
14411 		    clear_ire->ire_rfq,
14412 		    clear_ire->ire_stq,
14413 		    clear_ire->ire_type,
14414 		    clear_ire->ire_dlureq_mp,
14415 		    clear_ire->ire_ipif,
14416 		    clear_ire->ire_in_ill,
14417 		    clear_ire->ire_cmask,
14418 		    clear_ire->ire_phandle,
14419 		    clear_ire->ire_ihandle,
14420 		    clear_ire->ire_flags,
14421 		    &clear_ire->ire_uinfo,
14422 		    NULL,
14423 		    NULL) == NULL)
14424 			cmn_err(CE_PANIC, "ire_init() failed");
14425 		if (clear_ire->ire_stq == NULL) {
14426 			ire_t *ire_next = clear_ire->ire_next;
14427 			if (ire_next != NULL &&
14428 			    ire_next->ire_stq != NULL &&
14429 			    ire_next->ire_addr == clear_ire->ire_addr &&
14430 			    ire_next->ire_ipif->ipif_ill ==
14431 			    clear_ire->ire_ipif->ipif_ill) {
14432 				clear_ire_stq = ire_next;
14433 
14434 				bzero(new_nlb_ire, sizeof (ire_t));
14435 				/* XXX We need a recovery strategy here. */
14436 				if (ire_init(new_nlb_ire,
14437 				    (uchar_t *)&clear_ire_stq->ire_addr,
14438 				    (uchar_t *)&clear_ire_stq->ire_mask,
14439 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14440 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14441 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14442 				    &clear_ire_stq->ire_max_frag,
14443 				    clear_ire_stq->ire_fp_mp,
14444 				    clear_ire_stq->ire_rfq,
14445 				    clear_ire_stq->ire_stq,
14446 				    clear_ire_stq->ire_type,
14447 				    clear_ire_stq->ire_dlureq_mp,
14448 				    clear_ire_stq->ire_ipif,
14449 				    clear_ire_stq->ire_in_ill,
14450 				    clear_ire_stq->ire_cmask,
14451 				    clear_ire_stq->ire_phandle,
14452 				    clear_ire_stq->ire_ihandle,
14453 				    clear_ire_stq->ire_flags,
14454 				    &clear_ire_stq->ire_uinfo,
14455 				    NULL,
14456 				    NULL) == NULL)
14457 					cmn_err(CE_PANIC, "ire_init() failed");
14458 			}
14459 		}
14460 
14461 		/*
14462 		 * Delete the ire. We can't call ire_delete() since
14463 		 * we are holding the bucket lock. We can't release the
14464 		 * bucket lock since we can't allow irep to change. So just
14465 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14466 		 * ire from the list and do the refrele.
14467 		 */
14468 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14469 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14470 
14471 		if (clear_ire_stq != NULL) {
14472 			ire_fastpath_list_delete(
14473 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14474 			    clear_ire_stq);
14475 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14476 		}
14477 
14478 		/*
14479 		 * Also take care of otherfields like ib/ob pkt count
14480 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14481 		 */
14482 
14483 		/* Add the new ire's. Insert at *irep */
14484 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14485 		ire1 = *irep;
14486 		if (ire1 != NULL)
14487 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14488 		new_lb_ire->ire_next = ire1;
14489 		/* Link the new one in. */
14490 		new_lb_ire->ire_ptpn = irep;
14491 		membar_producer();
14492 		*irep = new_lb_ire;
14493 		new_lb_ire_used = B_TRUE;
14494 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14495 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14496 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14497 
14498 		if (clear_ire_stq != NULL) {
14499 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14500 			irep = &new_lb_ire->ire_next;
14501 			/* Add the new ire. Insert at *irep */
14502 			ire1 = *irep;
14503 			if (ire1 != NULL)
14504 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14505 			new_nlb_ire->ire_next = ire1;
14506 			/* Link the new one in. */
14507 			new_nlb_ire->ire_ptpn = irep;
14508 			membar_producer();
14509 			*irep = new_nlb_ire;
14510 			new_nlb_ire_used = B_TRUE;
14511 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14512 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14513 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14514 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14515 		}
14516 	}
14517 	rw_exit(&irb->irb_lock);
14518 	if (!new_lb_ire_used)
14519 		kmem_cache_free(ire_cache, new_lb_ire);
14520 	if (!new_nlb_ire_used)
14521 		kmem_cache_free(ire_cache, new_nlb_ire);
14522 	IRB_REFRELE(irb);
14523 }
14524 
14525 /*
14526  * Whenever an ipif goes down we have to renominate a different
14527  * broadcast ire to receive. Whenever an ipif comes up, we need
14528  * to make sure that we have only one nominated to receive.
14529  */
14530 static void
14531 ipif_renominate_bcast(ipif_t *ipif)
14532 {
14533 	ill_t *ill = ipif->ipif_ill;
14534 	ipaddr_t subnet_addr;
14535 	ipaddr_t net_addr;
14536 	ipaddr_t net_mask = 0;
14537 	ipaddr_t subnet_netmask;
14538 	ipaddr_t addr;
14539 	ill_group_t *illgrp;
14540 
14541 	illgrp = ill->ill_group;
14542 	/*
14543 	 * If this is the last ipif going down, it might take
14544 	 * the ill out of the group. In that case ipif_down ->
14545 	 * illgrp_delete takes care of doing the nomination.
14546 	 * ipif_down does not call for this case.
14547 	 */
14548 	ASSERT(illgrp != NULL);
14549 
14550 	/* There could not have been any ires associated with this */
14551 	if (ipif->ipif_subnet == 0)
14552 		return;
14553 
14554 	ill_mark_bcast(illgrp, 0);
14555 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14556 
14557 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14558 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14559 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14560 	} else {
14561 		net_mask = htonl(IN_CLASSA_NET);
14562 	}
14563 	addr = net_mask & ipif->ipif_subnet;
14564 	ill_mark_bcast(illgrp, addr);
14565 
14566 	net_addr = ~net_mask | addr;
14567 	ill_mark_bcast(illgrp, net_addr);
14568 
14569 	subnet_netmask = ipif->ipif_net_mask;
14570 	addr = ipif->ipif_subnet;
14571 	ill_mark_bcast(illgrp, addr);
14572 
14573 	subnet_addr = ~subnet_netmask | addr;
14574 	ill_mark_bcast(illgrp, subnet_addr);
14575 }
14576 
14577 /*
14578  * Whenever we form or delete ill groups, we need to nominate one set of
14579  * BROADCAST ires for receiving in the group.
14580  *
14581  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14582  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14583  *    for ill_ipif_up_count to be non-zero. This is the only case where
14584  *    ill_ipif_up_count is zero and we would still find the ires.
14585  *
14586  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14587  *    ipif is UP and we just have to do the nomination.
14588  *
14589  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14590  *    from the group. So, we have to do the nomination.
14591  *
14592  * Because of (3), there could be just one ill in the group. But we have
14593  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14594  * Thus, this function does not optimize when there is only one ill as
14595  * it is not correct for (3).
14596  */
14597 static void
14598 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14599 {
14600 	ill_t *ill;
14601 	ipif_t *ipif;
14602 	ipaddr_t subnet_addr;
14603 	ipaddr_t prev_subnet_addr = 0;
14604 	ipaddr_t net_addr;
14605 	ipaddr_t prev_net_addr = 0;
14606 	ipaddr_t net_mask = 0;
14607 	ipaddr_t subnet_netmask;
14608 	ipaddr_t addr;
14609 
14610 	/*
14611 	 * When the last memeber is leaving, there is nothing to
14612 	 * nominate.
14613 	 */
14614 	if (illgrp->illgrp_ill_count == 0) {
14615 		ASSERT(illgrp->illgrp_ill == NULL);
14616 		return;
14617 	}
14618 
14619 	ill = illgrp->illgrp_ill;
14620 	ASSERT(!ill->ill_isv6);
14621 	/*
14622 	 * We assume that ires with same address and belonging to the
14623 	 * same group, has been grouped together. Nominating a *single*
14624 	 * ill in the group for sending and receiving broadcast is done
14625 	 * by making sure that the first BROADCAST ire (which will be
14626 	 * the one returned by ire_ctable_lookup for ip_rput and the
14627 	 * one that will be used in ip_wput_ire) will be the one that
14628 	 * will not have IRE_MARK_NORECV set.
14629 	 *
14630 	 * 1) ip_rput checks and discards packets received on ires marked
14631 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14632 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14633 	 *    first ire in the group for every broadcast address in the group.
14634 	 *    ip_rput will accept packets only on the first ire i.e only
14635 	 *    one copy of the ill.
14636 	 *
14637 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14638 	 *    packet for the whole group. It needs to send out on the ill
14639 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14640 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14641 	 *    the copy echoed back on other port where the ire is not marked
14642 	 *    with IRE_MARK_NORECV.
14643 	 *
14644 	 * Note that we just need to have the first IRE either loopback or
14645 	 * non-loopback (either of them may not exist if ire_create failed
14646 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14647 	 * always hit the first one and hence will always accept one copy.
14648 	 *
14649 	 * We have a broadcast ire per ill for all the unique prefixes
14650 	 * hosted on that ill. As we don't have a way of knowing the
14651 	 * unique prefixes on a given ill and hence in the whole group,
14652 	 * we just call ill_mark_bcast on all the prefixes that exist
14653 	 * in the group. For the common case of one prefix, the code
14654 	 * below optimizes by remebering the last address used for
14655 	 * markng. In the case of multiple prefixes, this will still
14656 	 * optimize depending the order of prefixes.
14657 	 *
14658 	 * The only unique address across the whole group is 0.0.0.0 and
14659 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14660 	 * the first ire in the bucket for receiving and disables the
14661 	 * others.
14662 	 */
14663 	ill_mark_bcast(illgrp, 0);
14664 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14665 	for (; ill != NULL; ill = ill->ill_group_next) {
14666 
14667 		for (ipif = ill->ill_ipif; ipif != NULL;
14668 		    ipif = ipif->ipif_next) {
14669 
14670 			if (!(ipif->ipif_flags & IPIF_UP) ||
14671 			    ipif->ipif_subnet == 0) {
14672 				continue;
14673 			}
14674 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14675 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14676 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14677 			} else {
14678 				net_mask = htonl(IN_CLASSA_NET);
14679 			}
14680 			addr = net_mask & ipif->ipif_subnet;
14681 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14682 				ill_mark_bcast(illgrp, addr);
14683 				net_addr = ~net_mask | addr;
14684 				ill_mark_bcast(illgrp, net_addr);
14685 			}
14686 			prev_net_addr = addr;
14687 
14688 			subnet_netmask = ipif->ipif_net_mask;
14689 			addr = ipif->ipif_subnet;
14690 			if (prev_subnet_addr == 0 ||
14691 			    prev_subnet_addr != addr) {
14692 				ill_mark_bcast(illgrp, addr);
14693 				subnet_addr = ~subnet_netmask | addr;
14694 				ill_mark_bcast(illgrp, subnet_addr);
14695 			}
14696 			prev_subnet_addr = addr;
14697 		}
14698 	}
14699 }
14700 
14701 /*
14702  * This function is called while forming ill groups.
14703  *
14704  * Currently, we handle only allmulti groups. We want to join
14705  * allmulti on only one of the ills in the groups. In future,
14706  * when we have link aggregation, we may have to join normal
14707  * multicast groups on multiple ills as switch does inbound load
14708  * balancing. Following are the functions that calls this
14709  * function :
14710  *
14711  * 1) ill_recover_multicast : Interface is coming back UP.
14712  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14713  *    will call ill_recover_multicast to recover all the multicast
14714  *    groups. We need to make sure that only one member is joined
14715  *    in the ill group.
14716  *
14717  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14718  *    Somebody is joining allmulti. We need to make sure that only one
14719  *    member is joined in the group.
14720  *
14721  * 3) illgrp_insert : If allmulti has already joined, we need to make
14722  *    sure that only one member is joined in the group.
14723  *
14724  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14725  *    allmulti who we have nominated. We need to pick someother ill.
14726  *
14727  * 5) illgrp_delete : The ill we nominated is leaving the group,
14728  *    we need to pick a new ill to join the group.
14729  *
14730  * For (1), (2), (5) - we just have to check whether there is
14731  * a good ill joined in the group. If we could not find any ills
14732  * joined the group, we should join.
14733  *
14734  * For (4), the one that was nominated to receive, left the group.
14735  * There could be nobody joined in the group when this function is
14736  * called.
14737  *
14738  * For (3) - we need to explicitly check whether there are multiple
14739  * ills joined in the group.
14740  *
14741  * For simplicity, we don't differentiate any of the above cases. We
14742  * just leave the group if it is joined on any of them and join on
14743  * the first good ill.
14744  */
14745 int
14746 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14747 {
14748 	ilm_t *ilm;
14749 	ill_t *ill;
14750 	ill_t *fallback_inactive_ill = NULL;
14751 	ill_t *fallback_failed_ill = NULL;
14752 	int ret = 0;
14753 
14754 	/*
14755 	 * Leave the allmulti on all the ills and start fresh.
14756 	 */
14757 	for (ill = illgrp->illgrp_ill; ill != NULL;
14758 	    ill = ill->ill_group_next) {
14759 		if (ill->ill_join_allmulti)
14760 			(void) ip_leave_allmulti(ill->ill_ipif);
14761 	}
14762 
14763 	/*
14764 	 * Choose a good ill. Fallback to inactive or failed if
14765 	 * none available. We need to fallback to FAILED in the
14766 	 * case where we have 2 interfaces in a group - where
14767 	 * one of them is failed and another is a good one and
14768 	 * the good one (not marked inactive) is leaving the group.
14769 	 */
14770 	ret = 0;
14771 	for (ill = illgrp->illgrp_ill; ill != NULL;
14772 	    ill = ill->ill_group_next) {
14773 		/* Never pick an offline interface */
14774 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14775 			continue;
14776 
14777 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14778 			fallback_failed_ill = ill;
14779 			continue;
14780 		}
14781 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14782 			fallback_inactive_ill = ill;
14783 			continue;
14784 		}
14785 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14786 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14787 				ret = ip_join_allmulti(ill->ill_ipif);
14788 				/*
14789 				 * ip_join_allmulti can fail because of memory
14790 				 * failures. So, make sure we join at least
14791 				 * on one ill.
14792 				 */
14793 				if (ill->ill_join_allmulti)
14794 					return (0);
14795 			}
14796 		}
14797 	}
14798 	if (ret != 0) {
14799 		/*
14800 		 * If we tried nominating above and failed to do so,
14801 		 * return error. We might have tried multiple times.
14802 		 * But, return the latest error.
14803 		 */
14804 		return (ret);
14805 	}
14806 	if ((ill = fallback_inactive_ill) != NULL) {
14807 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14808 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14809 				ret = ip_join_allmulti(ill->ill_ipif);
14810 				return (ret);
14811 			}
14812 		}
14813 	} else if ((ill = fallback_failed_ill) != NULL) {
14814 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14815 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14816 				ret = ip_join_allmulti(ill->ill_ipif);
14817 				return (ret);
14818 			}
14819 		}
14820 	}
14821 	return (0);
14822 }
14823 
14824 /*
14825  * This function is called from illgrp_delete after it is
14826  * deleted from the group to reschedule responsibilities
14827  * to a different ill.
14828  */
14829 static void
14830 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14831 {
14832 	ilm_t	*ilm;
14833 	ipif_t	*ipif;
14834 	ipaddr_t subnet_addr;
14835 	ipaddr_t net_addr;
14836 	ipaddr_t net_mask = 0;
14837 	ipaddr_t subnet_netmask;
14838 	ipaddr_t addr;
14839 
14840 	ASSERT(ill->ill_group == NULL);
14841 	/*
14842 	 * Broadcast Responsibility:
14843 	 *
14844 	 * 1. If this ill has been nominated for receiving broadcast
14845 	 * packets, we need to find a new one. Before we find a new
14846 	 * one, we need to re-group the ires that are part of this new
14847 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14848 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14849 	 * thing for us.
14850 	 *
14851 	 * 2. If this ill was not nominated for receiving broadcast
14852 	 * packets, we need to clear the IRE_MARK_NORECV flag
14853 	 * so that we continue to send up broadcast packets.
14854 	 */
14855 	if (!ill->ill_isv6) {
14856 		/*
14857 		 * Case 1 above : No optimization here. Just redo the
14858 		 * nomination.
14859 		 */
14860 		ill_group_bcast_for_xmit(ill);
14861 		ill_nominate_bcast_rcv(illgrp);
14862 
14863 		/*
14864 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14865 		 */
14866 		ill_clear_bcast_mark(ill, 0);
14867 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14868 
14869 		for (ipif = ill->ill_ipif; ipif != NULL;
14870 		    ipif = ipif->ipif_next) {
14871 
14872 			if (!(ipif->ipif_flags & IPIF_UP) ||
14873 			    ipif->ipif_subnet == 0) {
14874 				continue;
14875 			}
14876 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14877 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14878 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14879 			} else {
14880 				net_mask = htonl(IN_CLASSA_NET);
14881 			}
14882 			addr = net_mask & ipif->ipif_subnet;
14883 			ill_clear_bcast_mark(ill, addr);
14884 
14885 			net_addr = ~net_mask | addr;
14886 			ill_clear_bcast_mark(ill, net_addr);
14887 
14888 			subnet_netmask = ipif->ipif_net_mask;
14889 			addr = ipif->ipif_subnet;
14890 			ill_clear_bcast_mark(ill, addr);
14891 
14892 			subnet_addr = ~subnet_netmask | addr;
14893 			ill_clear_bcast_mark(ill, subnet_addr);
14894 		}
14895 	}
14896 
14897 	/*
14898 	 * Multicast Responsibility.
14899 	 *
14900 	 * If we have joined allmulti on this one, find a new member
14901 	 * in the group to join allmulti. As this ill is already part
14902 	 * of allmulti, we don't have to join on this one.
14903 	 *
14904 	 * If we have not joined allmulti on this one, there is no
14905 	 * responsibility to handoff. But we need to take new
14906 	 * responsibility i.e, join allmulti on this one if we need
14907 	 * to.
14908 	 */
14909 	if (ill->ill_join_allmulti) {
14910 		(void) ill_nominate_mcast_rcv(illgrp);
14911 	} else {
14912 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14913 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14914 				(void) ip_join_allmulti(ill->ill_ipif);
14915 				break;
14916 			}
14917 		}
14918 	}
14919 
14920 	/*
14921 	 * We intentionally do the flushing of IRE_CACHES only matching
14922 	 * on the ill and not on groups. Note that we are already deleted
14923 	 * from the group.
14924 	 *
14925 	 * This will make sure that all IRE_CACHES whose stq is pointing
14926 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14927 	 * deleted and IRE_CACHES that are not pointing at this ill will
14928 	 * be left alone.
14929 	 */
14930 	if (ill->ill_isv6) {
14931 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14932 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14933 	} else {
14934 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14935 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14936 	}
14937 
14938 	/*
14939 	 * Some conn may have cached one of the IREs deleted above. By removing
14940 	 * the ire reference, we clean up the extra reference to the ill held in
14941 	 * ire->ire_stq.
14942 	 */
14943 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14944 
14945 	/*
14946 	 * Re-do source address selection for all the members in the
14947 	 * group, if they borrowed source address from one of the ipifs
14948 	 * in this ill.
14949 	 */
14950 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14951 		if (ill->ill_isv6) {
14952 			ipif_update_other_ipifs_v6(ipif, illgrp);
14953 		} else {
14954 			ipif_update_other_ipifs(ipif, illgrp);
14955 		}
14956 	}
14957 }
14958 
14959 /*
14960  * Delete the ill from the group. The caller makes sure that it is
14961  * in a group and it okay to delete from the group. So, we always
14962  * delete here.
14963  */
14964 static void
14965 illgrp_delete(ill_t *ill)
14966 {
14967 	ill_group_t *illgrp;
14968 	ill_group_t *tmpg;
14969 	ill_t *tmp_ill;
14970 
14971 	/*
14972 	 * Reset illgrp_ill_schednext if it was pointing at us.
14973 	 * We need to do this before we set ill_group to NULL.
14974 	 */
14975 	rw_enter(&ill_g_lock, RW_WRITER);
14976 	mutex_enter(&ill->ill_lock);
14977 
14978 	illgrp_reset_schednext(ill);
14979 
14980 	illgrp = ill->ill_group;
14981 
14982 	/* Delete the ill from illgrp. */
14983 	if (illgrp->illgrp_ill == ill) {
14984 		illgrp->illgrp_ill = ill->ill_group_next;
14985 	} else {
14986 		tmp_ill = illgrp->illgrp_ill;
14987 		while (tmp_ill->ill_group_next != ill) {
14988 			tmp_ill = tmp_ill->ill_group_next;
14989 			ASSERT(tmp_ill != NULL);
14990 		}
14991 		tmp_ill->ill_group_next = ill->ill_group_next;
14992 	}
14993 	ill->ill_group = NULL;
14994 	ill->ill_group_next = NULL;
14995 
14996 	illgrp->illgrp_ill_count--;
14997 	mutex_exit(&ill->ill_lock);
14998 	rw_exit(&ill_g_lock);
14999 
15000 	/*
15001 	 * As this ill is leaving the group, we need to hand off
15002 	 * the responsibilities to the other ills in the group, if
15003 	 * this ill had some responsibilities.
15004 	 */
15005 
15006 	ill_handoff_responsibility(ill, illgrp);
15007 
15008 	rw_enter(&ill_g_lock, RW_WRITER);
15009 
15010 	if (illgrp->illgrp_ill_count == 0) {
15011 
15012 		ASSERT(illgrp->illgrp_ill == NULL);
15013 		if (ill->ill_isv6) {
15014 			if (illgrp == illgrp_head_v6) {
15015 				illgrp_head_v6 = illgrp->illgrp_next;
15016 			} else {
15017 				tmpg = illgrp_head_v6;
15018 				while (tmpg->illgrp_next != illgrp) {
15019 					tmpg = tmpg->illgrp_next;
15020 					ASSERT(tmpg != NULL);
15021 				}
15022 				tmpg->illgrp_next = illgrp->illgrp_next;
15023 			}
15024 		} else {
15025 			if (illgrp == illgrp_head_v4) {
15026 				illgrp_head_v4 = illgrp->illgrp_next;
15027 			} else {
15028 				tmpg = illgrp_head_v4;
15029 				while (tmpg->illgrp_next != illgrp) {
15030 					tmpg = tmpg->illgrp_next;
15031 					ASSERT(tmpg != NULL);
15032 				}
15033 				tmpg->illgrp_next = illgrp->illgrp_next;
15034 			}
15035 		}
15036 		mutex_destroy(&illgrp->illgrp_lock);
15037 		mi_free(illgrp);
15038 	}
15039 	rw_exit(&ill_g_lock);
15040 
15041 	/*
15042 	 * Even though the ill is out of the group its not necessary
15043 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15044 	 * We will split the ipsq when phyint_groupname is set to NULL.
15045 	 */
15046 
15047 	/*
15048 	 * Send a routing sockets message if we are deleting from
15049 	 * groups with names.
15050 	 */
15051 	if (ill->ill_phyint->phyint_groupname_len != 0)
15052 		ip_rts_ifmsg(ill->ill_ipif);
15053 }
15054 
15055 /*
15056  * Re-do source address selection. This is normally called when
15057  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15058  * ipif comes up.
15059  */
15060 void
15061 ill_update_source_selection(ill_t *ill)
15062 {
15063 	ipif_t *ipif;
15064 
15065 	ASSERT(IAM_WRITER_ILL(ill));
15066 
15067 	if (ill->ill_group != NULL)
15068 		ill = ill->ill_group->illgrp_ill;
15069 
15070 	for (; ill != NULL; ill = ill->ill_group_next) {
15071 		for (ipif = ill->ill_ipif; ipif != NULL;
15072 		    ipif = ipif->ipif_next) {
15073 			if (ill->ill_isv6)
15074 				ipif_recreate_interface_routes_v6(NULL, ipif);
15075 			else
15076 				ipif_recreate_interface_routes(NULL, ipif);
15077 		}
15078 	}
15079 }
15080 
15081 /*
15082  * Insert ill in a group headed by illgrp_head. The caller can either
15083  * pass a groupname in which case we search for a group with the
15084  * same name to insert in or pass a group to insert in. This function
15085  * would only search groups with names.
15086  *
15087  * NOTE : The caller should make sure that there is at least one ipif
15088  *	  UP on this ill so that illgrp_scheduler can pick this ill
15089  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15090  *	  already sent a DL_UNBIND to the driver and we don't want to
15091  *	  send anymore packets. We don't assert for ipif_up_count
15092  *	  to be greater than zero, because ipif_up_done wants to call
15093  *	  this function before bumping up the ipif_up_count. See
15094  *	  ipif_up_done() for details.
15095  */
15096 int
15097 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15098     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15099 {
15100 	ill_group_t *illgrp;
15101 	ill_t *prev_ill;
15102 	phyint_t *phyi;
15103 
15104 	ASSERT(ill->ill_group == NULL);
15105 
15106 	rw_enter(&ill_g_lock, RW_WRITER);
15107 	mutex_enter(&ill->ill_lock);
15108 
15109 	if (groupname != NULL) {
15110 		/*
15111 		 * Look for a group with a matching groupname to insert.
15112 		 */
15113 		for (illgrp = *illgrp_head; illgrp != NULL;
15114 		    illgrp = illgrp->illgrp_next) {
15115 
15116 			ill_t *tmp_ill;
15117 
15118 			/*
15119 			 * If we have an ill_group_t in the list which has
15120 			 * no ill_t assigned then we must be in the process of
15121 			 * removing this group. We skip this as illgrp_delete()
15122 			 * will remove it from the list.
15123 			 */
15124 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15125 				ASSERT(illgrp->illgrp_ill_count == 0);
15126 				continue;
15127 			}
15128 
15129 			ASSERT(tmp_ill->ill_phyint != NULL);
15130 			phyi = tmp_ill->ill_phyint;
15131 			/*
15132 			 * Look at groups which has names only.
15133 			 */
15134 			if (phyi->phyint_groupname_len == 0)
15135 				continue;
15136 			/*
15137 			 * Names are stored in the phyint common to both
15138 			 * IPv4 and IPv6.
15139 			 */
15140 			if (mi_strcmp(phyi->phyint_groupname,
15141 			    groupname) == 0) {
15142 				break;
15143 			}
15144 		}
15145 	} else {
15146 		/*
15147 		 * If the caller passes in a NULL "grp_to_insert", we
15148 		 * allocate one below and insert this singleton.
15149 		 */
15150 		illgrp = grp_to_insert;
15151 	}
15152 
15153 	ill->ill_group_next = NULL;
15154 
15155 	if (illgrp == NULL) {
15156 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15157 		if (illgrp == NULL) {
15158 			return (ENOMEM);
15159 		}
15160 		illgrp->illgrp_next = *illgrp_head;
15161 		*illgrp_head = illgrp;
15162 		illgrp->illgrp_ill = ill;
15163 		illgrp->illgrp_ill_count = 1;
15164 		ill->ill_group = illgrp;
15165 		/*
15166 		 * Used in illgrp_scheduler to protect multiple threads
15167 		 * from traversing the list.
15168 		 */
15169 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15170 	} else {
15171 		ASSERT(ill->ill_net_type ==
15172 		    illgrp->illgrp_ill->ill_net_type);
15173 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15174 
15175 		/* Insert ill at tail of this group */
15176 		prev_ill = illgrp->illgrp_ill;
15177 		while (prev_ill->ill_group_next != NULL)
15178 			prev_ill = prev_ill->ill_group_next;
15179 		prev_ill->ill_group_next = ill;
15180 		ill->ill_group = illgrp;
15181 		illgrp->illgrp_ill_count++;
15182 		/*
15183 		 * Inherit group properties. Currently only forwarding
15184 		 * is the property we try to keep the same with all the
15185 		 * ills. When there are more, we will abstract this into
15186 		 * a function.
15187 		 */
15188 		ill->ill_flags &= ~ILLF_ROUTER;
15189 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15190 	}
15191 	mutex_exit(&ill->ill_lock);
15192 	rw_exit(&ill_g_lock);
15193 
15194 	/*
15195 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15196 	 *    may be zero as it has not yet been bumped. But the ires
15197 	 *    have already been added. So, we do the nomination here
15198 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15199 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15200 	 *    ill_ipif_up_count here while nominating broadcast ires for
15201 	 *    receive.
15202 	 *
15203 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15204 	 *    to group them properly as ire_add() has already happened
15205 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15206 	 *    case, we need to do it here anyway.
15207 	 */
15208 	if (!ill->ill_isv6) {
15209 		ill_group_bcast_for_xmit(ill);
15210 		ill_nominate_bcast_rcv(illgrp);
15211 	}
15212 
15213 	if (!ipif_is_coming_up) {
15214 		/*
15215 		 * When ipif_up_done() calls this function, the multicast
15216 		 * groups have not been joined yet. So, there is no point in
15217 		 * nomination. ip_join_allmulti will handle groups when
15218 		 * ill_recover_multicast is called from ipif_up_done() later.
15219 		 */
15220 		(void) ill_nominate_mcast_rcv(illgrp);
15221 		/*
15222 		 * ipif_up_done calls ill_update_source_selection
15223 		 * anyway. Moreover, we don't want to re-create
15224 		 * interface routes while ipif_up_done() still has reference
15225 		 * to them. Refer to ipif_up_done() for more details.
15226 		 */
15227 		ill_update_source_selection(ill);
15228 	}
15229 
15230 	/*
15231 	 * Send a routing sockets message if we are inserting into
15232 	 * groups with names.
15233 	 */
15234 	if (groupname != NULL)
15235 		ip_rts_ifmsg(ill->ill_ipif);
15236 	return (0);
15237 }
15238 
15239 /*
15240  * Return the first phyint matching the groupname. There could
15241  * be more than one when there are ill groups.
15242  *
15243  * Needs work: called only from ip_sioctl_groupname
15244  */
15245 static phyint_t *
15246 phyint_lookup_group(char *groupname)
15247 {
15248 	phyint_t *phyi;
15249 
15250 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15251 	/*
15252 	 * Group names are stored in the phyint - a common structure
15253 	 * to both IPv4 and IPv6.
15254 	 */
15255 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15256 	for (; phyi != NULL;
15257 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15258 	    phyi, AVL_AFTER)) {
15259 		if (phyi->phyint_groupname_len == 0)
15260 			continue;
15261 		ASSERT(phyi->phyint_groupname != NULL);
15262 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15263 			return (phyi);
15264 	}
15265 	return (NULL);
15266 }
15267 
15268 
15269 
15270 /*
15271  * MT notes on creation and deletion of IPMP groups
15272  *
15273  * Creation and deletion of IPMP groups introduce the need to merge or
15274  * split the associated serialization objects i.e the ipsq's. Normally all
15275  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15276  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15277  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15278  * is a need to change the <ill-ipsq> association and we have to operate on both
15279  * the source and destination IPMP groups. For eg. attempting to set the
15280  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15281  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15282  * source or destination IPMP group are mapped to a single ipsq for executing
15283  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15284  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15285  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15286  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15287  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15288  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15289  *
15290  * In the above example the ioctl handling code locates the current ipsq of hme0
15291  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15292  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15293  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15294  * the destination ipsq. If the destination ipsq is not busy, it also enters
15295  * the destination ipsq exclusively. Now the actual groupname setting operation
15296  * can proceed. If the destination ipsq is busy, the operation is enqueued
15297  * on the destination (merged) ipsq and will be handled in the unwind from
15298  * ipsq_exit.
15299  *
15300  * To prevent other threads accessing the ill while the group name change is
15301  * in progres, we bring down the ipifs which also removes the ill from the
15302  * group. The group is changed in phyint and when the first ipif on the ill
15303  * is brought up, the ill is inserted into the right IPMP group by
15304  * illgrp_insert.
15305  */
15306 /* ARGSUSED */
15307 int
15308 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15309     ip_ioctl_cmd_t *ipip, void *ifreq)
15310 {
15311 	int i;
15312 	char *tmp;
15313 	int namelen;
15314 	ill_t *ill = ipif->ipif_ill;
15315 	ill_t *ill_v4, *ill_v6;
15316 	int err = 0;
15317 	phyint_t *phyi;
15318 	phyint_t *phyi_tmp;
15319 	struct lifreq *lifr;
15320 	mblk_t	*mp1;
15321 	char *groupname;
15322 	ipsq_t *ipsq;
15323 
15324 	ASSERT(IAM_WRITER_IPIF(ipif));
15325 
15326 	/* Existance verified in ip_wput_nondata */
15327 	mp1 = mp->b_cont->b_cont;
15328 	lifr = (struct lifreq *)mp1->b_rptr;
15329 	groupname = lifr->lifr_groupname;
15330 
15331 	if (ipif->ipif_id != 0)
15332 		return (EINVAL);
15333 
15334 	phyi = ill->ill_phyint;
15335 	ASSERT(phyi != NULL);
15336 
15337 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15338 		return (EINVAL);
15339 
15340 	tmp = groupname;
15341 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15342 		;
15343 
15344 	if (i == LIFNAMSIZ) {
15345 		/* no null termination */
15346 		return (EINVAL);
15347 	}
15348 
15349 	/*
15350 	 * Calculate the namelen exclusive of the null
15351 	 * termination character.
15352 	 */
15353 	namelen = tmp - groupname;
15354 
15355 	ill_v4 = phyi->phyint_illv4;
15356 	ill_v6 = phyi->phyint_illv6;
15357 
15358 	/*
15359 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15360 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15361 	 * synchronization notes in ip.c
15362 	 */
15363 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15364 		return (EINVAL);
15365 	}
15366 
15367 	/*
15368 	 * mark the ill as changing.
15369 	 * this should queue all new requests on the syncq.
15370 	 */
15371 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15372 
15373 	if (ill_v4 != NULL)
15374 		ill_v4->ill_state_flags |= ILL_CHANGING;
15375 	if (ill_v6 != NULL)
15376 		ill_v6->ill_state_flags |= ILL_CHANGING;
15377 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15378 
15379 	if (namelen == 0) {
15380 		/*
15381 		 * Null string means remove this interface from the
15382 		 * existing group.
15383 		 */
15384 		if (phyi->phyint_groupname_len == 0) {
15385 			/*
15386 			 * Never was in a group.
15387 			 */
15388 			err = 0;
15389 			goto done;
15390 		}
15391 
15392 		/*
15393 		 * IPv4 or IPv6 may be temporarily out of the group when all
15394 		 * the ipifs are down. Thus, we need to check for ill_group to
15395 		 * be non-NULL.
15396 		 */
15397 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15398 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15399 			mutex_enter(&ill_v4->ill_lock);
15400 			if (!ill_is_quiescent(ill_v4)) {
15401 				/*
15402 				 * ipsq_pending_mp_add will not fail since
15403 				 * connp is NULL
15404 				 */
15405 				(void) ipsq_pending_mp_add(NULL,
15406 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15407 				mutex_exit(&ill_v4->ill_lock);
15408 				err = EINPROGRESS;
15409 				goto done;
15410 			}
15411 			mutex_exit(&ill_v4->ill_lock);
15412 		}
15413 
15414 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15415 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15416 			mutex_enter(&ill_v6->ill_lock);
15417 			if (!ill_is_quiescent(ill_v6)) {
15418 				(void) ipsq_pending_mp_add(NULL,
15419 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15420 				mutex_exit(&ill_v6->ill_lock);
15421 				err = EINPROGRESS;
15422 				goto done;
15423 			}
15424 			mutex_exit(&ill_v6->ill_lock);
15425 		}
15426 
15427 		rw_enter(&ill_g_lock, RW_WRITER);
15428 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15429 		mutex_enter(&phyi->phyint_lock);
15430 		ASSERT(phyi->phyint_groupname != NULL);
15431 		mi_free(phyi->phyint_groupname);
15432 		phyi->phyint_groupname = NULL;
15433 		phyi->phyint_groupname_len = 0;
15434 		mutex_exit(&phyi->phyint_lock);
15435 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15436 		rw_exit(&ill_g_lock);
15437 		err = ill_up_ipifs(ill, q, mp);
15438 
15439 		/*
15440 		 * set the split flag so that the ipsq can be split
15441 		 */
15442 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15443 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15444 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15445 
15446 	} else {
15447 		if (phyi->phyint_groupname_len != 0) {
15448 			ASSERT(phyi->phyint_groupname != NULL);
15449 			/* Are we inserting in the same group ? */
15450 			if (mi_strcmp(groupname,
15451 			    phyi->phyint_groupname) == 0) {
15452 				err = 0;
15453 				goto done;
15454 			}
15455 		}
15456 
15457 		rw_enter(&ill_g_lock, RW_READER);
15458 		/*
15459 		 * Merge ipsq for the group's.
15460 		 * This check is here as multiple groups/ills might be
15461 		 * sharing the same ipsq.
15462 		 * If we have to merege than the operation is restarted
15463 		 * on the new ipsq.
15464 		 */
15465 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15466 		if (phyi->phyint_ipsq != ipsq) {
15467 			rw_exit(&ill_g_lock);
15468 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15469 			goto done;
15470 		}
15471 		/*
15472 		 * Running exclusive on new ipsq.
15473 		 */
15474 
15475 		ASSERT(ipsq != NULL);
15476 		ASSERT(ipsq->ipsq_writer == curthread);
15477 
15478 		/*
15479 		 * Check whether the ill_type and ill_net_type matches before
15480 		 * we allocate any memory so that the cleanup is easier.
15481 		 *
15482 		 * We can't group dissimilar ones as we can't load spread
15483 		 * packets across the group because of potential link-level
15484 		 * header differences.
15485 		 */
15486 		phyi_tmp = phyint_lookup_group(groupname);
15487 		if (phyi_tmp != NULL) {
15488 			if ((ill_v4 != NULL &&
15489 			    phyi_tmp->phyint_illv4 != NULL) &&
15490 			    ((ill_v4->ill_net_type !=
15491 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15492 			    (ill_v4->ill_type !=
15493 			    phyi_tmp->phyint_illv4->ill_type))) {
15494 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15495 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15496 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15497 				rw_exit(&ill_g_lock);
15498 				return (EINVAL);
15499 			}
15500 			if ((ill_v6 != NULL &&
15501 			    phyi_tmp->phyint_illv6 != NULL) &&
15502 			    ((ill_v6->ill_net_type !=
15503 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15504 			    (ill_v6->ill_type !=
15505 			    phyi_tmp->phyint_illv6->ill_type))) {
15506 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15507 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15508 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15509 				rw_exit(&ill_g_lock);
15510 				return (EINVAL);
15511 			}
15512 		}
15513 
15514 		rw_exit(&ill_g_lock);
15515 
15516 		/*
15517 		 * bring down all v4 ipifs.
15518 		 */
15519 		if (ill_v4 != NULL) {
15520 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15521 		}
15522 
15523 		/*
15524 		 * bring down all v6 ipifs.
15525 		 */
15526 		if (ill_v6 != NULL) {
15527 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15528 		}
15529 
15530 		/*
15531 		 * make sure all ipifs are down and there are no active
15532 		 * references. Call to ipsq_pending_mp_add will not fail
15533 		 * since connp is NULL.
15534 		 */
15535 		if (ill_v4 != NULL) {
15536 			mutex_enter(&ill_v4->ill_lock);
15537 			if (!ill_is_quiescent(ill_v4)) {
15538 				(void) ipsq_pending_mp_add(NULL,
15539 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15540 				mutex_exit(&ill_v4->ill_lock);
15541 				err = EINPROGRESS;
15542 				goto done;
15543 			}
15544 			mutex_exit(&ill_v4->ill_lock);
15545 		}
15546 
15547 		if (ill_v6 != NULL) {
15548 			mutex_enter(&ill_v6->ill_lock);
15549 			if (!ill_is_quiescent(ill_v6)) {
15550 				(void) ipsq_pending_mp_add(NULL,
15551 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15552 				mutex_exit(&ill_v6->ill_lock);
15553 				err = EINPROGRESS;
15554 				goto done;
15555 			}
15556 			mutex_exit(&ill_v6->ill_lock);
15557 		}
15558 
15559 		/*
15560 		 * allocate including space for null terminator
15561 		 * before we insert.
15562 		 */
15563 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15564 		if (tmp == NULL)
15565 			return (ENOMEM);
15566 
15567 		rw_enter(&ill_g_lock, RW_WRITER);
15568 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15569 		mutex_enter(&phyi->phyint_lock);
15570 		if (phyi->phyint_groupname_len != 0) {
15571 			ASSERT(phyi->phyint_groupname != NULL);
15572 			mi_free(phyi->phyint_groupname);
15573 		}
15574 
15575 		/*
15576 		 * setup the new group name.
15577 		 */
15578 		phyi->phyint_groupname = tmp;
15579 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15580 		phyi->phyint_groupname_len = namelen + 1;
15581 		mutex_exit(&phyi->phyint_lock);
15582 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15583 		rw_exit(&ill_g_lock);
15584 
15585 		err = ill_up_ipifs(ill, q, mp);
15586 	}
15587 
15588 done:
15589 	/*
15590 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15591 	 */
15592 	if (err != EINPROGRESS) {
15593 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15594 		if (ill_v4 != NULL)
15595 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15596 		if (ill_v6 != NULL)
15597 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15598 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15599 	}
15600 	return (err);
15601 }
15602 
15603 /* ARGSUSED */
15604 int
15605 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15606     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15607 {
15608 	ill_t *ill;
15609 	phyint_t *phyi;
15610 	struct lifreq *lifr;
15611 	mblk_t	*mp1;
15612 
15613 	/* Existence verified in ip_wput_nondata */
15614 	mp1 = mp->b_cont->b_cont;
15615 	lifr = (struct lifreq *)mp1->b_rptr;
15616 	ill = ipif->ipif_ill;
15617 	phyi = ill->ill_phyint;
15618 
15619 	lifr->lifr_groupname[0] = '\0';
15620 	/*
15621 	 * ill_group may be null if all the interfaces
15622 	 * are down. But still, the phyint should always
15623 	 * hold the name.
15624 	 */
15625 	if (phyi->phyint_groupname_len != 0) {
15626 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15627 		    phyi->phyint_groupname_len);
15628 	}
15629 
15630 	return (0);
15631 }
15632 
15633 
15634 typedef struct conn_move_s {
15635 	ill_t	*cm_from_ill;
15636 	ill_t	*cm_to_ill;
15637 	int	cm_ifindex;
15638 } conn_move_t;
15639 
15640 /*
15641  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15642  */
15643 static void
15644 conn_move(conn_t *connp, caddr_t arg)
15645 {
15646 	conn_move_t *connm;
15647 	int ifindex;
15648 	int i;
15649 	ill_t *from_ill;
15650 	ill_t *to_ill;
15651 	ilg_t *ilg;
15652 	ilm_t *ret_ilm;
15653 
15654 	connm = (conn_move_t *)arg;
15655 	ifindex = connm->cm_ifindex;
15656 	from_ill = connm->cm_from_ill;
15657 	to_ill = connm->cm_to_ill;
15658 
15659 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15660 
15661 	/* All multicast fields protected by conn_lock */
15662 	mutex_enter(&connp->conn_lock);
15663 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15664 	if ((connp->conn_outgoing_ill == from_ill) &&
15665 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15666 		connp->conn_outgoing_ill = to_ill;
15667 		connp->conn_incoming_ill = to_ill;
15668 	}
15669 
15670 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15671 
15672 	if ((connp->conn_multicast_ill == from_ill) &&
15673 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15674 		connp->conn_multicast_ill = connm->cm_to_ill;
15675 	}
15676 
15677 	/* Change IP_XMIT_IF associations */
15678 	if ((connp->conn_xmit_if_ill == from_ill) &&
15679 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15680 		connp->conn_xmit_if_ill = to_ill;
15681 	}
15682 	/*
15683 	 * Change the ilg_ill to point to the new one. This assumes
15684 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15685 	 * has been told to receive packets on this interface.
15686 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15687 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15688 	 * some ilms may not have moved. We check to see whether
15689 	 * the ilms have moved to to_ill. We can't check on from_ill
15690 	 * as in the process of moving, we could have split an ilm
15691 	 * in to two - which has the same orig_ifindex and v6group.
15692 	 *
15693 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15694 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15695 	 */
15696 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15697 		ilg = &connp->conn_ilg[i];
15698 		if ((ilg->ilg_ill == from_ill) &&
15699 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15700 			/* ifindex != 0 indicates failback */
15701 			if (ifindex != 0) {
15702 				connp->conn_ilg[i].ilg_ill = to_ill;
15703 				continue;
15704 			}
15705 
15706 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15707 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15708 			    connp->conn_zoneid);
15709 
15710 			if (ret_ilm != NULL)
15711 				connp->conn_ilg[i].ilg_ill = to_ill;
15712 		}
15713 	}
15714 	mutex_exit(&connp->conn_lock);
15715 }
15716 
15717 static void
15718 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15719 {
15720 	conn_move_t connm;
15721 
15722 	connm.cm_from_ill = from_ill;
15723 	connm.cm_to_ill = to_ill;
15724 	connm.cm_ifindex = ifindex;
15725 
15726 	ipcl_walk(conn_move, (caddr_t)&connm);
15727 }
15728 
15729 /*
15730  * ilm has been moved from from_ill to to_ill.
15731  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15732  * appropriately.
15733  *
15734  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15735  *	  the code there de-references ipif_ill to get the ill to
15736  *	  send multicast requests. It does not work as ipif is on its
15737  *	  move and already moved when this function is called.
15738  *	  Thus, we need to use from_ill and to_ill send down multicast
15739  *	  requests.
15740  */
15741 static void
15742 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15743 {
15744 	ipif_t *ipif;
15745 	ilm_t *ilm;
15746 
15747 	/*
15748 	 * See whether we need to send down DL_ENABMULTI_REQ on
15749 	 * to_ill as ilm has just been added.
15750 	 */
15751 	ASSERT(IAM_WRITER_ILL(to_ill));
15752 	ASSERT(IAM_WRITER_ILL(from_ill));
15753 
15754 	ILM_WALKER_HOLD(to_ill);
15755 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15756 
15757 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
15758 			continue;
15759 		/*
15760 		 * no locks held, ill/ipif cannot dissappear as long
15761 		 * as we are writer.
15762 		 */
15763 		ipif = to_ill->ill_ipif;
15764 		/*
15765 		 * No need to hold any lock as we are the writer and this
15766 		 * can only be changed by a writer.
15767 		 */
15768 		ilm->ilm_is_new = B_FALSE;
15769 
15770 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15771 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15772 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15773 			    "resolver\n"));
15774 			continue;		/* Must be IRE_IF_NORESOLVER */
15775 		}
15776 
15777 
15778 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15779 			ip1dbg(("ilm_send_multicast_reqs: "
15780 			    "to_ill MULTI_BCAST\n"));
15781 			goto from;
15782 		}
15783 
15784 		if (to_ill->ill_isv6)
15785 			mld_joingroup(ilm);
15786 		else
15787 			igmp_joingroup(ilm);
15788 
15789 		if (to_ill->ill_ipif_up_count == 0) {
15790 			/*
15791 			 * Nobody there. All multicast addresses will be
15792 			 * re-joined when we get the DL_BIND_ACK bringing the
15793 			 * interface up.
15794 			 */
15795 			ilm->ilm_notify_driver = B_FALSE;
15796 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15797 			goto from;
15798 		}
15799 
15800 		/*
15801 		 * For allmulti address, we want to join on only one interface.
15802 		 * Checking for ilm_numentries_v6 is not correct as you may
15803 		 * find an ilm with zero address on to_ill, but we may not
15804 		 * have nominated to_ill for receiving. Thus, if we have
15805 		 * nominated from_ill (ill_join_allmulti is set), nominate
15806 		 * only if to_ill is not already nominated (to_ill normally
15807 		 * should not have been nominated if "from_ill" has already
15808 		 * been nominated. As we don't prevent failovers from happening
15809 		 * across groups, we don't assert).
15810 		 */
15811 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15812 			/*
15813 			 * There is no need to hold ill locks as we are
15814 			 * writer on both ills and when ill_join_allmulti
15815 			 * is changed the thread is always a writer.
15816 			 */
15817 			if (from_ill->ill_join_allmulti &&
15818 			    !to_ill->ill_join_allmulti) {
15819 				(void) ip_join_allmulti(to_ill->ill_ipif);
15820 			}
15821 		} else if (ilm->ilm_notify_driver) {
15822 
15823 			/*
15824 			 * This is a newly moved ilm so we need to tell the
15825 			 * driver about the new group. There can be more than
15826 			 * one ilm's for the same group in the list each with a
15827 			 * different orig_ifindex. We have to inform the driver
15828 			 * once. In ilm_move_v[4,6] we only set the flag
15829 			 * ilm_notify_driver for the first ilm.
15830 			 */
15831 
15832 			(void) ip_ll_send_enabmulti_req(to_ill,
15833 			    &ilm->ilm_v6addr);
15834 		}
15835 
15836 		ilm->ilm_notify_driver = B_FALSE;
15837 
15838 		/*
15839 		 * See whether we need to send down DL_DISABMULTI_REQ on
15840 		 * from_ill as ilm has just been removed.
15841 		 */
15842 from:
15843 		ipif = from_ill->ill_ipif;
15844 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15845 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15846 			ip1dbg(("ilm_send_multicast_reqs: "
15847 			    "from_ill not resolver\n"));
15848 			continue;		/* Must be IRE_IF_NORESOLVER */
15849 		}
15850 
15851 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15852 			ip1dbg(("ilm_send_multicast_reqs: "
15853 			    "from_ill MULTI_BCAST\n"));
15854 			continue;
15855 		}
15856 
15857 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15858 			if (from_ill->ill_join_allmulti)
15859 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15860 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15861 			(void) ip_ll_send_disabmulti_req(from_ill,
15862 		    &ilm->ilm_v6addr);
15863 		}
15864 	}
15865 	ILM_WALKER_RELE(to_ill);
15866 }
15867 
15868 /*
15869  * This function is called when all multicast memberships needs
15870  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15871  * called only once unlike the IPv4 counterpart where it is called after
15872  * every logical interface is moved. The reason is due to multicast
15873  * memberships are joined using an interface address in IPv4 while in
15874  * IPv6, interface index is used.
15875  */
15876 static void
15877 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15878 {
15879 	ilm_t	*ilm;
15880 	ilm_t	*ilm_next;
15881 	ilm_t	*new_ilm;
15882 	ilm_t	**ilmp;
15883 	int	count;
15884 	char buf[INET6_ADDRSTRLEN];
15885 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15886 
15887 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15888 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15889 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15890 
15891 	if (ifindex == 0) {
15892 		/*
15893 		 * Form the solicited node mcast address which is used later.
15894 		 */
15895 		ipif_t *ipif;
15896 
15897 		ipif = from_ill->ill_ipif;
15898 		ASSERT(ipif->ipif_id == 0);
15899 
15900 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15901 	}
15902 
15903 	ilmp = &from_ill->ill_ilm;
15904 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
15905 		ilm_next = ilm->ilm_next;
15906 
15907 		if (ilm->ilm_flags & ILM_DELETED) {
15908 			ilmp = &ilm->ilm_next;
15909 			continue;
15910 		}
15911 
15912 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15913 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15914 		ASSERT(ilm->ilm_orig_ifindex != 0);
15915 		if (ilm->ilm_orig_ifindex == ifindex) {
15916 			/*
15917 			 * We are failing back multicast memberships.
15918 			 * If the same ilm exists in to_ill, it means somebody
15919 			 * has joined the same group there e.g. ff02::1
15920 			 * is joined within the kernel when the interfaces
15921 			 * came UP.
15922 			 */
15923 			ASSERT(ilm->ilm_ipif == NULL);
15924 			if (new_ilm != NULL) {
15925 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15926 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15927 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15928 					new_ilm->ilm_is_new = B_TRUE;
15929 				}
15930 			} else {
15931 				/*
15932 				 * check if we can just move the ilm
15933 				 */
15934 				if (from_ill->ill_ilm_walker_cnt != 0) {
15935 					/*
15936 					 * We have walkers we cannot move
15937 					 * the ilm, so allocate a new ilm,
15938 					 * this (old) ilm will be marked
15939 					 * ILM_DELETED at the end of the loop
15940 					 * and will be freed when the
15941 					 * last walker exits.
15942 					 */
15943 					new_ilm = (ilm_t *)mi_zalloc
15944 					    (sizeof (ilm_t));
15945 					if (new_ilm == NULL) {
15946 						ip0dbg(("ilm_move_v6: "
15947 						    "FAILBACK of IPv6"
15948 						    " multicast address %s : "
15949 						    "from %s to"
15950 						    " %s failed : ENOMEM \n",
15951 						    inet_ntop(AF_INET6,
15952 						    &ilm->ilm_v6addr, buf,
15953 						    sizeof (buf)),
15954 						    from_ill->ill_name,
15955 						    to_ill->ill_name));
15956 
15957 							ilmp = &ilm->ilm_next;
15958 							continue;
15959 					}
15960 					*new_ilm = *ilm;
15961 					/*
15962 					 * we don't want new_ilm linked to
15963 					 * ilm's filter list.
15964 					 */
15965 					new_ilm->ilm_filter = NULL;
15966 				} else {
15967 					/*
15968 					 * No walkers we can move the ilm.
15969 					 * lets take it out of the list.
15970 					 */
15971 					*ilmp = ilm->ilm_next;
15972 					ilm->ilm_next = NULL;
15973 					new_ilm = ilm;
15974 				}
15975 
15976 				/*
15977 				 * if this is the first ilm for the group
15978 				 * set ilm_notify_driver so that we notify the
15979 				 * driver in ilm_send_multicast_reqs.
15980 				 */
15981 				if (ilm_lookup_ill_v6(to_ill,
15982 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
15983 					new_ilm->ilm_notify_driver = B_TRUE;
15984 
15985 				new_ilm->ilm_ill = to_ill;
15986 				/* Add to the to_ill's list */
15987 				new_ilm->ilm_next = to_ill->ill_ilm;
15988 				to_ill->ill_ilm = new_ilm;
15989 				/*
15990 				 * set the flag so that mld_joingroup is
15991 				 * called in ilm_send_multicast_reqs().
15992 				 */
15993 				new_ilm->ilm_is_new = B_TRUE;
15994 			}
15995 			goto bottom;
15996 		} else if (ifindex != 0) {
15997 			/*
15998 			 * If this is FAILBACK (ifindex != 0) and the ifindex
15999 			 * has not matched above, look at the next ilm.
16000 			 */
16001 			ilmp = &ilm->ilm_next;
16002 			continue;
16003 		}
16004 		/*
16005 		 * If we are here, it means ifindex is 0. Failover
16006 		 * everything.
16007 		 *
16008 		 * We need to handle solicited node mcast address
16009 		 * and all_nodes mcast address differently as they
16010 		 * are joined witin the kenrel (ipif_multicast_up)
16011 		 * and potentially from the userland. We are called
16012 		 * after the ipifs of from_ill has been moved.
16013 		 * If we still find ilms on ill with solicited node
16014 		 * mcast address or all_nodes mcast address, it must
16015 		 * belong to the UP interface that has not moved e.g.
16016 		 * ipif_id 0 with the link local prefix does not move.
16017 		 * We join this on the new ill accounting for all the
16018 		 * userland memberships so that applications don't
16019 		 * see any failure.
16020 		 *
16021 		 * We need to make sure that we account only for the
16022 		 * solicited node and all node multicast addresses
16023 		 * that was brought UP on these. In the case of
16024 		 * a failover from A to B, we might have ilms belonging
16025 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16026 		 * for the membership from the userland. If we are failing
16027 		 * over from B to C now, we will find the ones belonging
16028 		 * to A on B. These don't account for the ill_ipif_up_count.
16029 		 * They just move from B to C. The check below on
16030 		 * ilm_orig_ifindex ensures that.
16031 		 */
16032 		if ((ilm->ilm_orig_ifindex ==
16033 		    from_ill->ill_phyint->phyint_ifindex) &&
16034 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16035 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16036 		    &ilm->ilm_v6addr))) {
16037 			ASSERT(ilm->ilm_refcnt > 0);
16038 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16039 			/*
16040 			 * For indentation reasons, we are not using a
16041 			 * "else" here.
16042 			 */
16043 			if (count == 0) {
16044 				ilmp = &ilm->ilm_next;
16045 				continue;
16046 			}
16047 			ilm->ilm_refcnt -= count;
16048 			if (new_ilm != NULL) {
16049 				/*
16050 				 * Can find one with the same
16051 				 * ilm_orig_ifindex, if we are failing
16052 				 * over to a STANDBY. This happens
16053 				 * when somebody wants to join a group
16054 				 * on a STANDBY interface and we
16055 				 * internally join on a different one.
16056 				 * If we had joined on from_ill then, a
16057 				 * failover now will find a new ilm
16058 				 * with this index.
16059 				 */
16060 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16061 				    " new ilm on %s, group address %s\n",
16062 				    to_ill->ill_name,
16063 				    inet_ntop(AF_INET6,
16064 				    &ilm->ilm_v6addr, buf,
16065 				    sizeof (buf))));
16066 				new_ilm->ilm_refcnt += count;
16067 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16068 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16069 					new_ilm->ilm_is_new = B_TRUE;
16070 				}
16071 			} else {
16072 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16073 				if (new_ilm == NULL) {
16074 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16075 					    " multicast address %s : from %s to"
16076 					    " %s failed : ENOMEM \n",
16077 					    inet_ntop(AF_INET6,
16078 					    &ilm->ilm_v6addr, buf,
16079 					    sizeof (buf)), from_ill->ill_name,
16080 					    to_ill->ill_name));
16081 					ilmp = &ilm->ilm_next;
16082 					continue;
16083 				}
16084 				*new_ilm = *ilm;
16085 				new_ilm->ilm_filter = NULL;
16086 				new_ilm->ilm_refcnt = count;
16087 				new_ilm->ilm_timer = INFINITY;
16088 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16089 				new_ilm->ilm_is_new = B_TRUE;
16090 				/*
16091 				 * If the to_ill has not joined this
16092 				 * group we need to tell the driver in
16093 				 * ill_send_multicast_reqs.
16094 				 */
16095 				if (ilm_lookup_ill_v6(to_ill,
16096 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16097 					new_ilm->ilm_notify_driver = B_TRUE;
16098 
16099 				new_ilm->ilm_ill = to_ill;
16100 				/* Add to the to_ill's list */
16101 				new_ilm->ilm_next = to_ill->ill_ilm;
16102 				to_ill->ill_ilm = new_ilm;
16103 				ASSERT(new_ilm->ilm_ipif == NULL);
16104 			}
16105 			if (ilm->ilm_refcnt == 0) {
16106 				goto bottom;
16107 			} else {
16108 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16109 				CLEAR_SLIST(new_ilm->ilm_filter);
16110 				ilmp = &ilm->ilm_next;
16111 			}
16112 			continue;
16113 		} else {
16114 			/*
16115 			 * ifindex = 0 means, move everything pointing at
16116 			 * from_ill. We are doing this becuase ill has
16117 			 * either FAILED or became INACTIVE.
16118 			 *
16119 			 * As we would like to move things later back to
16120 			 * from_ill, we want to retain the identity of this
16121 			 * ilm. Thus, we don't blindly increment the reference
16122 			 * count on the ilms matching the address alone. We
16123 			 * need to match on the ilm_orig_index also. new_ilm
16124 			 * was obtained by matching ilm_orig_index also.
16125 			 */
16126 			if (new_ilm != NULL) {
16127 				/*
16128 				 * This is possible only if a previous restore
16129 				 * was incomplete i.e restore to
16130 				 * ilm_orig_ifindex left some ilms because
16131 				 * of some failures. Thus when we are failing
16132 				 * again, we might find our old friends there.
16133 				 */
16134 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16135 				    " on %s, group address %s\n",
16136 				    to_ill->ill_name,
16137 				    inet_ntop(AF_INET6,
16138 				    &ilm->ilm_v6addr, buf,
16139 				    sizeof (buf))));
16140 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16141 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16142 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16143 					new_ilm->ilm_is_new = B_TRUE;
16144 				}
16145 			} else {
16146 				if (from_ill->ill_ilm_walker_cnt != 0) {
16147 					new_ilm = (ilm_t *)
16148 					    mi_zalloc(sizeof (ilm_t));
16149 					if (new_ilm == NULL) {
16150 						ip0dbg(("ilm_move_v6: "
16151 						    "FAILOVER of IPv6"
16152 						    " multicast address %s : "
16153 						    "from %s to"
16154 						    " %s failed : ENOMEM \n",
16155 						    inet_ntop(AF_INET6,
16156 						    &ilm->ilm_v6addr, buf,
16157 						    sizeof (buf)),
16158 						    from_ill->ill_name,
16159 						    to_ill->ill_name));
16160 
16161 							ilmp = &ilm->ilm_next;
16162 							continue;
16163 					}
16164 					*new_ilm = *ilm;
16165 					new_ilm->ilm_filter = NULL;
16166 				} else {
16167 					*ilmp = ilm->ilm_next;
16168 					new_ilm = ilm;
16169 				}
16170 				/*
16171 				 * If the to_ill has not joined this
16172 				 * group we need to tell the driver in
16173 				 * ill_send_multicast_reqs.
16174 				 */
16175 				if (ilm_lookup_ill_v6(to_ill,
16176 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16177 					new_ilm->ilm_notify_driver = B_TRUE;
16178 
16179 				/* Add to the to_ill's list */
16180 				new_ilm->ilm_next = to_ill->ill_ilm;
16181 				to_ill->ill_ilm = new_ilm;
16182 				ASSERT(ilm->ilm_ipif == NULL);
16183 				new_ilm->ilm_ill = to_ill;
16184 				new_ilm->ilm_is_new = B_TRUE;
16185 			}
16186 
16187 		}
16188 
16189 bottom:
16190 		/*
16191 		 * Revert multicast filter state to (EXCLUDE, NULL).
16192 		 * new_ilm->ilm_is_new should already be set if needed.
16193 		 */
16194 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16195 		CLEAR_SLIST(new_ilm->ilm_filter);
16196 		/*
16197 		 * We allocated/got a new ilm, free the old one.
16198 		 */
16199 		if (new_ilm != ilm) {
16200 			if (from_ill->ill_ilm_walker_cnt == 0) {
16201 				*ilmp = ilm->ilm_next;
16202 				ilm->ilm_next = NULL;
16203 				FREE_SLIST(ilm->ilm_filter);
16204 				FREE_SLIST(ilm->ilm_pendsrcs);
16205 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16206 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16207 				mi_free((char *)ilm);
16208 			} else {
16209 				ilm->ilm_flags |= ILM_DELETED;
16210 				from_ill->ill_ilm_cleanup_reqd = 1;
16211 				ilmp = &ilm->ilm_next;
16212 			}
16213 		}
16214 	}
16215 }
16216 
16217 /*
16218  * Move all the multicast memberships to to_ill. Called when
16219  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16220  * different from IPv6 counterpart as multicast memberships are associated
16221  * with ills in IPv6. This function is called after every ipif is moved
16222  * unlike IPv6, where it is moved only once.
16223  */
16224 static void
16225 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16226 {
16227 	ilm_t	*ilm;
16228 	ilm_t	*ilm_next;
16229 	ilm_t	*new_ilm;
16230 	ilm_t	**ilmp;
16231 
16232 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16233 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16234 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16235 
16236 	ilmp = &from_ill->ill_ilm;
16237 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16238 		ilm_next = ilm->ilm_next;
16239 
16240 		if (ilm->ilm_flags & ILM_DELETED) {
16241 			ilmp = &ilm->ilm_next;
16242 			continue;
16243 		}
16244 
16245 		ASSERT(ilm->ilm_ipif != NULL);
16246 
16247 		if (ilm->ilm_ipif != ipif) {
16248 			ilmp = &ilm->ilm_next;
16249 			continue;
16250 		}
16251 
16252 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16253 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16254 			/*
16255 			 * We joined this in ipif_multicast_up
16256 			 * and we never did an ipif_multicast_down
16257 			 * for IPv4. If nobody else from the userland
16258 			 * has reference, we free the ilm, and later
16259 			 * when this ipif comes up on the new ill,
16260 			 * we will join this again.
16261 			 */
16262 			if (--ilm->ilm_refcnt == 0)
16263 				goto delete_ilm;
16264 
16265 			new_ilm = ilm_lookup_ipif(ipif,
16266 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16267 			if (new_ilm != NULL) {
16268 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16269 				/*
16270 				 * We still need to deal with the from_ill.
16271 				 */
16272 				new_ilm->ilm_is_new = B_TRUE;
16273 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16274 				CLEAR_SLIST(new_ilm->ilm_filter);
16275 				goto delete_ilm;
16276 			}
16277 			/*
16278 			 * If we could not find one e.g. ipif is
16279 			 * still down on to_ill, we add this ilm
16280 			 * on ill_new to preserve the reference
16281 			 * count.
16282 			 */
16283 		}
16284 		/*
16285 		 * When ipifs move, ilms always move with it
16286 		 * to the NEW ill. Thus we should never be
16287 		 * able to find ilm till we really move it here.
16288 		 */
16289 		ASSERT(ilm_lookup_ipif(ipif,
16290 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16291 
16292 		if (from_ill->ill_ilm_walker_cnt != 0) {
16293 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16294 			if (new_ilm == NULL) {
16295 				char buf[INET6_ADDRSTRLEN];
16296 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16297 				    " multicast address %s : "
16298 				    "from %s to"
16299 				    " %s failed : ENOMEM \n",
16300 				    inet_ntop(AF_INET,
16301 				    &ilm->ilm_v6addr, buf,
16302 				    sizeof (buf)),
16303 				    from_ill->ill_name,
16304 				    to_ill->ill_name));
16305 
16306 				ilmp = &ilm->ilm_next;
16307 				continue;
16308 			}
16309 			*new_ilm = *ilm;
16310 			/* We don't want new_ilm linked to ilm's filter list */
16311 			new_ilm->ilm_filter = NULL;
16312 		} else {
16313 			/* Remove from the list */
16314 			*ilmp = ilm->ilm_next;
16315 			new_ilm = ilm;
16316 		}
16317 
16318 		/*
16319 		 * If we have never joined this group on the to_ill
16320 		 * make sure we tell the driver.
16321 		 */
16322 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16323 		    ALL_ZONES) == NULL)
16324 			new_ilm->ilm_notify_driver = B_TRUE;
16325 
16326 		/* Add to the to_ill's list */
16327 		new_ilm->ilm_next = to_ill->ill_ilm;
16328 		to_ill->ill_ilm = new_ilm;
16329 		new_ilm->ilm_is_new = B_TRUE;
16330 
16331 		/*
16332 		 * Revert multicast filter state to (EXCLUDE, NULL)
16333 		 */
16334 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16335 		CLEAR_SLIST(new_ilm->ilm_filter);
16336 
16337 		/*
16338 		 * Delete only if we have allocated a new ilm.
16339 		 */
16340 		if (new_ilm != ilm) {
16341 delete_ilm:
16342 			if (from_ill->ill_ilm_walker_cnt == 0) {
16343 				/* Remove from the list */
16344 				*ilmp = ilm->ilm_next;
16345 				ilm->ilm_next = NULL;
16346 				FREE_SLIST(ilm->ilm_filter);
16347 				FREE_SLIST(ilm->ilm_pendsrcs);
16348 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16349 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16350 				mi_free((char *)ilm);
16351 			} else {
16352 				ilm->ilm_flags |= ILM_DELETED;
16353 				from_ill->ill_ilm_cleanup_reqd = 1;
16354 				ilmp = &ilm->ilm_next;
16355 			}
16356 		}
16357 	}
16358 }
16359 
16360 static uint_t
16361 ipif_get_id(ill_t *ill, uint_t id)
16362 {
16363 	uint_t	unit;
16364 	ipif_t	*tipif;
16365 	boolean_t found = B_FALSE;
16366 
16367 	/*
16368 	 * During failback, we want to go back to the same id
16369 	 * instead of the smallest id so that the original
16370 	 * configuration is maintained. id is non-zero in that
16371 	 * case.
16372 	 */
16373 	if (id != 0) {
16374 		/*
16375 		 * While failing back, if we still have an ipif with
16376 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16377 		 * as soon as we return from this function. It was
16378 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16379 		 * we can choose the smallest id. Thus we return zero
16380 		 * in that case ignoring the hint.
16381 		 */
16382 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16383 			return (0);
16384 		for (tipif = ill->ill_ipif; tipif != NULL;
16385 		    tipif = tipif->ipif_next) {
16386 			if (tipif->ipif_id == id) {
16387 				found = B_TRUE;
16388 				break;
16389 			}
16390 		}
16391 		/*
16392 		 * If somebody already plumbed another logical
16393 		 * with the same id, we won't be able to find it.
16394 		 */
16395 		if (!found)
16396 			return (id);
16397 	}
16398 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16399 		found = B_FALSE;
16400 		for (tipif = ill->ill_ipif; tipif != NULL;
16401 		    tipif = tipif->ipif_next) {
16402 			if (tipif->ipif_id == unit) {
16403 				found = B_TRUE;
16404 				break;
16405 			}
16406 		}
16407 		if (!found)
16408 			break;
16409 	}
16410 	return (unit);
16411 }
16412 
16413 /* ARGSUSED */
16414 static int
16415 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16416     ipif_t **rep_ipif_ptr)
16417 {
16418 	ill_t	*from_ill;
16419 	ipif_t	*rep_ipif;
16420 	ipif_t	**ipifp;
16421 	uint_t	unit;
16422 	int err = 0;
16423 	ipif_t	*to_ipif;
16424 	struct iocblk	*iocp;
16425 	boolean_t failback_cmd;
16426 	boolean_t remove_ipif;
16427 	int	rc;
16428 
16429 	ASSERT(IAM_WRITER_ILL(to_ill));
16430 	ASSERT(IAM_WRITER_IPIF(ipif));
16431 
16432 	iocp = (struct iocblk *)mp->b_rptr;
16433 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16434 	remove_ipif = B_FALSE;
16435 
16436 	from_ill = ipif->ipif_ill;
16437 
16438 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16439 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16440 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16441 
16442 	/*
16443 	 * Don't move LINK LOCAL addresses as they are tied to
16444 	 * physical interface.
16445 	 */
16446 	if (from_ill->ill_isv6 &&
16447 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16448 		ipif->ipif_was_up = B_FALSE;
16449 		IPIF_UNMARK_MOVING(ipif);
16450 		return (0);
16451 	}
16452 
16453 	/*
16454 	 * We set the ipif_id to maximum so that the search for
16455 	 * ipif_id will pick the lowest number i.e 0 in the
16456 	 * following 2 cases :
16457 	 *
16458 	 * 1) We have a replacement ipif at the head of to_ill.
16459 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16460 	 *    on to_ill and hence the MOVE might fail. We want to
16461 	 *    remove it only if we could move the ipif. Thus, by
16462 	 *    setting it to the MAX value, we make the search in
16463 	 *    ipif_get_id return the zeroth id.
16464 	 *
16465 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16466 	 *    we might just have a zero address plumbed on the ipif
16467 	 *    with zero id in the case of IPv4. We remove that while
16468 	 *    doing the failback. We want to remove it only if we
16469 	 *    could move the ipif. Thus, by setting it to the MAX
16470 	 *    value, we make the search in ipif_get_id return the
16471 	 *    zeroth id.
16472 	 *
16473 	 * Both (1) and (2) are done only when when we are moving
16474 	 * an ipif (either due to failover/failback) which originally
16475 	 * belonged to this interface i.e the ipif_orig_ifindex is
16476 	 * the same as to_ill's ifindex. This is needed so that
16477 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16478 	 * from B -> A (B is being removed from the group) and
16479 	 * FAILBACK from A -> B restores the original configuration.
16480 	 * Without the check for orig_ifindex, the second FAILOVER
16481 	 * could make the ipif belonging to B replace the A's zeroth
16482 	 * ipif and the subsequent failback re-creating the replacement
16483 	 * ipif again.
16484 	 *
16485 	 * NOTE : We created the replacement ipif when we did a
16486 	 * FAILOVER (See below). We could check for FAILBACK and
16487 	 * then look for replacement ipif to be removed. But we don't
16488 	 * want to do that because we wan't to allow the possibility
16489 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16490 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16491 	 * from B -> A.
16492 	 */
16493 	to_ipif = to_ill->ill_ipif;
16494 	if ((to_ill->ill_phyint->phyint_ifindex ==
16495 	    ipif->ipif_orig_ifindex) &&
16496 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16497 		ASSERT(to_ipif->ipif_id == 0);
16498 		remove_ipif = B_TRUE;
16499 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16500 	}
16501 	/*
16502 	 * Find the lowest logical unit number on the to_ill.
16503 	 * If we are failing back, try to get the original id
16504 	 * rather than the lowest one so that the original
16505 	 * configuration is maintained.
16506 	 *
16507 	 * XXX need a better scheme for this.
16508 	 */
16509 	if (failback_cmd) {
16510 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16511 	} else {
16512 		unit = ipif_get_id(to_ill, 0);
16513 	}
16514 
16515 	/* Reset back to zero in case we fail below */
16516 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16517 		to_ipif->ipif_id = 0;
16518 
16519 	if (unit == ip_addrs_per_if) {
16520 		ipif->ipif_was_up = B_FALSE;
16521 		IPIF_UNMARK_MOVING(ipif);
16522 		return (EINVAL);
16523 	}
16524 
16525 	/*
16526 	 * ipif is ready to move from "from_ill" to "to_ill".
16527 	 *
16528 	 * 1) If we are moving ipif with id zero, create a
16529 	 *    replacement ipif for this ipif on from_ill. If this fails
16530 	 *    fail the MOVE operation.
16531 	 *
16532 	 * 2) Remove the replacement ipif on to_ill if any.
16533 	 *    We could remove the replacement ipif when we are moving
16534 	 *    the ipif with id zero. But what if somebody already
16535 	 *    unplumbed it ? Thus we always remove it if it is present.
16536 	 *    We want to do it only if we are sure we are going to
16537 	 *    move the ipif to to_ill which is why there are no
16538 	 *    returns due to error till ipif is linked to to_ill.
16539 	 *    Note that the first ipif that we failback will always
16540 	 *    be zero if it is present.
16541 	 */
16542 	if (ipif->ipif_id == 0) {
16543 		ipaddr_t inaddr_any = INADDR_ANY;
16544 
16545 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16546 		if (rep_ipif == NULL) {
16547 			ipif->ipif_was_up = B_FALSE;
16548 			IPIF_UNMARK_MOVING(ipif);
16549 			return (ENOMEM);
16550 		}
16551 		*rep_ipif = ipif_zero;
16552 		/*
16553 		 * Before we put the ipif on the list, store the addresses
16554 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16555 		 * assumes so. This logic is not any different from what
16556 		 * ipif_allocate does.
16557 		 */
16558 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16559 		    &rep_ipif->ipif_v6lcl_addr);
16560 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16561 		    &rep_ipif->ipif_v6src_addr);
16562 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16563 		    &rep_ipif->ipif_v6subnet);
16564 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16565 		    &rep_ipif->ipif_v6net_mask);
16566 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16567 		    &rep_ipif->ipif_v6brd_addr);
16568 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16569 		    &rep_ipif->ipif_v6pp_dst_addr);
16570 		/*
16571 		 * We mark IPIF_NOFAILOVER so that this can never
16572 		 * move.
16573 		 */
16574 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16575 		rep_ipif->ipif_flags &= ~IPIF_UP;
16576 		rep_ipif->ipif_replace_zero = B_TRUE;
16577 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16578 		    MUTEX_DEFAULT, NULL);
16579 		rep_ipif->ipif_id = 0;
16580 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16581 		rep_ipif->ipif_ill = from_ill;
16582 		rep_ipif->ipif_orig_ifindex =
16583 		    from_ill->ill_phyint->phyint_ifindex;
16584 		/* Insert at head */
16585 		rep_ipif->ipif_next = from_ill->ill_ipif;
16586 		from_ill->ill_ipif = rep_ipif;
16587 		/*
16588 		 * We don't really care to let apps know about
16589 		 * this interface.
16590 		 */
16591 	}
16592 
16593 	if (remove_ipif) {
16594 		/*
16595 		 * We set to a max value above for this case to get
16596 		 * id zero. ASSERT that we did get one.
16597 		 */
16598 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16599 		rep_ipif = to_ipif;
16600 		to_ill->ill_ipif = rep_ipif->ipif_next;
16601 		rep_ipif->ipif_next = NULL;
16602 		/*
16603 		 * If some apps scanned and find this interface,
16604 		 * it is time to let them know, so that they can
16605 		 * delete it.
16606 		 */
16607 
16608 		*rep_ipif_ptr = rep_ipif;
16609 	}
16610 
16611 	/* Get it out of the ILL interface list. */
16612 	ipifp = &ipif->ipif_ill->ill_ipif;
16613 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16614 		if (*ipifp == ipif) {
16615 			*ipifp = ipif->ipif_next;
16616 			break;
16617 		}
16618 	}
16619 
16620 	/* Assign the new ill */
16621 	ipif->ipif_ill = to_ill;
16622 	ipif->ipif_id = unit;
16623 	/* id has already been checked */
16624 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16625 	ASSERT(rc == 0);
16626 	/* Let SCTP update its list */
16627 	sctp_move_ipif(ipif, from_ill, to_ill);
16628 	/*
16629 	 * Handle the failover and failback of ipif_t between
16630 	 * ill_t that have differing maximum mtu values.
16631 	 */
16632 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16633 		if (ipif->ipif_saved_mtu == 0) {
16634 			/*
16635 			 * As this ipif_t is moving to an ill_t
16636 			 * that has a lower ill_max_mtu, its
16637 			 * ipif_mtu needs to be saved so it can
16638 			 * be restored during failback or during
16639 			 * failover to an ill_t which has a
16640 			 * higher ill_max_mtu.
16641 			 */
16642 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16643 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16644 		} else {
16645 			/*
16646 			 * The ipif_t is, once again, moving to
16647 			 * an ill_t that has a lower maximum mtu
16648 			 * value.
16649 			 */
16650 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16651 		}
16652 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16653 	    ipif->ipif_saved_mtu != 0) {
16654 		/*
16655 		 * The mtu of this ipif_t had to be reduced
16656 		 * during an earlier failover; this is an
16657 		 * opportunity for it to be increased (either as
16658 		 * part of another failover or a failback).
16659 		 */
16660 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16661 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16662 			ipif->ipif_saved_mtu = 0;
16663 		} else {
16664 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16665 		}
16666 	}
16667 
16668 	/*
16669 	 * We preserve all the other fields of the ipif including
16670 	 * ipif_saved_ire_mp. The routes that are saved here will
16671 	 * be recreated on the new interface and back on the old
16672 	 * interface when we move back.
16673 	 */
16674 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16675 
16676 	return (err);
16677 }
16678 
16679 static int
16680 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16681     int ifindex, ipif_t **rep_ipif_ptr)
16682 {
16683 	ipif_t *mipif;
16684 	ipif_t *ipif_next;
16685 	int err;
16686 
16687 	/*
16688 	 * We don't really try to MOVE back things if some of the
16689 	 * operations fail. The daemon will take care of moving again
16690 	 * later on.
16691 	 */
16692 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16693 		ipif_next = mipif->ipif_next;
16694 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16695 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16696 
16697 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16698 
16699 			/*
16700 			 * When the MOVE fails, it is the job of the
16701 			 * application to take care of this properly
16702 			 * i.e try again if it is ENOMEM.
16703 			 */
16704 			if (mipif->ipif_ill != from_ill) {
16705 				/*
16706 				 * ipif has moved.
16707 				 *
16708 				 * Move the multicast memberships associated
16709 				 * with this ipif to the new ill. For IPv6, we
16710 				 * do it once after all the ipifs are moved
16711 				 * (in ill_move) as they are not associated
16712 				 * with ipifs.
16713 				 *
16714 				 * We need to move the ilms as the ipif has
16715 				 * already been moved to a new ill even
16716 				 * in the case of errors. Neither
16717 				 * ilm_free(ipif) will find the ilm
16718 				 * when somebody unplumbs this ipif nor
16719 				 * ilm_delete(ilm) will be able to find the
16720 				 * ilm, if we don't move now.
16721 				 */
16722 				if (!from_ill->ill_isv6)
16723 					ilm_move_v4(from_ill, to_ill, mipif);
16724 			}
16725 
16726 			if (err != 0)
16727 				return (err);
16728 		}
16729 	}
16730 	return (0);
16731 }
16732 
16733 static int
16734 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16735 {
16736 	int ifindex;
16737 	int err;
16738 	struct iocblk	*iocp;
16739 	ipif_t	*ipif;
16740 	ipif_t *rep_ipif_ptr = NULL;
16741 	ipif_t	*from_ipif = NULL;
16742 	boolean_t check_rep_if = B_FALSE;
16743 
16744 	iocp = (struct iocblk *)mp->b_rptr;
16745 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16746 		/*
16747 		 * Move everything pointing at from_ill to to_ill.
16748 		 * We acheive this by passing in 0 as ifindex.
16749 		 */
16750 		ifindex = 0;
16751 	} else {
16752 		/*
16753 		 * Move everything pointing at from_ill whose original
16754 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16755 		 * We acheive this by passing in ifindex rather than 0.
16756 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16757 		 */
16758 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16759 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16760 	}
16761 
16762 	/*
16763 	 * Determine if there is at least one ipif that would move from
16764 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16765 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16766 	 * the move, in which case we need to quiesce the replacement ipif also.
16767 	 */
16768 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16769 	    from_ipif = from_ipif->ipif_next) {
16770 		if (((ifindex == 0) ||
16771 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16772 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16773 			check_rep_if = B_TRUE;
16774 			break;
16775 		}
16776 	}
16777 
16778 
16779 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16780 
16781 	GRAB_ILL_LOCKS(from_ill, to_ill);
16782 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16783 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16784 		    mp, ILL_MOVE_OK);
16785 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16786 		return (EINPROGRESS);
16787 	}
16788 
16789 	/* Check if the replacement ipif is quiescent to delete */
16790 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16791 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16792 		to_ill->ill_ipif->ipif_state_flags |=
16793 		    IPIF_MOVING | IPIF_CHANGING;
16794 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16795 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16796 			    mp, ILL_MOVE_OK);
16797 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16798 			return (EINPROGRESS);
16799 		}
16800 	}
16801 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16802 
16803 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16804 	rw_enter(&ill_g_lock, RW_WRITER);
16805 	GRAB_ILL_LOCKS(from_ill, to_ill);
16806 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16807 
16808 	/* ilm_move is done inside ipif_move for IPv4 */
16809 	if (err == 0 && from_ill->ill_isv6)
16810 		ilm_move_v6(from_ill, to_ill, ifindex);
16811 
16812 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16813 	rw_exit(&ill_g_lock);
16814 
16815 	/*
16816 	 * send rts messages and multicast messages.
16817 	 */
16818 	if (rep_ipif_ptr != NULL) {
16819 		ip_rts_ifmsg(rep_ipif_ptr);
16820 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16821 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16822 		mi_free(rep_ipif_ptr);
16823 	}
16824 
16825 	ilm_send_multicast_reqs(from_ill, to_ill);
16826 
16827 	conn_move_ill(from_ill, to_ill, ifindex);
16828 
16829 	return (err);
16830 }
16831 
16832 /*
16833  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16834  * Also checks for the validity of the arguments.
16835  * Note: We are already exclusive inside the from group.
16836  * It is upto the caller to release refcnt on the to_ill's.
16837  */
16838 static int
16839 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16840     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16841 {
16842 	int dst_index;
16843 	ipif_t *ipif_v4, *ipif_v6;
16844 	struct lifreq *lifr;
16845 	mblk_t *mp1;
16846 	boolean_t exists;
16847 	sin_t	*sin;
16848 	int	err = 0;
16849 
16850 	if ((mp1 = mp->b_cont) == NULL)
16851 		return (EPROTO);
16852 
16853 	if ((mp1 = mp1->b_cont) == NULL)
16854 		return (EPROTO);
16855 
16856 	lifr = (struct lifreq *)mp1->b_rptr;
16857 	sin = (sin_t *)&lifr->lifr_addr;
16858 
16859 	/*
16860 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16861 	 * specific operations.
16862 	 */
16863 	if (sin->sin_family != AF_UNSPEC)
16864 		return (EINVAL);
16865 
16866 	/*
16867 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16868 	 * NULLs for the last 4 args and we know the lookup won't fail
16869 	 * with EINPROGRESS.
16870 	 */
16871 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16872 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16873 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16874 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16875 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16876 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16877 
16878 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16879 		return (ENXIO);
16880 
16881 	if (ipif_v4 != NULL) {
16882 		ASSERT(ipif_v4->ipif_refcnt != 0);
16883 		if (ipif_v4->ipif_id != 0) {
16884 			err = EINVAL;
16885 			goto done;
16886 		}
16887 
16888 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16889 		*ill_from_v4 = ipif_v4->ipif_ill;
16890 	}
16891 
16892 	if (ipif_v6 != NULL) {
16893 		ASSERT(ipif_v6->ipif_refcnt != 0);
16894 		if (ipif_v6->ipif_id != 0) {
16895 			err = EINVAL;
16896 			goto done;
16897 		}
16898 
16899 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16900 		*ill_from_v6 = ipif_v6->ipif_ill;
16901 	}
16902 
16903 	err = 0;
16904 	dst_index = lifr->lifr_movetoindex;
16905 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16906 	    q, mp, ip_process_ioctl, &err);
16907 	if (err != 0) {
16908 		/*
16909 		 * There could be only v6.
16910 		 */
16911 		if (err != ENXIO)
16912 			goto done;
16913 		err = 0;
16914 	}
16915 
16916 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16917 	    q, mp, ip_process_ioctl, &err);
16918 	if (err != 0) {
16919 		if (err != ENXIO)
16920 			goto done;
16921 		if (*ill_to_v4 == NULL) {
16922 			err = ENXIO;
16923 			goto done;
16924 		}
16925 		err = 0;
16926 	}
16927 
16928 	/*
16929 	 * If we have something to MOVE i.e "from" not NULL,
16930 	 * "to" should be non-NULL.
16931 	 */
16932 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16933 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16934 		err = EINVAL;
16935 	}
16936 
16937 done:
16938 	if (ipif_v4 != NULL)
16939 		ipif_refrele(ipif_v4);
16940 	if (ipif_v6 != NULL)
16941 		ipif_refrele(ipif_v6);
16942 	return (err);
16943 }
16944 
16945 /*
16946  * FAILOVER and FAILBACK are modelled as MOVE operations.
16947  *
16948  * We don't check whether the MOVE is within the same group or
16949  * not, because this ioctl can be used as a generic mechanism
16950  * to failover from interface A to B, though things will function
16951  * only if they are really part of the same group. Moreover,
16952  * all ipifs may be down and hence temporarily out of the group.
16953  *
16954  * ipif's that need to be moved are first brought down; V4 ipifs are brought
16955  * down first and then V6.  For each we wait for the ipif's to become quiescent.
16956  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
16957  * have been deleted and there are no active references. Once quiescent the
16958  * ipif's are moved and brought up on the new ill.
16959  *
16960  * Normally the source ill and destination ill belong to the same IPMP group
16961  * and hence the same ipsq_t. In the event they don't belong to the same
16962  * same group the two ipsq's are first merged into one ipsq - that of the
16963  * to_ill. The multicast memberships on the source and destination ill cannot
16964  * change during the move operation since multicast joins/leaves also have to
16965  * execute on the same ipsq and are hence serialized.
16966  */
16967 /* ARGSUSED */
16968 int
16969 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16970     ip_ioctl_cmd_t *ipip, void *ifreq)
16971 {
16972 	ill_t *ill_to_v4 = NULL;
16973 	ill_t *ill_to_v6 = NULL;
16974 	ill_t *ill_from_v4 = NULL;
16975 	ill_t *ill_from_v6 = NULL;
16976 	int err = 0;
16977 
16978 	/*
16979 	 * setup from and to ill's, we can get EINPROGRESS only for
16980 	 * to_ill's.
16981 	 */
16982 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
16983 	    &ill_to_v4, &ill_to_v6);
16984 
16985 	if (err != 0) {
16986 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
16987 		goto done;
16988 	}
16989 
16990 	/*
16991 	 * nothing to do.
16992 	 */
16993 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
16994 		goto done;
16995 	}
16996 
16997 	/*
16998 	 * nothing to do.
16999 	 */
17000 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17001 		goto done;
17002 	}
17003 
17004 	/*
17005 	 * Mark the ill as changing.
17006 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17007 	 * in ill_up_ipifs in case of error they are cleared below.
17008 	 */
17009 
17010 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17011 	if (ill_from_v4 != NULL)
17012 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17013 	if (ill_from_v6 != NULL)
17014 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17015 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17016 
17017 	/*
17018 	 * Make sure that both src and dst are
17019 	 * in the same syncq group. If not make it happen.
17020 	 * We are not holding any locks because we are the writer
17021 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17022 	 * to protect to_ipsq against changing.
17023 	 */
17024 	if (ill_from_v4 != NULL) {
17025 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17026 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17027 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17028 			    NULL, mp, q);
17029 			goto err_ret;
17030 
17031 		}
17032 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17033 	} else {
17034 
17035 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17036 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17037 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17038 			    NULL, mp, q);
17039 			goto err_ret;
17040 
17041 		}
17042 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17043 	}
17044 
17045 	/*
17046 	 * Now that the ipsq's have been merged and we are the writer
17047 	 * lets mark to_ill as changing as well.
17048 	 */
17049 
17050 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17051 	if (ill_to_v4 != NULL)
17052 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17053 	if (ill_to_v6 != NULL)
17054 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17055 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17056 
17057 	/*
17058 	 * Its ok for us to proceed with the move even if
17059 	 * ill_pending_mp is non null on one of the from ill's as the reply
17060 	 * should not be looking at the ipif, it should only care about the
17061 	 * ill itself.
17062 	 */
17063 
17064 	/*
17065 	 * lets move ipv4 first.
17066 	 */
17067 	if (ill_from_v4 != NULL) {
17068 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17069 		ill_from_v4->ill_move_in_progress = B_TRUE;
17070 		ill_to_v4->ill_move_in_progress = B_TRUE;
17071 		ill_to_v4->ill_move_peer = ill_from_v4;
17072 		ill_from_v4->ill_move_peer = ill_to_v4;
17073 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17074 	}
17075 
17076 	/*
17077 	 * Now lets move ipv6.
17078 	 */
17079 	if (err == 0 && ill_from_v6 != NULL) {
17080 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17081 		ill_from_v6->ill_move_in_progress = B_TRUE;
17082 		ill_to_v6->ill_move_in_progress = B_TRUE;
17083 		ill_to_v6->ill_move_peer = ill_from_v6;
17084 		ill_from_v6->ill_move_peer = ill_to_v6;
17085 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17086 	}
17087 
17088 err_ret:
17089 	/*
17090 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17091 	 * moved to become quiescent.
17092 	 */
17093 	if (err == EINPROGRESS) {
17094 		goto done;
17095 	}
17096 
17097 	/*
17098 	 * if err is set ill_up_ipifs will not be called
17099 	 * lets clear the flags.
17100 	 */
17101 
17102 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17103 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17104 	/*
17105 	 * Some of the clearing may be redundant. But it is simple
17106 	 * not making any extra checks.
17107 	 */
17108 	if (ill_from_v6 != NULL) {
17109 		ill_from_v6->ill_move_in_progress = B_FALSE;
17110 		ill_from_v6->ill_move_peer = NULL;
17111 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17112 	}
17113 	if (ill_from_v4 != NULL) {
17114 		ill_from_v4->ill_move_in_progress = B_FALSE;
17115 		ill_from_v4->ill_move_peer = NULL;
17116 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17117 	}
17118 	if (ill_to_v6 != NULL) {
17119 		ill_to_v6->ill_move_in_progress = B_FALSE;
17120 		ill_to_v6->ill_move_peer = NULL;
17121 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17122 	}
17123 	if (ill_to_v4 != NULL) {
17124 		ill_to_v4->ill_move_in_progress = B_FALSE;
17125 		ill_to_v4->ill_move_peer = NULL;
17126 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17127 	}
17128 
17129 	/*
17130 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
17131 	 * Do this always to maintain proper state i.e even in case of errors.
17132 	 * As phyint_inactive looks at both v4 and v6 interfaces,
17133 	 * we need not call on both v4 and v6 interfaces.
17134 	 */
17135 	if (ill_from_v4 != NULL) {
17136 		if ((ill_from_v4->ill_phyint->phyint_flags &
17137 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17138 			phyint_inactive(ill_from_v4->ill_phyint);
17139 		}
17140 	} else if (ill_from_v6 != NULL) {
17141 		if ((ill_from_v6->ill_phyint->phyint_flags &
17142 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17143 			phyint_inactive(ill_from_v6->ill_phyint);
17144 		}
17145 	}
17146 
17147 	if (ill_to_v4 != NULL) {
17148 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17149 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17150 		}
17151 	} else if (ill_to_v6 != NULL) {
17152 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17153 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17154 		}
17155 	}
17156 
17157 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17158 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17159 
17160 no_err:
17161 	/*
17162 	 * lets bring the interfaces up on the to_ill.
17163 	 */
17164 	if (err == 0) {
17165 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
17166 		    q, mp);
17167 	}
17168 done:
17169 
17170 	if (ill_to_v4 != NULL) {
17171 		ill_refrele(ill_to_v4);
17172 	}
17173 	if (ill_to_v6 != NULL) {
17174 		ill_refrele(ill_to_v6);
17175 	}
17176 
17177 	return (err);
17178 }
17179 
17180 static void
17181 ill_dl_down(ill_t *ill)
17182 {
17183 	/*
17184 	 * The ill is down; unbind but stay attached since we're still
17185 	 * associated with a PPA.
17186 	 */
17187 	mblk_t	*mp = ill->ill_unbind_mp;
17188 
17189 	ill->ill_unbind_mp = NULL;
17190 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17191 	if (mp != NULL) {
17192 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17193 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17194 		    ill->ill_name));
17195 		mutex_enter(&ill->ill_lock);
17196 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
17197 		mutex_exit(&ill->ill_lock);
17198 		ill_dlpi_send(ill, mp);
17199 	}
17200 
17201 	/*
17202 	 * Toss all of our multicast memberships.  We could keep them, but
17203 	 * then we'd have to do bookkeeping of any joins and leaves performed
17204 	 * by the application while the the interface is down (we can't just
17205 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17206 	 * on a downed interface).
17207 	 */
17208 	ill_leave_multicast(ill);
17209 
17210 	mutex_enter(&ill->ill_lock);
17211 	ill->ill_dl_up = 0;
17212 	mutex_exit(&ill->ill_lock);
17213 }
17214 
17215 void
17216 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17217 {
17218 	union DL_primitives *dlp;
17219 	t_uscalar_t prim;
17220 
17221 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17222 
17223 	dlp = (union DL_primitives *)mp->b_rptr;
17224 	prim = dlp->dl_primitive;
17225 
17226 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17227 		dlpi_prim_str(prim), prim, ill->ill_name));
17228 
17229 	switch (prim) {
17230 	case DL_PHYS_ADDR_REQ:
17231 	{
17232 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17233 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17234 		break;
17235 	}
17236 	case DL_BIND_REQ:
17237 		mutex_enter(&ill->ill_lock);
17238 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
17239 		mutex_exit(&ill->ill_lock);
17240 		break;
17241 	}
17242 
17243 	ill->ill_dlpi_pending = prim;
17244 
17245 	/*
17246 	 * Some drivers send M_FLUSH up to IP as part of unbind
17247 	 * request.  When this M_FLUSH is sent back to the driver,
17248 	 * this can go after we send the detach request if the
17249 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17250 	 * to the M_FLUSH in ip_rput and locally generate another
17251 	 * M_FLUSH for the correctness.  This will get freed in
17252 	 * ip_wput_nondata.
17253 	 */
17254 	if (prim == DL_UNBIND_REQ)
17255 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17256 
17257 	putnext(ill->ill_wq, mp);
17258 }
17259 
17260 /*
17261  * Send a DLPI control message to the driver but make sure there
17262  * is only one outstanding message. Uses ill_dlpi_pending to tell
17263  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17264  * when an ACK or a NAK is received to process the next queued message.
17265  *
17266  * We don't protect ill_dlpi_pending with any lock. This is okay as
17267  * every place where its accessed, ip is exclusive while accessing
17268  * ill_dlpi_pending except when this function is called from ill_init()
17269  */
17270 void
17271 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17272 {
17273 	mblk_t **mpp;
17274 
17275 	ASSERT(IAM_WRITER_ILL(ill));
17276 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17277 
17278 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17279 		/* Must queue message. Tail insertion */
17280 		mpp = &ill->ill_dlpi_deferred;
17281 		while (*mpp != NULL)
17282 			mpp = &((*mpp)->b_next);
17283 
17284 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17285 		    ill->ill_name));
17286 
17287 		*mpp = mp;
17288 		return;
17289 	}
17290 
17291 	ill_dlpi_dispatch(ill, mp);
17292 }
17293 
17294 /*
17295  * Called when an DLPI control message has been acked or nacked to
17296  * send down the next queued message (if any).
17297  */
17298 void
17299 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17300 {
17301 	mblk_t *mp;
17302 
17303 	ASSERT(IAM_WRITER_ILL(ill));
17304 
17305 	ASSERT(prim != DL_PRIM_INVAL);
17306 	if (ill->ill_dlpi_pending != prim) {
17307 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17308 			(void) mi_strlog(ill->ill_rq, 1,
17309 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17310 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17311 			    dlpi_prim_str(prim), ill->ill_name);
17312 		} else {
17313 			(void) mi_strlog(ill->ill_rq, 1,
17314 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17315 			    "ill_dlpi_done: unexpected ack for %s from %s "
17316 			    "(expecting ack for %s)\n",
17317 			    dlpi_prim_str(prim), ill->ill_name,
17318 			    dlpi_prim_str(ill->ill_dlpi_pending));
17319 		}
17320 		return;
17321 	}
17322 
17323 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17324 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17325 
17326 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17327 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17328 		return;
17329 	}
17330 
17331 	ill->ill_dlpi_deferred = mp->b_next;
17332 	mp->b_next = NULL;
17333 
17334 	ill_dlpi_dispatch(ill, mp);
17335 }
17336 
17337 void
17338 conn_delete_ire(conn_t *connp, caddr_t arg)
17339 {
17340 	ipif_t	*ipif = (ipif_t *)arg;
17341 	ire_t	*ire;
17342 
17343 	/*
17344 	 * Look at the cached ires on conns which has pointers to ipifs.
17345 	 * We just call ire_refrele which clears up the reference
17346 	 * to ire. Called when a conn closes. Also called from ipif_free
17347 	 * to cleanup indirect references to the stale ipif via the cached ire.
17348 	 */
17349 	mutex_enter(&connp->conn_lock);
17350 	ire = connp->conn_ire_cache;
17351 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17352 		connp->conn_ire_cache = NULL;
17353 		mutex_exit(&connp->conn_lock);
17354 		IRE_REFRELE_NOTR(ire);
17355 		return;
17356 	}
17357 	mutex_exit(&connp->conn_lock);
17358 
17359 }
17360 
17361 /*
17362  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17363  * of IREs. Those IREs may have been previously cached in the conn structure.
17364  * This ipcl_walk() walker function releases all references to such IREs based
17365  * on the condemned flag.
17366  */
17367 /* ARGSUSED */
17368 void
17369 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17370 {
17371 	ire_t	*ire;
17372 
17373 	mutex_enter(&connp->conn_lock);
17374 	ire = connp->conn_ire_cache;
17375 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17376 		connp->conn_ire_cache = NULL;
17377 		mutex_exit(&connp->conn_lock);
17378 		IRE_REFRELE_NOTR(ire);
17379 		return;
17380 	}
17381 	mutex_exit(&connp->conn_lock);
17382 }
17383 
17384 /*
17385  * Take down a specific interface, but don't lose any information about it.
17386  * Also delete interface from its interface group (ifgrp).
17387  * (Always called as writer.)
17388  * This function goes through the down sequence even if the interface is
17389  * already down. There are 2 reasons.
17390  * a. Currently we permit interface routes that depend on down interfaces
17391  *    to be added. This behaviour itself is questionable. However it appears
17392  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17393  *    time. We go thru the cleanup in order to remove these routes.
17394  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17395  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17396  *    down, but we need to cleanup i.e. do ill_dl_down and
17397  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17398  *
17399  * IP-MT notes:
17400  *
17401  * Model of reference to interfaces.
17402  *
17403  * The following members in ipif_t track references to the ipif.
17404  *	int     ipif_refcnt;    Active reference count
17405  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17406  * The following members in ill_t track references to the ill.
17407  *	int             ill_refcnt;     active refcnt
17408  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17409  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17410  *
17411  * Reference to an ipif or ill can be obtained in any of the following ways.
17412  *
17413  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17414  * Pointers to ipif / ill from other data structures viz ire and conn.
17415  * Implicit reference to the ipif / ill by holding a reference to the ire.
17416  *
17417  * The ipif/ill lookup functions return a reference held ipif / ill.
17418  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17419  * This is a purely dynamic reference count associated with threads holding
17420  * references to the ipif / ill. Pointers from other structures do not
17421  * count towards this reference count.
17422  *
17423  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17424  * ipif/ill. This is incremented whenever a new ire is created referencing the
17425  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17426  * actually added to the ire hash table. The count is decremented in
17427  * ire_inactive where the ire is destroyed.
17428  *
17429  * nce's reference ill's thru nce_ill and the count of nce's associated with
17430  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17431  * ndp_add() where the nce is actually added to the table. Similarly it is
17432  * decremented in ndp_inactive where the nce is destroyed.
17433  *
17434  * Flow of ioctls involving interface down/up
17435  *
17436  * The following is the sequence of an attempt to set some critical flags on an
17437  * up interface.
17438  * ip_sioctl_flags
17439  * ipif_down
17440  * wait for ipif to be quiescent
17441  * ipif_down_tail
17442  * ip_sioctl_flags_tail
17443  *
17444  * All set ioctls that involve down/up sequence would have a skeleton similar
17445  * to the above. All the *tail functions are called after the refcounts have
17446  * dropped to the appropriate values.
17447  *
17448  * The mechanism to quiesce an ipif is as follows.
17449  *
17450  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17451  * on the ipif. Callers either pass a flag requesting wait or the lookup
17452  *  functions will return NULL.
17453  *
17454  * Delete all ires referencing this ipif
17455  *
17456  * Any thread attempting to do an ipif_refhold on an ipif that has been
17457  * obtained thru a cached pointer will first make sure that
17458  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17459  * increment the refcount.
17460  *
17461  * The above guarantees that the ipif refcount will eventually come down to
17462  * zero and the ipif will quiesce, once all threads that currently hold a
17463  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17464  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17465  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17466  * drop to zero.
17467  *
17468  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17469  *
17470  * Threads trying to lookup an ipif or ill can pass a flag requesting
17471  * wait and restart if the ipif / ill cannot be looked up currently.
17472  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17473  * failure if the ipif is currently undergoing an exclusive operation, and
17474  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17475  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17476  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17477  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17478  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17479  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17480  * until we release the ipsq_lock, even though the the ill/ipif state flags
17481  * can change after we drop the ill_lock.
17482  *
17483  * An attempt to send out a packet using an ipif that is currently
17484  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17485  * operation and restart it later when the exclusive condition on the ipif ends.
17486  * This is an example of not passing the wait flag to the lookup functions. For
17487  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17488  * out a multicast packet on that ipif will fail while the ipif is
17489  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17490  * currently IPIF_CHANGING will also fail.
17491  */
17492 int
17493 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17494 {
17495 	ill_t		*ill = ipif->ipif_ill;
17496 	phyint_t	*phyi;
17497 	conn_t		*connp;
17498 	boolean_t	success;
17499 	boolean_t	ipif_was_up = B_FALSE;
17500 
17501 	ASSERT(IAM_WRITER_IPIF(ipif));
17502 
17503 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17504 
17505 	if (ipif->ipif_flags & IPIF_UP) {
17506 		mutex_enter(&ill->ill_lock);
17507 		ipif->ipif_flags &= ~IPIF_UP;
17508 		ASSERT(ill->ill_ipif_up_count > 0);
17509 		--ill->ill_ipif_up_count;
17510 		mutex_exit(&ill->ill_lock);
17511 		ipif_was_up = B_TRUE;
17512 		/* Update status in SCTP's list */
17513 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17514 	}
17515 
17516 	/*
17517 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17518 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17519 	 * know not to rejoin when the interface is brought back up).
17520 	 */
17521 	if (ipif->ipif_isv6)
17522 		ipif_multicast_down(ipif);
17523 	/*
17524 	 * Remove from the mapping for __sin6_src_id. We insert only
17525 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17526 	 * stored as mapped addresses, we need to check for mapped
17527 	 * INADDR_ANY also.
17528 	 */
17529 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17530 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17531 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17532 		int err;
17533 
17534 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17535 		    ipif->ipif_zoneid);
17536 		if (err != 0) {
17537 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17538 		}
17539 	}
17540 
17541 	/*
17542 	 * Before we delete the ill from the group (if any), we need
17543 	 * to make sure that we delete all the routes dependent on
17544 	 * this and also any ipifs dependent on this ipif for
17545 	 * source address. We need to do before we delete from
17546 	 * the group because
17547 	 *
17548 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17549 	 *
17550 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17551 	 *    for re-doing source address selection. Note that
17552 	 *    ipif_select_source[_v6] called from
17553 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17554 	 *    because we have already marked down here i.e cleared
17555 	 *    IPIF_UP.
17556 	 */
17557 	if (ipif->ipif_isv6)
17558 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17559 	else
17560 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17561 
17562 	/*
17563 	 * Need to add these also to be saved and restored when the
17564 	 * ipif is brought down and up
17565 	 */
17566 	mutex_enter(&ire_mrtun_lock);
17567 	if (ire_mrtun_count != 0) {
17568 		mutex_exit(&ire_mrtun_lock);
17569 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17570 		    (char *)ipif, NULL);
17571 	} else {
17572 		mutex_exit(&ire_mrtun_lock);
17573 	}
17574 
17575 	mutex_enter(&ire_srcif_table_lock);
17576 	if (ire_srcif_table_count > 0) {
17577 		mutex_exit(&ire_srcif_table_lock);
17578 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17579 	} else {
17580 		mutex_exit(&ire_srcif_table_lock);
17581 	}
17582 
17583 	/*
17584 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17585 	 * ires have been deleted above. Otherwise a thread could end up
17586 	 * caching an ire in a conn after we have finished the cleanup of the
17587 	 * conn. The caching is done after making sure that the ire is not yet
17588 	 * condemned. Also documented in the block comment above ip_output
17589 	 */
17590 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17591 	/* Also, delete the ires cached in SCTP */
17592 	sctp_ire_cache_flush(ipif);
17593 
17594 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17595 	nattymod_clean_ipif(ipif);
17596 
17597 	/*
17598 	 * Update any other ipifs which have used "our" local address as
17599 	 * a source address. This entails removing and recreating IRE_INTERFACE
17600 	 * entries for such ipifs.
17601 	 */
17602 	if (ipif->ipif_isv6)
17603 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17604 	else
17605 		ipif_update_other_ipifs(ipif, ill->ill_group);
17606 
17607 	if (ipif_was_up) {
17608 		/*
17609 		 * Check whether it is last ipif to leave this group.
17610 		 * If this is the last ipif to leave, we should remove
17611 		 * this ill from the group as ipif_select_source will not
17612 		 * be able to find any useful ipifs if this ill is selected
17613 		 * for load balancing.
17614 		 *
17615 		 * For nameless groups, we should call ifgrp_delete if this
17616 		 * belongs to some group. As this ipif is going down, we may
17617 		 * need to reconstruct groups.
17618 		 */
17619 		phyi = ill->ill_phyint;
17620 		/*
17621 		 * If the phyint_groupname_len is 0, it may or may not
17622 		 * be in the nameless group. If the phyint_groupname_len is
17623 		 * not 0, then this ill should be part of some group.
17624 		 * As we always insert this ill in the group if
17625 		 * phyint_groupname_len is not zero when the first ipif
17626 		 * comes up (in ipif_up_done), it should be in a group
17627 		 * when the namelen is not 0.
17628 		 *
17629 		 * NOTE : When we delete the ill from the group,it will
17630 		 * blow away all the IRE_CACHES pointing either at this ipif or
17631 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17632 		 * should be pointing at this ill.
17633 		 */
17634 		ASSERT(phyi->phyint_groupname_len == 0 ||
17635 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17636 
17637 		if (phyi->phyint_groupname_len != 0) {
17638 			if (ill->ill_ipif_up_count == 0)
17639 				illgrp_delete(ill);
17640 		}
17641 
17642 		/*
17643 		 * If we have deleted some of the broadcast ires associated
17644 		 * with this ipif, we need to re-nominate somebody else if
17645 		 * the ires that we deleted were the nominated ones.
17646 		 */
17647 		if (ill->ill_group != NULL && !ill->ill_isv6)
17648 			ipif_renominate_bcast(ipif);
17649 	}
17650 
17651 	if (ipif->ipif_isv6)
17652 		ipif_ndp_down(ipif);
17653 
17654 	/*
17655 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17656 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17657 	 * and ill_delete -> ipif_free -> ipif_down
17658 	 */
17659 	if (mp == NULL) {
17660 		ASSERT(q == NULL);
17661 		return (0);
17662 	}
17663 
17664 	if (CONN_Q(q)) {
17665 		connp = Q_TO_CONN(q);
17666 		mutex_enter(&connp->conn_lock);
17667 	} else {
17668 		connp = NULL;
17669 	}
17670 	mutex_enter(&ill->ill_lock);
17671 	/*
17672 	 * Are there any ire's pointing to this ipif that are still active ?
17673 	 * If this is the last ipif going down, are there any ire's pointing
17674 	 * to this ill that are still active ?
17675 	 */
17676 	if (ipif_is_quiescent(ipif)) {
17677 		mutex_exit(&ill->ill_lock);
17678 		if (connp != NULL)
17679 			mutex_exit(&connp->conn_lock);
17680 		return (0);
17681 	}
17682 
17683 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17684 	    ill->ill_name, (void *)ill));
17685 	/*
17686 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17687 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17688 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17689 	 */
17690 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17691 	if (!success) {
17692 		/* The conn is closing. So just return */
17693 		ASSERT(connp != NULL);
17694 		mutex_exit(&ill->ill_lock);
17695 		mutex_exit(&connp->conn_lock);
17696 		return (EINTR);
17697 	}
17698 
17699 	mutex_exit(&ill->ill_lock);
17700 	if (connp != NULL)
17701 		mutex_exit(&connp->conn_lock);
17702 	return (EINPROGRESS);
17703 }
17704 
17705 static void
17706 ipif_down_tail(ipif_t *ipif)
17707 {
17708 	ill_t	*ill = ipif->ipif_ill;
17709 
17710 	/*
17711 	 * Skip any loopback interface (null wq).
17712 	 * If this is the last logical interface on the ill
17713 	 * have ill_dl_down tell the driver we are gone (unbind)
17714 	 * Note that lun 0 can ipif_down even though
17715 	 * there are other logical units that are up.
17716 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17717 	 */
17718 	if (ipif->ipif_ill->ill_wq != NULL) {
17719 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17720 		    ill->ill_dl_up) {
17721 			ill_dl_down(ill);
17722 		}
17723 	}
17724 	ill->ill_logical_down = 0;
17725 
17726 	/*
17727 	 * Have to be after removing the routes in ipif_down_delete_ire.
17728 	 */
17729 	if (ipif->ipif_isv6) {
17730 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17731 			ipif_arp_down(ipif);
17732 	} else {
17733 		ipif_arp_down(ipif);
17734 	}
17735 
17736 	ip_rts_ifmsg(ipif);
17737 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17738 }
17739 
17740 /*
17741  * Bring interface logically down without bringing the physical interface
17742  * down e.g. when the netmask is changed. This avoids long lasting link
17743  * negotiations between an ethernet interface and a certain switches.
17744  */
17745 static int
17746 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17747 {
17748 	/*
17749 	 * The ill_logical_down flag is a transient flag. It is set here
17750 	 * and is cleared once the down has completed in ipif_down_tail.
17751 	 * This flag does not indicate whether the ill stream is in the
17752 	 * DL_BOUND state with the driver. Instead this flag is used by
17753 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17754 	 * the driver. The state of the ill stream i.e. whether it is
17755 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17756 	 */
17757 	ipif->ipif_ill->ill_logical_down = 1;
17758 	return (ipif_down(ipif, q, mp));
17759 }
17760 
17761 /*
17762  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17763  * If the usesrc client ILL is already part of a usesrc group or not,
17764  * in either case a ire_stq with the matching usesrc client ILL will
17765  * locate the IRE's that need to be deleted. We want IREs to be created
17766  * with the new source address.
17767  */
17768 static void
17769 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17770 {
17771 	ill_t	*ucill = (ill_t *)ill_arg;
17772 
17773 	ASSERT(IAM_WRITER_ILL(ucill));
17774 
17775 	if (ire->ire_stq == NULL)
17776 		return;
17777 
17778 	if ((ire->ire_type == IRE_CACHE) &&
17779 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17780 		ire_delete(ire);
17781 }
17782 
17783 /*
17784  * ire_walk routine to delete every IRE dependent on the interface
17785  * address that is going down.	(Always called as writer.)
17786  * Works for both v4 and v6.
17787  * In addition for checking for ire_ipif matches it also checks for
17788  * IRE_CACHE entries which have the same source address as the
17789  * disappearing ipif since ipif_select_source might have picked
17790  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17791  * care of any IRE_INTERFACE with the disappearing source address.
17792  */
17793 static void
17794 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17795 {
17796 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17797 	ill_t *ire_ill;
17798 	ill_t *ipif_ill;
17799 
17800 	ASSERT(IAM_WRITER_IPIF(ipif));
17801 	if (ire->ire_ipif == NULL)
17802 		return;
17803 
17804 	/*
17805 	 * For IPv4, we derive source addresses for an IRE from ipif's
17806 	 * belonging to the same IPMP group as the IRE's outgoing
17807 	 * interface.  If an IRE's outgoing interface isn't in the
17808 	 * same IPMP group as a particular ipif, then that ipif
17809 	 * couldn't have been used as a source address for this IRE.
17810 	 *
17811 	 * For IPv6, source addresses are only restricted to the IPMP group
17812 	 * if the IRE is for a link-local address or a multicast address.
17813 	 * Otherwise, source addresses for an IRE can be chosen from
17814 	 * interfaces other than the the outgoing interface for that IRE.
17815 	 *
17816 	 * For source address selection details, see ipif_select_source()
17817 	 * and ipif_select_source_v6().
17818 	 */
17819 	if (ire->ire_ipversion == IPV4_VERSION ||
17820 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17821 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17822 		ire_ill = ire->ire_ipif->ipif_ill;
17823 		ipif_ill = ipif->ipif_ill;
17824 
17825 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17826 			return;
17827 		}
17828 	}
17829 
17830 
17831 	if (ire->ire_ipif != ipif) {
17832 		/*
17833 		 * Look for a matching source address.
17834 		 */
17835 		if (ire->ire_type != IRE_CACHE)
17836 			return;
17837 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17838 			return;
17839 
17840 		if (ire->ire_ipversion == IPV4_VERSION) {
17841 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17842 				return;
17843 		} else {
17844 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17845 			    &ipif->ipif_v6lcl_addr))
17846 				return;
17847 		}
17848 		ire_delete(ire);
17849 		return;
17850 	}
17851 	/*
17852 	 * ire_delete() will do an ire_flush_cache which will delete
17853 	 * all ire_ipif matches
17854 	 */
17855 	ire_delete(ire);
17856 }
17857 
17858 /*
17859  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17860  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17861  * 2) when an interface is brought up or down (on that ill).
17862  * This ensures that the IRE_CACHE entries don't retain stale source
17863  * address selection results.
17864  */
17865 void
17866 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17867 {
17868 	ill_t	*ill = (ill_t *)ill_arg;
17869 	ill_t	*ipif_ill;
17870 
17871 	ASSERT(IAM_WRITER_ILL(ill));
17872 	/*
17873 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17874 	 * Hence this should be IRE_CACHE.
17875 	 */
17876 	ASSERT(ire->ire_type == IRE_CACHE);
17877 
17878 	/*
17879 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17880 	 * We are only interested in IRE_CACHES that has borrowed
17881 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17882 	 * for which we need to look at ire_ipif->ipif_ill match
17883 	 * with ill.
17884 	 */
17885 	ASSERT(ire->ire_ipif != NULL);
17886 	ipif_ill = ire->ire_ipif->ipif_ill;
17887 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17888 	    ipif_ill->ill_group == ill->ill_group)) {
17889 		ire_delete(ire);
17890 	}
17891 }
17892 
17893 /*
17894  * Delete all the ire whose stq references ill_arg.
17895  */
17896 static void
17897 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17898 {
17899 	ill_t	*ill = (ill_t *)ill_arg;
17900 	ill_t	*ire_ill;
17901 
17902 	ASSERT(IAM_WRITER_ILL(ill));
17903 	/*
17904 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17905 	 * Hence this should be IRE_CACHE.
17906 	 */
17907 	ASSERT(ire->ire_type == IRE_CACHE);
17908 
17909 	/*
17910 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17911 	 * matches ill. We are only interested in IRE_CACHES that
17912 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17913 	 * filtering here.
17914 	 */
17915 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17916 
17917 	if (ire_ill == ill)
17918 		ire_delete(ire);
17919 }
17920 
17921 /*
17922  * This is called when an ill leaves the group. We want to delete
17923  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17924  * pointing at ill.
17925  */
17926 static void
17927 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17928 {
17929 	ill_t	*ill = (ill_t *)ill_arg;
17930 
17931 	ASSERT(IAM_WRITER_ILL(ill));
17932 	ASSERT(ill->ill_group == NULL);
17933 	/*
17934 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17935 	 * Hence this should be IRE_CACHE.
17936 	 */
17937 	ASSERT(ire->ire_type == IRE_CACHE);
17938 	/*
17939 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17940 	 * matches ill. We are interested in both.
17941 	 */
17942 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17943 	    (ire->ire_ipif->ipif_ill == ill));
17944 
17945 	ire_delete(ire);
17946 }
17947 
17948 /*
17949  * Initiate deallocate of an IPIF. Always called as writer. Called by
17950  * ill_delete or ip_sioctl_removeif.
17951  */
17952 static void
17953 ipif_free(ipif_t *ipif)
17954 {
17955 	ASSERT(IAM_WRITER_IPIF(ipif));
17956 
17957 	/* Remove conn references */
17958 	reset_conn_ipif(ipif);
17959 
17960 	/*
17961 	 * Make sure we have valid net and subnet broadcast ire's for the
17962 	 * other ipif's which share them with this ipif.
17963 	 */
17964 	if (!ipif->ipif_isv6)
17965 		ipif_check_bcast_ires(ipif);
17966 
17967 	/*
17968 	 * Take down the interface. We can be called either from ill_delete
17969 	 * or from ip_sioctl_removeif.
17970 	 */
17971 	(void) ipif_down(ipif, NULL, NULL);
17972 
17973 	rw_enter(&ill_g_lock, RW_WRITER);
17974 	/* Remove pointers to this ill in the multicast routing tables */
17975 	reset_mrt_vif_ipif(ipif);
17976 	rw_exit(&ill_g_lock);
17977 }
17978 
17979 static void
17980 ipif_free_tail(ipif_t *ipif)
17981 {
17982 	mblk_t	*mp;
17983 	ipif_t	**ipifp;
17984 
17985 	/*
17986 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
17987 	 */
17988 	mutex_enter(&ipif->ipif_saved_ire_lock);
17989 	mp = ipif->ipif_saved_ire_mp;
17990 	ipif->ipif_saved_ire_mp = NULL;
17991 	mutex_exit(&ipif->ipif_saved_ire_lock);
17992 	freemsg(mp);
17993 
17994 	/*
17995 	 * Need to hold both ill_g_lock and ill_lock while
17996 	 * inserting or removing an ipif from the linked list
17997 	 * of ipifs hanging off the ill.
17998 	 */
17999 	rw_enter(&ill_g_lock, RW_WRITER);
18000 	/*
18001 	 * Remove all multicast memberships on the interface now.
18002 	 * This removes IPv4 multicast memberships joined within
18003 	 * the kernel as ipif_down does not do ipif_multicast_down
18004 	 * for IPv4. IPv6 is not handled here as the multicast memberships
18005 	 * are based on ill and not on ipif.
18006 	 */
18007 	ilm_free(ipif);
18008 
18009 	/*
18010 	 * Since we held the ill_g_lock while doing the ilm_free above,
18011 	 * we can assert the ilms were really deleted and not just marked
18012 	 * ILM_DELETED.
18013 	 */
18014 	ASSERT(ilm_walk_ipif(ipif) == 0);
18015 
18016 
18017 	IPIF_TRACE_CLEANUP(ipif);
18018 
18019 	/* Ask SCTP to take it out of it list */
18020 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18021 
18022 	mutex_enter(&ipif->ipif_ill->ill_lock);
18023 	/* Get it out of the ILL interface list. */
18024 	ipifp = &ipif->ipif_ill->ill_ipif;
18025 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
18026 		if (*ipifp == ipif) {
18027 			*ipifp = ipif->ipif_next;
18028 			break;
18029 		}
18030 	}
18031 
18032 	mutex_exit(&ipif->ipif_ill->ill_lock);
18033 	rw_exit(&ill_g_lock);
18034 
18035 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18036 	/* Free the memory. */
18037 	mi_free((char *)ipif);
18038 }
18039 
18040 /*
18041  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
18042  * "ill_name" otherwise.
18043  */
18044 char *
18045 ipif_get_name(const ipif_t *ipif, char *buf, int len)
18046 {
18047 	char	lbuf[32];
18048 	char	*name;
18049 	size_t	name_len;
18050 
18051 	buf[0] = '\0';
18052 	if (!ipif)
18053 		return (buf);
18054 	name = ipif->ipif_ill->ill_name;
18055 	name_len = ipif->ipif_ill->ill_name_length;
18056 	if (ipif->ipif_id != 0) {
18057 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
18058 		    ipif->ipif_id);
18059 		name = lbuf;
18060 		name_len = mi_strlen(name) + 1;
18061 	}
18062 	len -= 1;
18063 	buf[len] = '\0';
18064 	len = MIN(len, name_len);
18065 	bcopy(name, buf, len);
18066 	return (buf);
18067 }
18068 
18069 /*
18070  * Find an IPIF based on the name passed in.  Names can be of the
18071  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
18072  * The <phys> string can have forms like <dev><#> (e.g., le0),
18073  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
18074  * When there is no colon, the implied unit id is zero. <phys> must
18075  * correspond to the name of an ILL.  (May be called as writer.)
18076  */
18077 static ipif_t *
18078 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
18079     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
18080     mblk_t *mp, ipsq_func_t func, int *error)
18081 {
18082 	char	*cp;
18083 	char	*endp;
18084 	long	id;
18085 	ill_t	*ill;
18086 	ipif_t	*ipif;
18087 	uint_t	ire_type;
18088 	boolean_t did_alloc = B_FALSE;
18089 	ipsq_t	*ipsq;
18090 
18091 	if (error != NULL)
18092 		*error = 0;
18093 
18094 	/*
18095 	 * If the caller wants to us to create the ipif, make sure we have a
18096 	 * valid zoneid
18097 	 */
18098 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
18099 
18100 	if (namelen == 0) {
18101 		if (error != NULL)
18102 			*error = ENXIO;
18103 		return (NULL);
18104 	}
18105 
18106 	*exists = B_FALSE;
18107 	/* Look for a colon in the name. */
18108 	endp = &name[namelen];
18109 	for (cp = endp; --cp > name; ) {
18110 		if (*cp == IPIF_SEPARATOR_CHAR)
18111 			break;
18112 	}
18113 
18114 	if (*cp == IPIF_SEPARATOR_CHAR) {
18115 		/*
18116 		 * Reject any non-decimal aliases for logical
18117 		 * interfaces. Aliases with leading zeroes
18118 		 * are also rejected as they introduce ambiguity
18119 		 * in the naming of the interfaces.
18120 		 * In order to confirm with existing semantics,
18121 		 * and to not break any programs/script relying
18122 		 * on that behaviour, if<0>:0 is considered to be
18123 		 * a valid interface.
18124 		 *
18125 		 * If alias has two or more digits and the first
18126 		 * is zero, fail.
18127 		 */
18128 		if (&cp[2] < endp && cp[1] == '0')
18129 			return (NULL);
18130 	}
18131 
18132 	if (cp <= name) {
18133 		cp = endp;
18134 	} else {
18135 		*cp = '\0';
18136 	}
18137 
18138 	/*
18139 	 * Look up the ILL, based on the portion of the name
18140 	 * before the slash. ill_lookup_on_name returns a held ill.
18141 	 * Temporary to check whether ill exists already. If so
18142 	 * ill_lookup_on_name will clear it.
18143 	 */
18144 	ill = ill_lookup_on_name(name, do_alloc, isv6,
18145 	    q, mp, func, error, &did_alloc);
18146 	if (cp != endp)
18147 		*cp = IPIF_SEPARATOR_CHAR;
18148 	if (ill == NULL)
18149 		return (NULL);
18150 
18151 	/* Establish the unit number in the name. */
18152 	id = 0;
18153 	if (cp < endp && *endp == '\0') {
18154 		/* If there was a colon, the unit number follows. */
18155 		cp++;
18156 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
18157 			ill_refrele(ill);
18158 			if (error != NULL)
18159 				*error = ENXIO;
18160 			return (NULL);
18161 		}
18162 	}
18163 
18164 	GRAB_CONN_LOCK(q);
18165 	mutex_enter(&ill->ill_lock);
18166 	/* Now see if there is an IPIF with this unit number. */
18167 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
18168 		if (ipif->ipif_id == id) {
18169 			if (zoneid != ALL_ZONES &&
18170 			    zoneid != ipif->ipif_zoneid &&
18171 			    ipif->ipif_zoneid != ALL_ZONES) {
18172 				mutex_exit(&ill->ill_lock);
18173 				RELEASE_CONN_LOCK(q);
18174 				ill_refrele(ill);
18175 				if (error != NULL)
18176 					*error = ENXIO;
18177 				return (NULL);
18178 			}
18179 			/*
18180 			 * The block comment at the start of ipif_down
18181 			 * explains the use of the macros used below
18182 			 */
18183 			if (IPIF_CAN_LOOKUP(ipif)) {
18184 				ipif_refhold_locked(ipif);
18185 				mutex_exit(&ill->ill_lock);
18186 				if (!did_alloc)
18187 					*exists = B_TRUE;
18188 				/*
18189 				 * Drop locks before calling ill_refrele
18190 				 * since it can potentially call into
18191 				 * ipif_ill_refrele_tail which can end up
18192 				 * in trying to acquire any lock.
18193 				 */
18194 				RELEASE_CONN_LOCK(q);
18195 				ill_refrele(ill);
18196 				return (ipif);
18197 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18198 				ipsq = ill->ill_phyint->phyint_ipsq;
18199 				mutex_enter(&ipsq->ipsq_lock);
18200 				mutex_exit(&ill->ill_lock);
18201 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18202 				mutex_exit(&ipsq->ipsq_lock);
18203 				RELEASE_CONN_LOCK(q);
18204 				ill_refrele(ill);
18205 				*error = EINPROGRESS;
18206 				return (NULL);
18207 			}
18208 		}
18209 	}
18210 	RELEASE_CONN_LOCK(q);
18211 
18212 	if (!do_alloc) {
18213 		mutex_exit(&ill->ill_lock);
18214 		ill_refrele(ill);
18215 		if (error != NULL)
18216 			*error = ENXIO;
18217 		return (NULL);
18218 	}
18219 
18220 	/*
18221 	 * If none found, atomically allocate and return a new one.
18222 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18223 	 * to support "receive only" use of lo0:1 etc. as is still done
18224 	 * below as an initial guess.
18225 	 * However, this is now likely to be overriden later in ipif_up_done()
18226 	 * when we know for sure what address has been configured on the
18227 	 * interface, since we might have more than one loopback interface
18228 	 * with a loopback address, e.g. in the case of zones, and all the
18229 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18230 	 */
18231 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18232 		ire_type = IRE_LOOPBACK;
18233 	else
18234 		ire_type = IRE_LOCAL;
18235 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18236 	if (ipif != NULL)
18237 		ipif_refhold_locked(ipif);
18238 	else if (error != NULL)
18239 		*error = ENOMEM;
18240 	mutex_exit(&ill->ill_lock);
18241 	ill_refrele(ill);
18242 	return (ipif);
18243 }
18244 
18245 /*
18246  * This routine is called whenever a new address comes up on an ipif.  If
18247  * we are configured to respond to address mask requests, then we are supposed
18248  * to broadcast an address mask reply at this time.  This routine is also
18249  * called if we are already up, but a netmask change is made.  This is legal
18250  * but might not make the system manager very popular.	(May be called
18251  * as writer.)
18252  */
18253 static void
18254 ipif_mask_reply(ipif_t *ipif)
18255 {
18256 	icmph_t	*icmph;
18257 	ipha_t	*ipha;
18258 	mblk_t	*mp;
18259 
18260 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18261 
18262 	if (!ip_respond_to_address_mask_broadcast)
18263 		return;
18264 
18265 	/* ICMP mask reply is IPv4 only */
18266 	ASSERT(!ipif->ipif_isv6);
18267 	/* ICMP mask reply is not for a loopback interface */
18268 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18269 
18270 	mp = allocb(REPLY_LEN, BPRI_HI);
18271 	if (mp == NULL)
18272 		return;
18273 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18274 
18275 	ipha = (ipha_t *)mp->b_rptr;
18276 	bzero(ipha, REPLY_LEN);
18277 	*ipha = icmp_ipha;
18278 	ipha->ipha_ttl = ip_broadcast_ttl;
18279 	ipha->ipha_src = ipif->ipif_src_addr;
18280 	ipha->ipha_dst = ipif->ipif_brd_addr;
18281 	ipha->ipha_length = htons(REPLY_LEN);
18282 	ipha->ipha_ident = 0;
18283 
18284 	icmph = (icmph_t *)&ipha[1];
18285 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18286 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18287 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18288 	if (icmph->icmph_checksum == 0)
18289 		icmph->icmph_checksum = 0xffff;
18290 
18291 	put(ipif->ipif_wq, mp);
18292 
18293 #undef	REPLY_LEN
18294 }
18295 
18296 /*
18297  * When the mtu in the ipif changes, we call this routine through ire_walk
18298  * to update all the relevant IREs.
18299  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18300  */
18301 static void
18302 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18303 {
18304 	ipif_t *ipif = (ipif_t *)ipif_arg;
18305 
18306 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18307 		return;
18308 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18309 }
18310 
18311 /*
18312  * When the mtu in the ill changes, we call this routine through ire_walk
18313  * to update all the relevant IREs.
18314  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18315  */
18316 void
18317 ill_mtu_change(ire_t *ire, char *ill_arg)
18318 {
18319 	ill_t	*ill = (ill_t *)ill_arg;
18320 
18321 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18322 		return;
18323 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18324 }
18325 
18326 /*
18327  * Join the ipif specific multicast groups.
18328  * Must be called after a mapping has been set up in the resolver.  (Always
18329  * called as writer.)
18330  */
18331 void
18332 ipif_multicast_up(ipif_t *ipif)
18333 {
18334 	int err, index;
18335 	ill_t *ill;
18336 
18337 	ASSERT(IAM_WRITER_IPIF(ipif));
18338 
18339 	ill = ipif->ipif_ill;
18340 	index = ill->ill_phyint->phyint_ifindex;
18341 
18342 	ip1dbg(("ipif_multicast_up\n"));
18343 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18344 		return;
18345 
18346 	if (ipif->ipif_isv6) {
18347 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18348 			return;
18349 
18350 		/* Join the all hosts multicast address */
18351 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18352 		/*
18353 		 * Passing B_TRUE means we have to join the multicast
18354 		 * membership on this interface even though this is
18355 		 * FAILED. If we join on a different one in the group,
18356 		 * we will not be able to delete the membership later
18357 		 * as we currently don't track where we join when we
18358 		 * join within the kernel unlike applications where
18359 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18360 		 * for more on this.
18361 		 */
18362 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18363 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18364 		if (err != 0) {
18365 			ip0dbg(("ipif_multicast_up: "
18366 			    "all_hosts_mcast failed %d\n",
18367 			    err));
18368 			return;
18369 		}
18370 		/*
18371 		 * Enable multicast for the solicited node multicast address
18372 		 */
18373 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18374 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18375 
18376 			ipv6_multi.s6_addr32[3] |=
18377 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18378 
18379 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18380 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18381 			    NULL);
18382 			if (err != 0) {
18383 				ip0dbg(("ipif_multicast_up: solicited MC"
18384 				    " failed %d\n", err));
18385 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18386 				    ill, ill->ill_phyint->phyint_ifindex,
18387 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18388 				return;
18389 			}
18390 		}
18391 	} else {
18392 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18393 			return;
18394 
18395 		/* Join the all hosts multicast address */
18396 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18397 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18398 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18399 		if (err) {
18400 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18401 			return;
18402 		}
18403 	}
18404 	ipif->ipif_multicast_up = 1;
18405 }
18406 
18407 /*
18408  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18409  * any explicit memberships are blown away in ill_leave_multicast() when the
18410  * ill is brought down.
18411  */
18412 static void
18413 ipif_multicast_down(ipif_t *ipif)
18414 {
18415 	int err;
18416 
18417 	ASSERT(IAM_WRITER_IPIF(ipif));
18418 
18419 	ip1dbg(("ipif_multicast_down\n"));
18420 	if (!ipif->ipif_multicast_up)
18421 		return;
18422 
18423 	ASSERT(ipif->ipif_isv6);
18424 
18425 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18426 
18427 	/*
18428 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18429 	 * we should look for ilms on this ill rather than the ones that have
18430 	 * been failed over here.  They are here temporarily. As
18431 	 * ipif_multicast_up has joined on this ill, we should delete only
18432 	 * from this ill.
18433 	 */
18434 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18435 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18436 	    B_TRUE, B_TRUE);
18437 	if (err != 0) {
18438 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18439 		    err));
18440 	}
18441 	/*
18442 	 * Disable multicast for the solicited node multicast address
18443 	 */
18444 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18445 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18446 
18447 		ipv6_multi.s6_addr32[3] |=
18448 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18449 
18450 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18451 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18452 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18453 
18454 		if (err != 0) {
18455 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18456 			    err));
18457 		}
18458 	}
18459 
18460 	ipif->ipif_multicast_up = 0;
18461 }
18462 
18463 /*
18464  * Used when an interface comes up to recreate any extra routes on this
18465  * interface.
18466  */
18467 static ire_t **
18468 ipif_recover_ire(ipif_t *ipif)
18469 {
18470 	mblk_t	*mp;
18471 	ire_t	**ipif_saved_irep;
18472 	ire_t	**irep;
18473 
18474 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18475 	    ipif->ipif_id));
18476 
18477 	mutex_enter(&ipif->ipif_saved_ire_lock);
18478 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18479 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18480 	if (ipif_saved_irep == NULL) {
18481 		mutex_exit(&ipif->ipif_saved_ire_lock);
18482 		return (NULL);
18483 	}
18484 
18485 	irep = ipif_saved_irep;
18486 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18487 		ire_t		*ire;
18488 		queue_t		*rfq;
18489 		queue_t		*stq;
18490 		ifrt_t		*ifrt;
18491 		uchar_t		*src_addr;
18492 		uchar_t		*gateway_addr;
18493 		mblk_t		*resolver_mp;
18494 		ushort_t	type;
18495 
18496 		/*
18497 		 * When the ire was initially created and then added in
18498 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18499 		 * in the case of a traditional interface route, or as one of
18500 		 * the IRE_OFFSUBNET types (with the exception of
18501 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18502 		 * which we don't need to save or recover).  In the case where
18503 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18504 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18505 		 * to satisfy software like GateD and Sun Cluster which creates
18506 		 * routes using the the loopback interface's address as a
18507 		 * gateway.
18508 		 *
18509 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18510 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18511 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18512 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18513 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18514 		 * the route looks like a traditional interface route (where
18515 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18516 		 * the saved ifrt->ifrt_type.  This means that in the case where
18517 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18518 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18519 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18520 		 */
18521 		ifrt = (ifrt_t *)mp->b_rptr;
18522 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18523 			rfq = NULL;
18524 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18525 			    ? ipif->ipif_rq : ipif->ipif_wq;
18526 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18527 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18528 			    : (uint8_t *)&ipif->ipif_src_addr;
18529 			gateway_addr = NULL;
18530 			resolver_mp = ipif->ipif_resolver_mp;
18531 			type = ipif->ipif_net_type;
18532 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18533 			/* Recover multiroute broadcast IRE. */
18534 			rfq = ipif->ipif_rq;
18535 			stq = ipif->ipif_wq;
18536 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18537 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18538 			    : (uint8_t *)&ipif->ipif_src_addr;
18539 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18540 			resolver_mp = ipif->ipif_bcast_mp;
18541 			type = ifrt->ifrt_type;
18542 		} else {
18543 			rfq = NULL;
18544 			stq = NULL;
18545 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18546 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18547 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18548 			resolver_mp = NULL;
18549 			type = ifrt->ifrt_type;
18550 		}
18551 
18552 		/*
18553 		 * Create a copy of the IRE with the saved address and netmask.
18554 		 */
18555 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18556 		    "0x%x/0x%x\n",
18557 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18558 		    ntohl(ifrt->ifrt_addr),
18559 		    ntohl(ifrt->ifrt_mask)));
18560 		ire = ire_create(
18561 		    (uint8_t *)&ifrt->ifrt_addr,
18562 		    (uint8_t *)&ifrt->ifrt_mask,
18563 		    src_addr,
18564 		    gateway_addr,
18565 		    NULL,
18566 		    &ifrt->ifrt_max_frag,
18567 		    NULL,
18568 		    rfq,
18569 		    stq,
18570 		    type,
18571 		    resolver_mp,
18572 		    ipif,
18573 		    NULL,
18574 		    0,
18575 		    0,
18576 		    0,
18577 		    ifrt->ifrt_flags,
18578 		    &ifrt->ifrt_iulp_info,
18579 		    NULL,
18580 		    NULL);
18581 
18582 		if (ire == NULL) {
18583 			mutex_exit(&ipif->ipif_saved_ire_lock);
18584 			kmem_free(ipif_saved_irep,
18585 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18586 			return (NULL);
18587 		}
18588 
18589 		/*
18590 		 * Some software (for example, GateD and Sun Cluster) attempts
18591 		 * to create (what amount to) IRE_PREFIX routes with the
18592 		 * loopback address as the gateway.  This is primarily done to
18593 		 * set up prefixes with the RTF_REJECT flag set (for example,
18594 		 * when generating aggregate routes.)
18595 		 *
18596 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18597 		 * IRE_LOOPBACK, then we map the request into a
18598 		 * IRE_IF_NORESOLVER.
18599 		 */
18600 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18601 			ire->ire_type = IRE_IF_NORESOLVER;
18602 		/*
18603 		 * ire held by ire_add, will be refreled' towards the
18604 		 * the end of ipif_up_done
18605 		 */
18606 		(void) ire_add(&ire, NULL, NULL, NULL);
18607 		*irep = ire;
18608 		irep++;
18609 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18610 	}
18611 	mutex_exit(&ipif->ipif_saved_ire_lock);
18612 	return (ipif_saved_irep);
18613 }
18614 
18615 /*
18616  * Used to set the netmask and broadcast address to default values when the
18617  * interface is brought up.  (Always called as writer.)
18618  */
18619 static void
18620 ipif_set_default(ipif_t *ipif)
18621 {
18622 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18623 
18624 	if (!ipif->ipif_isv6) {
18625 		/*
18626 		 * Interface holds an IPv4 address. Default
18627 		 * mask is the natural netmask.
18628 		 */
18629 		if (!ipif->ipif_net_mask) {
18630 			ipaddr_t	v4mask;
18631 
18632 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18633 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18634 		}
18635 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18636 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18637 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18638 		} else {
18639 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18640 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18641 		}
18642 		/*
18643 		 * NOTE: SunOS 4.X does this even if the broadcast address
18644 		 * has been already set thus we do the same here.
18645 		 */
18646 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18647 			ipaddr_t	v4addr;
18648 
18649 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18650 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18651 		}
18652 	} else {
18653 		/*
18654 		 * Interface holds an IPv6-only address.  Default
18655 		 * mask is all-ones.
18656 		 */
18657 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18658 			ipif->ipif_v6net_mask = ipv6_all_ones;
18659 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18660 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18661 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18662 		} else {
18663 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18664 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18665 		}
18666 	}
18667 }
18668 
18669 /*
18670  * Return 0 if this address can be used as local address without causing
18671  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18672  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18673  * Special checks are needed to allow the same IPv6 link-local address
18674  * on different ills.
18675  * TODO: allowing the same site-local address on different ill's.
18676  */
18677 int
18678 ip_addr_availability_check(ipif_t *new_ipif)
18679 {
18680 	in6_addr_t our_v6addr;
18681 	ill_t *ill;
18682 	ipif_t *ipif;
18683 	ill_walk_context_t ctx;
18684 
18685 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18686 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18687 	ASSERT(RW_READ_HELD(&ill_g_lock));
18688 
18689 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18690 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18691 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18692 		return (0);
18693 
18694 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18695 
18696 	if (new_ipif->ipif_isv6)
18697 		ill = ILL_START_WALK_V6(&ctx);
18698 	else
18699 		ill = ILL_START_WALK_V4(&ctx);
18700 
18701 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18702 		for (ipif = ill->ill_ipif; ipif != NULL;
18703 		    ipif = ipif->ipif_next) {
18704 			if ((ipif == new_ipif) ||
18705 			    !(ipif->ipif_flags & IPIF_UP) ||
18706 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18707 				continue;
18708 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18709 			    &our_v6addr)) {
18710 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18711 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18712 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18713 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18714 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18715 				    new_ipif->ipif_ill != ill)
18716 					continue;
18717 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18718 				    new_ipif->ipif_ill != ill)
18719 					continue;
18720 				else if (new_ipif->ipif_zoneid !=
18721 				    ipif->ipif_zoneid &&
18722 				    ipif->ipif_zoneid != ALL_ZONES &&
18723 				    (ill->ill_phyint->phyint_flags &
18724 				    PHYI_LOOPBACK))
18725 					continue;
18726 				else if (new_ipif->ipif_ill == ill)
18727 					return (EADDRINUSE);
18728 				else
18729 					return (EADDRNOTAVAIL);
18730 			}
18731 		}
18732 	}
18733 
18734 	return (0);
18735 }
18736 
18737 /*
18738  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18739  * IREs for the ipif.
18740  * When the routine returns EINPROGRESS then mp has been consumed and
18741  * the ioctl will be acked from ip_rput_dlpi.
18742  */
18743 static int
18744 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18745 {
18746 	ill_t	*ill = ipif->ipif_ill;
18747 	boolean_t isv6 = ipif->ipif_isv6;
18748 	int	err = 0;
18749 	boolean_t success;
18750 
18751 	ASSERT(IAM_WRITER_IPIF(ipif));
18752 
18753 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18754 
18755 	/* Shouldn't get here if it is already up. */
18756 	if (ipif->ipif_flags & IPIF_UP)
18757 		return (EALREADY);
18758 
18759 	/* Skip arp/ndp for any loopback interface. */
18760 	if (ill->ill_wq != NULL) {
18761 		conn_t *connp = Q_TO_CONN(q);
18762 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18763 
18764 		if (!ill->ill_dl_up) {
18765 			/*
18766 			 * ill_dl_up is not yet set. i.e. we are yet to
18767 			 * DL_BIND with the driver and this is the first
18768 			 * logical interface on the ill to become "up".
18769 			 * Tell the driver to get going (via DL_BIND_REQ).
18770 			 * Note that changing "significant" IFF_ flags
18771 			 * address/netmask etc cause a down/up dance, but
18772 			 * does not cause an unbind (DL_UNBIND) with the driver
18773 			 */
18774 			return (ill_dl_up(ill, ipif, mp, q));
18775 		}
18776 
18777 		/*
18778 		 * ipif_resolver_up may end up sending an
18779 		 * AR_INTERFACE_UP message to ARP, which would, in
18780 		 * turn send a DLPI message to the driver. ioctls are
18781 		 * serialized and so we cannot send more than one
18782 		 * interface up message at a time. If ipif_resolver_up
18783 		 * does send an interface up message to ARP, we get
18784 		 * EINPROGRESS and we will complete in ip_arp_done.
18785 		 */
18786 
18787 		ASSERT(connp != NULL);
18788 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18789 		mutex_enter(&connp->conn_lock);
18790 		mutex_enter(&ill->ill_lock);
18791 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18792 		mutex_exit(&ill->ill_lock);
18793 		mutex_exit(&connp->conn_lock);
18794 		if (!success)
18795 			return (EINTR);
18796 
18797 		/*
18798 		 * Crank up IPv6 neighbor discovery
18799 		 * Unlike ARP, this should complete when
18800 		 * ipif_ndp_up returns. However, for
18801 		 * ILLF_XRESOLV interfaces we also send a
18802 		 * AR_INTERFACE_UP to the external resolver.
18803 		 * That ioctl will complete in ip_rput.
18804 		 */
18805 		if (isv6) {
18806 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18807 			    B_FALSE);
18808 			if (err != 0) {
18809 				mp = ipsq_pending_mp_get(ipsq, &connp);
18810 				return (err);
18811 			}
18812 		}
18813 		/* Now, ARP */
18814 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18815 		    EINPROGRESS) {
18816 			/* We will complete it in ip_arp_done */
18817 			return (err);
18818 		}
18819 		mp = ipsq_pending_mp_get(ipsq, &connp);
18820 		ASSERT(mp != NULL);
18821 		if (err != 0)
18822 			return (err);
18823 	}
18824 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18825 }
18826 
18827 /*
18828  * Perform a bind for the physical device.
18829  * When the routine returns EINPROGRESS then mp has been consumed and
18830  * the ioctl will be acked from ip_rput_dlpi.
18831  * Allocate an unbind message and save it until ipif_down.
18832  */
18833 static int
18834 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18835 {
18836 	mblk_t	*areq_mp = NULL;
18837 	mblk_t	*bind_mp = NULL;
18838 	mblk_t	*unbind_mp = NULL;
18839 	conn_t	*connp;
18840 	boolean_t success;
18841 
18842 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18843 	ASSERT(IAM_WRITER_ILL(ill));
18844 
18845 	ASSERT(mp != NULL);
18846 
18847 	/* Create a resolver cookie for ARP */
18848 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18849 		areq_t		*areq;
18850 		uint16_t	sap_addr;
18851 
18852 		areq_mp = ill_arp_alloc(ill,
18853 			(uchar_t *)&ip_areq_template, 0);
18854 		if (areq_mp == NULL) {
18855 			return (ENOMEM);
18856 		}
18857 		freemsg(ill->ill_resolver_mp);
18858 		ill->ill_resolver_mp = areq_mp;
18859 		areq = (areq_t *)areq_mp->b_rptr;
18860 		sap_addr = ill->ill_sap;
18861 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18862 		/*
18863 		 * Wait till we call ill_pending_mp_add to determine
18864 		 * the success before we free the ill_resolver_mp and
18865 		 * attach areq_mp in it's place.
18866 		 */
18867 	}
18868 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18869 	    DL_BIND_REQ);
18870 	if (bind_mp == NULL)
18871 		goto bad;
18872 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18873 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18874 
18875 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18876 	if (unbind_mp == NULL)
18877 		goto bad;
18878 
18879 	/*
18880 	 * Record state needed to complete this operation when the
18881 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18882 	 */
18883 	if (WR(q)->q_next == NULL) {
18884 		connp = Q_TO_CONN(q);
18885 		mutex_enter(&connp->conn_lock);
18886 	} else {
18887 		connp = NULL;
18888 	}
18889 	mutex_enter(&ipif->ipif_ill->ill_lock);
18890 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18891 	mutex_exit(&ipif->ipif_ill->ill_lock);
18892 	if (connp != NULL)
18893 		mutex_exit(&connp->conn_lock);
18894 	if (!success)
18895 		goto bad;
18896 
18897 	/*
18898 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18899 	 * the interface goes down.
18900 	 */
18901 	ASSERT(ill->ill_unbind_mp == NULL);
18902 	ill->ill_unbind_mp = unbind_mp;
18903 
18904 	ill_dlpi_send(ill, bind_mp);
18905 	/* Send down link-layer capabilities probe if not already done. */
18906 	ill_capability_probe(ill);
18907 
18908 	/*
18909 	 * Sysid used to rely on the fact that netboots set domainname
18910 	 * and the like. Now that miniroot boots aren't strictly netboots
18911 	 * and miniroot network configuration is driven from userland
18912 	 * these things still need to be set. This situation can be detected
18913 	 * by comparing the interface being configured here to the one
18914 	 * dhcack was set to reference by the boot loader. Once sysid is
18915 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18916 	 */
18917 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18918 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18919 	    (strlen(srpc_domain) == 0)) {
18920 		if (dhcpinit() != 0)
18921 			cmn_err(CE_WARN, "no cached dhcp response");
18922 	}
18923 
18924 	/*
18925 	 * This operation will complete in ip_rput_dlpi with either
18926 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18927 	 */
18928 	return (EINPROGRESS);
18929 bad:
18930 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18931 	/*
18932 	 * We don't have to check for possible removal from illgrp
18933 	 * as we have not yet inserted in illgrp. For groups
18934 	 * without names, this ipif is still not UP and hence
18935 	 * this could not have possibly had any influence in forming
18936 	 * groups.
18937 	 */
18938 
18939 	if (bind_mp != NULL)
18940 		freemsg(bind_mp);
18941 	if (unbind_mp != NULL)
18942 		freemsg(unbind_mp);
18943 	return (ENOMEM);
18944 }
18945 
18946 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18947 
18948 /*
18949  * DLPI and ARP is up.
18950  * Create all the IREs associated with an interface bring up multicast.
18951  * Set the interface flag and finish other initialization
18952  * that potentially had to be differed to after DL_BIND_ACK.
18953  */
18954 int
18955 ipif_up_done(ipif_t *ipif)
18956 {
18957 	ire_t	*ire_array[20];
18958 	ire_t	**irep = ire_array;
18959 	ire_t	**irep1;
18960 	ipaddr_t net_mask = 0;
18961 	ipaddr_t subnet_mask, route_mask;
18962 	ill_t	*ill = ipif->ipif_ill;
18963 	queue_t	*stq;
18964 	ipif_t	 *src_ipif;
18965 	ipif_t   *tmp_ipif;
18966 	boolean_t	flush_ire_cache = B_TRUE;
18967 	int	err = 0;
18968 	phyint_t *phyi;
18969 	ire_t	**ipif_saved_irep = NULL;
18970 	int ipif_saved_ire_cnt;
18971 	int	cnt;
18972 	boolean_t	src_ipif_held = B_FALSE;
18973 	boolean_t	ire_added = B_FALSE;
18974 	boolean_t	loopback = B_FALSE;
18975 
18976 	ip1dbg(("ipif_up_done(%s:%u)\n",
18977 		ipif->ipif_ill->ill_name, ipif->ipif_id));
18978 	/* Check if this is a loopback interface */
18979 	if (ipif->ipif_ill->ill_wq == NULL)
18980 		loopback = B_TRUE;
18981 
18982 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18983 	/*
18984 	 * If all other interfaces for this ill are down or DEPRECATED,
18985 	 * or otherwise unsuitable for source address selection, remove
18986 	 * any IRE_CACHE entries for this ill to make sure source
18987 	 * address selection gets to take this new ipif into account.
18988 	 * No need to hold ill_lock while traversing the ipif list since
18989 	 * we are writer
18990 	 */
18991 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
18992 		tmp_ipif = tmp_ipif->ipif_next) {
18993 		if (((tmp_ipif->ipif_flags &
18994 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
18995 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
18996 		    (tmp_ipif == ipif))
18997 			continue;
18998 		/* first useable pre-existing interface */
18999 		flush_ire_cache = B_FALSE;
19000 		break;
19001 	}
19002 	if (flush_ire_cache)
19003 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19004 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19005 
19006 	/*
19007 	 * Figure out which way the send-to queue should go.  Only
19008 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19009 	 * should show up here.
19010 	 */
19011 	switch (ill->ill_net_type) {
19012 	case IRE_IF_RESOLVER:
19013 		stq = ill->ill_rq;
19014 		break;
19015 	case IRE_IF_NORESOLVER:
19016 	case IRE_LOOPBACK:
19017 		stq = ill->ill_wq;
19018 		break;
19019 	default:
19020 		return (EINVAL);
19021 	}
19022 
19023 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
19024 		/*
19025 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19026 		 * ipif_lookup_on_name(), but in the case of zones we can have
19027 		 * several loopback addresses on lo0. So all the interfaces with
19028 		 * loopback addresses need to be marked IRE_LOOPBACK.
19029 		 */
19030 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19031 		    htonl(INADDR_LOOPBACK))
19032 			ipif->ipif_ire_type = IRE_LOOPBACK;
19033 		else
19034 			ipif->ipif_ire_type = IRE_LOCAL;
19035 	}
19036 
19037 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19038 		/*
19039 		 * Can't use our source address. Select a different
19040 		 * source address for the IRE_INTERFACE and IRE_LOCAL
19041 		 */
19042 		src_ipif = ipif_select_source(ipif->ipif_ill,
19043 		    ipif->ipif_subnet, ipif->ipif_zoneid);
19044 		if (src_ipif == NULL)
19045 			src_ipif = ipif;	/* Last resort */
19046 		else
19047 			src_ipif_held = B_TRUE;
19048 	} else {
19049 		src_ipif = ipif;
19050 	}
19051 
19052 	/* Create all the IREs associated with this interface */
19053 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19054 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19055 
19056 		/*
19057 		 * If we're on a labeled system then make sure that zone-
19058 		 * private addresses have proper remote host database entries.
19059 		 */
19060 		if (is_system_labeled() &&
19061 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
19062 		    !tsol_check_interface_address(ipif))
19063 			return (EINVAL);
19064 
19065 		/* Register the source address for __sin6_src_id */
19066 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
19067 		    ipif->ipif_zoneid);
19068 		if (err != 0) {
19069 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
19070 			return (err);
19071 		}
19072 
19073 		/* If the interface address is set, create the local IRE. */
19074 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
19075 			(void *)ipif,
19076 			ipif->ipif_ire_type,
19077 			ntohl(ipif->ipif_lcl_addr)));
19078 		*irep++ = ire_create(
19079 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
19080 		    (uchar_t *)&ip_g_all_ones,		/* mask */
19081 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
19082 		    NULL,				/* no gateway */
19083 		    NULL,
19084 		    &ip_loopback_mtuplus,		/* max frag size */
19085 		    NULL,
19086 		    ipif->ipif_rq,			/* recv-from queue */
19087 		    NULL,				/* no send-to queue */
19088 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
19089 		    NULL,
19090 		    ipif,
19091 		    NULL,
19092 		    0,
19093 		    0,
19094 		    0,
19095 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
19096 		    RTF_PRIVATE : 0,
19097 		    &ire_uinfo_null,
19098 		    NULL,
19099 		    NULL);
19100 	} else {
19101 		ip1dbg((
19102 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
19103 		    ipif->ipif_ire_type,
19104 		    ntohl(ipif->ipif_lcl_addr),
19105 		    (uint_t)ipif->ipif_flags));
19106 	}
19107 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19108 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19109 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
19110 	} else {
19111 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
19112 	}
19113 
19114 	subnet_mask = ipif->ipif_net_mask;
19115 
19116 	/*
19117 	 * If mask was not specified, use natural netmask of
19118 	 * interface address. Also, store this mask back into the
19119 	 * ipif struct.
19120 	 */
19121 	if (subnet_mask == 0) {
19122 		subnet_mask = net_mask;
19123 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
19124 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
19125 		    ipif->ipif_v6subnet);
19126 	}
19127 
19128 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
19129 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
19130 	    ipif->ipif_subnet != INADDR_ANY) {
19131 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19132 
19133 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19134 			route_mask = IP_HOST_MASK;
19135 		} else {
19136 			route_mask = subnet_mask;
19137 		}
19138 
19139 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
19140 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
19141 			(void *)ipif, (void *)ill,
19142 			ill->ill_net_type,
19143 			ntohl(ipif->ipif_subnet)));
19144 		*irep++ = ire_create(
19145 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
19146 		    (uchar_t *)&route_mask,		/* mask */
19147 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
19148 		    NULL,				/* no gateway */
19149 		    NULL,
19150 		    &ipif->ipif_mtu,			/* max frag */
19151 		    NULL,
19152 		    NULL,				/* no recv queue */
19153 		    stq,				/* send-to queue */
19154 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19155 		    ill->ill_resolver_mp,		/* xmit header */
19156 		    ipif,
19157 		    NULL,
19158 		    0,
19159 		    0,
19160 		    0,
19161 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
19162 		    &ire_uinfo_null,
19163 		    NULL,
19164 		    NULL);
19165 	}
19166 
19167 	/*
19168 	 * If the interface address is set, create the broadcast IREs.
19169 	 *
19170 	 * ire_create_bcast checks if the proposed new IRE matches
19171 	 * any existing IRE's with the same physical interface (ILL).
19172 	 * This should get rid of duplicates.
19173 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
19174 	 * any broadcast ires.
19175 	 */
19176 	if ((ipif->ipif_subnet != INADDR_ANY) &&
19177 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
19178 		ipaddr_t addr;
19179 
19180 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
19181 		irep = ire_check_and_create_bcast(ipif, 0, irep,
19182 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19183 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
19184 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19185 
19186 		/*
19187 		 * For backward compatibility, we need to create net
19188 		 * broadcast ire's based on the old "IP address class
19189 		 * system."  The reason is that some old machines only
19190 		 * respond to these class derived net broadcast.
19191 		 *
19192 		 * But we should not create these net broadcast ire's if
19193 		 * the subnet_mask is shorter than the IP address class based
19194 		 * derived netmask.  Otherwise, we may create a net
19195 		 * broadcast address which is the same as an IP address
19196 		 * on the subnet.  Then TCP will refuse to talk to that
19197 		 * address.
19198 		 *
19199 		 * Nor do we need IRE_BROADCAST ire's for the interface
19200 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19201 		 * interface is already created.  Creating these broadcast
19202 		 * ire's will only create confusion as the "addr" is going
19203 		 * to be same as that of the IP address of the interface.
19204 		 */
19205 		if (net_mask < subnet_mask) {
19206 			addr = net_mask & ipif->ipif_subnet;
19207 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19208 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19209 			irep = ire_check_and_create_bcast(ipif,
19210 			    ~net_mask | addr, irep,
19211 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19212 		}
19213 
19214 		if (subnet_mask != 0xFFFFFFFF) {
19215 			addr = ipif->ipif_subnet;
19216 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19217 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19218 			irep = ire_check_and_create_bcast(ipif,
19219 			    ~subnet_mask|addr, irep,
19220 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19221 		}
19222 	}
19223 
19224 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19225 
19226 	/* If an earlier ire_create failed, get out now */
19227 	for (irep1 = irep; irep1 > ire_array; ) {
19228 		irep1--;
19229 		if (*irep1 == NULL) {
19230 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19231 			err = ENOMEM;
19232 			goto bad;
19233 		}
19234 	}
19235 
19236 	/*
19237 	 * Need to atomically check for ip_addr_availablity_check
19238 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19239 	 * from group also.The ill_g_lock is grabbed as reader
19240 	 * just to make sure no new ills or new ipifs are being added
19241 	 * to the system while we are checking the uniqueness of addresses.
19242 	 */
19243 	rw_enter(&ill_g_lock, RW_READER);
19244 	mutex_enter(&ip_addr_avail_lock);
19245 	/* Mark it up, and increment counters. */
19246 	ill->ill_ipif_up_count++;
19247 	ipif->ipif_flags |= IPIF_UP;
19248 	err = ip_addr_availability_check(ipif);
19249 	mutex_exit(&ip_addr_avail_lock);
19250 	rw_exit(&ill_g_lock);
19251 
19252 	if (err != 0) {
19253 		/*
19254 		 * Our address may already be up on the same ill. In this case,
19255 		 * the ARP entry for our ipif replaced the one for the other
19256 		 * ipif. So we don't want to delete it (otherwise the other ipif
19257 		 * would be unable to send packets).
19258 		 * ip_addr_availability_check() identifies this case for us and
19259 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19260 		 * which is the expected error code.
19261 		 */
19262 		if (err == EADDRINUSE) {
19263 			freemsg(ipif->ipif_arp_del_mp);
19264 			ipif->ipif_arp_del_mp = NULL;
19265 			err = EADDRNOTAVAIL;
19266 		}
19267 		ill->ill_ipif_up_count--;
19268 		ipif->ipif_flags &= ~IPIF_UP;
19269 		goto bad;
19270 	}
19271 
19272 	/*
19273 	 * Add in all newly created IREs.  ire_create_bcast() has
19274 	 * already checked for duplicates of the IRE_BROADCAST type.
19275 	 * We want to add before we call ifgrp_insert which wants
19276 	 * to know whether IRE_IF_RESOLVER exists or not.
19277 	 *
19278 	 * NOTE : We refrele the ire though we may branch to "bad"
19279 	 *	  later on where we do ire_delete. This is okay
19280 	 *	  because nobody can delete it as we are running
19281 	 *	  exclusively.
19282 	 */
19283 	for (irep1 = irep; irep1 > ire_array; ) {
19284 		irep1--;
19285 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19286 		/*
19287 		 * refheld by ire_add. refele towards the end of the func
19288 		 */
19289 		(void) ire_add(irep1, NULL, NULL, NULL);
19290 	}
19291 	ire_added = B_TRUE;
19292 	/*
19293 	 * Form groups if possible.
19294 	 *
19295 	 * If we are supposed to be in a ill_group with a name, insert it
19296 	 * now as we know that at least one ipif is UP. Otherwise form
19297 	 * nameless groups.
19298 	 *
19299 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19300 	 * this ipif into the appropriate interface group, or create a
19301 	 * new one. If this is already in a nameless group, we try to form
19302 	 * a bigger group looking at other ills potentially sharing this
19303 	 * ipif's prefix.
19304 	 */
19305 	phyi = ill->ill_phyint;
19306 	if (phyi->phyint_groupname_len != 0) {
19307 		ASSERT(phyi->phyint_groupname != NULL);
19308 		if (ill->ill_ipif_up_count == 1) {
19309 			ASSERT(ill->ill_group == NULL);
19310 			err = illgrp_insert(&illgrp_head_v4, ill,
19311 			    phyi->phyint_groupname, NULL, B_TRUE);
19312 			if (err != 0) {
19313 				ip1dbg(("ipif_up_done: illgrp allocation "
19314 				    "failed, error %d\n", err));
19315 				goto bad;
19316 			}
19317 		}
19318 		ASSERT(ill->ill_group != NULL);
19319 	}
19320 
19321 	/*
19322 	 * When this is part of group, we need to make sure that
19323 	 * any broadcast ires created because of this ipif coming
19324 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19325 	 * so that we don't receive duplicate broadcast packets.
19326 	 */
19327 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19328 		ipif_renominate_bcast(ipif);
19329 
19330 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19331 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19332 	ipif_saved_irep = ipif_recover_ire(ipif);
19333 
19334 	if (!loopback) {
19335 		/*
19336 		 * If the broadcast address has been set, make sure it makes
19337 		 * sense based on the interface address.
19338 		 * Only match on ill since we are sharing broadcast addresses.
19339 		 */
19340 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19341 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19342 			ire_t	*ire;
19343 
19344 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19345 			    IRE_BROADCAST, ipif, ALL_ZONES,
19346 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19347 
19348 			if (ire == NULL) {
19349 				/*
19350 				 * If there isn't a matching broadcast IRE,
19351 				 * revert to the default for this netmask.
19352 				 */
19353 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19354 				mutex_enter(&ipif->ipif_ill->ill_lock);
19355 				ipif_set_default(ipif);
19356 				mutex_exit(&ipif->ipif_ill->ill_lock);
19357 			} else {
19358 				ire_refrele(ire);
19359 			}
19360 		}
19361 
19362 	}
19363 
19364 
19365 	/* This is the first interface on this ill */
19366 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19367 		/*
19368 		 * Need to recover all multicast memberships in the driver.
19369 		 * This had to be deferred until we had attached.
19370 		 */
19371 		ill_recover_multicast(ill);
19372 	}
19373 	/* Join the allhosts multicast address */
19374 	ipif_multicast_up(ipif);
19375 
19376 	if (!loopback) {
19377 		/*
19378 		 * See whether anybody else would benefit from the
19379 		 * new ipif that we added. We call this always rather
19380 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19381 		 * ipif is for the benefit of illgrp_insert (done above)
19382 		 * which does not do source address selection as it does
19383 		 * not want to re-create interface routes that we are
19384 		 * having reference to it here.
19385 		 */
19386 		ill_update_source_selection(ill);
19387 	}
19388 
19389 	for (irep1 = irep; irep1 > ire_array; ) {
19390 		irep1--;
19391 		if (*irep1 != NULL) {
19392 			/* was held in ire_add */
19393 			ire_refrele(*irep1);
19394 		}
19395 	}
19396 
19397 	cnt = ipif_saved_ire_cnt;
19398 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19399 		if (*irep1 != NULL) {
19400 			/* was held in ire_add */
19401 			ire_refrele(*irep1);
19402 		}
19403 	}
19404 
19405 	/*
19406 	 * This had to be deferred until we had bound.
19407 	 * tell routing sockets that this interface is up
19408 	 */
19409 	ip_rts_ifmsg(ipif);
19410 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19411 
19412 	if (!loopback) {
19413 		/* Broadcast an address mask reply. */
19414 		ipif_mask_reply(ipif);
19415 	}
19416 	if (ipif_saved_irep != NULL) {
19417 		kmem_free(ipif_saved_irep,
19418 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19419 	}
19420 	if (src_ipif_held)
19421 		ipif_refrele(src_ipif);
19422 	/* Let SCTP update the status for this ipif */
19423 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19424 	return (0);
19425 
19426 bad:
19427 	ip1dbg(("ipif_up_done: FAILED \n"));
19428 	/*
19429 	 * We don't have to bother removing from ill groups because
19430 	 *
19431 	 * 1) For groups with names, we insert only when the first ipif
19432 	 *    comes up. In that case if it fails, it will not be in any
19433 	 *    group. So, we need not try to remove for that case.
19434 	 *
19435 	 * 2) For groups without names, either we tried to insert ipif_ill
19436 	 *    in a group as singleton or found some other group to become
19437 	 *    a bigger group. For the former, if it fails we don't have
19438 	 *    anything to do as ipif_ill is not in the group and for the
19439 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19440 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19441 	 */
19442 	while (irep > ire_array) {
19443 		irep--;
19444 		if (*irep != NULL) {
19445 			ire_delete(*irep);
19446 			if (ire_added)
19447 				ire_refrele(*irep);
19448 		}
19449 	}
19450 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19451 
19452 	if (ipif_saved_irep != NULL) {
19453 		kmem_free(ipif_saved_irep,
19454 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19455 	}
19456 	if (src_ipif_held)
19457 		ipif_refrele(src_ipif);
19458 
19459 	ipif_arp_down(ipif);
19460 	return (err);
19461 }
19462 
19463 /*
19464  * Turn off the ARP with the ILLF_NOARP flag.
19465  */
19466 static int
19467 ill_arp_off(ill_t *ill)
19468 {
19469 	mblk_t	*arp_off_mp = NULL;
19470 	mblk_t	*arp_on_mp = NULL;
19471 
19472 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19473 
19474 	ASSERT(IAM_WRITER_ILL(ill));
19475 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19476 
19477 	/*
19478 	 * If the on message is still around we've already done
19479 	 * an arp_off without doing an arp_on thus there is no
19480 	 * work needed.
19481 	 */
19482 	if (ill->ill_arp_on_mp != NULL)
19483 		return (0);
19484 
19485 	/*
19486 	 * Allocate an ARP on message (to be saved) and an ARP off message
19487 	 */
19488 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19489 	if (!arp_off_mp)
19490 		return (ENOMEM);
19491 
19492 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19493 	if (!arp_on_mp)
19494 		goto failed;
19495 
19496 	ASSERT(ill->ill_arp_on_mp == NULL);
19497 	ill->ill_arp_on_mp = arp_on_mp;
19498 
19499 	/* Send an AR_INTERFACE_OFF request */
19500 	putnext(ill->ill_rq, arp_off_mp);
19501 	return (0);
19502 failed:
19503 
19504 	if (arp_off_mp)
19505 		freemsg(arp_off_mp);
19506 	return (ENOMEM);
19507 }
19508 
19509 /*
19510  * Turn on ARP by turning off the ILLF_NOARP flag.
19511  */
19512 static int
19513 ill_arp_on(ill_t *ill)
19514 {
19515 	mblk_t	*mp;
19516 
19517 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19518 
19519 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19520 
19521 	ASSERT(IAM_WRITER_ILL(ill));
19522 	/*
19523 	 * Send an AR_INTERFACE_ON request if we have already done
19524 	 * an arp_off (which allocated the message).
19525 	 */
19526 	if (ill->ill_arp_on_mp != NULL) {
19527 		mp = ill->ill_arp_on_mp;
19528 		ill->ill_arp_on_mp = NULL;
19529 		putnext(ill->ill_rq, mp);
19530 	}
19531 	return (0);
19532 }
19533 
19534 /*
19535  * Called after either deleting ill from the group or when setting
19536  * FAILED or STANDBY on the interface.
19537  */
19538 static void
19539 illgrp_reset_schednext(ill_t *ill)
19540 {
19541 	ill_group_t *illgrp;
19542 	ill_t *save_ill;
19543 
19544 	ASSERT(IAM_WRITER_ILL(ill));
19545 	/*
19546 	 * When called from illgrp_delete, ill_group will be non-NULL.
19547 	 * But when called from ip_sioctl_flags, it could be NULL if
19548 	 * somebody is setting FAILED/INACTIVE on some interface which
19549 	 * is not part of a group.
19550 	 */
19551 	illgrp = ill->ill_group;
19552 	if (illgrp == NULL)
19553 		return;
19554 	if (illgrp->illgrp_ill_schednext != ill)
19555 		return;
19556 
19557 	illgrp->illgrp_ill_schednext = NULL;
19558 	save_ill = ill;
19559 	/*
19560 	 * Choose a good ill to be the next one for
19561 	 * outbound traffic. As the flags FAILED/STANDBY is
19562 	 * not yet marked when called from ip_sioctl_flags,
19563 	 * we check for ill separately.
19564 	 */
19565 	for (ill = illgrp->illgrp_ill; ill != NULL;
19566 	    ill = ill->ill_group_next) {
19567 		if ((ill != save_ill) &&
19568 		    !(ill->ill_phyint->phyint_flags &
19569 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19570 			illgrp->illgrp_ill_schednext = ill;
19571 			return;
19572 		}
19573 	}
19574 }
19575 
19576 /*
19577  * Given an ill, find the next ill in the group to be scheduled.
19578  * (This should be called by ip_newroute() before ire_create().)
19579  * The passed in ill may be pulled out of the group, after we have picked
19580  * up a different outgoing ill from the same group. However ire add will
19581  * atomically check this.
19582  */
19583 ill_t *
19584 illgrp_scheduler(ill_t *ill)
19585 {
19586 	ill_t *retill;
19587 	ill_group_t *illgrp;
19588 	int illcnt;
19589 	int i;
19590 	uint64_t flags;
19591 
19592 	/*
19593 	 * We don't use a lock to check for the ill_group. If this ill
19594 	 * is currently being inserted we may end up just returning this
19595 	 * ill itself. That is ok.
19596 	 */
19597 	if (ill->ill_group == NULL) {
19598 		ill_refhold(ill);
19599 		return (ill);
19600 	}
19601 
19602 	/*
19603 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19604 	 * a set of stable ills. No ill can be added or deleted or change
19605 	 * group while we hold the reader lock.
19606 	 */
19607 	rw_enter(&ill_g_lock, RW_READER);
19608 	if ((illgrp = ill->ill_group) == NULL) {
19609 		rw_exit(&ill_g_lock);
19610 		ill_refhold(ill);
19611 		return (ill);
19612 	}
19613 
19614 	illcnt = illgrp->illgrp_ill_count;
19615 	mutex_enter(&illgrp->illgrp_lock);
19616 	retill = illgrp->illgrp_ill_schednext;
19617 
19618 	if (retill == NULL)
19619 		retill = illgrp->illgrp_ill;
19620 
19621 	/*
19622 	 * We do a circular search beginning at illgrp_ill_schednext
19623 	 * or illgrp_ill. We don't check the flags against the ill lock
19624 	 * since it can change anytime. The ire creation will be atomic
19625 	 * and will fail if the ill is FAILED or OFFLINE.
19626 	 */
19627 	for (i = 0; i < illcnt; i++) {
19628 		flags = retill->ill_phyint->phyint_flags;
19629 
19630 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19631 		    ILL_CAN_LOOKUP(retill)) {
19632 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19633 			ill_refhold(retill);
19634 			break;
19635 		}
19636 		retill = retill->ill_group_next;
19637 		if (retill == NULL)
19638 			retill = illgrp->illgrp_ill;
19639 	}
19640 	mutex_exit(&illgrp->illgrp_lock);
19641 	rw_exit(&ill_g_lock);
19642 
19643 	return (i == illcnt ? NULL : retill);
19644 }
19645 
19646 /*
19647  * Checks for availbility of a usable source address (if there is one) when the
19648  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19649  * this selection is done regardless of the destination.
19650  */
19651 boolean_t
19652 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19653 {
19654 	uint_t	ifindex;
19655 	ipif_t	*ipif = NULL;
19656 	ill_t	*uill;
19657 	boolean_t isv6;
19658 
19659 	ASSERT(ill != NULL);
19660 
19661 	isv6 = ill->ill_isv6;
19662 	ifindex = ill->ill_usesrc_ifindex;
19663 	if (ifindex != 0) {
19664 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19665 		    NULL);
19666 		if (uill == NULL)
19667 			return (NULL);
19668 		mutex_enter(&uill->ill_lock);
19669 		for (ipif = uill->ill_ipif; ipif != NULL;
19670 		    ipif = ipif->ipif_next) {
19671 			if (!IPIF_CAN_LOOKUP(ipif))
19672 				continue;
19673 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19674 				continue;
19675 			if (!(ipif->ipif_flags & IPIF_UP))
19676 				continue;
19677 			if (ipif->ipif_zoneid != zoneid)
19678 				continue;
19679 			if ((isv6 &&
19680 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19681 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19682 				continue;
19683 			mutex_exit(&uill->ill_lock);
19684 			ill_refrele(uill);
19685 			return (B_TRUE);
19686 		}
19687 		mutex_exit(&uill->ill_lock);
19688 		ill_refrele(uill);
19689 	}
19690 	return (B_FALSE);
19691 }
19692 
19693 /*
19694  * Determine the best source address given a destination address and an ill.
19695  * Prefers non-deprecated over deprecated but will return a deprecated
19696  * address if there is no other choice. If there is a usable source address
19697  * on the interface pointed to by ill_usesrc_ifindex then that is given
19698  * first preference.
19699  *
19700  * Returns NULL if there is no suitable source address for the ill.
19701  * This only occurs when there is no valid source address for the ill.
19702  */
19703 ipif_t *
19704 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19705 {
19706 	ipif_t *ipif;
19707 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19708 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19709 	int index = 0;
19710 	boolean_t wrapped = B_FALSE;
19711 	boolean_t same_subnet_only = B_FALSE;
19712 	boolean_t ipif_same_found, ipif_other_found;
19713 	boolean_t specific_found;
19714 	ill_t	*till, *usill = NULL;
19715 	tsol_tpc_t *src_rhtp, *dst_rhtp;
19716 
19717 	if (ill->ill_usesrc_ifindex != 0) {
19718 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19719 		    NULL, NULL, NULL, NULL);
19720 		if (usill != NULL)
19721 			ill = usill;	/* Select source from usesrc ILL */
19722 		else
19723 			return (NULL);
19724 	}
19725 
19726 	/*
19727 	 * If we're dealing with an unlabeled destination on a labeled system,
19728 	 * make sure that we ignore source addresses that are incompatible with
19729 	 * the destination's default label.  That destination's default label
19730 	 * must dominate the minimum label on the source address.
19731 	 */
19732 	dst_rhtp = NULL;
19733 	if (is_system_labeled()) {
19734 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
19735 		if (dst_rhtp == NULL)
19736 			return (NULL);
19737 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
19738 			TPC_RELE(dst_rhtp);
19739 			dst_rhtp = NULL;
19740 		}
19741 	}
19742 
19743 	/*
19744 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19745 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19746 	 * After selecting the right ipif, under ill_lock make sure ipif is
19747 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19748 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19749 	 * but not under a lock.
19750 	 */
19751 	rw_enter(&ill_g_lock, RW_READER);
19752 
19753 retry:
19754 	till = ill;
19755 	ipif_arr[0] = NULL;
19756 
19757 	if (till->ill_group != NULL)
19758 		till = till->ill_group->illgrp_ill;
19759 
19760 	/*
19761 	 * Choose one good source address from each ill across the group.
19762 	 * If possible choose a source address in the same subnet as
19763 	 * the destination address.
19764 	 *
19765 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19766 	 * This is okay because of the following.
19767 	 *
19768 	 *    If PHYI_FAILED is set and we still have non-deprecated
19769 	 *    addresses, it means the addresses have not yet been
19770 	 *    failed over to a different interface. We potentially
19771 	 *    select them to create IRE_CACHES, which will be later
19772 	 *    flushed when the addresses move over.
19773 	 *
19774 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19775 	 *    addresses, it means either the user has configured them
19776 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19777 	 *    been moved over. For the former, in.mpathd does a failover
19778 	 *    when the interface becomes INACTIVE and hence we should
19779 	 *    not find them. Once INACTIVE is set, we don't allow them
19780 	 *    to create logical interfaces anymore. For the latter, a
19781 	 *    flush will happen when INACTIVE is cleared which will
19782 	 *    flush the IRE_CACHES.
19783 	 *
19784 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19785 	 *    over soon. We potentially select them to create IRE_CACHEs,
19786 	 *    which will be later flushed when the addresses move over.
19787 	 *
19788 	 * NOTE : As ipif_select_source is called to borrow source address
19789 	 * for an ipif that is part of a group, source address selection
19790 	 * will be re-done whenever the group changes i.e either an
19791 	 * insertion/deletion in the group.
19792 	 *
19793 	 * Fill ipif_arr[] with source addresses, using these rules:
19794 	 *
19795 	 *	1. At most one source address from a given ill ends up
19796 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19797 	 *	   associated with a given ill ends up in ipif_arr[].
19798 	 *
19799 	 *	2. If there is at least one non-deprecated ipif in the
19800 	 *	   IPMP group with a source address on the same subnet as
19801 	 *	   our destination, then fill ipif_arr[] only with
19802 	 *	   source addresses on the same subnet as our destination.
19803 	 *	   Note that because of (1), only the first
19804 	 *	   non-deprecated ipif found with a source address
19805 	 *	   matching the destination ends up in ipif_arr[].
19806 	 *
19807 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19808 	 *	   addresses not in the same subnet as our destination.
19809 	 *	   Again, because of (1), only the first off-subnet source
19810 	 *	   address will be chosen.
19811 	 *
19812 	 *	4. If there are no non-deprecated ipifs, then just use
19813 	 *	   the source address associated with the last deprecated
19814 	 *	   one we find that happens to be on the same subnet,
19815 	 *	   otherwise the first one not in the same subnet.
19816 	 */
19817 	specific_found = B_FALSE;
19818 	for (; till != NULL; till = till->ill_group_next) {
19819 		ipif_same_found = B_FALSE;
19820 		ipif_other_found = B_FALSE;
19821 		for (ipif = till->ill_ipif; ipif != NULL;
19822 		    ipif = ipif->ipif_next) {
19823 			if (!IPIF_CAN_LOOKUP(ipif))
19824 				continue;
19825 			/* Always skip NOLOCAL and ANYCAST interfaces */
19826 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19827 				continue;
19828 			if (!(ipif->ipif_flags & IPIF_UP))
19829 				continue;
19830 			if (ipif->ipif_zoneid != zoneid &&
19831 			    ipif->ipif_zoneid != ALL_ZONES)
19832 				continue;
19833 			/*
19834 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19835 			 * but are not valid as source addresses.
19836 			 */
19837 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19838 				continue;
19839 
19840 			/*
19841 			 * Check compatibility of local address for
19842 			 * destination's default label if we're on a labeled
19843 			 * system.  Incompatible addresses can't be used at
19844 			 * all.
19845 			 */
19846 			if (dst_rhtp != NULL) {
19847 				boolean_t incompat;
19848 
19849 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
19850 				    IPV4_VERSION, B_FALSE);
19851 				if (src_rhtp == NULL)
19852 					continue;
19853 				incompat =
19854 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
19855 				    src_rhtp->tpc_tp.tp_doi !=
19856 				    dst_rhtp->tpc_tp.tp_doi ||
19857 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
19858 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
19859 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
19860 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
19861 				TPC_RELE(src_rhtp);
19862 				if (incompat)
19863 					continue;
19864 			}
19865 
19866 			/*
19867 			 * We prefer not to use all all-zones addresses, if we
19868 			 * can avoid it, as they pose problems with unlabeled
19869 			 * destinations.
19870 			 */
19871 			if (ipif->ipif_zoneid != ALL_ZONES) {
19872 				if (!specific_found &&
19873 				    (!same_subnet_only ||
19874 				    (ipif->ipif_net_mask & dst) ==
19875 				    ipif->ipif_subnet)) {
19876 					index = 0;
19877 					specific_found = B_TRUE;
19878 					ipif_other_found = B_FALSE;
19879 				}
19880 			} else {
19881 				if (specific_found)
19882 					continue;
19883 			}
19884 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19885 				if (ipif_dep == NULL ||
19886 				    (ipif->ipif_net_mask & dst) ==
19887 				    ipif->ipif_subnet)
19888 					ipif_dep = ipif;
19889 				continue;
19890 			}
19891 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19892 				/* found a source address in the same subnet */
19893 				if (!same_subnet_only) {
19894 					same_subnet_only = B_TRUE;
19895 					index = 0;
19896 				}
19897 				ipif_same_found = B_TRUE;
19898 			} else {
19899 				if (same_subnet_only || ipif_other_found)
19900 					continue;
19901 				ipif_other_found = B_TRUE;
19902 			}
19903 			ipif_arr[index++] = ipif;
19904 			if (index == MAX_IPIF_SELECT_SOURCE) {
19905 				wrapped = B_TRUE;
19906 				index = 0;
19907 			}
19908 			if (ipif_same_found)
19909 				break;
19910 		}
19911 	}
19912 
19913 	if (ipif_arr[0] == NULL) {
19914 		ipif = ipif_dep;
19915 	} else {
19916 		if (wrapped)
19917 			index = MAX_IPIF_SELECT_SOURCE;
19918 		ipif = ipif_arr[ipif_rand() % index];
19919 		ASSERT(ipif != NULL);
19920 	}
19921 
19922 	if (ipif != NULL) {
19923 		mutex_enter(&ipif->ipif_ill->ill_lock);
19924 		if (!IPIF_CAN_LOOKUP(ipif)) {
19925 			mutex_exit(&ipif->ipif_ill->ill_lock);
19926 			goto retry;
19927 		}
19928 		ipif_refhold_locked(ipif);
19929 		mutex_exit(&ipif->ipif_ill->ill_lock);
19930 	}
19931 
19932 	rw_exit(&ill_g_lock);
19933 	if (usill != NULL)
19934 		ill_refrele(usill);
19935 	if (dst_rhtp != NULL)
19936 		TPC_RELE(dst_rhtp);
19937 
19938 #ifdef DEBUG
19939 	if (ipif == NULL) {
19940 		char buf1[INET6_ADDRSTRLEN];
19941 
19942 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19943 		    ill->ill_name,
19944 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19945 	} else {
19946 		char buf1[INET6_ADDRSTRLEN];
19947 		char buf2[INET6_ADDRSTRLEN];
19948 
19949 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
19950 		    ipif->ipif_ill->ill_name,
19951 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
19952 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
19953 		    buf2, sizeof (buf2))));
19954 	}
19955 #endif /* DEBUG */
19956 	return (ipif);
19957 }
19958 
19959 
19960 /*
19961  * If old_ipif is not NULL, see if ipif was derived from old
19962  * ipif and if so, recreate the interface route by re-doing
19963  * source address selection. This happens when ipif_down ->
19964  * ipif_update_other_ipifs calls us.
19965  *
19966  * If old_ipif is NULL, just redo the source address selection
19967  * if needed. This happens when illgrp_insert or ipif_up_done
19968  * calls us.
19969  */
19970 static void
19971 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
19972 {
19973 	ire_t *ire;
19974 	ire_t *ipif_ire;
19975 	queue_t *stq;
19976 	ipif_t *nipif;
19977 	ill_t *ill;
19978 	boolean_t need_rele = B_FALSE;
19979 
19980 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
19981 	ASSERT(IAM_WRITER_IPIF(ipif));
19982 
19983 	ill = ipif->ipif_ill;
19984 	if (!(ipif->ipif_flags &
19985 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
19986 		/*
19987 		 * Can't possibly have borrowed the source
19988 		 * from old_ipif.
19989 		 */
19990 		return;
19991 	}
19992 
19993 	/*
19994 	 * Is there any work to be done? No work if the address
19995 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
19996 	 * ipif_select_source() does not borrow addresses from
19997 	 * NOLOCAL and ANYCAST interfaces).
19998 	 */
19999 	if ((old_ipif != NULL) &&
20000 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20001 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20002 	    (old_ipif->ipif_flags &
20003 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20004 		return;
20005 	}
20006 
20007 	/*
20008 	 * Perform the same checks as when creating the
20009 	 * IRE_INTERFACE in ipif_up_done.
20010 	 */
20011 	if (!(ipif->ipif_flags & IPIF_UP))
20012 		return;
20013 
20014 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20015 	    (ipif->ipif_subnet == INADDR_ANY))
20016 		return;
20017 
20018 	ipif_ire = ipif_to_ire(ipif);
20019 	if (ipif_ire == NULL)
20020 		return;
20021 
20022 	/*
20023 	 * We know that ipif uses some other source for its
20024 	 * IRE_INTERFACE. Is it using the source of this
20025 	 * old_ipif?
20026 	 */
20027 	if (old_ipif != NULL &&
20028 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20029 		ire_refrele(ipif_ire);
20030 		return;
20031 	}
20032 	if (ip_debug > 2) {
20033 		/* ip1dbg */
20034 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20035 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20036 	}
20037 
20038 	stq = ipif_ire->ire_stq;
20039 
20040 	/*
20041 	 * Can't use our source address. Select a different
20042 	 * source address for the IRE_INTERFACE.
20043 	 */
20044 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20045 	if (nipif == NULL) {
20046 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20047 		nipif = ipif;
20048 	} else {
20049 		need_rele = B_TRUE;
20050 	}
20051 
20052 	ire = ire_create(
20053 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20054 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20055 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20056 	    NULL,				/* no gateway */
20057 	    NULL,
20058 	    &ipif->ipif_mtu,			/* max frag */
20059 	    NULL,				/* fast path header */
20060 	    NULL,				/* no recv from queue */
20061 	    stq,				/* send-to queue */
20062 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20063 	    ill->ill_resolver_mp,		/* xmit header */
20064 	    ipif,
20065 	    NULL,
20066 	    0,
20067 	    0,
20068 	    0,
20069 	    0,
20070 	    &ire_uinfo_null,
20071 	    NULL,
20072 	    NULL);
20073 
20074 	if (ire != NULL) {
20075 		ire_t *ret_ire;
20076 		int error;
20077 
20078 		/*
20079 		 * We don't need ipif_ire anymore. We need to delete
20080 		 * before we add so that ire_add does not detect
20081 		 * duplicates.
20082 		 */
20083 		ire_delete(ipif_ire);
20084 		ret_ire = ire;
20085 		error = ire_add(&ret_ire, NULL, NULL, NULL);
20086 		ASSERT(error == 0);
20087 		ASSERT(ire == ret_ire);
20088 		/* Held in ire_add */
20089 		ire_refrele(ret_ire);
20090 	}
20091 	/*
20092 	 * Either we are falling through from above or could not
20093 	 * allocate a replacement.
20094 	 */
20095 	ire_refrele(ipif_ire);
20096 	if (need_rele)
20097 		ipif_refrele(nipif);
20098 }
20099 
20100 /*
20101  * This old_ipif is going away.
20102  *
20103  * Determine if any other ipif's is using our address as
20104  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
20105  * IPIF_DEPRECATED).
20106  * Find the IRE_INTERFACE for such ipifs and recreate them
20107  * to use an different source address following the rules in
20108  * ipif_up_done.
20109  *
20110  * This function takes an illgrp as an argument so that illgrp_delete
20111  * can call this to update source address even after deleting the
20112  * old_ipif->ipif_ill from the ill group.
20113  */
20114 static void
20115 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
20116 {
20117 	ipif_t *ipif;
20118 	ill_t *ill;
20119 	char	buf[INET6_ADDRSTRLEN];
20120 
20121 	ASSERT(IAM_WRITER_IPIF(old_ipif));
20122 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
20123 
20124 	ill = old_ipif->ipif_ill;
20125 
20126 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
20127 	    ill->ill_name,
20128 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
20129 	    buf, sizeof (buf))));
20130 	/*
20131 	 * If this part of a group, look at all ills as ipif_select_source
20132 	 * borrows source address across all the ills in the group.
20133 	 */
20134 	if (illgrp != NULL)
20135 		ill = illgrp->illgrp_ill;
20136 
20137 	for (; ill != NULL; ill = ill->ill_group_next) {
20138 		for (ipif = ill->ill_ipif; ipif != NULL;
20139 		    ipif = ipif->ipif_next) {
20140 
20141 			if (ipif == old_ipif)
20142 				continue;
20143 
20144 			ipif_recreate_interface_routes(old_ipif, ipif);
20145 		}
20146 	}
20147 }
20148 
20149 /* ARGSUSED */
20150 int
20151 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20152 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20153 {
20154 	/*
20155 	 * ill_phyint_reinit merged the v4 and v6 into a single
20156 	 * ipsq. Could also have become part of a ipmp group in the
20157 	 * process, and we might not have been able to complete the
20158 	 * operation in ipif_set_values, if we could not become
20159 	 * exclusive.  If so restart it here.
20160 	 */
20161 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20162 }
20163 
20164 
20165 /* ARGSUSED */
20166 int
20167 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20168     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20169 {
20170 	queue_t		*q1 = q;
20171 	char 		*cp;
20172 	char		interf_name[LIFNAMSIZ];
20173 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
20174 
20175 	if (!q->q_next) {
20176 		ip1dbg((
20177 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
20178 		return (EINVAL);
20179 	}
20180 
20181 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
20182 		return (EALREADY);
20183 
20184 	do {
20185 		q1 = q1->q_next;
20186 	} while (q1->q_next);
20187 	cp = q1->q_qinfo->qi_minfo->mi_idname;
20188 	(void) sprintf(interf_name, "%s%d", cp, ppa);
20189 
20190 	/*
20191 	 * Here we are not going to delay the ioack until after
20192 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
20193 	 * original ioctl message before sending the requests.
20194 	 */
20195 	return (ipif_set_values(q, mp, interf_name, &ppa));
20196 }
20197 
20198 /* ARGSUSED */
20199 int
20200 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20201     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20202 {
20203 	return (ENXIO);
20204 }
20205 
20206 /*
20207  * Net and subnet broadcast ire's are now specific to the particular
20208  * physical interface (ill) and not to any one locigal interface (ipif).
20209  * However, if a particular logical interface is being taken down, it's
20210  * associated ire's will be taken down as well.  Hence, when we go to
20211  * take down or change the local address, broadcast address or netmask
20212  * of a specific logical interface, we must check to make sure that we
20213  * have valid net and subnet broadcast ire's for the other logical
20214  * interfaces which may have been shared with the logical interface
20215  * being brought down or changed.
20216  *
20217  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
20218  * is tied to the first interface coming UP. If that ipif is going down,
20219  * we need to recreate them on the next valid ipif.
20220  *
20221  * Note: assume that the ipif passed in is still up so that it's IRE
20222  * entries are still valid.
20223  */
20224 static void
20225 ipif_check_bcast_ires(ipif_t *test_ipif)
20226 {
20227 	ipif_t	*ipif;
20228 	ire_t	*test_subnet_ire, *test_net_ire;
20229 	ire_t	*test_allzero_ire, *test_allone_ire;
20230 	ire_t	*ire_array[12];
20231 	ire_t	**irep = &ire_array[0];
20232 	ire_t	**irep1;
20233 
20234 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
20235 	ipaddr_t test_net_addr, test_subnet_addr;
20236 	ipaddr_t test_net_mask, test_subnet_mask;
20237 	boolean_t need_net_bcast_ire = B_FALSE;
20238 	boolean_t need_subnet_bcast_ire = B_FALSE;
20239 	boolean_t allzero_bcast_ire_created = B_FALSE;
20240 	boolean_t allone_bcast_ire_created = B_FALSE;
20241 	boolean_t net_bcast_ire_created = B_FALSE;
20242 	boolean_t subnet_bcast_ire_created = B_FALSE;
20243 
20244 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
20245 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
20246 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
20247 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
20248 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
20249 
20250 	ASSERT(!test_ipif->ipif_isv6);
20251 	ASSERT(IAM_WRITER_IPIF(test_ipif));
20252 
20253 	/*
20254 	 * No broadcast IREs for the LOOPBACK interface
20255 	 * or others such as point to point and IPIF_NOXMIT.
20256 	 */
20257 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
20258 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
20259 		return;
20260 
20261 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20262 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20263 
20264 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20265 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20266 
20267 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20268 	test_subnet_mask = test_ipif->ipif_net_mask;
20269 
20270 	/*
20271 	 * If no net mask set, assume the default based on net class.
20272 	 */
20273 	if (test_subnet_mask == 0)
20274 		test_subnet_mask = test_net_mask;
20275 
20276 	/*
20277 	 * Check if there is a network broadcast ire associated with this ipif
20278 	 */
20279 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20280 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20281 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20282 
20283 	/*
20284 	 * Check if there is a subnet broadcast IRE associated with this ipif
20285 	 */
20286 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20287 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20288 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20289 
20290 	/*
20291 	 * No broadcast ire's associated with this ipif.
20292 	 */
20293 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20294 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20295 		return;
20296 	}
20297 
20298 	/*
20299 	 * We have established which bcast ires have to be replaced.
20300 	 * Next we try to locate ipifs that match there ires.
20301 	 * The rules are simple: If we find an ipif that matches on the subnet
20302 	 * address it will also match on the net address, the allzeros and
20303 	 * allones address. Any ipif that matches only on the net address will
20304 	 * also match the allzeros and allones addresses.
20305 	 * The other criterion is the ipif_flags. We look for non-deprecated
20306 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20307 	 * ipifs with check_flags matching (deprecated, etc) are used only
20308 	 * if good ipifs are not available. While looping, we save existing
20309 	 * deprecated ipifs as backup_ipif.
20310 	 * We loop through all the ipifs for this ill looking for ipifs
20311 	 * whose broadcast addr match the ipif passed in, but do not have
20312 	 * their own broadcast ires. For creating 0.0.0.0 and
20313 	 * 255.255.255.255 we just need an ipif on this ill to create.
20314 	 */
20315 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20316 	    ipif = ipif->ipif_next) {
20317 
20318 		ASSERT(!ipif->ipif_isv6);
20319 		/*
20320 		 * Already checked the ipif passed in.
20321 		 */
20322 		if (ipif == test_ipif) {
20323 			continue;
20324 		}
20325 
20326 		/*
20327 		 * We only need to recreate broadcast ires if another ipif in
20328 		 * the same zone uses them. The new ires must be created in the
20329 		 * same zone.
20330 		 */
20331 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20332 			continue;
20333 		}
20334 
20335 		/*
20336 		 * Only interested in logical interfaces with valid local
20337 		 * addresses or with the ability to broadcast.
20338 		 */
20339 		if ((ipif->ipif_subnet == 0) ||
20340 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20341 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20342 		    !(ipif->ipif_flags & IPIF_UP)) {
20343 			continue;
20344 		}
20345 		/*
20346 		 * Check if there is a net broadcast ire for this
20347 		 * net address.  If it turns out that the ipif we are
20348 		 * about to take down owns this ire, we must make a
20349 		 * new one because it is potentially going away.
20350 		 */
20351 		if (test_net_ire && (!net_bcast_ire_created)) {
20352 			net_mask = ip_net_mask(ipif->ipif_subnet);
20353 			net_addr = net_mask & ipif->ipif_subnet;
20354 			if (net_addr == test_net_addr) {
20355 				need_net_bcast_ire = B_TRUE;
20356 				/*
20357 				 * Use DEPRECATED ipif only if no good
20358 				 * ires are available. subnet_addr is
20359 				 * a better match than net_addr.
20360 				 */
20361 				if ((ipif->ipif_flags & check_flags) &&
20362 				    (backup_ipif_net == NULL)) {
20363 					backup_ipif_net = ipif;
20364 				}
20365 			}
20366 		}
20367 		/*
20368 		 * Check if there is a subnet broadcast ire for this
20369 		 * net address.  If it turns out that the ipif we are
20370 		 * about to take down owns this ire, we must make a
20371 		 * new one because it is potentially going away.
20372 		 */
20373 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20374 			subnet_mask = ipif->ipif_net_mask;
20375 			subnet_addr = ipif->ipif_subnet;
20376 			if (subnet_addr == test_subnet_addr) {
20377 				need_subnet_bcast_ire = B_TRUE;
20378 				if ((ipif->ipif_flags & check_flags) &&
20379 				    (backup_ipif_subnet == NULL)) {
20380 					backup_ipif_subnet = ipif;
20381 				}
20382 			}
20383 		}
20384 
20385 
20386 		/* Short circuit here if this ipif is deprecated */
20387 		if (ipif->ipif_flags & check_flags) {
20388 			if ((test_allzero_ire != NULL) &&
20389 			    (!allzero_bcast_ire_created) &&
20390 			    (backup_ipif_allzeros == NULL)) {
20391 				backup_ipif_allzeros = ipif;
20392 			}
20393 			if ((test_allone_ire != NULL) &&
20394 			    (!allone_bcast_ire_created) &&
20395 			    (backup_ipif_allones == NULL)) {
20396 				backup_ipif_allones = ipif;
20397 			}
20398 			continue;
20399 		}
20400 
20401 		/*
20402 		 * Found an ipif which has the same broadcast ire as the
20403 		 * ipif passed in and the ipif passed in "owns" the ire.
20404 		 * Create new broadcast ire's for this broadcast addr.
20405 		 */
20406 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20407 			irep = ire_create_bcast(ipif, net_addr, irep);
20408 			irep = ire_create_bcast(ipif,
20409 			    ~net_mask | net_addr, irep);
20410 			net_bcast_ire_created = B_TRUE;
20411 		}
20412 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20413 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20414 			irep = ire_create_bcast(ipif,
20415 			    ~subnet_mask | subnet_addr, irep);
20416 			subnet_bcast_ire_created = B_TRUE;
20417 		}
20418 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20419 			irep = ire_create_bcast(ipif, 0, irep);
20420 			allzero_bcast_ire_created = B_TRUE;
20421 		}
20422 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20423 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20424 			allone_bcast_ire_created = B_TRUE;
20425 		}
20426 		/*
20427 		 * Once we have created all the appropriate ires, we
20428 		 * just break out of this loop to add what we have created.
20429 		 * This has been indented similar to ire_match_args for
20430 		 * readability.
20431 		 */
20432 		if (((test_net_ire == NULL) ||
20433 			(net_bcast_ire_created)) &&
20434 		    ((test_subnet_ire == NULL) ||
20435 			(subnet_bcast_ire_created)) &&
20436 		    ((test_allzero_ire == NULL) ||
20437 			(allzero_bcast_ire_created)) &&
20438 		    ((test_allone_ire == NULL) ||
20439 			(allone_bcast_ire_created))) {
20440 			break;
20441 		}
20442 	}
20443 
20444 	/*
20445 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20446 	 * exist. 6 pairs of bcast ires are needed.
20447 	 * Note - the old ires are deleted in ipif_down.
20448 	 */
20449 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20450 		ipif = backup_ipif_net;
20451 		irep = ire_create_bcast(ipif, net_addr, irep);
20452 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20453 		net_bcast_ire_created = B_TRUE;
20454 	}
20455 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20456 	    backup_ipif_subnet) {
20457 		ipif = backup_ipif_subnet;
20458 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20459 		irep = ire_create_bcast(ipif,
20460 		    ~subnet_mask | subnet_addr, irep);
20461 		subnet_bcast_ire_created = B_TRUE;
20462 	}
20463 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20464 	    backup_ipif_allzeros) {
20465 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20466 		allzero_bcast_ire_created = B_TRUE;
20467 	}
20468 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20469 	    backup_ipif_allones) {
20470 		irep = ire_create_bcast(backup_ipif_allones,
20471 		    INADDR_BROADCAST, irep);
20472 		allone_bcast_ire_created = B_TRUE;
20473 	}
20474 
20475 	/*
20476 	 * If we can't create all of them, don't add any of them.
20477 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20478 	 * always have a non-loopback copy and loopback copy
20479 	 * for a given address.
20480 	 */
20481 	for (irep1 = irep; irep1 > ire_array; ) {
20482 		irep1--;
20483 		if (*irep1 == NULL) {
20484 			ip0dbg(("ipif_check_bcast_ires: can't create "
20485 			    "IRE_BROADCAST, memory allocation failure\n"));
20486 			while (irep > ire_array) {
20487 				irep--;
20488 				if (*irep != NULL)
20489 					ire_delete(*irep);
20490 			}
20491 			goto bad;
20492 		}
20493 	}
20494 	for (irep1 = irep; irep1 > ire_array; ) {
20495 		int error;
20496 
20497 		irep1--;
20498 		error = ire_add(irep1, NULL, NULL, NULL);
20499 		if (error == 0) {
20500 			ire_refrele(*irep1);		/* Held in ire_add */
20501 		}
20502 	}
20503 bad:
20504 	if (test_allzero_ire != NULL)
20505 		ire_refrele(test_allzero_ire);
20506 	if (test_allone_ire != NULL)
20507 		ire_refrele(test_allone_ire);
20508 	if (test_net_ire != NULL)
20509 		ire_refrele(test_net_ire);
20510 	if (test_subnet_ire != NULL)
20511 		ire_refrele(test_subnet_ire);
20512 }
20513 
20514 /*
20515  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20516  * from lifr_flags and the name from lifr_name.
20517  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20518  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20519  * Returns EINPROGRESS when mp has been consumed by queueing it on
20520  * ill_pending_mp and the ioctl will complete in ip_rput.
20521  */
20522 /* ARGSUSED */
20523 int
20524 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20525     ip_ioctl_cmd_t *ipip, void *if_req)
20526 {
20527 	int	err;
20528 	ill_t	*ill;
20529 	struct lifreq *lifr = (struct lifreq *)if_req;
20530 
20531 	ASSERT(ipif != NULL);
20532 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20533 	ASSERT(q->q_next != NULL);
20534 
20535 	ill = (ill_t *)q->q_ptr;
20536 	/*
20537 	 * If we are not writer on 'q' then this interface exists already
20538 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20539 	 * So return EALREADY
20540 	 */
20541 	if (ill != ipif->ipif_ill)
20542 		return (EALREADY);
20543 
20544 	if (ill->ill_name[0] != '\0')
20545 		return (EALREADY);
20546 
20547 	/*
20548 	 * Set all the flags. Allows all kinds of override. Provide some
20549 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20550 	 * unless there is either multicast/broadcast support in the driver
20551 	 * or it is a pt-pt link.
20552 	 */
20553 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20554 		/* Meaningless to IP thus don't allow them to be set. */
20555 		ip1dbg(("ip_setname: EINVAL 1\n"));
20556 		return (EINVAL);
20557 	}
20558 	/*
20559 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20560 	 * ill_bcast_addr_length info.
20561 	 */
20562 	if (!ill->ill_needs_attach &&
20563 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20564 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20565 	    ill->ill_bcast_addr_length == 0)) {
20566 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20567 		ip1dbg(("ip_setname: EINVAL 2\n"));
20568 		return (EINVAL);
20569 	}
20570 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20571 	    ((lifr->lifr_flags & IFF_IPV6) ||
20572 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20573 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20574 		ip1dbg(("ip_setname: EINVAL 3\n"));
20575 		return (EINVAL);
20576 	}
20577 	if (lifr->lifr_flags & IFF_UP) {
20578 		/* Can only be set with SIOCSLIFFLAGS */
20579 		ip1dbg(("ip_setname: EINVAL 4\n"));
20580 		return (EINVAL);
20581 	}
20582 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20583 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20584 		ip1dbg(("ip_setname: EINVAL 5\n"));
20585 		return (EINVAL);
20586 	}
20587 	/*
20588 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20589 	 */
20590 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20591 	    !(lifr->lifr_flags & IFF_IPV6) &&
20592 	    !(ipif->ipif_isv6)) {
20593 		ip1dbg(("ip_setname: EINVAL 6\n"));
20594 		return (EINVAL);
20595 	}
20596 
20597 	/*
20598 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20599 	 * we have all the flags here. So, we assign rather than we OR.
20600 	 * We can't OR the flags here because we don't want to set
20601 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20602 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20603 	 * on lifr_flags value here.
20604 	 */
20605 	/*
20606 	 * This ill has not been inserted into the global list.
20607 	 * So we are still single threaded and don't need any lock
20608 	 */
20609 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20610 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20611 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20612 
20613 	/* We started off as V4. */
20614 	if (ill->ill_flags & ILLF_IPV6) {
20615 		ill->ill_phyint->phyint_illv6 = ill;
20616 		ill->ill_phyint->phyint_illv4 = NULL;
20617 	}
20618 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20619 	return (err);
20620 }
20621 
20622 /* ARGSUSED */
20623 int
20624 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20625     ip_ioctl_cmd_t *ipip, void *if_req)
20626 {
20627 	/*
20628 	 * ill_phyint_reinit merged the v4 and v6 into a single
20629 	 * ipsq. Could also have become part of a ipmp group in the
20630 	 * process, and we might not have been able to complete the
20631 	 * slifname in ipif_set_values, if we could not become
20632 	 * exclusive.  If so restart it here
20633 	 */
20634 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20635 }
20636 
20637 /*
20638  * Return a pointer to the ipif which matches the index, IP version type and
20639  * zoneid.
20640  */
20641 ipif_t *
20642 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20643     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20644 {
20645 	ill_t	*ill;
20646 	ipsq_t  *ipsq;
20647 	phyint_t *phyi;
20648 	ipif_t	*ipif;
20649 
20650 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20651 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20652 
20653 	if (err != NULL)
20654 		*err = 0;
20655 
20656 	/*
20657 	 * Indexes are stored in the phyint - a common structure
20658 	 * to both IPv4 and IPv6.
20659 	 */
20660 
20661 	rw_enter(&ill_g_lock, RW_READER);
20662 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20663 	    (void *) &index, NULL);
20664 	if (phyi != NULL) {
20665 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20666 		if (ill == NULL) {
20667 			rw_exit(&ill_g_lock);
20668 			if (err != NULL)
20669 				*err = ENXIO;
20670 			return (NULL);
20671 		}
20672 		GRAB_CONN_LOCK(q);
20673 		mutex_enter(&ill->ill_lock);
20674 		if (ILL_CAN_LOOKUP(ill)) {
20675 			for (ipif = ill->ill_ipif; ipif != NULL;
20676 			    ipif = ipif->ipif_next) {
20677 				if (IPIF_CAN_LOOKUP(ipif) &&
20678 				    (zoneid == ALL_ZONES ||
20679 				    zoneid == ipif->ipif_zoneid ||
20680 				    ipif->ipif_zoneid == ALL_ZONES)) {
20681 					ipif_refhold_locked(ipif);
20682 					mutex_exit(&ill->ill_lock);
20683 					RELEASE_CONN_LOCK(q);
20684 					rw_exit(&ill_g_lock);
20685 					return (ipif);
20686 				}
20687 			}
20688 		} else if (ILL_CAN_WAIT(ill, q)) {
20689 			ipsq = ill->ill_phyint->phyint_ipsq;
20690 			mutex_enter(&ipsq->ipsq_lock);
20691 			rw_exit(&ill_g_lock);
20692 			mutex_exit(&ill->ill_lock);
20693 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20694 			mutex_exit(&ipsq->ipsq_lock);
20695 			RELEASE_CONN_LOCK(q);
20696 			*err = EINPROGRESS;
20697 			return (NULL);
20698 		}
20699 		mutex_exit(&ill->ill_lock);
20700 		RELEASE_CONN_LOCK(q);
20701 	}
20702 	rw_exit(&ill_g_lock);
20703 	if (err != NULL)
20704 		*err = ENXIO;
20705 	return (NULL);
20706 }
20707 
20708 typedef struct conn_change_s {
20709 	uint_t cc_old_ifindex;
20710 	uint_t cc_new_ifindex;
20711 } conn_change_t;
20712 
20713 /*
20714  * ipcl_walk function for changing interface index.
20715  */
20716 static void
20717 conn_change_ifindex(conn_t *connp, caddr_t arg)
20718 {
20719 	conn_change_t *connc;
20720 	uint_t old_ifindex;
20721 	uint_t new_ifindex;
20722 	int i;
20723 	ilg_t *ilg;
20724 
20725 	connc = (conn_change_t *)arg;
20726 	old_ifindex = connc->cc_old_ifindex;
20727 	new_ifindex = connc->cc_new_ifindex;
20728 
20729 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20730 		connp->conn_orig_bound_ifindex = new_ifindex;
20731 
20732 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20733 		connp->conn_orig_multicast_ifindex = new_ifindex;
20734 
20735 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20736 		connp->conn_orig_xmit_ifindex = new_ifindex;
20737 
20738 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20739 		ilg = &connp->conn_ilg[i];
20740 		if (ilg->ilg_orig_ifindex == old_ifindex)
20741 			ilg->ilg_orig_ifindex = new_ifindex;
20742 	}
20743 }
20744 
20745 /*
20746  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20747  * to new_index if it matches the old_index.
20748  *
20749  * Failovers typically happen within a group of ills. But somebody
20750  * can remove an ill from the group after a failover happened. If
20751  * we are setting the ifindex after this, we potentially need to
20752  * look at all the ills rather than just the ones in the group.
20753  * We cut down the work by looking at matching ill_net_types
20754  * and ill_types as we could not possibly grouped them together.
20755  */
20756 static void
20757 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20758 {
20759 	ill_t *ill;
20760 	ipif_t *ipif;
20761 	uint_t old_ifindex;
20762 	uint_t new_ifindex;
20763 	ilm_t *ilm;
20764 	ill_walk_context_t ctx;
20765 
20766 	old_ifindex = connc->cc_old_ifindex;
20767 	new_ifindex = connc->cc_new_ifindex;
20768 
20769 	rw_enter(&ill_g_lock, RW_READER);
20770 	ill = ILL_START_WALK_ALL(&ctx);
20771 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20772 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20773 			(ill_orig->ill_type != ill->ill_type)) {
20774 			continue;
20775 		}
20776 		for (ipif = ill->ill_ipif; ipif != NULL;
20777 				ipif = ipif->ipif_next) {
20778 			if (ipif->ipif_orig_ifindex == old_ifindex)
20779 				ipif->ipif_orig_ifindex = new_ifindex;
20780 		}
20781 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20782 			if (ilm->ilm_orig_ifindex == old_ifindex)
20783 				ilm->ilm_orig_ifindex = new_ifindex;
20784 		}
20785 	}
20786 	rw_exit(&ill_g_lock);
20787 }
20788 
20789 /*
20790  * We first need to ensure that the new index is unique, and
20791  * then carry the change across both v4 and v6 ill representation
20792  * of the physical interface.
20793  */
20794 /* ARGSUSED */
20795 int
20796 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20797     ip_ioctl_cmd_t *ipip, void *ifreq)
20798 {
20799 	ill_t		*ill;
20800 	ill_t		*ill_other;
20801 	phyint_t	*phyi;
20802 	int		old_index;
20803 	conn_change_t	connc;
20804 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20805 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20806 	uint_t	index;
20807 	ill_t	*ill_v4;
20808 	ill_t	*ill_v6;
20809 
20810 	if (ipip->ipi_cmd_type == IF_CMD)
20811 		index = ifr->ifr_index;
20812 	else
20813 		index = lifr->lifr_index;
20814 
20815 	/*
20816 	 * Only allow on physical interface. Also, index zero is illegal.
20817 	 *
20818 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20819 	 *
20820 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20821 	 *    implies a possible failback might have to happen. As failback
20822 	 *    depends on the old index, we should fail setting the index.
20823 	 *
20824 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20825 	 *    any addresses or multicast memberships are failed over to
20826 	 *    a non-STANDBY interface. As failback depends on the old
20827 	 *    index, we should fail setting the index for this case also.
20828 	 *
20829 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20830 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20831 	 */
20832 	ill = ipif->ipif_ill;
20833 	phyi = ill->ill_phyint;
20834 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20835 	    ipif->ipif_id != 0 || index == 0) {
20836 		return (EINVAL);
20837 	}
20838 	old_index = phyi->phyint_ifindex;
20839 
20840 	/* If the index is not changing, no work to do */
20841 	if (old_index == index)
20842 		return (0);
20843 
20844 	/*
20845 	 * Use ill_lookup_on_ifindex to determine if the
20846 	 * new index is unused and if so allow the change.
20847 	 */
20848 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20849 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20850 	if (ill_v6 != NULL || ill_v4 != NULL) {
20851 		if (ill_v4 != NULL)
20852 			ill_refrele(ill_v4);
20853 		if (ill_v6 != NULL)
20854 			ill_refrele(ill_v6);
20855 		return (EBUSY);
20856 	}
20857 
20858 	/*
20859 	 * The new index is unused. Set it in the phyint.
20860 	 * Locate the other ill so that we can send a routing
20861 	 * sockets message.
20862 	 */
20863 	if (ill->ill_isv6) {
20864 		ill_other = phyi->phyint_illv4;
20865 	} else {
20866 		ill_other = phyi->phyint_illv6;
20867 	}
20868 
20869 	phyi->phyint_ifindex = index;
20870 
20871 	connc.cc_old_ifindex = old_index;
20872 	connc.cc_new_ifindex = index;
20873 	ip_change_ifindex(ill, &connc);
20874 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20875 
20876 	/* Send the routing sockets message */
20877 	ip_rts_ifmsg(ipif);
20878 	if (ill_other != NULL)
20879 		ip_rts_ifmsg(ill_other->ill_ipif);
20880 
20881 	return (0);
20882 }
20883 
20884 /* ARGSUSED */
20885 int
20886 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20887     ip_ioctl_cmd_t *ipip, void *ifreq)
20888 {
20889 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20890 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20891 
20892 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20893 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20894 	/* Get the interface index */
20895 	if (ipip->ipi_cmd_type == IF_CMD) {
20896 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20897 	} else {
20898 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20899 	}
20900 	return (0);
20901 }
20902 
20903 /* ARGSUSED */
20904 int
20905 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20906     ip_ioctl_cmd_t *ipip, void *ifreq)
20907 {
20908 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20909 
20910 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20911 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20912 	/* Get the interface zone */
20913 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20914 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20915 	return (0);
20916 }
20917 
20918 /*
20919  * Set the zoneid of an interface.
20920  */
20921 /* ARGSUSED */
20922 int
20923 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20924     ip_ioctl_cmd_t *ipip, void *ifreq)
20925 {
20926 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20927 	int err = 0;
20928 	boolean_t need_up = B_FALSE;
20929 	zone_t *zptr;
20930 	zone_status_t status;
20931 	zoneid_t zoneid;
20932 
20933 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20934 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
20935 		if (!is_system_labeled())
20936 			return (ENOTSUP);
20937 		zoneid = GLOBAL_ZONEID;
20938 	}
20939 
20940 	/* cannot assign instance zero to a non-global zone */
20941 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
20942 		return (ENOTSUP);
20943 
20944 	/*
20945 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20946 	 * the event of a race with the zone shutdown processing, since IP
20947 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20948 	 * interface will be cleaned up even if the zone is shut down
20949 	 * immediately after the status check. If the interface can't be brought
20950 	 * down right away, and the zone is shut down before the restart
20951 	 * function is called, we resolve the possible races by rechecking the
20952 	 * zone status in the restart function.
20953 	 */
20954 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
20955 		return (EINVAL);
20956 	status = zone_status_get(zptr);
20957 	zone_rele(zptr);
20958 
20959 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
20960 		return (EINVAL);
20961 
20962 	if (ipif->ipif_flags & IPIF_UP) {
20963 		/*
20964 		 * If the interface is already marked up,
20965 		 * we call ipif_down which will take care
20966 		 * of ditching any IREs that have been set
20967 		 * up based on the old interface address.
20968 		 */
20969 		err = ipif_logical_down(ipif, q, mp);
20970 		if (err == EINPROGRESS)
20971 			return (err);
20972 		ipif_down_tail(ipif);
20973 		need_up = B_TRUE;
20974 	}
20975 
20976 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
20977 	return (err);
20978 }
20979 
20980 static int
20981 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
20982     queue_t *q, mblk_t *mp, boolean_t need_up)
20983 {
20984 	int	err = 0;
20985 
20986 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
20987 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20988 
20989 	/* Set the new zone id. */
20990 	ipif->ipif_zoneid = zoneid;
20991 
20992 	/* Update sctp list */
20993 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
20994 
20995 	if (need_up) {
20996 		/*
20997 		 * Now bring the interface back up.  If this
20998 		 * is the only IPIF for the ILL, ipif_up
20999 		 * will have to re-bind to the device, so
21000 		 * we may get back EINPROGRESS, in which
21001 		 * case, this IOCTL will get completed in
21002 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21003 		 */
21004 		err = ipif_up(ipif, q, mp);
21005 	}
21006 	return (err);
21007 }
21008 
21009 /* ARGSUSED */
21010 int
21011 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21012     ip_ioctl_cmd_t *ipip, void *if_req)
21013 {
21014 	struct lifreq *lifr = (struct lifreq *)if_req;
21015 	zoneid_t zoneid;
21016 	zone_t *zptr;
21017 	zone_status_t status;
21018 
21019 	ASSERT(ipif->ipif_id != 0);
21020 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21021 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21022 		zoneid = GLOBAL_ZONEID;
21023 
21024 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21025 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21026 
21027 	/*
21028 	 * We recheck the zone status to resolve the following race condition:
21029 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21030 	 * 2) hme0:1 is up and can't be brought down right away;
21031 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21032 	 * 3) zone "myzone" is halted; the zone status switches to
21033 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21034 	 * the interfaces to remove - hme0:1 is not returned because it's not
21035 	 * yet in "myzone", so it won't be removed;
21036 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21037 	 * status check here, we would have hme0:1 in "myzone" after it's been
21038 	 * destroyed.
21039 	 * Note that if the status check fails, we need to bring the interface
21040 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21041 	 * ipif_up_done[_v6]().
21042 	 */
21043 	status = ZONE_IS_UNINITIALIZED;
21044 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21045 		status = zone_status_get(zptr);
21046 		zone_rele(zptr);
21047 	}
21048 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21049 		if (ipif->ipif_isv6) {
21050 			(void) ipif_up_done_v6(ipif);
21051 		} else {
21052 			(void) ipif_up_done(ipif);
21053 		}
21054 		return (EINVAL);
21055 	}
21056 
21057 	ipif_down_tail(ipif);
21058 
21059 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21060 	    B_TRUE));
21061 }
21062 
21063 /* ARGSUSED */
21064 int
21065 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21066 	ip_ioctl_cmd_t *ipip, void *ifreq)
21067 {
21068 	struct lifreq	*lifr = ifreq;
21069 
21070 	ASSERT(q->q_next == NULL);
21071 	ASSERT(CONN_Q(q));
21072 
21073 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
21074 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21075 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
21076 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
21077 
21078 	return (0);
21079 }
21080 
21081 
21082 /* Find the previous ILL in this usesrc group */
21083 static ill_t *
21084 ill_prev_usesrc(ill_t *uill)
21085 {
21086 	ill_t *ill;
21087 
21088 	for (ill = uill->ill_usesrc_grp_next;
21089 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
21090 	    ill = ill->ill_usesrc_grp_next)
21091 		/* do nothing */;
21092 	return (ill);
21093 }
21094 
21095 /*
21096  * Release all members of the usesrc group. This routine is called
21097  * from ill_delete when the interface being unplumbed is the
21098  * group head.
21099  */
21100 static void
21101 ill_disband_usesrc_group(ill_t *uill)
21102 {
21103 	ill_t *next_ill, *tmp_ill;
21104 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
21105 	next_ill = uill->ill_usesrc_grp_next;
21106 
21107 	do {
21108 		ASSERT(next_ill != NULL);
21109 		tmp_ill = next_ill->ill_usesrc_grp_next;
21110 		ASSERT(tmp_ill != NULL);
21111 		next_ill->ill_usesrc_grp_next = NULL;
21112 		next_ill->ill_usesrc_ifindex = 0;
21113 		next_ill = tmp_ill;
21114 	} while (next_ill->ill_usesrc_ifindex != 0);
21115 	uill->ill_usesrc_grp_next = NULL;
21116 }
21117 
21118 /*
21119  * Remove the client usesrc ILL from the list and relink to a new list
21120  */
21121 int
21122 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
21123 {
21124 	ill_t *ill, *tmp_ill;
21125 
21126 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
21127 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
21128 
21129 	/*
21130 	 * Check if the usesrc client ILL passed in is not already
21131 	 * in use as a usesrc ILL i.e one whose source address is
21132 	 * in use OR a usesrc ILL is not already in use as a usesrc
21133 	 * client ILL
21134 	 */
21135 	if ((ucill->ill_usesrc_ifindex == 0) ||
21136 	    (uill->ill_usesrc_ifindex != 0)) {
21137 		return (-1);
21138 	}
21139 
21140 	ill = ill_prev_usesrc(ucill);
21141 	ASSERT(ill->ill_usesrc_grp_next != NULL);
21142 
21143 	/* Remove from the current list */
21144 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
21145 		/* Only two elements in the list */
21146 		ASSERT(ill->ill_usesrc_ifindex == 0);
21147 		ill->ill_usesrc_grp_next = NULL;
21148 	} else {
21149 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
21150 	}
21151 
21152 	if (ifindex == 0) {
21153 		ucill->ill_usesrc_ifindex = 0;
21154 		ucill->ill_usesrc_grp_next = NULL;
21155 		return (0);
21156 	}
21157 
21158 	ucill->ill_usesrc_ifindex = ifindex;
21159 	tmp_ill = uill->ill_usesrc_grp_next;
21160 	uill->ill_usesrc_grp_next = ucill;
21161 	ucill->ill_usesrc_grp_next =
21162 	    (tmp_ill != NULL) ? tmp_ill : uill;
21163 	return (0);
21164 }
21165 
21166 /*
21167  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
21168  * ip.c for locking details.
21169  */
21170 /* ARGSUSED */
21171 int
21172 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21173     ip_ioctl_cmd_t *ipip, void *ifreq)
21174 {
21175 	struct lifreq *lifr = (struct lifreq *)ifreq;
21176 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
21177 	    ill_flag_changed = B_FALSE;
21178 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
21179 	int err = 0, ret;
21180 	uint_t ifindex;
21181 	phyint_t *us_phyint, *us_cli_phyint;
21182 	ipsq_t *ipsq = NULL;
21183 
21184 	ASSERT(IAM_WRITER_IPIF(ipif));
21185 	ASSERT(q->q_next == NULL);
21186 	ASSERT(CONN_Q(q));
21187 
21188 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
21189 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
21190 
21191 	ASSERT(us_cli_phyint != NULL);
21192 
21193 	/*
21194 	 * If the client ILL is being used for IPMP, abort.
21195 	 * Note, this can be done before ipsq_try_enter since we are already
21196 	 * exclusive on this ILL
21197 	 */
21198 	if ((us_cli_phyint->phyint_groupname != NULL) ||
21199 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
21200 		return (EINVAL);
21201 	}
21202 
21203 	ifindex = lifr->lifr_index;
21204 	if (ifindex == 0) {
21205 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
21206 			/* non usesrc group interface, nothing to reset */
21207 			return (0);
21208 		}
21209 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
21210 		/* valid reset request */
21211 		reset_flg = B_TRUE;
21212 	}
21213 
21214 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
21215 	    ip_process_ioctl, &err);
21216 
21217 	if (usesrc_ill == NULL) {
21218 		return (err);
21219 	}
21220 
21221 	/*
21222 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
21223 	 * group nor can either of the interfaces be used for standy. So
21224 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
21225 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
21226 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
21227 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
21228 	 * the usesrc_cli_ill
21229 	 */
21230 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
21231 	    NEW_OP, B_TRUE);
21232 	if (ipsq == NULL) {
21233 		err = EINPROGRESS;
21234 		/* Operation enqueued on the ipsq of the usesrc ILL */
21235 		goto done;
21236 	}
21237 
21238 	/* Check if the usesrc_ill is used for IPMP */
21239 	us_phyint = usesrc_ill->ill_phyint;
21240 	if ((us_phyint->phyint_groupname != NULL) ||
21241 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
21242 		err = EINVAL;
21243 		goto done;
21244 	}
21245 
21246 	/*
21247 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
21248 	 * already a client then return EINVAL
21249 	 */
21250 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
21251 		err = EINVAL;
21252 		goto done;
21253 	}
21254 
21255 	/*
21256 	 * If the ill_usesrc_ifindex field is already set to what it needs to
21257 	 * be then this is a duplicate operation.
21258 	 */
21259 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
21260 		err = 0;
21261 		goto done;
21262 	}
21263 
21264 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
21265 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
21266 	    usesrc_ill->ill_isv6));
21267 
21268 	/*
21269 	 * The next step ensures that no new ires will be created referencing
21270 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21271 	 * we go through an ire walk deleting all ire caches that reference
21272 	 * the client ill. New ires referencing the client ill that are added
21273 	 * to the ire table before the ILL_CHANGING flag is set, will be
21274 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21275 	 * the client ill while the ILL_CHANGING flag is set will be failed
21276 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21277 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21278 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21279 	 * belong to the same usesrc group.
21280 	 */
21281 	mutex_enter(&usesrc_cli_ill->ill_lock);
21282 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21283 	mutex_exit(&usesrc_cli_ill->ill_lock);
21284 	ill_flag_changed = B_TRUE;
21285 
21286 	if (ipif->ipif_isv6)
21287 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21288 		    ALL_ZONES);
21289 	else
21290 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21291 		    ALL_ZONES);
21292 
21293 	/*
21294 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21295 	 * and the ill_usesrc_ifindex fields
21296 	 */
21297 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21298 
21299 	if (reset_flg) {
21300 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21301 		if (ret != 0) {
21302 			err = EINVAL;
21303 		}
21304 		rw_exit(&ill_g_usesrc_lock);
21305 		goto done;
21306 	}
21307 
21308 	/*
21309 	 * Four possibilities to consider:
21310 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21311 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21312 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21313 	 * 4. Both are part of their respective usesrc groups
21314 	 */
21315 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21316 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21317 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21318 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21319 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21320 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21321 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21322 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21323 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21324 		/* Insert at head of list */
21325 		usesrc_cli_ill->ill_usesrc_grp_next =
21326 		    usesrc_ill->ill_usesrc_grp_next;
21327 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21328 	} else {
21329 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21330 		    ifindex);
21331 		if (ret != 0)
21332 			err = EINVAL;
21333 	}
21334 	rw_exit(&ill_g_usesrc_lock);
21335 
21336 done:
21337 	if (ill_flag_changed) {
21338 		mutex_enter(&usesrc_cli_ill->ill_lock);
21339 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21340 		mutex_exit(&usesrc_cli_ill->ill_lock);
21341 	}
21342 	if (ipsq != NULL)
21343 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21344 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21345 	ill_refrele(usesrc_ill);
21346 	return (err);
21347 }
21348 
21349 /*
21350  * comparison function used by avl.
21351  */
21352 static int
21353 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21354 {
21355 
21356 	uint_t index;
21357 
21358 	ASSERT(phyip != NULL && index_ptr != NULL);
21359 
21360 	index = *((uint_t *)index_ptr);
21361 	/*
21362 	 * let the phyint with the lowest index be on top.
21363 	 */
21364 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21365 		return (1);
21366 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21367 		return (-1);
21368 	return (0);
21369 }
21370 
21371 /*
21372  * comparison function used by avl.
21373  */
21374 static int
21375 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21376 {
21377 	ill_t *ill;
21378 	int res = 0;
21379 
21380 	ASSERT(phyip != NULL && name_ptr != NULL);
21381 
21382 	if (((phyint_t *)phyip)->phyint_illv4)
21383 		ill = ((phyint_t *)phyip)->phyint_illv4;
21384 	else
21385 		ill = ((phyint_t *)phyip)->phyint_illv6;
21386 	ASSERT(ill != NULL);
21387 
21388 	res = strcmp(ill->ill_name, (char *)name_ptr);
21389 	if (res > 0)
21390 		return (1);
21391 	else if (res < 0)
21392 		return (-1);
21393 	return (0);
21394 }
21395 /*
21396  * This function is called from ill_delete when the ill is being
21397  * unplumbed. We remove the reference from the phyint and we also
21398  * free the phyint when there are no more references to it.
21399  */
21400 static void
21401 ill_phyint_free(ill_t *ill)
21402 {
21403 	phyint_t *phyi;
21404 	phyint_t *next_phyint;
21405 	ipsq_t *cur_ipsq;
21406 
21407 	ASSERT(ill->ill_phyint != NULL);
21408 
21409 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21410 	phyi = ill->ill_phyint;
21411 	ill->ill_phyint = NULL;
21412 	/*
21413 	 * ill_init allocates a phyint always to store the copy
21414 	 * of flags relevant to phyint. At that point in time, we could
21415 	 * not assign the name and hence phyint_illv4/v6 could not be
21416 	 * initialized. Later in ipif_set_values, we assign the name to
21417 	 * the ill, at which point in time we assign phyint_illv4/v6.
21418 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21419 	 */
21420 	if (ill->ill_flags & ILLF_IPV6) {
21421 		phyi->phyint_illv6 = NULL;
21422 	} else {
21423 		phyi->phyint_illv4 = NULL;
21424 	}
21425 	/*
21426 	 * ipif_down removes it from the group when the last ipif goes
21427 	 * down.
21428 	 */
21429 	ASSERT(ill->ill_group == NULL);
21430 
21431 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21432 		return;
21433 
21434 	/*
21435 	 * Make sure this phyint was put in the list.
21436 	 */
21437 	if (phyi->phyint_ifindex > 0) {
21438 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21439 		    phyi);
21440 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21441 		    phyi);
21442 	}
21443 	/*
21444 	 * remove phyint from the ipsq list.
21445 	 */
21446 	cur_ipsq = phyi->phyint_ipsq;
21447 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21448 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21449 	} else {
21450 		next_phyint = cur_ipsq->ipsq_phyint_list;
21451 		while (next_phyint != NULL) {
21452 			if (next_phyint->phyint_ipsq_next == phyi) {
21453 				next_phyint->phyint_ipsq_next =
21454 					phyi->phyint_ipsq_next;
21455 				break;
21456 			}
21457 			next_phyint = next_phyint->phyint_ipsq_next;
21458 		}
21459 		ASSERT(next_phyint != NULL);
21460 	}
21461 	IPSQ_DEC_REF(cur_ipsq);
21462 
21463 	if (phyi->phyint_groupname_len != 0) {
21464 		ASSERT(phyi->phyint_groupname != NULL);
21465 		mi_free(phyi->phyint_groupname);
21466 	}
21467 	mi_free(phyi);
21468 }
21469 
21470 /*
21471  * Attach the ill to the phyint structure which can be shared by both
21472  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21473  * function is called from ipif_set_values and ill_lookup_on_name (for
21474  * loopback) where we know the name of the ill. We lookup the ill and if
21475  * there is one present already with the name use that phyint. Otherwise
21476  * reuse the one allocated by ill_init.
21477  */
21478 static void
21479 ill_phyint_reinit(ill_t *ill)
21480 {
21481 	boolean_t isv6 = ill->ill_isv6;
21482 	phyint_t *phyi_old;
21483 	phyint_t *phyi;
21484 	avl_index_t where = 0;
21485 	ill_t	*ill_other = NULL;
21486 	ipsq_t	*ipsq;
21487 
21488 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21489 
21490 	phyi_old = ill->ill_phyint;
21491 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21492 	    phyi_old->phyint_illv6 == NULL));
21493 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21494 	    phyi_old->phyint_illv4 == NULL));
21495 	ASSERT(phyi_old->phyint_ifindex == 0);
21496 
21497 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21498 	    ill->ill_name, &where);
21499 
21500 	/*
21501 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21502 	 *    the global list of ills. So no other thread could have located
21503 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21504 	 * 2. Now locate the other protocol instance of this ill.
21505 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21506 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21507 	 *    of neither ill can change.
21508 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21509 	 *    other ill.
21510 	 * 5. Release all locks.
21511 	 */
21512 
21513 	/*
21514 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21515 	 * we are initializing IPv4.
21516 	 */
21517 	if (phyi != NULL) {
21518 		ill_other = (isv6) ? phyi->phyint_illv4 :
21519 		    phyi->phyint_illv6;
21520 		ASSERT(ill_other->ill_phyint != NULL);
21521 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21522 		    (!isv6 && ill_other->ill_isv6));
21523 		GRAB_ILL_LOCKS(ill, ill_other);
21524 		/*
21525 		 * We are potentially throwing away phyint_flags which
21526 		 * could be different from the one that we obtain from
21527 		 * ill_other->ill_phyint. But it is okay as we are assuming
21528 		 * that the state maintained within IP is correct.
21529 		 */
21530 		mutex_enter(&phyi->phyint_lock);
21531 		if (isv6) {
21532 			ASSERT(phyi->phyint_illv6 == NULL);
21533 			phyi->phyint_illv6 = ill;
21534 		} else {
21535 			ASSERT(phyi->phyint_illv4 == NULL);
21536 			phyi->phyint_illv4 = ill;
21537 		}
21538 		/*
21539 		 * This is a new ill, currently undergoing SLIFNAME
21540 		 * So we could not have joined an IPMP group until now.
21541 		 */
21542 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21543 		    phyi_old->phyint_groupname == NULL);
21544 
21545 		/*
21546 		 * This phyi_old is going away. Decref ipsq_refs and
21547 		 * assert it is zero. The ipsq itself will be freed in
21548 		 * ipsq_exit
21549 		 */
21550 		ipsq = phyi_old->phyint_ipsq;
21551 		IPSQ_DEC_REF(ipsq);
21552 		ASSERT(ipsq->ipsq_refs == 0);
21553 		/* Get the singleton phyint out of the ipsq list */
21554 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21555 		ipsq->ipsq_phyint_list = NULL;
21556 		phyi_old->phyint_illv4 = NULL;
21557 		phyi_old->phyint_illv6 = NULL;
21558 		mi_free(phyi_old);
21559 	} else {
21560 		mutex_enter(&ill->ill_lock);
21561 		/*
21562 		 * We don't need to acquire any lock, since
21563 		 * the ill is not yet visible globally  and we
21564 		 * have not yet released the ill_g_lock.
21565 		 */
21566 		phyi = phyi_old;
21567 		mutex_enter(&phyi->phyint_lock);
21568 		/* XXX We need a recovery strategy here. */
21569 		if (!phyint_assign_ifindex(phyi))
21570 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21571 
21572 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21573 		    (void *)phyi, where);
21574 
21575 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21576 		    &phyi->phyint_ifindex, &where);
21577 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21578 		    (void *)phyi, where);
21579 	}
21580 
21581 	/*
21582 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21583 	 * pending mp is not affected because that is per ill basis.
21584 	 */
21585 	ill->ill_phyint = phyi;
21586 
21587 	/*
21588 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21589 	 * We do this here as when the first ipif was allocated,
21590 	 * ipif_allocate does not know the right interface index.
21591 	 */
21592 
21593 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21594 	/*
21595 	 * Now that the phyint's ifindex has been assigned, complete the
21596 	 * remaining
21597 	 */
21598 	if (ill->ill_isv6) {
21599 		ill->ill_ip6_mib->ipv6IfIndex =
21600 		    ill->ill_phyint->phyint_ifindex;
21601 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21602 		    ill->ill_phyint->phyint_ifindex;
21603 	}
21604 
21605 	RELEASE_ILL_LOCKS(ill, ill_other);
21606 	mutex_exit(&phyi->phyint_lock);
21607 }
21608 
21609 /*
21610  * Notify any downstream modules of the name of this interface.
21611  * An M_IOCTL is used even though we don't expect a successful reply.
21612  * Any reply message from the driver (presumably an M_IOCNAK) will
21613  * eventually get discarded somewhere upstream.  The message format is
21614  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21615  * to IP.
21616  */
21617 static void
21618 ip_ifname_notify(ill_t *ill, queue_t *q)
21619 {
21620 	mblk_t *mp1, *mp2;
21621 	struct iocblk *iocp;
21622 	struct lifreq *lifr;
21623 
21624 	mp1 = mkiocb(SIOCSLIFNAME);
21625 	if (mp1 == NULL)
21626 		return;
21627 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21628 	if (mp2 == NULL) {
21629 		freeb(mp1);
21630 		return;
21631 	}
21632 
21633 	mp1->b_cont = mp2;
21634 	iocp = (struct iocblk *)mp1->b_rptr;
21635 	iocp->ioc_count = sizeof (struct lifreq);
21636 
21637 	lifr = (struct lifreq *)mp2->b_rptr;
21638 	mp2->b_wptr += sizeof (struct lifreq);
21639 	bzero(lifr, sizeof (struct lifreq));
21640 
21641 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21642 	lifr->lifr_ppa = ill->ill_ppa;
21643 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21644 
21645 	putnext(q, mp1);
21646 }
21647 
21648 static boolean_t ip_trash_timer_started = B_FALSE;
21649 
21650 static int
21651 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21652 {
21653 	int err;
21654 
21655 	/* Set the obsolete NDD per-interface forwarding name. */
21656 	err = ill_set_ndd_name(ill);
21657 	if (err != 0) {
21658 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21659 		    err);
21660 	}
21661 
21662 	/* Tell downstream modules where they are. */
21663 	ip_ifname_notify(ill, q);
21664 
21665 	/*
21666 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21667 	 * Error cases are ENOMEM ...
21668 	 */
21669 	err = ill_dl_phys(ill, ipif, mp, q);
21670 
21671 	/*
21672 	 * If there is no IRE expiration timer running, get one started.
21673 	 * igmp and mld timers will be triggered by the first multicast
21674 	 */
21675 	if (!ip_trash_timer_started) {
21676 		/*
21677 		 * acquire the lock and check again.
21678 		 */
21679 		mutex_enter(&ip_trash_timer_lock);
21680 		if (!ip_trash_timer_started) {
21681 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21682 			    MSEC_TO_TICK(ip_timer_interval));
21683 			ip_trash_timer_started = B_TRUE;
21684 		}
21685 		mutex_exit(&ip_trash_timer_lock);
21686 	}
21687 
21688 	if (ill->ill_isv6) {
21689 		mutex_enter(&mld_slowtimeout_lock);
21690 		if (mld_slowtimeout_id == 0) {
21691 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21692 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21693 		}
21694 		mutex_exit(&mld_slowtimeout_lock);
21695 	} else {
21696 		mutex_enter(&igmp_slowtimeout_lock);
21697 		if (igmp_slowtimeout_id == 0) {
21698 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21699 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21700 		}
21701 		mutex_exit(&igmp_slowtimeout_lock);
21702 	}
21703 
21704 	return (err);
21705 }
21706 
21707 /*
21708  * Common routine for ppa and ifname setting. Should be called exclusive.
21709  *
21710  * Returns EINPROGRESS when mp has been consumed by queueing it on
21711  * ill_pending_mp and the ioctl will complete in ip_rput.
21712  *
21713  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21714  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21715  * For SLIFNAME, we pass these values back to the userland.
21716  */
21717 static int
21718 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21719 {
21720 	ill_t	*ill;
21721 	ipif_t	*ipif;
21722 	ipsq_t	*ipsq;
21723 	char	*ppa_ptr;
21724 	char	*old_ptr;
21725 	char	old_char;
21726 	int	error;
21727 
21728 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21729 	ASSERT(q->q_next != NULL);
21730 	ASSERT(interf_name != NULL);
21731 
21732 	ill = (ill_t *)q->q_ptr;
21733 
21734 	ASSERT(ill->ill_name[0] == '\0');
21735 	ASSERT(IAM_WRITER_ILL(ill));
21736 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21737 	ASSERT(ill->ill_ppa == UINT_MAX);
21738 
21739 	/* The ppa is sent down by ifconfig or is chosen */
21740 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21741 		return (EINVAL);
21742 	}
21743 
21744 	/*
21745 	 * make sure ppa passed in is same as ppa in the name.
21746 	 * This check is not made when ppa == UINT_MAX in that case ppa
21747 	 * in the name could be anything. System will choose a ppa and
21748 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21749 	 */
21750 	if (*new_ppa_ptr != UINT_MAX) {
21751 		/* stoi changes the pointer */
21752 		old_ptr = ppa_ptr;
21753 		/*
21754 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21755 		 * (they don't have an externally visible ppa).  We assign one
21756 		 * here so that we can manage the interface.  Note that in
21757 		 * the past this value was always 0 for DLPI 1 drivers.
21758 		 */
21759 		if (*new_ppa_ptr == 0)
21760 			*new_ppa_ptr = stoi(&old_ptr);
21761 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21762 			return (EINVAL);
21763 	}
21764 	/*
21765 	 * terminate string before ppa
21766 	 * save char at that location.
21767 	 */
21768 	old_char = ppa_ptr[0];
21769 	ppa_ptr[0] = '\0';
21770 
21771 	ill->ill_ppa = *new_ppa_ptr;
21772 	/*
21773 	 * Finish as much work now as possible before calling ill_glist_insert
21774 	 * which makes the ill globally visible and also merges it with the
21775 	 * other protocol instance of this phyint. The remaining work is
21776 	 * done after entering the ipsq which may happen sometime later.
21777 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21778 	 */
21779 	ipif = ill->ill_ipif;
21780 
21781 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21782 	ipif_assign_seqid(ipif);
21783 
21784 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21785 		ill->ill_flags |= ILLF_IPV4;
21786 
21787 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21788 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21789 
21790 	if (ill->ill_flags & ILLF_IPV6) {
21791 
21792 		ill->ill_isv6 = B_TRUE;
21793 		if (ill->ill_rq != NULL) {
21794 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21795 			ill->ill_wq->q_qinfo = &winit_ipv6;
21796 		}
21797 
21798 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21799 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21800 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21801 		ipif->ipif_v6subnet = ipv6_all_zeros;
21802 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21803 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21804 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21805 		/*
21806 		 * point-to-point or Non-mulicast capable
21807 		 * interfaces won't do NUD unless explicitly
21808 		 * configured to do so.
21809 		 */
21810 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21811 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21812 			ill->ill_flags |= ILLF_NONUD;
21813 		}
21814 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21815 		if (ill->ill_flags & ILLF_NOARP) {
21816 			/*
21817 			 * Note: xresolv interfaces will eventually need
21818 			 * NOARP set here as well, but that will require
21819 			 * those external resolvers to have some
21820 			 * knowledge of that flag and act appropriately.
21821 			 * Not to be changed at present.
21822 			 */
21823 			ill->ill_flags &= ~ILLF_NOARP;
21824 		}
21825 		/*
21826 		 * Set the ILLF_ROUTER flag according to the global
21827 		 * IPv6 forwarding policy.
21828 		 */
21829 		if (ipv6_forward != 0)
21830 			ill->ill_flags |= ILLF_ROUTER;
21831 	} else if (ill->ill_flags & ILLF_IPV4) {
21832 		ill->ill_isv6 = B_FALSE;
21833 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21834 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21835 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21836 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21837 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21838 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21839 		/*
21840 		 * Set the ILLF_ROUTER flag according to the global
21841 		 * IPv4 forwarding policy.
21842 		 */
21843 		if (ip_g_forward != 0)
21844 			ill->ill_flags |= ILLF_ROUTER;
21845 	}
21846 
21847 	ASSERT(ill->ill_phyint != NULL);
21848 
21849 	/*
21850 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21851 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21852 	 */
21853 	if (ill->ill_isv6) {
21854 		/* allocate v6 mib */
21855 		if (!ill_allocate_mibs(ill))
21856 			return (ENOMEM);
21857 	}
21858 
21859 	/*
21860 	 * Pick a default sap until we get the DL_INFO_ACK back from
21861 	 * the driver.
21862 	 */
21863 	if (ill->ill_sap == 0) {
21864 		if (ill->ill_isv6)
21865 			ill->ill_sap  = IP6_DL_SAP;
21866 		else
21867 			ill->ill_sap  = IP_DL_SAP;
21868 	}
21869 
21870 	ill->ill_ifname_pending = 1;
21871 	ill->ill_ifname_pending_err = 0;
21872 
21873 	ill_refhold(ill);
21874 	rw_enter(&ill_g_lock, RW_WRITER);
21875 	if ((error = ill_glist_insert(ill, interf_name,
21876 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21877 		ill->ill_ppa = UINT_MAX;
21878 		ill->ill_name[0] = '\0';
21879 		/*
21880 		 * undo null termination done above.
21881 		 */
21882 		ppa_ptr[0] = old_char;
21883 		rw_exit(&ill_g_lock);
21884 		ill_refrele(ill);
21885 		return (error);
21886 	}
21887 
21888 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21889 
21890 	/*
21891 	 * When we return the buffer pointed to by interf_name should contain
21892 	 * the same name as in ill_name.
21893 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21894 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21895 	 * so copy full name and update the ppa ptr.
21896 	 * When ppa passed in != UINT_MAX all values are correct just undo
21897 	 * null termination, this saves a bcopy.
21898 	 */
21899 	if (*new_ppa_ptr == UINT_MAX) {
21900 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21901 		*new_ppa_ptr = ill->ill_ppa;
21902 	} else {
21903 		/*
21904 		 * undo null termination done above.
21905 		 */
21906 		ppa_ptr[0] = old_char;
21907 	}
21908 
21909 	/* Let SCTP know about this ILL */
21910 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21911 
21912 	/* and also about the first ipif */
21913 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21914 
21915 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21916 	    B_TRUE);
21917 
21918 	rw_exit(&ill_g_lock);
21919 	ill_refrele(ill);
21920 	if (ipsq == NULL)
21921 		return (EINPROGRESS);
21922 
21923 	/*
21924 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21925 	 * due to the phyint merge in ill_phyint_reinit.
21926 	 */
21927 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21928 		ipsq->ipsq_current_ipif == ipif);
21929 	ipsq->ipsq_current_ipif = ipif;
21930 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21931 	error = ipif_set_values_tail(ill, ipif, mp, q);
21932 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21933 	if (error != 0 && error != EINPROGRESS) {
21934 		/*
21935 		 * restore previous values
21936 		 */
21937 		ill->ill_isv6 = B_FALSE;
21938 	}
21939 	return (error);
21940 }
21941 
21942 
21943 extern void (*ip_cleanup_func)(void);
21944 
21945 void
21946 ipif_init(void)
21947 {
21948 	hrtime_t hrt;
21949 	int i;
21950 
21951 	/*
21952 	 * Can't call drv_getparm here as it is too early in the boot.
21953 	 * As we use ipif_src_random just for picking a different
21954 	 * source address everytime, this need not be really random.
21955 	 */
21956 	hrt = gethrtime();
21957 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
21958 
21959 	for (i = 0; i < MAX_G_HEADS; i++) {
21960 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
21961 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
21962 	}
21963 
21964 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
21965 	    ill_phyint_compare_index,
21966 	    sizeof (phyint_t),
21967 	    offsetof(struct phyint, phyint_avl_by_index));
21968 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
21969 	    ill_phyint_compare_name,
21970 	    sizeof (phyint_t),
21971 	    offsetof(struct phyint, phyint_avl_by_name));
21972 
21973 	ip_cleanup_func = ip_thread_exit;
21974 }
21975 
21976 /*
21977  * This is called by ip_rt_add when src_addr value is other than zero.
21978  * src_addr signifies the source address of the incoming packet. For
21979  * reverse tunnel route we need to create a source addr based routing
21980  * table. This routine creates ip_mrtun_table if it's empty and then
21981  * it adds the route entry hashed by source address. It verifies that
21982  * the outgoing interface is always a non-resolver interface (tunnel).
21983  */
21984 int
21985 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
21986     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
21987 {
21988 	ire_t   *ire;
21989 	ire_t	*save_ire;
21990 	ipif_t  *ipif;
21991 	ill_t   *in_ill = NULL;
21992 	ill_t	*out_ill;
21993 	queue_t	*stq;
21994 	mblk_t	*dlureq_mp;
21995 	int	error;
21996 
21997 	if (ire_arg != NULL)
21998 		*ire_arg = NULL;
21999 	ASSERT(in_src_addr != INADDR_ANY);
22000 
22001 	ipif = ipif_arg;
22002 	if (ipif != NULL) {
22003 		out_ill = ipif->ipif_ill;
22004 	} else {
22005 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
22006 		return (EINVAL);
22007 	}
22008 
22009 	if (src_ipif == NULL) {
22010 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
22011 		return (EINVAL);
22012 	}
22013 	in_ill = src_ipif->ipif_ill;
22014 
22015 	/*
22016 	 * Check for duplicates. We don't need to
22017 	 * match out_ill, because the uniqueness of
22018 	 * a route is only dependent on src_addr and
22019 	 * in_ill.
22020 	 */
22021 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
22022 	if (ire != NULL) {
22023 		ire_refrele(ire);
22024 		return (EEXIST);
22025 	}
22026 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
22027 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
22028 		    ipif->ipif_net_type));
22029 		return (EINVAL);
22030 	}
22031 
22032 	stq = ipif->ipif_wq;
22033 	ASSERT(stq != NULL);
22034 
22035 	/*
22036 	 * The outgoing interface must be non-resolver
22037 	 * interface.
22038 	 */
22039 	dlureq_mp = ill_dlur_gen(NULL,
22040 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
22041 	    out_ill->ill_sap_length);
22042 
22043 	if (dlureq_mp == NULL) {
22044 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
22045 		return (ENOMEM);
22046 	}
22047 
22048 	/* Create the IRE. */
22049 
22050 	ire = ire_create(
22051 	    NULL,				/* Zero dst addr */
22052 	    NULL,				/* Zero mask */
22053 	    NULL,				/* Zero gateway addr */
22054 	    NULL,				/* Zero ipif_src addr */
22055 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
22056 	    &ipif->ipif_mtu,
22057 	    NULL,
22058 	    NULL,				/* rfq */
22059 	    stq,
22060 	    IRE_MIPRTUN,
22061 	    dlureq_mp,
22062 	    ipif,
22063 	    in_ill,
22064 	    0,
22065 	    0,
22066 	    0,
22067 	    flags,
22068 	    &ire_uinfo_null,
22069 	    NULL,
22070 	    NULL);
22071 
22072 	if (ire == NULL)
22073 		return (ENOMEM);
22074 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
22075 	    ire->ire_type));
22076 	save_ire = ire;
22077 	ASSERT(save_ire != NULL);
22078 	error = ire_add_mrtun(&ire, q, mp, func);
22079 	/*
22080 	 * If ire_add_mrtun() failed, the ire passed in was freed
22081 	 * so there is no need to do so here.
22082 	 */
22083 	if (error != 0) {
22084 		return (error);
22085 	}
22086 
22087 	/* Duplicate check */
22088 	if (ire != save_ire) {
22089 		/* route already exists by now */
22090 		ire_refrele(ire);
22091 		return (EEXIST);
22092 	}
22093 
22094 	if (ire_arg != NULL) {
22095 		/*
22096 		 * Store the ire that was just added. the caller
22097 		 * ip_rts_request responsible for doing ire_refrele()
22098 		 * on it.
22099 		 */
22100 		*ire_arg = ire;
22101 	} else {
22102 		ire_refrele(ire);	/* held in ire_add_mrtun */
22103 	}
22104 
22105 	return (0);
22106 }
22107 
22108 /*
22109  * It is called by ip_rt_delete() only when mipagent requests to delete
22110  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
22111  */
22112 
22113 int
22114 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
22115 {
22116 	ire_t   *ire = NULL;
22117 
22118 	if (in_src_addr == INADDR_ANY)
22119 		return (EINVAL);
22120 	if (src_ipif == NULL)
22121 		return (EINVAL);
22122 
22123 	/* search if this route exists in the ip_mrtun_table */
22124 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
22125 	if (ire == NULL) {
22126 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
22127 		return (ESRCH);
22128 	}
22129 	ire_delete(ire);
22130 	ire_refrele(ire);
22131 	return (0);
22132 }
22133 
22134 /*
22135  * Lookup the ipif corresponding to the onlink destination address. For
22136  * point-to-point interfaces, it matches with remote endpoint destination
22137  * address. For point-to-multipoint interfaces it only tries to match the
22138  * destination with the interface's subnet address. The longest, most specific
22139  * match is found to take care of such rare network configurations like -
22140  * le0: 129.146.1.1/16
22141  * le1: 129.146.2.2/24
22142  * It is used only by SO_DONTROUTE at the moment.
22143  */
22144 ipif_t *
22145 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
22146 {
22147 	ipif_t	*ipif, *best_ipif;
22148 	ill_t	*ill;
22149 	ill_walk_context_t ctx;
22150 
22151 	ASSERT(zoneid != ALL_ZONES);
22152 	best_ipif = NULL;
22153 
22154 	rw_enter(&ill_g_lock, RW_READER);
22155 	ill = ILL_START_WALK_V4(&ctx);
22156 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22157 		mutex_enter(&ill->ill_lock);
22158 		for (ipif = ill->ill_ipif; ipif != NULL;
22159 		    ipif = ipif->ipif_next) {
22160 			if (!IPIF_CAN_LOOKUP(ipif))
22161 				continue;
22162 			if (ipif->ipif_zoneid != zoneid &&
22163 			    ipif->ipif_zoneid != ALL_ZONES)
22164 				continue;
22165 			/*
22166 			 * Point-to-point case. Look for exact match with
22167 			 * destination address.
22168 			 */
22169 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
22170 				if (ipif->ipif_pp_dst_addr == addr) {
22171 					ipif_refhold_locked(ipif);
22172 					mutex_exit(&ill->ill_lock);
22173 					rw_exit(&ill_g_lock);
22174 					if (best_ipif != NULL)
22175 						ipif_refrele(best_ipif);
22176 					return (ipif);
22177 				}
22178 			} else if (ipif->ipif_subnet == (addr &
22179 			    ipif->ipif_net_mask)) {
22180 				/*
22181 				 * Point-to-multipoint case. Looping through to
22182 				 * find the most specific match. If there are
22183 				 * multiple best match ipif's then prefer ipif's
22184 				 * that are UP. If there is only one best match
22185 				 * ipif and it is DOWN we must still return it.
22186 				 */
22187 				if ((best_ipif == NULL) ||
22188 				    (ipif->ipif_net_mask >
22189 				    best_ipif->ipif_net_mask) ||
22190 				    ((ipif->ipif_net_mask ==
22191 				    best_ipif->ipif_net_mask) &&
22192 				    ((ipif->ipif_flags & IPIF_UP) &&
22193 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
22194 					ipif_refhold_locked(ipif);
22195 					mutex_exit(&ill->ill_lock);
22196 					rw_exit(&ill_g_lock);
22197 					if (best_ipif != NULL)
22198 						ipif_refrele(best_ipif);
22199 					best_ipif = ipif;
22200 					rw_enter(&ill_g_lock, RW_READER);
22201 					mutex_enter(&ill->ill_lock);
22202 				}
22203 			}
22204 		}
22205 		mutex_exit(&ill->ill_lock);
22206 	}
22207 	rw_exit(&ill_g_lock);
22208 	return (best_ipif);
22209 }
22210 
22211 
22212 /*
22213  * Save enough information so that we can recreate the IRE if
22214  * the interface goes down and then up.
22215  */
22216 static void
22217 ipif_save_ire(ipif_t *ipif, ire_t *ire)
22218 {
22219 	mblk_t	*save_mp;
22220 
22221 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
22222 	if (save_mp != NULL) {
22223 		ifrt_t	*ifrt;
22224 
22225 		save_mp->b_wptr += sizeof (ifrt_t);
22226 		ifrt = (ifrt_t *)save_mp->b_rptr;
22227 		bzero(ifrt, sizeof (ifrt_t));
22228 		ifrt->ifrt_type = ire->ire_type;
22229 		ifrt->ifrt_addr = ire->ire_addr;
22230 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
22231 		ifrt->ifrt_src_addr = ire->ire_src_addr;
22232 		ifrt->ifrt_mask = ire->ire_mask;
22233 		ifrt->ifrt_flags = ire->ire_flags;
22234 		ifrt->ifrt_max_frag = ire->ire_max_frag;
22235 		mutex_enter(&ipif->ipif_saved_ire_lock);
22236 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
22237 		ipif->ipif_saved_ire_mp = save_mp;
22238 		ipif->ipif_saved_ire_cnt++;
22239 		mutex_exit(&ipif->ipif_saved_ire_lock);
22240 	}
22241 }
22242 
22243 
22244 static void
22245 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
22246 {
22247 	mblk_t	**mpp;
22248 	mblk_t	*mp;
22249 	ifrt_t	*ifrt;
22250 
22251 	/* Remove from ipif_saved_ire_mp list if it is there */
22252 	mutex_enter(&ipif->ipif_saved_ire_lock);
22253 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
22254 	    mpp = &(*mpp)->b_cont) {
22255 		/*
22256 		 * On a given ipif, the triple of address, gateway and
22257 		 * mask is unique for each saved IRE (in the case of
22258 		 * ordinary interface routes, the gateway address is
22259 		 * all-zeroes).
22260 		 */
22261 		mp = *mpp;
22262 		ifrt = (ifrt_t *)mp->b_rptr;
22263 		if (ifrt->ifrt_addr == ire->ire_addr &&
22264 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
22265 		    ifrt->ifrt_mask == ire->ire_mask) {
22266 			*mpp = mp->b_cont;
22267 			ipif->ipif_saved_ire_cnt--;
22268 			freeb(mp);
22269 			break;
22270 		}
22271 	}
22272 	mutex_exit(&ipif->ipif_saved_ire_lock);
22273 }
22274 
22275 
22276 /*
22277  * IP multirouting broadcast routes handling
22278  * Append CGTP broadcast IREs to regular ones created
22279  * at ifconfig time.
22280  */
22281 static void
22282 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22283 {
22284 	ire_t *ire_prim;
22285 
22286 	ASSERT(ire != NULL);
22287 	ASSERT(ire_dst != NULL);
22288 
22289 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22290 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22291 	if (ire_prim != NULL) {
22292 		/*
22293 		 * We are in the special case of broadcasts for
22294 		 * CGTP. We add an IRE_BROADCAST that holds
22295 		 * the RTF_MULTIRT flag, the destination
22296 		 * address of ire_dst and the low level
22297 		 * info of ire_prim. In other words, CGTP
22298 		 * broadcast is added to the redundant ipif.
22299 		 */
22300 		ipif_t *ipif_prim;
22301 		ire_t  *bcast_ire;
22302 
22303 		ipif_prim = ire_prim->ire_ipif;
22304 
22305 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22306 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22307 		    (void *)ire_dst, (void *)ire_prim,
22308 		    (void *)ipif_prim));
22309 
22310 		bcast_ire = ire_create(
22311 		    (uchar_t *)&ire->ire_addr,
22312 		    (uchar_t *)&ip_g_all_ones,
22313 		    (uchar_t *)&ire_dst->ire_src_addr,
22314 		    (uchar_t *)&ire->ire_gateway_addr,
22315 		    NULL,
22316 		    &ipif_prim->ipif_mtu,
22317 		    NULL,
22318 		    ipif_prim->ipif_rq,
22319 		    ipif_prim->ipif_wq,
22320 		    IRE_BROADCAST,
22321 		    ipif_prim->ipif_bcast_mp,
22322 		    ipif_prim,
22323 		    NULL,
22324 		    0,
22325 		    0,
22326 		    0,
22327 		    ire->ire_flags,
22328 		    &ire_uinfo_null,
22329 		    NULL,
22330 		    NULL);
22331 
22332 		if (bcast_ire != NULL) {
22333 
22334 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
22335 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22336 				    "added bcast_ire %p\n",
22337 				    (void *)bcast_ire));
22338 
22339 				ipif_save_ire(bcast_ire->ire_ipif,
22340 				    bcast_ire);
22341 				ire_refrele(bcast_ire);
22342 			}
22343 		}
22344 		ire_refrele(ire_prim);
22345 	}
22346 }
22347 
22348 
22349 /*
22350  * IP multirouting broadcast routes handling
22351  * Remove the broadcast ire
22352  */
22353 static void
22354 ip_cgtp_bcast_delete(ire_t *ire)
22355 {
22356 	ire_t *ire_dst;
22357 
22358 	ASSERT(ire != NULL);
22359 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22360 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22361 	if (ire_dst != NULL) {
22362 		ire_t *ire_prim;
22363 
22364 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22365 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22366 		if (ire_prim != NULL) {
22367 			ipif_t *ipif_prim;
22368 			ire_t  *bcast_ire;
22369 
22370 			ipif_prim = ire_prim->ire_ipif;
22371 
22372 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22373 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22374 			    (void *)ire_dst, (void *)ire_prim,
22375 			    (void *)ipif_prim));
22376 
22377 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22378 			    ire->ire_gateway_addr,
22379 			    IRE_BROADCAST,
22380 			    ipif_prim, ALL_ZONES,
22381 			    NULL,
22382 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22383 			    MATCH_IRE_MASK);
22384 
22385 			if (bcast_ire != NULL) {
22386 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22387 				    "looked up bcast_ire %p\n",
22388 				    (void *)bcast_ire));
22389 				ipif_remove_ire(bcast_ire->ire_ipif,
22390 					bcast_ire);
22391 				ire_delete(bcast_ire);
22392 			}
22393 			ire_refrele(ire_prim);
22394 		}
22395 		ire_refrele(ire_dst);
22396 	}
22397 }
22398 
22399 /*
22400  * IPsec hardware acceleration capabilities related functions.
22401  */
22402 
22403 /*
22404  * Free a per-ill IPsec capabilities structure.
22405  */
22406 static void
22407 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22408 {
22409 	if (capab->auth_hw_algs != NULL)
22410 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22411 	if (capab->encr_hw_algs != NULL)
22412 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22413 	if (capab->encr_algparm != NULL)
22414 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22415 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22416 }
22417 
22418 /*
22419  * Allocate a new per-ill IPsec capabilities structure. This structure
22420  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22421  * an array which specifies, for each algorithm, whether this algorithm
22422  * is supported by the ill or not.
22423  */
22424 static ill_ipsec_capab_t *
22425 ill_ipsec_capab_alloc(void)
22426 {
22427 	ill_ipsec_capab_t *capab;
22428 	uint_t nelems;
22429 
22430 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22431 	if (capab == NULL)
22432 		return (NULL);
22433 
22434 	/* we need one bit per algorithm */
22435 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22436 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22437 
22438 	/* allocate memory to store algorithm flags */
22439 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22440 	if (capab->encr_hw_algs == NULL)
22441 		goto nomem;
22442 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22443 	if (capab->auth_hw_algs == NULL)
22444 		goto nomem;
22445 	/*
22446 	 * Leave encr_algparm NULL for now since we won't need it half
22447 	 * the time
22448 	 */
22449 	return (capab);
22450 
22451 nomem:
22452 	ill_ipsec_capab_free(capab);
22453 	return (NULL);
22454 }
22455 
22456 /*
22457  * Resize capability array.  Since we're exclusive, this is OK.
22458  */
22459 static boolean_t
22460 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22461 {
22462 	ipsec_capab_algparm_t *nalp, *oalp;
22463 	uint32_t olen, nlen;
22464 
22465 	oalp = capab->encr_algparm;
22466 	olen = capab->encr_algparm_size;
22467 
22468 	if (oalp != NULL) {
22469 		if (algid < capab->encr_algparm_end)
22470 			return (B_TRUE);
22471 	}
22472 
22473 	nlen = (algid + 1) * sizeof (*nalp);
22474 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22475 	if (nalp == NULL)
22476 		return (B_FALSE);
22477 
22478 	if (oalp != NULL) {
22479 		bcopy(oalp, nalp, olen);
22480 		kmem_free(oalp, olen);
22481 	}
22482 	capab->encr_algparm = nalp;
22483 	capab->encr_algparm_size = nlen;
22484 	capab->encr_algparm_end = algid + 1;
22485 
22486 	return (B_TRUE);
22487 }
22488 
22489 /*
22490  * Compare the capabilities of the specified ill with the protocol
22491  * and algorithms specified by the SA passed as argument.
22492  * If they match, returns B_TRUE, B_FALSE if they do not match.
22493  *
22494  * The ill can be passed as a pointer to it, or by specifying its index
22495  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22496  *
22497  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22498  * packet is eligible for hardware acceleration, and by
22499  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22500  * to a particular ill.
22501  */
22502 boolean_t
22503 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22504     ipsa_t *sa)
22505 {
22506 	boolean_t sa_isv6;
22507 	uint_t algid;
22508 	struct ill_ipsec_capab_s *cpp;
22509 	boolean_t need_refrele = B_FALSE;
22510 
22511 	if (ill == NULL) {
22512 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22513 		    NULL, NULL, NULL);
22514 		if (ill == NULL) {
22515 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22516 			return (B_FALSE);
22517 		}
22518 		need_refrele = B_TRUE;
22519 	}
22520 
22521 	/*
22522 	 * Use the address length specified by the SA to determine
22523 	 * if it corresponds to a IPv6 address, and fail the matching
22524 	 * if the isv6 flag passed as argument does not match.
22525 	 * Note: this check is used for SADB capability checking before
22526 	 * sending SA information to an ill.
22527 	 */
22528 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22529 	if (sa_isv6 != ill_isv6)
22530 		/* protocol mismatch */
22531 		goto done;
22532 
22533 	/*
22534 	 * Check if the ill supports the protocol, algorithm(s) and
22535 	 * key size(s) specified by the SA, and get the pointers to
22536 	 * the algorithms supported by the ill.
22537 	 */
22538 	switch (sa->ipsa_type) {
22539 
22540 	case SADB_SATYPE_ESP:
22541 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22542 			/* ill does not support ESP acceleration */
22543 			goto done;
22544 		cpp = ill->ill_ipsec_capab_esp;
22545 		algid = sa->ipsa_auth_alg;
22546 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22547 			goto done;
22548 		algid = sa->ipsa_encr_alg;
22549 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22550 			goto done;
22551 		if (algid < cpp->encr_algparm_end) {
22552 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22553 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22554 				goto done;
22555 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22556 				goto done;
22557 		}
22558 		break;
22559 
22560 	case SADB_SATYPE_AH:
22561 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22562 			/* ill does not support AH acceleration */
22563 			goto done;
22564 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22565 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22566 			goto done;
22567 		break;
22568 	}
22569 
22570 	if (need_refrele)
22571 		ill_refrele(ill);
22572 	return (B_TRUE);
22573 done:
22574 	if (need_refrele)
22575 		ill_refrele(ill);
22576 	return (B_FALSE);
22577 }
22578 
22579 
22580 /*
22581  * Add a new ill to the list of IPsec capable ills.
22582  * Called from ill_capability_ipsec_ack() when an ACK was received
22583  * indicating that IPsec hardware processing was enabled for an ill.
22584  *
22585  * ill must point to the ill for which acceleration was enabled.
22586  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22587  */
22588 static void
22589 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22590 {
22591 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22592 	uint_t sa_type;
22593 	uint_t ipproto;
22594 
22595 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22596 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22597 
22598 	switch (dl_cap) {
22599 	case DL_CAPAB_IPSEC_AH:
22600 		sa_type = SADB_SATYPE_AH;
22601 		ills = &ipsec_capab_ills_ah;
22602 		ipproto = IPPROTO_AH;
22603 		break;
22604 	case DL_CAPAB_IPSEC_ESP:
22605 		sa_type = SADB_SATYPE_ESP;
22606 		ills = &ipsec_capab_ills_esp;
22607 		ipproto = IPPROTO_ESP;
22608 		break;
22609 	}
22610 
22611 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22612 
22613 	/*
22614 	 * Add ill index to list of hardware accelerators. If
22615 	 * already in list, do nothing.
22616 	 */
22617 	for (cur_ill = *ills; cur_ill != NULL &&
22618 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22619 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22620 		;
22621 
22622 	if (cur_ill == NULL) {
22623 		/* if this is a new entry for this ill */
22624 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22625 		if (new_ill == NULL) {
22626 			rw_exit(&ipsec_capab_ills_lock);
22627 			return;
22628 		}
22629 
22630 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22631 		new_ill->ill_isv6 = ill->ill_isv6;
22632 		new_ill->next = *ills;
22633 		*ills = new_ill;
22634 	} else if (!sadb_resync) {
22635 		/* not resync'ing SADB and an entry exists for this ill */
22636 		rw_exit(&ipsec_capab_ills_lock);
22637 		return;
22638 	}
22639 
22640 	rw_exit(&ipsec_capab_ills_lock);
22641 
22642 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22643 		/*
22644 		 * IPsec module for protocol loaded, initiate dump
22645 		 * of the SADB to this ill.
22646 		 */
22647 		sadb_ill_download(ill, sa_type);
22648 }
22649 
22650 /*
22651  * Remove an ill from the list of IPsec capable ills.
22652  */
22653 static void
22654 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22655 {
22656 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22657 
22658 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22659 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22660 
22661 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22662 	    &ipsec_capab_ills_esp;
22663 
22664 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22665 
22666 	prev_ill = NULL;
22667 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22668 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22669 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22670 		;
22671 	if (cur_ill == NULL) {
22672 		/* entry not found */
22673 		rw_exit(&ipsec_capab_ills_lock);
22674 		return;
22675 	}
22676 	if (prev_ill == NULL) {
22677 		/* entry at front of list */
22678 		*ills = NULL;
22679 	} else {
22680 		prev_ill->next = cur_ill->next;
22681 	}
22682 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22683 	rw_exit(&ipsec_capab_ills_lock);
22684 }
22685 
22686 
22687 /*
22688  * Handling of DL_CONTROL_REQ messages that must be sent down to
22689  * an ill while having exclusive access.
22690  */
22691 /* ARGSUSED */
22692 static void
22693 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22694 {
22695 	ill_t *ill = (ill_t *)q->q_ptr;
22696 
22697 	ill_dlpi_send(ill, mp);
22698 }
22699 
22700 
22701 /*
22702  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22703  * supporting the specified IPsec protocol acceleration.
22704  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22705  * We free the mblk and, if sa is non-null, release the held referece.
22706  */
22707 void
22708 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22709 {
22710 	ipsec_capab_ill_t *ici, *cur_ici;
22711 	ill_t *ill;
22712 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22713 
22714 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22715 	    ipsec_capab_ills_esp;
22716 
22717 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22718 
22719 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22720 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22721 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22722 
22723 		/*
22724 		 * Handle the case where the ill goes away while the SADB is
22725 		 * attempting to send messages.  If it's going away, it's
22726 		 * nuking its shadow SADB, so we don't care..
22727 		 */
22728 
22729 		if (ill == NULL)
22730 			continue;
22731 
22732 		if (sa != NULL) {
22733 			/*
22734 			 * Make sure capabilities match before
22735 			 * sending SA to ill.
22736 			 */
22737 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22738 			    cur_ici->ill_isv6, sa)) {
22739 				ill_refrele(ill);
22740 				continue;
22741 			}
22742 
22743 			mutex_enter(&sa->ipsa_lock);
22744 			sa->ipsa_flags |= IPSA_F_HW;
22745 			mutex_exit(&sa->ipsa_lock);
22746 		}
22747 
22748 		/*
22749 		 * Copy template message, and add it to the front
22750 		 * of the mblk ship list. We want to avoid holding
22751 		 * the ipsec_capab_ills_lock while sending the
22752 		 * message to the ills.
22753 		 *
22754 		 * The b_next and b_prev are temporarily used
22755 		 * to build a list of mblks to be sent down, and to
22756 		 * save the ill to which they must be sent.
22757 		 */
22758 		nmp = copymsg(mp);
22759 		if (nmp == NULL) {
22760 			ill_refrele(ill);
22761 			continue;
22762 		}
22763 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22764 		nmp->b_next = mp_ship_list;
22765 		mp_ship_list = nmp;
22766 		nmp->b_prev = (mblk_t *)ill;
22767 	}
22768 
22769 	rw_exit(&ipsec_capab_ills_lock);
22770 
22771 	nmp = mp_ship_list;
22772 	while (nmp != NULL) {
22773 		/* restore the mblk to a sane state */
22774 		next_mp = nmp->b_next;
22775 		nmp->b_next = NULL;
22776 		ill = (ill_t *)nmp->b_prev;
22777 		nmp->b_prev = NULL;
22778 
22779 		/*
22780 		 * Ship the mblk to the ill, must be exclusive. Keep the
22781 		 * reference to the ill as qwriter_ip() does a ill_referele().
22782 		 */
22783 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22784 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22785 
22786 		nmp = next_mp;
22787 	}
22788 
22789 	if (sa != NULL)
22790 		IPSA_REFRELE(sa);
22791 	freemsg(mp);
22792 }
22793 
22794 
22795 /*
22796  * Derive an interface id from the link layer address.
22797  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22798  */
22799 static boolean_t
22800 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22801 {
22802 	char		*addr;
22803 
22804 	if (phys_length != ETHERADDRL)
22805 		return (B_FALSE);
22806 
22807 	/* Form EUI-64 like address */
22808 	addr = (char *)&v6addr->s6_addr32[2];
22809 	bcopy((char *)phys_addr, addr, 3);
22810 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22811 	addr[3] = (char)0xff;
22812 	addr[4] = (char)0xfe;
22813 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22814 	return (B_TRUE);
22815 }
22816 
22817 /* ARGSUSED */
22818 static boolean_t
22819 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22820 {
22821 	return (B_FALSE);
22822 }
22823 
22824 /* ARGSUSED */
22825 static boolean_t
22826 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22827     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22828 {
22829 	/*
22830 	 * Multicast address mappings used over Ethernet/802.X.
22831 	 * This address is used as a base for mappings.
22832 	 */
22833 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22834 	    0x00, 0x00, 0x00};
22835 
22836 	/*
22837 	 * Extract low order 32 bits from IPv6 multicast address.
22838 	 * Or that into the link layer address, starting from the
22839 	 * second byte.
22840 	 */
22841 	*hw_start = 2;
22842 	v6_extract_mask->s6_addr32[0] = 0;
22843 	v6_extract_mask->s6_addr32[1] = 0;
22844 	v6_extract_mask->s6_addr32[2] = 0;
22845 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22846 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22847 	return (B_TRUE);
22848 }
22849 
22850 /*
22851  * Indicate by return value whether multicast is supported. If not,
22852  * this code should not touch/change any parameters.
22853  */
22854 /* ARGSUSED */
22855 static boolean_t
22856 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22857     uint32_t *hw_start, ipaddr_t *extract_mask)
22858 {
22859 	/*
22860 	 * Multicast address mappings used over Ethernet/802.X.
22861 	 * This address is used as a base for mappings.
22862 	 */
22863 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22864 	    0x00, 0x00, 0x00 };
22865 
22866 	if (phys_length != ETHERADDRL)
22867 		return (B_FALSE);
22868 
22869 	*extract_mask = htonl(0x007fffff);
22870 	*hw_start = 2;
22871 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22872 	return (B_TRUE);
22873 }
22874 
22875 /*
22876  * Derive IPoIB interface id from the link layer address.
22877  */
22878 static boolean_t
22879 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22880 {
22881 	char		*addr;
22882 
22883 	if (phys_length != 20)
22884 		return (B_FALSE);
22885 	addr = (char *)&v6addr->s6_addr32[2];
22886 	bcopy(phys_addr + 12, addr, 8);
22887 	/*
22888 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22889 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22890 	 * rules. In these cases, the IBA considers these GUIDs to be in
22891 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22892 	 * required; vendors are required not to assign global EUI-64's
22893 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22894 	 * of the interface identifier. Whether the GUID is in modified
22895 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22896 	 * bit set to 1.
22897 	 */
22898 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22899 	return (B_TRUE);
22900 }
22901 
22902 /*
22903  * Note on mapping from multicast IP addresses to IPoIB multicast link
22904  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22905  * The format of an IPoIB multicast address is:
22906  *
22907  *  4 byte QPN      Scope Sign.  Pkey
22908  * +--------------------------------------------+
22909  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22910  * +--------------------------------------------+
22911  *
22912  * The Scope and Pkey components are properties of the IBA port and
22913  * network interface. They can be ascertained from the broadcast address.
22914  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22915  */
22916 
22917 static boolean_t
22918 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22919     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22920 {
22921 	/*
22922 	 * Base IPoIB IPv6 multicast address used for mappings.
22923 	 * Does not contain the IBA scope/Pkey values.
22924 	 */
22925 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22926 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22927 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22928 
22929 	/*
22930 	 * Extract low order 80 bits from IPv6 multicast address.
22931 	 * Or that into the link layer address, starting from the
22932 	 * sixth byte.
22933 	 */
22934 	*hw_start = 6;
22935 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22936 
22937 	/*
22938 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22939 	 */
22940 	*(maddr + 5) = *(bphys_addr + 5);
22941 	*(maddr + 8) = *(bphys_addr + 8);
22942 	*(maddr + 9) = *(bphys_addr + 9);
22943 
22944 	v6_extract_mask->s6_addr32[0] = 0;
22945 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22946 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22947 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22948 	return (B_TRUE);
22949 }
22950 
22951 static boolean_t
22952 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22953     uint32_t *hw_start, ipaddr_t *extract_mask)
22954 {
22955 	/*
22956 	 * Base IPoIB IPv4 multicast address used for mappings.
22957 	 * Does not contain the IBA scope/Pkey values.
22958 	 */
22959 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22960 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
22961 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22962 
22963 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
22964 		return (B_FALSE);
22965 
22966 	/*
22967 	 * Extract low order 28 bits from IPv4 multicast address.
22968 	 * Or that into the link layer address, starting from the
22969 	 * sixteenth byte.
22970 	 */
22971 	*extract_mask = htonl(0x0fffffff);
22972 	*hw_start = 16;
22973 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
22974 
22975 	/*
22976 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22977 	 */
22978 	*(maddr + 5) = *(bphys_addr + 5);
22979 	*(maddr + 8) = *(bphys_addr + 8);
22980 	*(maddr + 9) = *(bphys_addr + 9);
22981 	return (B_TRUE);
22982 }
22983 
22984 /*
22985  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
22986  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
22987  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
22988  * the link-local address is preferred.
22989  */
22990 boolean_t
22991 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
22992 {
22993 	ipif_t	*ipif;
22994 	ipif_t	*maybe_ipif = NULL;
22995 
22996 	mutex_enter(&ill->ill_lock);
22997 	if (ill->ill_state_flags & ILL_CONDEMNED) {
22998 		mutex_exit(&ill->ill_lock);
22999 		if (ipifp != NULL)
23000 			*ipifp = NULL;
23001 		return (B_FALSE);
23002 	}
23003 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23004 		if (!IPIF_CAN_LOOKUP(ipif))
23005 			continue;
23006 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23007 		    ipif->ipif_zoneid != ALL_ZONES)
23008 			continue;
23009 		if ((ipif->ipif_flags & flags) != flags)
23010 			continue;
23011 
23012 		if (ipifp == NULL) {
23013 			mutex_exit(&ill->ill_lock);
23014 			ASSERT(maybe_ipif == NULL);
23015 			return (B_TRUE);
23016 		}
23017 		if (!ill->ill_isv6 ||
23018 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23019 			ipif_refhold_locked(ipif);
23020 			mutex_exit(&ill->ill_lock);
23021 			*ipifp = ipif;
23022 			return (B_TRUE);
23023 		}
23024 		if (maybe_ipif == NULL)
23025 			maybe_ipif = ipif;
23026 	}
23027 	if (ipifp != NULL) {
23028 		if (maybe_ipif != NULL)
23029 			ipif_refhold_locked(maybe_ipif);
23030 		*ipifp = maybe_ipif;
23031 	}
23032 	mutex_exit(&ill->ill_lock);
23033 	return (maybe_ipif != NULL);
23034 }
23035 
23036 /*
23037  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23038  */
23039 boolean_t
23040 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23041 {
23042 	ill_t *illg;
23043 
23044 	/*
23045 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23046 	 */
23047 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23048 		return (B_TRUE);
23049 	}
23050 	rw_enter(&ill_g_lock, RW_READER);
23051 	if (ill->ill_group == NULL) {
23052 		/* ill not in a group */
23053 		rw_exit(&ill_g_lock);
23054 		return (B_FALSE);
23055 	}
23056 
23057 	/*
23058 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23059 	 * group. We need to look for an ipif in the zone on all the ills in the
23060 	 * group.
23061 	 */
23062 	illg = ill->ill_group->illgrp_ill;
23063 	do {
23064 		/*
23065 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23066 		 * that it's not there.
23067 		 */
23068 		if (illg != ill &&
23069 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23070 			break;
23071 		}
23072 	} while ((illg = illg->ill_group_next) != NULL);
23073 	rw_exit(&ill_g_lock);
23074 	return (illg != NULL);
23075 }
23076 
23077 /*
23078  * Check if this ill is only being used to send ICMP probes for IPMP
23079  */
23080 boolean_t
23081 ill_is_probeonly(ill_t *ill)
23082 {
23083 	/*
23084 	 * Check if the interface is FAILED, or INACTIVE
23085 	 */
23086 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23087 		return (B_TRUE);
23088 
23089 	return (B_FALSE);
23090 }
23091