xref: /titanic_50/usr/src/uts/common/inet/ip/ip_if.c (revision 587032cf0967234b39ccb50adca936a367841063)
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 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
6236 		    mask == IP_HOST_MASK) {
6237 			ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif,
6238 			    ALL_ZONES, NULL, match_flags);
6239 			if (ire != NULL) {
6240 				ire_refrele(ire);
6241 				if (ipif_refheld)
6242 					ipif_refrele(ipif);
6243 				return (EEXIST);
6244 			}
6245 			ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x"
6246 			    "for 0x%x\n", (void *)ipif,
6247 			    ipif->ipif_ire_type,
6248 			    ntohl(ipif->ipif_lcl_addr)));
6249 			ire = ire_create(
6250 			    (uchar_t *)&dst_addr,	/* dest address */
6251 			    (uchar_t *)&mask,		/* mask */
6252 			    (uchar_t *)&ipif->ipif_src_addr,
6253 			    NULL,			/* no gateway */
6254 			    NULL,
6255 			    &ipif->ipif_mtu,
6256 			    NULL,
6257 			    ipif->ipif_rq,		/* recv-from queue */
6258 			    NULL,			/* no send-to queue */
6259 			    ipif->ipif_ire_type,	/* LOOPBACK */
6260 			    NULL,
6261 			    ipif,
6262 			    NULL,
6263 			    0,
6264 			    0,
6265 			    0,
6266 			    (ipif->ipif_flags & IPIF_PRIVATE) ?
6267 			    RTF_PRIVATE : 0,
6268 			    &ire_uinfo_null,
6269 			    NULL,
6270 			    NULL);
6271 
6272 			if (ire == NULL) {
6273 				if (ipif_refheld)
6274 					ipif_refrele(ipif);
6275 				return (ENOMEM);
6276 			}
6277 			error = ire_add(&ire, q, mp, func);
6278 			if (error == 0)
6279 				goto save_ire;
6280 			if (ipif_refheld)
6281 				ipif_refrele(ipif);
6282 			return (error);
6283 
6284 		}
6285 	}
6286 
6287 	/*
6288 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
6289 	 * and the gateway address provided is one of the system's interface
6290 	 * addresses.  By using the routing socket interface and supplying an
6291 	 * RTA_IFP sockaddr with an interface index, an alternate method of
6292 	 * specifying an interface route to be created is available which uses
6293 	 * the interface index that specifies the outgoing interface rather than
6294 	 * the address of an outgoing interface (which may not be able to
6295 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
6296 	 * flag, routes can be specified which not only specify the next-hop to
6297 	 * be used when routing to a certain prefix, but also which outgoing
6298 	 * interface should be used.
6299 	 *
6300 	 * Previously, interfaces would have unique addresses assigned to them
6301 	 * and so the address assigned to a particular interface could be used
6302 	 * to identify a particular interface.  One exception to this was the
6303 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
6304 	 *
6305 	 * With the advent of IPv6 and its link-local addresses, this
6306 	 * restriction was relaxed and interfaces could share addresses between
6307 	 * themselves.  In fact, typically all of the link-local interfaces on
6308 	 * an IPv6 node or router will have the same link-local address.  In
6309 	 * order to differentiate between these interfaces, the use of an
6310 	 * interface index is necessary and this index can be carried inside a
6311 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
6312 	 * of using the interface index, however, is that all of the ipif's that
6313 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
6314 	 * cannot be used to differentiate between ipif's (or logical
6315 	 * interfaces) that belong to the same ill (physical interface).
6316 	 *
6317 	 * For example, in the following case involving IPv4 interfaces and
6318 	 * logical interfaces
6319 	 *
6320 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
6321 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0:1
6322 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0:2
6323 	 *
6324 	 * the ipif's corresponding to each of these interface routes can be
6325 	 * uniquely identified by the "gateway" (actually interface address).
6326 	 *
6327 	 * In this case involving multiple IPv6 default routes to a particular
6328 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
6329 	 * default route is of interest:
6330 	 *
6331 	 *	default		fe80::123:4567:89ab:cdef	U	if0
6332 	 *	default		fe80::123:4567:89ab:cdef	U	if1
6333 	 */
6334 
6335 	/* RTF_GATEWAY not set */
6336 	if (!(flags & RTF_GATEWAY)) {
6337 		queue_t	*stq;
6338 		queue_t	*rfq = NULL;
6339 		ill_t	*in_ill = NULL;
6340 
6341 		if (sp != NULL) {
6342 			ip2dbg(("ip_rt_add: gateway security attributes "
6343 			    "cannot be set with interface route\n"));
6344 			if (ipif_refheld)
6345 				ipif_refrele(ipif);
6346 			return (EINVAL);
6347 		}
6348 
6349 		/*
6350 		 * As the interface index specified with the RTA_IFP sockaddr is
6351 		 * the same for all ipif's off of an ill, the matching logic
6352 		 * below uses MATCH_IRE_ILL if such an index was specified.
6353 		 * This means that routes sharing the same prefix when added
6354 		 * using a RTA_IFP sockaddr must have distinct interface
6355 		 * indices (namely, they must be on distinct ill's).
6356 		 *
6357 		 * On the other hand, since the gateway address will usually be
6358 		 * different for each ipif on the system, the matching logic
6359 		 * uses MATCH_IRE_IPIF in the case of a traditional interface
6360 		 * route.  This means that interface routes for the same prefix
6361 		 * can be created if they belong to distinct ipif's and if a
6362 		 * RTA_IFP sockaddr is not present.
6363 		 */
6364 		if (ipif_arg != NULL) {
6365 			if (ipif_refheld)  {
6366 				ipif_refrele(ipif);
6367 				ipif_refheld = B_FALSE;
6368 			}
6369 			ipif = ipif_arg;
6370 			match_flags |= MATCH_IRE_ILL;
6371 		} else {
6372 			/*
6373 			 * Check the ipif corresponding to the gw_addr
6374 			 */
6375 			if (ipif == NULL)
6376 				return (ENETUNREACH);
6377 			match_flags |= MATCH_IRE_IPIF;
6378 		}
6379 		ASSERT(ipif != NULL);
6380 		/*
6381 		 * If src_ipif is not NULL, we have to create
6382 		 * an ire with non-null ire_in_ill value
6383 		 */
6384 		if (src_ipif != NULL) {
6385 			in_ill = src_ipif->ipif_ill;
6386 		}
6387 
6388 		/*
6389 		 * We check for an existing entry at this point.
6390 		 *
6391 		 * Since a netmask isn't passed in via the ioctl interface
6392 		 * (SIOCADDRT), we don't check for a matching netmask in that
6393 		 * case.
6394 		 */
6395 		if (!ioctl_msg)
6396 			match_flags |= MATCH_IRE_MASK;
6397 		if (src_ipif != NULL) {
6398 			/* Look up in the special table */
6399 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6400 			    ipif, src_ipif->ipif_ill, match_flags);
6401 		} else {
6402 			ire = ire_ftable_lookup(dst_addr, mask, 0,
6403 			    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6404 			    NULL, match_flags);
6405 		}
6406 		if (ire != NULL) {
6407 			ire_refrele(ire);
6408 			if (ipif_refheld)
6409 				ipif_refrele(ipif);
6410 			return (EEXIST);
6411 		}
6412 
6413 		if (src_ipif != NULL) {
6414 			/*
6415 			 * Create the special ire for the IRE table
6416 			 * which hangs out of ire_in_ill. This ire
6417 			 * is in-between IRE_CACHE and IRE_INTERFACE.
6418 			 * Thus rfq is non-NULL.
6419 			 */
6420 			rfq = ipif->ipif_rq;
6421 		}
6422 		/* Create the usual interface ires */
6423 
6424 		stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
6425 		    ? ipif->ipif_rq : ipif->ipif_wq;
6426 
6427 		/*
6428 		 * Create a copy of the IRE_LOOPBACK,
6429 		 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with
6430 		 * the modified address and netmask.
6431 		 */
6432 		ire = ire_create(
6433 		    (uchar_t *)&dst_addr,
6434 		    (uint8_t *)&mask,
6435 		    (uint8_t *)&ipif->ipif_src_addr,
6436 		    NULL,
6437 		    NULL,
6438 		    &ipif->ipif_mtu,
6439 		    NULL,
6440 		    rfq,
6441 		    stq,
6442 		    ipif->ipif_net_type,
6443 		    ipif->ipif_resolver_mp,
6444 		    ipif,
6445 		    in_ill,
6446 		    0,
6447 		    0,
6448 		    0,
6449 		    flags,
6450 		    &ire_uinfo_null,
6451 		    NULL,
6452 		    NULL);
6453 		if (ire == NULL) {
6454 			if (ipif_refheld)
6455 				ipif_refrele(ipif);
6456 			return (ENOMEM);
6457 		}
6458 
6459 		/*
6460 		 * Some software (for example, GateD and Sun Cluster) attempts
6461 		 * to create (what amount to) IRE_PREFIX routes with the
6462 		 * loopback address as the gateway.  This is primarily done to
6463 		 * set up prefixes with the RTF_REJECT flag set (for example,
6464 		 * when generating aggregate routes.)
6465 		 *
6466 		 * If the IRE type (as defined by ipif->ipif_net_type) is
6467 		 * IRE_LOOPBACK, then we map the request into a
6468 		 * IRE_IF_NORESOLVER.
6469 		 *
6470 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
6471 		 * routine, but rather using ire_create() directly.
6472 		 */
6473 		if (ipif->ipif_net_type == IRE_LOOPBACK)
6474 			ire->ire_type = IRE_IF_NORESOLVER;
6475 		error = ire_add(&ire, q, mp, func);
6476 		if (error == 0)
6477 			goto save_ire;
6478 
6479 		/*
6480 		 * In the result of failure, ire_add() will have already
6481 		 * deleted the ire in question, so there is no need to
6482 		 * do that here.
6483 		 */
6484 		if (ipif_refheld)
6485 			ipif_refrele(ipif);
6486 		return (error);
6487 	}
6488 	if (ipif_refheld) {
6489 		ipif_refrele(ipif);
6490 		ipif_refheld = B_FALSE;
6491 	}
6492 
6493 	if (src_ipif != NULL) {
6494 		/* RTA_SRCIFP is not supported on RTF_GATEWAY */
6495 		ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n"));
6496 		return (EINVAL);
6497 	}
6498 	/*
6499 	 * Get an interface IRE for the specified gateway.
6500 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
6501 	 * gateway, it is currently unreachable and we fail the request
6502 	 * accordingly.
6503 	 */
6504 	ipif = ipif_arg;
6505 	if (ipif_arg != NULL)
6506 		match_flags |= MATCH_IRE_ILL;
6507 	gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL,
6508 	    ALL_ZONES, 0, NULL, match_flags);
6509 	if (gw_ire == NULL)
6510 		return (ENETUNREACH);
6511 
6512 	/*
6513 	 * We create one of three types of IREs as a result of this request
6514 	 * based on the netmask.  A netmask of all ones (which is automatically
6515 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
6516 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
6517 	 * created.  Otherwise, an IRE_PREFIX route is created for the
6518 	 * destination prefix.
6519 	 */
6520 	if (mask == IP_HOST_MASK)
6521 		type = IRE_HOST;
6522 	else if (mask == 0)
6523 		type = IRE_DEFAULT;
6524 	else
6525 		type = IRE_PREFIX;
6526 
6527 	/* check for a duplicate entry */
6528 	ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg,
6529 	    NULL, ALL_ZONES, 0, NULL,
6530 	    match_flags | MATCH_IRE_MASK | MATCH_IRE_GW);
6531 	if (ire != NULL) {
6532 		ire_refrele(gw_ire);
6533 		ire_refrele(ire);
6534 		return (EEXIST);
6535 	}
6536 
6537 	/* Security attribute exists */
6538 	if (sp != NULL) {
6539 		tsol_gcgrp_addr_t ga;
6540 
6541 		/* find or create the gateway credentials group */
6542 		ga.ga_af = AF_INET;
6543 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
6544 
6545 		/* we hold reference to it upon success */
6546 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
6547 		if (gcgrp == NULL) {
6548 			ire_refrele(gw_ire);
6549 			return (ENOMEM);
6550 		}
6551 
6552 		/*
6553 		 * Create and add the security attribute to the group; a
6554 		 * reference to the group is made upon allocating a new
6555 		 * entry successfully.  If it finds an already-existing
6556 		 * entry for the security attribute in the group, it simply
6557 		 * returns it and no new reference is made to the group.
6558 		 */
6559 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
6560 		if (gc == NULL) {
6561 			/* release reference held by gcgrp_lookup */
6562 			GCGRP_REFRELE(gcgrp);
6563 			ire_refrele(gw_ire);
6564 			return (ENOMEM);
6565 		}
6566 	}
6567 
6568 	/* Create the IRE. */
6569 	ire = ire_create(
6570 	    (uchar_t *)&dst_addr,		/* dest address */
6571 	    (uchar_t *)&mask,			/* mask */
6572 	    /* src address assigned by the caller? */
6573 	    (uchar_t *)(((src_addr != INADDR_ANY) &&
6574 		(flags & RTF_SETSRC)) ?  &src_addr : NULL),
6575 	    (uchar_t *)&gw_addr,		/* gateway address */
6576 	    NULL,				/* no in-srcaddress */
6577 	    &gw_ire->ire_max_frag,
6578 	    NULL,				/* no Fast Path header */
6579 	    NULL,				/* no recv-from queue */
6580 	    NULL,				/* no send-to queue */
6581 	    (ushort_t)type,			/* IRE type */
6582 	    NULL,
6583 	    ipif_arg,
6584 	    NULL,
6585 	    0,
6586 	    0,
6587 	    0,
6588 	    flags,
6589 	    &gw_ire->ire_uinfo,			/* Inherit ULP info from gw */
6590 	    gc,					/* security attribute */
6591 	    NULL);
6592 	/*
6593 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
6594 	 * reference to the 'gcgrp'. We can now release the extra reference
6595 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
6596 	 */
6597 	if (gcgrp_xtraref)
6598 		GCGRP_REFRELE(gcgrp);
6599 	if (ire == NULL) {
6600 		if (gc != NULL)
6601 			GC_REFRELE(gc);
6602 		ire_refrele(gw_ire);
6603 		return (ENOMEM);
6604 	}
6605 
6606 	/*
6607 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
6608 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
6609 	 */
6610 
6611 	/* Add the new IRE. */
6612 	error = ire_add(&ire, q, mp, func);
6613 	if (error != 0) {
6614 		/*
6615 		 * In the result of failure, ire_add() will have already
6616 		 * deleted the ire in question, so there is no need to
6617 		 * do that here.
6618 		 */
6619 		ire_refrele(gw_ire);
6620 		return (error);
6621 	}
6622 
6623 	if (flags & RTF_MULTIRT) {
6624 		/*
6625 		 * Invoke the CGTP (multirouting) filtering module
6626 		 * to add the dst address in the filtering database.
6627 		 * Replicated inbound packets coming from that address
6628 		 * will be filtered to discard the duplicates.
6629 		 * It is not necessary to call the CGTP filter hook
6630 		 * when the dst address is a broadcast or multicast,
6631 		 * because an IP source address cannot be a broadcast
6632 		 * or a multicast.
6633 		 */
6634 		ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0,
6635 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
6636 		if (ire_dst != NULL) {
6637 			ip_cgtp_bcast_add(ire, ire_dst);
6638 			ire_refrele(ire_dst);
6639 			goto save_ire;
6640 		}
6641 		if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr)) {
6642 			int res = ip_cgtp_filter_ops->cfo_add_dest_v4(
6643 			    ire->ire_addr,
6644 			    ire->ire_gateway_addr,
6645 			    ire->ire_src_addr,
6646 			    gw_ire->ire_src_addr);
6647 			if (res != 0) {
6648 				ire_refrele(gw_ire);
6649 				ire_delete(ire);
6650 				return (res);
6651 			}
6652 		}
6653 	}
6654 
6655 	/*
6656 	 * Now that the prefix IRE entry has been created, delete any
6657 	 * existing gateway IRE cache entries as well as any IRE caches
6658 	 * using the gateway, and force them to be created through
6659 	 * ip_newroute.
6660 	 */
6661 	if (gc != NULL) {
6662 		ASSERT(gcgrp != NULL);
6663 		ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES);
6664 	}
6665 
6666 save_ire:
6667 	if (gw_ire != NULL) {
6668 		ire_refrele(gw_ire);
6669 	}
6670 	/*
6671 	 * We do not do save_ire for the routes added with RTA_SRCIFP
6672 	 * flag. This route is only added and deleted by mipagent.
6673 	 * So, for simplicity of design, we refrain from saving
6674 	 * ires that are created with srcif value. This may change
6675 	 * in future if we find more usage of srcifp feature.
6676 	 */
6677 	if (ipif != NULL && src_ipif == NULL) {
6678 		/*
6679 		 * Save enough information so that we can recreate the IRE if
6680 		 * the interface goes down and then up.  The metrics associated
6681 		 * with the route will be saved as well when rts_setmetrics() is
6682 		 * called after the IRE has been created.  In the case where
6683 		 * memory cannot be allocated, none of this information will be
6684 		 * saved.
6685 		 */
6686 		ipif_save_ire(ipif, ire);
6687 	}
6688 	if (ioctl_msg)
6689 		ip_rts_rtmsg(RTM_OLDADD, ire, 0);
6690 	if (ire_arg != NULL) {
6691 		/*
6692 		 * Store the ire that was successfully added into where ire_arg
6693 		 * points to so that callers don't have to look it up
6694 		 * themselves (but they are responsible for ire_refrele()ing
6695 		 * the ire when they are finished with it).
6696 		 */
6697 		*ire_arg = ire;
6698 	} else {
6699 		ire_refrele(ire);		/* Held in ire_add */
6700 	}
6701 	if (ipif_refheld)
6702 		ipif_refrele(ipif);
6703 	return (0);
6704 }
6705 
6706 /*
6707  * ip_rt_delete is called to delete an IPv4 route.
6708  * ipif_arg is passed in to associate it with the correct interface.
6709  * src_ipif is passed to associate the incoming interface of the packet.
6710  * We may need to restart this operation if the ipif cannot be looked up
6711  * due to an exclusive operation that is currently in progress. The restart
6712  * entry point is specified by 'func'
6713  */
6714 /* ARGSUSED4 */
6715 int
6716 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
6717     uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif,
6718     boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func)
6719 {
6720 	ire_t	*ire = NULL;
6721 	ipif_t	*ipif;
6722 	boolean_t ipif_refheld = B_FALSE;
6723 	uint_t	type;
6724 	uint_t	match_flags = MATCH_IRE_TYPE;
6725 	int	err = 0;
6726 
6727 	ip1dbg(("ip_rt_delete:"));
6728 	/*
6729 	 * If this is the case of RTF_HOST being set, then we set the netmask
6730 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
6731 	 */
6732 	if (flags & RTF_HOST) {
6733 		mask = IP_HOST_MASK;
6734 		match_flags |= MATCH_IRE_MASK;
6735 	} else if (rtm_addrs & RTA_NETMASK) {
6736 		match_flags |= MATCH_IRE_MASK;
6737 	}
6738 
6739 	/*
6740 	 * Note that RTF_GATEWAY is never set on a delete, therefore
6741 	 * we check if the gateway address is one of our interfaces first,
6742 	 * and fall back on RTF_GATEWAY routes.
6743 	 *
6744 	 * This makes it possible to delete an original
6745 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6746 	 *
6747 	 * As the interface index specified with the RTA_IFP sockaddr is the
6748 	 * same for all ipif's off of an ill, the matching logic below uses
6749 	 * MATCH_IRE_ILL if such an index was specified.  This means a route
6750 	 * sharing the same prefix and interface index as the the route
6751 	 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr
6752 	 * is specified in the request.
6753 	 *
6754 	 * On the other hand, since the gateway address will usually be
6755 	 * different for each ipif on the system, the matching logic
6756 	 * uses MATCH_IRE_IPIF in the case of a traditional interface
6757 	 * route.  This means that interface routes for the same prefix can be
6758 	 * uniquely identified if they belong to distinct ipif's and if a
6759 	 * RTA_IFP sockaddr is not present.
6760 	 *
6761 	 * For more detail on specifying routes by gateway address and by
6762 	 * interface index, see the comments in ip_rt_add().
6763 	 * gw_addr could be zero in some cases when both RTA_SRCIFP and
6764 	 * RTA_IFP are specified. If RTA_SRCIFP is specified and  both
6765 	 * RTA_IFP and gateway_addr are NULL/zero, then delete will not
6766 	 * succeed.
6767 	 */
6768 	if (src_ipif != NULL) {
6769 		if (ipif_arg == NULL && gw_addr != 0) {
6770 			ipif_arg = ipif_lookup_interface(gw_addr, dst_addr,
6771 			    q, mp, func, &err);
6772 			if (ipif_arg != NULL)
6773 				ipif_refheld = B_TRUE;
6774 		}
6775 		if (ipif_arg == NULL) {
6776 			err = (err == EINPROGRESS) ? err : ESRCH;
6777 			return (err);
6778 		}
6779 		ipif = ipif_arg;
6780 	} else {
6781 		ipif = ipif_lookup_interface(gw_addr, dst_addr,
6782 			    q, mp, func, &err);
6783 		if (ipif != NULL)
6784 			ipif_refheld = B_TRUE;
6785 		else if (err == EINPROGRESS)
6786 			return (err);
6787 		else
6788 			err = 0;
6789 	}
6790 	if (ipif != NULL) {
6791 		if (ipif_arg != NULL) {
6792 			if (ipif_refheld) {
6793 				ipif_refrele(ipif);
6794 				ipif_refheld = B_FALSE;
6795 			}
6796 			ipif = ipif_arg;
6797 			match_flags |= MATCH_IRE_ILL;
6798 		} else {
6799 			match_flags |= MATCH_IRE_IPIF;
6800 		}
6801 		if (src_ipif != NULL) {
6802 			ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE,
6803 			    ipif, src_ipif->ipif_ill, match_flags);
6804 		} else {
6805 			if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6806 				ire = ire_ctable_lookup(dst_addr, 0,
6807 				    IRE_LOOPBACK, ipif, ALL_ZONES, NULL,
6808 				    match_flags);
6809 			}
6810 			if (ire == NULL) {
6811 				ire = ire_ftable_lookup(dst_addr, mask, 0,
6812 				    IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
6813 				    NULL, match_flags);
6814 			}
6815 		}
6816 	}
6817 
6818 	if (ire == NULL) {
6819 		/*
6820 		 * At this point, the gateway address is not one of our own
6821 		 * addresses or a matching interface route was not found.  We
6822 		 * set the IRE type to lookup based on whether
6823 		 * this is a host route, a default route or just a prefix.
6824 		 *
6825 		 * If an ipif_arg was passed in, then the lookup is based on an
6826 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6827 		 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is
6828 		 * set as the route being looked up is not a traditional
6829 		 * interface route.
6830 		 * Since we do not add gateway route with srcipif, we don't
6831 		 * expect to find it either.
6832 		 */
6833 		if (src_ipif != NULL) {
6834 			if (ipif_refheld)
6835 				ipif_refrele(ipif);
6836 			return (ESRCH);
6837 		} else {
6838 			match_flags &= ~MATCH_IRE_IPIF;
6839 			match_flags |= MATCH_IRE_GW;
6840 			if (ipif_arg != NULL)
6841 				match_flags |= MATCH_IRE_ILL;
6842 			if (mask == IP_HOST_MASK)
6843 				type = IRE_HOST;
6844 			else if (mask == 0)
6845 				type = IRE_DEFAULT;
6846 			else
6847 				type = IRE_PREFIX;
6848 			ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type,
6849 			    ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags);
6850 			if (ire == NULL && type == IRE_HOST) {
6851 				ire = ire_ftable_lookup(dst_addr, mask, gw_addr,
6852 				    IRE_HOST_REDIRECT, ipif_arg, NULL,
6853 				    ALL_ZONES, 0, NULL, match_flags);
6854 			}
6855 		}
6856 	}
6857 
6858 	if (ipif_refheld)
6859 		ipif_refrele(ipif);
6860 
6861 	/* ipif is not refheld anymore */
6862 	if (ire == NULL)
6863 		return (ESRCH);
6864 
6865 	if (ire->ire_flags & RTF_MULTIRT) {
6866 		/*
6867 		 * Invoke the CGTP (multirouting) filtering module
6868 		 * to remove the dst address from the filtering database.
6869 		 * Packets coming from that address will no longer be
6870 		 * filtered to remove duplicates.
6871 		 */
6872 		if (ip_cgtp_filter_ops != NULL) {
6873 			err = ip_cgtp_filter_ops->cfo_del_dest_v4(ire->ire_addr,
6874 			    ire->ire_gateway_addr);
6875 		}
6876 		ip_cgtp_bcast_delete(ire);
6877 	}
6878 
6879 	ipif = ire->ire_ipif;
6880 	/*
6881 	 * Removing from ipif_saved_ire_mp is not necessary
6882 	 * when src_ipif being non-NULL. ip_rt_add does not
6883 	 * save the ires which src_ipif being non-NULL.
6884 	 */
6885 	if (ipif != NULL && src_ipif == NULL) {
6886 		ipif_remove_ire(ipif, ire);
6887 	}
6888 	if (ioctl_msg)
6889 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0);
6890 	ire_delete(ire);
6891 	ire_refrele(ire);
6892 	return (err);
6893 }
6894 
6895 /*
6896  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6897  */
6898 /* ARGSUSED */
6899 int
6900 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6901     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6902 {
6903 	ipaddr_t dst_addr;
6904 	ipaddr_t gw_addr;
6905 	ipaddr_t mask;
6906 	int error = 0;
6907 	mblk_t *mp1;
6908 	struct rtentry *rt;
6909 	ipif_t *ipif = NULL;
6910 
6911 	ip1dbg(("ip_siocaddrt:"));
6912 	/* Existence of mp1 verified in ip_wput_nondata */
6913 	mp1 = mp->b_cont->b_cont;
6914 	rt = (struct rtentry *)mp1->b_rptr;
6915 
6916 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6917 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6918 
6919 	/*
6920 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6921 	 * to a particular host address.  In this case, we set the netmask to
6922 	 * all ones for the particular destination address.  Otherwise,
6923 	 * determine the netmask to be used based on dst_addr and the interfaces
6924 	 * in use.
6925 	 */
6926 	if (rt->rt_flags & RTF_HOST) {
6927 		mask = IP_HOST_MASK;
6928 	} else {
6929 		/*
6930 		 * Note that ip_subnet_mask returns a zero mask in the case of
6931 		 * default (an all-zeroes address).
6932 		 */
6933 		mask = ip_subnet_mask(dst_addr, &ipif);
6934 	}
6935 
6936 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6937 	    NULL, B_TRUE, q, mp, ip_process_ioctl, NULL);
6938 	if (ipif != NULL)
6939 		ipif_refrele(ipif);
6940 	return (error);
6941 }
6942 
6943 /*
6944  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6945  */
6946 /* ARGSUSED */
6947 int
6948 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6949     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6950 {
6951 	ipaddr_t dst_addr;
6952 	ipaddr_t gw_addr;
6953 	ipaddr_t mask;
6954 	int error;
6955 	mblk_t *mp1;
6956 	struct rtentry *rt;
6957 	ipif_t *ipif = NULL;
6958 
6959 	ip1dbg(("ip_siocdelrt:"));
6960 	/* Existence of mp1 verified in ip_wput_nondata */
6961 	mp1 = mp->b_cont->b_cont;
6962 	rt = (struct rtentry *)mp1->b_rptr;
6963 
6964 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6965 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6966 
6967 	/*
6968 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6969 	 * to a particular host address.  In this case, we set the netmask to
6970 	 * all ones for the particular destination address.  Otherwise,
6971 	 * determine the netmask to be used based on dst_addr and the interfaces
6972 	 * in use.
6973 	 */
6974 	if (rt->rt_flags & RTF_HOST) {
6975 		mask = IP_HOST_MASK;
6976 	} else {
6977 		/*
6978 		 * Note that ip_subnet_mask returns a zero mask in the case of
6979 		 * default (an all-zeroes address).
6980 		 */
6981 		mask = ip_subnet_mask(dst_addr, &ipif);
6982 	}
6983 
6984 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6985 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL,
6986 	    B_TRUE, q, mp, ip_process_ioctl);
6987 	if (ipif != NULL)
6988 		ipif_refrele(ipif);
6989 	return (error);
6990 }
6991 
6992 /*
6993  * Enqueue the mp onto the ipsq, chained by b_next.
6994  * b_prev stores the function to be executed later, and b_queue the queue
6995  * where this mp originated.
6996  */
6997 void
6998 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6999     ill_t *pending_ill)
7000 {
7001 	conn_t	*connp = NULL;
7002 
7003 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7004 	ASSERT(func != NULL);
7005 
7006 	mp->b_queue = q;
7007 	mp->b_prev = (void *)func;
7008 	mp->b_next = NULL;
7009 
7010 	switch (type) {
7011 	case CUR_OP:
7012 		if (ipsq->ipsq_mptail != NULL) {
7013 			ASSERT(ipsq->ipsq_mphead != NULL);
7014 			ipsq->ipsq_mptail->b_next = mp;
7015 		} else {
7016 			ASSERT(ipsq->ipsq_mphead == NULL);
7017 			ipsq->ipsq_mphead = mp;
7018 		}
7019 		ipsq->ipsq_mptail = mp;
7020 		break;
7021 
7022 	case NEW_OP:
7023 		if (ipsq->ipsq_xopq_mptail != NULL) {
7024 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
7025 			ipsq->ipsq_xopq_mptail->b_next = mp;
7026 		} else {
7027 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
7028 			ipsq->ipsq_xopq_mphead = mp;
7029 		}
7030 		ipsq->ipsq_xopq_mptail = mp;
7031 		break;
7032 	default:
7033 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
7034 	}
7035 
7036 	if (CONN_Q(q) && pending_ill != NULL) {
7037 		connp = Q_TO_CONN(q);
7038 
7039 		ASSERT(MUTEX_HELD(&connp->conn_lock));
7040 		connp->conn_oper_pending_ill = pending_ill;
7041 	}
7042 }
7043 
7044 /*
7045  * Return the mp at the head of the ipsq. After emptying the ipsq
7046  * look at the next ioctl, if this ioctl is complete. Otherwise
7047  * return, we will resume when we complete the current ioctl.
7048  * The current ioctl will wait till it gets a response from the
7049  * driver below.
7050  */
7051 static mblk_t *
7052 ipsq_dq(ipsq_t *ipsq)
7053 {
7054 	mblk_t	*mp;
7055 
7056 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
7057 
7058 	mp = ipsq->ipsq_mphead;
7059 	if (mp != NULL) {
7060 		ipsq->ipsq_mphead = mp->b_next;
7061 		if (ipsq->ipsq_mphead == NULL)
7062 			ipsq->ipsq_mptail = NULL;
7063 		mp->b_next = NULL;
7064 		return (mp);
7065 	}
7066 	if (ipsq->ipsq_current_ipif != NULL)
7067 		return (NULL);
7068 	mp = ipsq->ipsq_xopq_mphead;
7069 	if (mp != NULL) {
7070 		ipsq->ipsq_xopq_mphead = mp->b_next;
7071 		if (ipsq->ipsq_xopq_mphead == NULL)
7072 			ipsq->ipsq_xopq_mptail = NULL;
7073 		mp->b_next = NULL;
7074 		return (mp);
7075 	}
7076 	return (NULL);
7077 }
7078 
7079 /*
7080  * Enter the ipsq corresponding to ill, by waiting synchronously till
7081  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
7082  * will have to drain completely before ipsq_enter returns success.
7083  * ipsq_current_ipif will be set if some exclusive ioctl is in progress,
7084  * and the ipsq_exit logic will start the next enqueued ioctl after
7085  * completion of the current ioctl. If 'force' is used, we don't wait
7086  * for the enqueued ioctls. This is needed when a conn_close wants to
7087  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
7088  * of an ill can also use this option. But we dont' use it currently.
7089  */
7090 #define	ENTER_SQ_WAIT_TICKS 100
7091 boolean_t
7092 ipsq_enter(ill_t *ill, boolean_t force)
7093 {
7094 	ipsq_t	*ipsq;
7095 	boolean_t waited_enough = B_FALSE;
7096 
7097 	/*
7098 	 * Holding the ill_lock prevents <ill-ipsq> assocs from changing.
7099 	 * Since the <ill-ipsq> assocs could change while we wait for the
7100 	 * writer, it is easier to wait on a fixed global rather than try to
7101 	 * cv_wait on a changing ipsq.
7102 	 */
7103 	mutex_enter(&ill->ill_lock);
7104 	for (;;) {
7105 		if (ill->ill_state_flags & ILL_CONDEMNED) {
7106 			mutex_exit(&ill->ill_lock);
7107 			return (B_FALSE);
7108 		}
7109 
7110 		ipsq = ill->ill_phyint->phyint_ipsq;
7111 		mutex_enter(&ipsq->ipsq_lock);
7112 		if (ipsq->ipsq_writer == NULL &&
7113 		    (ipsq->ipsq_current_ipif == NULL || waited_enough)) {
7114 			break;
7115 		} else if (ipsq->ipsq_writer != NULL) {
7116 			mutex_exit(&ipsq->ipsq_lock);
7117 			cv_wait(&ill->ill_cv, &ill->ill_lock);
7118 		} else {
7119 			mutex_exit(&ipsq->ipsq_lock);
7120 			if (force) {
7121 				(void) cv_timedwait(&ill->ill_cv,
7122 				    &ill->ill_lock,
7123 				    lbolt + ENTER_SQ_WAIT_TICKS);
7124 				waited_enough = B_TRUE;
7125 				continue;
7126 			} else {
7127 				cv_wait(&ill->ill_cv, &ill->ill_lock);
7128 			}
7129 		}
7130 	}
7131 
7132 	ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL);
7133 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7134 	ipsq->ipsq_writer = curthread;
7135 	ipsq->ipsq_reentry_cnt++;
7136 #ifdef ILL_DEBUG
7137 	ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7138 #endif
7139 	mutex_exit(&ipsq->ipsq_lock);
7140 	mutex_exit(&ill->ill_lock);
7141 	return (B_TRUE);
7142 }
7143 
7144 /*
7145  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
7146  * certain critical operations like plumbing (i.e. most set ioctls),
7147  * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP
7148  * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per
7149  * IPMP group. The ipsq serializes exclusive ioctls issued by applications
7150  * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple
7151  * threads executing in the ipsq. Responses from the driver pertain to the
7152  * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated
7153  * as part of bringing up the interface) and are enqueued in ipsq_mphead.
7154  *
7155  * If a thread does not want to reenter the ipsq when it is already writer,
7156  * it must make sure that the specified reentry point to be called later
7157  * when the ipsq is empty, nor any code path starting from the specified reentry
7158  * point must never ever try to enter the ipsq again. Otherwise it can lead
7159  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
7160  * When the thread that is currently exclusive finishes, it (ipsq_exit)
7161  * dequeues the requests waiting to become exclusive in ipsq_mphead and calls
7162  * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit
7163  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
7164  * ioctl if the current ioctl has completed. If the current ioctl is still
7165  * in progress it simply returns. The current ioctl could be waiting for
7166  * a response from another module (arp_ or the driver or could be waiting for
7167  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp
7168  * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the
7169  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
7170  * ipsq_current_ipif is clear which happens only on ioctl completion.
7171  */
7172 
7173 /*
7174  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7175  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7176  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7177  * completion.
7178  */
7179 ipsq_t *
7180 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7181     ipsq_func_t func, int type, boolean_t reentry_ok)
7182 {
7183 	ipsq_t	*ipsq;
7184 
7185 	/* Only 1 of ipif or ill can be specified */
7186 	ASSERT((ipif != NULL) ^ (ill != NULL));
7187 	if (ipif != NULL)
7188 		ill = ipif->ipif_ill;
7189 
7190 	/*
7191 	 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
7192 	 * ipsq of an ill can't change when ill_lock is held.
7193 	 */
7194 	GRAB_CONN_LOCK(q);
7195 	mutex_enter(&ill->ill_lock);
7196 	ipsq = ill->ill_phyint->phyint_ipsq;
7197 	mutex_enter(&ipsq->ipsq_lock);
7198 
7199 	/*
7200 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
7201 	 *    (Note: If the caller does not specify reentry_ok then neither
7202 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
7203 	 *    again. Otherwise it can lead to an infinite loop
7204 	 * 2. Enter the ipsq if there is no current writer and this attempted
7205 	 *    entry is part of the current ioctl or operation
7206 	 * 3. Enter the ipsq if there is no current writer and this is a new
7207 	 *    ioctl (or operation) and the ioctl (or operation) queue is
7208 	 *    empty and there is no ioctl (or operation) currently in progress
7209 	 */
7210 	if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) ||
7211 	    (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL &&
7212 	    ipsq->ipsq_current_ipif == NULL))) ||
7213 	    (ipsq->ipsq_writer == curthread && reentry_ok)) {
7214 		/* Success. */
7215 		ipsq->ipsq_reentry_cnt++;
7216 		ipsq->ipsq_writer = curthread;
7217 		mutex_exit(&ipsq->ipsq_lock);
7218 		mutex_exit(&ill->ill_lock);
7219 		RELEASE_CONN_LOCK(q);
7220 #ifdef ILL_DEBUG
7221 		ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH);
7222 #endif
7223 		return (ipsq);
7224 	}
7225 
7226 	ipsq_enq(ipsq, q, mp, func, type, ill);
7227 
7228 	mutex_exit(&ipsq->ipsq_lock);
7229 	mutex_exit(&ill->ill_lock);
7230 	RELEASE_CONN_LOCK(q);
7231 	return (NULL);
7232 }
7233 
7234 /*
7235  * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of
7236  * ipif or ill can be specified). The caller ensures ipif or ill is valid by
7237  * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued
7238  * completion.
7239  *
7240  * This function does a refrele on the ipif/ill.
7241  */
7242 void
7243 qwriter_ip(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
7244     ipsq_func_t func, int type, boolean_t reentry_ok)
7245 {
7246 	ipsq_t	*ipsq;
7247 
7248 	ipsq = ipsq_try_enter(ipif, ill, q, mp, func, type, reentry_ok);
7249 	/*
7250 	 * Caller must have done a refhold on the ipif. ipif_refrele
7251 	 * happens on the passed ipif. We can do this since we are
7252 	 * already exclusive, or we won't access ipif henceforth, Both
7253 	 * this func and caller will just return if we ipsq_try_enter
7254 	 * fails above. This is needed because func needs to
7255 	 * see the correct refcount. Eg. removeif can work only then.
7256 	 */
7257 	if (ipif != NULL)
7258 		ipif_refrele(ipif);
7259 	else
7260 		ill_refrele(ill);
7261 	if (ipsq != NULL) {
7262 		(*func)(ipsq, q, mp, NULL);
7263 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
7264 	}
7265 }
7266 
7267 /*
7268  * If there are more than ILL_GRP_CNT ills in a group,
7269  * we use kmem alloc'd buffers, else use the stack
7270  */
7271 #define	ILL_GRP_CNT	14
7272 /*
7273  * Drain the ipsq, if there are messages on it, and then leave the ipsq.
7274  * Called by a thread that is currently exclusive on this ipsq.
7275  */
7276 void
7277 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer)
7278 {
7279 	queue_t	*q;
7280 	mblk_t	*mp;
7281 	ipsq_func_t	func;
7282 	int	next;
7283 	ill_t	**ill_list = NULL;
7284 	size_t	ill_list_size = 0;
7285 	int	cnt = 0;
7286 	boolean_t need_ipsq_free = B_FALSE;
7287 
7288 	ASSERT(IAM_WRITER_IPSQ(ipsq));
7289 	mutex_enter(&ipsq->ipsq_lock);
7290 	ASSERT(ipsq->ipsq_reentry_cnt >= 1);
7291 	if (ipsq->ipsq_reentry_cnt != 1) {
7292 		ipsq->ipsq_reentry_cnt--;
7293 		mutex_exit(&ipsq->ipsq_lock);
7294 		return;
7295 	}
7296 
7297 	mp = ipsq_dq(ipsq);
7298 	while (mp != NULL) {
7299 again:
7300 		mutex_exit(&ipsq->ipsq_lock);
7301 		func = (ipsq_func_t)mp->b_prev;
7302 		q = (queue_t *)mp->b_queue;
7303 		mp->b_prev = NULL;
7304 		mp->b_queue = NULL;
7305 
7306 		/*
7307 		 * If 'q' is an conn queue, it is valid, since we did a
7308 		 * a refhold on the connp, at the start of the ioctl.
7309 		 * If 'q' is an ill queue, it is valid, since close of an
7310 		 * ill will clean up the 'ipsq'.
7311 		 */
7312 		(*func)(ipsq, q, mp, NULL);
7313 
7314 		mutex_enter(&ipsq->ipsq_lock);
7315 		mp = ipsq_dq(ipsq);
7316 	}
7317 
7318 	mutex_exit(&ipsq->ipsq_lock);
7319 
7320 	/*
7321 	 * Need to grab the locks in the right order. Need to
7322 	 * atomically check (under ipsq_lock) that there are no
7323 	 * messages before relinquishing the ipsq. Also need to
7324 	 * atomically wakeup waiters on ill_cv while holding ill_lock.
7325 	 * Holding ill_g_lock ensures that ipsq list of ills is stable.
7326 	 * If we need to call ill_split_ipsq and change <ill-ipsq> we need
7327 	 * to grab ill_g_lock as writer.
7328 	 */
7329 	rw_enter(&ill_g_lock, ipsq->ipsq_split ? RW_WRITER : RW_READER);
7330 
7331 	/* ipsq_refs can't change while ill_g_lock is held as reader */
7332 	if (ipsq->ipsq_refs != 0) {
7333 		/* At most 2 ills v4/v6 per phyint */
7334 		cnt = ipsq->ipsq_refs << 1;
7335 		ill_list_size = cnt * sizeof (ill_t *);
7336 		/*
7337 		 * If memory allocation fails, we will do the split
7338 		 * the next time ipsq_exit is called for whatever reason.
7339 		 * As long as the ipsq_split flag is set the need to
7340 		 * split is remembered.
7341 		 */
7342 		ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
7343 		if (ill_list != NULL)
7344 			cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt);
7345 	}
7346 	mutex_enter(&ipsq->ipsq_lock);
7347 	mp = ipsq_dq(ipsq);
7348 	if (mp != NULL) {
7349 		/* oops, some message has landed up, we can't get out */
7350 		if (ill_list != NULL)
7351 			ill_unlock_ills(ill_list, cnt);
7352 		rw_exit(&ill_g_lock);
7353 		if (ill_list != NULL)
7354 			kmem_free(ill_list, ill_list_size);
7355 		ill_list = NULL;
7356 		ill_list_size = 0;
7357 		cnt = 0;
7358 		goto again;
7359 	}
7360 
7361 	/*
7362 	 * Split only if no ioctl is pending and if memory alloc succeeded
7363 	 * above.
7364 	 */
7365 	if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL &&
7366 		ill_list != NULL) {
7367 		/*
7368 		 * No new ill can join this ipsq since we are holding the
7369 		 * ill_g_lock. Hence ill_split_ipsq can safely traverse the
7370 		 * ipsq. ill_split_ipsq may fail due to memory shortage.
7371 		 * If so we will retry on the next ipsq_exit.
7372 		 */
7373 		ipsq->ipsq_split = ill_split_ipsq(ipsq);
7374 	}
7375 
7376 	/*
7377 	 * We are holding the ipsq lock, hence no new messages can
7378 	 * land up on the ipsq, and there are no messages currently.
7379 	 * Now safe to get out. Wake up waiters and relinquish ipsq
7380 	 * atomically while holding ill locks.
7381 	 */
7382 	ipsq->ipsq_writer = NULL;
7383 	ipsq->ipsq_reentry_cnt--;
7384 	ASSERT(ipsq->ipsq_reentry_cnt == 0);
7385 #ifdef ILL_DEBUG
7386 	ipsq->ipsq_depth = 0;
7387 #endif
7388 	mutex_exit(&ipsq->ipsq_lock);
7389 	/*
7390 	 * For IPMP this should wake up all ills in this ipsq.
7391 	 * We need to hold the ill_lock while waking up waiters to
7392 	 * avoid missed wakeups. But there is no need to acquire all
7393 	 * the ill locks and then wakeup. If we have not acquired all
7394 	 * the locks (due to memory failure above) ill_signal_ipsq_ills
7395 	 * wakes up ills one at a time after getting the right ill_lock
7396 	 */
7397 	ill_signal_ipsq_ills(ipsq, ill_list != NULL);
7398 	if (ill_list != NULL)
7399 		ill_unlock_ills(ill_list, cnt);
7400 	if (ipsq->ipsq_refs == 0)
7401 		need_ipsq_free = B_TRUE;
7402 	rw_exit(&ill_g_lock);
7403 	if (ill_list != 0)
7404 		kmem_free(ill_list, ill_list_size);
7405 
7406 	if (need_ipsq_free) {
7407 		/*
7408 		 * Free the ipsq. ipsq_refs can't increase because ipsq can't be
7409 		 * looked up. ipsq can be looked up only thru ill or phyint
7410 		 * and there are no ills/phyint on this ipsq.
7411 		 */
7412 		ipsq_delete(ipsq);
7413 	}
7414 	/*
7415 	 * Now start any igmp or mld timers that could not be started
7416 	 * while inside the ipsq. The timers can't be started while inside
7417 	 * the ipsq, since igmp_start_timers may need to call untimeout()
7418 	 * which can't be done while holding a lock i.e. the ipsq. Otherwise
7419 	 * there could be a deadlock since the timeout handlers
7420 	 * mld_timeout_handler / igmp_timeout_handler also synchronously
7421 	 * wait in ipsq_enter() trying to get the ipsq.
7422 	 *
7423 	 * However there is one exception to the above. If this thread is
7424 	 * itself the igmp/mld timeout handler thread, then we don't want
7425 	 * to start any new timer until the current handler is done. The
7426 	 * handler thread passes in B_FALSE for start_igmp/mld_timers, while
7427 	 * all others pass B_TRUE.
7428 	 */
7429 	if (start_igmp_timer) {
7430 		mutex_enter(&igmp_timer_lock);
7431 		next = igmp_deferred_next;
7432 		igmp_deferred_next = INFINITY;
7433 		mutex_exit(&igmp_timer_lock);
7434 
7435 		if (next != INFINITY)
7436 			igmp_start_timers(next);
7437 	}
7438 
7439 	if (start_mld_timer) {
7440 		mutex_enter(&mld_timer_lock);
7441 		next = mld_deferred_next;
7442 		mld_deferred_next = INFINITY;
7443 		mutex_exit(&mld_timer_lock);
7444 
7445 		if (next != INFINITY)
7446 			mld_start_timers(next);
7447 	}
7448 }
7449 
7450 /*
7451  * The ill is closing. Flush all messages on the ipsq that originated
7452  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
7453  * for this ill since ipsq_enter could not have entered until then.
7454  * New messages can't be queued since the CONDEMNED flag is set.
7455  */
7456 static void
7457 ipsq_flush(ill_t *ill)
7458 {
7459 	queue_t	*q;
7460 	mblk_t	*prev;
7461 	mblk_t	*mp;
7462 	mblk_t	*mp_next;
7463 	ipsq_t	*ipsq;
7464 
7465 	ASSERT(IAM_WRITER_ILL(ill));
7466 	ipsq = ill->ill_phyint->phyint_ipsq;
7467 	/*
7468 	 * Flush any messages sent up by the driver.
7469 	 */
7470 	mutex_enter(&ipsq->ipsq_lock);
7471 	for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) {
7472 		mp_next = mp->b_next;
7473 		q = mp->b_queue;
7474 		if (q == ill->ill_rq || q == ill->ill_wq) {
7475 			/* Remove the mp from the ipsq */
7476 			if (prev == NULL)
7477 				ipsq->ipsq_mphead = mp->b_next;
7478 			else
7479 				prev->b_next = mp->b_next;
7480 			if (ipsq->ipsq_mptail == mp) {
7481 				ASSERT(mp_next == NULL);
7482 				ipsq->ipsq_mptail = prev;
7483 			}
7484 			inet_freemsg(mp);
7485 		} else {
7486 			prev = mp;
7487 		}
7488 	}
7489 	mutex_exit(&ipsq->ipsq_lock);
7490 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7491 	ipsq_xopq_mp_cleanup(ill, NULL);
7492 	ill_pending_mp_cleanup(ill);
7493 }
7494 
7495 /*
7496  * Clean up one squeue element. ill_inuse_ref is protected by ill_lock.
7497  * The real cleanup happens behind the squeue via ip_squeue_clean function but
7498  * we need to protect ourselfs from 2 threads trying to cleanup at the same
7499  * time (possible with one port going down for aggr and someone tearing down the
7500  * entire aggr simultaneously. So we use ill_inuse_ref protected by ill_lock
7501  * to indicate when the cleanup has started (1 ref) and when the cleanup
7502  * is done (0 ref). When a new ring gets assigned to squeue, we start by
7503  * putting 2 ref on ill_inuse_ref.
7504  */
7505 static void
7506 ipsq_clean_ring(ill_t *ill, ill_rx_ring_t *rx_ring)
7507 {
7508 	conn_t *connp;
7509 	squeue_t *sqp;
7510 	mblk_t *mp;
7511 
7512 	ASSERT(rx_ring != NULL);
7513 
7514 	/* Just clean one squeue */
7515 	mutex_enter(&ill->ill_lock);
7516 	/*
7517 	 * Reset the ILL_SOFT_RING_ASSIGN bit so that
7518 	 * ip_squeue_soft_ring_affinty() will not go
7519 	 * ahead with assigning rings.
7520 	 */
7521 	ill->ill_state_flags &= ~ILL_SOFT_RING_ASSIGN;
7522 	while (rx_ring->rr_ring_state == ILL_RING_INPROC)
7523 		/* Some operations pending on the ring. Wait */
7524 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7525 
7526 	if (rx_ring->rr_ring_state != ILL_RING_INUSE) {
7527 		/*
7528 		 * Someone already trying to clean
7529 		 * this squeue or its already been cleaned.
7530 		 */
7531 		mutex_exit(&ill->ill_lock);
7532 		return;
7533 	}
7534 	sqp = rx_ring->rr_sqp;
7535 
7536 	if (sqp == NULL) {
7537 		/*
7538 		 * The rx_ring never had a squeue assigned to it.
7539 		 * We are under ill_lock so we can clean it up
7540 		 * here itself since no one can get to it.
7541 		 */
7542 		rx_ring->rr_blank = NULL;
7543 		rx_ring->rr_handle = NULL;
7544 		rx_ring->rr_sqp = NULL;
7545 		rx_ring->rr_ring_state = ILL_RING_FREE;
7546 		mutex_exit(&ill->ill_lock);
7547 		return;
7548 	}
7549 
7550 	/* Set the state that its being cleaned */
7551 	rx_ring->rr_ring_state = ILL_RING_BEING_FREED;
7552 	ASSERT(sqp != NULL);
7553 	mutex_exit(&ill->ill_lock);
7554 
7555 	/*
7556 	 * Use the preallocated ill_unbind_conn for this purpose
7557 	 */
7558 	connp = ill->ill_dls_capab->ill_unbind_conn;
7559 	mp = &connp->conn_tcp->tcp_closemp;
7560 	CONN_INC_REF(connp);
7561 	squeue_enter(sqp, mp, ip_squeue_clean, connp, NULL);
7562 
7563 	mutex_enter(&ill->ill_lock);
7564 	while (rx_ring->rr_ring_state != ILL_RING_FREE)
7565 		cv_wait(&ill->ill_cv, &ill->ill_lock);
7566 
7567 	mutex_exit(&ill->ill_lock);
7568 }
7569 
7570 static void
7571 ipsq_clean_all(ill_t *ill)
7572 {
7573 	int idx;
7574 
7575 	/*
7576 	 * No need to clean if poll_capab isn't set for this ill
7577 	 */
7578 	if (!(ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)))
7579 		return;
7580 
7581 	for (idx = 0; idx < ILL_MAX_RINGS; idx++) {
7582 		ill_rx_ring_t *ipr = &ill->ill_dls_capab->ill_ring_tbl[idx];
7583 		ipsq_clean_ring(ill, ipr);
7584 	}
7585 
7586 	ill->ill_capabilities &= ~(ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING);
7587 }
7588 
7589 /* ARGSUSED */
7590 int
7591 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7592     ip_ioctl_cmd_t *ipip, void *ifreq)
7593 {
7594 	ill_t	*ill;
7595 	struct lifreq	*lifr = (struct lifreq *)ifreq;
7596 	boolean_t isv6;
7597 	conn_t	*connp;
7598 
7599 	connp = Q_TO_CONN(q);
7600 	isv6 = connp->conn_af_isv6;
7601 	/*
7602 	 * Set original index.
7603 	 * Failover and failback move logical interfaces
7604 	 * from one physical interface to another.  The
7605 	 * original index indicates the parent of a logical
7606 	 * interface, in other words, the physical interface
7607 	 * the logical interface will be moved back to on
7608 	 * failback.
7609 	 */
7610 
7611 	/*
7612 	 * Don't allow the original index to be changed
7613 	 * for non-failover addresses, autoconfigured
7614 	 * addresses, or IPv6 link local addresses.
7615 	 */
7616 	if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) ||
7617 	    (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) {
7618 		return (EINVAL);
7619 	}
7620 	/*
7621 	 * The new original index must be in use by some
7622 	 * physical interface.
7623 	 */
7624 	ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL,
7625 	    NULL, NULL);
7626 	if (ill == NULL)
7627 		return (ENXIO);
7628 	ill_refrele(ill);
7629 
7630 	ipif->ipif_orig_ifindex = lifr->lifr_index;
7631 	/*
7632 	 * When this ipif gets failed back, don't
7633 	 * preserve the original id, as it is no
7634 	 * longer applicable.
7635 	 */
7636 	ipif->ipif_orig_ipifid = 0;
7637 	/*
7638 	 * For IPv4, change the original index of any
7639 	 * multicast addresses associated with the
7640 	 * ipif to the new value.
7641 	 */
7642 	if (!isv6) {
7643 		ilm_t *ilm;
7644 
7645 		mutex_enter(&ipif->ipif_ill->ill_lock);
7646 		for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL;
7647 		    ilm = ilm->ilm_next) {
7648 			if (ilm->ilm_ipif == ipif) {
7649 				ilm->ilm_orig_ifindex = lifr->lifr_index;
7650 			}
7651 		}
7652 		mutex_exit(&ipif->ipif_ill->ill_lock);
7653 	}
7654 	return (0);
7655 }
7656 
7657 /* ARGSUSED */
7658 int
7659 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
7660     ip_ioctl_cmd_t *ipip, void *ifreq)
7661 {
7662 	struct lifreq *lifr = (struct lifreq *)ifreq;
7663 
7664 	/*
7665 	 * Get the original interface index i.e the one
7666 	 * before FAILOVER if it ever happened.
7667 	 */
7668 	lifr->lifr_index = ipif->ipif_orig_ifindex;
7669 	return (0);
7670 }
7671 
7672 /*
7673  * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls,
7674  * refhold and return the associated ipif
7675  */
7676 int
7677 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func)
7678 {
7679 	boolean_t exists;
7680 	struct iftun_req *ta;
7681 	ipif_t	*ipif;
7682 	ill_t	*ill;
7683 	boolean_t isv6;
7684 	mblk_t	*mp1;
7685 	int	error;
7686 	conn_t	*connp;
7687 
7688 	/* Existence verified in ip_wput_nondata */
7689 	mp1 = mp->b_cont->b_cont;
7690 	ta = (struct iftun_req *)mp1->b_rptr;
7691 	/*
7692 	 * Null terminate the string to protect against buffer
7693 	 * overrun. String was generated by user code and may not
7694 	 * be trusted.
7695 	 */
7696 	ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0';
7697 
7698 	connp = Q_TO_CONN(q);
7699 	isv6 = connp->conn_af_isv6;
7700 
7701 	/* Disallows implicit create */
7702 	ipif = ipif_lookup_on_name(ta->ifta_lifr_name,
7703 	    mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6,
7704 	    connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error);
7705 	if (ipif == NULL)
7706 		return (error);
7707 
7708 	if (ipif->ipif_id != 0) {
7709 		/*
7710 		 * We really don't want to set/get tunnel parameters
7711 		 * on virtual tunnel interfaces.  Only allow the
7712 		 * base tunnel to do these.
7713 		 */
7714 		ipif_refrele(ipif);
7715 		return (EINVAL);
7716 	}
7717 
7718 	/*
7719 	 * Send down to tunnel mod for ioctl processing.
7720 	 * Will finish ioctl in ip_rput_other().
7721 	 */
7722 	ill = ipif->ipif_ill;
7723 	if (ill->ill_net_type == IRE_LOOPBACK) {
7724 		ipif_refrele(ipif);
7725 		return (EOPNOTSUPP);
7726 	}
7727 
7728 	if (ill->ill_wq == NULL) {
7729 		ipif_refrele(ipif);
7730 		return (ENXIO);
7731 	}
7732 	/*
7733 	 * Mark the ioctl as coming from an IPv6 interface for
7734 	 * tun's convenience.
7735 	 */
7736 	if (ill->ill_isv6)
7737 		ta->ifta_flags |= 0x80000000;
7738 	*ipifp = ipif;
7739 	return (0);
7740 }
7741 
7742 /*
7743  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7744  * and return the associated ipif.
7745  * Return value:
7746  *	Non zero: An error has occurred. ci may not be filled out.
7747  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7748  *	a held ipif in ci.ci_ipif.
7749  */
7750 int
7751 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags,
7752     cmd_info_t *ci, ipsq_func_t func)
7753 {
7754 	sin_t		*sin;
7755 	sin6_t		*sin6;
7756 	char		*name;
7757 	struct ifreq    *ifr;
7758 	struct lifreq    *lifr;
7759 	ipif_t		*ipif = NULL;
7760 	ill_t		*ill;
7761 	conn_t		*connp;
7762 	boolean_t	isv6;
7763 	struct iocblk   *iocp = (struct iocblk *)mp->b_rptr;
7764 	boolean_t	exists;
7765 	int		err;
7766 	mblk_t		*mp1;
7767 	zoneid_t	zoneid;
7768 
7769 	if (q->q_next != NULL) {
7770 		ill = (ill_t *)q->q_ptr;
7771 		isv6 = ill->ill_isv6;
7772 		connp = NULL;
7773 		zoneid = ALL_ZONES;
7774 	} else {
7775 		ill = NULL;
7776 		connp = Q_TO_CONN(q);
7777 		isv6 = connp->conn_af_isv6;
7778 		zoneid = connp->conn_zoneid;
7779 		if (zoneid == GLOBAL_ZONEID) {
7780 			/* global zone can access ipifs in all zones */
7781 			zoneid = ALL_ZONES;
7782 		}
7783 	}
7784 
7785 	/* Has been checked in ip_wput_nondata */
7786 	mp1 = mp->b_cont->b_cont;
7787 
7788 
7789 	if (cmd_type == IF_CMD) {
7790 		/* This a old style SIOC[GS]IF* command */
7791 		ifr = (struct ifreq *)mp1->b_rptr;
7792 		/*
7793 		 * Null terminate the string to protect against buffer
7794 		 * overrun. String was generated by user code and may not
7795 		 * be trusted.
7796 		 */
7797 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7798 		sin = (sin_t *)&ifr->ifr_addr;
7799 		name = ifr->ifr_name;
7800 		ci->ci_sin = sin;
7801 		ci->ci_sin6 = NULL;
7802 		ci->ci_lifr = (struct lifreq *)ifr;
7803 	} else {
7804 		/* This a new style SIOC[GS]LIF* command */
7805 		ASSERT(cmd_type == LIF_CMD);
7806 		lifr = (struct lifreq *)mp1->b_rptr;
7807 		/*
7808 		 * Null terminate the string to protect against buffer
7809 		 * overrun. String was generated by user code and may not
7810 		 * be trusted.
7811 		 */
7812 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7813 		name = lifr->lifr_name;
7814 		sin = (sin_t *)&lifr->lifr_addr;
7815 		sin6 = (sin6_t *)&lifr->lifr_addr;
7816 		if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) {
7817 			(void) strncpy(ci->ci_groupname, lifr->lifr_groupname,
7818 			    LIFNAMSIZ);
7819 		}
7820 		ci->ci_sin = sin;
7821 		ci->ci_sin6 = sin6;
7822 		ci->ci_lifr = lifr;
7823 	}
7824 
7825 
7826 	if (iocp->ioc_cmd == SIOCSLIFNAME) {
7827 		/*
7828 		 * The ioctl will be failed if the ioctl comes down
7829 		 * an conn stream
7830 		 */
7831 		if (ill == NULL) {
7832 			/*
7833 			 * Not an ill queue, return EINVAL same as the
7834 			 * old error code.
7835 			 */
7836 			return (ENXIO);
7837 		}
7838 		ipif = ill->ill_ipif;
7839 		ipif_refhold(ipif);
7840 	} else {
7841 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7842 		    &exists, isv6, zoneid,
7843 		    (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err);
7844 		if (ipif == NULL) {
7845 			if (err == EINPROGRESS)
7846 				return (err);
7847 			if (iocp->ioc_cmd == SIOCLIFFAILOVER ||
7848 			    iocp->ioc_cmd == SIOCLIFFAILBACK) {
7849 				/*
7850 				 * Need to try both v4 and v6 since this
7851 				 * ioctl can come down either v4 or v6
7852 				 * socket. The lifreq.lifr_family passed
7853 				 * down by this ioctl is AF_UNSPEC.
7854 				 */
7855 				ipif = ipif_lookup_on_name(name,
7856 				    mi_strlen(name), B_FALSE, &exists, !isv6,
7857 				    zoneid, (connp == NULL) ? q :
7858 				    CONNP_TO_WQ(connp), mp, func, &err);
7859 				if (err == EINPROGRESS)
7860 					return (err);
7861 			}
7862 			err = 0;	/* Ensure we don't use it below */
7863 		}
7864 	}
7865 
7866 	/*
7867 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7868 	 */
7869 	if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) {
7870 		ipif_refrele(ipif);
7871 		return (ENXIO);
7872 	}
7873 
7874 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7875 	    name[0] == '\0') {
7876 		/*
7877 		 * Handle a or a SIOC?IF* with a null name
7878 		 * during plumb (on the ill queue before the I_PLINK).
7879 		 */
7880 		ipif = ill->ill_ipif;
7881 		ipif_refhold(ipif);
7882 	}
7883 
7884 	if (ipif == NULL)
7885 		return (ENXIO);
7886 
7887 	/*
7888 	 * Allow only GET operations if this ipif has been created
7889 	 * temporarily due to a MOVE operation.
7890 	 */
7891 	if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) {
7892 		ipif_refrele(ipif);
7893 		return (EINVAL);
7894 	}
7895 
7896 	ci->ci_ipif = ipif;
7897 	return (0);
7898 }
7899 
7900 /*
7901  * Return the total number of ipifs.
7902  */
7903 static uint_t
7904 ip_get_numifs(zoneid_t zoneid)
7905 {
7906 	uint_t numifs = 0;
7907 	ill_t	*ill;
7908 	ill_walk_context_t	ctx;
7909 	ipif_t	*ipif;
7910 
7911 	rw_enter(&ill_g_lock, RW_READER);
7912 	ill = ILL_START_WALK_V4(&ctx);
7913 
7914 	while (ill != NULL) {
7915 		for (ipif = ill->ill_ipif; ipif != NULL;
7916 		    ipif = ipif->ipif_next) {
7917 			if (ipif->ipif_zoneid == zoneid ||
7918 			    ipif->ipif_zoneid == ALL_ZONES)
7919 				numifs++;
7920 		}
7921 		ill = ill_next(&ctx, ill);
7922 	}
7923 	rw_exit(&ill_g_lock);
7924 	return (numifs);
7925 }
7926 
7927 /*
7928  * Return the total number of ipifs.
7929  */
7930 static uint_t
7931 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid)
7932 {
7933 	uint_t numifs = 0;
7934 	ill_t	*ill;
7935 	ipif_t	*ipif;
7936 	ill_walk_context_t	ctx;
7937 
7938 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7939 
7940 	rw_enter(&ill_g_lock, RW_READER);
7941 	if (family == AF_INET)
7942 		ill = ILL_START_WALK_V4(&ctx);
7943 	else if (family == AF_INET6)
7944 		ill = ILL_START_WALK_V6(&ctx);
7945 	else
7946 		ill = ILL_START_WALK_ALL(&ctx);
7947 
7948 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7949 		for (ipif = ill->ill_ipif; ipif != NULL;
7950 		    ipif = ipif->ipif_next) {
7951 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7952 			    !(lifn_flags & LIFC_NOXMIT))
7953 				continue;
7954 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7955 			    !(lifn_flags & LIFC_TEMPORARY))
7956 				continue;
7957 			if (((ipif->ipif_flags &
7958 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7959 			    IPIF_DEPRECATED)) ||
7960 			    (ill->ill_phyint->phyint_flags &
7961 			    PHYI_LOOPBACK) ||
7962 			    !(ipif->ipif_flags & IPIF_UP)) &&
7963 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7964 				continue;
7965 
7966 			if (zoneid != ipif->ipif_zoneid &&
7967 			    ipif->ipif_zoneid != ALL_ZONES &&
7968 			    (zoneid != GLOBAL_ZONEID ||
7969 			    !(lifn_flags & LIFC_ALLZONES)))
7970 				continue;
7971 
7972 			numifs++;
7973 		}
7974 	}
7975 	rw_exit(&ill_g_lock);
7976 	return (numifs);
7977 }
7978 
7979 uint_t
7980 ip_get_lifsrcofnum(ill_t *ill)
7981 {
7982 	uint_t numifs = 0;
7983 	ill_t	*ill_head = ill;
7984 
7985 	/*
7986 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7987 	 * other thread may be trying to relink the ILLs in this usesrc group
7988 	 * and adjusting the ill_usesrc_grp_next pointers
7989 	 */
7990 	rw_enter(&ill_g_usesrc_lock, RW_READER);
7991 	if ((ill->ill_usesrc_ifindex == 0) &&
7992 	    (ill->ill_usesrc_grp_next != NULL)) {
7993 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7994 		    ill = ill->ill_usesrc_grp_next)
7995 			numifs++;
7996 	}
7997 	rw_exit(&ill_g_usesrc_lock);
7998 
7999 	return (numifs);
8000 }
8001 
8002 /* Null values are passed in for ipif, sin, and ifreq */
8003 /* ARGSUSED */
8004 int
8005 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8006     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8007 {
8008 	int *nump;
8009 
8010 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8011 
8012 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8013 	nump = (int *)mp->b_cont->b_cont->b_rptr;
8014 
8015 	*nump = ip_get_numifs(Q_TO_CONN(q)->conn_zoneid);
8016 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
8017 	return (0);
8018 }
8019 
8020 /* Null values are passed in for ipif, sin, and ifreq */
8021 /* ARGSUSED */
8022 int
8023 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
8024     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8025 {
8026 	struct lifnum *lifn;
8027 	mblk_t	*mp1;
8028 
8029 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
8030 
8031 	/* Existence checked in ip_wput_nondata */
8032 	mp1 = mp->b_cont->b_cont;
8033 
8034 	lifn = (struct lifnum *)mp1->b_rptr;
8035 	switch (lifn->lifn_family) {
8036 	case AF_UNSPEC:
8037 	case AF_INET:
8038 	case AF_INET6:
8039 		break;
8040 	default:
8041 		return (EAFNOSUPPORT);
8042 	}
8043 
8044 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
8045 	    Q_TO_CONN(q)->conn_zoneid);
8046 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
8047 	return (0);
8048 }
8049 
8050 /* ARGSUSED */
8051 int
8052 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8053     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8054 {
8055 	STRUCT_HANDLE(ifconf, ifc);
8056 	mblk_t *mp1;
8057 	struct iocblk *iocp;
8058 	struct ifreq *ifr;
8059 	ill_walk_context_t	ctx;
8060 	ill_t	*ill;
8061 	ipif_t	*ipif;
8062 	struct sockaddr_in *sin;
8063 	int32_t	ifclen;
8064 	zoneid_t zoneid;
8065 
8066 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
8067 
8068 	ip1dbg(("ip_sioctl_get_ifconf"));
8069 	/* Existence verified in ip_wput_nondata */
8070 	mp1 = mp->b_cont->b_cont;
8071 	iocp = (struct iocblk *)mp->b_rptr;
8072 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8073 
8074 	/*
8075 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
8076 	 * the user buffer address and length into which the list of struct
8077 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
8078 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
8079 	 * the SIOCGIFCONF operation was redefined to simply provide
8080 	 * a large output buffer into which we are supposed to jam the ifreq
8081 	 * array.  The same ioctl command code was used, despite the fact that
8082 	 * both the applications and the kernel code had to change, thus making
8083 	 * it impossible to support both interfaces.
8084 	 *
8085 	 * For reasons not good enough to try to explain, the following
8086 	 * algorithm is used for deciding what to do with one of these:
8087 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
8088 	 * form with the output buffer coming down as the continuation message.
8089 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
8090 	 * and we have to copy in the ifconf structure to find out how big the
8091 	 * output buffer is and where to copy out to.  Sure no problem...
8092 	 *
8093 	 */
8094 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
8095 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
8096 		int numifs = 0;
8097 		size_t ifc_bufsize;
8098 
8099 		/*
8100 		 * Must be (better be!) continuation of a TRANSPARENT
8101 		 * IOCTL.  We just copied in the ifconf structure.
8102 		 */
8103 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
8104 		    (struct ifconf *)mp1->b_rptr);
8105 
8106 		/*
8107 		 * Allocate a buffer to hold requested information.
8108 		 *
8109 		 * If ifc_len is larger than what is needed, we only
8110 		 * allocate what we will use.
8111 		 *
8112 		 * If ifc_len is smaller than what is needed, return
8113 		 * EINVAL.
8114 		 *
8115 		 * XXX: the ill_t structure can hava 2 counters, for
8116 		 * v4 and v6 (not just ill_ipif_up_count) to store the
8117 		 * number of interfaces for a device, so we don't need
8118 		 * to count them here...
8119 		 */
8120 		numifs = ip_get_numifs(zoneid);
8121 
8122 		ifclen = STRUCT_FGET(ifc, ifc_len);
8123 		ifc_bufsize = numifs * sizeof (struct ifreq);
8124 		if (ifc_bufsize > ifclen) {
8125 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8126 				/* old behaviour */
8127 				return (EINVAL);
8128 			} else {
8129 				ifc_bufsize = ifclen;
8130 			}
8131 		}
8132 
8133 		mp1 = mi_copyout_alloc(q, mp,
8134 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
8135 		if (mp1 == NULL)
8136 			return (ENOMEM);
8137 
8138 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
8139 	}
8140 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8141 	/*
8142 	 * the SIOCGIFCONF ioctl only knows about
8143 	 * IPv4 addresses, so don't try to tell
8144 	 * it about interfaces with IPv6-only
8145 	 * addresses. (Last parm 'isv6' is B_FALSE)
8146 	 */
8147 
8148 	ifr = (struct ifreq *)mp1->b_rptr;
8149 
8150 	rw_enter(&ill_g_lock, RW_READER);
8151 	ill = ILL_START_WALK_V4(&ctx);
8152 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8153 		for (ipif = ill->ill_ipif; ipif;
8154 		    ipif = ipif->ipif_next) {
8155 			if (zoneid != ipif->ipif_zoneid &&
8156 			    ipif->ipif_zoneid != ALL_ZONES)
8157 				continue;
8158 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
8159 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
8160 					/* old behaviour */
8161 					rw_exit(&ill_g_lock);
8162 					return (EINVAL);
8163 				} else {
8164 					goto if_copydone;
8165 				}
8166 			}
8167 			(void) ipif_get_name(ipif,
8168 			    ifr->ifr_name,
8169 			    sizeof (ifr->ifr_name));
8170 			sin = (sin_t *)&ifr->ifr_addr;
8171 			*sin = sin_null;
8172 			sin->sin_family = AF_INET;
8173 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8174 			ifr++;
8175 		}
8176 	}
8177 if_copydone:
8178 	rw_exit(&ill_g_lock);
8179 	mp1->b_wptr = (uchar_t *)ifr;
8180 
8181 	if (STRUCT_BUF(ifc) != NULL) {
8182 		STRUCT_FSET(ifc, ifc_len,
8183 			(int)((uchar_t *)ifr - mp1->b_rptr));
8184 	}
8185 	return (0);
8186 }
8187 
8188 /*
8189  * Get the interfaces using the address hosted on the interface passed in,
8190  * as a source adddress
8191  */
8192 /* ARGSUSED */
8193 int
8194 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8195     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8196 {
8197 	mblk_t *mp1;
8198 	ill_t	*ill, *ill_head;
8199 	ipif_t	*ipif, *orig_ipif;
8200 	int	numlifs = 0;
8201 	size_t	lifs_bufsize, lifsmaxlen;
8202 	struct	lifreq *lifr;
8203 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8204 	uint_t	ifindex;
8205 	zoneid_t zoneid;
8206 	int err = 0;
8207 	boolean_t isv6 = B_FALSE;
8208 	struct	sockaddr_in	*sin;
8209 	struct	sockaddr_in6	*sin6;
8210 
8211 	STRUCT_HANDLE(lifsrcof, lifs);
8212 
8213 	ASSERT(q->q_next == NULL);
8214 
8215 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8216 
8217 	/* Existence verified in ip_wput_nondata */
8218 	mp1 = mp->b_cont->b_cont;
8219 
8220 	/*
8221 	 * Must be (better be!) continuation of a TRANSPARENT
8222 	 * IOCTL.  We just copied in the lifsrcof structure.
8223 	 */
8224 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
8225 	    (struct lifsrcof *)mp1->b_rptr);
8226 
8227 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
8228 		return (EINVAL);
8229 
8230 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
8231 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
8232 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp,
8233 	    ip_process_ioctl, &err);
8234 	if (ipif == NULL) {
8235 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
8236 		    ifindex));
8237 		return (err);
8238 	}
8239 
8240 
8241 	/* Allocate a buffer to hold requested information */
8242 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
8243 	lifs_bufsize = numlifs * sizeof (struct lifreq);
8244 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
8245 	/* The actual size needed is always returned in lifs_len */
8246 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
8247 
8248 	/* If the amount we need is more than what is passed in, abort */
8249 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
8250 		ipif_refrele(ipif);
8251 		return (0);
8252 	}
8253 
8254 	mp1 = mi_copyout_alloc(q, mp,
8255 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
8256 	if (mp1 == NULL) {
8257 		ipif_refrele(ipif);
8258 		return (ENOMEM);
8259 	}
8260 
8261 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
8262 	bzero(mp1->b_rptr, lifs_bufsize);
8263 
8264 	lifr = (struct lifreq *)mp1->b_rptr;
8265 
8266 	ill = ill_head = ipif->ipif_ill;
8267 	orig_ipif = ipif;
8268 
8269 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
8270 	rw_enter(&ill_g_usesrc_lock, RW_READER);
8271 	rw_enter(&ill_g_lock, RW_READER);
8272 
8273 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
8274 	for (; (ill != NULL) && (ill != ill_head);
8275 	    ill = ill->ill_usesrc_grp_next) {
8276 
8277 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
8278 			break;
8279 
8280 		ipif = ill->ill_ipif;
8281 		(void) ipif_get_name(ipif,
8282 		    lifr->lifr_name, sizeof (lifr->lifr_name));
8283 		if (ipif->ipif_isv6) {
8284 			sin6 = (sin6_t *)&lifr->lifr_addr;
8285 			*sin6 = sin6_null;
8286 			sin6->sin6_family = AF_INET6;
8287 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
8288 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
8289 			    &ipif->ipif_v6net_mask);
8290 		} else {
8291 			sin = (sin_t *)&lifr->lifr_addr;
8292 			*sin = sin_null;
8293 			sin->sin_family = AF_INET;
8294 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
8295 			lifr->lifr_addrlen = ip_mask_to_plen(
8296 			    ipif->ipif_net_mask);
8297 		}
8298 		lifr++;
8299 	}
8300 	rw_exit(&ill_g_usesrc_lock);
8301 	rw_exit(&ill_g_lock);
8302 	ipif_refrele(orig_ipif);
8303 	mp1->b_wptr = (uchar_t *)lifr;
8304 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
8305 
8306 	return (0);
8307 }
8308 
8309 /* ARGSUSED */
8310 int
8311 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
8312     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8313 {
8314 	mblk_t *mp1;
8315 	int	list;
8316 	ill_t	*ill;
8317 	ipif_t	*ipif;
8318 	int	flags;
8319 	int	numlifs = 0;
8320 	size_t	lifc_bufsize;
8321 	struct	lifreq *lifr;
8322 	sa_family_t	family;
8323 	struct	sockaddr_in	*sin;
8324 	struct	sockaddr_in6	*sin6;
8325 	ill_walk_context_t	ctx;
8326 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8327 	int32_t	lifclen;
8328 	zoneid_t zoneid;
8329 	STRUCT_HANDLE(lifconf, lifc);
8330 
8331 	ip1dbg(("ip_sioctl_get_lifconf"));
8332 
8333 	ASSERT(q->q_next == NULL);
8334 
8335 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8336 
8337 	/* Existence verified in ip_wput_nondata */
8338 	mp1 = mp->b_cont->b_cont;
8339 
8340 	/*
8341 	 * An extended version of SIOCGIFCONF that takes an
8342 	 * additional address family and flags field.
8343 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
8344 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
8345 	 * interfaces are omitted.
8346 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
8347 	 * unless LIFC_TEMPORARY is specified.
8348 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
8349 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
8350 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
8351 	 * has priority over LIFC_NOXMIT.
8352 	 */
8353 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
8354 
8355 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
8356 		return (EINVAL);
8357 
8358 	/*
8359 	 * Must be (better be!) continuation of a TRANSPARENT
8360 	 * IOCTL.  We just copied in the lifconf structure.
8361 	 */
8362 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
8363 
8364 	family = STRUCT_FGET(lifc, lifc_family);
8365 	flags = STRUCT_FGET(lifc, lifc_flags);
8366 
8367 	switch (family) {
8368 	case AF_UNSPEC:
8369 		/*
8370 		 * walk all ILL's.
8371 		 */
8372 		list = MAX_G_HEADS;
8373 		break;
8374 	case AF_INET:
8375 		/*
8376 		 * walk only IPV4 ILL's.
8377 		 */
8378 		list = IP_V4_G_HEAD;
8379 		break;
8380 	case AF_INET6:
8381 		/*
8382 		 * walk only IPV6 ILL's.
8383 		 */
8384 		list = IP_V6_G_HEAD;
8385 		break;
8386 	default:
8387 		return (EAFNOSUPPORT);
8388 	}
8389 
8390 	/*
8391 	 * Allocate a buffer to hold requested information.
8392 	 *
8393 	 * If lifc_len is larger than what is needed, we only
8394 	 * allocate what we will use.
8395 	 *
8396 	 * If lifc_len is smaller than what is needed, return
8397 	 * EINVAL.
8398 	 */
8399 	numlifs = ip_get_numlifs(family, flags, zoneid);
8400 	lifc_bufsize = numlifs * sizeof (struct lifreq);
8401 	lifclen = STRUCT_FGET(lifc, lifc_len);
8402 	if (lifc_bufsize > lifclen) {
8403 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
8404 			return (EINVAL);
8405 		else
8406 			lifc_bufsize = lifclen;
8407 	}
8408 
8409 	mp1 = mi_copyout_alloc(q, mp,
8410 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
8411 	if (mp1 == NULL)
8412 		return (ENOMEM);
8413 
8414 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
8415 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
8416 
8417 	lifr = (struct lifreq *)mp1->b_rptr;
8418 
8419 	rw_enter(&ill_g_lock, RW_READER);
8420 	ill = ill_first(list, list, &ctx);
8421 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
8422 		for (ipif = ill->ill_ipif; ipif != NULL;
8423 		    ipif = ipif->ipif_next) {
8424 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
8425 			    !(flags & LIFC_NOXMIT))
8426 				continue;
8427 
8428 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
8429 			    !(flags & LIFC_TEMPORARY))
8430 				continue;
8431 
8432 			if (((ipif->ipif_flags &
8433 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
8434 			    IPIF_DEPRECATED)) ||
8435 			    (ill->ill_phyint->phyint_flags &
8436 			    PHYI_LOOPBACK) ||
8437 			    !(ipif->ipif_flags & IPIF_UP)) &&
8438 			    (flags & LIFC_EXTERNAL_SOURCE))
8439 				continue;
8440 
8441 			if (zoneid != ipif->ipif_zoneid &&
8442 			    ipif->ipif_zoneid != ALL_ZONES &&
8443 			    (zoneid != GLOBAL_ZONEID ||
8444 			    !(flags & LIFC_ALLZONES)))
8445 				continue;
8446 
8447 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
8448 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
8449 					rw_exit(&ill_g_lock);
8450 					return (EINVAL);
8451 				} else {
8452 					goto lif_copydone;
8453 				}
8454 			}
8455 
8456 			(void) ipif_get_name(ipif,
8457 				lifr->lifr_name,
8458 				sizeof (lifr->lifr_name));
8459 			if (ipif->ipif_isv6) {
8460 				sin6 = (sin6_t *)&lifr->lifr_addr;
8461 				*sin6 = sin6_null;
8462 				sin6->sin6_family = AF_INET6;
8463 				sin6->sin6_addr =
8464 				ipif->ipif_v6lcl_addr;
8465 				lifr->lifr_addrlen =
8466 				ip_mask_to_plen_v6(
8467 				    &ipif->ipif_v6net_mask);
8468 			} else {
8469 				sin = (sin_t *)&lifr->lifr_addr;
8470 				*sin = sin_null;
8471 				sin->sin_family = AF_INET;
8472 				sin->sin_addr.s_addr =
8473 				    ipif->ipif_lcl_addr;
8474 				lifr->lifr_addrlen =
8475 				    ip_mask_to_plen(
8476 				    ipif->ipif_net_mask);
8477 			}
8478 			lifr++;
8479 		}
8480 	}
8481 lif_copydone:
8482 	rw_exit(&ill_g_lock);
8483 
8484 	mp1->b_wptr = (uchar_t *)lifr;
8485 	if (STRUCT_BUF(lifc) != NULL) {
8486 		STRUCT_FSET(lifc, lifc_len,
8487 			(int)((uchar_t *)lifr - mp1->b_rptr));
8488 	}
8489 	return (0);
8490 }
8491 
8492 /* ARGSUSED */
8493 int
8494 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin,
8495     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
8496 {
8497 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
8498 	ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr;
8499 	return (0);
8500 }
8501 
8502 static void
8503 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
8504 {
8505 	ip6_asp_t *table;
8506 	size_t table_size;
8507 	mblk_t *data_mp;
8508 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8509 
8510 	/* These two ioctls are I_STR only */
8511 	if (iocp->ioc_count == TRANSPARENT) {
8512 		miocnak(q, mp, 0, EINVAL);
8513 		return;
8514 	}
8515 
8516 	data_mp = mp->b_cont;
8517 	if (data_mp == NULL) {
8518 		/* The user passed us a NULL argument */
8519 		table = NULL;
8520 		table_size = iocp->ioc_count;
8521 	} else {
8522 		/*
8523 		 * The user provided a table.  The stream head
8524 		 * may have copied in the user data in chunks,
8525 		 * so make sure everything is pulled up
8526 		 * properly.
8527 		 */
8528 		if (MBLKL(data_mp) < iocp->ioc_count) {
8529 			mblk_t *new_data_mp;
8530 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
8531 			    NULL) {
8532 				miocnak(q, mp, 0, ENOMEM);
8533 				return;
8534 			}
8535 			freemsg(data_mp);
8536 			data_mp = new_data_mp;
8537 			mp->b_cont = data_mp;
8538 		}
8539 		table = (ip6_asp_t *)data_mp->b_rptr;
8540 		table_size = iocp->ioc_count;
8541 	}
8542 
8543 	switch (iocp->ioc_cmd) {
8544 	case SIOCGIP6ADDRPOLICY:
8545 		iocp->ioc_rval = ip6_asp_get(table, table_size);
8546 		if (iocp->ioc_rval == -1)
8547 			iocp->ioc_error = EINVAL;
8548 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8549 		else if (table != NULL &&
8550 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
8551 			ip6_asp_t *src = table;
8552 			ip6_asp32_t *dst = (void *)table;
8553 			int count = table_size / sizeof (ip6_asp_t);
8554 			int i;
8555 
8556 			/*
8557 			 * We need to do an in-place shrink of the array
8558 			 * to match the alignment attributes of the
8559 			 * 32-bit ABI looking at it.
8560 			 */
8561 			/* LINTED: logical expression always true: op "||" */
8562 			ASSERT(sizeof (*src) > sizeof (*dst));
8563 			for (i = 1; i < count; i++)
8564 				bcopy(src + i, dst + i, sizeof (*dst));
8565 		}
8566 #endif
8567 		break;
8568 
8569 	case SIOCSIP6ADDRPOLICY:
8570 		ASSERT(mp->b_prev == NULL);
8571 		mp->b_prev = (void *)q;
8572 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
8573 		/*
8574 		 * We pass in the datamodel here so that the ip6_asp_replace()
8575 		 * routine can handle converting from 32-bit to native formats
8576 		 * where necessary.
8577 		 *
8578 		 * A better way to handle this might be to convert the inbound
8579 		 * data structure here, and hang it off a new 'mp'; thus the
8580 		 * ip6_asp_replace() logic would always be dealing with native
8581 		 * format data structures..
8582 		 *
8583 		 * (An even simpler way to handle these ioctls is to just
8584 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
8585 		 * and just recompile everything that depends on it.)
8586 		 */
8587 #endif
8588 		ip6_asp_replace(mp, table, table_size, B_FALSE,
8589 		    iocp->ioc_flag & IOC_MODELS);
8590 		return;
8591 	}
8592 
8593 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
8594 	qreply(q, mp);
8595 }
8596 
8597 static void
8598 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
8599 {
8600 	mblk_t 		*data_mp;
8601 	struct dstinforeq	*dir;
8602 	uint8_t		*end, *cur;
8603 	in6_addr_t	*daddr, *saddr;
8604 	ipaddr_t	v4daddr;
8605 	ire_t		*ire;
8606 	char		*slabel, *dlabel;
8607 	boolean_t	isipv4;
8608 	int		match_ire;
8609 	ill_t		*dst_ill;
8610 	ipif_t		*src_ipif, *ire_ipif;
8611 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8612 	zoneid_t	zoneid;
8613 
8614 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8615 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8616 
8617 	/*
8618 	 * This ioctl is I_STR only, and must have a
8619 	 * data mblk following the M_IOCTL mblk.
8620 	 */
8621 	data_mp = mp->b_cont;
8622 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
8623 		miocnak(q, mp, 0, EINVAL);
8624 		return;
8625 	}
8626 
8627 	if (MBLKL(data_mp) < iocp->ioc_count) {
8628 		mblk_t *new_data_mp;
8629 
8630 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
8631 			miocnak(q, mp, 0, ENOMEM);
8632 			return;
8633 		}
8634 		freemsg(data_mp);
8635 		data_mp = new_data_mp;
8636 		mp->b_cont = data_mp;
8637 	}
8638 	match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT;
8639 
8640 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
8641 	    end - cur >= sizeof (struct dstinforeq);
8642 	    cur += sizeof (struct dstinforeq)) {
8643 		dir = (struct dstinforeq *)cur;
8644 		daddr = &dir->dir_daddr;
8645 		saddr = &dir->dir_saddr;
8646 
8647 		/*
8648 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
8649 		 * v4 mapped addresses; ire_ftable_lookup[_v6]()
8650 		 * and ipif_select_source[_v6]() do not.
8651 		 */
8652 		dir->dir_dscope = ip_addr_scope_v6(daddr);
8653 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence);
8654 
8655 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
8656 		if (isipv4) {
8657 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
8658 			ire = ire_ftable_lookup(v4daddr, NULL, NULL,
8659 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8660 		} else {
8661 			ire = ire_ftable_lookup_v6(daddr, NULL, NULL,
8662 			    0, NULL, NULL, zoneid, 0, NULL, match_ire);
8663 		}
8664 		if (ire == NULL) {
8665 			dir->dir_dreachable = 0;
8666 
8667 			/* move on to next dst addr */
8668 			continue;
8669 		}
8670 		dir->dir_dreachable = 1;
8671 
8672 		ire_ipif = ire->ire_ipif;
8673 		if (ire_ipif == NULL)
8674 			goto next_dst;
8675 
8676 		/*
8677 		 * We expect to get back an interface ire or a
8678 		 * gateway ire cache entry.  For both types, the
8679 		 * output interface is ire_ipif->ipif_ill.
8680 		 */
8681 		dst_ill = ire_ipif->ipif_ill;
8682 		dir->dir_dmactype = dst_ill->ill_mactype;
8683 
8684 		if (isipv4) {
8685 			src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid);
8686 		} else {
8687 			src_ipif = ipif_select_source_v6(dst_ill,
8688 			    daddr, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
8689 			    zoneid);
8690 		}
8691 		if (src_ipif == NULL)
8692 			goto next_dst;
8693 
8694 		*saddr = src_ipif->ipif_v6lcl_addr;
8695 		dir->dir_sscope = ip_addr_scope_v6(saddr);
8696 		slabel = ip6_asp_lookup(saddr, NULL);
8697 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
8698 		dir->dir_sdeprecated =
8699 		    (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
8700 		ipif_refrele(src_ipif);
8701 next_dst:
8702 		ire_refrele(ire);
8703 	}
8704 	miocack(q, mp, iocp->ioc_count, 0);
8705 }
8706 
8707 
8708 /*
8709  * Check if this is an address assigned to this machine.
8710  * Skips interfaces that are down by using ire checks.
8711  * Translates mapped addresses to v4 addresses and then
8712  * treats them as such, returning true if the v4 address
8713  * associated with this mapped address is configured.
8714  * Note: Applications will have to be careful what they do
8715  * with the response; use of mapped addresses limits
8716  * what can be done with the socket, especially with
8717  * respect to socket options and ioctls - neither IPv4
8718  * options nor IPv6 sticky options/ancillary data options
8719  * may be used.
8720  */
8721 /* ARGSUSED */
8722 int
8723 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8724     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8725 {
8726 	struct sioc_addrreq *sia;
8727 	sin_t *sin;
8728 	ire_t *ire;
8729 	mblk_t *mp1;
8730 	zoneid_t zoneid;
8731 
8732 	ip1dbg(("ip_sioctl_tmyaddr"));
8733 
8734 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8735 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8736 
8737 	/* Existence verified in ip_wput_nondata */
8738 	mp1 = mp->b_cont->b_cont;
8739 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8740 	sin = (sin_t *)&sia->sa_addr;
8741 	switch (sin->sin_family) {
8742 	case AF_INET6: {
8743 		sin6_t *sin6 = (sin6_t *)sin;
8744 
8745 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8746 			ipaddr_t v4_addr;
8747 
8748 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8749 			    v4_addr);
8750 			ire = ire_ctable_lookup(v4_addr, 0,
8751 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8752 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8753 		} else {
8754 			in6_addr_t v6addr;
8755 
8756 			v6addr = sin6->sin6_addr;
8757 			ire = ire_ctable_lookup_v6(&v6addr, 0,
8758 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8759 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8760 		}
8761 		break;
8762 	}
8763 	case AF_INET: {
8764 		ipaddr_t v4addr;
8765 
8766 		v4addr = sin->sin_addr.s_addr;
8767 		ire = ire_ctable_lookup(v4addr, 0,
8768 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8769 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY);
8770 		break;
8771 	}
8772 	default:
8773 		return (EAFNOSUPPORT);
8774 	}
8775 	if (ire != NULL) {
8776 		sia->sa_res = 1;
8777 		ire_refrele(ire);
8778 	} else {
8779 		sia->sa_res = 0;
8780 	}
8781 	return (0);
8782 }
8783 
8784 /*
8785  * Check if this is an address assigned on-link i.e. neighbor,
8786  * and makes sure it's reachable from the current zone.
8787  * Returns true for my addresses as well.
8788  * Translates mapped addresses to v4 addresses and then
8789  * treats them as such, returning true if the v4 address
8790  * associated with this mapped address is configured.
8791  * Note: Applications will have to be careful what they do
8792  * with the response; use of mapped addresses limits
8793  * what can be done with the socket, especially with
8794  * respect to socket options and ioctls - neither IPv4
8795  * options nor IPv6 sticky options/ancillary data options
8796  * may be used.
8797  */
8798 /* ARGSUSED */
8799 int
8800 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8801     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8802 {
8803 	struct sioc_addrreq *sia;
8804 	sin_t *sin;
8805 	mblk_t	*mp1;
8806 	ire_t *ire = NULL;
8807 	zoneid_t zoneid;
8808 
8809 	ip1dbg(("ip_sioctl_tonlink"));
8810 
8811 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8812 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8813 
8814 	/* Existence verified in ip_wput_nondata */
8815 	mp1 = mp->b_cont->b_cont;
8816 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8817 	sin = (sin_t *)&sia->sa_addr;
8818 
8819 	/*
8820 	 * Match addresses with a zero gateway field to avoid
8821 	 * routes going through a router.
8822 	 * Exclude broadcast and multicast addresses.
8823 	 */
8824 	switch (sin->sin_family) {
8825 	case AF_INET6: {
8826 		sin6_t *sin6 = (sin6_t *)sin;
8827 
8828 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8829 			ipaddr_t v4_addr;
8830 
8831 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8832 			    v4_addr);
8833 			if (!CLASSD(v4_addr)) {
8834 				ire = ire_route_lookup(v4_addr, 0, 0, 0,
8835 				    NULL, NULL, zoneid, NULL,
8836 				    MATCH_IRE_GW);
8837 			}
8838 		} else {
8839 			in6_addr_t v6addr;
8840 			in6_addr_t v6gw;
8841 
8842 			v6addr = sin6->sin6_addr;
8843 			v6gw = ipv6_all_zeros;
8844 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8845 				ire = ire_route_lookup_v6(&v6addr, 0,
8846 				    &v6gw, 0, NULL, NULL, zoneid,
8847 				    NULL, MATCH_IRE_GW);
8848 			}
8849 		}
8850 		break;
8851 	}
8852 	case AF_INET: {
8853 		ipaddr_t v4addr;
8854 
8855 		v4addr = sin->sin_addr.s_addr;
8856 		if (!CLASSD(v4addr)) {
8857 			ire = ire_route_lookup(v4addr, 0, 0, 0,
8858 			    NULL, NULL, zoneid, NULL,
8859 			    MATCH_IRE_GW);
8860 		}
8861 		break;
8862 	}
8863 	default:
8864 		return (EAFNOSUPPORT);
8865 	}
8866 	sia->sa_res = 0;
8867 	if (ire != NULL) {
8868 		if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE|
8869 		    IRE_LOCAL|IRE_LOOPBACK)) {
8870 			sia->sa_res = 1;
8871 		}
8872 		ire_refrele(ire);
8873 	}
8874 	return (0);
8875 }
8876 
8877 /*
8878  * TBD: implement when kernel maintaines a list of site prefixes.
8879  */
8880 /* ARGSUSED */
8881 int
8882 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8883     ip_ioctl_cmd_t *ipip, void *ifreq)
8884 {
8885 	return (ENXIO);
8886 }
8887 
8888 /* ARGSUSED */
8889 int
8890 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8891     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8892 {
8893 	ill_t  		*ill;
8894 	mblk_t		*mp1;
8895 	conn_t		*connp;
8896 	boolean_t	success;
8897 
8898 	ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n",
8899 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
8900 	/* ioctl comes down on an conn */
8901 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8902 	connp = Q_TO_CONN(q);
8903 
8904 	mp->b_datap->db_type = M_IOCTL;
8905 
8906 	/*
8907 	 * Send down a copy. (copymsg does not copy b_next/b_prev).
8908 	 * The original mp contains contaminated b_next values due to 'mi',
8909 	 * which is needed to do the mi_copy_done. Unfortunately if we
8910 	 * send down the original mblk itself and if we are popped due to an
8911 	 * an unplumb before the response comes back from tunnel,
8912 	 * the streamhead (which does a freemsg) will see this contaminated
8913 	 * message and the assertion in freemsg about non-null b_next/b_prev
8914 	 * will panic a DEBUG kernel.
8915 	 */
8916 	mp1 = copymsg(mp);
8917 	if (mp1 == NULL)
8918 		return (ENOMEM);
8919 
8920 	ill = ipif->ipif_ill;
8921 	mutex_enter(&connp->conn_lock);
8922 	mutex_enter(&ill->ill_lock);
8923 	if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) {
8924 		success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp),
8925 		    mp, 0);
8926 	} else {
8927 		success = ill_pending_mp_add(ill, connp, mp);
8928 	}
8929 	mutex_exit(&ill->ill_lock);
8930 	mutex_exit(&connp->conn_lock);
8931 
8932 	if (success) {
8933 		ip1dbg(("sending down tunparam request "));
8934 		putnext(ill->ill_wq, mp1);
8935 		return (EINPROGRESS);
8936 	} else {
8937 		/* The conn has started closing */
8938 		freemsg(mp1);
8939 		return (EINTR);
8940 	}
8941 }
8942 
8943 static int
8944 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin,
8945     boolean_t x_arp_ioctl, boolean_t if_arp_ioctl)
8946 {
8947 	mblk_t *mp1;
8948 	mblk_t *mp2;
8949 	mblk_t *pending_mp;
8950 	ipaddr_t ipaddr;
8951 	area_t *area;
8952 	struct iocblk *iocp;
8953 	conn_t *connp;
8954 	struct arpreq *ar;
8955 	struct xarpreq *xar;
8956 	boolean_t success;
8957 	int flags, alength;
8958 	char *lladdr;
8959 
8960 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8961 	connp = Q_TO_CONN(q);
8962 
8963 	iocp = (struct iocblk *)mp->b_rptr;
8964 	/*
8965 	 * ill has already been set depending on whether
8966 	 * bsd style or interface style ioctl.
8967 	 */
8968 	ASSERT(ill != NULL);
8969 
8970 	/*
8971 	 * Is this one of the new SIOC*XARP ioctls?
8972 	 */
8973 	if (x_arp_ioctl) {
8974 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8975 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8976 		ar = NULL;
8977 
8978 		flags = xar->xarp_flags;
8979 		lladdr = LLADDR(&xar->xarp_ha);
8980 		/*
8981 		 * Validate against user's link layer address length
8982 		 * input and name and addr length limits.
8983 		 */
8984 		alength = ill->ill_phys_addr_length;
8985 		if (iocp->ioc_cmd == SIOCSXARP) {
8986 			if (alength != xar->xarp_ha.sdl_alen ||
8987 			    (alength + xar->xarp_ha.sdl_nlen >
8988 			    sizeof (xar->xarp_ha.sdl_data)))
8989 				return (EINVAL);
8990 		}
8991 	} else {
8992 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8993 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8994 		xar = NULL;
8995 
8996 		flags = ar->arp_flags;
8997 		lladdr = ar->arp_ha.sa_data;
8998 		/*
8999 		 * Theoretically, the sa_family could tell us what link
9000 		 * layer type this operation is trying to deal with. By
9001 		 * common usage AF_UNSPEC means ethernet. We'll assume
9002 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
9003 		 * for now. Our new SIOC*XARP ioctls can be used more
9004 		 * generally.
9005 		 *
9006 		 * If the underlying media happens to have a non 6 byte
9007 		 * address, arp module will fail set/get, but the del
9008 		 * operation will succeed.
9009 		 */
9010 		alength = 6;
9011 		if ((iocp->ioc_cmd != SIOCDARP) &&
9012 		    (alength != ill->ill_phys_addr_length)) {
9013 			return (EINVAL);
9014 		}
9015 	}
9016 
9017 	/*
9018 	 * We are going to pass up to ARP a packet chain that looks
9019 	 * like:
9020 	 *
9021 	 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9022 	 *
9023 	 * Get a copy of the original IOCTL mblk to head the chain,
9024 	 * to be sent up (in mp1). Also get another copy to store
9025 	 * in the ill_pending_mp list, for matching the response
9026 	 * when it comes back from ARP.
9027 	 */
9028 	mp1 = copyb(mp);
9029 	pending_mp = copymsg(mp);
9030 	if (mp1 == NULL || pending_mp == NULL) {
9031 		if (mp1 != NULL)
9032 			freeb(mp1);
9033 		if (pending_mp != NULL)
9034 			inet_freemsg(pending_mp);
9035 		return (ENOMEM);
9036 	}
9037 
9038 	ipaddr = sin->sin_addr.s_addr;
9039 
9040 	mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template,
9041 	    (caddr_t)&ipaddr);
9042 	if (mp2 == NULL) {
9043 		freeb(mp1);
9044 		inet_freemsg(pending_mp);
9045 		return (ENOMEM);
9046 	}
9047 	/* Put together the chain. */
9048 	mp1->b_cont = mp2;
9049 	mp1->b_datap->db_type = M_IOCTL;
9050 	mp2->b_cont = mp;
9051 	mp2->b_datap->db_type = M_DATA;
9052 
9053 	iocp = (struct iocblk *)mp1->b_rptr;
9054 
9055 	/*
9056 	 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an
9057 	 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a
9058 	 * cp_private field (or cp_rval on 32-bit systems) in place of the
9059 	 * ioc_count field; set ioc_count to be correct.
9060 	 */
9061 	iocp->ioc_count = MBLKL(mp1->b_cont);
9062 
9063 	/*
9064 	 * Set the proper command in the ARP message.
9065 	 * Convert the SIOC{G|S|D}ARP calls into our
9066 	 * AR_ENTRY_xxx calls.
9067 	 */
9068 	area = (area_t *)mp2->b_rptr;
9069 	switch (iocp->ioc_cmd) {
9070 	case SIOCDARP:
9071 	case SIOCDXARP:
9072 		/*
9073 		 * We defer deleting the corresponding IRE until
9074 		 * we return from arp.
9075 		 */
9076 		area->area_cmd = AR_ENTRY_DELETE;
9077 		area->area_proto_mask_offset = 0;
9078 		break;
9079 	case SIOCGARP:
9080 	case SIOCGXARP:
9081 		area->area_cmd = AR_ENTRY_SQUERY;
9082 		area->area_proto_mask_offset = 0;
9083 		break;
9084 	case SIOCSARP:
9085 	case SIOCSXARP: {
9086 		/*
9087 		 * Delete the corresponding ire to make sure IP will
9088 		 * pick up any change from arp.
9089 		 */
9090 		if (!if_arp_ioctl) {
9091 			(void) ip_ire_clookup_and_delete(ipaddr, NULL);
9092 			break;
9093 		} else {
9094 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
9095 			if (ipif != NULL) {
9096 				(void) ip_ire_clookup_and_delete(ipaddr, ipif);
9097 				ipif_refrele(ipif);
9098 			}
9099 			break;
9100 		}
9101 	}
9102 	}
9103 	iocp->ioc_cmd = area->area_cmd;
9104 
9105 	/*
9106 	 * Before sending 'mp' to ARP, we have to clear the b_next
9107 	 * and b_prev. Otherwise if STREAMS encounters such a message
9108 	 * in freemsg(), (because ARP can close any time) it can cause
9109 	 * a panic. But mi code needs the b_next and b_prev values of
9110 	 * mp->b_cont, to complete the ioctl. So we store it here
9111 	 * in pending_mp->bcont, and restore it in ip_sioctl_iocack()
9112 	 * when the response comes down from ARP.
9113 	 */
9114 	pending_mp->b_cont->b_next = mp->b_cont->b_next;
9115 	pending_mp->b_cont->b_prev = mp->b_cont->b_prev;
9116 	mp->b_cont->b_next = NULL;
9117 	mp->b_cont->b_prev = NULL;
9118 
9119 	mutex_enter(&connp->conn_lock);
9120 	mutex_enter(&ill->ill_lock);
9121 	/* conn has not yet started closing, hence this can't fail */
9122 	success = ill_pending_mp_add(ill, connp, pending_mp);
9123 	ASSERT(success);
9124 	mutex_exit(&ill->ill_lock);
9125 	mutex_exit(&connp->conn_lock);
9126 
9127 	/*
9128 	 * Fill in the rest of the ARP operation fields.
9129 	 */
9130 	area->area_hw_addr_length = alength;
9131 	bcopy(lladdr,
9132 	    (char *)area + area->area_hw_addr_offset,
9133 	    area->area_hw_addr_length);
9134 	/* Translate the flags. */
9135 	if (flags & ATF_PERM)
9136 		area->area_flags |= ACE_F_PERMANENT;
9137 	if (flags & ATF_PUBL)
9138 		area->area_flags |= ACE_F_PUBLISH;
9139 
9140 	/*
9141 	 * Up to ARP it goes.  The response will come
9142 	 * back in ip_wput as an M_IOCACK message, and
9143 	 * will be handed to ip_sioctl_iocack for
9144 	 * completion.
9145 	 */
9146 	putnext(ill->ill_rq, mp1);
9147 	return (EINPROGRESS);
9148 }
9149 
9150 /* ARGSUSED */
9151 int
9152 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9153     ip_ioctl_cmd_t *ipip, void *ifreq)
9154 {
9155 	struct xarpreq *xar;
9156 	boolean_t isv6;
9157 	mblk_t	*mp1;
9158 	int	err;
9159 	conn_t	*connp;
9160 	int ifnamelen;
9161 	ire_t	*ire = NULL;
9162 	ill_t	*ill = NULL;
9163 	struct sockaddr_in *sin;
9164 	boolean_t if_arp_ioctl = B_FALSE;
9165 
9166 	/* ioctl comes down on an conn */
9167 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9168 	connp = Q_TO_CONN(q);
9169 	isv6 = connp->conn_af_isv6;
9170 
9171 	/* Existance verified in ip_wput_nondata */
9172 	mp1 = mp->b_cont->b_cont;
9173 
9174 	ASSERT(MBLKL(mp1) >= sizeof (*xar));
9175 	xar = (struct xarpreq *)mp1->b_rptr;
9176 	sin = (sin_t *)&xar->xarp_pa;
9177 
9178 	if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) ||
9179 	    (xar->xarp_pa.ss_family != AF_INET))
9180 		return (ENXIO);
9181 
9182 	ifnamelen = xar->xarp_ha.sdl_nlen;
9183 	if (ifnamelen != 0) {
9184 		char	*cptr, cval;
9185 
9186 		if (ifnamelen >= LIFNAMSIZ)
9187 			return (EINVAL);
9188 
9189 		/*
9190 		 * Instead of bcopying a bunch of bytes,
9191 		 * null-terminate the string in-situ.
9192 		 */
9193 		cptr = xar->xarp_ha.sdl_data + ifnamelen;
9194 		cval = *cptr;
9195 		*cptr = '\0';
9196 		ill = ill_lookup_on_name(xar->xarp_ha.sdl_data,
9197 		    B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl,
9198 		    &err, NULL);
9199 		*cptr = cval;
9200 		if (ill == NULL)
9201 			return (err);
9202 		if (ill->ill_net_type != IRE_IF_RESOLVER) {
9203 			ill_refrele(ill);
9204 			return (ENXIO);
9205 		}
9206 
9207 		if_arp_ioctl = B_TRUE;
9208 	} else {
9209 		/*
9210 		 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves
9211 		 * as an extended BSD ioctl. The kernel uses the IP address
9212 		 * to figure out the network interface.
9213 		 */
9214 		ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9215 		if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9216 		    ((ill = ire_to_ill(ire)) == NULL) ||
9217 		    (ill->ill_net_type != IRE_IF_RESOLVER)) {
9218 			if (ire != NULL)
9219 				ire_refrele(ire);
9220 			ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9221 			    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9222 			    NULL, MATCH_IRE_TYPE);
9223 			if ((ire == NULL) ||
9224 			    ((ill = ire_to_ill(ire)) == NULL)) {
9225 				if (ire != NULL)
9226 					ire_refrele(ire);
9227 				return (ENXIO);
9228 			}
9229 		}
9230 		ASSERT(ire != NULL && ill != NULL);
9231 	}
9232 
9233 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl);
9234 	if (if_arp_ioctl)
9235 		ill_refrele(ill);
9236 	if (ire != NULL)
9237 		ire_refrele(ire);
9238 
9239 	return (err);
9240 }
9241 
9242 /*
9243  * ARP IOCTLs.
9244  * How does IP get in the business of fronting ARP configuration/queries?
9245  * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP)
9246  * are by tradition passed in through a datagram socket.  That lands in IP.
9247  * As it happens, this is just as well since the interface is quite crude in
9248  * that it passes in no information about protocol or hardware types, or
9249  * interface association.  After making the protocol assumption, IP is in
9250  * the position to look up the name of the ILL, which ARP will need, and
9251  * format a request that can be handled by ARP.	 The request is passed up
9252  * stream to ARP, and the original IOCTL is completed by IP when ARP passes
9253  * back a response.  ARP supports its own set of more general IOCTLs, in
9254  * case anyone is interested.
9255  */
9256 /* ARGSUSED */
9257 int
9258 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9259     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
9260 {
9261 	struct arpreq *ar;
9262 	struct sockaddr_in *sin;
9263 	ire_t	*ire;
9264 	boolean_t isv6;
9265 	mblk_t	*mp1;
9266 	int	err;
9267 	conn_t	*connp;
9268 	ill_t	*ill;
9269 
9270 	/* ioctl comes down on an conn */
9271 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
9272 	connp = Q_TO_CONN(q);
9273 	isv6 = connp->conn_af_isv6;
9274 	if (isv6)
9275 		return (ENXIO);
9276 
9277 	/* Existance verified in ip_wput_nondata */
9278 	mp1 = mp->b_cont->b_cont;
9279 
9280 	ar = (struct arpreq *)mp1->b_rptr;
9281 	sin = (sin_t *)&ar->arp_pa;
9282 
9283 	/*
9284 	 * We need to let ARP know on which interface the IP
9285 	 * address has an ARP mapping. In the IPMP case, a
9286 	 * simple forwarding table lookup will return the
9287 	 * IRE_IF_RESOLVER for the first interface in the group,
9288 	 * which might not be the interface on which the
9289 	 * requested IP address was resolved due to the ill
9290 	 * selection algorithm (see ip_newroute_get_dst_ill()).
9291 	 * So we do a cache table lookup first: if the IRE cache
9292 	 * entry for the IP address is still there, it will
9293 	 * contain the ill pointer for the right interface, so
9294 	 * we use that. If the cache entry has been flushed, we
9295 	 * fall back to the forwarding table lookup. This should
9296 	 * be rare enough since IRE cache entries have a longer
9297 	 * life expectancy than ARP cache entries.
9298 	 */
9299 	ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL);
9300 	if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) ||
9301 	    ((ill = ire_to_ill(ire)) == NULL)) {
9302 		if (ire != NULL)
9303 			ire_refrele(ire);
9304 		ire = ire_ftable_lookup(sin->sin_addr.s_addr,
9305 		    0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0,
9306 		    NULL, MATCH_IRE_TYPE);
9307 		if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) {
9308 			if (ire != NULL)
9309 				ire_refrele(ire);
9310 			return (ENXIO);
9311 		}
9312 	}
9313 	ASSERT(ire != NULL && ill != NULL);
9314 
9315 	err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE);
9316 	ire_refrele(ire);
9317 	return (err);
9318 }
9319 
9320 /*
9321  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
9322  * atomically set/clear the muxids. Also complete the ioctl by acking or
9323  * naking it.  Note that the code is structured such that the link type,
9324  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
9325  * its clones use the persistent link, while pppd(1M) and perhaps many
9326  * other daemons may use non-persistent link.  When combined with some
9327  * ill_t states, linking and unlinking lower streams may be used as
9328  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
9329  */
9330 /* ARGSUSED */
9331 void
9332 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
9333 {
9334 	mblk_t *mp1;
9335 	mblk_t *mp2;
9336 	struct linkblk *li;
9337 	queue_t	*ipwq;
9338 	char	*name;
9339 	struct qinit *qinfo;
9340 	struct ipmx_s *ipmxp;
9341 	ill_t	*ill = NULL;
9342 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9343 	int	err = 0;
9344 	boolean_t	entered_ipsq = B_FALSE;
9345 	boolean_t islink;
9346 	queue_t *dwq = NULL;
9347 
9348 	ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK ||
9349 	    iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK);
9350 
9351 	islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ?
9352 	    B_TRUE : B_FALSE;
9353 
9354 	mp1 = mp->b_cont;	/* This is the linkblk info */
9355 	li = (struct linkblk *)mp1->b_rptr;
9356 
9357 	/*
9358 	 * ARP has added this special mblk, and the utility is asking us
9359 	 * to perform consistency checks, and also atomically set the
9360 	 * muxid. Ifconfig is an example.  It achieves this by using
9361 	 * /dev/arp as the mux to plink the arp stream, and pushes arp on
9362 	 * to /dev/udp[6] stream for use as the mux when plinking the IP
9363 	 * stream. SIOCSLIFMUXID is not required.  See ifconfig.c, arp.c
9364 	 * and other comments in this routine for more details.
9365 	 */
9366 	mp2 = mp1->b_cont;	/* This is added by ARP */
9367 
9368 	/*
9369 	 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than
9370 	 * ifconfig which didn't push ARP on top of the dummy mux, we won't
9371 	 * get the special mblk above.  For backward compatibility, we just
9372 	 * return success.  The utility will use SIOCSLIFMUXID to store
9373 	 * the muxids.  This is not atomic, and can leave the streams
9374 	 * unplumbable if the utility is interrrupted, before it does the
9375 	 * SIOCSLIFMUXID.
9376 	 */
9377 	if (mp2 == NULL) {
9378 		/*
9379 		 * At this point we don't know whether or not this is the
9380 		 * IP module stream or the ARP device stream.  We need to
9381 		 * walk the lower stream in order to find this out, since
9382 		 * the capability negotiation is done only on the IP module
9383 		 * stream.  IP module instance is identified by the module
9384 		 * name IP, non-null q_next, and it's wput not being ip_lwput.
9385 		 * STREAMS ensures that the lower stream (l_qbot) will not
9386 		 * vanish until this ioctl completes. So we can safely walk
9387 		 * the stream or refer to the q_ptr.
9388 		 */
9389 		ipwq = li->l_qbot;
9390 		while (ipwq != NULL) {
9391 			qinfo = ipwq->q_qinfo;
9392 			name = qinfo->qi_minfo->mi_idname;
9393 			if (name != NULL && name[0] != NULL &&
9394 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9395 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9396 			    (ipwq->q_next != NULL)) {
9397 				break;
9398 			}
9399 			ipwq = ipwq->q_next;
9400 		}
9401 		/*
9402 		 * This looks like an IP module stream, so trigger
9403 		 * the capability reset or re-negotiation if necessary.
9404 		 */
9405 		if (ipwq != NULL) {
9406 			ill = ipwq->q_ptr;
9407 			ASSERT(ill != NULL);
9408 
9409 			if (ipsq == NULL) {
9410 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9411 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9412 				if (ipsq == NULL)
9413 					return;
9414 				entered_ipsq = B_TRUE;
9415 			}
9416 			ASSERT(IAM_WRITER_ILL(ill));
9417 			/*
9418 			 * Store the upper read queue of the module
9419 			 * immediately below IP, and count the total
9420 			 * number of lower modules.  Do this only
9421 			 * for I_PLINK or I_LINK event.
9422 			 */
9423 			ill->ill_lmod_rq = NULL;
9424 			ill->ill_lmod_cnt = 0;
9425 			if (islink && (dwq = ipwq->q_next) != NULL) {
9426 				ill->ill_lmod_rq = RD(dwq);
9427 
9428 				while (dwq != NULL) {
9429 					ill->ill_lmod_cnt++;
9430 					dwq = dwq->q_next;
9431 				}
9432 			}
9433 			/*
9434 			 * There's no point in resetting or re-negotiating if
9435 			 * we are not bound to the driver, so only do this if
9436 			 * the DLPI state is idle (up); we assume such state
9437 			 * since ill_ipif_up_count gets incremented in
9438 			 * ipif_up_done(), which is after we are bound to the
9439 			 * driver.  Note that in the case of logical
9440 			 * interfaces, IP won't rebind to the driver unless
9441 			 * the ill_ipif_up_count is 0, meaning that all other
9442 			 * IP interfaces (including the main ipif) are in the
9443 			 * down state.  Because of this, we use such counter
9444 			 * as an indicator, instead of relying on the IPIF_UP
9445 			 * flag, which is per ipif instance.
9446 			 */
9447 			if (ill->ill_ipif_up_count > 0) {
9448 				if (islink)
9449 					ill_capability_probe(ill);
9450 				else
9451 					ill_capability_reset(ill);
9452 			}
9453 		}
9454 		goto done;
9455 	}
9456 
9457 	/*
9458 	 * This is an I_{P}LINK sent down by ifconfig on
9459 	 * /dev/arp. ARP has appended this last (3rd) mblk,
9460 	 * giving more info. STREAMS ensures that the lower
9461 	 * stream (l_qbot) will not vanish until this ioctl
9462 	 * completes. So we can safely walk the stream or refer
9463 	 * to the q_ptr.
9464 	 */
9465 	ipmxp = (struct ipmx_s *)mp2->b_rptr;
9466 	if (ipmxp->ipmx_arpdev_stream) {
9467 		/*
9468 		 * The operation is occuring on the arp-device
9469 		 * stream.
9470 		 */
9471 		ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE,
9472 		    q, mp, ip_sioctl_plink, &err, NULL);
9473 		if (ill == NULL) {
9474 			if (err == EINPROGRESS) {
9475 				return;
9476 			} else {
9477 				err = EINVAL;
9478 				goto done;
9479 			}
9480 		}
9481 
9482 		if (ipsq == NULL) {
9483 			ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
9484 			    NEW_OP, B_TRUE);
9485 			if (ipsq == NULL) {
9486 				ill_refrele(ill);
9487 				return;
9488 			}
9489 			entered_ipsq = B_TRUE;
9490 		}
9491 		ASSERT(IAM_WRITER_ILL(ill));
9492 		ill_refrele(ill);
9493 		/*
9494 		 * To ensure consistency between IP and ARP,
9495 		 * the following LIFO scheme is used in
9496 		 * plink/punlink. (IP first, ARP last).
9497 		 * This is because the muxid's are stored
9498 		 * in the IP stream on the ill.
9499 		 *
9500 		 * I_{P}LINK: ifconfig plinks the IP stream before
9501 		 * plinking the ARP stream. On an arp-dev
9502 		 * stream, IP checks that it is not yet
9503 		 * plinked, and it also checks that the
9504 		 * corresponding IP stream is already plinked.
9505 		 *
9506 		 * I_{P}UNLINK: ifconfig punlinks the ARP stream
9507 		 * before punlinking the IP stream. IP does
9508 		 * not allow punlink of the IP stream unless
9509 		 * the arp stream has been punlinked.
9510 		 *
9511 		 */
9512 		if ((islink &&
9513 		    (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) ||
9514 		    (!islink &&
9515 		    ill->ill_arp_muxid != li->l_index)) {
9516 			err = EINVAL;
9517 			goto done;
9518 		}
9519 		if (islink) {
9520 			ill->ill_arp_muxid = li->l_index;
9521 		} else {
9522 			ill->ill_arp_muxid = 0;
9523 		}
9524 	} else {
9525 		/*
9526 		 * This must be the IP module stream with or
9527 		 * without arp. Walk the stream and locate the
9528 		 * IP module. An IP module instance is
9529 		 * identified by the module name IP, non-null
9530 		 * q_next, and it's wput not being ip_lwput.
9531 		 */
9532 		ipwq = li->l_qbot;
9533 		while (ipwq != NULL) {
9534 			qinfo = ipwq->q_qinfo;
9535 			name = qinfo->qi_minfo->mi_idname;
9536 			if (name != NULL && name[0] != NULL &&
9537 			    (strcmp(name, ip_mod_info.mi_idname) == 0) &&
9538 			    ((void *)(qinfo->qi_putp) != (void *)ip_lwput) &&
9539 			    (ipwq->q_next != NULL)) {
9540 				break;
9541 			}
9542 			ipwq = ipwq->q_next;
9543 		}
9544 		if (ipwq != NULL) {
9545 			ill = ipwq->q_ptr;
9546 			ASSERT(ill != NULL);
9547 
9548 			if (ipsq == NULL) {
9549 				ipsq = ipsq_try_enter(NULL, ill, q, mp,
9550 				    ip_sioctl_plink, NEW_OP, B_TRUE);
9551 				if (ipsq == NULL)
9552 					return;
9553 				entered_ipsq = B_TRUE;
9554 			}
9555 			ASSERT(IAM_WRITER_ILL(ill));
9556 			/*
9557 			 * Return error if the ip_mux_id is
9558 			 * non-zero and command is I_{P}LINK.
9559 			 * If command is I_{P}UNLINK, return
9560 			 * error if the arp-devstr is not
9561 			 * yet punlinked.
9562 			 */
9563 			if ((islink && ill->ill_ip_muxid != 0) ||
9564 			    (!islink && ill->ill_arp_muxid != 0)) {
9565 				err = EINVAL;
9566 				goto done;
9567 			}
9568 			ill->ill_lmod_rq = NULL;
9569 			ill->ill_lmod_cnt = 0;
9570 			if (islink) {
9571 				/*
9572 				 * Store the upper read queue of the module
9573 				 * immediately below IP, and count the total
9574 				 * number of lower modules.
9575 				 */
9576 				if ((dwq = ipwq->q_next) != NULL) {
9577 					ill->ill_lmod_rq = RD(dwq);
9578 
9579 					while (dwq != NULL) {
9580 						ill->ill_lmod_cnt++;
9581 						dwq = dwq->q_next;
9582 					}
9583 				}
9584 				ill->ill_ip_muxid = li->l_index;
9585 			} else {
9586 				ill->ill_ip_muxid = 0;
9587 			}
9588 
9589 			/*
9590 			 * See comments above about resetting/re-
9591 			 * negotiating driver sub-capabilities.
9592 			 */
9593 			if (ill->ill_ipif_up_count > 0) {
9594 				if (islink)
9595 					ill_capability_probe(ill);
9596 				else
9597 					ill_capability_reset(ill);
9598 			}
9599 		}
9600 	}
9601 done:
9602 	iocp->ioc_count = 0;
9603 	iocp->ioc_error = err;
9604 	if (err == 0)
9605 		mp->b_datap->db_type = M_IOCACK;
9606 	else
9607 		mp->b_datap->db_type = M_IOCNAK;
9608 	qreply(q, mp);
9609 
9610 	/* Conn was refheld in ip_sioctl_copyin_setup */
9611 	if (CONN_Q(q))
9612 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
9613 	if (entered_ipsq)
9614 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
9615 }
9616 
9617 /*
9618  * Search the ioctl command in the ioctl tables and return a pointer
9619  * to the ioctl command information. The ioctl command tables are
9620  * static and fully populated at compile time.
9621  */
9622 ip_ioctl_cmd_t *
9623 ip_sioctl_lookup(int ioc_cmd)
9624 {
9625 	int index;
9626 	ip_ioctl_cmd_t *ipip;
9627 	ip_ioctl_cmd_t *ipip_end;
9628 
9629 	if (ioc_cmd == IPI_DONTCARE)
9630 		return (NULL);
9631 
9632 	/*
9633 	 * Do a 2 step search. First search the indexed table
9634 	 * based on the least significant byte of the ioctl cmd.
9635 	 * If we don't find a match, then search the misc table
9636 	 * serially.
9637 	 */
9638 	index = ioc_cmd & 0xFF;
9639 	if (index < ip_ndx_ioctl_count) {
9640 		ipip = &ip_ndx_ioctl_table[index];
9641 		if (ipip->ipi_cmd == ioc_cmd) {
9642 			/* Found a match in the ndx table */
9643 			return (ipip);
9644 		}
9645 	}
9646 
9647 	/* Search the misc table */
9648 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
9649 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
9650 		if (ipip->ipi_cmd == ioc_cmd)
9651 			/* Found a match in the misc table */
9652 			return (ipip);
9653 	}
9654 
9655 	return (NULL);
9656 }
9657 
9658 /*
9659  * Wrapper function for resuming deferred ioctl processing
9660  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9661  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9662  */
9663 /* ARGSUSED */
9664 void
9665 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9666     void *dummy_arg)
9667 {
9668 	ip_sioctl_copyin_setup(q, mp);
9669 }
9670 
9671 /*
9672  * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message
9673  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9674  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9675  * We establish here the size of the block to be copied in.  mi_copyin
9676  * arranges for this to happen, an processing continues in ip_wput with
9677  * an M_IOCDATA message.
9678  */
9679 void
9680 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9681 {
9682 	int	copyin_size;
9683 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9684 	ip_ioctl_cmd_t *ipip;
9685 	cred_t *cr;
9686 
9687 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9688 	if (ipip == NULL) {
9689 		/*
9690 		 * The ioctl is not one we understand or own.
9691 		 * Pass it along to be processed down stream,
9692 		 * if this is a module instance of IP, else nak
9693 		 * the ioctl.
9694 		 */
9695 		if (q->q_next == NULL) {
9696 			goto nak;
9697 		} else {
9698 			putnext(q, mp);
9699 			return;
9700 		}
9701 	}
9702 
9703 	/*
9704 	 * If this is deferred, then we will do all the checks when we
9705 	 * come back.
9706 	 */
9707 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9708 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup()) {
9709 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9710 		return;
9711 	}
9712 
9713 	/*
9714 	 * Only allow a very small subset of IP ioctls on this stream if
9715 	 * IP is a module and not a driver. Allowing ioctls to be processed
9716 	 * in this case may cause assert failures or data corruption.
9717 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9718 	 * ioctls allowed on an IP module stream, after which this stream
9719 	 * normally becomes a multiplexor (at which time the stream head
9720 	 * will fail all ioctls).
9721 	 */
9722 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9723 		if (ipip->ipi_flags & IPI_PASS_DOWN) {
9724 			/*
9725 			 * Pass common Streams ioctls which the IP
9726 			 * module does not own or consume along to
9727 			 * be processed down stream.
9728 			 */
9729 			putnext(q, mp);
9730 			return;
9731 		} else {
9732 			goto nak;
9733 		}
9734 	}
9735 
9736 	/* Make sure we have ioctl data to process. */
9737 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9738 		goto nak;
9739 
9740 	/*
9741 	 * Prefer dblk credential over ioctl credential; some synthesized
9742 	 * ioctls have kcred set because there's no way to crhold()
9743 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9744 	 * the framework; the caller of ioctl needs to hold the reference
9745 	 * for the duration of the call).
9746 	 */
9747 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
9748 
9749 	/* Make sure normal users don't send down privileged ioctls */
9750 	if ((ipip->ipi_flags & IPI_PRIV) &&
9751 	    (cr != NULL) && secpolicy_net_config(cr, B_TRUE) != 0) {
9752 		/* We checked the privilege earlier but log it here */
9753 		miocnak(q, mp, 0, secpolicy_net_config(cr, B_FALSE));
9754 		return;
9755 	}
9756 
9757 	/*
9758 	 * The ioctl command tables can only encode fixed length
9759 	 * ioctl data. If the length is variable, the table will
9760 	 * encode the length as zero. Such special cases are handled
9761 	 * below in the switch.
9762 	 */
9763 	if (ipip->ipi_copyin_size != 0) {
9764 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9765 		return;
9766 	}
9767 
9768 	switch (iocp->ioc_cmd) {
9769 	case O_SIOCGIFCONF:
9770 	case SIOCGIFCONF:
9771 		/*
9772 		 * This IOCTL is hilarious.  See comments in
9773 		 * ip_sioctl_get_ifconf for the story.
9774 		 */
9775 		if (iocp->ioc_count == TRANSPARENT)
9776 			copyin_size = SIZEOF_STRUCT(ifconf,
9777 			    iocp->ioc_flag);
9778 		else
9779 			copyin_size = iocp->ioc_count;
9780 		mi_copyin(q, mp, NULL, copyin_size);
9781 		return;
9782 
9783 	case O_SIOCGLIFCONF:
9784 	case SIOCGLIFCONF:
9785 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9786 		mi_copyin(q, mp, NULL, copyin_size);
9787 		return;
9788 
9789 	case SIOCGLIFSRCOF:
9790 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9791 		mi_copyin(q, mp, NULL, copyin_size);
9792 		return;
9793 	case SIOCGIP6ADDRPOLICY:
9794 		ip_sioctl_ip6addrpolicy(q, mp);
9795 		ip6_asp_table_refrele();
9796 		return;
9797 
9798 	case SIOCSIP6ADDRPOLICY:
9799 		ip_sioctl_ip6addrpolicy(q, mp);
9800 		return;
9801 
9802 	case SIOCGDSTINFO:
9803 		ip_sioctl_dstinfo(q, mp);
9804 		ip6_asp_table_refrele();
9805 		return;
9806 
9807 	case I_PLINK:
9808 	case I_PUNLINK:
9809 	case I_LINK:
9810 	case I_UNLINK:
9811 		/*
9812 		 * We treat non-persistent link similarly as the persistent
9813 		 * link case, in terms of plumbing/unplumbing, as well as
9814 		 * dynamic re-plumbing events indicator.  See comments
9815 		 * in ip_sioctl_plink() for more.
9816 		 *
9817 		 * Request can be enqueued in the 'ipsq' while waiting
9818 		 * to become exclusive. So bump up the conn ref.
9819 		 */
9820 		if (CONN_Q(q))
9821 			CONN_INC_REF(Q_TO_CONN(q));
9822 		ip_sioctl_plink(NULL, q, mp, NULL);
9823 		return;
9824 
9825 	case ND_GET:
9826 	case ND_SET:
9827 		/*
9828 		 * Use of the nd table requires holding the reader lock.
9829 		 * Modifying the nd table thru nd_load/nd_unload requires
9830 		 * the writer lock.
9831 		 */
9832 		rw_enter(&ip_g_nd_lock, RW_READER);
9833 		if (nd_getset(q, ip_g_nd, mp)) {
9834 			rw_exit(&ip_g_nd_lock);
9835 
9836 			if (iocp->ioc_error)
9837 				iocp->ioc_count = 0;
9838 			mp->b_datap->db_type = M_IOCACK;
9839 			qreply(q, mp);
9840 			return;
9841 		}
9842 		rw_exit(&ip_g_nd_lock);
9843 		/*
9844 		 * We don't understand this subioctl of ND_GET / ND_SET.
9845 		 * Maybe intended for some driver / module below us
9846 		 */
9847 		if (q->q_next) {
9848 			putnext(q, mp);
9849 		} else {
9850 			iocp->ioc_error = ENOENT;
9851 			mp->b_datap->db_type = M_IOCNAK;
9852 			iocp->ioc_count = 0;
9853 			qreply(q, mp);
9854 		}
9855 		return;
9856 
9857 	case IP_IOCTL:
9858 		ip_wput_ioctl(q, mp);
9859 		return;
9860 	default:
9861 		cmn_err(CE_PANIC, "should not happen ");
9862 	}
9863 nak:
9864 	if (mp->b_cont != NULL) {
9865 		freemsg(mp->b_cont);
9866 		mp->b_cont = NULL;
9867 	}
9868 	iocp->ioc_error = EINVAL;
9869 	mp->b_datap->db_type = M_IOCNAK;
9870 	iocp->ioc_count = 0;
9871 	qreply(q, mp);
9872 }
9873 
9874 /* ip_wput hands off ARP IOCTL responses to us */
9875 void
9876 ip_sioctl_iocack(queue_t *q, mblk_t *mp)
9877 {
9878 	struct arpreq *ar;
9879 	struct xarpreq *xar;
9880 	area_t	*area;
9881 	mblk_t	*area_mp;
9882 	struct iocblk *iocp;
9883 	mblk_t	*orig_ioc_mp, *tmp;
9884 	struct iocblk	*orig_iocp;
9885 	ill_t *ill;
9886 	conn_t *connp = NULL;
9887 	uint_t ioc_id;
9888 	mblk_t *pending_mp;
9889 	int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE;
9890 	int *flagsp;
9891 	char *storage = NULL;
9892 	sin_t *sin;
9893 	ipaddr_t addr;
9894 	int err;
9895 
9896 	ill = q->q_ptr;
9897 	ASSERT(ill != NULL);
9898 
9899 	/*
9900 	 * We should get back from ARP a packet chain that looks like:
9901 	 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK
9902 	 */
9903 	if (!(area_mp = mp->b_cont) ||
9904 	    (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) ||
9905 	    !(orig_ioc_mp = area_mp->b_cont) ||
9906 	    !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) {
9907 		freemsg(mp);
9908 		return;
9909 	}
9910 
9911 	orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr;
9912 
9913 	tmp = (orig_ioc_mp->b_cont)->b_cont;
9914 	if ((orig_iocp->ioc_cmd == SIOCGXARP) ||
9915 	    (orig_iocp->ioc_cmd == SIOCSXARP) ||
9916 	    (orig_iocp->ioc_cmd == SIOCDXARP)) {
9917 		x_arp_ioctl = B_TRUE;
9918 		xar = (struct xarpreq *)tmp->b_rptr;
9919 		sin = (sin_t *)&xar->xarp_pa;
9920 		flagsp = &xar->xarp_flags;
9921 		storage = xar->xarp_ha.sdl_data;
9922 		if (xar->xarp_ha.sdl_nlen != 0)
9923 			ifx_arp_ioctl = B_TRUE;
9924 	} else {
9925 		ar = (struct arpreq *)tmp->b_rptr;
9926 		sin = (sin_t *)&ar->arp_pa;
9927 		flagsp = &ar->arp_flags;
9928 		storage = ar->arp_ha.sa_data;
9929 	}
9930 
9931 	iocp = (struct iocblk *)mp->b_rptr;
9932 
9933 	/*
9934 	 * Pick out the originating queue based on the ioc_id.
9935 	 */
9936 	ioc_id = iocp->ioc_id;
9937 	pending_mp = ill_pending_mp_get(ill, &connp, ioc_id);
9938 	if (pending_mp == NULL) {
9939 		ASSERT(connp == NULL);
9940 		inet_freemsg(mp);
9941 		return;
9942 	}
9943 	ASSERT(connp != NULL);
9944 	q = CONNP_TO_WQ(connp);
9945 
9946 	/* Uncouple the internally generated IOCTL from the original one */
9947 	area = (area_t *)area_mp->b_rptr;
9948 	area_mp->b_cont = NULL;
9949 
9950 	/*
9951 	 * Restore the b_next and b_prev used by mi code. This is needed
9952 	 * to complete the ioctl using mi* functions. We stored them in
9953 	 * the pending mp prior to sending the request to ARP.
9954 	 */
9955 	orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next;
9956 	orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev;
9957 	inet_freemsg(pending_mp);
9958 
9959 	/*
9960 	 * We're done if there was an error or if this is not an SIOCG{X}ARP
9961 	 * Catch the case where there is an IRE_CACHE by no entry in the
9962 	 * arp table.
9963 	 */
9964 	addr = sin->sin_addr.s_addr;
9965 	if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) {
9966 		ire_t			*ire;
9967 		dl_unitdata_req_t	*dlup;
9968 		mblk_t			*llmp;
9969 		int			addr_len;
9970 		ill_t			*ipsqill = NULL;
9971 
9972 		if (ifx_arp_ioctl) {
9973 			/*
9974 			 * There's no need to lookup the ill, since
9975 			 * we've already done that when we started
9976 			 * processing the ioctl and sent the message
9977 			 * to ARP on that ill.  So use the ill that
9978 			 * is stored in q->q_ptr.
9979 			 */
9980 			ipsqill = ill;
9981 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9982 			    ipsqill->ill_ipif, ALL_ZONES,
9983 			    NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
9984 		} else {
9985 			ire = ire_ctable_lookup(addr, 0, IRE_CACHE,
9986 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
9987 			if (ire != NULL)
9988 				ipsqill = ire_to_ill(ire);
9989 		}
9990 
9991 		if ((x_arp_ioctl) && (ipsqill != NULL))
9992 			storage += ill_xarp_info(&xar->xarp_ha, ipsqill);
9993 
9994 		if (ire != NULL) {
9995 			*flagsp = ATF_INUSE;
9996 			llmp = ire->ire_dlureq_mp;
9997 			if (llmp != NULL && ipsqill != NULL) {
9998 				uchar_t *macaddr;
9999 
10000 				addr_len = ipsqill->ill_phys_addr_length;
10001 				if (x_arp_ioctl && ((addr_len +
10002 				    ipsqill->ill_name_length) >
10003 				    sizeof (xar->xarp_ha.sdl_data))) {
10004 					ire_refrele(ire);
10005 					freemsg(mp);
10006 					ip_ioctl_finish(q, orig_ioc_mp,
10007 					    EINVAL, NO_COPYOUT, NULL, NULL);
10008 					return;
10009 				}
10010 				*flagsp |= ATF_COM;
10011 				dlup = (dl_unitdata_req_t *)llmp->b_rptr;
10012 				if (ipsqill->ill_sap_length < 0)
10013 					macaddr = llmp->b_rptr +
10014 					    dlup->dl_dest_addr_offset;
10015 				else
10016 					macaddr = llmp->b_rptr +
10017 					    dlup->dl_dest_addr_offset +
10018 					    ipsqill->ill_sap_length;
10019 				/*
10020 				 * For SIOCGARP, MAC address length
10021 				 * validation has already been done
10022 				 * before the ioctl was issued to ARP to
10023 				 * allow it to progress only on 6 byte
10024 				 * addressable (ethernet like) media. Thus
10025 				 * the mac address copying can not overwrite
10026 				 * the sa_data area below.
10027 				 */
10028 				bcopy(macaddr, storage, addr_len);
10029 			}
10030 			/* Ditch the internal IOCTL. */
10031 			freemsg(mp);
10032 			ire_refrele(ire);
10033 			ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10034 			return;
10035 		}
10036 	}
10037 
10038 	/*
10039 	 * Delete the coresponding IRE_CACHE if any.
10040 	 * Reset the error if there was one (in case there was no entry
10041 	 * in arp.)
10042 	 */
10043 	if (iocp->ioc_cmd == AR_ENTRY_DELETE) {
10044 		ipif_t *ipintf = NULL;
10045 
10046 		if (ifx_arp_ioctl) {
10047 			/*
10048 			 * There's no need to lookup the ill, since
10049 			 * we've already done that when we started
10050 			 * processing the ioctl and sent the message
10051 			 * to ARP on that ill.  So use the ill that
10052 			 * is stored in q->q_ptr.
10053 			 */
10054 			ipintf = ill->ill_ipif;
10055 		}
10056 		if (ip_ire_clookup_and_delete(addr, ipintf)) {
10057 			/*
10058 			 * The address in "addr" may be an entry for a
10059 			 * router. If that's true, then any off-net
10060 			 * IRE_CACHE entries that go through the router
10061 			 * with address "addr" must be clobbered. Use
10062 			 * ire_walk to achieve this goal.
10063 			 */
10064 			if (ifx_arp_ioctl)
10065 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
10066 				    ire_delete_cache_gw, (char *)&addr, ill);
10067 			else
10068 				ire_walk_v4(ire_delete_cache_gw, (char *)&addr,
10069 				    ALL_ZONES);
10070 			iocp->ioc_error = 0;
10071 		}
10072 	}
10073 
10074 	if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) {
10075 		err = iocp->ioc_error;
10076 		freemsg(mp);
10077 		ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL, NULL);
10078 		return;
10079 	}
10080 
10081 	/*
10082 	 * Completion of an SIOCG{X}ARP.  Translate the information from
10083 	 * the area_t into the struct {x}arpreq.
10084 	 */
10085 	if (x_arp_ioctl) {
10086 		storage += ill_xarp_info(&xar->xarp_ha, ill);
10087 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
10088 		    sizeof (xar->xarp_ha.sdl_data)) {
10089 			freemsg(mp);
10090 			ip_ioctl_finish(q, orig_ioc_mp, EINVAL,
10091 			    NO_COPYOUT, NULL, NULL);
10092 			return;
10093 		}
10094 	}
10095 	*flagsp = ATF_INUSE;
10096 	if (area->area_flags & ACE_F_PERMANENT)
10097 		*flagsp |= ATF_PERM;
10098 	if (area->area_flags & ACE_F_PUBLISH)
10099 		*flagsp |= ATF_PUBL;
10100 	if (area->area_hw_addr_length != 0) {
10101 		*flagsp |= ATF_COM;
10102 		/*
10103 		 * For SIOCGARP, MAC address length validation has
10104 		 * already been done before the ioctl was issued to ARP
10105 		 * to allow it to progress only on 6 byte addressable
10106 		 * (ethernet like) media. Thus the mac address copying
10107 		 * can not overwrite the sa_data area below.
10108 		 */
10109 		bcopy((char *)area + area->area_hw_addr_offset,
10110 		    storage, area->area_hw_addr_length);
10111 	}
10112 
10113 	/* Ditch the internal IOCTL. */
10114 	freemsg(mp);
10115 	/* Complete the original. */
10116 	ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL, NULL);
10117 }
10118 
10119 /*
10120  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
10121  * interface) create the next available logical interface for this
10122  * physical interface.
10123  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
10124  * ipif with the specified name.
10125  *
10126  * If the address family is not AF_UNSPEC then set the address as well.
10127  *
10128  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
10129  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
10130  *
10131  * Executed as a writer on the ill or ill group.
10132  * So no lock is needed to traverse the ipif chain, or examine the
10133  * phyint flags.
10134  */
10135 /* ARGSUSED */
10136 int
10137 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
10138     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10139 {
10140 	mblk_t	*mp1;
10141 	struct lifreq *lifr;
10142 	boolean_t	isv6;
10143 	boolean_t	exists;
10144 	char 	*name;
10145 	char	*endp;
10146 	char	*cp;
10147 	int	namelen;
10148 	ipif_t	*ipif;
10149 	long	id;
10150 	ipsq_t	*ipsq;
10151 	ill_t	*ill;
10152 	sin_t	*sin;
10153 	int	err = 0;
10154 	boolean_t found_sep = B_FALSE;
10155 	conn_t	*connp;
10156 	zoneid_t zoneid;
10157 	int	orig_ifindex = 0;
10158 
10159 	ip1dbg(("ip_sioctl_addif\n"));
10160 	/* Existence of mp1 has been checked in ip_wput_nondata */
10161 	mp1 = mp->b_cont->b_cont;
10162 	/*
10163 	 * Null terminate the string to protect against buffer
10164 	 * overrun. String was generated by user code and may not
10165 	 * be trusted.
10166 	 */
10167 	lifr = (struct lifreq *)mp1->b_rptr;
10168 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
10169 	name = lifr->lifr_name;
10170 	ASSERT(CONN_Q(q));
10171 	connp = Q_TO_CONN(q);
10172 	isv6 = connp->conn_af_isv6;
10173 	zoneid = connp->conn_zoneid;
10174 	namelen = mi_strlen(name);
10175 	if (namelen == 0)
10176 		return (EINVAL);
10177 
10178 	exists = B_FALSE;
10179 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
10180 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
10181 		/*
10182 		 * Allow creating lo0 using SIOCLIFADDIF.
10183 		 * can't be any other writer thread. So can pass null below
10184 		 * for the last 4 args to ipif_lookup_name.
10185 		 */
10186 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen,
10187 		    B_TRUE, &exists, isv6, zoneid, NULL, NULL, NULL, NULL);
10188 		/* Prevent any further action */
10189 		if (ipif == NULL) {
10190 			return (ENOBUFS);
10191 		} else if (!exists) {
10192 			/* We created the ipif now and as writer */
10193 			ipif_refrele(ipif);
10194 			return (0);
10195 		} else {
10196 			ill = ipif->ipif_ill;
10197 			ill_refhold(ill);
10198 			ipif_refrele(ipif);
10199 		}
10200 	} else {
10201 		/* Look for a colon in the name. */
10202 		endp = &name[namelen];
10203 		for (cp = endp; --cp > name; ) {
10204 			if (*cp == IPIF_SEPARATOR_CHAR) {
10205 				found_sep = B_TRUE;
10206 				/*
10207 				 * Reject any non-decimal aliases for plumbing
10208 				 * of logical interfaces. Aliases with leading
10209 				 * zeroes are also rejected as they introduce
10210 				 * ambiguity in the naming of the interfaces.
10211 				 * Comparing with "0" takes care of all such
10212 				 * cases.
10213 				 */
10214 				if ((strncmp("0", cp+1, 1)) == 0)
10215 					return (EINVAL);
10216 
10217 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
10218 				    id <= 0 || *endp != '\0') {
10219 					return (EINVAL);
10220 				}
10221 				*cp = '\0';
10222 				break;
10223 			}
10224 		}
10225 		ill = ill_lookup_on_name(name, B_FALSE, isv6,
10226 		    CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL);
10227 		if (found_sep)
10228 			*cp = IPIF_SEPARATOR_CHAR;
10229 		if (ill == NULL)
10230 			return (err);
10231 	}
10232 
10233 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
10234 	    B_TRUE);
10235 
10236 	/*
10237 	 * Release the refhold due to the lookup, now that we are excl
10238 	 * or we are just returning
10239 	 */
10240 	ill_refrele(ill);
10241 
10242 	if (ipsq == NULL)
10243 		return (EINPROGRESS);
10244 
10245 	/*
10246 	 * If the interface is failed, inactive or offlined, look for a working
10247 	 * interface in the ill group and create the ipif there. If we can't
10248 	 * find a good interface, create the ipif anyway so that in.mpathd can
10249 	 * move it to the first repaired interface.
10250 	 */
10251 	if ((ill->ill_phyint->phyint_flags &
10252 	    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10253 	    ill->ill_phyint->phyint_groupname_len != 0) {
10254 		phyint_t *phyi;
10255 		char *groupname = ill->ill_phyint->phyint_groupname;
10256 
10257 		/*
10258 		 * We're looking for a working interface, but it doesn't matter
10259 		 * if it's up or down; so instead of following the group lists,
10260 		 * we look at each physical interface and compare the groupname.
10261 		 * We're only interested in interfaces with IPv4 (resp. IPv6)
10262 		 * plumbed when we're adding an IPv4 (resp. IPv6) ipif.
10263 		 * Otherwise we create the ipif on the failed interface.
10264 		 */
10265 		rw_enter(&ill_g_lock, RW_READER);
10266 		phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
10267 		for (; phyi != NULL;
10268 		    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
10269 		    phyi, AVL_AFTER)) {
10270 			if (phyi->phyint_groupname_len == 0)
10271 				continue;
10272 			ASSERT(phyi->phyint_groupname != NULL);
10273 			if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 &&
10274 			    !(phyi->phyint_flags &
10275 			    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
10276 			    (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) :
10277 			    (phyi->phyint_illv4 != NULL))) {
10278 				break;
10279 			}
10280 		}
10281 		rw_exit(&ill_g_lock);
10282 
10283 		if (phyi != NULL) {
10284 			orig_ifindex = ill->ill_phyint->phyint_ifindex;
10285 			ill = (ill->ill_isv6 ? phyi->phyint_illv6 :
10286 			    phyi->phyint_illv4);
10287 		}
10288 	}
10289 
10290 	/*
10291 	 * We are now exclusive on the ipsq, so an ill move will be serialized
10292 	 * before or after us.
10293 	 */
10294 	ASSERT(IAM_WRITER_ILL(ill));
10295 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10296 
10297 	if (found_sep && orig_ifindex == 0) {
10298 		/* Now see if there is an IPIF with this unit number. */
10299 		for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
10300 			if (ipif->ipif_id == id) {
10301 				err = EEXIST;
10302 				goto done;
10303 			}
10304 		}
10305 	}
10306 
10307 	/*
10308 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
10309 	 * of lo0. We never come here when we plumb lo0:0. It
10310 	 * happens in ipif_lookup_on_name.
10311 	 * The specified unit number is ignored when we create the ipif on a
10312 	 * different interface. However, we save it in ipif_orig_ipifid below so
10313 	 * that the ipif fails back to the right position.
10314 	 */
10315 	if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ?
10316 	    id : -1, IRE_LOCAL, B_TRUE)) == NULL) {
10317 		err = ENOBUFS;
10318 		goto done;
10319 	}
10320 
10321 	/* Return created name with ioctl */
10322 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
10323 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
10324 	ip1dbg(("created %s\n", lifr->lifr_name));
10325 
10326 	/* Set address */
10327 	sin = (sin_t *)&lifr->lifr_addr;
10328 	if (sin->sin_family != AF_UNSPEC) {
10329 		err = ip_sioctl_addr(ipif, sin, q, mp,
10330 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
10331 	}
10332 
10333 	/* Set ifindex and unit number for failback */
10334 	if (err == 0 && orig_ifindex != 0) {
10335 		ipif->ipif_orig_ifindex = orig_ifindex;
10336 		if (found_sep) {
10337 			ipif->ipif_orig_ipifid = id;
10338 		}
10339 	}
10340 
10341 done:
10342 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
10343 	return (err);
10344 }
10345 
10346 /*
10347  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
10348  * interface) delete it based on the IP address (on this physical interface).
10349  * Otherwise delete it based on the ipif_id.
10350  * Also, special handling to allow a removeif of lo0.
10351  */
10352 /* ARGSUSED */
10353 int
10354 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10355     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10356 {
10357 	conn_t		*connp;
10358 	ill_t		*ill = ipif->ipif_ill;
10359 	boolean_t	 success;
10360 
10361 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
10362 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10363 	ASSERT(IAM_WRITER_IPIF(ipif));
10364 
10365 	connp = Q_TO_CONN(q);
10366 	/*
10367 	 * Special case for unplumbing lo0 (the loopback physical interface).
10368 	 * If unplumbing lo0, the incoming address structure has been
10369 	 * initialized to all zeros. When unplumbing lo0, all its logical
10370 	 * interfaces must be removed too.
10371 	 *
10372 	 * Note that this interface may be called to remove a specific
10373 	 * loopback logical interface (eg, lo0:1). But in that case
10374 	 * ipif->ipif_id != 0 so that the code path for that case is the
10375 	 * same as any other interface (meaning it skips the code directly
10376 	 * below).
10377 	 */
10378 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10379 		if (sin->sin_family == AF_UNSPEC &&
10380 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
10381 			/*
10382 			 * Mark it condemned. No new ref. will be made to ill.
10383 			 */
10384 			mutex_enter(&ill->ill_lock);
10385 			ill->ill_state_flags |= ILL_CONDEMNED;
10386 			for (ipif = ill->ill_ipif; ipif != NULL;
10387 			    ipif = ipif->ipif_next) {
10388 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
10389 			}
10390 			mutex_exit(&ill->ill_lock);
10391 
10392 			ipif = ill->ill_ipif;
10393 			/* unplumb the loopback interface */
10394 			ill_delete(ill);
10395 			mutex_enter(&connp->conn_lock);
10396 			mutex_enter(&ill->ill_lock);
10397 			ASSERT(ill->ill_group == NULL);
10398 
10399 			/* Are any references to this ill active */
10400 			if (ill_is_quiescent(ill)) {
10401 				mutex_exit(&ill->ill_lock);
10402 				mutex_exit(&connp->conn_lock);
10403 				ill_delete_tail(ill);
10404 				mi_free(ill);
10405 				return (0);
10406 			}
10407 			success = ipsq_pending_mp_add(connp, ipif,
10408 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
10409 			mutex_exit(&connp->conn_lock);
10410 			mutex_exit(&ill->ill_lock);
10411 			if (success)
10412 				return (EINPROGRESS);
10413 			else
10414 				return (EINTR);
10415 		}
10416 	}
10417 
10418 	/*
10419 	 * We are exclusive on the ipsq, so an ill move will be serialized
10420 	 * before or after us.
10421 	 */
10422 	ASSERT(ill->ill_move_in_progress == B_FALSE);
10423 
10424 	if (ipif->ipif_id == 0) {
10425 		/* Find based on address */
10426 		if (ipif->ipif_isv6) {
10427 			sin6_t *sin6;
10428 
10429 			if (sin->sin_family != AF_INET6)
10430 				return (EAFNOSUPPORT);
10431 
10432 			sin6 = (sin6_t *)sin;
10433 			/* We are a writer, so we should be able to lookup */
10434 			ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10435 			    ill, ALL_ZONES, NULL, NULL, NULL, NULL);
10436 			if (ipif == NULL) {
10437 				/*
10438 				 * Maybe the address in on another interface in
10439 				 * the same IPMP group? We check this below.
10440 				 */
10441 				ipif = ipif_lookup_addr_v6(&sin6->sin6_addr,
10442 				    NULL, ALL_ZONES, NULL, NULL, NULL, NULL);
10443 			}
10444 		} else {
10445 			ipaddr_t addr;
10446 
10447 			if (sin->sin_family != AF_INET)
10448 				return (EAFNOSUPPORT);
10449 
10450 			addr = sin->sin_addr.s_addr;
10451 			/* We are a writer, so we should be able to lookup */
10452 			ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL,
10453 			    NULL, NULL, NULL);
10454 			if (ipif == NULL) {
10455 				/*
10456 				 * Maybe the address in on another interface in
10457 				 * the same IPMP group? We check this below.
10458 				 */
10459 				ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES,
10460 				    NULL, NULL, NULL, NULL);
10461 			}
10462 		}
10463 		if (ipif == NULL) {
10464 			return (EADDRNOTAVAIL);
10465 		}
10466 		/*
10467 		 * When the address to be removed is hosted on a different
10468 		 * interface, we check if the interface is in the same IPMP
10469 		 * group as the specified one; if so we proceed with the
10470 		 * removal.
10471 		 * ill->ill_group is NULL when the ill is down, so we have to
10472 		 * compare the group names instead.
10473 		 */
10474 		if (ipif->ipif_ill != ill &&
10475 		    (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 ||
10476 		    ill->ill_phyint->phyint_groupname_len == 0 ||
10477 		    mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname,
10478 		    ill->ill_phyint->phyint_groupname) != 0)) {
10479 			ipif_refrele(ipif);
10480 			return (EADDRNOTAVAIL);
10481 		}
10482 
10483 		/* This is a writer */
10484 		ipif_refrele(ipif);
10485 	}
10486 
10487 	/*
10488 	 * Can not delete instance zero since it is tied to the ill.
10489 	 */
10490 	if (ipif->ipif_id == 0)
10491 		return (EBUSY);
10492 
10493 	mutex_enter(&ill->ill_lock);
10494 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
10495 	mutex_exit(&ill->ill_lock);
10496 
10497 	ipif_free(ipif);
10498 
10499 	mutex_enter(&connp->conn_lock);
10500 	mutex_enter(&ill->ill_lock);
10501 
10502 	/* Are any references to this ipif active */
10503 	if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) {
10504 		mutex_exit(&ill->ill_lock);
10505 		mutex_exit(&connp->conn_lock);
10506 		ipif_down_tail(ipif);
10507 		ipif_free_tail(ipif);
10508 		return (0);
10509 	    }
10510 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
10511 	    IPIF_FREE);
10512 	mutex_exit(&ill->ill_lock);
10513 	mutex_exit(&connp->conn_lock);
10514 	if (success)
10515 		return (EINPROGRESS);
10516 	else
10517 		return (EINTR);
10518 }
10519 
10520 /*
10521  * Restart the removeif ioctl. The refcnt has gone down to 0.
10522  * The ipif is already condemned. So can't find it thru lookups.
10523  */
10524 /* ARGSUSED */
10525 int
10526 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
10527     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
10528 {
10529 	ill_t *ill;
10530 
10531 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
10532 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10533 	if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) {
10534 		ill = ipif->ipif_ill;
10535 		ASSERT(IAM_WRITER_ILL(ill));
10536 		ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) &&
10537 		    (ill->ill_state_flags & IPIF_CONDEMNED));
10538 		ill_delete_tail(ill);
10539 		mi_free(ill);
10540 		return (0);
10541 	}
10542 
10543 	ill = ipif->ipif_ill;
10544 	ASSERT(IAM_WRITER_IPIF(ipif));
10545 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
10546 
10547 	ipif_down_tail(ipif);
10548 	ipif_free_tail(ipif);
10549 
10550 	ILL_UNMARK_CHANGING(ill);
10551 	return (0);
10552 }
10553 
10554 /*
10555  * Set the local interface address.
10556  * Allow an address of all zero when the interface is down.
10557  */
10558 /* ARGSUSED */
10559 int
10560 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10561     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
10562 {
10563 	int err = 0;
10564 	in6_addr_t v6addr;
10565 	boolean_t need_up = B_FALSE;
10566 
10567 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
10568 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10569 
10570 	ASSERT(IAM_WRITER_IPIF(ipif));
10571 
10572 	if (ipif->ipif_isv6) {
10573 		sin6_t *sin6;
10574 		ill_t *ill;
10575 		phyint_t *phyi;
10576 
10577 		if (sin->sin_family != AF_INET6)
10578 			return (EAFNOSUPPORT);
10579 
10580 		sin6 = (sin6_t *)sin;
10581 		v6addr = sin6->sin6_addr;
10582 		ill = ipif->ipif_ill;
10583 		phyi = ill->ill_phyint;
10584 
10585 		/*
10586 		 * Enforce that true multicast interfaces have a link-local
10587 		 * address for logical unit 0.
10588 		 */
10589 		if (ipif->ipif_id == 0 &&
10590 		    (ill->ill_flags & ILLF_MULTICAST) &&
10591 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
10592 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
10593 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
10594 			return (EADDRNOTAVAIL);
10595 		}
10596 
10597 		/*
10598 		 * up interfaces shouldn't have the unspecified address
10599 		 * unless they also have the IPIF_NOLOCAL flags set and
10600 		 * have a subnet assigned.
10601 		 */
10602 		if ((ipif->ipif_flags & IPIF_UP) &&
10603 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
10604 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
10605 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
10606 			return (EADDRNOTAVAIL);
10607 		}
10608 
10609 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10610 			return (EADDRNOTAVAIL);
10611 	} else {
10612 		ipaddr_t addr;
10613 
10614 		if (sin->sin_family != AF_INET)
10615 			return (EAFNOSUPPORT);
10616 
10617 		addr = sin->sin_addr.s_addr;
10618 
10619 		/* Allow 0 as the local address. */
10620 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10621 			return (EADDRNOTAVAIL);
10622 
10623 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10624 	}
10625 
10626 
10627 	/*
10628 	 * Even if there is no change we redo things just to rerun
10629 	 * ipif_set_default.
10630 	 */
10631 	if (ipif->ipif_flags & IPIF_UP) {
10632 		/*
10633 		 * Setting a new local address, make sure
10634 		 * we have net and subnet bcast ire's for
10635 		 * the old address if we need them.
10636 		 */
10637 		if (!ipif->ipif_isv6)
10638 			ipif_check_bcast_ires(ipif);
10639 		/*
10640 		 * If the interface is already marked up,
10641 		 * we call ipif_down which will take care
10642 		 * of ditching any IREs that have been set
10643 		 * up based on the old interface address.
10644 		 */
10645 		err = ipif_logical_down(ipif, q, mp);
10646 		if (err == EINPROGRESS)
10647 			return (err);
10648 		ipif_down_tail(ipif);
10649 		need_up = 1;
10650 	}
10651 
10652 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
10653 	return (err);
10654 }
10655 
10656 int
10657 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10658     boolean_t need_up)
10659 {
10660 	in6_addr_t v6addr;
10661 	ipaddr_t addr;
10662 	sin6_t	*sin6;
10663 	int	err = 0;
10664 
10665 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
10666 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10667 	ASSERT(IAM_WRITER_IPIF(ipif));
10668 	if (ipif->ipif_isv6) {
10669 		sin6 = (sin6_t *)sin;
10670 		v6addr = sin6->sin6_addr;
10671 	} else {
10672 		addr = sin->sin_addr.s_addr;
10673 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10674 	}
10675 	mutex_enter(&ipif->ipif_ill->ill_lock);
10676 	ipif->ipif_v6lcl_addr = v6addr;
10677 	if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) {
10678 		ipif->ipif_v6src_addr = ipv6_all_zeros;
10679 	} else {
10680 		ipif->ipif_v6src_addr = v6addr;
10681 	}
10682 
10683 	if ((ipif->ipif_isv6) && IN6_IS_ADDR_6TO4(&v6addr) &&
10684 		(!ipif->ipif_ill->ill_is_6to4tun)) {
10685 		queue_t *wqp = ipif->ipif_ill->ill_wq;
10686 
10687 		/*
10688 		 * The local address of this interface is a 6to4 address,
10689 		 * check if this interface is in fact a 6to4 tunnel or just
10690 		 * an interface configured with a 6to4 address.  We are only
10691 		 * interested in the former.
10692 		 */
10693 		if (wqp != NULL) {
10694 			while ((wqp->q_next != NULL) &&
10695 			    (wqp->q_next->q_qinfo != NULL) &&
10696 			    (wqp->q_next->q_qinfo->qi_minfo != NULL)) {
10697 
10698 				if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum
10699 				    == TUN6TO4_MODID) {
10700 					/* set for use in IP */
10701 					ipif->ipif_ill->ill_is_6to4tun = 1;
10702 					break;
10703 				}
10704 				wqp = wqp->q_next;
10705 			}
10706 		}
10707 	}
10708 
10709 	ipif_set_default(ipif);
10710 	mutex_exit(&ipif->ipif_ill->ill_lock);
10711 
10712 	if (need_up) {
10713 		/*
10714 		 * Now bring the interface back up.  If this
10715 		 * is the only IPIF for the ILL, ipif_up
10716 		 * will have to re-bind to the device, so
10717 		 * we may get back EINPROGRESS, in which
10718 		 * case, this IOCTL will get completed in
10719 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10720 		 */
10721 		err = ipif_up(ipif, q, mp);
10722 	} else {
10723 		/*
10724 		 * Update the IPIF list in SCTP, ipif_up_done() will do it
10725 		 * if need_up is true.
10726 		 */
10727 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10728 	}
10729 
10730 	return (err);
10731 }
10732 
10733 
10734 /*
10735  * Restart entry point to restart the address set operation after the
10736  * refcounts have dropped to zero.
10737  */
10738 /* ARGSUSED */
10739 int
10740 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10741     ip_ioctl_cmd_t *ipip, void *ifreq)
10742 {
10743 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
10744 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10745 	ASSERT(IAM_WRITER_IPIF(ipif));
10746 	ipif_down_tail(ipif);
10747 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
10748 }
10749 
10750 /* ARGSUSED */
10751 int
10752 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10753     ip_ioctl_cmd_t *ipip, void *if_req)
10754 {
10755 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10756 	struct lifreq *lifr = (struct lifreq *)if_req;
10757 
10758 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
10759 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10760 	/*
10761 	 * The net mask and address can't change since we have a
10762 	 * reference to the ipif. So no lock is necessary.
10763 	 */
10764 	if (ipif->ipif_isv6) {
10765 		*sin6 = sin6_null;
10766 		sin6->sin6_family = AF_INET6;
10767 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
10768 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10769 		lifr->lifr_addrlen =
10770 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10771 	} else {
10772 		*sin = sin_null;
10773 		sin->sin_family = AF_INET;
10774 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
10775 		if (ipip->ipi_cmd_type == LIF_CMD) {
10776 			lifr->lifr_addrlen =
10777 			    ip_mask_to_plen(ipif->ipif_net_mask);
10778 		}
10779 	}
10780 	return (0);
10781 }
10782 
10783 /*
10784  * Set the destination address for a pt-pt interface.
10785  */
10786 /* ARGSUSED */
10787 int
10788 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10789     ip_ioctl_cmd_t *ipip, void *if_req)
10790 {
10791 	int err = 0;
10792 	in6_addr_t v6addr;
10793 	boolean_t need_up = B_FALSE;
10794 
10795 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
10796 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10797 	ASSERT(IAM_WRITER_IPIF(ipif));
10798 
10799 	if (ipif->ipif_isv6) {
10800 		sin6_t *sin6;
10801 
10802 		if (sin->sin_family != AF_INET6)
10803 			return (EAFNOSUPPORT);
10804 
10805 		sin6 = (sin6_t *)sin;
10806 		v6addr = sin6->sin6_addr;
10807 
10808 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
10809 			return (EADDRNOTAVAIL);
10810 	} else {
10811 		ipaddr_t addr;
10812 
10813 		if (sin->sin_family != AF_INET)
10814 			return (EAFNOSUPPORT);
10815 
10816 		addr = sin->sin_addr.s_addr;
10817 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
10818 			return (EADDRNOTAVAIL);
10819 
10820 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10821 	}
10822 
10823 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10824 		return (0);	/* No change */
10825 
10826 	if (ipif->ipif_flags & IPIF_UP) {
10827 		/*
10828 		 * If the interface is already marked up,
10829 		 * we call ipif_down which will take care
10830 		 * of ditching any IREs that have been set
10831 		 * up based on the old pp dst address.
10832 		 */
10833 		err = ipif_logical_down(ipif, q, mp);
10834 		if (err == EINPROGRESS)
10835 			return (err);
10836 		ipif_down_tail(ipif);
10837 		need_up = B_TRUE;
10838 	}
10839 	/*
10840 	 * could return EINPROGRESS. If so ioctl will complete in
10841 	 * ip_rput_dlpi_writer
10842 	 */
10843 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10844 	return (err);
10845 }
10846 
10847 static int
10848 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10849     boolean_t need_up)
10850 {
10851 	in6_addr_t v6addr;
10852 	ill_t	*ill = ipif->ipif_ill;
10853 	int	err = 0;
10854 
10855 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n",
10856 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10857 	if (ipif->ipif_isv6) {
10858 		sin6_t *sin6;
10859 
10860 		sin6 = (sin6_t *)sin;
10861 		v6addr = sin6->sin6_addr;
10862 	} else {
10863 		ipaddr_t addr;
10864 
10865 		addr = sin->sin_addr.s_addr;
10866 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10867 	}
10868 	mutex_enter(&ill->ill_lock);
10869 	/* Set point to point destination address. */
10870 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10871 		/*
10872 		 * Allow this as a means of creating logical
10873 		 * pt-pt interfaces on top of e.g. an Ethernet.
10874 		 * XXX Undocumented HACK for testing.
10875 		 * pt-pt interfaces are created with NUD disabled.
10876 		 */
10877 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10878 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10879 		if (ipif->ipif_isv6)
10880 			ipif->ipif_ill->ill_flags |= ILLF_NONUD;
10881 	}
10882 
10883 	/* Set the new address. */
10884 	ipif->ipif_v6pp_dst_addr = v6addr;
10885 	/* Make sure subnet tracks pp_dst */
10886 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10887 	mutex_exit(&ill->ill_lock);
10888 
10889 	if (need_up) {
10890 		/*
10891 		 * Now bring the interface back up.  If this
10892 		 * is the only IPIF for the ILL, ipif_up
10893 		 * will have to re-bind to the device, so
10894 		 * we may get back EINPROGRESS, in which
10895 		 * case, this IOCTL will get completed in
10896 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10897 		 */
10898 		err = ipif_up(ipif, q, mp);
10899 	}
10900 	return (err);
10901 }
10902 
10903 /*
10904  * Restart entry point to restart the dstaddress set operation after the
10905  * refcounts have dropped to zero.
10906  */
10907 /* ARGSUSED */
10908 int
10909 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10910     ip_ioctl_cmd_t *ipip, void *ifreq)
10911 {
10912 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10913 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10914 	ipif_down_tail(ipif);
10915 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10916 }
10917 
10918 /* ARGSUSED */
10919 int
10920 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10921     ip_ioctl_cmd_t *ipip, void *if_req)
10922 {
10923 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10924 
10925 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10926 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10927 	/*
10928 	 * Get point to point destination address. The addresses can't
10929 	 * change since we hold a reference to the ipif.
10930 	 */
10931 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10932 		return (EADDRNOTAVAIL);
10933 
10934 	if (ipif->ipif_isv6) {
10935 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10936 		*sin6 = sin6_null;
10937 		sin6->sin6_family = AF_INET6;
10938 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10939 	} else {
10940 		*sin = sin_null;
10941 		sin->sin_family = AF_INET;
10942 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10943 	}
10944 	return (0);
10945 }
10946 
10947 /*
10948  * part of ipmp, make this func return the active/inactive state and
10949  * caller can set once atomically instead of multiple mutex_enter/mutex_exit
10950  */
10951 /*
10952  * This function either sets or clears the IFF_INACTIVE flag.
10953  *
10954  * As long as there are some addresses or multicast memberships on the
10955  * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we
10956  * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface
10957  * will be used for outbound packets.
10958  *
10959  * Caller needs to verify the validity of setting IFF_INACTIVE.
10960  */
10961 static void
10962 phyint_inactive(phyint_t *phyi)
10963 {
10964 	ill_t *ill_v4;
10965 	ill_t *ill_v6;
10966 	ipif_t *ipif;
10967 	ilm_t *ilm;
10968 
10969 	ill_v4 = phyi->phyint_illv4;
10970 	ill_v6 = phyi->phyint_illv6;
10971 
10972 	/*
10973 	 * No need for a lock while traversing the list since iam
10974 	 * a writer
10975 	 */
10976 	if (ill_v4 != NULL) {
10977 		ASSERT(IAM_WRITER_ILL(ill_v4));
10978 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
10979 		    ipif = ipif->ipif_next) {
10980 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
10981 				mutex_enter(&phyi->phyint_lock);
10982 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10983 				mutex_exit(&phyi->phyint_lock);
10984 				return;
10985 			}
10986 		}
10987 		for (ilm = ill_v4->ill_ilm; ilm != NULL;
10988 		    ilm = ilm->ilm_next) {
10989 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
10990 				mutex_enter(&phyi->phyint_lock);
10991 				phyi->phyint_flags &= ~PHYI_INACTIVE;
10992 				mutex_exit(&phyi->phyint_lock);
10993 				return;
10994 			}
10995 		}
10996 	}
10997 	if (ill_v6 != NULL) {
10998 		ill_v6 = phyi->phyint_illv6;
10999 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
11000 		    ipif = ipif->ipif_next) {
11001 			if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) {
11002 				mutex_enter(&phyi->phyint_lock);
11003 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11004 				mutex_exit(&phyi->phyint_lock);
11005 				return;
11006 			}
11007 		}
11008 		for (ilm = ill_v6->ill_ilm; ilm != NULL;
11009 		    ilm = ilm->ilm_next) {
11010 			if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) {
11011 				mutex_enter(&phyi->phyint_lock);
11012 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11013 				mutex_exit(&phyi->phyint_lock);
11014 				return;
11015 			}
11016 		}
11017 	}
11018 	mutex_enter(&phyi->phyint_lock);
11019 	phyi->phyint_flags |= PHYI_INACTIVE;
11020 	mutex_exit(&phyi->phyint_lock);
11021 }
11022 
11023 /*
11024  * This function is called only when the phyint flags change. Currently
11025  * called from ip_sioctl_flags. We re-do the broadcast nomination so
11026  * that we can select a good ill.
11027  */
11028 static void
11029 ip_redo_nomination(phyint_t *phyi)
11030 {
11031 	ill_t *ill_v4;
11032 
11033 	ill_v4 = phyi->phyint_illv4;
11034 
11035 	if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
11036 		ASSERT(IAM_WRITER_ILL(ill_v4));
11037 		if (ill_v4->ill_group->illgrp_ill_count > 1)
11038 			ill_nominate_bcast_rcv(ill_v4->ill_group);
11039 	}
11040 }
11041 
11042 /*
11043  * Heuristic to check if ill is INACTIVE.
11044  * Checks if ill has an ipif with an usable ip address.
11045  *
11046  * Return values:
11047  *	B_TRUE	- ill is INACTIVE; has no usable ipif
11048  *	B_FALSE - ill is not INACTIVE; ill has at least one usable ipif
11049  */
11050 static boolean_t
11051 ill_is_inactive(ill_t *ill)
11052 {
11053 	ipif_t *ipif;
11054 
11055 	/* Check whether it is in an IPMP group */
11056 	if (ill->ill_phyint->phyint_groupname == NULL)
11057 		return (B_FALSE);
11058 
11059 	if (ill->ill_ipif_up_count == 0)
11060 		return (B_TRUE);
11061 
11062 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11063 		uint64_t flags = ipif->ipif_flags;
11064 
11065 		/*
11066 		 * This ipif is usable if it is IPIF_UP and not a
11067 		 * dedicated test address.  A dedicated test address
11068 		 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED
11069 		 * (note in particular that V6 test addresses are
11070 		 * link-local data addresses and thus are marked
11071 		 * IPIF_NOFAILOVER but not IPIF_DEPRECATED).
11072 		 */
11073 		if ((flags & IPIF_UP) &&
11074 		    ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) !=
11075 		    (IPIF_DEPRECATED|IPIF_NOFAILOVER)))
11076 			return (B_FALSE);
11077 	}
11078 	return (B_TRUE);
11079 }
11080 
11081 /*
11082  * Set interface flags.
11083  * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT,
11084  * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST,
11085  * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE.
11086  *
11087  * NOTE : We really don't enforce that ipif_id zero should be used
11088  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
11089  *	  is because applications generally does SICGLIFFLAGS and
11090  *	  ORs in the new flags (that affects the logical) and does a
11091  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
11092  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
11093  *	  flags that will be turned on is correct with respect to
11094  *	  ipif_id 0. For backward compatibility reasons, it is not done.
11095  */
11096 /* ARGSUSED */
11097 int
11098 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11099     ip_ioctl_cmd_t *ipip, void *if_req)
11100 {
11101 	uint64_t turn_on;
11102 	uint64_t turn_off;
11103 	int	err;
11104 	boolean_t need_up = B_FALSE;
11105 	phyint_t *phyi;
11106 	ill_t *ill;
11107 	uint64_t intf_flags;
11108 	boolean_t phyint_flags_modified = B_FALSE;
11109 	uint64_t flags;
11110 	struct ifreq *ifr;
11111 	struct lifreq *lifr;
11112 	boolean_t set_linklocal = B_FALSE;
11113 	boolean_t zero_source = B_FALSE;
11114 
11115 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
11116 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11117 
11118 	ASSERT(IAM_WRITER_IPIF(ipif));
11119 
11120 	ill = ipif->ipif_ill;
11121 	phyi = ill->ill_phyint;
11122 
11123 	if (ipip->ipi_cmd_type == IF_CMD) {
11124 		ifr = (struct ifreq *)if_req;
11125 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
11126 	} else {
11127 		lifr = (struct lifreq *)if_req;
11128 		flags = lifr->lifr_flags;
11129 	}
11130 
11131 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11132 
11133 	/*
11134 	 * Has the flags been set correctly till now ?
11135 	 */
11136 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11137 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11138 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11139 	/*
11140 	 * Compare the new flags to the old, and partition
11141 	 * into those coming on and those going off.
11142 	 * For the 16 bit command keep the bits above bit 16 unchanged.
11143 	 */
11144 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
11145 		flags |= intf_flags & ~0xFFFF;
11146 
11147 	/*
11148 	 * First check which bits will change and then which will
11149 	 * go on and off
11150 	 */
11151 	turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE;
11152 	if (!turn_on)
11153 		return (0);	/* No change */
11154 
11155 	turn_off = intf_flags & turn_on;
11156 	turn_on ^= turn_off;
11157 	err = 0;
11158 
11159 	/*
11160 	 * Don't allow any bits belonging to the logical interface
11161 	 * to be set or cleared on the replacement ipif that was
11162 	 * created temporarily during a MOVE.
11163 	 */
11164 	if (ipif->ipif_replace_zero &&
11165 	    ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) {
11166 		return (EINVAL);
11167 	}
11168 
11169 	/*
11170 	 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on
11171 	 * IPv6 interfaces.
11172 	 */
11173 	if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6))
11174 		return (EINVAL);
11175 
11176 	/*
11177 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
11178 	 * interfaces.  It makes no sense in that context.
11179 	 */
11180 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
11181 		return (EINVAL);
11182 
11183 	if (flags & (IFF_NOLOCAL|IFF_ANYCAST))
11184 		zero_source = B_TRUE;
11185 
11186 	/*
11187 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
11188 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
11189 	 * If the link local address isn't set, and can be set, it will get
11190 	 * set later on in this function.
11191 	 */
11192 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
11193 	    (flags & IFF_UP) && !zero_source &&
11194 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
11195 		if (ipif_cant_setlinklocal(ipif))
11196 			return (EINVAL);
11197 		set_linklocal = B_TRUE;
11198 	}
11199 
11200 	/*
11201 	 * ILL cannot be part of a usesrc group and and IPMP group at the
11202 	 * same time. No need to grab ill_g_usesrc_lock here, see
11203 	 * synchronization notes in ip.c
11204 	 */
11205 	if (turn_on & PHYI_STANDBY &&
11206 	    ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
11207 		return (EINVAL);
11208 	}
11209 
11210 	/*
11211 	 * If we modify physical interface flags, we'll potentially need to
11212 	 * send up two routing socket messages for the changes (one for the
11213 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
11214 	 */
11215 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
11216 		phyint_flags_modified = B_TRUE;
11217 
11218 	/*
11219 	 * If we are setting or clearing FAILED or STANDBY or OFFLINE,
11220 	 * we need to flush the IRE_CACHES belonging to this ill.
11221 	 * We handle this case here without doing the DOWN/UP dance
11222 	 * like it is done for other flags. If some other flags are
11223 	 * being turned on/off with FAILED/STANDBY/OFFLINE, the code
11224 	 * below will handle it by bringing it down and then
11225 	 * bringing it UP.
11226 	 */
11227 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) {
11228 		ill_t *ill_v4, *ill_v6;
11229 
11230 		ill_v4 = phyi->phyint_illv4;
11231 		ill_v6 = phyi->phyint_illv6;
11232 
11233 		/*
11234 		 * First set the INACTIVE flag if needed. Then delete the ires.
11235 		 * ire_add will atomically prevent creating new IRE_CACHEs
11236 		 * unless hidden flag is set.
11237 		 * PHYI_FAILED and PHYI_INACTIVE are exclusive
11238 		 */
11239 		if ((turn_on & PHYI_FAILED) &&
11240 		    ((intf_flags & PHYI_STANDBY) || !ipmp_enable_failback)) {
11241 			/* Reset PHYI_INACTIVE when PHYI_FAILED is being set */
11242 			phyi->phyint_flags &= ~PHYI_INACTIVE;
11243 		}
11244 		if ((turn_off & PHYI_FAILED) &&
11245 		    ((intf_flags & PHYI_STANDBY) ||
11246 		    (!ipmp_enable_failback && ill_is_inactive(ill)))) {
11247 			phyint_inactive(phyi);
11248 		}
11249 
11250 		if (turn_on & PHYI_STANDBY) {
11251 			/*
11252 			 * We implicitly set INACTIVE only when STANDBY is set.
11253 			 * INACTIVE is also set on non-STANDBY phyint when user
11254 			 * disables FAILBACK using configuration file.
11255 			 * Do not allow STANDBY to be set on such INACTIVE
11256 			 * phyint
11257 			 */
11258 			if (phyi->phyint_flags & PHYI_INACTIVE)
11259 				return (EINVAL);
11260 			if (!(phyi->phyint_flags & PHYI_FAILED))
11261 				phyint_inactive(phyi);
11262 		}
11263 		if (turn_off & PHYI_STANDBY) {
11264 			if (ipmp_enable_failback) {
11265 				/*
11266 				 * Reset PHYI_INACTIVE.
11267 				 */
11268 				phyi->phyint_flags &= ~PHYI_INACTIVE;
11269 			} else if (ill_is_inactive(ill) &&
11270 			    !(phyi->phyint_flags & PHYI_FAILED)) {
11271 				/*
11272 				 * Need to set INACTIVE, when user sets
11273 				 * STANDBY on a non-STANDBY phyint and
11274 				 * later resets STANDBY
11275 				 */
11276 				phyint_inactive(phyi);
11277 			}
11278 		}
11279 		/*
11280 		 * We should always send up a message so that the
11281 		 * daemons come to know of it. Note that the zeroth
11282 		 * interface can be down and the check below for IPIF_UP
11283 		 * will not make sense as we are actually setting
11284 		 * a phyint flag here. We assume that the ipif used
11285 		 * is always the zeroth ipif. (ip_rts_ifmsg does not
11286 		 * send up any message for non-zero ipifs).
11287 		 */
11288 		phyint_flags_modified = B_TRUE;
11289 
11290 		if (ill_v4 != NULL) {
11291 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11292 			    IRE_CACHE, ill_stq_cache_delete,
11293 			    (char *)ill_v4, ill_v4);
11294 			illgrp_reset_schednext(ill_v4);
11295 		}
11296 		if (ill_v6 != NULL) {
11297 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
11298 			    IRE_CACHE, ill_stq_cache_delete,
11299 			    (char *)ill_v6, ill_v6);
11300 			illgrp_reset_schednext(ill_v6);
11301 		}
11302 	}
11303 
11304 	/*
11305 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
11306 	 * status of the interface and, if the interface is part of an IPMP
11307 	 * group, all other interfaces that are part of the same IPMP
11308 	 * group.
11309 	 */
11310 	if ((turn_on | turn_off) & ILLF_ROUTER) {
11311 		(void) ill_forward_set(q, mp, ((turn_on & ILLF_ROUTER) != 0),
11312 		    (caddr_t)ill);
11313 	}
11314 
11315 	/*
11316 	 * If the interface is not UP and we are not going to
11317 	 * bring it UP, record the flags and return. When the
11318 	 * interface comes UP later, the right actions will be
11319 	 * taken.
11320 	 */
11321 	if (!(ipif->ipif_flags & IPIF_UP) &&
11322 	    !(turn_on & IPIF_UP)) {
11323 		/* Record new flags in their respective places. */
11324 		mutex_enter(&ill->ill_lock);
11325 		mutex_enter(&ill->ill_phyint->phyint_lock);
11326 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11327 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11328 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11329 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11330 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11331 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11332 		mutex_exit(&ill->ill_lock);
11333 		mutex_exit(&ill->ill_phyint->phyint_lock);
11334 
11335 		/*
11336 		 * We do the broadcast and nomination here rather
11337 		 * than waiting for a FAILOVER/FAILBACK to happen. In
11338 		 * the case of FAILBACK from INACTIVE standby to the
11339 		 * interface that has been repaired, PHYI_FAILED has not
11340 		 * been cleared yet. If there are only two interfaces in
11341 		 * that group, all we have is a FAILED and INACTIVE
11342 		 * interface. If we do the nomination soon after a failback,
11343 		 * the broadcast nomination code would select the
11344 		 * INACTIVE interface for receiving broadcasts as FAILED is
11345 		 * not yet cleared. As we don't want STANDBY/INACTIVE to
11346 		 * receive broadcast packets, we need to redo nomination
11347 		 * when the FAILED is cleared here. Thus, in general we
11348 		 * always do the nomination here for FAILED, STANDBY
11349 		 * and OFFLINE.
11350 		 */
11351 		if (((turn_on | turn_off) &
11352 		    (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) {
11353 			ip_redo_nomination(phyi);
11354 		}
11355 		if (phyint_flags_modified) {
11356 			if (phyi->phyint_illv4 != NULL) {
11357 				ip_rts_ifmsg(phyi->phyint_illv4->
11358 				    ill_ipif);
11359 			}
11360 			if (phyi->phyint_illv6 != NULL) {
11361 				ip_rts_ifmsg(phyi->phyint_illv6->
11362 				    ill_ipif);
11363 			}
11364 		}
11365 		return (0);
11366 	} else if (set_linklocal || zero_source) {
11367 		mutex_enter(&ill->ill_lock);
11368 		if (set_linklocal)
11369 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
11370 		if (zero_source)
11371 			ipif->ipif_state_flags |= IPIF_ZERO_SOURCE;
11372 		mutex_exit(&ill->ill_lock);
11373 	}
11374 
11375 	/*
11376 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
11377 	 * or point-to-point interfaces with an unspecified destination. We do
11378 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
11379 	 * have a subnet assigned, which is how in.ndpd currently manages its
11380 	 * onlink prefix list when no addresses are configured with those
11381 	 * prefixes.
11382 	 */
11383 	if (ipif->ipif_isv6 &&
11384 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
11385 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
11386 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
11387 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11388 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
11389 		return (EINVAL);
11390 	}
11391 
11392 	/*
11393 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
11394 	 * from being brought up.
11395 	 */
11396 	if (!ipif->ipif_isv6 &&
11397 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
11398 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
11399 		return (EINVAL);
11400 	}
11401 
11402 	/*
11403 	 * The only flag changes that we currently take specific action on
11404 	 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL,
11405 	 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and
11406 	 * IPIF_PREFERRED.  This is done by bring the ipif down, changing
11407 	 * the flags and bringing it back up again.
11408 	 */
11409 	if ((turn_on|turn_off) &
11410 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
11411 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) {
11412 		/*
11413 		 * Taking this ipif down, make sure we have
11414 		 * valid net and subnet bcast ire's for other
11415 		 * logical interfaces, if we need them.
11416 		 */
11417 		if (!ipif->ipif_isv6)
11418 			ipif_check_bcast_ires(ipif);
11419 
11420 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
11421 		    !(turn_off & IPIF_UP)) {
11422 			need_up = B_TRUE;
11423 			if (ipif->ipif_flags & IPIF_UP)
11424 				ill->ill_logical_down = 1;
11425 			turn_on &= ~IPIF_UP;
11426 		}
11427 		err = ipif_down(ipif, q, mp);
11428 		ip1dbg(("ipif_down returns %d err ", err));
11429 		if (err == EINPROGRESS)
11430 			return (err);
11431 		ipif_down_tail(ipif);
11432 	}
11433 	return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up));
11434 }
11435 
11436 static int
11437 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp,
11438     boolean_t need_up)
11439 {
11440 	ill_t	*ill;
11441 	phyint_t *phyi;
11442 	uint64_t turn_on;
11443 	uint64_t turn_off;
11444 	uint64_t intf_flags;
11445 	boolean_t phyint_flags_modified = B_FALSE;
11446 	int	err = 0;
11447 	boolean_t set_linklocal = B_FALSE;
11448 	boolean_t zero_source = B_FALSE;
11449 
11450 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
11451 		ipif->ipif_ill->ill_name, ipif->ipif_id));
11452 
11453 	ASSERT(IAM_WRITER_IPIF(ipif));
11454 
11455 	ill = ipif->ipif_ill;
11456 	phyi = ill->ill_phyint;
11457 
11458 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
11459 	turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP);
11460 
11461 	turn_off = intf_flags & turn_on;
11462 	turn_on ^= turn_off;
11463 
11464 	if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))
11465 		phyint_flags_modified = B_TRUE;
11466 
11467 	/*
11468 	 * Now we change the flags. Track current value of
11469 	 * other flags in their respective places.
11470 	 */
11471 	mutex_enter(&ill->ill_lock);
11472 	mutex_enter(&phyi->phyint_lock);
11473 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
11474 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
11475 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
11476 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
11477 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
11478 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
11479 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
11480 		set_linklocal = B_TRUE;
11481 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
11482 	}
11483 	if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) {
11484 		zero_source = B_TRUE;
11485 		ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE;
11486 	}
11487 	mutex_exit(&ill->ill_lock);
11488 	mutex_exit(&phyi->phyint_lock);
11489 
11490 	if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)))
11491 		ip_redo_nomination(phyi);
11492 
11493 	if (set_linklocal)
11494 		(void) ipif_setlinklocal(ipif);
11495 
11496 	if (zero_source)
11497 		ipif->ipif_v6src_addr = ipv6_all_zeros;
11498 	else
11499 		ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr;
11500 
11501 	if (need_up) {
11502 		/*
11503 		 * XXX ipif_up really does not know whether a phyint flags
11504 		 * was modified or not. So, it sends up information on
11505 		 * only one routing sockets message. As we don't bring up
11506 		 * the interface and also set STANDBY/FAILED simultaneously
11507 		 * it should be okay.
11508 		 */
11509 		err = ipif_up(ipif, q, mp);
11510 	} else {
11511 		/*
11512 		 * Make sure routing socket sees all changes to the flags.
11513 		 * ipif_up_done* handles this when we use ipif_up.
11514 		 */
11515 		if (phyint_flags_modified) {
11516 			if (phyi->phyint_illv4 != NULL) {
11517 				ip_rts_ifmsg(phyi->phyint_illv4->
11518 				    ill_ipif);
11519 			}
11520 			if (phyi->phyint_illv6 != NULL) {
11521 				ip_rts_ifmsg(phyi->phyint_illv6->
11522 				    ill_ipif);
11523 			}
11524 		} else {
11525 			ip_rts_ifmsg(ipif);
11526 		}
11527 	}
11528 	return (err);
11529 }
11530 
11531 /*
11532  * Restart entry point to restart the flags restart operation after the
11533  * refcounts have dropped to zero.
11534  */
11535 /* ARGSUSED */
11536 int
11537 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11538     ip_ioctl_cmd_t *ipip, void *if_req)
11539 {
11540 	int	err;
11541 	struct ifreq *ifr = (struct ifreq *)if_req;
11542 	struct lifreq *lifr = (struct lifreq *)if_req;
11543 
11544 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
11545 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11546 
11547 	ipif_down_tail(ipif);
11548 	if (ipip->ipi_cmd_type == IF_CMD) {
11549 		/*
11550 		 * Since ip_sioctl_flags expects an int and ifr_flags
11551 		 * is a short we need to cast ifr_flags into an int
11552 		 * to avoid having sign extension cause bits to get
11553 		 * set that should not be.
11554 		 */
11555 		err = ip_sioctl_flags_tail(ipif,
11556 		    (uint64_t)(ifr->ifr_flags & 0x0000ffff),
11557 		    q, mp, B_TRUE);
11558 	} else {
11559 		err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags,
11560 		    q, mp, B_TRUE);
11561 	}
11562 	return (err);
11563 }
11564 
11565 /* ARGSUSED */
11566 int
11567 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11568     ip_ioctl_cmd_t *ipip, void *if_req)
11569 {
11570 	/*
11571 	 * Has the flags been set correctly till now ?
11572 	 */
11573 	ill_t *ill = ipif->ipif_ill;
11574 	phyint_t *phyi = ill->ill_phyint;
11575 
11576 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
11577 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11578 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
11579 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
11580 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
11581 
11582 	/*
11583 	 * Need a lock since some flags can be set even when there are
11584 	 * references to the ipif.
11585 	 */
11586 	mutex_enter(&ill->ill_lock);
11587 	if (ipip->ipi_cmd_type == IF_CMD) {
11588 		struct ifreq *ifr = (struct ifreq *)if_req;
11589 
11590 		/* Get interface flags (low 16 only). */
11591 		ifr->ifr_flags = ((ipif->ipif_flags |
11592 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
11593 	} else {
11594 		struct lifreq *lifr = (struct lifreq *)if_req;
11595 
11596 		/* Get interface flags. */
11597 		lifr->lifr_flags = ipif->ipif_flags |
11598 		    ill->ill_flags | phyi->phyint_flags;
11599 	}
11600 	mutex_exit(&ill->ill_lock);
11601 	return (0);
11602 }
11603 
11604 /* ARGSUSED */
11605 int
11606 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11607     ip_ioctl_cmd_t *ipip, void *if_req)
11608 {
11609 	int mtu;
11610 	int ip_min_mtu;
11611 	struct ifreq	*ifr;
11612 	struct lifreq *lifr;
11613 	ire_t	*ire;
11614 
11615 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
11616 	    ipif->ipif_id, (void *)ipif));
11617 	if (ipip->ipi_cmd_type == IF_CMD) {
11618 		ifr = (struct ifreq *)if_req;
11619 		mtu = ifr->ifr_metric;
11620 	} else {
11621 		lifr = (struct lifreq *)if_req;
11622 		mtu = lifr->lifr_mtu;
11623 	}
11624 
11625 	if (ipif->ipif_isv6)
11626 		ip_min_mtu = IPV6_MIN_MTU;
11627 	else
11628 		ip_min_mtu = IP_MIN_MTU;
11629 
11630 	if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu)
11631 		return (EINVAL);
11632 
11633 	/*
11634 	 * Change the MTU size in all relevant ire's.
11635 	 * Mtu change Vs. new ire creation - protocol below.
11636 	 * First change ipif_mtu and the ire_max_frag of the
11637 	 * interface ire. Then do an ire walk and change the
11638 	 * ire_max_frag of all affected ires. During ire_add
11639 	 * under the bucket lock, set the ire_max_frag of the
11640 	 * new ire being created from the ipif/ire from which
11641 	 * it is being derived. If an mtu change happens after
11642 	 * the ire is added, the new ire will be cleaned up.
11643 	 * Conversely if the mtu change happens before the ire
11644 	 * is added, ire_add will see the new value of the mtu.
11645 	 */
11646 	ipif->ipif_mtu = mtu;
11647 	ipif->ipif_flags |= IPIF_FIXEDMTU;
11648 
11649 	if (ipif->ipif_isv6)
11650 		ire = ipif_to_ire_v6(ipif);
11651 	else
11652 		ire = ipif_to_ire(ipif);
11653 	if (ire != NULL) {
11654 		ire->ire_max_frag = ipif->ipif_mtu;
11655 		ire_refrele(ire);
11656 	}
11657 	if (ipif->ipif_flags & IPIF_UP) {
11658 		if (ipif->ipif_isv6)
11659 			ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11660 		else
11661 			ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES);
11662 	}
11663 	/* Update the MTU in SCTP's list */
11664 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
11665 	return (0);
11666 }
11667 
11668 /* Get interface MTU. */
11669 /* ARGSUSED */
11670 int
11671 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11672 	ip_ioctl_cmd_t *ipip, void *if_req)
11673 {
11674 	struct ifreq	*ifr;
11675 	struct lifreq	*lifr;
11676 
11677 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
11678 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11679 	if (ipip->ipi_cmd_type == IF_CMD) {
11680 		ifr = (struct ifreq *)if_req;
11681 		ifr->ifr_metric = ipif->ipif_mtu;
11682 	} else {
11683 		lifr = (struct lifreq *)if_req;
11684 		lifr->lifr_mtu = ipif->ipif_mtu;
11685 	}
11686 	return (0);
11687 }
11688 
11689 /* Set interface broadcast address. */
11690 /* ARGSUSED2 */
11691 int
11692 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11693 	ip_ioctl_cmd_t *ipip, void *if_req)
11694 {
11695 	ipaddr_t addr;
11696 	ire_t	*ire;
11697 
11698 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name,
11699 	    ipif->ipif_id));
11700 
11701 	ASSERT(IAM_WRITER_IPIF(ipif));
11702 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11703 		return (EADDRNOTAVAIL);
11704 
11705 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
11706 
11707 	if (sin->sin_family != AF_INET)
11708 		return (EAFNOSUPPORT);
11709 
11710 	addr = sin->sin_addr.s_addr;
11711 	if (ipif->ipif_flags & IPIF_UP) {
11712 		/*
11713 		 * If we are already up, make sure the new
11714 		 * broadcast address makes sense.  If it does,
11715 		 * there should be an IRE for it already.
11716 		 * Don't match on ipif, only on the ill
11717 		 * since we are sharing these now. Don't use
11718 		 * MATCH_IRE_ILL_GROUP as we are looking for
11719 		 * the broadcast ire on this ill and each ill
11720 		 * in the group has its own broadcast ire.
11721 		 */
11722 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST,
11723 		    ipif, ALL_ZONES, NULL,
11724 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE));
11725 		if (ire == NULL) {
11726 			return (EINVAL);
11727 		} else {
11728 			ire_refrele(ire);
11729 		}
11730 	}
11731 	/*
11732 	 * Changing the broadcast addr for this ipif.
11733 	 * Make sure we have valid net and subnet bcast
11734 	 * ire's for other logical interfaces, if needed.
11735 	 */
11736 	if (addr != ipif->ipif_brd_addr)
11737 		ipif_check_bcast_ires(ipif);
11738 	IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
11739 	return (0);
11740 }
11741 
11742 /* Get interface broadcast address. */
11743 /* ARGSUSED */
11744 int
11745 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11746     ip_ioctl_cmd_t *ipip, void *if_req)
11747 {
11748 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
11749 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11750 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
11751 		return (EADDRNOTAVAIL);
11752 
11753 	/* IPIF_BROADCAST not possible with IPv6 */
11754 	ASSERT(!ipif->ipif_isv6);
11755 	*sin = sin_null;
11756 	sin->sin_family = AF_INET;
11757 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
11758 	return (0);
11759 }
11760 
11761 /*
11762  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
11763  */
11764 /* ARGSUSED */
11765 int
11766 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11767     ip_ioctl_cmd_t *ipip, void *if_req)
11768 {
11769 	int err = 0;
11770 	in6_addr_t v6mask;
11771 
11772 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
11773 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11774 
11775 	ASSERT(IAM_WRITER_IPIF(ipif));
11776 
11777 	if (ipif->ipif_isv6) {
11778 		sin6_t *sin6;
11779 
11780 		if (sin->sin_family != AF_INET6)
11781 			return (EAFNOSUPPORT);
11782 
11783 		sin6 = (sin6_t *)sin;
11784 		v6mask = sin6->sin6_addr;
11785 	} else {
11786 		ipaddr_t mask;
11787 
11788 		if (sin->sin_family != AF_INET)
11789 			return (EAFNOSUPPORT);
11790 
11791 		mask = sin->sin_addr.s_addr;
11792 		V4MASK_TO_V6(mask, v6mask);
11793 	}
11794 
11795 	/*
11796 	 * No big deal if the interface isn't already up, or the mask
11797 	 * isn't really changing, or this is pt-pt.
11798 	 */
11799 	if (!(ipif->ipif_flags & IPIF_UP) ||
11800 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
11801 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
11802 		ipif->ipif_v6net_mask = v6mask;
11803 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11804 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
11805 			    ipif->ipif_v6net_mask,
11806 			    ipif->ipif_v6subnet);
11807 		}
11808 		return (0);
11809 	}
11810 	/*
11811 	 * Make sure we have valid net and subnet broadcast ire's
11812 	 * for the old netmask, if needed by other logical interfaces.
11813 	 */
11814 	if (!ipif->ipif_isv6)
11815 		ipif_check_bcast_ires(ipif);
11816 
11817 	err = ipif_logical_down(ipif, q, mp);
11818 	if (err == EINPROGRESS)
11819 		return (err);
11820 	ipif_down_tail(ipif);
11821 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
11822 	return (err);
11823 }
11824 
11825 static int
11826 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
11827 {
11828 	in6_addr_t v6mask;
11829 	int err = 0;
11830 
11831 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
11832 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11833 
11834 	if (ipif->ipif_isv6) {
11835 		sin6_t *sin6;
11836 
11837 		sin6 = (sin6_t *)sin;
11838 		v6mask = sin6->sin6_addr;
11839 	} else {
11840 		ipaddr_t mask;
11841 
11842 		mask = sin->sin_addr.s_addr;
11843 		V4MASK_TO_V6(mask, v6mask);
11844 	}
11845 
11846 	ipif->ipif_v6net_mask = v6mask;
11847 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11848 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11849 		    ipif->ipif_v6subnet);
11850 	}
11851 	err = ipif_up(ipif, q, mp);
11852 
11853 	if (err == 0 || err == EINPROGRESS) {
11854 		/*
11855 		 * The interface must be DL_BOUND if this packet has to
11856 		 * go out on the wire. Since we only go through a logical
11857 		 * down and are bound with the driver during an internal
11858 		 * down/up that is satisfied.
11859 		 */
11860 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11861 			/* Potentially broadcast an address mask reply. */
11862 			ipif_mask_reply(ipif);
11863 		}
11864 	}
11865 	return (err);
11866 }
11867 
11868 /* ARGSUSED */
11869 int
11870 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11871     ip_ioctl_cmd_t *ipip, void *if_req)
11872 {
11873 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11874 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11875 	ipif_down_tail(ipif);
11876 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11877 }
11878 
11879 /* Get interface net mask. */
11880 /* ARGSUSED */
11881 int
11882 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11883     ip_ioctl_cmd_t *ipip, void *if_req)
11884 {
11885 	struct lifreq *lifr = (struct lifreq *)if_req;
11886 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11887 
11888 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11889 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11890 
11891 	/*
11892 	 * net mask can't change since we have a reference to the ipif.
11893 	 */
11894 	if (ipif->ipif_isv6) {
11895 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11896 		*sin6 = sin6_null;
11897 		sin6->sin6_family = AF_INET6;
11898 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11899 		lifr->lifr_addrlen =
11900 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11901 	} else {
11902 		*sin = sin_null;
11903 		sin->sin_family = AF_INET;
11904 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11905 		if (ipip->ipi_cmd_type == LIF_CMD) {
11906 			lifr->lifr_addrlen =
11907 			    ip_mask_to_plen(ipif->ipif_net_mask);
11908 		}
11909 	}
11910 	return (0);
11911 }
11912 
11913 /* ARGSUSED */
11914 int
11915 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11916     ip_ioctl_cmd_t *ipip, void *if_req)
11917 {
11918 
11919 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11920 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11921 	/*
11922 	 * Set interface metric.  We don't use this for
11923 	 * anything but we keep track of it in case it is
11924 	 * important to routing applications or such.
11925 	 */
11926 	if (ipip->ipi_cmd_type == IF_CMD) {
11927 		struct ifreq    *ifr;
11928 
11929 		ifr = (struct ifreq *)if_req;
11930 		ipif->ipif_metric = ifr->ifr_metric;
11931 	} else {
11932 		struct lifreq   *lifr;
11933 
11934 		lifr = (struct lifreq *)if_req;
11935 		ipif->ipif_metric = lifr->lifr_metric;
11936 	}
11937 	return (0);
11938 }
11939 
11940 
11941 /* ARGSUSED */
11942 int
11943 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11944     ip_ioctl_cmd_t *ipip, void *if_req)
11945 {
11946 
11947 	/* Get interface metric. */
11948 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11949 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11950 	if (ipip->ipi_cmd_type == IF_CMD) {
11951 		struct ifreq    *ifr;
11952 
11953 		ifr = (struct ifreq *)if_req;
11954 		ifr->ifr_metric = ipif->ipif_metric;
11955 	} else {
11956 		struct lifreq   *lifr;
11957 
11958 		lifr = (struct lifreq *)if_req;
11959 		lifr->lifr_metric = ipif->ipif_metric;
11960 	}
11961 
11962 	return (0);
11963 }
11964 
11965 /* ARGSUSED */
11966 int
11967 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11968     ip_ioctl_cmd_t *ipip, void *if_req)
11969 {
11970 
11971 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11972 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11973 	/*
11974 	 * Set the muxid returned from I_PLINK.
11975 	 */
11976 	if (ipip->ipi_cmd_type == IF_CMD) {
11977 		struct ifreq *ifr = (struct ifreq *)if_req;
11978 
11979 		ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid;
11980 		ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid;
11981 	} else {
11982 		struct lifreq *lifr = (struct lifreq *)if_req;
11983 
11984 		ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid;
11985 		ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid;
11986 	}
11987 	return (0);
11988 }
11989 
11990 /* ARGSUSED */
11991 int
11992 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11993     ip_ioctl_cmd_t *ipip, void *if_req)
11994 {
11995 
11996 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11997 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11998 	/*
11999 	 * Get the muxid saved in ill for I_PUNLINK.
12000 	 */
12001 	if (ipip->ipi_cmd_type == IF_CMD) {
12002 		struct ifreq *ifr = (struct ifreq *)if_req;
12003 
12004 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12005 		ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12006 	} else {
12007 		struct lifreq *lifr = (struct lifreq *)if_req;
12008 
12009 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid;
12010 		lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid;
12011 	}
12012 	return (0);
12013 }
12014 
12015 /*
12016  * Set the subnet prefix. Does not modify the broadcast address.
12017  */
12018 /* ARGSUSED */
12019 int
12020 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12021     ip_ioctl_cmd_t *ipip, void *if_req)
12022 {
12023 	int err = 0;
12024 	in6_addr_t v6addr;
12025 	in6_addr_t v6mask;
12026 	boolean_t need_up = B_FALSE;
12027 	int addrlen;
12028 
12029 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
12030 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12031 
12032 	ASSERT(IAM_WRITER_IPIF(ipif));
12033 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
12034 
12035 	if (ipif->ipif_isv6) {
12036 		sin6_t *sin6;
12037 
12038 		if (sin->sin_family != AF_INET6)
12039 			return (EAFNOSUPPORT);
12040 
12041 		sin6 = (sin6_t *)sin;
12042 		v6addr = sin6->sin6_addr;
12043 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
12044 			return (EADDRNOTAVAIL);
12045 	} else {
12046 		ipaddr_t addr;
12047 
12048 		if (sin->sin_family != AF_INET)
12049 			return (EAFNOSUPPORT);
12050 
12051 		addr = sin->sin_addr.s_addr;
12052 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
12053 			return (EADDRNOTAVAIL);
12054 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12055 		/* Add 96 bits */
12056 		addrlen += IPV6_ABITS - IP_ABITS;
12057 	}
12058 
12059 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
12060 		return (EINVAL);
12061 
12062 	/* Check if bits in the address is set past the mask */
12063 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
12064 		return (EINVAL);
12065 
12066 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
12067 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
12068 		return (0);	/* No change */
12069 
12070 	if (ipif->ipif_flags & IPIF_UP) {
12071 		/*
12072 		 * If the interface is already marked up,
12073 		 * we call ipif_down which will take care
12074 		 * of ditching any IREs that have been set
12075 		 * up based on the old interface address.
12076 		 */
12077 		err = ipif_logical_down(ipif, q, mp);
12078 		if (err == EINPROGRESS)
12079 			return (err);
12080 		ipif_down_tail(ipif);
12081 		need_up = B_TRUE;
12082 	}
12083 
12084 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
12085 	return (err);
12086 }
12087 
12088 static int
12089 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
12090     queue_t *q, mblk_t *mp, boolean_t need_up)
12091 {
12092 	ill_t	*ill = ipif->ipif_ill;
12093 	int	err = 0;
12094 
12095 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
12096 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12097 
12098 	/* Set the new address. */
12099 	mutex_enter(&ill->ill_lock);
12100 	ipif->ipif_v6net_mask = v6mask;
12101 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
12102 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
12103 		    ipif->ipif_v6subnet);
12104 	}
12105 	mutex_exit(&ill->ill_lock);
12106 
12107 	if (need_up) {
12108 		/*
12109 		 * Now bring the interface back up.  If this
12110 		 * is the only IPIF for the ILL, ipif_up
12111 		 * will have to re-bind to the device, so
12112 		 * we may get back EINPROGRESS, in which
12113 		 * case, this IOCTL will get completed in
12114 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12115 		 */
12116 		err = ipif_up(ipif, q, mp);
12117 		if (err == EINPROGRESS)
12118 			return (err);
12119 	}
12120 	return (err);
12121 }
12122 
12123 /* ARGSUSED */
12124 int
12125 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12126     ip_ioctl_cmd_t *ipip, void *if_req)
12127 {
12128 	int	addrlen;
12129 	in6_addr_t v6addr;
12130 	in6_addr_t v6mask;
12131 	struct lifreq *lifr = (struct lifreq *)if_req;
12132 
12133 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
12134 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12135 	ipif_down_tail(ipif);
12136 
12137 	addrlen = lifr->lifr_addrlen;
12138 	if (ipif->ipif_isv6) {
12139 		sin6_t *sin6;
12140 
12141 		sin6 = (sin6_t *)sin;
12142 		v6addr = sin6->sin6_addr;
12143 	} else {
12144 		ipaddr_t addr;
12145 
12146 		addr = sin->sin_addr.s_addr;
12147 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
12148 		addrlen += IPV6_ABITS - IP_ABITS;
12149 	}
12150 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
12151 
12152 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
12153 }
12154 
12155 /* ARGSUSED */
12156 int
12157 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12158     ip_ioctl_cmd_t *ipip, void *if_req)
12159 {
12160 	struct lifreq *lifr = (struct lifreq *)if_req;
12161 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
12162 
12163 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
12164 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12165 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
12166 
12167 	if (ipif->ipif_isv6) {
12168 		*sin6 = sin6_null;
12169 		sin6->sin6_family = AF_INET6;
12170 		sin6->sin6_addr = ipif->ipif_v6subnet;
12171 		lifr->lifr_addrlen =
12172 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
12173 	} else {
12174 		*sin = sin_null;
12175 		sin->sin_family = AF_INET;
12176 		sin->sin_addr.s_addr = ipif->ipif_subnet;
12177 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
12178 	}
12179 	return (0);
12180 }
12181 
12182 /*
12183  * Set the IPv6 address token.
12184  */
12185 /* ARGSUSED */
12186 int
12187 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12188     ip_ioctl_cmd_t *ipi, void *if_req)
12189 {
12190 	ill_t *ill = ipif->ipif_ill;
12191 	int err;
12192 	in6_addr_t v6addr;
12193 	in6_addr_t v6mask;
12194 	boolean_t need_up = B_FALSE;
12195 	int i;
12196 	sin6_t *sin6 = (sin6_t *)sin;
12197 	struct lifreq *lifr = (struct lifreq *)if_req;
12198 	int addrlen;
12199 
12200 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
12201 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12202 	ASSERT(IAM_WRITER_IPIF(ipif));
12203 
12204 	addrlen = lifr->lifr_addrlen;
12205 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12206 	if (ipif->ipif_id != 0)
12207 		return (EINVAL);
12208 
12209 	if (!ipif->ipif_isv6)
12210 		return (EINVAL);
12211 
12212 	if (addrlen > IPV6_ABITS)
12213 		return (EINVAL);
12214 
12215 	v6addr = sin6->sin6_addr;
12216 
12217 	/*
12218 	 * The length of the token is the length from the end.  To get
12219 	 * the proper mask for this, compute the mask of the bits not
12220 	 * in the token; ie. the prefix, and then xor to get the mask.
12221 	 */
12222 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
12223 		return (EINVAL);
12224 	for (i = 0; i < 4; i++) {
12225 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12226 	}
12227 
12228 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
12229 	    ill->ill_token_length == addrlen)
12230 		return (0);	/* No change */
12231 
12232 	if (ipif->ipif_flags & IPIF_UP) {
12233 		err = ipif_logical_down(ipif, q, mp);
12234 		if (err == EINPROGRESS)
12235 			return (err);
12236 		ipif_down_tail(ipif);
12237 		need_up = B_TRUE;
12238 	}
12239 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
12240 	return (err);
12241 }
12242 
12243 static int
12244 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
12245     mblk_t *mp, boolean_t need_up)
12246 {
12247 	in6_addr_t v6addr;
12248 	in6_addr_t v6mask;
12249 	ill_t	*ill = ipif->ipif_ill;
12250 	int	i;
12251 	int	err = 0;
12252 
12253 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
12254 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12255 	v6addr = sin6->sin6_addr;
12256 	/*
12257 	 * The length of the token is the length from the end.  To get
12258 	 * the proper mask for this, compute the mask of the bits not
12259 	 * in the token; ie. the prefix, and then xor to get the mask.
12260 	 */
12261 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
12262 	for (i = 0; i < 4; i++)
12263 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
12264 
12265 	mutex_enter(&ill->ill_lock);
12266 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
12267 	ill->ill_token_length = addrlen;
12268 	mutex_exit(&ill->ill_lock);
12269 
12270 	if (need_up) {
12271 		/*
12272 		 * Now bring the interface back up.  If this
12273 		 * is the only IPIF for the ILL, ipif_up
12274 		 * will have to re-bind to the device, so
12275 		 * we may get back EINPROGRESS, in which
12276 		 * case, this IOCTL will get completed in
12277 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
12278 		 */
12279 		err = ipif_up(ipif, q, mp);
12280 		if (err == EINPROGRESS)
12281 			return (err);
12282 	}
12283 	return (err);
12284 }
12285 
12286 /* ARGSUSED */
12287 int
12288 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12289     ip_ioctl_cmd_t *ipi, void *if_req)
12290 {
12291 	ill_t *ill;
12292 	sin6_t *sin6 = (sin6_t *)sin;
12293 	struct lifreq *lifr = (struct lifreq *)if_req;
12294 
12295 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
12296 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12297 	if (ipif->ipif_id != 0)
12298 		return (EINVAL);
12299 
12300 	ill = ipif->ipif_ill;
12301 	if (!ill->ill_isv6)
12302 		return (ENXIO);
12303 
12304 	*sin6 = sin6_null;
12305 	sin6->sin6_family = AF_INET6;
12306 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
12307 	sin6->sin6_addr = ill->ill_token;
12308 	lifr->lifr_addrlen = ill->ill_token_length;
12309 	return (0);
12310 }
12311 
12312 /*
12313  * Set (hardware) link specific information that might override
12314  * what was acquired through the DL_INFO_ACK.
12315  * The logic is as follows.
12316  *
12317  * become exclusive
12318  * set CHANGING flag
12319  * change mtu on affected IREs
12320  * clear CHANGING flag
12321  *
12322  * An ire add that occurs before the CHANGING flag is set will have its mtu
12323  * changed by the ip_sioctl_lnkinfo.
12324  *
12325  * During the time the CHANGING flag is set, no new ires will be added to the
12326  * bucket, and ire add will fail (due the CHANGING flag).
12327  *
12328  * An ire add that occurs after the CHANGING flag is set will have the right mtu
12329  * before it is added to the bucket.
12330  *
12331  * Obviously only 1 thread can set the CHANGING flag and we need to become
12332  * exclusive to set the flag.
12333  */
12334 /* ARGSUSED */
12335 int
12336 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12337     ip_ioctl_cmd_t *ipi, void *if_req)
12338 {
12339 	ill_t		*ill = ipif->ipif_ill;
12340 	ipif_t		*nipif;
12341 	int		ip_min_mtu;
12342 	boolean_t	mtu_walk = B_FALSE;
12343 	struct lifreq	*lifr = (struct lifreq *)if_req;
12344 	lif_ifinfo_req_t *lir;
12345 	ire_t		*ire;
12346 
12347 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
12348 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12349 	lir = &lifr->lifr_ifinfo;
12350 	ASSERT(IAM_WRITER_IPIF(ipif));
12351 
12352 	/* Only allow for logical unit zero i.e. not on "le0:17" */
12353 	if (ipif->ipif_id != 0)
12354 		return (EINVAL);
12355 
12356 	/* Set interface MTU. */
12357 	if (ipif->ipif_isv6)
12358 		ip_min_mtu = IPV6_MIN_MTU;
12359 	else
12360 		ip_min_mtu = IP_MIN_MTU;
12361 
12362 	/*
12363 	 * Verify values before we set anything. Allow zero to
12364 	 * mean unspecified.
12365 	 */
12366 	if (lir->lir_maxmtu != 0 &&
12367 	    (lir->lir_maxmtu > ill->ill_max_frag ||
12368 	    lir->lir_maxmtu < ip_min_mtu))
12369 		return (EINVAL);
12370 	if (lir->lir_reachtime != 0 &&
12371 	    lir->lir_reachtime > ND_MAX_REACHTIME)
12372 		return (EINVAL);
12373 	if (lir->lir_reachretrans != 0 &&
12374 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
12375 		return (EINVAL);
12376 
12377 	mutex_enter(&ill->ill_lock);
12378 	ill->ill_state_flags |= ILL_CHANGING;
12379 	for (nipif = ill->ill_ipif; nipif != NULL;
12380 	    nipif = nipif->ipif_next) {
12381 		nipif->ipif_state_flags |= IPIF_CHANGING;
12382 	}
12383 
12384 	mutex_exit(&ill->ill_lock);
12385 
12386 	if (lir->lir_maxmtu != 0) {
12387 		ill->ill_max_mtu = lir->lir_maxmtu;
12388 		ill->ill_mtu_userspecified = 1;
12389 		mtu_walk = B_TRUE;
12390 	}
12391 
12392 	if (lir->lir_reachtime != 0)
12393 		ill->ill_reachable_time = lir->lir_reachtime;
12394 
12395 	if (lir->lir_reachretrans != 0)
12396 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
12397 
12398 	ill->ill_max_hops = lir->lir_maxhops;
12399 
12400 	ill->ill_max_buf = ND_MAX_Q;
12401 
12402 	if (mtu_walk) {
12403 		/*
12404 		 * Set the MTU on all ipifs associated with this ill except
12405 		 * for those whose MTU was fixed via SIOCSLIFMTU.
12406 		 */
12407 		for (nipif = ill->ill_ipif; nipif != NULL;
12408 		    nipif = nipif->ipif_next) {
12409 			if (nipif->ipif_flags & IPIF_FIXEDMTU)
12410 				continue;
12411 
12412 			nipif->ipif_mtu = ill->ill_max_mtu;
12413 
12414 			if (!(nipif->ipif_flags & IPIF_UP))
12415 				continue;
12416 
12417 			if (nipif->ipif_isv6)
12418 				ire = ipif_to_ire_v6(nipif);
12419 			else
12420 				ire = ipif_to_ire(nipif);
12421 			if (ire != NULL) {
12422 				ire->ire_max_frag = ipif->ipif_mtu;
12423 				ire_refrele(ire);
12424 			}
12425 			if (ill->ill_isv6) {
12426 				ire_walk_ill_v6(MATCH_IRE_ILL, 0,
12427 				    ipif_mtu_change, (char *)nipif,
12428 				    ill);
12429 			} else {
12430 				ire_walk_ill_v4(MATCH_IRE_ILL, 0,
12431 				    ipif_mtu_change, (char *)nipif,
12432 				    ill);
12433 			}
12434 		}
12435 	}
12436 
12437 	mutex_enter(&ill->ill_lock);
12438 	for (nipif = ill->ill_ipif; nipif != NULL;
12439 	    nipif = nipif->ipif_next) {
12440 		nipif->ipif_state_flags &= ~IPIF_CHANGING;
12441 	}
12442 	ILL_UNMARK_CHANGING(ill);
12443 	mutex_exit(&ill->ill_lock);
12444 
12445 	return (0);
12446 }
12447 
12448 /* ARGSUSED */
12449 int
12450 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12451     ip_ioctl_cmd_t *ipi, void *if_req)
12452 {
12453 	struct lif_ifinfo_req *lir;
12454 	ill_t *ill = ipif->ipif_ill;
12455 
12456 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
12457 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
12458 	if (ipif->ipif_id != 0)
12459 		return (EINVAL);
12460 
12461 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
12462 	lir->lir_maxhops = ill->ill_max_hops;
12463 	lir->lir_reachtime = ill->ill_reachable_time;
12464 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
12465 	lir->lir_maxmtu = ill->ill_max_mtu;
12466 
12467 	return (0);
12468 }
12469 
12470 /*
12471  * Return best guess as to the subnet mask for the specified address.
12472  * Based on the subnet masks for all the configured interfaces.
12473  *
12474  * We end up returning a zero mask in the case of default, multicast or
12475  * experimental.
12476  */
12477 static ipaddr_t
12478 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp)
12479 {
12480 	ipaddr_t net_mask;
12481 	ill_t	*ill;
12482 	ipif_t	*ipif;
12483 	ill_walk_context_t ctx;
12484 	ipif_t	*fallback_ipif = NULL;
12485 
12486 	net_mask = ip_net_mask(addr);
12487 	if (net_mask == 0) {
12488 		*ipifp = NULL;
12489 		return (0);
12490 	}
12491 
12492 	/* Let's check to see if this is maybe a local subnet route. */
12493 	/* this function only applies to IPv4 interfaces */
12494 	rw_enter(&ill_g_lock, RW_READER);
12495 	ill = ILL_START_WALK_V4(&ctx);
12496 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
12497 		mutex_enter(&ill->ill_lock);
12498 		for (ipif = ill->ill_ipif; ipif != NULL;
12499 		    ipif = ipif->ipif_next) {
12500 			if (!IPIF_CAN_LOOKUP(ipif))
12501 				continue;
12502 			if (!(ipif->ipif_flags & IPIF_UP))
12503 				continue;
12504 			if ((ipif->ipif_subnet & net_mask) ==
12505 			    (addr & net_mask)) {
12506 				/*
12507 				 * Don't trust pt-pt interfaces if there are
12508 				 * other interfaces.
12509 				 */
12510 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
12511 					if (fallback_ipif == NULL) {
12512 						ipif_refhold_locked(ipif);
12513 						fallback_ipif = ipif;
12514 					}
12515 					continue;
12516 				}
12517 
12518 				/*
12519 				 * Fine. Just assume the same net mask as the
12520 				 * directly attached subnet interface is using.
12521 				 */
12522 				ipif_refhold_locked(ipif);
12523 				mutex_exit(&ill->ill_lock);
12524 				rw_exit(&ill_g_lock);
12525 				if (fallback_ipif != NULL)
12526 					ipif_refrele(fallback_ipif);
12527 				*ipifp = ipif;
12528 				return (ipif->ipif_net_mask);
12529 			}
12530 		}
12531 		mutex_exit(&ill->ill_lock);
12532 	}
12533 	rw_exit(&ill_g_lock);
12534 
12535 	*ipifp = fallback_ipif;
12536 	return ((fallback_ipif != NULL) ?
12537 	    fallback_ipif->ipif_net_mask : net_mask);
12538 }
12539 
12540 /*
12541  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
12542  */
12543 static void
12544 ip_wput_ioctl(queue_t *q, mblk_t *mp)
12545 {
12546 	IOCP	iocp;
12547 	ipft_t	*ipft;
12548 	ipllc_t	*ipllc;
12549 	mblk_t	*mp1;
12550 	cred_t	*cr;
12551 	int	error = 0;
12552 	conn_t	*connp;
12553 
12554 	ip1dbg(("ip_wput_ioctl"));
12555 	iocp = (IOCP)mp->b_rptr;
12556 	mp1 = mp->b_cont;
12557 	if (mp1 == NULL) {
12558 		iocp->ioc_error = EINVAL;
12559 		mp->b_datap->db_type = M_IOCNAK;
12560 		iocp->ioc_count = 0;
12561 		qreply(q, mp);
12562 		return;
12563 	}
12564 
12565 	/*
12566 	 * These IOCTLs provide various control capabilities to
12567 	 * upstream agents such as ULPs and processes.	There
12568 	 * are currently two such IOCTLs implemented.  They
12569 	 * are used by TCP to provide update information for
12570 	 * existing IREs and to forcibly delete an IRE for a
12571 	 * host that is not responding, thereby forcing an
12572 	 * attempt at a new route.
12573 	 */
12574 	iocp->ioc_error = EINVAL;
12575 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
12576 		goto done;
12577 
12578 	ipllc = (ipllc_t *)mp1->b_rptr;
12579 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
12580 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
12581 			break;
12582 	}
12583 	/*
12584 	 * prefer credential from mblk over ioctl;
12585 	 * see ip_sioctl_copyin_setup
12586 	 */
12587 	cr = DB_CREDDEF(mp, iocp->ioc_cr);
12588 
12589 	/*
12590 	 * Refhold the conn in case the request gets queued up in some lookup
12591 	 */
12592 	ASSERT(CONN_Q(q));
12593 	connp = Q_TO_CONN(q);
12594 	CONN_INC_REF(connp);
12595 	if (ipft->ipft_pfi &&
12596 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
12597 		pullupmsg(mp1, ipft->ipft_min_size))) {
12598 		error = (*ipft->ipft_pfi)(q,
12599 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
12600 	}
12601 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
12602 		/*
12603 		 * CONN_OPER_PENDING_DONE happens in the function called
12604 		 * through ipft_pfi above.
12605 		 */
12606 		return;
12607 	}
12608 
12609 	CONN_OPER_PENDING_DONE(connp);
12610 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
12611 		freemsg(mp);
12612 		return;
12613 	}
12614 	iocp->ioc_error = error;
12615 
12616 done:
12617 	mp->b_datap->db_type = M_IOCACK;
12618 	if (iocp->ioc_error)
12619 		iocp->ioc_count = 0;
12620 	qreply(q, mp);
12621 }
12622 
12623 /*
12624  * Lookup an ipif using the sequence id (ipif_seqid)
12625  */
12626 ipif_t *
12627 ipif_lookup_seqid(ill_t *ill, uint_t seqid)
12628 {
12629 	ipif_t *ipif;
12630 
12631 	ASSERT(MUTEX_HELD(&ill->ill_lock));
12632 
12633 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12634 		if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif))
12635 			return (ipif);
12636 	}
12637 	return (NULL);
12638 }
12639 
12640 uint64_t ipif_g_seqid;
12641 
12642 /*
12643  * Assign a unique id for the ipif. This is used later when we send
12644  * IRES to ARP for resolution where we initialize ire_ipif_seqid
12645  * to the value pointed by ire_ipif->ipif_seqid. Later when the
12646  * IRE is added, we verify that ipif has not disappeared.
12647  */
12648 
12649 static void
12650 ipif_assign_seqid(ipif_t *ipif)
12651 {
12652 	ipif->ipif_seqid = atomic_add_64_nv(&ipif_g_seqid, 1);
12653 }
12654 
12655 /*
12656  * Insert the ipif, so that the list of ipifs on the ill will be sorted
12657  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
12658  * be inserted into the first space available in the list. The value of
12659  * ipif_id will then be set to the appropriate value for its position.
12660  */
12661 static int
12662 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock)
12663 {
12664 	ill_t *ill;
12665 	ipif_t *tipif;
12666 	ipif_t **tipifp;
12667 	int id;
12668 
12669 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
12670 	    IAM_WRITER_IPIF(ipif));
12671 
12672 	ill = ipif->ipif_ill;
12673 	ASSERT(ill != NULL);
12674 
12675 	/*
12676 	 * In the case of lo0:0 we already hold the ill_g_lock.
12677 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
12678 	 * ipif_insert. Another such caller is ipif_move.
12679 	 */
12680 	if (acquire_g_lock)
12681 		rw_enter(&ill_g_lock, RW_WRITER);
12682 	if (acquire_ill_lock)
12683 		mutex_enter(&ill->ill_lock);
12684 	id = ipif->ipif_id;
12685 	tipifp = &(ill->ill_ipif);
12686 	if (id == -1) {	/* need to find a real id */
12687 		id = 0;
12688 		while ((tipif = *tipifp) != NULL) {
12689 			ASSERT(tipif->ipif_id >= id);
12690 			if (tipif->ipif_id != id)
12691 				break; /* non-consecutive id */
12692 			id++;
12693 			tipifp = &(tipif->ipif_next);
12694 		}
12695 		/* limit number of logical interfaces */
12696 		if (id >= ip_addrs_per_if) {
12697 			if (acquire_ill_lock)
12698 				mutex_exit(&ill->ill_lock);
12699 			if (acquire_g_lock)
12700 				rw_exit(&ill_g_lock);
12701 			return (-1);
12702 		}
12703 		ipif->ipif_id = id; /* assign new id */
12704 	} else if (id < ip_addrs_per_if) {
12705 		/* we have a real id; insert ipif in the right place */
12706 		while ((tipif = *tipifp) != NULL) {
12707 			ASSERT(tipif->ipif_id != id);
12708 			if (tipif->ipif_id > id)
12709 				break; /* found correct location */
12710 			tipifp = &(tipif->ipif_next);
12711 		}
12712 	} else {
12713 		if (acquire_ill_lock)
12714 			mutex_exit(&ill->ill_lock);
12715 		if (acquire_g_lock)
12716 			rw_exit(&ill_g_lock);
12717 		return (-1);
12718 	}
12719 
12720 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
12721 
12722 	ipif->ipif_next = tipif;
12723 	*tipifp = ipif;
12724 	if (acquire_ill_lock)
12725 		mutex_exit(&ill->ill_lock);
12726 	if (acquire_g_lock)
12727 		rw_exit(&ill_g_lock);
12728 	return (0);
12729 }
12730 
12731 /*
12732  * Allocate and initialize a new interface control structure.  (Always
12733  * called as writer.)
12734  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
12735  * is not part of the global linked list of ills. ipif_seqid is unique
12736  * in the system and to preserve the uniqueness, it is assigned only
12737  * when ill becomes part of the global list. At that point ill will
12738  * have a name. If it doesn't get assigned here, it will get assigned
12739  * in ipif_set_values() as part of SIOCSLIFNAME processing.
12740  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
12741  * the interface flags or any other information from the DL_INFO_ACK for
12742  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
12743  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
12744  * second DL_INFO_ACK comes in from the driver.
12745  */
12746 static ipif_t *
12747 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize)
12748 {
12749 	ipif_t	*ipif;
12750 	phyint_t *phyi;
12751 
12752 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
12753 	    ill->ill_name, id, (void *)ill));
12754 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
12755 
12756 	if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL)
12757 		return (NULL);
12758 	*ipif = ipif_zero;	/* start clean */
12759 
12760 	ipif->ipif_ill = ill;
12761 	ipif->ipif_id = id;	/* could be -1 */
12762 	ipif->ipif_zoneid = GLOBAL_ZONEID;
12763 
12764 	mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
12765 
12766 	ipif->ipif_refcnt = 0;
12767 	ipif->ipif_saved_ire_cnt = 0;
12768 
12769 	if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) {
12770 		mi_free(ipif);
12771 		return (NULL);
12772 	}
12773 	/* -1 id should have been replaced by real id */
12774 	id = ipif->ipif_id;
12775 	ASSERT(id >= 0);
12776 
12777 	if (ill->ill_name[0] != '\0') {
12778 		ipif_assign_seqid(ipif);
12779 		if (ill->ill_phyint->phyint_ifindex != 0)
12780 			sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
12781 	}
12782 	/*
12783 	 * Keep a copy of original id in ipif_orig_ipifid.  Failback
12784 	 * will attempt to restore the original id.  The SIOCSLIFOINDEX
12785 	 * ioctl sets ipif_orig_ipifid to zero.
12786 	 */
12787 	ipif->ipif_orig_ipifid = id;
12788 
12789 	/*
12790 	 * We grab the ill_lock and phyint_lock to protect the flag changes.
12791 	 * The ipif is still not up and can't be looked up until the
12792 	 * ioctl completes and the IPIF_CHANGING flag is cleared.
12793 	 */
12794 	mutex_enter(&ill->ill_lock);
12795 	mutex_enter(&ill->ill_phyint->phyint_lock);
12796 	/*
12797 	 * Set the running flag when logical interface zero is created.
12798 	 * For subsequent logical interfaces, a DLPI link down
12799 	 * notification message may have cleared the running flag to
12800 	 * indicate the link is down, so we shouldn't just blindly set it.
12801 	 */
12802 	if (id == 0)
12803 		ill->ill_phyint->phyint_flags |= PHYI_RUNNING;
12804 	ipif->ipif_ire_type = ire_type;
12805 	phyi = ill->ill_phyint;
12806 	ipif->ipif_orig_ifindex = phyi->phyint_ifindex;
12807 
12808 	if (ipif->ipif_isv6) {
12809 		ill->ill_flags |= ILLF_IPV6;
12810 	} else {
12811 		ipaddr_t inaddr_any = INADDR_ANY;
12812 
12813 		ill->ill_flags |= ILLF_IPV4;
12814 
12815 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12816 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12817 		    &ipif->ipif_v6lcl_addr);
12818 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12819 		    &ipif->ipif_v6src_addr);
12820 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12821 		    &ipif->ipif_v6subnet);
12822 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12823 		    &ipif->ipif_v6net_mask);
12824 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12825 		    &ipif->ipif_v6brd_addr);
12826 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12827 		    &ipif->ipif_v6pp_dst_addr);
12828 	}
12829 
12830 	/*
12831 	 * Don't set the interface flags etc. now, will do it in
12832 	 * ip_ll_subnet_defaults.
12833 	 */
12834 	if (!initialize) {
12835 		mutex_exit(&ill->ill_lock);
12836 		mutex_exit(&ill->ill_phyint->phyint_lock);
12837 		return (ipif);
12838 	}
12839 	ipif->ipif_mtu = ill->ill_max_mtu;
12840 
12841 	if (ill->ill_bcast_addr_length != 0) {
12842 		/*
12843 		 * Later detect lack of DLPI driver multicast
12844 		 * capability by catching DL_ENABMULTI errors in
12845 		 * ip_rput_dlpi.
12846 		 */
12847 		ill->ill_flags |= ILLF_MULTICAST;
12848 		if (!ipif->ipif_isv6)
12849 			ipif->ipif_flags |= IPIF_BROADCAST;
12850 	} else {
12851 		if (ill->ill_net_type != IRE_LOOPBACK) {
12852 			if (ipif->ipif_isv6)
12853 				/*
12854 				 * Note: xresolv interfaces will eventually need
12855 				 * NOARP set here as well, but that will require
12856 				 * those external resolvers to have some
12857 				 * knowledge of that flag and act appropriately.
12858 				 * Not to be changed at present.
12859 				 */
12860 				ill->ill_flags |= ILLF_NONUD;
12861 			else
12862 				ill->ill_flags |= ILLF_NOARP;
12863 		}
12864 		if (ill->ill_phys_addr_length == 0) {
12865 			if (ill->ill_media &&
12866 			    ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) {
12867 				ipif->ipif_flags |= IPIF_NOXMIT;
12868 				phyi->phyint_flags |= PHYI_VIRTUAL;
12869 			} else {
12870 				/* pt-pt supports multicast. */
12871 				ill->ill_flags |= ILLF_MULTICAST;
12872 				if (ill->ill_net_type == IRE_LOOPBACK) {
12873 					phyi->phyint_flags |=
12874 					    (PHYI_LOOPBACK | PHYI_VIRTUAL);
12875 				} else {
12876 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12877 				}
12878 			}
12879 		}
12880 	}
12881 	mutex_exit(&ill->ill_lock);
12882 	mutex_exit(&ill->ill_phyint->phyint_lock);
12883 	return (ipif);
12884 }
12885 
12886 /*
12887  * If appropriate, send a message up to the resolver delete the entry
12888  * for the address of this interface which is going out of business.
12889  * (Always called as writer).
12890  *
12891  * NOTE : We need to check for NULL mps as some of the fields are
12892  *	  initialized only for some interface types. See ipif_resolver_up()
12893  *	  for details.
12894  */
12895 void
12896 ipif_arp_down(ipif_t *ipif)
12897 {
12898 	mblk_t	*mp;
12899 
12900 	ip1dbg(("ipif_arp_down(%s:%u)\n",
12901 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
12902 	ASSERT(IAM_WRITER_IPIF(ipif));
12903 
12904 	/* Delete the mapping for the local address */
12905 	mp = ipif->ipif_arp_del_mp;
12906 	if (mp != NULL) {
12907 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12908 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12909 		    ipif->ipif_ill->ill_name, ipif->ipif_id));
12910 		putnext(ipif->ipif_ill->ill_rq, mp);
12911 		ipif->ipif_arp_del_mp = NULL;
12912 	}
12913 
12914 	/*
12915 	 * If this is the last ipif that is going down, we need
12916 	 * to clean up ARP completely.
12917 	 */
12918 	if (ipif->ipif_ill->ill_ipif_up_count == 0) {
12919 
12920 		/* Send up AR_INTERFACE_DOWN message */
12921 		mp = ipif->ipif_ill->ill_arp_down_mp;
12922 		if (mp != NULL) {
12923 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12924 			    dlpi_prim_str(*(int *)mp->b_rptr),
12925 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12926 			    ipif->ipif_id));
12927 			putnext(ipif->ipif_ill->ill_rq, mp);
12928 			ipif->ipif_ill->ill_arp_down_mp = NULL;
12929 		}
12930 
12931 		/* Tell ARP to delete the multicast mappings */
12932 		mp = ipif->ipif_ill->ill_arp_del_mapping_mp;
12933 		if (mp != NULL) {
12934 			ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12935 			    dlpi_prim_str(*(int *)mp->b_rptr),
12936 			    *(int *)mp->b_rptr, ipif->ipif_ill->ill_name,
12937 			    ipif->ipif_id));
12938 			putnext(ipif->ipif_ill->ill_rq, mp);
12939 			ipif->ipif_ill->ill_arp_del_mapping_mp = NULL;
12940 		}
12941 	}
12942 }
12943 
12944 /*
12945  * This function sets up the multicast mappings in ARP. When ipif_resolver_up
12946  * calls this function, it passes a non-NULL arp_add_mapping_mp indicating
12947  * that it wants the add_mp allocated in this function to be returned
12948  * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to
12949  * just re-do the multicast, it wants us to send the add_mp to ARP also.
12950  * ipif_resolver_up does not want us to do the "add" i.e sending to ARP,
12951  * as it does a ipif_arp_down after calling this function - which will
12952  * remove what we add here.
12953  *
12954  * Returns -1 on failures and 0 on success.
12955  */
12956 int
12957 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp)
12958 {
12959 	mblk_t	*del_mp = NULL;
12960 	mblk_t *add_mp = NULL;
12961 	mblk_t *mp;
12962 	ill_t	*ill = ipif->ipif_ill;
12963 	phyint_t *phyi = ill->ill_phyint;
12964 	ipaddr_t addr, mask, extract_mask = 0;
12965 	arma_t	*arma;
12966 	uint8_t *maddr, *bphys_addr;
12967 	uint32_t hw_start;
12968 	dl_unitdata_req_t *dlur;
12969 
12970 	ASSERT(IAM_WRITER_IPIF(ipif));
12971 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
12972 		return (0);
12973 
12974 	/*
12975 	 * Delete the existing mapping from ARP. Normally ipif_down
12976 	 * -> ipif_arp_down should send this up to ARP. The only
12977 	 * reason we would find this when we are switching from
12978 	 * Multicast to Broadcast where we did not do a down.
12979 	 */
12980 	mp = ill->ill_arp_del_mapping_mp;
12981 	if (mp != NULL) {
12982 		ip1dbg(("ipif_arp_down: %s (%u) for %s:%u\n",
12983 		    dlpi_prim_str(*(int *)mp->b_rptr),
12984 		    *(int *)mp->b_rptr, ill->ill_name, ipif->ipif_id));
12985 		putnext(ill->ill_rq, mp);
12986 		ill->ill_arp_del_mapping_mp = NULL;
12987 	}
12988 
12989 	if (arp_add_mapping_mp != NULL)
12990 		*arp_add_mapping_mp = NULL;
12991 
12992 	/*
12993 	 * Check that the address is not to long for the constant
12994 	 * length reserved in the template arma_t.
12995 	 */
12996 	if (ill->ill_phys_addr_length > IP_MAX_HW_LEN)
12997 		return (-1);
12998 
12999 	/* Add mapping mblk */
13000 	addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP);
13001 	mask = (ipaddr_t)htonl(IN_CLASSD_NET);
13002 	add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template,
13003 	    (caddr_t)&addr);
13004 	if (add_mp == NULL)
13005 		return (-1);
13006 	arma = (arma_t *)add_mp->b_rptr;
13007 	maddr = (uint8_t *)arma + arma->arma_hw_addr_offset;
13008 	bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN);
13009 	arma->arma_hw_addr_length = ill->ill_phys_addr_length;
13010 
13011 	/*
13012 	 * Determine the broadcast address.
13013 	 */
13014 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
13015 	if (ill->ill_sap_length < 0)
13016 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
13017 	else
13018 		bphys_addr = (uchar_t *)dlur +
13019 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
13020 	/*
13021 	 * Check PHYI_MULTI_BCAST and length of physical
13022 	 * address to determine if we use the mapping or the
13023 	 * broadcast address.
13024 	 */
13025 	if (!(phyi->phyint_flags & PHYI_MULTI_BCAST))
13026 		if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length,
13027 		    bphys_addr, maddr, &hw_start, &extract_mask))
13028 			phyi->phyint_flags |= PHYI_MULTI_BCAST;
13029 
13030 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) ||
13031 	    (ill->ill_flags & ILLF_MULTICAST)) {
13032 		/* Make sure this will not match the "exact" entry. */
13033 		addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP);
13034 		del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template,
13035 		    (caddr_t)&addr);
13036 		if (del_mp == NULL) {
13037 			freemsg(add_mp);
13038 			return (-1);
13039 		}
13040 		bcopy(&extract_mask, (char *)arma +
13041 		    arma->arma_proto_extract_mask_offset, IP_ADDR_LEN);
13042 		if (phyi->phyint_flags & PHYI_MULTI_BCAST) {
13043 			/* Use link-layer broadcast address for MULTI_BCAST */
13044 			bcopy(bphys_addr, maddr, ill->ill_phys_addr_length);
13045 			ip2dbg(("ipif_arp_setup_multicast: adding"
13046 			    " MULTI_BCAST ARP setup for %s\n", ill->ill_name));
13047 		} else {
13048 			arma->arma_hw_mapping_start = hw_start;
13049 			ip2dbg(("ipif_arp_setup_multicast: adding multicast"
13050 			    " ARP setup for %s\n", ill->ill_name));
13051 		}
13052 	} else {
13053 		freemsg(add_mp);
13054 		ASSERT(del_mp == NULL);
13055 		/* It is neither MULTICAST nor MULTI_BCAST */
13056 		return (0);
13057 	}
13058 	ASSERT(add_mp != NULL && del_mp != NULL);
13059 	ill->ill_arp_del_mapping_mp = del_mp;
13060 	if (arp_add_mapping_mp != NULL) {
13061 		/* The caller just wants the mblks allocated */
13062 		*arp_add_mapping_mp = add_mp;
13063 	} else {
13064 		/* The caller wants us to send it to arp */
13065 		putnext(ill->ill_rq, add_mp);
13066 	}
13067 	return (0);
13068 }
13069 
13070 /*
13071  * Get the resolver set up for a new interface address.
13072  * (Always called as writer.)
13073  * Called both for IPv4 and IPv6 interfaces,
13074  * though it only sets up the resolver for v6
13075  * if it's an xresolv interface (one using an external resolver).
13076  * Honors ILLF_NOARP.
13077  * The boolean value arp_just_publish, if B_TRUE, indicates that
13078  * it only needs to send an AR_ENTRY_ADD message up to ARP for
13079  * IPv4 interfaces. Currently, B_TRUE is only set when this
13080  * function is called by ip_rput_dlpi_writer() to handle
13081  * asynchronous hardware address change notification.
13082  * Returns error on failure.
13083  */
13084 int
13085 ipif_resolver_up(ipif_t *ipif, boolean_t arp_just_publish)
13086 {
13087 	caddr_t	addr;
13088 	mblk_t	*arp_up_mp = NULL;
13089 	mblk_t	*arp_down_mp = NULL;
13090 	mblk_t	*arp_add_mp = NULL;
13091 	mblk_t	*arp_del_mp = NULL;
13092 	mblk_t	*arp_add_mapping_mp = NULL;
13093 	mblk_t	*arp_del_mapping_mp = NULL;
13094 	ill_t	*ill = ipif->ipif_ill;
13095 	uchar_t	*area_p = NULL;
13096 	uchar_t	*ared_p = NULL;
13097 	int	err = ENOMEM;
13098 
13099 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
13100 	    ipif->ipif_ill->ill_name, ipif->ipif_id,
13101 	    (uint_t)ipif->ipif_flags));
13102 	ASSERT(IAM_WRITER_IPIF(ipif));
13103 
13104 	if ((ill->ill_net_type != IRE_IF_RESOLVER) ||
13105 	    (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV))) {
13106 		return (0);
13107 	}
13108 
13109 	if (ill->ill_isv6) {
13110 		/*
13111 		 * External resolver for IPv6
13112 		 */
13113 		ASSERT(!arp_just_publish);
13114 		if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
13115 			addr = (caddr_t)&ipif->ipif_v6lcl_addr;
13116 			area_p = (uchar_t *)&ip6_area_template;
13117 			ared_p = (uchar_t *)&ip6_ared_template;
13118 		}
13119 	} else {
13120 		/*
13121 		 * IPv4 arp case. If the ARP stream has already started
13122 		 * closing, fail this request for ARP bringup. Else
13123 		 * record the fact that an ARP bringup is pending.
13124 		 */
13125 		mutex_enter(&ill->ill_lock);
13126 		if (ill->ill_arp_closing) {
13127 			mutex_exit(&ill->ill_lock);
13128 			err = EINVAL;
13129 			goto failed;
13130 		} else {
13131 			if (ill->ill_ipif_up_count == 0)
13132 				ill->ill_arp_bringup_pending = 1;
13133 			mutex_exit(&ill->ill_lock);
13134 		}
13135 		if (ipif->ipif_lcl_addr != INADDR_ANY) {
13136 			addr = (caddr_t)&ipif->ipif_lcl_addr;
13137 			area_p = (uchar_t *)&ip_area_template;
13138 			ared_p = (uchar_t *)&ip_ared_template;
13139 		}
13140 	}
13141 
13142 	/*
13143 	 * Add an entry for the local address in ARP only if it
13144 	 * is not UNNUMBERED and the address is not INADDR_ANY.
13145 	 */
13146 	if (((ipif->ipif_flags & IPIF_UNNUMBERED) == 0) && area_p != NULL) {
13147 		/* Now ask ARP to publish our address. */
13148 		arp_add_mp = ill_arp_alloc(ill, area_p, addr);
13149 		if (arp_add_mp == NULL)
13150 			goto failed;
13151 		if (arp_just_publish) {
13152 			/*
13153 			 * Copy the new hardware address and length into
13154 			 * arp_add_mp to be sent to ARP.
13155 			 */
13156 			area_t *area = (area_t *)arp_add_mp->b_rptr;
13157 			area->area_hw_addr_length =
13158 			    ill->ill_phys_addr_length;
13159 			bcopy((char *)ill->ill_phys_addr,
13160 			    ((char *)area + area->area_hw_addr_offset),
13161 			    area->area_hw_addr_length);
13162 		}
13163 
13164 		((area_t *)arp_add_mp->b_rptr)->area_flags =
13165 		    ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR;
13166 
13167 		if (arp_just_publish)
13168 			goto arp_setup_multicast;
13169 
13170 		/*
13171 		 * Allocate an ARP deletion message so we know we can tell ARP
13172 		 * when the interface goes down.
13173 		 */
13174 		arp_del_mp = ill_arp_alloc(ill, ared_p, addr);
13175 		if (arp_del_mp == NULL)
13176 			goto failed;
13177 
13178 	} else {
13179 		if (arp_just_publish)
13180 			goto done;
13181 	}
13182 	/*
13183 	 * Need to bring up ARP or setup multicast mapping only
13184 	 * when the first interface is coming UP.
13185 	 */
13186 	if (ill->ill_ipif_up_count != 0)
13187 		goto done;
13188 
13189 	/*
13190 	 * Allocate an ARP down message (to be saved) and an ARP up
13191 	 * message.
13192 	 */
13193 	arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0);
13194 	if (arp_down_mp == NULL)
13195 		goto failed;
13196 
13197 	arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0);
13198 	if (arp_up_mp == NULL)
13199 		goto failed;
13200 
13201 	if (ipif->ipif_flags & IPIF_POINTOPOINT)
13202 		goto done;
13203 
13204 arp_setup_multicast:
13205 	/*
13206 	 * Setup the multicast mappings. This function initializes
13207 	 * ill_arp_del_mapping_mp also. This does not need to be done for
13208 	 * IPv6.
13209 	 */
13210 	if (!ill->ill_isv6) {
13211 		err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp);
13212 		if (err != 0)
13213 			goto failed;
13214 		ASSERT(ill->ill_arp_del_mapping_mp != NULL);
13215 		ASSERT(arp_add_mapping_mp != NULL);
13216 	}
13217 
13218 done:;
13219 	if (arp_del_mp != NULL) {
13220 		ASSERT(ipif->ipif_arp_del_mp == NULL);
13221 		ipif->ipif_arp_del_mp = arp_del_mp;
13222 	}
13223 	if (arp_down_mp != NULL) {
13224 		ASSERT(ill->ill_arp_down_mp == NULL);
13225 		ill->ill_arp_down_mp = arp_down_mp;
13226 	}
13227 	if (arp_del_mapping_mp != NULL) {
13228 		ASSERT(ill->ill_arp_del_mapping_mp == NULL);
13229 		ill->ill_arp_del_mapping_mp = arp_del_mapping_mp;
13230 	}
13231 	if (arp_up_mp != NULL) {
13232 		ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n",
13233 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13234 		putnext(ill->ill_rq, arp_up_mp);
13235 	}
13236 	if (arp_add_mp != NULL) {
13237 		ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n",
13238 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13239 		putnext(ill->ill_rq, arp_add_mp);
13240 	}
13241 	if (arp_add_mapping_mp != NULL) {
13242 		ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n",
13243 			ipif->ipif_ill->ill_name, ipif->ipif_id));
13244 		putnext(ill->ill_rq, arp_add_mapping_mp);
13245 	}
13246 	if (arp_just_publish)
13247 		return (0);
13248 
13249 	if (ill->ill_flags & ILLF_NOARP)
13250 		err = ill_arp_off(ill);
13251 	else
13252 		err = ill_arp_on(ill);
13253 	if (err) {
13254 		ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err));
13255 		freemsg(ipif->ipif_arp_del_mp);
13256 		if (arp_down_mp != NULL)
13257 			freemsg(ill->ill_arp_down_mp);
13258 		if (ill->ill_arp_del_mapping_mp != NULL)
13259 			freemsg(ill->ill_arp_del_mapping_mp);
13260 		ipif->ipif_arp_del_mp = NULL;
13261 		ill->ill_arp_down_mp = NULL;
13262 		ill->ill_arp_del_mapping_mp = NULL;
13263 		return (err);
13264 	}
13265 	return (ill->ill_ipif_up_count != 0 ? 0 : EINPROGRESS);
13266 
13267 failed:;
13268 	ip1dbg(("ipif_resolver_up: FAILED\n"));
13269 	freemsg(arp_add_mp);
13270 	freemsg(arp_del_mp);
13271 	freemsg(arp_add_mapping_mp);
13272 	freemsg(arp_up_mp);
13273 	freemsg(arp_down_mp);
13274 	ill->ill_arp_bringup_pending = 0;
13275 	return (err);
13276 }
13277 
13278 /*
13279  * Wakeup all threads waiting to enter the ipsq, and sleeping
13280  * on any of the ills in this ipsq. The ill_lock of the ill
13281  * must be held so that waiters don't miss wakeups
13282  */
13283 static void
13284 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock)
13285 {
13286 	phyint_t *phyint;
13287 
13288 	phyint = ipsq->ipsq_phyint_list;
13289 	while (phyint != NULL) {
13290 		if (phyint->phyint_illv4) {
13291 			if (!caller_holds_lock)
13292 				mutex_enter(&phyint->phyint_illv4->ill_lock);
13293 			ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13294 			cv_broadcast(&phyint->phyint_illv4->ill_cv);
13295 			if (!caller_holds_lock)
13296 				mutex_exit(&phyint->phyint_illv4->ill_lock);
13297 		}
13298 		if (phyint->phyint_illv6) {
13299 			if (!caller_holds_lock)
13300 				mutex_enter(&phyint->phyint_illv6->ill_lock);
13301 			ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13302 			cv_broadcast(&phyint->phyint_illv6->ill_cv);
13303 			if (!caller_holds_lock)
13304 				mutex_exit(&phyint->phyint_illv6->ill_lock);
13305 		}
13306 		phyint = phyint->phyint_ipsq_next;
13307 	}
13308 }
13309 
13310 static ipsq_t *
13311 ipsq_create(char *groupname)
13312 {
13313 	ipsq_t	*ipsq;
13314 
13315 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13316 	ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP);
13317 	if (ipsq == NULL) {
13318 		return (NULL);
13319 	}
13320 
13321 	if (groupname != NULL)
13322 		(void) strcpy(ipsq->ipsq_name, groupname);
13323 	else
13324 		ipsq->ipsq_name[0] = '\0';
13325 
13326 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL);
13327 	ipsq->ipsq_flags |= IPSQ_GROUP;
13328 	ipsq->ipsq_next = ipsq_g_head;
13329 	ipsq_g_head = ipsq;
13330 	return (ipsq);
13331 }
13332 
13333 /*
13334  * Return an ipsq correspoding to the groupname. If 'create' is true
13335  * allocate a new ipsq if one does not exist. Usually an ipsq is associated
13336  * uniquely with an IPMP group. However during IPMP groupname operations,
13337  * multiple IPMP groups may be associated with a single ipsq. But no
13338  * IPMP group can be associated with more than 1 ipsq at any time.
13339  * For example
13340  *	Interfaces		IPMP grpname	ipsq	ipsq_name      ipsq_refs
13341  * 	hme1, hme2		mpk17-84	ipsq1	mpk17-84	2
13342  *	hme3, hme4		mpk17-85	ipsq2	mpk17-85	2
13343  *
13344  * Now the command ifconfig hme3 group mpk17-84 results in the temporary
13345  * status shown below during the execution of the above command.
13346  * 	hme1, hme2, hme3, hme4	mpk17-84, mpk17-85	ipsq1	mpk17-84  4
13347  *
13348  * After the completion of the above groupname command we return to the stable
13349  * state shown below.
13350  * 	hme1, hme2, hme3	mpk17-84	ipsq1	mpk17-84	3
13351  *	hme4			mpk17-85	ipsq2	mpk17-85	1
13352  *
13353  * Because of the above, we don't search based on the ipsq_name since that
13354  * would miss the correct ipsq during certain windows as shown above.
13355  * The ipsq_name is only used during split of an ipsq to return the ipsq to its
13356  * natural state.
13357  */
13358 static ipsq_t *
13359 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq)
13360 {
13361 	ipsq_t	*ipsq;
13362 	int	group_len;
13363 	phyint_t *phyint;
13364 
13365 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13366 
13367 	group_len = strlen(groupname);
13368 	ASSERT(group_len != 0);
13369 	group_len++;
13370 
13371 	for (ipsq = ipsq_g_head; ipsq != NULL; ipsq = ipsq->ipsq_next) {
13372 		/*
13373 		 * When an ipsq is being split, and ill_split_ipsq
13374 		 * calls this function, we exclude it from being considered.
13375 		 */
13376 		if (ipsq == exclude_ipsq)
13377 			continue;
13378 
13379 		/*
13380 		 * Compare against the ipsq_name. The groupname change happens
13381 		 * in 2 phases. The 1st phase merges the from group into
13382 		 * the to group's ipsq, by calling ill_merge_groups and restarts
13383 		 * the ioctl. The 2nd phase then locates the ipsq again thru
13384 		 * ipsq_name. At this point the phyint_groupname has not been
13385 		 * updated.
13386 		 */
13387 		if ((group_len == strlen(ipsq->ipsq_name) + 1) &&
13388 		    (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) {
13389 			/*
13390 			 * Verify that an ipmp groupname is exactly
13391 			 * part of 1 ipsq and is not found in any other
13392 			 * ipsq.
13393 			 */
13394 			ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq) ==
13395 			    NULL);
13396 			return (ipsq);
13397 		}
13398 
13399 		/*
13400 		 * Comparison against ipsq_name alone is not sufficient.
13401 		 * In the case when groups are currently being
13402 		 * merged, the ipsq could hold other IPMP groups temporarily.
13403 		 * so we walk the phyint list and compare against the
13404 		 * phyint_groupname as well.
13405 		 */
13406 		phyint = ipsq->ipsq_phyint_list;
13407 		while (phyint != NULL) {
13408 			if ((group_len == phyint->phyint_groupname_len) &&
13409 			    (bcmp(phyint->phyint_groupname, groupname,
13410 			    group_len) == 0)) {
13411 				/*
13412 				 * Verify that an ipmp groupname is exactly
13413 				 * part of 1 ipsq and is not found in any other
13414 				 * ipsq.
13415 				 */
13416 				ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq)
13417 					== NULL);
13418 				return (ipsq);
13419 			}
13420 			phyint = phyint->phyint_ipsq_next;
13421 		}
13422 	}
13423 	if (create)
13424 		ipsq = ipsq_create(groupname);
13425 	return (ipsq);
13426 }
13427 
13428 static void
13429 ipsq_delete(ipsq_t *ipsq)
13430 {
13431 	ipsq_t *nipsq;
13432 	ipsq_t *pipsq = NULL;
13433 
13434 	/*
13435 	 * We don't hold the ipsq lock, but we are sure no new
13436 	 * messages can land up, since the ipsq_refs is zero.
13437 	 * i.e. this ipsq is unnamed and no phyint or phyint group
13438 	 * is associated with this ipsq. (Lookups are based on ill_name
13439 	 * or phyint_group_name)
13440 	 */
13441 	ASSERT(ipsq->ipsq_refs == 0);
13442 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL);
13443 	ASSERT(ipsq->ipsq_pending_mp == NULL);
13444 	if (!(ipsq->ipsq_flags & IPSQ_GROUP)) {
13445 		/*
13446 		 * This is not the ipsq of an IPMP group.
13447 		 */
13448 		kmem_free(ipsq, sizeof (ipsq_t));
13449 		return;
13450 	}
13451 
13452 	rw_enter(&ill_g_lock, RW_WRITER);
13453 
13454 	/*
13455 	 * Locate the ipsq  before we can remove it from
13456 	 * the singly linked list of ipsq's.
13457 	 */
13458 	for (nipsq = ipsq_g_head; nipsq != NULL; nipsq = nipsq->ipsq_next) {
13459 		if (nipsq == ipsq) {
13460 			break;
13461 		}
13462 		pipsq = nipsq;
13463 	}
13464 
13465 	ASSERT(nipsq == ipsq);
13466 
13467 	/* unlink ipsq from the list */
13468 	if (pipsq != NULL)
13469 		pipsq->ipsq_next = ipsq->ipsq_next;
13470 	else
13471 		ipsq_g_head = ipsq->ipsq_next;
13472 	kmem_free(ipsq, sizeof (ipsq_t));
13473 	rw_exit(&ill_g_lock);
13474 }
13475 
13476 static void
13477 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp,
13478     queue_t *q)
13479 
13480 {
13481 
13482 	ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock));
13483 	ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL);
13484 	ASSERT(old_ipsq->ipsq_pending_ipif == NULL);
13485 	ASSERT(old_ipsq->ipsq_pending_mp == NULL);
13486 	ASSERT(current_mp != NULL);
13487 
13488 	ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl,
13489 		NEW_OP, NULL);
13490 
13491 	ASSERT(new_ipsq->ipsq_xopq_mptail != NULL &&
13492 	    new_ipsq->ipsq_xopq_mphead != NULL);
13493 
13494 	/*
13495 	 * move from old ipsq to the new ipsq.
13496 	 */
13497 	new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead;
13498 	if (old_ipsq->ipsq_xopq_mphead != NULL)
13499 		new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail;
13500 
13501 	old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL;
13502 }
13503 
13504 void
13505 ill_group_cleanup(ill_t *ill)
13506 {
13507 	ill_t *ill_v4;
13508 	ill_t *ill_v6;
13509 	ipif_t *ipif;
13510 
13511 	ill_v4 = ill->ill_phyint->phyint_illv4;
13512 	ill_v6 = ill->ill_phyint->phyint_illv6;
13513 
13514 	if (ill_v4 != NULL) {
13515 		mutex_enter(&ill_v4->ill_lock);
13516 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13517 		    ipif = ipif->ipif_next) {
13518 			IPIF_UNMARK_MOVING(ipif);
13519 		}
13520 		ill_v4->ill_up_ipifs = B_FALSE;
13521 		mutex_exit(&ill_v4->ill_lock);
13522 	}
13523 
13524 	if (ill_v6 != NULL) {
13525 		mutex_enter(&ill_v6->ill_lock);
13526 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13527 		    ipif = ipif->ipif_next) {
13528 			IPIF_UNMARK_MOVING(ipif);
13529 		}
13530 		ill_v6->ill_up_ipifs = B_FALSE;
13531 		mutex_exit(&ill_v6->ill_lock);
13532 	}
13533 }
13534 /*
13535  * This function is called when an ill has had a change in its group status
13536  * to bring up all the ipifs that were up before the change.
13537  */
13538 int
13539 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
13540 {
13541 	ipif_t *ipif;
13542 	ill_t *ill_v4;
13543 	ill_t *ill_v6;
13544 	ill_t *from_ill;
13545 	int err = 0;
13546 
13547 
13548 	ASSERT(IAM_WRITER_ILL(ill));
13549 
13550 	/*
13551 	 * Except for ipif_state_flags and ill_state_flags the other
13552 	 * fields of the ipif/ill that are modified below are protected
13553 	 * implicitly since we are a writer. We would have tried to down
13554 	 * even an ipif that was already down, in ill_down_ipifs. So we
13555 	 * just blindly clear the IPIF_CHANGING flag here on all ipifs.
13556 	 */
13557 	ill_v4 = ill->ill_phyint->phyint_illv4;
13558 	ill_v6 = ill->ill_phyint->phyint_illv6;
13559 	if (ill_v4 != NULL) {
13560 		ill_v4->ill_up_ipifs = B_TRUE;
13561 		for (ipif = ill_v4->ill_ipif; ipif != NULL;
13562 		    ipif = ipif->ipif_next) {
13563 			mutex_enter(&ill_v4->ill_lock);
13564 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13565 			IPIF_UNMARK_MOVING(ipif);
13566 			mutex_exit(&ill_v4->ill_lock);
13567 			if (ipif->ipif_was_up) {
13568 				if (!(ipif->ipif_flags & IPIF_UP))
13569 					err = ipif_up(ipif, q, mp);
13570 				ipif->ipif_was_up = B_FALSE;
13571 				if (err != 0) {
13572 					/*
13573 					 * Can there be any other error ?
13574 					 */
13575 					ASSERT(err == EINPROGRESS);
13576 					return (err);
13577 				}
13578 			}
13579 		}
13580 		mutex_enter(&ill_v4->ill_lock);
13581 		ill_v4->ill_state_flags &= ~ILL_CHANGING;
13582 		mutex_exit(&ill_v4->ill_lock);
13583 		ill_v4->ill_up_ipifs = B_FALSE;
13584 		if (ill_v4->ill_move_in_progress) {
13585 			ASSERT(ill_v4->ill_move_peer != NULL);
13586 			ill_v4->ill_move_in_progress = B_FALSE;
13587 			from_ill = ill_v4->ill_move_peer;
13588 			from_ill->ill_move_in_progress = B_FALSE;
13589 			from_ill->ill_move_peer = NULL;
13590 			mutex_enter(&from_ill->ill_lock);
13591 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13592 			mutex_exit(&from_ill->ill_lock);
13593 			if (ill_v6 == NULL) {
13594 				if (from_ill->ill_phyint->phyint_flags &
13595 				    PHYI_STANDBY) {
13596 					phyint_inactive(from_ill->ill_phyint);
13597 				}
13598 				if (ill_v4->ill_phyint->phyint_flags &
13599 				    PHYI_STANDBY) {
13600 					phyint_inactive(ill_v4->ill_phyint);
13601 				}
13602 			}
13603 			ill_v4->ill_move_peer = NULL;
13604 		}
13605 	}
13606 
13607 	if (ill_v6 != NULL) {
13608 		ill_v6->ill_up_ipifs = B_TRUE;
13609 		for (ipif = ill_v6->ill_ipif; ipif != NULL;
13610 		    ipif = ipif->ipif_next) {
13611 			mutex_enter(&ill_v6->ill_lock);
13612 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
13613 			IPIF_UNMARK_MOVING(ipif);
13614 			mutex_exit(&ill_v6->ill_lock);
13615 			if (ipif->ipif_was_up) {
13616 				if (!(ipif->ipif_flags & IPIF_UP))
13617 					err = ipif_up(ipif, q, mp);
13618 				ipif->ipif_was_up = B_FALSE;
13619 				if (err != 0) {
13620 					/*
13621 					 * Can there be any other error ?
13622 					 */
13623 					ASSERT(err == EINPROGRESS);
13624 					return (err);
13625 				}
13626 			}
13627 		}
13628 		mutex_enter(&ill_v6->ill_lock);
13629 		ill_v6->ill_state_flags &= ~ILL_CHANGING;
13630 		mutex_exit(&ill_v6->ill_lock);
13631 		ill_v6->ill_up_ipifs = B_FALSE;
13632 		if (ill_v6->ill_move_in_progress) {
13633 			ASSERT(ill_v6->ill_move_peer != NULL);
13634 			ill_v6->ill_move_in_progress = B_FALSE;
13635 			from_ill = ill_v6->ill_move_peer;
13636 			from_ill->ill_move_in_progress = B_FALSE;
13637 			from_ill->ill_move_peer = NULL;
13638 			mutex_enter(&from_ill->ill_lock);
13639 			from_ill->ill_state_flags &= ~ILL_CHANGING;
13640 			mutex_exit(&from_ill->ill_lock);
13641 			if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
13642 				phyint_inactive(from_ill->ill_phyint);
13643 			}
13644 			if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) {
13645 				phyint_inactive(ill_v6->ill_phyint);
13646 			}
13647 			ill_v6->ill_move_peer = NULL;
13648 		}
13649 	}
13650 	return (0);
13651 }
13652 
13653 /*
13654  * bring down all the approriate ipifs.
13655  */
13656 /* ARGSUSED */
13657 static void
13658 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover)
13659 {
13660 	ipif_t *ipif;
13661 
13662 	ASSERT(IAM_WRITER_ILL(ill));
13663 
13664 	/*
13665 	 * Except for ipif_state_flags the other fields of the ipif/ill that
13666 	 * are modified below are protected implicitly since we are a writer
13667 	 */
13668 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13669 		if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER))
13670 			continue;
13671 		if (index == 0 || index == ipif->ipif_orig_ifindex) {
13672 			/*
13673 			 * We go through the ipif_down logic even if the ipif
13674 			 * is already down, since routes can be added based
13675 			 * on down ipifs. Going through ipif_down once again
13676 			 * will delete any IREs created based on these routes.
13677 			 */
13678 			if (ipif->ipif_flags & IPIF_UP)
13679 				ipif->ipif_was_up = B_TRUE;
13680 			/*
13681 			 * If called with chk_nofailover true ipif is moving.
13682 			 */
13683 			mutex_enter(&ill->ill_lock);
13684 			if (chk_nofailover) {
13685 				ipif->ipif_state_flags |=
13686 					IPIF_MOVING | IPIF_CHANGING;
13687 			} else {
13688 				ipif->ipif_state_flags |= IPIF_CHANGING;
13689 			}
13690 			mutex_exit(&ill->ill_lock);
13691 			/*
13692 			 * Need to re-create net/subnet bcast ires if
13693 			 * they are dependent on ipif.
13694 			 */
13695 			if (!ipif->ipif_isv6)
13696 				ipif_check_bcast_ires(ipif);
13697 			(void) ipif_logical_down(ipif, NULL, NULL);
13698 			ipif_down_tail(ipif);
13699 			/*
13700 			 * We don't do ipif_multicast_down for IPv4 in
13701 			 * ipif_down. We need to set this so that
13702 			 * ipif_multicast_up will join the
13703 			 * ALLHOSTS_GROUP on to_ill.
13704 			 */
13705 			ipif->ipif_multicast_up = B_FALSE;
13706 		}
13707 	}
13708 }
13709 
13710 #define	IPSQ_INC_REF(ipsq)	{			\
13711 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13712 	(ipsq)->ipsq_refs++;				\
13713 }
13714 
13715 #define	IPSQ_DEC_REF(ipsq)	{			\
13716 	ASSERT(RW_WRITE_HELD(&ill_g_lock));		\
13717 	(ipsq)->ipsq_refs--;				\
13718 	if ((ipsq)->ipsq_refs == 0)				\
13719 		(ipsq)->ipsq_name[0] = '\0'; 		\
13720 }
13721 
13722 /*
13723  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13724  * new_ipsq.
13725  */
13726 static void
13727 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq)
13728 {
13729 	phyint_t *phyint;
13730 	phyint_t *next_phyint;
13731 
13732 	/*
13733 	 * To change the ipsq of an ill, we need to hold the ill_g_lock as
13734 	 * writer and the ill_lock of the ill in question. Also the dest
13735 	 * ipsq can't vanish while we hold the ill_g_lock as writer.
13736 	 */
13737 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13738 
13739 	phyint = cur_ipsq->ipsq_phyint_list;
13740 	cur_ipsq->ipsq_phyint_list = NULL;
13741 	while (phyint != NULL) {
13742 		next_phyint = phyint->phyint_ipsq_next;
13743 		IPSQ_DEC_REF(cur_ipsq);
13744 		phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list;
13745 		new_ipsq->ipsq_phyint_list = phyint;
13746 		IPSQ_INC_REF(new_ipsq);
13747 		phyint->phyint_ipsq = new_ipsq;
13748 		phyint = next_phyint;
13749 	}
13750 }
13751 
13752 #define	SPLIT_SUCCESS		0
13753 #define	SPLIT_NOT_NEEDED	1
13754 #define	SPLIT_FAILED		2
13755 
13756 int
13757 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry)
13758 {
13759 	ipsq_t *newipsq = NULL;
13760 
13761 	/*
13762 	 * Assertions denote pre-requisites for changing the ipsq of
13763 	 * a phyint
13764 	 */
13765 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13766 	/*
13767 	 * <ill-phyint> assocs can't change while ill_g_lock
13768 	 * is held as writer. See ill_phyint_reinit()
13769 	 */
13770 	ASSERT(phyint->phyint_illv4 == NULL ||
13771 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13772 	ASSERT(phyint->phyint_illv6 == NULL ||
13773 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13774 
13775 	if ((phyint->phyint_groupname_len !=
13776 	    (strlen(cur_ipsq->ipsq_name) + 1) ||
13777 	    bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name,
13778 	    phyint->phyint_groupname_len) != 0)) {
13779 		/*
13780 		 * Once we fail in creating a new ipsq due to memory shortage,
13781 		 * don't attempt to create new ipsq again, based on another
13782 		 * phyint, since we want all phyints belonging to an IPMP group
13783 		 * to be in the same ipsq even in the event of mem alloc fails.
13784 		 */
13785 		newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry,
13786 		    cur_ipsq);
13787 		if (newipsq == NULL) {
13788 			/* Memory allocation failure */
13789 			return (SPLIT_FAILED);
13790 		} else {
13791 			/* ipsq_refs protected by ill_g_lock (writer) */
13792 			IPSQ_DEC_REF(cur_ipsq);
13793 			phyint->phyint_ipsq = newipsq;
13794 			phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list;
13795 			newipsq->ipsq_phyint_list = phyint;
13796 			IPSQ_INC_REF(newipsq);
13797 			return (SPLIT_SUCCESS);
13798 		}
13799 	}
13800 	return (SPLIT_NOT_NEEDED);
13801 }
13802 
13803 /*
13804  * The ill locks of the phyint and the ill_g_lock (writer) must be held
13805  * to do this split
13806  */
13807 static int
13808 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq)
13809 {
13810 	ipsq_t *newipsq;
13811 
13812 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
13813 	/*
13814 	 * <ill-phyint> assocs can't change while ill_g_lock
13815 	 * is held as writer. See ill_phyint_reinit()
13816 	 */
13817 
13818 	ASSERT(phyint->phyint_illv4 == NULL ||
13819 	    MUTEX_HELD(&phyint->phyint_illv4->ill_lock));
13820 	ASSERT(phyint->phyint_illv6 == NULL ||
13821 	    MUTEX_HELD(&phyint->phyint_illv6->ill_lock));
13822 
13823 	if (!ipsq_init((phyint->phyint_illv4 != NULL) ?
13824 	    phyint->phyint_illv4: phyint->phyint_illv6)) {
13825 		/*
13826 		 * ipsq_init failed due to no memory
13827 		 * caller will use the same ipsq
13828 		 */
13829 		return (SPLIT_FAILED);
13830 	}
13831 
13832 	/* ipsq_ref is protected by ill_g_lock (writer) */
13833 	IPSQ_DEC_REF(cur_ipsq);
13834 
13835 	/*
13836 	 * This is a new ipsq that is unknown to the world.
13837 	 * So we don't need to hold ipsq_lock,
13838 	 */
13839 	newipsq = phyint->phyint_ipsq;
13840 	newipsq->ipsq_writer = NULL;
13841 	newipsq->ipsq_reentry_cnt--;
13842 	ASSERT(newipsq->ipsq_reentry_cnt == 0);
13843 #ifdef ILL_DEBUG
13844 	newipsq->ipsq_depth = 0;
13845 #endif
13846 
13847 	return (SPLIT_SUCCESS);
13848 }
13849 
13850 /*
13851  * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to
13852  * ipsq's representing their individual groups or themselves. Return
13853  * whether split needs to be retried again later.
13854  */
13855 static boolean_t
13856 ill_split_ipsq(ipsq_t *cur_ipsq)
13857 {
13858 	phyint_t *phyint;
13859 	phyint_t *next_phyint;
13860 	int	error;
13861 	boolean_t need_retry = B_FALSE;
13862 
13863 	phyint = cur_ipsq->ipsq_phyint_list;
13864 	cur_ipsq->ipsq_phyint_list = NULL;
13865 	while (phyint != NULL) {
13866 		next_phyint = phyint->phyint_ipsq_next;
13867 		/*
13868 		 * 'created' will tell us whether the callee actually
13869 		 * created an ipsq. Lack of memory may force the callee
13870 		 * to return without creating an ipsq.
13871 		 */
13872 		if (phyint->phyint_groupname == NULL) {
13873 			error = ill_split_to_own_ipsq(phyint, cur_ipsq);
13874 		} else {
13875 			error = ill_split_to_grp_ipsq(phyint, cur_ipsq,
13876 					need_retry);
13877 		}
13878 
13879 		switch (error) {
13880 		case SPLIT_FAILED:
13881 			need_retry = B_TRUE;
13882 			/* FALLTHRU */
13883 		case SPLIT_NOT_NEEDED:
13884 			/*
13885 			 * Keep it on the list.
13886 			 */
13887 			phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list;
13888 			cur_ipsq->ipsq_phyint_list = phyint;
13889 			break;
13890 		case SPLIT_SUCCESS:
13891 			break;
13892 		default:
13893 			ASSERT(0);
13894 		}
13895 
13896 		phyint = next_phyint;
13897 	}
13898 	return (need_retry);
13899 }
13900 
13901 /*
13902  * given an ipsq 'ipsq' lock all ills associated with this ipsq.
13903  * and return the ills in the list. This list will be
13904  * needed to unlock all the ills later on by the caller.
13905  * The <ill-ipsq> associations could change between the
13906  * lock and unlock. Hence the unlock can't traverse the
13907  * ipsq to get the list of ills.
13908  */
13909 static int
13910 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max)
13911 {
13912 	int	cnt = 0;
13913 	phyint_t	*phyint;
13914 
13915 	/*
13916 	 * The caller holds ill_g_lock to ensure that the ill memberships
13917 	 * of the ipsq don't change
13918 	 */
13919 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
13920 
13921 	phyint = ipsq->ipsq_phyint_list;
13922 	while (phyint != NULL) {
13923 		if (phyint->phyint_illv4 != NULL) {
13924 			ASSERT(cnt < list_max);
13925 			list[cnt++] = phyint->phyint_illv4;
13926 		}
13927 		if (phyint->phyint_illv6 != NULL) {
13928 			ASSERT(cnt < list_max);
13929 			list[cnt++] = phyint->phyint_illv6;
13930 		}
13931 		phyint = phyint->phyint_ipsq_next;
13932 	}
13933 	ill_lock_ills(list, cnt);
13934 	return (cnt);
13935 }
13936 
13937 void
13938 ill_lock_ills(ill_t **list, int cnt)
13939 {
13940 	int	i;
13941 
13942 	if (cnt > 1) {
13943 		boolean_t try_again;
13944 		do {
13945 			try_again = B_FALSE;
13946 			for (i = 0; i < cnt - 1; i++) {
13947 				if (list[i] < list[i + 1]) {
13948 					ill_t	*tmp;
13949 
13950 					/* swap the elements */
13951 					tmp = list[i];
13952 					list[i] = list[i + 1];
13953 					list[i + 1] = tmp;
13954 					try_again = B_TRUE;
13955 				}
13956 			}
13957 		} while (try_again);
13958 	}
13959 
13960 	for (i = 0; i < cnt; i++) {
13961 		if (i == 0) {
13962 			if (list[i] != NULL)
13963 				mutex_enter(&list[i]->ill_lock);
13964 			else
13965 				return;
13966 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13967 			mutex_enter(&list[i]->ill_lock);
13968 		}
13969 	}
13970 }
13971 
13972 void
13973 ill_unlock_ills(ill_t **list, int cnt)
13974 {
13975 	int	i;
13976 
13977 	for (i = 0; i < cnt; i++) {
13978 		if ((i == 0) && (list[i] != NULL)) {
13979 			mutex_exit(&list[i]->ill_lock);
13980 		} else if ((list[i-1] != list[i]) && (list[i] != NULL)) {
13981 			mutex_exit(&list[i]->ill_lock);
13982 		}
13983 	}
13984 }
13985 
13986 /*
13987  * Merge all the ills from 1 ipsq group into another ipsq group.
13988  * The source ipsq group is specified by the ipsq associated with
13989  * 'from_ill'. The destination ipsq group is specified by the ipsq
13990  * associated with 'to_ill' or 'groupname' respectively.
13991  * Note that ipsq itself does not have a reference count mechanism
13992  * and functions don't look up an ipsq and pass it around. Instead
13993  * functions pass around an ill or groupname, and the ipsq is looked
13994  * up from the ill or groupname and the required operation performed
13995  * atomically with the lookup on the ipsq.
13996  */
13997 static int
13998 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp,
13999     queue_t *q)
14000 {
14001 	ipsq_t *old_ipsq;
14002 	ipsq_t *new_ipsq;
14003 	ill_t	**ill_list;
14004 	int	cnt;
14005 	size_t	ill_list_size;
14006 	boolean_t became_writer_on_new_sq = B_FALSE;
14007 
14008 	/* Exactly 1 of 'to_ill' and groupname can be specified. */
14009 	ASSERT((to_ill != NULL) ^ (groupname != NULL));
14010 
14011 	/*
14012 	 * Need to hold ill_g_lock as writer and also the ill_lock to
14013 	 * change the <ill-ipsq> assoc of an ill. Need to hold the
14014 	 * ipsq_lock to prevent new messages from landing on an ipsq.
14015 	 */
14016 	rw_enter(&ill_g_lock, RW_WRITER);
14017 
14018 	old_ipsq = from_ill->ill_phyint->phyint_ipsq;
14019 	if (groupname != NULL)
14020 		new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL);
14021 	else {
14022 		new_ipsq = to_ill->ill_phyint->phyint_ipsq;
14023 	}
14024 
14025 	ASSERT(old_ipsq != NULL && new_ipsq != NULL);
14026 
14027 	/*
14028 	 * both groups are on the same ipsq.
14029 	 */
14030 	if (old_ipsq == new_ipsq) {
14031 		rw_exit(&ill_g_lock);
14032 		return (0);
14033 	}
14034 
14035 	cnt = old_ipsq->ipsq_refs << 1;
14036 	ill_list_size = cnt * sizeof (ill_t *);
14037 	ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP);
14038 	if (ill_list == NULL) {
14039 		rw_exit(&ill_g_lock);
14040 		return (ENOMEM);
14041 	}
14042 	cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt);
14043 
14044 	/* Need ipsq lock to enque messages on new ipsq or to become writer */
14045 	mutex_enter(&new_ipsq->ipsq_lock);
14046 	if ((new_ipsq->ipsq_writer == NULL &&
14047 		new_ipsq->ipsq_current_ipif == NULL) ||
14048 	    (new_ipsq->ipsq_writer == curthread)) {
14049 		new_ipsq->ipsq_writer = curthread;
14050 		new_ipsq->ipsq_reentry_cnt++;
14051 		became_writer_on_new_sq = B_TRUE;
14052 	}
14053 
14054 	/*
14055 	 * We are holding ill_g_lock as writer and all the ill locks of
14056 	 * the old ipsq. So the old_ipsq can't be looked up, and hence no new
14057 	 * message can land up on the old ipsq even though we don't hold the
14058 	 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq.
14059 	 */
14060 	ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q);
14061 
14062 	/*
14063 	 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'.
14064 	 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq>
14065 	 * assocs. till we release the ill_g_lock, and hence it can't vanish.
14066 	 */
14067 	ill_merge_ipsq(old_ipsq, new_ipsq);
14068 
14069 	/*
14070 	 * Mark the new ipsq as needing a split since it is currently
14071 	 * being shared by more than 1 IPMP group. The split will
14072 	 * occur at the end of ipsq_exit
14073 	 */
14074 	new_ipsq->ipsq_split = B_TRUE;
14075 
14076 	/* Now release all the locks */
14077 	mutex_exit(&new_ipsq->ipsq_lock);
14078 	ill_unlock_ills(ill_list, cnt);
14079 	rw_exit(&ill_g_lock);
14080 
14081 	kmem_free(ill_list, ill_list_size);
14082 
14083 	/*
14084 	 * If we succeeded in becoming writer on the new ipsq, then
14085 	 * drain the new ipsq and start processing  all enqueued messages
14086 	 * including the current ioctl we are processing which is either
14087 	 * a set groupname or failover/failback.
14088 	 */
14089 	if (became_writer_on_new_sq)
14090 		ipsq_exit(new_ipsq, B_TRUE, B_TRUE);
14091 
14092 	/*
14093 	 * syncq has been changed and all the messages have been moved.
14094 	 */
14095 	mutex_enter(&old_ipsq->ipsq_lock);
14096 	old_ipsq->ipsq_current_ipif = NULL;
14097 	mutex_exit(&old_ipsq->ipsq_lock);
14098 	return (EINPROGRESS);
14099 }
14100 
14101 /*
14102  * Delete and add the loopback copy and non-loopback copy of
14103  * the BROADCAST ire corresponding to ill and addr. Used to
14104  * group broadcast ires together when ill becomes part of
14105  * a group.
14106  *
14107  * This function is also called when ill is leaving the group
14108  * so that the ires belonging to the group gets re-grouped.
14109  */
14110 static void
14111 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr)
14112 {
14113 	ire_t *ire, *nire, *nire_next, *ire_head = NULL;
14114 	ire_t **ire_ptpn = &ire_head;
14115 
14116 	/*
14117 	 * The loopback and non-loopback IREs are inserted in the order in which
14118 	 * they're found, on the basis that they are correctly ordered (loopback
14119 	 * first).
14120 	 */
14121 	for (;;) {
14122 		ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14123 		    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14124 		if (ire == NULL)
14125 			break;
14126 
14127 		/*
14128 		 * we are passing in KM_SLEEP because it is not easy to
14129 		 * go back to a sane state in case of memory failure.
14130 		 */
14131 		nire = kmem_cache_alloc(ire_cache, KM_SLEEP);
14132 		ASSERT(nire != NULL);
14133 		bzero(nire, sizeof (ire_t));
14134 		/*
14135 		 * Don't use ire_max_frag directly since we don't
14136 		 * hold on to 'ire' until we add the new ire 'nire' and
14137 		 * we don't want the new ire to have a dangling reference
14138 		 * to 'ire'. The ire_max_frag of a broadcast ire must
14139 		 * be in sync with the ipif_mtu of the associate ipif.
14140 		 * For eg. this happens as a result of SIOCSLIFNAME,
14141 		 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by
14142 		 * the driver. A change in ire_max_frag triggered as
14143 		 * as a result of path mtu discovery, or due to an
14144 		 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a
14145 		 * route change -mtu command does not apply to broadcast ires.
14146 		 *
14147 		 * XXX We need a recovery strategy here if ire_init fails
14148 		 */
14149 		if (ire_init(nire,
14150 		    (uchar_t *)&ire->ire_addr,
14151 		    (uchar_t *)&ire->ire_mask,
14152 		    (uchar_t *)&ire->ire_src_addr,
14153 		    (uchar_t *)&ire->ire_gateway_addr,
14154 		    (uchar_t *)&ire->ire_in_src_addr,
14155 		    ire->ire_stq == NULL ? &ip_loopback_mtu :
14156 			&ire->ire_ipif->ipif_mtu,
14157 		    ire->ire_fp_mp,
14158 		    ire->ire_rfq,
14159 		    ire->ire_stq,
14160 		    ire->ire_type,
14161 		    ire->ire_dlureq_mp,
14162 		    ire->ire_ipif,
14163 		    ire->ire_in_ill,
14164 		    ire->ire_cmask,
14165 		    ire->ire_phandle,
14166 		    ire->ire_ihandle,
14167 		    ire->ire_flags,
14168 		    &ire->ire_uinfo,
14169 		    NULL,
14170 		    NULL) == NULL) {
14171 			cmn_err(CE_PANIC, "ire_init() failed");
14172 		}
14173 		ire_delete(ire);
14174 		ire_refrele(ire);
14175 
14176 		/*
14177 		 * The newly created IREs are inserted at the tail of the list
14178 		 * starting with ire_head. As we've just allocated them no one
14179 		 * knows about them so it's safe.
14180 		 */
14181 		*ire_ptpn = nire;
14182 		ire_ptpn = &nire->ire_next;
14183 	}
14184 
14185 	for (nire = ire_head; nire != NULL; nire = nire_next) {
14186 		int error;
14187 		ire_t *oire;
14188 		/* unlink the IRE from our list before calling ire_add() */
14189 		nire_next = nire->ire_next;
14190 		nire->ire_next = NULL;
14191 
14192 		/* ire_add adds the ire at the right place in the list */
14193 		oire = nire;
14194 		error = ire_add(&nire, NULL, NULL, NULL);
14195 		ASSERT(error == 0);
14196 		ASSERT(oire == nire);
14197 		ire_refrele(nire);	/* Held in ire_add */
14198 	}
14199 }
14200 
14201 /*
14202  * This function is usually called when an ill is inserted in
14203  * a group and all the ipifs are already UP. As all the ipifs
14204  * are already UP, the broadcast ires have already been created
14205  * and been inserted. But, ire_add_v4 would not have grouped properly.
14206  * We need to re-group for the benefit of ip_wput_ire which
14207  * expects BROADCAST ires to be grouped properly to avoid sending
14208  * more than one copy of the broadcast packet per group.
14209  *
14210  * NOTE : We don't check for ill_ipif_up_count to be non-zero here
14211  *	  because when ipif_up_done ends up calling this, ires have
14212  *        already been added before illgrp_insert i.e before ill_group
14213  *	  has been initialized.
14214  */
14215 static void
14216 ill_group_bcast_for_xmit(ill_t *ill)
14217 {
14218 	ill_group_t *illgrp;
14219 	ipif_t *ipif;
14220 	ipaddr_t addr;
14221 	ipaddr_t net_mask;
14222 	ipaddr_t subnet_netmask;
14223 
14224 	illgrp = ill->ill_group;
14225 
14226 	/*
14227 	 * This function is called even when an ill is deleted from
14228 	 * the group. Hence, illgrp could be null.
14229 	 */
14230 	if (illgrp != NULL && illgrp->illgrp_ill_count == 1)
14231 		return;
14232 
14233 	/*
14234 	 * Delete all the BROADCAST ires matching this ill and add
14235 	 * them back. This time, ire_add_v4 should take care of
14236 	 * grouping them with others because ill is part of the
14237 	 * group.
14238 	 */
14239 	ill_bcast_delete_and_add(ill, 0);
14240 	ill_bcast_delete_and_add(ill, INADDR_BROADCAST);
14241 
14242 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14243 
14244 		if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14245 		    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14246 			net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14247 		} else {
14248 			net_mask = htonl(IN_CLASSA_NET);
14249 		}
14250 		addr = net_mask & ipif->ipif_subnet;
14251 		ill_bcast_delete_and_add(ill, addr);
14252 		ill_bcast_delete_and_add(ill, ~net_mask | addr);
14253 
14254 		subnet_netmask = ipif->ipif_net_mask;
14255 		addr = ipif->ipif_subnet;
14256 		ill_bcast_delete_and_add(ill, addr);
14257 		ill_bcast_delete_and_add(ill, ~subnet_netmask | addr);
14258 	}
14259 }
14260 
14261 /*
14262  * This function is called from illgrp_delete when ill is being deleted
14263  * from the group.
14264  *
14265  * As ill is not there in the group anymore, any address belonging
14266  * to this ill should be cleared of IRE_MARK_NORECV.
14267  */
14268 static void
14269 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr)
14270 {
14271 	ire_t *ire;
14272 	irb_t *irb;
14273 
14274 	ASSERT(ill->ill_group == NULL);
14275 
14276 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif,
14277 	    ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL);
14278 
14279 	if (ire != NULL) {
14280 		/*
14281 		 * IPMP and plumbing operations are serialized on the ipsq, so
14282 		 * no one will insert or delete a broadcast ire under our feet.
14283 		 */
14284 		irb = ire->ire_bucket;
14285 		rw_enter(&irb->irb_lock, RW_READER);
14286 		ire_refrele(ire);
14287 
14288 		for (; ire != NULL; ire = ire->ire_next) {
14289 			if (ire->ire_addr != addr)
14290 				break;
14291 			if (ire_to_ill(ire) != ill)
14292 				continue;
14293 
14294 			ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED));
14295 			ire->ire_marks &= ~IRE_MARK_NORECV;
14296 		}
14297 		rw_exit(&irb->irb_lock);
14298 	}
14299 }
14300 
14301 /*
14302  * This function must be called only after the broadcast ires
14303  * have been grouped together. For a given address addr, nominate
14304  * only one of the ires whose interface is not FAILED or OFFLINE.
14305  *
14306  * This is also called when an ipif goes down, so that we can nominate
14307  * a different ire with the same address for receiving.
14308  */
14309 static void
14310 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr)
14311 {
14312 	irb_t *irb;
14313 	ire_t *ire;
14314 	ire_t *ire1;
14315 	ire_t *save_ire;
14316 	ire_t **irep = NULL;
14317 	boolean_t first = B_TRUE;
14318 	ire_t *clear_ire = NULL;
14319 	ire_t *start_ire = NULL;
14320 	ire_t	*new_lb_ire;
14321 	ire_t	*new_nlb_ire;
14322 	boolean_t new_lb_ire_used = B_FALSE;
14323 	boolean_t new_nlb_ire_used = B_FALSE;
14324 	uint64_t match_flags;
14325 	uint64_t phyi_flags;
14326 	boolean_t fallback = B_FALSE;
14327 
14328 	ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES,
14329 	    NULL, MATCH_IRE_TYPE);
14330 	/*
14331 	 * We may not be able to find some ires if a previous
14332 	 * ire_create failed. This happens when an ipif goes
14333 	 * down and we are unable to create BROADCAST ires due
14334 	 * to memory failure. Thus, we have to check for NULL
14335 	 * below. This should handle the case for LOOPBACK,
14336 	 * POINTOPOINT and interfaces with some POINTOPOINT
14337 	 * logicals for which there are no BROADCAST ires.
14338 	 */
14339 	if (ire == NULL)
14340 		return;
14341 	/*
14342 	 * Currently IRE_BROADCASTS are deleted when an ipif
14343 	 * goes down which runs exclusively. Thus, setting
14344 	 * IRE_MARK_RCVD should not race with ire_delete marking
14345 	 * IRE_MARK_CONDEMNED. We grab the lock below just to
14346 	 * be consistent with other parts of the code that walks
14347 	 * a given bucket.
14348 	 */
14349 	save_ire = ire;
14350 	irb = ire->ire_bucket;
14351 	new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14352 	if (new_lb_ire == NULL) {
14353 		ire_refrele(ire);
14354 		return;
14355 	}
14356 	new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP);
14357 	if (new_nlb_ire == NULL) {
14358 		ire_refrele(ire);
14359 		kmem_cache_free(ire_cache, new_lb_ire);
14360 		return;
14361 	}
14362 	IRB_REFHOLD(irb);
14363 	rw_enter(&irb->irb_lock, RW_WRITER);
14364 	/*
14365 	 * Get to the first ire matching the address and the
14366 	 * group. If the address does not match we are done
14367 	 * as we could not find the IRE. If the address matches
14368 	 * we should get to the first one matching the group.
14369 	 */
14370 	while (ire != NULL) {
14371 		if (ire->ire_addr != addr ||
14372 		    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14373 			break;
14374 		}
14375 		ire = ire->ire_next;
14376 	}
14377 	match_flags = PHYI_FAILED | PHYI_INACTIVE;
14378 	start_ire = ire;
14379 redo:
14380 	while (ire != NULL && ire->ire_addr == addr &&
14381 	    ire->ire_ipif->ipif_ill->ill_group == illgrp) {
14382 		/*
14383 		 * The first ire for any address within a group
14384 		 * should always be the one with IRE_MARK_NORECV cleared
14385 		 * so that ip_wput_ire can avoid searching for one.
14386 		 * Note down the insertion point which will be used
14387 		 * later.
14388 		 */
14389 		if (first && (irep == NULL))
14390 			irep = ire->ire_ptpn;
14391 		/*
14392 		 * PHYI_FAILED is set when the interface fails.
14393 		 * This interface might have become good, but the
14394 		 * daemon has not yet detected. We should still
14395 		 * not receive on this. PHYI_OFFLINE should never
14396 		 * be picked as this has been offlined and soon
14397 		 * be removed.
14398 		 */
14399 		phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags;
14400 		if (phyi_flags & PHYI_OFFLINE) {
14401 			ire->ire_marks |= IRE_MARK_NORECV;
14402 			ire = ire->ire_next;
14403 			continue;
14404 		}
14405 		if (phyi_flags & match_flags) {
14406 			ire->ire_marks |= IRE_MARK_NORECV;
14407 			ire = ire->ire_next;
14408 			if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
14409 			    PHYI_INACTIVE) {
14410 				fallback = B_TRUE;
14411 			}
14412 			continue;
14413 		}
14414 		if (first) {
14415 			/*
14416 			 * We will move this to the front of the list later
14417 			 * on.
14418 			 */
14419 			clear_ire = ire;
14420 			ire->ire_marks &= ~IRE_MARK_NORECV;
14421 		} else {
14422 			ire->ire_marks |= IRE_MARK_NORECV;
14423 		}
14424 		first = B_FALSE;
14425 		ire = ire->ire_next;
14426 	}
14427 	/*
14428 	 * If we never nominated anybody, try nominating at least
14429 	 * an INACTIVE, if we found one. Do it only once though.
14430 	 */
14431 	if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) &&
14432 	    fallback) {
14433 		match_flags = PHYI_FAILED;
14434 		ire = start_ire;
14435 		irep = NULL;
14436 		goto redo;
14437 	}
14438 	ire_refrele(save_ire);
14439 
14440 	/*
14441 	 * irep non-NULL indicates that we entered the while loop
14442 	 * above. If clear_ire is at the insertion point, we don't
14443 	 * have to do anything. clear_ire will be NULL if all the
14444 	 * interfaces are failed.
14445 	 *
14446 	 * We cannot unlink and reinsert the ire at the right place
14447 	 * in the list since there can be other walkers of this bucket.
14448 	 * Instead we delete and recreate the ire
14449 	 */
14450 	if (clear_ire != NULL && irep != NULL && *irep != clear_ire) {
14451 		ire_t *clear_ire_stq = NULL;
14452 		bzero(new_lb_ire, sizeof (ire_t));
14453 		/* XXX We need a recovery strategy here. */
14454 		if (ire_init(new_lb_ire,
14455 		    (uchar_t *)&clear_ire->ire_addr,
14456 		    (uchar_t *)&clear_ire->ire_mask,
14457 		    (uchar_t *)&clear_ire->ire_src_addr,
14458 		    (uchar_t *)&clear_ire->ire_gateway_addr,
14459 		    (uchar_t *)&clear_ire->ire_in_src_addr,
14460 		    &clear_ire->ire_max_frag,
14461 		    clear_ire->ire_fp_mp,
14462 		    clear_ire->ire_rfq,
14463 		    clear_ire->ire_stq,
14464 		    clear_ire->ire_type,
14465 		    clear_ire->ire_dlureq_mp,
14466 		    clear_ire->ire_ipif,
14467 		    clear_ire->ire_in_ill,
14468 		    clear_ire->ire_cmask,
14469 		    clear_ire->ire_phandle,
14470 		    clear_ire->ire_ihandle,
14471 		    clear_ire->ire_flags,
14472 		    &clear_ire->ire_uinfo,
14473 		    NULL,
14474 		    NULL) == NULL)
14475 			cmn_err(CE_PANIC, "ire_init() failed");
14476 		if (clear_ire->ire_stq == NULL) {
14477 			ire_t *ire_next = clear_ire->ire_next;
14478 			if (ire_next != NULL &&
14479 			    ire_next->ire_stq != NULL &&
14480 			    ire_next->ire_addr == clear_ire->ire_addr &&
14481 			    ire_next->ire_ipif->ipif_ill ==
14482 			    clear_ire->ire_ipif->ipif_ill) {
14483 				clear_ire_stq = ire_next;
14484 
14485 				bzero(new_nlb_ire, sizeof (ire_t));
14486 				/* XXX We need a recovery strategy here. */
14487 				if (ire_init(new_nlb_ire,
14488 				    (uchar_t *)&clear_ire_stq->ire_addr,
14489 				    (uchar_t *)&clear_ire_stq->ire_mask,
14490 				    (uchar_t *)&clear_ire_stq->ire_src_addr,
14491 				    (uchar_t *)&clear_ire_stq->ire_gateway_addr,
14492 				    (uchar_t *)&clear_ire_stq->ire_in_src_addr,
14493 				    &clear_ire_stq->ire_max_frag,
14494 				    clear_ire_stq->ire_fp_mp,
14495 				    clear_ire_stq->ire_rfq,
14496 				    clear_ire_stq->ire_stq,
14497 				    clear_ire_stq->ire_type,
14498 				    clear_ire_stq->ire_dlureq_mp,
14499 				    clear_ire_stq->ire_ipif,
14500 				    clear_ire_stq->ire_in_ill,
14501 				    clear_ire_stq->ire_cmask,
14502 				    clear_ire_stq->ire_phandle,
14503 				    clear_ire_stq->ire_ihandle,
14504 				    clear_ire_stq->ire_flags,
14505 				    &clear_ire_stq->ire_uinfo,
14506 				    NULL,
14507 				    NULL) == NULL)
14508 					cmn_err(CE_PANIC, "ire_init() failed");
14509 			}
14510 		}
14511 
14512 		/*
14513 		 * Delete the ire. We can't call ire_delete() since
14514 		 * we are holding the bucket lock. We can't release the
14515 		 * bucket lock since we can't allow irep to change. So just
14516 		 * mark it CONDEMNED. The IRB_REFRELE will delete the
14517 		 * ire from the list and do the refrele.
14518 		 */
14519 		clear_ire->ire_marks |= IRE_MARK_CONDEMNED;
14520 		irb->irb_marks |= IRE_MARK_CONDEMNED;
14521 
14522 		if (clear_ire_stq != NULL) {
14523 			ire_fastpath_list_delete(
14524 			    (ill_t *)clear_ire_stq->ire_stq->q_ptr,
14525 			    clear_ire_stq);
14526 			clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED;
14527 		}
14528 
14529 		/*
14530 		 * Also take care of otherfields like ib/ob pkt count
14531 		 * etc. Need to dup them. ditto in ill_bcast_delete_and_add
14532 		 */
14533 
14534 		/* Add the new ire's. Insert at *irep */
14535 		new_lb_ire->ire_bucket = clear_ire->ire_bucket;
14536 		ire1 = *irep;
14537 		if (ire1 != NULL)
14538 			ire1->ire_ptpn = &new_lb_ire->ire_next;
14539 		new_lb_ire->ire_next = ire1;
14540 		/* Link the new one in. */
14541 		new_lb_ire->ire_ptpn = irep;
14542 		membar_producer();
14543 		*irep = new_lb_ire;
14544 		new_lb_ire_used = B_TRUE;
14545 		BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14546 		new_lb_ire->ire_bucket->irb_ire_cnt++;
14547 		new_lb_ire->ire_ipif->ipif_ire_cnt++;
14548 
14549 		if (clear_ire_stq != NULL) {
14550 			new_nlb_ire->ire_bucket = clear_ire->ire_bucket;
14551 			irep = &new_lb_ire->ire_next;
14552 			/* Add the new ire. Insert at *irep */
14553 			ire1 = *irep;
14554 			if (ire1 != NULL)
14555 				ire1->ire_ptpn = &new_nlb_ire->ire_next;
14556 			new_nlb_ire->ire_next = ire1;
14557 			/* Link the new one in. */
14558 			new_nlb_ire->ire_ptpn = irep;
14559 			membar_producer();
14560 			*irep = new_nlb_ire;
14561 			new_nlb_ire_used = B_TRUE;
14562 			BUMP_IRE_STATS(ire_stats_v4, ire_stats_inserted);
14563 			new_nlb_ire->ire_bucket->irb_ire_cnt++;
14564 			new_nlb_ire->ire_ipif->ipif_ire_cnt++;
14565 			((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++;
14566 		}
14567 	}
14568 	rw_exit(&irb->irb_lock);
14569 	if (!new_lb_ire_used)
14570 		kmem_cache_free(ire_cache, new_lb_ire);
14571 	if (!new_nlb_ire_used)
14572 		kmem_cache_free(ire_cache, new_nlb_ire);
14573 	IRB_REFRELE(irb);
14574 }
14575 
14576 /*
14577  * Whenever an ipif goes down we have to renominate a different
14578  * broadcast ire to receive. Whenever an ipif comes up, we need
14579  * to make sure that we have only one nominated to receive.
14580  */
14581 static void
14582 ipif_renominate_bcast(ipif_t *ipif)
14583 {
14584 	ill_t *ill = ipif->ipif_ill;
14585 	ipaddr_t subnet_addr;
14586 	ipaddr_t net_addr;
14587 	ipaddr_t net_mask = 0;
14588 	ipaddr_t subnet_netmask;
14589 	ipaddr_t addr;
14590 	ill_group_t *illgrp;
14591 
14592 	illgrp = ill->ill_group;
14593 	/*
14594 	 * If this is the last ipif going down, it might take
14595 	 * the ill out of the group. In that case ipif_down ->
14596 	 * illgrp_delete takes care of doing the nomination.
14597 	 * ipif_down does not call for this case.
14598 	 */
14599 	ASSERT(illgrp != NULL);
14600 
14601 	/* There could not have been any ires associated with this */
14602 	if (ipif->ipif_subnet == 0)
14603 		return;
14604 
14605 	ill_mark_bcast(illgrp, 0);
14606 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14607 
14608 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14609 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14610 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14611 	} else {
14612 		net_mask = htonl(IN_CLASSA_NET);
14613 	}
14614 	addr = net_mask & ipif->ipif_subnet;
14615 	ill_mark_bcast(illgrp, addr);
14616 
14617 	net_addr = ~net_mask | addr;
14618 	ill_mark_bcast(illgrp, net_addr);
14619 
14620 	subnet_netmask = ipif->ipif_net_mask;
14621 	addr = ipif->ipif_subnet;
14622 	ill_mark_bcast(illgrp, addr);
14623 
14624 	subnet_addr = ~subnet_netmask | addr;
14625 	ill_mark_bcast(illgrp, subnet_addr);
14626 }
14627 
14628 /*
14629  * Whenever we form or delete ill groups, we need to nominate one set of
14630  * BROADCAST ires for receiving in the group.
14631  *
14632  * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires
14633  *    have been added, but ill_ipif_up_count is 0. Thus, we don't assert
14634  *    for ill_ipif_up_count to be non-zero. This is the only case where
14635  *    ill_ipif_up_count is zero and we would still find the ires.
14636  *
14637  * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one
14638  *    ipif is UP and we just have to do the nomination.
14639  *
14640  * 3) When ill_handoff_responsibility calls us, some ill has been removed
14641  *    from the group. So, we have to do the nomination.
14642  *
14643  * Because of (3), there could be just one ill in the group. But we have
14644  * to nominate still as IRE_MARK_NORCV may have been marked on this.
14645  * Thus, this function does not optimize when there is only one ill as
14646  * it is not correct for (3).
14647  */
14648 static void
14649 ill_nominate_bcast_rcv(ill_group_t *illgrp)
14650 {
14651 	ill_t *ill;
14652 	ipif_t *ipif;
14653 	ipaddr_t subnet_addr;
14654 	ipaddr_t prev_subnet_addr = 0;
14655 	ipaddr_t net_addr;
14656 	ipaddr_t prev_net_addr = 0;
14657 	ipaddr_t net_mask = 0;
14658 	ipaddr_t subnet_netmask;
14659 	ipaddr_t addr;
14660 
14661 	/*
14662 	 * When the last memeber is leaving, there is nothing to
14663 	 * nominate.
14664 	 */
14665 	if (illgrp->illgrp_ill_count == 0) {
14666 		ASSERT(illgrp->illgrp_ill == NULL);
14667 		return;
14668 	}
14669 
14670 	ill = illgrp->illgrp_ill;
14671 	ASSERT(!ill->ill_isv6);
14672 	/*
14673 	 * We assume that ires with same address and belonging to the
14674 	 * same group, has been grouped together. Nominating a *single*
14675 	 * ill in the group for sending and receiving broadcast is done
14676 	 * by making sure that the first BROADCAST ire (which will be
14677 	 * the one returned by ire_ctable_lookup for ip_rput and the
14678 	 * one that will be used in ip_wput_ire) will be the one that
14679 	 * will not have IRE_MARK_NORECV set.
14680 	 *
14681 	 * 1) ip_rput checks and discards packets received on ires marked
14682 	 *    with IRE_MARK_NORECV. Thus, we don't send up duplicate
14683 	 *    broadcast packets. We need to clear IRE_MARK_NORECV on the
14684 	 *    first ire in the group for every broadcast address in the group.
14685 	 *    ip_rput will accept packets only on the first ire i.e only
14686 	 *    one copy of the ill.
14687 	 *
14688 	 * 2) ip_wput_ire needs to send out just one copy of the broadcast
14689 	 *    packet for the whole group. It needs to send out on the ill
14690 	 *    whose ire has not been marked with IRE_MARK_NORECV. If it sends
14691 	 *    on the one marked with IRE_MARK_NORECV, ip_rput will accept
14692 	 *    the copy echoed back on other port where the ire is not marked
14693 	 *    with IRE_MARK_NORECV.
14694 	 *
14695 	 * Note that we just need to have the first IRE either loopback or
14696 	 * non-loopback (either of them may not exist if ire_create failed
14697 	 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will
14698 	 * always hit the first one and hence will always accept one copy.
14699 	 *
14700 	 * We have a broadcast ire per ill for all the unique prefixes
14701 	 * hosted on that ill. As we don't have a way of knowing the
14702 	 * unique prefixes on a given ill and hence in the whole group,
14703 	 * we just call ill_mark_bcast on all the prefixes that exist
14704 	 * in the group. For the common case of one prefix, the code
14705 	 * below optimizes by remebering the last address used for
14706 	 * markng. In the case of multiple prefixes, this will still
14707 	 * optimize depending the order of prefixes.
14708 	 *
14709 	 * The only unique address across the whole group is 0.0.0.0 and
14710 	 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables
14711 	 * the first ire in the bucket for receiving and disables the
14712 	 * others.
14713 	 */
14714 	ill_mark_bcast(illgrp, 0);
14715 	ill_mark_bcast(illgrp, INADDR_BROADCAST);
14716 	for (; ill != NULL; ill = ill->ill_group_next) {
14717 
14718 		for (ipif = ill->ill_ipif; ipif != NULL;
14719 		    ipif = ipif->ipif_next) {
14720 
14721 			if (!(ipif->ipif_flags & IPIF_UP) ||
14722 			    ipif->ipif_subnet == 0) {
14723 				continue;
14724 			}
14725 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14726 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14727 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14728 			} else {
14729 				net_mask = htonl(IN_CLASSA_NET);
14730 			}
14731 			addr = net_mask & ipif->ipif_subnet;
14732 			if (prev_net_addr == 0 || prev_net_addr != addr) {
14733 				ill_mark_bcast(illgrp, addr);
14734 				net_addr = ~net_mask | addr;
14735 				ill_mark_bcast(illgrp, net_addr);
14736 			}
14737 			prev_net_addr = addr;
14738 
14739 			subnet_netmask = ipif->ipif_net_mask;
14740 			addr = ipif->ipif_subnet;
14741 			if (prev_subnet_addr == 0 ||
14742 			    prev_subnet_addr != addr) {
14743 				ill_mark_bcast(illgrp, addr);
14744 				subnet_addr = ~subnet_netmask | addr;
14745 				ill_mark_bcast(illgrp, subnet_addr);
14746 			}
14747 			prev_subnet_addr = addr;
14748 		}
14749 	}
14750 }
14751 
14752 /*
14753  * This function is called while forming ill groups.
14754  *
14755  * Currently, we handle only allmulti groups. We want to join
14756  * allmulti on only one of the ills in the groups. In future,
14757  * when we have link aggregation, we may have to join normal
14758  * multicast groups on multiple ills as switch does inbound load
14759  * balancing. Following are the functions that calls this
14760  * function :
14761  *
14762  * 1) ill_recover_multicast : Interface is coming back UP.
14763  *    When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6
14764  *    will call ill_recover_multicast to recover all the multicast
14765  *    groups. We need to make sure that only one member is joined
14766  *    in the ill group.
14767  *
14768  * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed.
14769  *    Somebody is joining allmulti. We need to make sure that only one
14770  *    member is joined in the group.
14771  *
14772  * 3) illgrp_insert : If allmulti has already joined, we need to make
14773  *    sure that only one member is joined in the group.
14774  *
14775  * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving
14776  *    allmulti who we have nominated. We need to pick someother ill.
14777  *
14778  * 5) illgrp_delete : The ill we nominated is leaving the group,
14779  *    we need to pick a new ill to join the group.
14780  *
14781  * For (1), (2), (5) - we just have to check whether there is
14782  * a good ill joined in the group. If we could not find any ills
14783  * joined the group, we should join.
14784  *
14785  * For (4), the one that was nominated to receive, left the group.
14786  * There could be nobody joined in the group when this function is
14787  * called.
14788  *
14789  * For (3) - we need to explicitly check whether there are multiple
14790  * ills joined in the group.
14791  *
14792  * For simplicity, we don't differentiate any of the above cases. We
14793  * just leave the group if it is joined on any of them and join on
14794  * the first good ill.
14795  */
14796 int
14797 ill_nominate_mcast_rcv(ill_group_t *illgrp)
14798 {
14799 	ilm_t *ilm;
14800 	ill_t *ill;
14801 	ill_t *fallback_inactive_ill = NULL;
14802 	ill_t *fallback_failed_ill = NULL;
14803 	int ret = 0;
14804 
14805 	/*
14806 	 * Leave the allmulti on all the ills and start fresh.
14807 	 */
14808 	for (ill = illgrp->illgrp_ill; ill != NULL;
14809 	    ill = ill->ill_group_next) {
14810 		if (ill->ill_join_allmulti)
14811 			(void) ip_leave_allmulti(ill->ill_ipif);
14812 	}
14813 
14814 	/*
14815 	 * Choose a good ill. Fallback to inactive or failed if
14816 	 * none available. We need to fallback to FAILED in the
14817 	 * case where we have 2 interfaces in a group - where
14818 	 * one of them is failed and another is a good one and
14819 	 * the good one (not marked inactive) is leaving the group.
14820 	 */
14821 	ret = 0;
14822 	for (ill = illgrp->illgrp_ill; ill != NULL;
14823 	    ill = ill->ill_group_next) {
14824 		/* Never pick an offline interface */
14825 		if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE)
14826 			continue;
14827 
14828 		if (ill->ill_phyint->phyint_flags & PHYI_FAILED) {
14829 			fallback_failed_ill = ill;
14830 			continue;
14831 		}
14832 		if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) {
14833 			fallback_inactive_ill = ill;
14834 			continue;
14835 		}
14836 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14837 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14838 				ret = ip_join_allmulti(ill->ill_ipif);
14839 				/*
14840 				 * ip_join_allmulti can fail because of memory
14841 				 * failures. So, make sure we join at least
14842 				 * on one ill.
14843 				 */
14844 				if (ill->ill_join_allmulti)
14845 					return (0);
14846 			}
14847 		}
14848 	}
14849 	if (ret != 0) {
14850 		/*
14851 		 * If we tried nominating above and failed to do so,
14852 		 * return error. We might have tried multiple times.
14853 		 * But, return the latest error.
14854 		 */
14855 		return (ret);
14856 	}
14857 	if ((ill = fallback_inactive_ill) != NULL) {
14858 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14859 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14860 				ret = ip_join_allmulti(ill->ill_ipif);
14861 				return (ret);
14862 			}
14863 		}
14864 	} else if ((ill = fallback_failed_ill) != NULL) {
14865 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14866 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14867 				ret = ip_join_allmulti(ill->ill_ipif);
14868 				return (ret);
14869 			}
14870 		}
14871 	}
14872 	return (0);
14873 }
14874 
14875 /*
14876  * This function is called from illgrp_delete after it is
14877  * deleted from the group to reschedule responsibilities
14878  * to a different ill.
14879  */
14880 static void
14881 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp)
14882 {
14883 	ilm_t	*ilm;
14884 	ipif_t	*ipif;
14885 	ipaddr_t subnet_addr;
14886 	ipaddr_t net_addr;
14887 	ipaddr_t net_mask = 0;
14888 	ipaddr_t subnet_netmask;
14889 	ipaddr_t addr;
14890 
14891 	ASSERT(ill->ill_group == NULL);
14892 	/*
14893 	 * Broadcast Responsibility:
14894 	 *
14895 	 * 1. If this ill has been nominated for receiving broadcast
14896 	 * packets, we need to find a new one. Before we find a new
14897 	 * one, we need to re-group the ires that are part of this new
14898 	 * group (assumed by ill_nominate_bcast_rcv). We do this by
14899 	 * calling ill_group_bcast_for_xmit(ill) which will do the right
14900 	 * thing for us.
14901 	 *
14902 	 * 2. If this ill was not nominated for receiving broadcast
14903 	 * packets, we need to clear the IRE_MARK_NORECV flag
14904 	 * so that we continue to send up broadcast packets.
14905 	 */
14906 	if (!ill->ill_isv6) {
14907 		/*
14908 		 * Case 1 above : No optimization here. Just redo the
14909 		 * nomination.
14910 		 */
14911 		ill_group_bcast_for_xmit(ill);
14912 		ill_nominate_bcast_rcv(illgrp);
14913 
14914 		/*
14915 		 * Case 2 above : Lookup and clear IRE_MARK_NORECV.
14916 		 */
14917 		ill_clear_bcast_mark(ill, 0);
14918 		ill_clear_bcast_mark(ill, INADDR_BROADCAST);
14919 
14920 		for (ipif = ill->ill_ipif; ipif != NULL;
14921 		    ipif = ipif->ipif_next) {
14922 
14923 			if (!(ipif->ipif_flags & IPIF_UP) ||
14924 			    ipif->ipif_subnet == 0) {
14925 				continue;
14926 			}
14927 			if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14928 			    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14929 				net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14930 			} else {
14931 				net_mask = htonl(IN_CLASSA_NET);
14932 			}
14933 			addr = net_mask & ipif->ipif_subnet;
14934 			ill_clear_bcast_mark(ill, addr);
14935 
14936 			net_addr = ~net_mask | addr;
14937 			ill_clear_bcast_mark(ill, net_addr);
14938 
14939 			subnet_netmask = ipif->ipif_net_mask;
14940 			addr = ipif->ipif_subnet;
14941 			ill_clear_bcast_mark(ill, addr);
14942 
14943 			subnet_addr = ~subnet_netmask | addr;
14944 			ill_clear_bcast_mark(ill, subnet_addr);
14945 		}
14946 	}
14947 
14948 	/*
14949 	 * Multicast Responsibility.
14950 	 *
14951 	 * If we have joined allmulti on this one, find a new member
14952 	 * in the group to join allmulti. As this ill is already part
14953 	 * of allmulti, we don't have to join on this one.
14954 	 *
14955 	 * If we have not joined allmulti on this one, there is no
14956 	 * responsibility to handoff. But we need to take new
14957 	 * responsibility i.e, join allmulti on this one if we need
14958 	 * to.
14959 	 */
14960 	if (ill->ill_join_allmulti) {
14961 		(void) ill_nominate_mcast_rcv(illgrp);
14962 	} else {
14963 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14964 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
14965 				(void) ip_join_allmulti(ill->ill_ipif);
14966 				break;
14967 			}
14968 		}
14969 	}
14970 
14971 	/*
14972 	 * We intentionally do the flushing of IRE_CACHES only matching
14973 	 * on the ill and not on groups. Note that we are already deleted
14974 	 * from the group.
14975 	 *
14976 	 * This will make sure that all IRE_CACHES whose stq is pointing
14977 	 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get
14978 	 * deleted and IRE_CACHES that are not pointing at this ill will
14979 	 * be left alone.
14980 	 */
14981 	if (ill->ill_isv6) {
14982 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14983 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14984 	} else {
14985 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
14986 		    IRE_CACHE, illgrp_cache_delete, (char *)ill, ill);
14987 	}
14988 
14989 	/*
14990 	 * Some conn may have cached one of the IREs deleted above. By removing
14991 	 * the ire reference, we clean up the extra reference to the ill held in
14992 	 * ire->ire_stq.
14993 	 */
14994 	ipcl_walk(conn_cleanup_stale_ire, NULL);
14995 
14996 	/*
14997 	 * Re-do source address selection for all the members in the
14998 	 * group, if they borrowed source address from one of the ipifs
14999 	 * in this ill.
15000 	 */
15001 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15002 		if (ill->ill_isv6) {
15003 			ipif_update_other_ipifs_v6(ipif, illgrp);
15004 		} else {
15005 			ipif_update_other_ipifs(ipif, illgrp);
15006 		}
15007 	}
15008 }
15009 
15010 /*
15011  * Delete the ill from the group. The caller makes sure that it is
15012  * in a group and it okay to delete from the group. So, we always
15013  * delete here.
15014  */
15015 static void
15016 illgrp_delete(ill_t *ill)
15017 {
15018 	ill_group_t *illgrp;
15019 	ill_group_t *tmpg;
15020 	ill_t *tmp_ill;
15021 
15022 	/*
15023 	 * Reset illgrp_ill_schednext if it was pointing at us.
15024 	 * We need to do this before we set ill_group to NULL.
15025 	 */
15026 	rw_enter(&ill_g_lock, RW_WRITER);
15027 	mutex_enter(&ill->ill_lock);
15028 
15029 	illgrp_reset_schednext(ill);
15030 
15031 	illgrp = ill->ill_group;
15032 
15033 	/* Delete the ill from illgrp. */
15034 	if (illgrp->illgrp_ill == ill) {
15035 		illgrp->illgrp_ill = ill->ill_group_next;
15036 	} else {
15037 		tmp_ill = illgrp->illgrp_ill;
15038 		while (tmp_ill->ill_group_next != ill) {
15039 			tmp_ill = tmp_ill->ill_group_next;
15040 			ASSERT(tmp_ill != NULL);
15041 		}
15042 		tmp_ill->ill_group_next = ill->ill_group_next;
15043 	}
15044 	ill->ill_group = NULL;
15045 	ill->ill_group_next = NULL;
15046 
15047 	illgrp->illgrp_ill_count--;
15048 	mutex_exit(&ill->ill_lock);
15049 	rw_exit(&ill_g_lock);
15050 
15051 	/*
15052 	 * As this ill is leaving the group, we need to hand off
15053 	 * the responsibilities to the other ills in the group, if
15054 	 * this ill had some responsibilities.
15055 	 */
15056 
15057 	ill_handoff_responsibility(ill, illgrp);
15058 
15059 	rw_enter(&ill_g_lock, RW_WRITER);
15060 
15061 	if (illgrp->illgrp_ill_count == 0) {
15062 
15063 		ASSERT(illgrp->illgrp_ill == NULL);
15064 		if (ill->ill_isv6) {
15065 			if (illgrp == illgrp_head_v6) {
15066 				illgrp_head_v6 = illgrp->illgrp_next;
15067 			} else {
15068 				tmpg = illgrp_head_v6;
15069 				while (tmpg->illgrp_next != illgrp) {
15070 					tmpg = tmpg->illgrp_next;
15071 					ASSERT(tmpg != NULL);
15072 				}
15073 				tmpg->illgrp_next = illgrp->illgrp_next;
15074 			}
15075 		} else {
15076 			if (illgrp == illgrp_head_v4) {
15077 				illgrp_head_v4 = illgrp->illgrp_next;
15078 			} else {
15079 				tmpg = illgrp_head_v4;
15080 				while (tmpg->illgrp_next != illgrp) {
15081 					tmpg = tmpg->illgrp_next;
15082 					ASSERT(tmpg != NULL);
15083 				}
15084 				tmpg->illgrp_next = illgrp->illgrp_next;
15085 			}
15086 		}
15087 		mutex_destroy(&illgrp->illgrp_lock);
15088 		mi_free(illgrp);
15089 	}
15090 	rw_exit(&ill_g_lock);
15091 
15092 	/*
15093 	 * Even though the ill is out of the group its not necessary
15094 	 * to set ipsq_split as TRUE as the ipifs could be down temporarily
15095 	 * We will split the ipsq when phyint_groupname is set to NULL.
15096 	 */
15097 
15098 	/*
15099 	 * Send a routing sockets message if we are deleting from
15100 	 * groups with names.
15101 	 */
15102 	if (ill->ill_phyint->phyint_groupname_len != 0)
15103 		ip_rts_ifmsg(ill->ill_ipif);
15104 }
15105 
15106 /*
15107  * Re-do source address selection. This is normally called when
15108  * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST
15109  * ipif comes up.
15110  */
15111 void
15112 ill_update_source_selection(ill_t *ill)
15113 {
15114 	ipif_t *ipif;
15115 
15116 	ASSERT(IAM_WRITER_ILL(ill));
15117 
15118 	if (ill->ill_group != NULL)
15119 		ill = ill->ill_group->illgrp_ill;
15120 
15121 	for (; ill != NULL; ill = ill->ill_group_next) {
15122 		for (ipif = ill->ill_ipif; ipif != NULL;
15123 		    ipif = ipif->ipif_next) {
15124 			if (ill->ill_isv6)
15125 				ipif_recreate_interface_routes_v6(NULL, ipif);
15126 			else
15127 				ipif_recreate_interface_routes(NULL, ipif);
15128 		}
15129 	}
15130 }
15131 
15132 /*
15133  * Insert ill in a group headed by illgrp_head. The caller can either
15134  * pass a groupname in which case we search for a group with the
15135  * same name to insert in or pass a group to insert in. This function
15136  * would only search groups with names.
15137  *
15138  * NOTE : The caller should make sure that there is at least one ipif
15139  *	  UP on this ill so that illgrp_scheduler can pick this ill
15140  *	  for outbound packets. If ill_ipif_up_count is zero, we have
15141  *	  already sent a DL_UNBIND to the driver and we don't want to
15142  *	  send anymore packets. We don't assert for ipif_up_count
15143  *	  to be greater than zero, because ipif_up_done wants to call
15144  *	  this function before bumping up the ipif_up_count. See
15145  *	  ipif_up_done() for details.
15146  */
15147 int
15148 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname,
15149     ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up)
15150 {
15151 	ill_group_t *illgrp;
15152 	ill_t *prev_ill;
15153 	phyint_t *phyi;
15154 
15155 	ASSERT(ill->ill_group == NULL);
15156 
15157 	rw_enter(&ill_g_lock, RW_WRITER);
15158 	mutex_enter(&ill->ill_lock);
15159 
15160 	if (groupname != NULL) {
15161 		/*
15162 		 * Look for a group with a matching groupname to insert.
15163 		 */
15164 		for (illgrp = *illgrp_head; illgrp != NULL;
15165 		    illgrp = illgrp->illgrp_next) {
15166 
15167 			ill_t *tmp_ill;
15168 
15169 			/*
15170 			 * If we have an ill_group_t in the list which has
15171 			 * no ill_t assigned then we must be in the process of
15172 			 * removing this group. We skip this as illgrp_delete()
15173 			 * will remove it from the list.
15174 			 */
15175 			if ((tmp_ill = illgrp->illgrp_ill) == NULL) {
15176 				ASSERT(illgrp->illgrp_ill_count == 0);
15177 				continue;
15178 			}
15179 
15180 			ASSERT(tmp_ill->ill_phyint != NULL);
15181 			phyi = tmp_ill->ill_phyint;
15182 			/*
15183 			 * Look at groups which has names only.
15184 			 */
15185 			if (phyi->phyint_groupname_len == 0)
15186 				continue;
15187 			/*
15188 			 * Names are stored in the phyint common to both
15189 			 * IPv4 and IPv6.
15190 			 */
15191 			if (mi_strcmp(phyi->phyint_groupname,
15192 			    groupname) == 0) {
15193 				break;
15194 			}
15195 		}
15196 	} else {
15197 		/*
15198 		 * If the caller passes in a NULL "grp_to_insert", we
15199 		 * allocate one below and insert this singleton.
15200 		 */
15201 		illgrp = grp_to_insert;
15202 	}
15203 
15204 	ill->ill_group_next = NULL;
15205 
15206 	if (illgrp == NULL) {
15207 		illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t));
15208 		if (illgrp == NULL) {
15209 			return (ENOMEM);
15210 		}
15211 		illgrp->illgrp_next = *illgrp_head;
15212 		*illgrp_head = illgrp;
15213 		illgrp->illgrp_ill = ill;
15214 		illgrp->illgrp_ill_count = 1;
15215 		ill->ill_group = illgrp;
15216 		/*
15217 		 * Used in illgrp_scheduler to protect multiple threads
15218 		 * from traversing the list.
15219 		 */
15220 		mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0);
15221 	} else {
15222 		ASSERT(ill->ill_net_type ==
15223 		    illgrp->illgrp_ill->ill_net_type);
15224 		ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type);
15225 
15226 		/* Insert ill at tail of this group */
15227 		prev_ill = illgrp->illgrp_ill;
15228 		while (prev_ill->ill_group_next != NULL)
15229 			prev_ill = prev_ill->ill_group_next;
15230 		prev_ill->ill_group_next = ill;
15231 		ill->ill_group = illgrp;
15232 		illgrp->illgrp_ill_count++;
15233 		/*
15234 		 * Inherit group properties. Currently only forwarding
15235 		 * is the property we try to keep the same with all the
15236 		 * ills. When there are more, we will abstract this into
15237 		 * a function.
15238 		 */
15239 		ill->ill_flags &= ~ILLF_ROUTER;
15240 		ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER);
15241 	}
15242 	mutex_exit(&ill->ill_lock);
15243 	rw_exit(&ill_g_lock);
15244 
15245 	/*
15246 	 * 1) When ipif_up_done() calls this function, ipif_up_count
15247 	 *    may be zero as it has not yet been bumped. But the ires
15248 	 *    have already been added. So, we do the nomination here
15249 	 *    itself. But, when ip_sioctl_groupname calls this, it checks
15250 	 *    for ill_ipif_up_count != 0. Thus we don't check for
15251 	 *    ill_ipif_up_count here while nominating broadcast ires for
15252 	 *    receive.
15253 	 *
15254 	 * 2) Similarly, we need to call ill_group_bcast_for_xmit here
15255 	 *    to group them properly as ire_add() has already happened
15256 	 *    in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert
15257 	 *    case, we need to do it here anyway.
15258 	 */
15259 	if (!ill->ill_isv6) {
15260 		ill_group_bcast_for_xmit(ill);
15261 		ill_nominate_bcast_rcv(illgrp);
15262 	}
15263 
15264 	if (!ipif_is_coming_up) {
15265 		/*
15266 		 * When ipif_up_done() calls this function, the multicast
15267 		 * groups have not been joined yet. So, there is no point in
15268 		 * nomination. ip_join_allmulti will handle groups when
15269 		 * ill_recover_multicast is called from ipif_up_done() later.
15270 		 */
15271 		(void) ill_nominate_mcast_rcv(illgrp);
15272 		/*
15273 		 * ipif_up_done calls ill_update_source_selection
15274 		 * anyway. Moreover, we don't want to re-create
15275 		 * interface routes while ipif_up_done() still has reference
15276 		 * to them. Refer to ipif_up_done() for more details.
15277 		 */
15278 		ill_update_source_selection(ill);
15279 	}
15280 
15281 	/*
15282 	 * Send a routing sockets message if we are inserting into
15283 	 * groups with names.
15284 	 */
15285 	if (groupname != NULL)
15286 		ip_rts_ifmsg(ill->ill_ipif);
15287 	return (0);
15288 }
15289 
15290 /*
15291  * Return the first phyint matching the groupname. There could
15292  * be more than one when there are ill groups.
15293  *
15294  * Needs work: called only from ip_sioctl_groupname
15295  */
15296 static phyint_t *
15297 phyint_lookup_group(char *groupname)
15298 {
15299 	phyint_t *phyi;
15300 
15301 	ASSERT(RW_LOCK_HELD(&ill_g_lock));
15302 	/*
15303 	 * Group names are stored in the phyint - a common structure
15304 	 * to both IPv4 and IPv6.
15305 	 */
15306 	phyi = avl_first(&phyint_g_list.phyint_list_avl_by_index);
15307 	for (; phyi != NULL;
15308 	    phyi = avl_walk(&phyint_g_list.phyint_list_avl_by_index,
15309 	    phyi, AVL_AFTER)) {
15310 		if (phyi->phyint_groupname_len == 0)
15311 			continue;
15312 		ASSERT(phyi->phyint_groupname != NULL);
15313 		if (mi_strcmp(groupname, phyi->phyint_groupname) == 0)
15314 			return (phyi);
15315 	}
15316 	return (NULL);
15317 }
15318 
15319 
15320 
15321 /*
15322  * MT notes on creation and deletion of IPMP groups
15323  *
15324  * Creation and deletion of IPMP groups introduce the need to merge or
15325  * split the associated serialization objects i.e the ipsq's. Normally all
15326  * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled
15327  * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during
15328  * the execution of the SIOCSLIFGROUPNAME command the picture changes. There
15329  * is a need to change the <ill-ipsq> association and we have to operate on both
15330  * the source and destination IPMP groups. For eg. attempting to set the
15331  * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to
15332  * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the
15333  * source or destination IPMP group are mapped to a single ipsq for executing
15334  * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's.
15335  * The <ill-ipsq> mapping is restored back to normal at a later point. This is
15336  * termed as a split of the ipsq. The converse of the merge i.e. a split of the
15337  * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname
15338  * occurred on the ipsq, then the ipsq_split flag is set. This indicates the
15339  * ipsq has to be examined for redoing the <ill-ipsq> associations.
15340  *
15341  * In the above example the ioctl handling code locates the current ipsq of hme0
15342  * which is ipsq(mpk17-84). It then enters the above ipsq immediately or
15343  * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates
15344  * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into
15345  * the destination ipsq. If the destination ipsq is not busy, it also enters
15346  * the destination ipsq exclusively. Now the actual groupname setting operation
15347  * can proceed. If the destination ipsq is busy, the operation is enqueued
15348  * on the destination (merged) ipsq and will be handled in the unwind from
15349  * ipsq_exit.
15350  *
15351  * To prevent other threads accessing the ill while the group name change is
15352  * in progres, we bring down the ipifs which also removes the ill from the
15353  * group. The group is changed in phyint and when the first ipif on the ill
15354  * is brought up, the ill is inserted into the right IPMP group by
15355  * illgrp_insert.
15356  */
15357 /* ARGSUSED */
15358 int
15359 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15360     ip_ioctl_cmd_t *ipip, void *ifreq)
15361 {
15362 	int i;
15363 	char *tmp;
15364 	int namelen;
15365 	ill_t *ill = ipif->ipif_ill;
15366 	ill_t *ill_v4, *ill_v6;
15367 	int err = 0;
15368 	phyint_t *phyi;
15369 	phyint_t *phyi_tmp;
15370 	struct lifreq *lifr;
15371 	mblk_t	*mp1;
15372 	char *groupname;
15373 	ipsq_t *ipsq;
15374 
15375 	ASSERT(IAM_WRITER_IPIF(ipif));
15376 
15377 	/* Existance verified in ip_wput_nondata */
15378 	mp1 = mp->b_cont->b_cont;
15379 	lifr = (struct lifreq *)mp1->b_rptr;
15380 	groupname = lifr->lifr_groupname;
15381 
15382 	if (ipif->ipif_id != 0)
15383 		return (EINVAL);
15384 
15385 	phyi = ill->ill_phyint;
15386 	ASSERT(phyi != NULL);
15387 
15388 	if (phyi->phyint_flags & PHYI_VIRTUAL)
15389 		return (EINVAL);
15390 
15391 	tmp = groupname;
15392 	for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++)
15393 		;
15394 
15395 	if (i == LIFNAMSIZ) {
15396 		/* no null termination */
15397 		return (EINVAL);
15398 	}
15399 
15400 	/*
15401 	 * Calculate the namelen exclusive of the null
15402 	 * termination character.
15403 	 */
15404 	namelen = tmp - groupname;
15405 
15406 	ill_v4 = phyi->phyint_illv4;
15407 	ill_v6 = phyi->phyint_illv6;
15408 
15409 	/*
15410 	 * ILL cannot be part of a usesrc group and and IPMP group at the
15411 	 * same time. No need to grab the ill_g_usesrc_lock here, see
15412 	 * synchronization notes in ip.c
15413 	 */
15414 	if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) {
15415 		return (EINVAL);
15416 	}
15417 
15418 	/*
15419 	 * mark the ill as changing.
15420 	 * this should queue all new requests on the syncq.
15421 	 */
15422 	GRAB_ILL_LOCKS(ill_v4, ill_v6);
15423 
15424 	if (ill_v4 != NULL)
15425 		ill_v4->ill_state_flags |= ILL_CHANGING;
15426 	if (ill_v6 != NULL)
15427 		ill_v6->ill_state_flags |= ILL_CHANGING;
15428 	RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15429 
15430 	if (namelen == 0) {
15431 		/*
15432 		 * Null string means remove this interface from the
15433 		 * existing group.
15434 		 */
15435 		if (phyi->phyint_groupname_len == 0) {
15436 			/*
15437 			 * Never was in a group.
15438 			 */
15439 			err = 0;
15440 			goto done;
15441 		}
15442 
15443 		/*
15444 		 * IPv4 or IPv6 may be temporarily out of the group when all
15445 		 * the ipifs are down. Thus, we need to check for ill_group to
15446 		 * be non-NULL.
15447 		 */
15448 		if (ill_v4 != NULL && ill_v4->ill_group != NULL) {
15449 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15450 			mutex_enter(&ill_v4->ill_lock);
15451 			if (!ill_is_quiescent(ill_v4)) {
15452 				/*
15453 				 * ipsq_pending_mp_add will not fail since
15454 				 * connp is NULL
15455 				 */
15456 				(void) ipsq_pending_mp_add(NULL,
15457 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15458 				mutex_exit(&ill_v4->ill_lock);
15459 				err = EINPROGRESS;
15460 				goto done;
15461 			}
15462 			mutex_exit(&ill_v4->ill_lock);
15463 		}
15464 
15465 		if (ill_v6 != NULL && ill_v6->ill_group != NULL) {
15466 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15467 			mutex_enter(&ill_v6->ill_lock);
15468 			if (!ill_is_quiescent(ill_v6)) {
15469 				(void) ipsq_pending_mp_add(NULL,
15470 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15471 				mutex_exit(&ill_v6->ill_lock);
15472 				err = EINPROGRESS;
15473 				goto done;
15474 			}
15475 			mutex_exit(&ill_v6->ill_lock);
15476 		}
15477 
15478 		rw_enter(&ill_g_lock, RW_WRITER);
15479 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15480 		mutex_enter(&phyi->phyint_lock);
15481 		ASSERT(phyi->phyint_groupname != NULL);
15482 		mi_free(phyi->phyint_groupname);
15483 		phyi->phyint_groupname = NULL;
15484 		phyi->phyint_groupname_len = 0;
15485 		mutex_exit(&phyi->phyint_lock);
15486 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15487 		rw_exit(&ill_g_lock);
15488 		err = ill_up_ipifs(ill, q, mp);
15489 
15490 		/*
15491 		 * set the split flag so that the ipsq can be split
15492 		 */
15493 		mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15494 		phyi->phyint_ipsq->ipsq_split = B_TRUE;
15495 		mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15496 
15497 	} else {
15498 		if (phyi->phyint_groupname_len != 0) {
15499 			ASSERT(phyi->phyint_groupname != NULL);
15500 			/* Are we inserting in the same group ? */
15501 			if (mi_strcmp(groupname,
15502 			    phyi->phyint_groupname) == 0) {
15503 				err = 0;
15504 				goto done;
15505 			}
15506 		}
15507 
15508 		rw_enter(&ill_g_lock, RW_READER);
15509 		/*
15510 		 * Merge ipsq for the group's.
15511 		 * This check is here as multiple groups/ills might be
15512 		 * sharing the same ipsq.
15513 		 * If we have to merege than the operation is restarted
15514 		 * on the new ipsq.
15515 		 */
15516 		ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL);
15517 		if (phyi->phyint_ipsq != ipsq) {
15518 			rw_exit(&ill_g_lock);
15519 			err = ill_merge_groups(ill, NULL, groupname, mp, q);
15520 			goto done;
15521 		}
15522 		/*
15523 		 * Running exclusive on new ipsq.
15524 		 */
15525 
15526 		ASSERT(ipsq != NULL);
15527 		ASSERT(ipsq->ipsq_writer == curthread);
15528 
15529 		/*
15530 		 * Check whether the ill_type and ill_net_type matches before
15531 		 * we allocate any memory so that the cleanup is easier.
15532 		 *
15533 		 * We can't group dissimilar ones as we can't load spread
15534 		 * packets across the group because of potential link-level
15535 		 * header differences.
15536 		 */
15537 		phyi_tmp = phyint_lookup_group(groupname);
15538 		if (phyi_tmp != NULL) {
15539 			if ((ill_v4 != NULL &&
15540 			    phyi_tmp->phyint_illv4 != NULL) &&
15541 			    ((ill_v4->ill_net_type !=
15542 			    phyi_tmp->phyint_illv4->ill_net_type) ||
15543 			    (ill_v4->ill_type !=
15544 			    phyi_tmp->phyint_illv4->ill_type))) {
15545 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15546 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15547 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15548 				rw_exit(&ill_g_lock);
15549 				return (EINVAL);
15550 			}
15551 			if ((ill_v6 != NULL &&
15552 			    phyi_tmp->phyint_illv6 != NULL) &&
15553 			    ((ill_v6->ill_net_type !=
15554 			    phyi_tmp->phyint_illv6->ill_net_type) ||
15555 			    (ill_v6->ill_type !=
15556 			    phyi_tmp->phyint_illv6->ill_type))) {
15557 				mutex_enter(&phyi->phyint_ipsq->ipsq_lock);
15558 				phyi->phyint_ipsq->ipsq_split = B_TRUE;
15559 				mutex_exit(&phyi->phyint_ipsq->ipsq_lock);
15560 				rw_exit(&ill_g_lock);
15561 				return (EINVAL);
15562 			}
15563 		}
15564 
15565 		rw_exit(&ill_g_lock);
15566 
15567 		/*
15568 		 * bring down all v4 ipifs.
15569 		 */
15570 		if (ill_v4 != NULL) {
15571 			ill_down_ipifs(ill_v4, mp, 0, B_FALSE);
15572 		}
15573 
15574 		/*
15575 		 * bring down all v6 ipifs.
15576 		 */
15577 		if (ill_v6 != NULL) {
15578 			ill_down_ipifs(ill_v6, mp, 0, B_FALSE);
15579 		}
15580 
15581 		/*
15582 		 * make sure all ipifs are down and there are no active
15583 		 * references. Call to ipsq_pending_mp_add will not fail
15584 		 * since connp is NULL.
15585 		 */
15586 		if (ill_v4 != NULL) {
15587 			mutex_enter(&ill_v4->ill_lock);
15588 			if (!ill_is_quiescent(ill_v4)) {
15589 				(void) ipsq_pending_mp_add(NULL,
15590 				    ill_v4->ill_ipif, q, mp, ILL_DOWN);
15591 				mutex_exit(&ill_v4->ill_lock);
15592 				err = EINPROGRESS;
15593 				goto done;
15594 			}
15595 			mutex_exit(&ill_v4->ill_lock);
15596 		}
15597 
15598 		if (ill_v6 != NULL) {
15599 			mutex_enter(&ill_v6->ill_lock);
15600 			if (!ill_is_quiescent(ill_v6)) {
15601 				(void) ipsq_pending_mp_add(NULL,
15602 				    ill_v6->ill_ipif, q, mp, ILL_DOWN);
15603 				mutex_exit(&ill_v6->ill_lock);
15604 				err = EINPROGRESS;
15605 				goto done;
15606 			}
15607 			mutex_exit(&ill_v6->ill_lock);
15608 		}
15609 
15610 		/*
15611 		 * allocate including space for null terminator
15612 		 * before we insert.
15613 		 */
15614 		tmp = (char *)mi_alloc(namelen + 1, BPRI_MED);
15615 		if (tmp == NULL)
15616 			return (ENOMEM);
15617 
15618 		rw_enter(&ill_g_lock, RW_WRITER);
15619 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15620 		mutex_enter(&phyi->phyint_lock);
15621 		if (phyi->phyint_groupname_len != 0) {
15622 			ASSERT(phyi->phyint_groupname != NULL);
15623 			mi_free(phyi->phyint_groupname);
15624 		}
15625 
15626 		/*
15627 		 * setup the new group name.
15628 		 */
15629 		phyi->phyint_groupname = tmp;
15630 		bcopy(groupname, phyi->phyint_groupname, namelen + 1);
15631 		phyi->phyint_groupname_len = namelen + 1;
15632 		mutex_exit(&phyi->phyint_lock);
15633 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15634 		rw_exit(&ill_g_lock);
15635 
15636 		err = ill_up_ipifs(ill, q, mp);
15637 	}
15638 
15639 done:
15640 	/*
15641 	 *  normally ILL_CHANGING is cleared in ill_up_ipifs.
15642 	 */
15643 	if (err != EINPROGRESS) {
15644 		GRAB_ILL_LOCKS(ill_v4, ill_v6);
15645 		if (ill_v4 != NULL)
15646 			ill_v4->ill_state_flags &= ~ILL_CHANGING;
15647 		if (ill_v6 != NULL)
15648 			ill_v6->ill_state_flags &= ~ILL_CHANGING;
15649 		RELEASE_ILL_LOCKS(ill_v4, ill_v6);
15650 	}
15651 	return (err);
15652 }
15653 
15654 /* ARGSUSED */
15655 int
15656 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
15657     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15658 {
15659 	ill_t *ill;
15660 	phyint_t *phyi;
15661 	struct lifreq *lifr;
15662 	mblk_t	*mp1;
15663 
15664 	/* Existence verified in ip_wput_nondata */
15665 	mp1 = mp->b_cont->b_cont;
15666 	lifr = (struct lifreq *)mp1->b_rptr;
15667 	ill = ipif->ipif_ill;
15668 	phyi = ill->ill_phyint;
15669 
15670 	lifr->lifr_groupname[0] = '\0';
15671 	/*
15672 	 * ill_group may be null if all the interfaces
15673 	 * are down. But still, the phyint should always
15674 	 * hold the name.
15675 	 */
15676 	if (phyi->phyint_groupname_len != 0) {
15677 		bcopy(phyi->phyint_groupname, lifr->lifr_groupname,
15678 		    phyi->phyint_groupname_len);
15679 	}
15680 
15681 	return (0);
15682 }
15683 
15684 
15685 typedef struct conn_move_s {
15686 	ill_t	*cm_from_ill;
15687 	ill_t	*cm_to_ill;
15688 	int	cm_ifindex;
15689 } conn_move_t;
15690 
15691 /*
15692  * ipcl_walk function for moving conn_multicast_ill for a given ill.
15693  */
15694 static void
15695 conn_move(conn_t *connp, caddr_t arg)
15696 {
15697 	conn_move_t *connm;
15698 	int ifindex;
15699 	int i;
15700 	ill_t *from_ill;
15701 	ill_t *to_ill;
15702 	ilg_t *ilg;
15703 	ilm_t *ret_ilm;
15704 
15705 	connm = (conn_move_t *)arg;
15706 	ifindex = connm->cm_ifindex;
15707 	from_ill = connm->cm_from_ill;
15708 	to_ill = connm->cm_to_ill;
15709 
15710 	/* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */
15711 
15712 	/* All multicast fields protected by conn_lock */
15713 	mutex_enter(&connp->conn_lock);
15714 	ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill);
15715 	if ((connp->conn_outgoing_ill == from_ill) &&
15716 	    (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) {
15717 		connp->conn_outgoing_ill = to_ill;
15718 		connp->conn_incoming_ill = to_ill;
15719 	}
15720 
15721 	/* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */
15722 
15723 	if ((connp->conn_multicast_ill == from_ill) &&
15724 	    (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) {
15725 		connp->conn_multicast_ill = connm->cm_to_ill;
15726 	}
15727 
15728 	/* Change IP_XMIT_IF associations */
15729 	if ((connp->conn_xmit_if_ill == from_ill) &&
15730 	    (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) {
15731 		connp->conn_xmit_if_ill = to_ill;
15732 	}
15733 	/*
15734 	 * Change the ilg_ill to point to the new one. This assumes
15735 	 * ilm_move_v6 has moved the ilms to new_ill and the driver
15736 	 * has been told to receive packets on this interface.
15737 	 * ilm_move_v6 FAILBACKS all the ilms successfully always.
15738 	 * But when doing a FAILOVER, it might fail with ENOMEM and so
15739 	 * some ilms may not have moved. We check to see whether
15740 	 * the ilms have moved to to_ill. We can't check on from_ill
15741 	 * as in the process of moving, we could have split an ilm
15742 	 * in to two - which has the same orig_ifindex and v6group.
15743 	 *
15744 	 * For IPv4, ilg_ipif moves implicitly. The code below really
15745 	 * does not do anything for IPv4 as ilg_ill is NULL for IPv4.
15746 	 */
15747 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
15748 		ilg = &connp->conn_ilg[i];
15749 		if ((ilg->ilg_ill == from_ill) &&
15750 		    (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) {
15751 			/* ifindex != 0 indicates failback */
15752 			if (ifindex != 0) {
15753 				connp->conn_ilg[i].ilg_ill = to_ill;
15754 				continue;
15755 			}
15756 
15757 			ret_ilm = ilm_lookup_ill_index_v6(to_ill,
15758 			    &ilg->ilg_v6group, ilg->ilg_orig_ifindex,
15759 			    connp->conn_zoneid);
15760 
15761 			if (ret_ilm != NULL)
15762 				connp->conn_ilg[i].ilg_ill = to_ill;
15763 		}
15764 	}
15765 	mutex_exit(&connp->conn_lock);
15766 }
15767 
15768 static void
15769 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex)
15770 {
15771 	conn_move_t connm;
15772 
15773 	connm.cm_from_ill = from_ill;
15774 	connm.cm_to_ill = to_ill;
15775 	connm.cm_ifindex = ifindex;
15776 
15777 	ipcl_walk(conn_move, (caddr_t)&connm);
15778 }
15779 
15780 /*
15781  * ilm has been moved from from_ill to to_ill.
15782  * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill.
15783  * appropriately.
15784  *
15785  * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because
15786  *	  the code there de-references ipif_ill to get the ill to
15787  *	  send multicast requests. It does not work as ipif is on its
15788  *	  move and already moved when this function is called.
15789  *	  Thus, we need to use from_ill and to_ill send down multicast
15790  *	  requests.
15791  */
15792 static void
15793 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill)
15794 {
15795 	ipif_t *ipif;
15796 	ilm_t *ilm;
15797 
15798 	/*
15799 	 * See whether we need to send down DL_ENABMULTI_REQ on
15800 	 * to_ill as ilm has just been added.
15801 	 */
15802 	ASSERT(IAM_WRITER_ILL(to_ill));
15803 	ASSERT(IAM_WRITER_ILL(from_ill));
15804 
15805 	ILM_WALKER_HOLD(to_ill);
15806 	for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15807 
15808 		if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED))
15809 			continue;
15810 		/*
15811 		 * no locks held, ill/ipif cannot dissappear as long
15812 		 * as we are writer.
15813 		 */
15814 		ipif = to_ill->ill_ipif;
15815 		/*
15816 		 * No need to hold any lock as we are the writer and this
15817 		 * can only be changed by a writer.
15818 		 */
15819 		ilm->ilm_is_new = B_FALSE;
15820 
15821 		if (to_ill->ill_net_type != IRE_IF_RESOLVER ||
15822 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15823 			ip1dbg(("ilm_send_multicast_reqs: to_ill not "
15824 			    "resolver\n"));
15825 			continue;		/* Must be IRE_IF_NORESOLVER */
15826 		}
15827 
15828 
15829 		if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15830 			ip1dbg(("ilm_send_multicast_reqs: "
15831 			    "to_ill MULTI_BCAST\n"));
15832 			goto from;
15833 		}
15834 
15835 		if (to_ill->ill_isv6)
15836 			mld_joingroup(ilm);
15837 		else
15838 			igmp_joingroup(ilm);
15839 
15840 		if (to_ill->ill_ipif_up_count == 0) {
15841 			/*
15842 			 * Nobody there. All multicast addresses will be
15843 			 * re-joined when we get the DL_BIND_ACK bringing the
15844 			 * interface up.
15845 			 */
15846 			ilm->ilm_notify_driver = B_FALSE;
15847 			ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n"));
15848 			goto from;
15849 		}
15850 
15851 		/*
15852 		 * For allmulti address, we want to join on only one interface.
15853 		 * Checking for ilm_numentries_v6 is not correct as you may
15854 		 * find an ilm with zero address on to_ill, but we may not
15855 		 * have nominated to_ill for receiving. Thus, if we have
15856 		 * nominated from_ill (ill_join_allmulti is set), nominate
15857 		 * only if to_ill is not already nominated (to_ill normally
15858 		 * should not have been nominated if "from_ill" has already
15859 		 * been nominated. As we don't prevent failovers from happening
15860 		 * across groups, we don't assert).
15861 		 */
15862 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15863 			/*
15864 			 * There is no need to hold ill locks as we are
15865 			 * writer on both ills and when ill_join_allmulti
15866 			 * is changed the thread is always a writer.
15867 			 */
15868 			if (from_ill->ill_join_allmulti &&
15869 			    !to_ill->ill_join_allmulti) {
15870 				(void) ip_join_allmulti(to_ill->ill_ipif);
15871 			}
15872 		} else if (ilm->ilm_notify_driver) {
15873 
15874 			/*
15875 			 * This is a newly moved ilm so we need to tell the
15876 			 * driver about the new group. There can be more than
15877 			 * one ilm's for the same group in the list each with a
15878 			 * different orig_ifindex. We have to inform the driver
15879 			 * once. In ilm_move_v[4,6] we only set the flag
15880 			 * ilm_notify_driver for the first ilm.
15881 			 */
15882 
15883 			(void) ip_ll_send_enabmulti_req(to_ill,
15884 			    &ilm->ilm_v6addr);
15885 		}
15886 
15887 		ilm->ilm_notify_driver = B_FALSE;
15888 
15889 		/*
15890 		 * See whether we need to send down DL_DISABMULTI_REQ on
15891 		 * from_ill as ilm has just been removed.
15892 		 */
15893 from:
15894 		ipif = from_ill->ill_ipif;
15895 		if (from_ill->ill_net_type != IRE_IF_RESOLVER ||
15896 		    ipif->ipif_flags & IPIF_POINTOPOINT) {
15897 			ip1dbg(("ilm_send_multicast_reqs: "
15898 			    "from_ill not resolver\n"));
15899 			continue;		/* Must be IRE_IF_NORESOLVER */
15900 		}
15901 
15902 		if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
15903 			ip1dbg(("ilm_send_multicast_reqs: "
15904 			    "from_ill MULTI_BCAST\n"));
15905 			continue;
15906 		}
15907 
15908 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
15909 			if (from_ill->ill_join_allmulti)
15910 			    (void) ip_leave_allmulti(from_ill->ill_ipif);
15911 		} else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) {
15912 			(void) ip_ll_send_disabmulti_req(from_ill,
15913 		    &ilm->ilm_v6addr);
15914 		}
15915 	}
15916 	ILM_WALKER_RELE(to_ill);
15917 }
15918 
15919 /*
15920  * This function is called when all multicast memberships needs
15921  * to be moved from "from_ill" to "to_ill" for IPv6. This function is
15922  * called only once unlike the IPv4 counterpart where it is called after
15923  * every logical interface is moved. The reason is due to multicast
15924  * memberships are joined using an interface address in IPv4 while in
15925  * IPv6, interface index is used.
15926  */
15927 static void
15928 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex)
15929 {
15930 	ilm_t	*ilm;
15931 	ilm_t	*ilm_next;
15932 	ilm_t	*new_ilm;
15933 	ilm_t	**ilmp;
15934 	int	count;
15935 	char buf[INET6_ADDRSTRLEN];
15936 	in6_addr_t ipv6_snm = ipv6_solicited_node_mcast;
15937 
15938 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
15939 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
15940 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
15941 
15942 	if (ifindex == 0) {
15943 		/*
15944 		 * Form the solicited node mcast address which is used later.
15945 		 */
15946 		ipif_t *ipif;
15947 
15948 		ipif = from_ill->ill_ipif;
15949 		ASSERT(ipif->ipif_id == 0);
15950 
15951 		ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
15952 	}
15953 
15954 	ilmp = &from_ill->ill_ilm;
15955 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
15956 		ilm_next = ilm->ilm_next;
15957 
15958 		if (ilm->ilm_flags & ILM_DELETED) {
15959 			ilmp = &ilm->ilm_next;
15960 			continue;
15961 		}
15962 
15963 		new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr,
15964 		    ilm->ilm_orig_ifindex, ilm->ilm_zoneid);
15965 		ASSERT(ilm->ilm_orig_ifindex != 0);
15966 		if (ilm->ilm_orig_ifindex == ifindex) {
15967 			/*
15968 			 * We are failing back multicast memberships.
15969 			 * If the same ilm exists in to_ill, it means somebody
15970 			 * has joined the same group there e.g. ff02::1
15971 			 * is joined within the kernel when the interfaces
15972 			 * came UP.
15973 			 */
15974 			ASSERT(ilm->ilm_ipif == NULL);
15975 			if (new_ilm != NULL) {
15976 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
15977 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
15978 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
15979 					new_ilm->ilm_is_new = B_TRUE;
15980 				}
15981 			} else {
15982 				/*
15983 				 * check if we can just move the ilm
15984 				 */
15985 				if (from_ill->ill_ilm_walker_cnt != 0) {
15986 					/*
15987 					 * We have walkers we cannot move
15988 					 * the ilm, so allocate a new ilm,
15989 					 * this (old) ilm will be marked
15990 					 * ILM_DELETED at the end of the loop
15991 					 * and will be freed when the
15992 					 * last walker exits.
15993 					 */
15994 					new_ilm = (ilm_t *)mi_zalloc
15995 					    (sizeof (ilm_t));
15996 					if (new_ilm == NULL) {
15997 						ip0dbg(("ilm_move_v6: "
15998 						    "FAILBACK of IPv6"
15999 						    " multicast address %s : "
16000 						    "from %s to"
16001 						    " %s failed : ENOMEM \n",
16002 						    inet_ntop(AF_INET6,
16003 						    &ilm->ilm_v6addr, buf,
16004 						    sizeof (buf)),
16005 						    from_ill->ill_name,
16006 						    to_ill->ill_name));
16007 
16008 							ilmp = &ilm->ilm_next;
16009 							continue;
16010 					}
16011 					*new_ilm = *ilm;
16012 					/*
16013 					 * we don't want new_ilm linked to
16014 					 * ilm's filter list.
16015 					 */
16016 					new_ilm->ilm_filter = NULL;
16017 				} else {
16018 					/*
16019 					 * No walkers we can move the ilm.
16020 					 * lets take it out of the list.
16021 					 */
16022 					*ilmp = ilm->ilm_next;
16023 					ilm->ilm_next = NULL;
16024 					new_ilm = ilm;
16025 				}
16026 
16027 				/*
16028 				 * if this is the first ilm for the group
16029 				 * set ilm_notify_driver so that we notify the
16030 				 * driver in ilm_send_multicast_reqs.
16031 				 */
16032 				if (ilm_lookup_ill_v6(to_ill,
16033 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16034 					new_ilm->ilm_notify_driver = B_TRUE;
16035 
16036 				new_ilm->ilm_ill = to_ill;
16037 				/* Add to the to_ill's list */
16038 				new_ilm->ilm_next = to_ill->ill_ilm;
16039 				to_ill->ill_ilm = new_ilm;
16040 				/*
16041 				 * set the flag so that mld_joingroup is
16042 				 * called in ilm_send_multicast_reqs().
16043 				 */
16044 				new_ilm->ilm_is_new = B_TRUE;
16045 			}
16046 			goto bottom;
16047 		} else if (ifindex != 0) {
16048 			/*
16049 			 * If this is FAILBACK (ifindex != 0) and the ifindex
16050 			 * has not matched above, look at the next ilm.
16051 			 */
16052 			ilmp = &ilm->ilm_next;
16053 			continue;
16054 		}
16055 		/*
16056 		 * If we are here, it means ifindex is 0. Failover
16057 		 * everything.
16058 		 *
16059 		 * We need to handle solicited node mcast address
16060 		 * and all_nodes mcast address differently as they
16061 		 * are joined witin the kenrel (ipif_multicast_up)
16062 		 * and potentially from the userland. We are called
16063 		 * after the ipifs of from_ill has been moved.
16064 		 * If we still find ilms on ill with solicited node
16065 		 * mcast address or all_nodes mcast address, it must
16066 		 * belong to the UP interface that has not moved e.g.
16067 		 * ipif_id 0 with the link local prefix does not move.
16068 		 * We join this on the new ill accounting for all the
16069 		 * userland memberships so that applications don't
16070 		 * see any failure.
16071 		 *
16072 		 * We need to make sure that we account only for the
16073 		 * solicited node and all node multicast addresses
16074 		 * that was brought UP on these. In the case of
16075 		 * a failover from A to B, we might have ilms belonging
16076 		 * to A (ilm_orig_ifindex pointing at A) on B accounting
16077 		 * for the membership from the userland. If we are failing
16078 		 * over from B to C now, we will find the ones belonging
16079 		 * to A on B. These don't account for the ill_ipif_up_count.
16080 		 * They just move from B to C. The check below on
16081 		 * ilm_orig_ifindex ensures that.
16082 		 */
16083 		if ((ilm->ilm_orig_ifindex ==
16084 		    from_ill->ill_phyint->phyint_ifindex) &&
16085 		    (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) ||
16086 		    IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast,
16087 		    &ilm->ilm_v6addr))) {
16088 			ASSERT(ilm->ilm_refcnt > 0);
16089 			count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count;
16090 			/*
16091 			 * For indentation reasons, we are not using a
16092 			 * "else" here.
16093 			 */
16094 			if (count == 0) {
16095 				ilmp = &ilm->ilm_next;
16096 				continue;
16097 			}
16098 			ilm->ilm_refcnt -= count;
16099 			if (new_ilm != NULL) {
16100 				/*
16101 				 * Can find one with the same
16102 				 * ilm_orig_ifindex, if we are failing
16103 				 * over to a STANDBY. This happens
16104 				 * when somebody wants to join a group
16105 				 * on a STANDBY interface and we
16106 				 * internally join on a different one.
16107 				 * If we had joined on from_ill then, a
16108 				 * failover now will find a new ilm
16109 				 * with this index.
16110 				 */
16111 				ip1dbg(("ilm_move_v6: FAILOVER, found"
16112 				    " new ilm on %s, group address %s\n",
16113 				    to_ill->ill_name,
16114 				    inet_ntop(AF_INET6,
16115 				    &ilm->ilm_v6addr, buf,
16116 				    sizeof (buf))));
16117 				new_ilm->ilm_refcnt += count;
16118 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16119 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16120 					new_ilm->ilm_is_new = B_TRUE;
16121 				}
16122 			} else {
16123 				new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16124 				if (new_ilm == NULL) {
16125 					ip0dbg(("ilm_move_v6: FAILOVER of IPv6"
16126 					    " multicast address %s : from %s to"
16127 					    " %s failed : ENOMEM \n",
16128 					    inet_ntop(AF_INET6,
16129 					    &ilm->ilm_v6addr, buf,
16130 					    sizeof (buf)), from_ill->ill_name,
16131 					    to_ill->ill_name));
16132 					ilmp = &ilm->ilm_next;
16133 					continue;
16134 				}
16135 				*new_ilm = *ilm;
16136 				new_ilm->ilm_filter = NULL;
16137 				new_ilm->ilm_refcnt = count;
16138 				new_ilm->ilm_timer = INFINITY;
16139 				new_ilm->ilm_rtx.rtx_timer = INFINITY;
16140 				new_ilm->ilm_is_new = B_TRUE;
16141 				/*
16142 				 * If the to_ill has not joined this
16143 				 * group we need to tell the driver in
16144 				 * ill_send_multicast_reqs.
16145 				 */
16146 				if (ilm_lookup_ill_v6(to_ill,
16147 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16148 					new_ilm->ilm_notify_driver = B_TRUE;
16149 
16150 				new_ilm->ilm_ill = to_ill;
16151 				/* Add to the to_ill's list */
16152 				new_ilm->ilm_next = to_ill->ill_ilm;
16153 				to_ill->ill_ilm = new_ilm;
16154 				ASSERT(new_ilm->ilm_ipif == NULL);
16155 			}
16156 			if (ilm->ilm_refcnt == 0) {
16157 				goto bottom;
16158 			} else {
16159 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16160 				CLEAR_SLIST(new_ilm->ilm_filter);
16161 				ilmp = &ilm->ilm_next;
16162 			}
16163 			continue;
16164 		} else {
16165 			/*
16166 			 * ifindex = 0 means, move everything pointing at
16167 			 * from_ill. We are doing this becuase ill has
16168 			 * either FAILED or became INACTIVE.
16169 			 *
16170 			 * As we would like to move things later back to
16171 			 * from_ill, we want to retain the identity of this
16172 			 * ilm. Thus, we don't blindly increment the reference
16173 			 * count on the ilms matching the address alone. We
16174 			 * need to match on the ilm_orig_index also. new_ilm
16175 			 * was obtained by matching ilm_orig_index also.
16176 			 */
16177 			if (new_ilm != NULL) {
16178 				/*
16179 				 * This is possible only if a previous restore
16180 				 * was incomplete i.e restore to
16181 				 * ilm_orig_ifindex left some ilms because
16182 				 * of some failures. Thus when we are failing
16183 				 * again, we might find our old friends there.
16184 				 */
16185 				ip1dbg(("ilm_move_v6: FAILOVER, found new ilm"
16186 				    " on %s, group address %s\n",
16187 				    to_ill->ill_name,
16188 				    inet_ntop(AF_INET6,
16189 				    &ilm->ilm_v6addr, buf,
16190 				    sizeof (buf))));
16191 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16192 				if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE ||
16193 				    !SLIST_IS_EMPTY(new_ilm->ilm_filter)) {
16194 					new_ilm->ilm_is_new = B_TRUE;
16195 				}
16196 			} else {
16197 				if (from_ill->ill_ilm_walker_cnt != 0) {
16198 					new_ilm = (ilm_t *)
16199 					    mi_zalloc(sizeof (ilm_t));
16200 					if (new_ilm == NULL) {
16201 						ip0dbg(("ilm_move_v6: "
16202 						    "FAILOVER of IPv6"
16203 						    " multicast address %s : "
16204 						    "from %s to"
16205 						    " %s failed : ENOMEM \n",
16206 						    inet_ntop(AF_INET6,
16207 						    &ilm->ilm_v6addr, buf,
16208 						    sizeof (buf)),
16209 						    from_ill->ill_name,
16210 						    to_ill->ill_name));
16211 
16212 							ilmp = &ilm->ilm_next;
16213 							continue;
16214 					}
16215 					*new_ilm = *ilm;
16216 					new_ilm->ilm_filter = NULL;
16217 				} else {
16218 					*ilmp = ilm->ilm_next;
16219 					new_ilm = ilm;
16220 				}
16221 				/*
16222 				 * If the to_ill has not joined this
16223 				 * group we need to tell the driver in
16224 				 * ill_send_multicast_reqs.
16225 				 */
16226 				if (ilm_lookup_ill_v6(to_ill,
16227 				    &new_ilm->ilm_v6addr, ALL_ZONES) == NULL)
16228 					new_ilm->ilm_notify_driver = B_TRUE;
16229 
16230 				/* Add to the to_ill's list */
16231 				new_ilm->ilm_next = to_ill->ill_ilm;
16232 				to_ill->ill_ilm = new_ilm;
16233 				ASSERT(ilm->ilm_ipif == NULL);
16234 				new_ilm->ilm_ill = to_ill;
16235 				new_ilm->ilm_is_new = B_TRUE;
16236 			}
16237 
16238 		}
16239 
16240 bottom:
16241 		/*
16242 		 * Revert multicast filter state to (EXCLUDE, NULL).
16243 		 * new_ilm->ilm_is_new should already be set if needed.
16244 		 */
16245 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16246 		CLEAR_SLIST(new_ilm->ilm_filter);
16247 		/*
16248 		 * We allocated/got a new ilm, free the old one.
16249 		 */
16250 		if (new_ilm != ilm) {
16251 			if (from_ill->ill_ilm_walker_cnt == 0) {
16252 				*ilmp = ilm->ilm_next;
16253 				ilm->ilm_next = NULL;
16254 				FREE_SLIST(ilm->ilm_filter);
16255 				FREE_SLIST(ilm->ilm_pendsrcs);
16256 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16257 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16258 				mi_free((char *)ilm);
16259 			} else {
16260 				ilm->ilm_flags |= ILM_DELETED;
16261 				from_ill->ill_ilm_cleanup_reqd = 1;
16262 				ilmp = &ilm->ilm_next;
16263 			}
16264 		}
16265 	}
16266 }
16267 
16268 /*
16269  * Move all the multicast memberships to to_ill. Called when
16270  * an ipif moves from "from_ill" to "to_ill". This function is slightly
16271  * different from IPv6 counterpart as multicast memberships are associated
16272  * with ills in IPv6. This function is called after every ipif is moved
16273  * unlike IPv6, where it is moved only once.
16274  */
16275 static void
16276 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif)
16277 {
16278 	ilm_t	*ilm;
16279 	ilm_t	*ilm_next;
16280 	ilm_t	*new_ilm;
16281 	ilm_t	**ilmp;
16282 
16283 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16284 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16285 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16286 
16287 	ilmp = &from_ill->ill_ilm;
16288 	for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) {
16289 		ilm_next = ilm->ilm_next;
16290 
16291 		if (ilm->ilm_flags & ILM_DELETED) {
16292 			ilmp = &ilm->ilm_next;
16293 			continue;
16294 		}
16295 
16296 		ASSERT(ilm->ilm_ipif != NULL);
16297 
16298 		if (ilm->ilm_ipif != ipif) {
16299 			ilmp = &ilm->ilm_next;
16300 			continue;
16301 		}
16302 
16303 		if (V4_PART_OF_V6(ilm->ilm_v6addr) ==
16304 		    htonl(INADDR_ALLHOSTS_GROUP)) {
16305 			/*
16306 			 * We joined this in ipif_multicast_up
16307 			 * and we never did an ipif_multicast_down
16308 			 * for IPv4. If nobody else from the userland
16309 			 * has reference, we free the ilm, and later
16310 			 * when this ipif comes up on the new ill,
16311 			 * we will join this again.
16312 			 */
16313 			if (--ilm->ilm_refcnt == 0)
16314 				goto delete_ilm;
16315 
16316 			new_ilm = ilm_lookup_ipif(ipif,
16317 			    V4_PART_OF_V6(ilm->ilm_v6addr));
16318 			if (new_ilm != NULL) {
16319 				new_ilm->ilm_refcnt += ilm->ilm_refcnt;
16320 				/*
16321 				 * We still need to deal with the from_ill.
16322 				 */
16323 				new_ilm->ilm_is_new = B_TRUE;
16324 				new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16325 				CLEAR_SLIST(new_ilm->ilm_filter);
16326 				goto delete_ilm;
16327 			}
16328 			/*
16329 			 * If we could not find one e.g. ipif is
16330 			 * still down on to_ill, we add this ilm
16331 			 * on ill_new to preserve the reference
16332 			 * count.
16333 			 */
16334 		}
16335 		/*
16336 		 * When ipifs move, ilms always move with it
16337 		 * to the NEW ill. Thus we should never be
16338 		 * able to find ilm till we really move it here.
16339 		 */
16340 		ASSERT(ilm_lookup_ipif(ipif,
16341 		    V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL);
16342 
16343 		if (from_ill->ill_ilm_walker_cnt != 0) {
16344 			new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t));
16345 			if (new_ilm == NULL) {
16346 				char buf[INET6_ADDRSTRLEN];
16347 				ip0dbg(("ilm_move_v4: FAILBACK of IPv4"
16348 				    " multicast address %s : "
16349 				    "from %s to"
16350 				    " %s failed : ENOMEM \n",
16351 				    inet_ntop(AF_INET,
16352 				    &ilm->ilm_v6addr, buf,
16353 				    sizeof (buf)),
16354 				    from_ill->ill_name,
16355 				    to_ill->ill_name));
16356 
16357 				ilmp = &ilm->ilm_next;
16358 				continue;
16359 			}
16360 			*new_ilm = *ilm;
16361 			/* We don't want new_ilm linked to ilm's filter list */
16362 			new_ilm->ilm_filter = NULL;
16363 		} else {
16364 			/* Remove from the list */
16365 			*ilmp = ilm->ilm_next;
16366 			new_ilm = ilm;
16367 		}
16368 
16369 		/*
16370 		 * If we have never joined this group on the to_ill
16371 		 * make sure we tell the driver.
16372 		 */
16373 		if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr,
16374 		    ALL_ZONES) == NULL)
16375 			new_ilm->ilm_notify_driver = B_TRUE;
16376 
16377 		/* Add to the to_ill's list */
16378 		new_ilm->ilm_next = to_ill->ill_ilm;
16379 		to_ill->ill_ilm = new_ilm;
16380 		new_ilm->ilm_is_new = B_TRUE;
16381 
16382 		/*
16383 		 * Revert multicast filter state to (EXCLUDE, NULL)
16384 		 */
16385 		new_ilm->ilm_fmode = MODE_IS_EXCLUDE;
16386 		CLEAR_SLIST(new_ilm->ilm_filter);
16387 
16388 		/*
16389 		 * Delete only if we have allocated a new ilm.
16390 		 */
16391 		if (new_ilm != ilm) {
16392 delete_ilm:
16393 			if (from_ill->ill_ilm_walker_cnt == 0) {
16394 				/* Remove from the list */
16395 				*ilmp = ilm->ilm_next;
16396 				ilm->ilm_next = NULL;
16397 				FREE_SLIST(ilm->ilm_filter);
16398 				FREE_SLIST(ilm->ilm_pendsrcs);
16399 				FREE_SLIST(ilm->ilm_rtx.rtx_allow);
16400 				FREE_SLIST(ilm->ilm_rtx.rtx_block);
16401 				mi_free((char *)ilm);
16402 			} else {
16403 				ilm->ilm_flags |= ILM_DELETED;
16404 				from_ill->ill_ilm_cleanup_reqd = 1;
16405 				ilmp = &ilm->ilm_next;
16406 			}
16407 		}
16408 	}
16409 }
16410 
16411 static uint_t
16412 ipif_get_id(ill_t *ill, uint_t id)
16413 {
16414 	uint_t	unit;
16415 	ipif_t	*tipif;
16416 	boolean_t found = B_FALSE;
16417 
16418 	/*
16419 	 * During failback, we want to go back to the same id
16420 	 * instead of the smallest id so that the original
16421 	 * configuration is maintained. id is non-zero in that
16422 	 * case.
16423 	 */
16424 	if (id != 0) {
16425 		/*
16426 		 * While failing back, if we still have an ipif with
16427 		 * MAX_ADDRS_PER_IF, it means this will be replaced
16428 		 * as soon as we return from this function. It was
16429 		 * to set to MAX_ADDRS_PER_IF by the caller so that
16430 		 * we can choose the smallest id. Thus we return zero
16431 		 * in that case ignoring the hint.
16432 		 */
16433 		if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF)
16434 			return (0);
16435 		for (tipif = ill->ill_ipif; tipif != NULL;
16436 		    tipif = tipif->ipif_next) {
16437 			if (tipif->ipif_id == id) {
16438 				found = B_TRUE;
16439 				break;
16440 			}
16441 		}
16442 		/*
16443 		 * If somebody already plumbed another logical
16444 		 * with the same id, we won't be able to find it.
16445 		 */
16446 		if (!found)
16447 			return (id);
16448 	}
16449 	for (unit = 0; unit <= ip_addrs_per_if; unit++) {
16450 		found = B_FALSE;
16451 		for (tipif = ill->ill_ipif; tipif != NULL;
16452 		    tipif = tipif->ipif_next) {
16453 			if (tipif->ipif_id == unit) {
16454 				found = B_TRUE;
16455 				break;
16456 			}
16457 		}
16458 		if (!found)
16459 			break;
16460 	}
16461 	return (unit);
16462 }
16463 
16464 /* ARGSUSED */
16465 static int
16466 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp,
16467     ipif_t **rep_ipif_ptr)
16468 {
16469 	ill_t	*from_ill;
16470 	ipif_t	*rep_ipif;
16471 	ipif_t	**ipifp;
16472 	uint_t	unit;
16473 	int err = 0;
16474 	ipif_t	*to_ipif;
16475 	struct iocblk	*iocp;
16476 	boolean_t failback_cmd;
16477 	boolean_t remove_ipif;
16478 	int	rc;
16479 
16480 	ASSERT(IAM_WRITER_ILL(to_ill));
16481 	ASSERT(IAM_WRITER_IPIF(ipif));
16482 
16483 	iocp = (struct iocblk *)mp->b_rptr;
16484 	failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK);
16485 	remove_ipif = B_FALSE;
16486 
16487 	from_ill = ipif->ipif_ill;
16488 
16489 	ASSERT(MUTEX_HELD(&to_ill->ill_lock));
16490 	ASSERT(MUTEX_HELD(&from_ill->ill_lock));
16491 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
16492 
16493 	/*
16494 	 * Don't move LINK LOCAL addresses as they are tied to
16495 	 * physical interface.
16496 	 */
16497 	if (from_ill->ill_isv6 &&
16498 	    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) {
16499 		ipif->ipif_was_up = B_FALSE;
16500 		IPIF_UNMARK_MOVING(ipif);
16501 		return (0);
16502 	}
16503 
16504 	/*
16505 	 * We set the ipif_id to maximum so that the search for
16506 	 * ipif_id will pick the lowest number i.e 0 in the
16507 	 * following 2 cases :
16508 	 *
16509 	 * 1) We have a replacement ipif at the head of to_ill.
16510 	 *    We can't remove it yet as we can exceed ip_addrs_per_if
16511 	 *    on to_ill and hence the MOVE might fail. We want to
16512 	 *    remove it only if we could move the ipif. Thus, by
16513 	 *    setting it to the MAX value, we make the search in
16514 	 *    ipif_get_id return the zeroth id.
16515 	 *
16516 	 * 2) When DR pulls out the NIC and re-plumbs the interface,
16517 	 *    we might just have a zero address plumbed on the ipif
16518 	 *    with zero id in the case of IPv4. We remove that while
16519 	 *    doing the failback. We want to remove it only if we
16520 	 *    could move the ipif. Thus, by setting it to the MAX
16521 	 *    value, we make the search in ipif_get_id return the
16522 	 *    zeroth id.
16523 	 *
16524 	 * Both (1) and (2) are done only when when we are moving
16525 	 * an ipif (either due to failover/failback) which originally
16526 	 * belonged to this interface i.e the ipif_orig_ifindex is
16527 	 * the same as to_ill's ifindex. This is needed so that
16528 	 * FAILOVER from A -> B ( A failed) followed by FAILOVER
16529 	 * from B -> A (B is being removed from the group) and
16530 	 * FAILBACK from A -> B restores the original configuration.
16531 	 * Without the check for orig_ifindex, the second FAILOVER
16532 	 * could make the ipif belonging to B replace the A's zeroth
16533 	 * ipif and the subsequent failback re-creating the replacement
16534 	 * ipif again.
16535 	 *
16536 	 * NOTE : We created the replacement ipif when we did a
16537 	 * FAILOVER (See below). We could check for FAILBACK and
16538 	 * then look for replacement ipif to be removed. But we don't
16539 	 * want to do that because we wan't to allow the possibility
16540 	 * of a FAILOVER from A -> B (which creates the replacement ipif),
16541 	 * followed by a *FAILOVER* from B -> A instead of a FAILBACK
16542 	 * from B -> A.
16543 	 */
16544 	to_ipif = to_ill->ill_ipif;
16545 	if ((to_ill->ill_phyint->phyint_ifindex ==
16546 	    ipif->ipif_orig_ifindex) &&
16547 	    IPIF_REPL_CHECK(to_ipif, failback_cmd)) {
16548 		ASSERT(to_ipif->ipif_id == 0);
16549 		remove_ipif = B_TRUE;
16550 		to_ipif->ipif_id = MAX_ADDRS_PER_IF;
16551 	}
16552 	/*
16553 	 * Find the lowest logical unit number on the to_ill.
16554 	 * If we are failing back, try to get the original id
16555 	 * rather than the lowest one so that the original
16556 	 * configuration is maintained.
16557 	 *
16558 	 * XXX need a better scheme for this.
16559 	 */
16560 	if (failback_cmd) {
16561 		unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid);
16562 	} else {
16563 		unit = ipif_get_id(to_ill, 0);
16564 	}
16565 
16566 	/* Reset back to zero in case we fail below */
16567 	if (to_ipif->ipif_id == MAX_ADDRS_PER_IF)
16568 		to_ipif->ipif_id = 0;
16569 
16570 	if (unit == ip_addrs_per_if) {
16571 		ipif->ipif_was_up = B_FALSE;
16572 		IPIF_UNMARK_MOVING(ipif);
16573 		return (EINVAL);
16574 	}
16575 
16576 	/*
16577 	 * ipif is ready to move from "from_ill" to "to_ill".
16578 	 *
16579 	 * 1) If we are moving ipif with id zero, create a
16580 	 *    replacement ipif for this ipif on from_ill. If this fails
16581 	 *    fail the MOVE operation.
16582 	 *
16583 	 * 2) Remove the replacement ipif on to_ill if any.
16584 	 *    We could remove the replacement ipif when we are moving
16585 	 *    the ipif with id zero. But what if somebody already
16586 	 *    unplumbed it ? Thus we always remove it if it is present.
16587 	 *    We want to do it only if we are sure we are going to
16588 	 *    move the ipif to to_ill which is why there are no
16589 	 *    returns due to error till ipif is linked to to_ill.
16590 	 *    Note that the first ipif that we failback will always
16591 	 *    be zero if it is present.
16592 	 */
16593 	if (ipif->ipif_id == 0) {
16594 		ipaddr_t inaddr_any = INADDR_ANY;
16595 
16596 		rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED);
16597 		if (rep_ipif == NULL) {
16598 			ipif->ipif_was_up = B_FALSE;
16599 			IPIF_UNMARK_MOVING(ipif);
16600 			return (ENOMEM);
16601 		}
16602 		*rep_ipif = ipif_zero;
16603 		/*
16604 		 * Before we put the ipif on the list, store the addresses
16605 		 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR
16606 		 * assumes so. This logic is not any different from what
16607 		 * ipif_allocate does.
16608 		 */
16609 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16610 		    &rep_ipif->ipif_v6lcl_addr);
16611 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16612 		    &rep_ipif->ipif_v6src_addr);
16613 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16614 		    &rep_ipif->ipif_v6subnet);
16615 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16616 		    &rep_ipif->ipif_v6net_mask);
16617 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16618 		    &rep_ipif->ipif_v6brd_addr);
16619 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
16620 		    &rep_ipif->ipif_v6pp_dst_addr);
16621 		/*
16622 		 * We mark IPIF_NOFAILOVER so that this can never
16623 		 * move.
16624 		 */
16625 		rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER;
16626 		rep_ipif->ipif_flags &= ~IPIF_UP;
16627 		rep_ipif->ipif_replace_zero = B_TRUE;
16628 		mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL,
16629 		    MUTEX_DEFAULT, NULL);
16630 		rep_ipif->ipif_id = 0;
16631 		rep_ipif->ipif_ire_type = ipif->ipif_ire_type;
16632 		rep_ipif->ipif_ill = from_ill;
16633 		rep_ipif->ipif_orig_ifindex =
16634 		    from_ill->ill_phyint->phyint_ifindex;
16635 		/* Insert at head */
16636 		rep_ipif->ipif_next = from_ill->ill_ipif;
16637 		from_ill->ill_ipif = rep_ipif;
16638 		/*
16639 		 * We don't really care to let apps know about
16640 		 * this interface.
16641 		 */
16642 	}
16643 
16644 	if (remove_ipif) {
16645 		/*
16646 		 * We set to a max value above for this case to get
16647 		 * id zero. ASSERT that we did get one.
16648 		 */
16649 		ASSERT((to_ipif->ipif_id == 0) && (unit == 0));
16650 		rep_ipif = to_ipif;
16651 		to_ill->ill_ipif = rep_ipif->ipif_next;
16652 		rep_ipif->ipif_next = NULL;
16653 		/*
16654 		 * If some apps scanned and find this interface,
16655 		 * it is time to let them know, so that they can
16656 		 * delete it.
16657 		 */
16658 
16659 		*rep_ipif_ptr = rep_ipif;
16660 	}
16661 
16662 	/* Get it out of the ILL interface list. */
16663 	ipifp = &ipif->ipif_ill->ill_ipif;
16664 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
16665 		if (*ipifp == ipif) {
16666 			*ipifp = ipif->ipif_next;
16667 			break;
16668 		}
16669 	}
16670 
16671 	/* Assign the new ill */
16672 	ipif->ipif_ill = to_ill;
16673 	ipif->ipif_id = unit;
16674 	/* id has already been checked */
16675 	rc = ipif_insert(ipif, B_FALSE, B_FALSE);
16676 	ASSERT(rc == 0);
16677 	/* Let SCTP update its list */
16678 	sctp_move_ipif(ipif, from_ill, to_ill);
16679 	/*
16680 	 * Handle the failover and failback of ipif_t between
16681 	 * ill_t that have differing maximum mtu values.
16682 	 */
16683 	if (ipif->ipif_mtu > to_ill->ill_max_mtu) {
16684 		if (ipif->ipif_saved_mtu == 0) {
16685 			/*
16686 			 * As this ipif_t is moving to an ill_t
16687 			 * that has a lower ill_max_mtu, its
16688 			 * ipif_mtu needs to be saved so it can
16689 			 * be restored during failback or during
16690 			 * failover to an ill_t which has a
16691 			 * higher ill_max_mtu.
16692 			 */
16693 			ipif->ipif_saved_mtu = ipif->ipif_mtu;
16694 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16695 		} else {
16696 			/*
16697 			 * The ipif_t is, once again, moving to
16698 			 * an ill_t that has a lower maximum mtu
16699 			 * value.
16700 			 */
16701 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16702 		}
16703 	} else if (ipif->ipif_mtu < to_ill->ill_max_mtu &&
16704 	    ipif->ipif_saved_mtu != 0) {
16705 		/*
16706 		 * The mtu of this ipif_t had to be reduced
16707 		 * during an earlier failover; this is an
16708 		 * opportunity for it to be increased (either as
16709 		 * part of another failover or a failback).
16710 		 */
16711 		if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) {
16712 			ipif->ipif_mtu = ipif->ipif_saved_mtu;
16713 			ipif->ipif_saved_mtu = 0;
16714 		} else {
16715 			ipif->ipif_mtu = to_ill->ill_max_mtu;
16716 		}
16717 	}
16718 
16719 	/*
16720 	 * We preserve all the other fields of the ipif including
16721 	 * ipif_saved_ire_mp. The routes that are saved here will
16722 	 * be recreated on the new interface and back on the old
16723 	 * interface when we move back.
16724 	 */
16725 	ASSERT(ipif->ipif_arp_del_mp == NULL);
16726 
16727 	return (err);
16728 }
16729 
16730 static int
16731 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp,
16732     int ifindex, ipif_t **rep_ipif_ptr)
16733 {
16734 	ipif_t *mipif;
16735 	ipif_t *ipif_next;
16736 	int err;
16737 
16738 	/*
16739 	 * We don't really try to MOVE back things if some of the
16740 	 * operations fail. The daemon will take care of moving again
16741 	 * later on.
16742 	 */
16743 	for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) {
16744 		ipif_next = mipif->ipif_next;
16745 		if (!(mipif->ipif_flags & IPIF_NOFAILOVER) &&
16746 		    (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) {
16747 
16748 			err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr);
16749 
16750 			/*
16751 			 * When the MOVE fails, it is the job of the
16752 			 * application to take care of this properly
16753 			 * i.e try again if it is ENOMEM.
16754 			 */
16755 			if (mipif->ipif_ill != from_ill) {
16756 				/*
16757 				 * ipif has moved.
16758 				 *
16759 				 * Move the multicast memberships associated
16760 				 * with this ipif to the new ill. For IPv6, we
16761 				 * do it once after all the ipifs are moved
16762 				 * (in ill_move) as they are not associated
16763 				 * with ipifs.
16764 				 *
16765 				 * We need to move the ilms as the ipif has
16766 				 * already been moved to a new ill even
16767 				 * in the case of errors. Neither
16768 				 * ilm_free(ipif) will find the ilm
16769 				 * when somebody unplumbs this ipif nor
16770 				 * ilm_delete(ilm) will be able to find the
16771 				 * ilm, if we don't move now.
16772 				 */
16773 				if (!from_ill->ill_isv6)
16774 					ilm_move_v4(from_ill, to_ill, mipif);
16775 			}
16776 
16777 			if (err != 0)
16778 				return (err);
16779 		}
16780 	}
16781 	return (0);
16782 }
16783 
16784 static int
16785 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp)
16786 {
16787 	int ifindex;
16788 	int err;
16789 	struct iocblk	*iocp;
16790 	ipif_t	*ipif;
16791 	ipif_t *rep_ipif_ptr = NULL;
16792 	ipif_t	*from_ipif = NULL;
16793 	boolean_t check_rep_if = B_FALSE;
16794 
16795 	iocp = (struct iocblk *)mp->b_rptr;
16796 	if (iocp->ioc_cmd == SIOCLIFFAILOVER) {
16797 		/*
16798 		 * Move everything pointing at from_ill to to_ill.
16799 		 * We acheive this by passing in 0 as ifindex.
16800 		 */
16801 		ifindex = 0;
16802 	} else {
16803 		/*
16804 		 * Move everything pointing at from_ill whose original
16805 		 * ifindex of connp, ipif, ilm points at to_ill->ill_index.
16806 		 * We acheive this by passing in ifindex rather than 0.
16807 		 * Multicast vifs, ilgs move implicitly because ipifs move.
16808 		 */
16809 		ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK);
16810 		ifindex = to_ill->ill_phyint->phyint_ifindex;
16811 	}
16812 
16813 	/*
16814 	 * Determine if there is at least one ipif that would move from
16815 	 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement
16816 	 * ipif (if it exists) on the to_ill would be consumed as a result of
16817 	 * the move, in which case we need to quiesce the replacement ipif also.
16818 	 */
16819 	for (from_ipif = from_ill->ill_ipif; from_ipif != NULL;
16820 	    from_ipif = from_ipif->ipif_next) {
16821 		if (((ifindex == 0) ||
16822 		    (ifindex == from_ipif->ipif_orig_ifindex)) &&
16823 		    !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) {
16824 			check_rep_if = B_TRUE;
16825 			break;
16826 		}
16827 	}
16828 
16829 
16830 	ill_down_ipifs(from_ill, mp, ifindex, B_TRUE);
16831 
16832 	GRAB_ILL_LOCKS(from_ill, to_ill);
16833 	if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) {
16834 		(void) ipsq_pending_mp_add(NULL, ipif, q,
16835 		    mp, ILL_MOVE_OK);
16836 		RELEASE_ILL_LOCKS(from_ill, to_ill);
16837 		return (EINPROGRESS);
16838 	}
16839 
16840 	/* Check if the replacement ipif is quiescent to delete */
16841 	if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif,
16842 	    (iocp->ioc_cmd == SIOCLIFFAILBACK))) {
16843 		to_ill->ill_ipif->ipif_state_flags |=
16844 		    IPIF_MOVING | IPIF_CHANGING;
16845 		if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) {
16846 			(void) ipsq_pending_mp_add(NULL, ipif, q,
16847 			    mp, ILL_MOVE_OK);
16848 			RELEASE_ILL_LOCKS(from_ill, to_ill);
16849 			return (EINPROGRESS);
16850 		}
16851 	}
16852 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16853 
16854 	ASSERT(!MUTEX_HELD(&to_ill->ill_lock));
16855 	rw_enter(&ill_g_lock, RW_WRITER);
16856 	GRAB_ILL_LOCKS(from_ill, to_ill);
16857 	err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr);
16858 
16859 	/* ilm_move is done inside ipif_move for IPv4 */
16860 	if (err == 0 && from_ill->ill_isv6)
16861 		ilm_move_v6(from_ill, to_ill, ifindex);
16862 
16863 	RELEASE_ILL_LOCKS(from_ill, to_ill);
16864 	rw_exit(&ill_g_lock);
16865 
16866 	/*
16867 	 * send rts messages and multicast messages.
16868 	 */
16869 	if (rep_ipif_ptr != NULL) {
16870 		ip_rts_ifmsg(rep_ipif_ptr);
16871 		ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr);
16872 		IPIF_TRACE_CLEANUP(rep_ipif_ptr);
16873 		mi_free(rep_ipif_ptr);
16874 	}
16875 
16876 	ilm_send_multicast_reqs(from_ill, to_ill);
16877 
16878 	conn_move_ill(from_ill, to_ill, ifindex);
16879 
16880 	return (err);
16881 }
16882 
16883 /*
16884  * Used to extract arguments for FAILOVER/FAILBACK ioctls.
16885  * Also checks for the validity of the arguments.
16886  * Note: We are already exclusive inside the from group.
16887  * It is upto the caller to release refcnt on the to_ill's.
16888  */
16889 static int
16890 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4,
16891     ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6)
16892 {
16893 	int dst_index;
16894 	ipif_t *ipif_v4, *ipif_v6;
16895 	struct lifreq *lifr;
16896 	mblk_t *mp1;
16897 	boolean_t exists;
16898 	sin_t	*sin;
16899 	int	err = 0;
16900 
16901 	if ((mp1 = mp->b_cont) == NULL)
16902 		return (EPROTO);
16903 
16904 	if ((mp1 = mp1->b_cont) == NULL)
16905 		return (EPROTO);
16906 
16907 	lifr = (struct lifreq *)mp1->b_rptr;
16908 	sin = (sin_t *)&lifr->lifr_addr;
16909 
16910 	/*
16911 	 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6
16912 	 * specific operations.
16913 	 */
16914 	if (sin->sin_family != AF_UNSPEC)
16915 		return (EINVAL);
16916 
16917 	/*
16918 	 * Get ipif with id 0. We are writer on the from ill. So we can pass
16919 	 * NULLs for the last 4 args and we know the lookup won't fail
16920 	 * with EINPROGRESS.
16921 	 */
16922 	ipif_v4 = ipif_lookup_on_name(lifr->lifr_name,
16923 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE,
16924 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16925 	ipif_v6 = ipif_lookup_on_name(lifr->lifr_name,
16926 	    mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE,
16927 	    ALL_ZONES, NULL, NULL, NULL, NULL);
16928 
16929 	if (ipif_v4 == NULL && ipif_v6 == NULL)
16930 		return (ENXIO);
16931 
16932 	if (ipif_v4 != NULL) {
16933 		ASSERT(ipif_v4->ipif_refcnt != 0);
16934 		if (ipif_v4->ipif_id != 0) {
16935 			err = EINVAL;
16936 			goto done;
16937 		}
16938 
16939 		ASSERT(IAM_WRITER_IPIF(ipif_v4));
16940 		*ill_from_v4 = ipif_v4->ipif_ill;
16941 	}
16942 
16943 	if (ipif_v6 != NULL) {
16944 		ASSERT(ipif_v6->ipif_refcnt != 0);
16945 		if (ipif_v6->ipif_id != 0) {
16946 			err = EINVAL;
16947 			goto done;
16948 		}
16949 
16950 		ASSERT(IAM_WRITER_IPIF(ipif_v6));
16951 		*ill_from_v6 = ipif_v6->ipif_ill;
16952 	}
16953 
16954 	err = 0;
16955 	dst_index = lifr->lifr_movetoindex;
16956 	*ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE,
16957 	    q, mp, ip_process_ioctl, &err);
16958 	if (err != 0) {
16959 		/*
16960 		 * There could be only v6.
16961 		 */
16962 		if (err != ENXIO)
16963 			goto done;
16964 		err = 0;
16965 	}
16966 
16967 	*ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE,
16968 	    q, mp, ip_process_ioctl, &err);
16969 	if (err != 0) {
16970 		if (err != ENXIO)
16971 			goto done;
16972 		if (*ill_to_v4 == NULL) {
16973 			err = ENXIO;
16974 			goto done;
16975 		}
16976 		err = 0;
16977 	}
16978 
16979 	/*
16980 	 * If we have something to MOVE i.e "from" not NULL,
16981 	 * "to" should be non-NULL.
16982 	 */
16983 	if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) ||
16984 	    (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) {
16985 		err = EINVAL;
16986 	}
16987 
16988 done:
16989 	if (ipif_v4 != NULL)
16990 		ipif_refrele(ipif_v4);
16991 	if (ipif_v6 != NULL)
16992 		ipif_refrele(ipif_v6);
16993 	return (err);
16994 }
16995 
16996 /*
16997  * FAILOVER and FAILBACK are modelled as MOVE operations.
16998  *
16999  * We don't check whether the MOVE is within the same group or
17000  * not, because this ioctl can be used as a generic mechanism
17001  * to failover from interface A to B, though things will function
17002  * only if they are really part of the same group. Moreover,
17003  * all ipifs may be down and hence temporarily out of the group.
17004  *
17005  * ipif's that need to be moved are first brought down; V4 ipifs are brought
17006  * down first and then V6.  For each we wait for the ipif's to become quiescent.
17007  * Bringing down the ipifs ensures that all ires pointing to these ipifs's
17008  * have been deleted and there are no active references. Once quiescent the
17009  * ipif's are moved and brought up on the new ill.
17010  *
17011  * Normally the source ill and destination ill belong to the same IPMP group
17012  * and hence the same ipsq_t. In the event they don't belong to the same
17013  * same group the two ipsq's are first merged into one ipsq - that of the
17014  * to_ill. The multicast memberships on the source and destination ill cannot
17015  * change during the move operation since multicast joins/leaves also have to
17016  * execute on the same ipsq and are hence serialized.
17017  */
17018 /* ARGSUSED */
17019 int
17020 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
17021     ip_ioctl_cmd_t *ipip, void *ifreq)
17022 {
17023 	ill_t *ill_to_v4 = NULL;
17024 	ill_t *ill_to_v6 = NULL;
17025 	ill_t *ill_from_v4 = NULL;
17026 	ill_t *ill_from_v6 = NULL;
17027 	int err = 0;
17028 
17029 	/*
17030 	 * setup from and to ill's, we can get EINPROGRESS only for
17031 	 * to_ill's.
17032 	 */
17033 	err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6,
17034 	    &ill_to_v4, &ill_to_v6);
17035 
17036 	if (err != 0) {
17037 		ip0dbg(("ip_sioctl_move: extract args failed\n"));
17038 		goto done;
17039 	}
17040 
17041 	/*
17042 	 * nothing to do.
17043 	 */
17044 	if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) {
17045 		goto done;
17046 	}
17047 
17048 	/*
17049 	 * nothing to do.
17050 	 */
17051 	if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) {
17052 		goto done;
17053 	}
17054 
17055 	/*
17056 	 * Mark the ill as changing.
17057 	 * ILL_CHANGING flag is cleared when the ipif's are brought up
17058 	 * in ill_up_ipifs in case of error they are cleared below.
17059 	 */
17060 
17061 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17062 	if (ill_from_v4 != NULL)
17063 		ill_from_v4->ill_state_flags |= ILL_CHANGING;
17064 	if (ill_from_v6 != NULL)
17065 		ill_from_v6->ill_state_flags |= ILL_CHANGING;
17066 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17067 
17068 	/*
17069 	 * Make sure that both src and dst are
17070 	 * in the same syncq group. If not make it happen.
17071 	 * We are not holding any locks because we are the writer
17072 	 * on the from_ipsq and we will hold locks in ill_merge_groups
17073 	 * to protect to_ipsq against changing.
17074 	 */
17075 	if (ill_from_v4 != NULL) {
17076 		if (ill_from_v4->ill_phyint->phyint_ipsq !=
17077 		    ill_to_v4->ill_phyint->phyint_ipsq) {
17078 			err = ill_merge_groups(ill_from_v4, ill_to_v4,
17079 			    NULL, mp, q);
17080 			goto err_ret;
17081 
17082 		}
17083 		ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock));
17084 	} else {
17085 
17086 		if (ill_from_v6->ill_phyint->phyint_ipsq !=
17087 		    ill_to_v6->ill_phyint->phyint_ipsq) {
17088 			err = ill_merge_groups(ill_from_v6, ill_to_v6,
17089 			    NULL, mp, q);
17090 			goto err_ret;
17091 
17092 		}
17093 		ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock));
17094 	}
17095 
17096 	/*
17097 	 * Now that the ipsq's have been merged and we are the writer
17098 	 * lets mark to_ill as changing as well.
17099 	 */
17100 
17101 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17102 	if (ill_to_v4 != NULL)
17103 		ill_to_v4->ill_state_flags |= ILL_CHANGING;
17104 	if (ill_to_v6 != NULL)
17105 		ill_to_v6->ill_state_flags |= ILL_CHANGING;
17106 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17107 
17108 	/*
17109 	 * Its ok for us to proceed with the move even if
17110 	 * ill_pending_mp is non null on one of the from ill's as the reply
17111 	 * should not be looking at the ipif, it should only care about the
17112 	 * ill itself.
17113 	 */
17114 
17115 	/*
17116 	 * lets move ipv4 first.
17117 	 */
17118 	if (ill_from_v4 != NULL) {
17119 		ASSERT(IAM_WRITER_ILL(ill_to_v4));
17120 		ill_from_v4->ill_move_in_progress = B_TRUE;
17121 		ill_to_v4->ill_move_in_progress = B_TRUE;
17122 		ill_to_v4->ill_move_peer = ill_from_v4;
17123 		ill_from_v4->ill_move_peer = ill_to_v4;
17124 		err = ill_move(ill_from_v4, ill_to_v4, q, mp);
17125 	}
17126 
17127 	/*
17128 	 * Now lets move ipv6.
17129 	 */
17130 	if (err == 0 && ill_from_v6 != NULL) {
17131 		ASSERT(IAM_WRITER_ILL(ill_to_v6));
17132 		ill_from_v6->ill_move_in_progress = B_TRUE;
17133 		ill_to_v6->ill_move_in_progress = B_TRUE;
17134 		ill_to_v6->ill_move_peer = ill_from_v6;
17135 		ill_from_v6->ill_move_peer = ill_to_v6;
17136 		err = ill_move(ill_from_v6, ill_to_v6, q, mp);
17137 	}
17138 
17139 err_ret:
17140 	/*
17141 	 * EINPROGRESS means we are waiting for the ipif's that need to be
17142 	 * moved to become quiescent.
17143 	 */
17144 	if (err == EINPROGRESS) {
17145 		goto done;
17146 	}
17147 
17148 	/*
17149 	 * if err is set ill_up_ipifs will not be called
17150 	 * lets clear the flags.
17151 	 */
17152 
17153 	GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6);
17154 	GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6);
17155 	/*
17156 	 * Some of the clearing may be redundant. But it is simple
17157 	 * not making any extra checks.
17158 	 */
17159 	if (ill_from_v6 != NULL) {
17160 		ill_from_v6->ill_move_in_progress = B_FALSE;
17161 		ill_from_v6->ill_move_peer = NULL;
17162 		ill_from_v6->ill_state_flags &= ~ILL_CHANGING;
17163 	}
17164 	if (ill_from_v4 != NULL) {
17165 		ill_from_v4->ill_move_in_progress = B_FALSE;
17166 		ill_from_v4->ill_move_peer = NULL;
17167 		ill_from_v4->ill_state_flags &= ~ILL_CHANGING;
17168 	}
17169 	if (ill_to_v6 != NULL) {
17170 		ill_to_v6->ill_move_in_progress = B_FALSE;
17171 		ill_to_v6->ill_move_peer = NULL;
17172 		ill_to_v6->ill_state_flags &= ~ILL_CHANGING;
17173 	}
17174 	if (ill_to_v4 != NULL) {
17175 		ill_to_v4->ill_move_in_progress = B_FALSE;
17176 		ill_to_v4->ill_move_peer = NULL;
17177 		ill_to_v4->ill_state_flags &= ~ILL_CHANGING;
17178 	}
17179 
17180 	/*
17181 	 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set.
17182 	 * Do this always to maintain proper state i.e even in case of errors.
17183 	 * As phyint_inactive looks at both v4 and v6 interfaces,
17184 	 * we need not call on both v4 and v6 interfaces.
17185 	 */
17186 	if (ill_from_v4 != NULL) {
17187 		if ((ill_from_v4->ill_phyint->phyint_flags &
17188 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17189 			phyint_inactive(ill_from_v4->ill_phyint);
17190 		}
17191 	} else if (ill_from_v6 != NULL) {
17192 		if ((ill_from_v6->ill_phyint->phyint_flags &
17193 		    (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) {
17194 			phyint_inactive(ill_from_v6->ill_phyint);
17195 		}
17196 	}
17197 
17198 	if (ill_to_v4 != NULL) {
17199 		if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17200 			ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17201 		}
17202 	} else if (ill_to_v6 != NULL) {
17203 		if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) {
17204 			ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE;
17205 		}
17206 	}
17207 
17208 	RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6);
17209 	RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6);
17210 
17211 no_err:
17212 	/*
17213 	 * lets bring the interfaces up on the to_ill.
17214 	 */
17215 	if (err == 0) {
17216 		err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4,
17217 		    q, mp);
17218 	}
17219 done:
17220 
17221 	if (ill_to_v4 != NULL) {
17222 		ill_refrele(ill_to_v4);
17223 	}
17224 	if (ill_to_v6 != NULL) {
17225 		ill_refrele(ill_to_v6);
17226 	}
17227 
17228 	return (err);
17229 }
17230 
17231 static void
17232 ill_dl_down(ill_t *ill)
17233 {
17234 	/*
17235 	 * The ill is down; unbind but stay attached since we're still
17236 	 * associated with a PPA.
17237 	 */
17238 	mblk_t	*mp = ill->ill_unbind_mp;
17239 
17240 	ill->ill_unbind_mp = NULL;
17241 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
17242 	if (mp != NULL) {
17243 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
17244 		    dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
17245 		    ill->ill_name));
17246 		mutex_enter(&ill->ill_lock);
17247 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
17248 		mutex_exit(&ill->ill_lock);
17249 		ill_dlpi_send(ill, mp);
17250 	}
17251 
17252 	/*
17253 	 * Toss all of our multicast memberships.  We could keep them, but
17254 	 * then we'd have to do bookkeeping of any joins and leaves performed
17255 	 * by the application while the the interface is down (we can't just
17256 	 * issue them because arp cannot currently process AR_ENTRY_SQUERY's
17257 	 * on a downed interface).
17258 	 */
17259 	ill_leave_multicast(ill);
17260 
17261 	mutex_enter(&ill->ill_lock);
17262 	ill->ill_dl_up = 0;
17263 	mutex_exit(&ill->ill_lock);
17264 }
17265 
17266 void
17267 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
17268 {
17269 	union DL_primitives *dlp;
17270 	t_uscalar_t prim;
17271 
17272 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17273 
17274 	dlp = (union DL_primitives *)mp->b_rptr;
17275 	prim = dlp->dl_primitive;
17276 
17277 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
17278 		dlpi_prim_str(prim), prim, ill->ill_name));
17279 
17280 	switch (prim) {
17281 	case DL_PHYS_ADDR_REQ:
17282 	{
17283 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
17284 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
17285 		break;
17286 	}
17287 	case DL_BIND_REQ:
17288 		mutex_enter(&ill->ill_lock);
17289 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
17290 		mutex_exit(&ill->ill_lock);
17291 		break;
17292 	}
17293 
17294 	ill->ill_dlpi_pending = prim;
17295 
17296 	/*
17297 	 * Some drivers send M_FLUSH up to IP as part of unbind
17298 	 * request.  When this M_FLUSH is sent back to the driver,
17299 	 * this can go after we send the detach request if the
17300 	 * M_FLUSH ends up in IP's syncq. To avoid that, we reply
17301 	 * to the M_FLUSH in ip_rput and locally generate another
17302 	 * M_FLUSH for the correctness.  This will get freed in
17303 	 * ip_wput_nondata.
17304 	 */
17305 	if (prim == DL_UNBIND_REQ)
17306 		(void) putnextctl1(ill->ill_rq, M_FLUSH, FLUSHRW);
17307 
17308 	putnext(ill->ill_wq, mp);
17309 }
17310 
17311 /*
17312  * Send a DLPI control message to the driver but make sure there
17313  * is only one outstanding message. Uses ill_dlpi_pending to tell
17314  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
17315  * when an ACK or a NAK is received to process the next queued message.
17316  *
17317  * We don't protect ill_dlpi_pending with any lock. This is okay as
17318  * every place where its accessed, ip is exclusive while accessing
17319  * ill_dlpi_pending except when this function is called from ill_init()
17320  */
17321 void
17322 ill_dlpi_send(ill_t *ill, mblk_t *mp)
17323 {
17324 	mblk_t **mpp;
17325 
17326 	ASSERT(IAM_WRITER_ILL(ill));
17327 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
17328 
17329 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
17330 		/* Must queue message. Tail insertion */
17331 		mpp = &ill->ill_dlpi_deferred;
17332 		while (*mpp != NULL)
17333 			mpp = &((*mpp)->b_next);
17334 
17335 		ip1dbg(("ill_dlpi_send: deferring request for %s\n",
17336 		    ill->ill_name));
17337 
17338 		*mpp = mp;
17339 		return;
17340 	}
17341 
17342 	ill_dlpi_dispatch(ill, mp);
17343 }
17344 
17345 /*
17346  * Called when an DLPI control message has been acked or nacked to
17347  * send down the next queued message (if any).
17348  */
17349 void
17350 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
17351 {
17352 	mblk_t *mp;
17353 
17354 	ASSERT(IAM_WRITER_ILL(ill));
17355 
17356 	ASSERT(prim != DL_PRIM_INVAL);
17357 	if (ill->ill_dlpi_pending != prim) {
17358 		if (ill->ill_dlpi_pending == DL_PRIM_INVAL) {
17359 			(void) mi_strlog(ill->ill_rq, 1,
17360 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17361 			    "ill_dlpi_done: unsolicited ack for %s from %s\n",
17362 			    dlpi_prim_str(prim), ill->ill_name);
17363 		} else {
17364 			(void) mi_strlog(ill->ill_rq, 1,
17365 			    SL_CONSOLE|SL_ERROR|SL_TRACE,
17366 			    "ill_dlpi_done: unexpected ack for %s from %s "
17367 			    "(expecting ack for %s)\n",
17368 			    dlpi_prim_str(prim), ill->ill_name,
17369 			    dlpi_prim_str(ill->ill_dlpi_pending));
17370 		}
17371 		return;
17372 	}
17373 
17374 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
17375 	    dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
17376 
17377 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
17378 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
17379 		return;
17380 	}
17381 
17382 	ill->ill_dlpi_deferred = mp->b_next;
17383 	mp->b_next = NULL;
17384 
17385 	ill_dlpi_dispatch(ill, mp);
17386 }
17387 
17388 void
17389 conn_delete_ire(conn_t *connp, caddr_t arg)
17390 {
17391 	ipif_t	*ipif = (ipif_t *)arg;
17392 	ire_t	*ire;
17393 
17394 	/*
17395 	 * Look at the cached ires on conns which has pointers to ipifs.
17396 	 * We just call ire_refrele which clears up the reference
17397 	 * to ire. Called when a conn closes. Also called from ipif_free
17398 	 * to cleanup indirect references to the stale ipif via the cached ire.
17399 	 */
17400 	mutex_enter(&connp->conn_lock);
17401 	ire = connp->conn_ire_cache;
17402 	if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) {
17403 		connp->conn_ire_cache = NULL;
17404 		mutex_exit(&connp->conn_lock);
17405 		IRE_REFRELE_NOTR(ire);
17406 		return;
17407 	}
17408 	mutex_exit(&connp->conn_lock);
17409 
17410 }
17411 
17412 /*
17413  * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number
17414  * of IREs. Those IREs may have been previously cached in the conn structure.
17415  * This ipcl_walk() walker function releases all references to such IREs based
17416  * on the condemned flag.
17417  */
17418 /* ARGSUSED */
17419 void
17420 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg)
17421 {
17422 	ire_t	*ire;
17423 
17424 	mutex_enter(&connp->conn_lock);
17425 	ire = connp->conn_ire_cache;
17426 	if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) {
17427 		connp->conn_ire_cache = NULL;
17428 		mutex_exit(&connp->conn_lock);
17429 		IRE_REFRELE_NOTR(ire);
17430 		return;
17431 	}
17432 	mutex_exit(&connp->conn_lock);
17433 }
17434 
17435 /*
17436  * Take down a specific interface, but don't lose any information about it.
17437  * Also delete interface from its interface group (ifgrp).
17438  * (Always called as writer.)
17439  * This function goes through the down sequence even if the interface is
17440  * already down. There are 2 reasons.
17441  * a. Currently we permit interface routes that depend on down interfaces
17442  *    to be added. This behaviour itself is questionable. However it appears
17443  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
17444  *    time. We go thru the cleanup in order to remove these routes.
17445  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
17446  *    DL_ERROR_ACK in response to the the DL_BIND request. The interface is
17447  *    down, but we need to cleanup i.e. do ill_dl_down and
17448  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
17449  *
17450  * IP-MT notes:
17451  *
17452  * Model of reference to interfaces.
17453  *
17454  * The following members in ipif_t track references to the ipif.
17455  *	int     ipif_refcnt;    Active reference count
17456  *	uint_t  ipif_ire_cnt;   Number of ire's referencing this ipif
17457  * The following members in ill_t track references to the ill.
17458  *	int             ill_refcnt;     active refcnt
17459  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
17460  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
17461  *
17462  * Reference to an ipif or ill can be obtained in any of the following ways.
17463  *
17464  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
17465  * Pointers to ipif / ill from other data structures viz ire and conn.
17466  * Implicit reference to the ipif / ill by holding a reference to the ire.
17467  *
17468  * The ipif/ill lookup functions return a reference held ipif / ill.
17469  * ipif_refcnt and ill_refcnt track the reference counts respectively.
17470  * This is a purely dynamic reference count associated with threads holding
17471  * references to the ipif / ill. Pointers from other structures do not
17472  * count towards this reference count.
17473  *
17474  * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the
17475  * ipif/ill. This is incremented whenever a new ire is created referencing the
17476  * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is
17477  * actually added to the ire hash table. The count is decremented in
17478  * ire_inactive where the ire is destroyed.
17479  *
17480  * nce's reference ill's thru nce_ill and the count of nce's associated with
17481  * an ill is recorded in ill_nce_cnt. This is incremented atomically in
17482  * ndp_add() where the nce is actually added to the table. Similarly it is
17483  * decremented in ndp_inactive where the nce is destroyed.
17484  *
17485  * Flow of ioctls involving interface down/up
17486  *
17487  * The following is the sequence of an attempt to set some critical flags on an
17488  * up interface.
17489  * ip_sioctl_flags
17490  * ipif_down
17491  * wait for ipif to be quiescent
17492  * ipif_down_tail
17493  * ip_sioctl_flags_tail
17494  *
17495  * All set ioctls that involve down/up sequence would have a skeleton similar
17496  * to the above. All the *tail functions are called after the refcounts have
17497  * dropped to the appropriate values.
17498  *
17499  * The mechanism to quiesce an ipif is as follows.
17500  *
17501  * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed
17502  * on the ipif. Callers either pass a flag requesting wait or the lookup
17503  *  functions will return NULL.
17504  *
17505  * Delete all ires referencing this ipif
17506  *
17507  * Any thread attempting to do an ipif_refhold on an ipif that has been
17508  * obtained thru a cached pointer will first make sure that
17509  * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then
17510  * increment the refcount.
17511  *
17512  * The above guarantees that the ipif refcount will eventually come down to
17513  * zero and the ipif will quiesce, once all threads that currently hold a
17514  * reference to the ipif refrelease the ipif. The ipif is quiescent after the
17515  * ipif_refcount has dropped to zero and all ire's associated with this ipif
17516  * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both
17517  * drop to zero.
17518  *
17519  * Lookups during the IPIF_CHANGING/ILL_CHANGING interval.
17520  *
17521  * Threads trying to lookup an ipif or ill can pass a flag requesting
17522  * wait and restart if the ipif / ill cannot be looked up currently.
17523  * For eg. bind, and route operations (Eg. route add / delete) cannot return
17524  * failure if the ipif is currently undergoing an exclusive operation, and
17525  * hence pass the flag. The mblk is then enqueued in the ipsq and the operation
17526  * is restarted by ipsq_exit() when the currently exclusive ioctl completes.
17527  * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The
17528  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
17529  * change while the ill_lock is held. Before dropping the ill_lock we acquire
17530  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
17531  * until we release the ipsq_lock, even though the the ill/ipif state flags
17532  * can change after we drop the ill_lock.
17533  *
17534  * An attempt to send out a packet using an ipif that is currently
17535  * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this
17536  * operation and restart it later when the exclusive condition on the ipif ends.
17537  * This is an example of not passing the wait flag to the lookup functions. For
17538  * example an attempt to refhold and use conn->conn_multicast_ipif and send
17539  * out a multicast packet on that ipif will fail while the ipif is
17540  * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is
17541  * currently IPIF_CHANGING will also fail.
17542  */
17543 int
17544 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17545 {
17546 	ill_t		*ill = ipif->ipif_ill;
17547 	phyint_t	*phyi;
17548 	conn_t		*connp;
17549 	boolean_t	success;
17550 	boolean_t	ipif_was_up = B_FALSE;
17551 
17552 	ASSERT(IAM_WRITER_IPIF(ipif));
17553 
17554 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
17555 
17556 	if (ipif->ipif_flags & IPIF_UP) {
17557 		mutex_enter(&ill->ill_lock);
17558 		ipif->ipif_flags &= ~IPIF_UP;
17559 		ASSERT(ill->ill_ipif_up_count > 0);
17560 		--ill->ill_ipif_up_count;
17561 		mutex_exit(&ill->ill_lock);
17562 		ipif_was_up = B_TRUE;
17563 		/* Update status in SCTP's list */
17564 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
17565 	}
17566 
17567 	/*
17568 	 * Blow away v6 memberships we established in ipif_multicast_up(); the
17569 	 * v4 ones are left alone (as is the ipif_multicast_up flag, so we
17570 	 * know not to rejoin when the interface is brought back up).
17571 	 */
17572 	if (ipif->ipif_isv6)
17573 		ipif_multicast_down(ipif);
17574 	/*
17575 	 * Remove from the mapping for __sin6_src_id. We insert only
17576 	 * when the address is not INADDR_ANY. As IPv4 addresses are
17577 	 * stored as mapped addresses, we need to check for mapped
17578 	 * INADDR_ANY also.
17579 	 */
17580 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
17581 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
17582 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
17583 		int err;
17584 
17585 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
17586 		    ipif->ipif_zoneid);
17587 		if (err != 0) {
17588 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
17589 		}
17590 	}
17591 
17592 	/*
17593 	 * Before we delete the ill from the group (if any), we need
17594 	 * to make sure that we delete all the routes dependent on
17595 	 * this and also any ipifs dependent on this ipif for
17596 	 * source address. We need to do before we delete from
17597 	 * the group because
17598 	 *
17599 	 * 1) ipif_down_delete_ire de-references ill->ill_group.
17600 	 *
17601 	 * 2) ipif_update_other_ipifs needs to walk the whole group
17602 	 *    for re-doing source address selection. Note that
17603 	 *    ipif_select_source[_v6] called from
17604 	 *    ipif_update_other_ipifs[_v6] will not pick this ipif
17605 	 *    because we have already marked down here i.e cleared
17606 	 *    IPIF_UP.
17607 	 */
17608 	if (ipif->ipif_isv6)
17609 		ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17610 	else
17611 		ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES);
17612 
17613 	/*
17614 	 * Need to add these also to be saved and restored when the
17615 	 * ipif is brought down and up
17616 	 */
17617 	mutex_enter(&ire_mrtun_lock);
17618 	if (ire_mrtun_count != 0) {
17619 		mutex_exit(&ire_mrtun_lock);
17620 		ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire,
17621 		    (char *)ipif, NULL);
17622 	} else {
17623 		mutex_exit(&ire_mrtun_lock);
17624 	}
17625 
17626 	mutex_enter(&ire_srcif_table_lock);
17627 	if (ire_srcif_table_count > 0) {
17628 		mutex_exit(&ire_srcif_table_lock);
17629 		ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif);
17630 	} else {
17631 		mutex_exit(&ire_srcif_table_lock);
17632 	}
17633 
17634 	/*
17635 	 * Cleaning up the conn_ire_cache or conns must be done only after the
17636 	 * ires have been deleted above. Otherwise a thread could end up
17637 	 * caching an ire in a conn after we have finished the cleanup of the
17638 	 * conn. The caching is done after making sure that the ire is not yet
17639 	 * condemned. Also documented in the block comment above ip_output
17640 	 */
17641 	ipcl_walk(conn_cleanup_stale_ire, NULL);
17642 	/* Also, delete the ires cached in SCTP */
17643 	sctp_ire_cache_flush(ipif);
17644 
17645 	/* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */
17646 	nattymod_clean_ipif(ipif);
17647 
17648 	/*
17649 	 * Update any other ipifs which have used "our" local address as
17650 	 * a source address. This entails removing and recreating IRE_INTERFACE
17651 	 * entries for such ipifs.
17652 	 */
17653 	if (ipif->ipif_isv6)
17654 		ipif_update_other_ipifs_v6(ipif, ill->ill_group);
17655 	else
17656 		ipif_update_other_ipifs(ipif, ill->ill_group);
17657 
17658 	if (ipif_was_up) {
17659 		/*
17660 		 * Check whether it is last ipif to leave this group.
17661 		 * If this is the last ipif to leave, we should remove
17662 		 * this ill from the group as ipif_select_source will not
17663 		 * be able to find any useful ipifs if this ill is selected
17664 		 * for load balancing.
17665 		 *
17666 		 * For nameless groups, we should call ifgrp_delete if this
17667 		 * belongs to some group. As this ipif is going down, we may
17668 		 * need to reconstruct groups.
17669 		 */
17670 		phyi = ill->ill_phyint;
17671 		/*
17672 		 * If the phyint_groupname_len is 0, it may or may not
17673 		 * be in the nameless group. If the phyint_groupname_len is
17674 		 * not 0, then this ill should be part of some group.
17675 		 * As we always insert this ill in the group if
17676 		 * phyint_groupname_len is not zero when the first ipif
17677 		 * comes up (in ipif_up_done), it should be in a group
17678 		 * when the namelen is not 0.
17679 		 *
17680 		 * NOTE : When we delete the ill from the group,it will
17681 		 * blow away all the IRE_CACHES pointing either at this ipif or
17682 		 * ill_wq (illgrp_cache_delete does this). Thus, no IRES
17683 		 * should be pointing at this ill.
17684 		 */
17685 		ASSERT(phyi->phyint_groupname_len == 0 ||
17686 		    (phyi->phyint_groupname != NULL && ill->ill_group != NULL));
17687 
17688 		if (phyi->phyint_groupname_len != 0) {
17689 			if (ill->ill_ipif_up_count == 0)
17690 				illgrp_delete(ill);
17691 		}
17692 
17693 		/*
17694 		 * If we have deleted some of the broadcast ires associated
17695 		 * with this ipif, we need to re-nominate somebody else if
17696 		 * the ires that we deleted were the nominated ones.
17697 		 */
17698 		if (ill->ill_group != NULL && !ill->ill_isv6)
17699 			ipif_renominate_bcast(ipif);
17700 	}
17701 
17702 	if (ipif->ipif_isv6)
17703 		ipif_ndp_down(ipif);
17704 
17705 	/*
17706 	 * If mp is NULL the caller will wait for the appropriate refcnt.
17707 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
17708 	 * and ill_delete -> ipif_free -> ipif_down
17709 	 */
17710 	if (mp == NULL) {
17711 		ASSERT(q == NULL);
17712 		return (0);
17713 	}
17714 
17715 	if (CONN_Q(q)) {
17716 		connp = Q_TO_CONN(q);
17717 		mutex_enter(&connp->conn_lock);
17718 	} else {
17719 		connp = NULL;
17720 	}
17721 	mutex_enter(&ill->ill_lock);
17722 	/*
17723 	 * Are there any ire's pointing to this ipif that are still active ?
17724 	 * If this is the last ipif going down, are there any ire's pointing
17725 	 * to this ill that are still active ?
17726 	 */
17727 	if (ipif_is_quiescent(ipif)) {
17728 		mutex_exit(&ill->ill_lock);
17729 		if (connp != NULL)
17730 			mutex_exit(&connp->conn_lock);
17731 		return (0);
17732 	}
17733 
17734 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
17735 	    ill->ill_name, (void *)ill));
17736 	/*
17737 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
17738 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
17739 	 * which in turn is called by the last refrele on the ipif/ill/ire.
17740 	 */
17741 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
17742 	if (!success) {
17743 		/* The conn is closing. So just return */
17744 		ASSERT(connp != NULL);
17745 		mutex_exit(&ill->ill_lock);
17746 		mutex_exit(&connp->conn_lock);
17747 		return (EINTR);
17748 	}
17749 
17750 	mutex_exit(&ill->ill_lock);
17751 	if (connp != NULL)
17752 		mutex_exit(&connp->conn_lock);
17753 	return (EINPROGRESS);
17754 }
17755 
17756 static void
17757 ipif_down_tail(ipif_t *ipif)
17758 {
17759 	ill_t	*ill = ipif->ipif_ill;
17760 
17761 	/*
17762 	 * Skip any loopback interface (null wq).
17763 	 * If this is the last logical interface on the ill
17764 	 * have ill_dl_down tell the driver we are gone (unbind)
17765 	 * Note that lun 0 can ipif_down even though
17766 	 * there are other logical units that are up.
17767 	 * This occurs e.g. when we change a "significant" IFF_ flag.
17768 	 */
17769 	if (ipif->ipif_ill->ill_wq != NULL) {
17770 		if (!ill->ill_logical_down && (ill->ill_ipif_up_count == 0) &&
17771 		    ill->ill_dl_up) {
17772 			ill_dl_down(ill);
17773 		}
17774 	}
17775 	ill->ill_logical_down = 0;
17776 
17777 	/*
17778 	 * Have to be after removing the routes in ipif_down_delete_ire.
17779 	 */
17780 	if (ipif->ipif_isv6) {
17781 		if (ipif->ipif_ill->ill_flags & ILLF_XRESOLV)
17782 			ipif_arp_down(ipif);
17783 	} else {
17784 		ipif_arp_down(ipif);
17785 	}
17786 
17787 	ip_rts_ifmsg(ipif);
17788 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif);
17789 }
17790 
17791 /*
17792  * Bring interface logically down without bringing the physical interface
17793  * down e.g. when the netmask is changed. This avoids long lasting link
17794  * negotiations between an ethernet interface and a certain switches.
17795  */
17796 static int
17797 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
17798 {
17799 	/*
17800 	 * The ill_logical_down flag is a transient flag. It is set here
17801 	 * and is cleared once the down has completed in ipif_down_tail.
17802 	 * This flag does not indicate whether the ill stream is in the
17803 	 * DL_BOUND state with the driver. Instead this flag is used by
17804 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
17805 	 * the driver. The state of the ill stream i.e. whether it is
17806 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
17807 	 */
17808 	ipif->ipif_ill->ill_logical_down = 1;
17809 	return (ipif_down(ipif, q, mp));
17810 }
17811 
17812 /*
17813  * This is called when the SIOCSLIFUSESRC ioctl is processed in IP.
17814  * If the usesrc client ILL is already part of a usesrc group or not,
17815  * in either case a ire_stq with the matching usesrc client ILL will
17816  * locate the IRE's that need to be deleted. We want IREs to be created
17817  * with the new source address.
17818  */
17819 static void
17820 ipif_delete_cache_ire(ire_t *ire, char *ill_arg)
17821 {
17822 	ill_t	*ucill = (ill_t *)ill_arg;
17823 
17824 	ASSERT(IAM_WRITER_ILL(ucill));
17825 
17826 	if (ire->ire_stq == NULL)
17827 		return;
17828 
17829 	if ((ire->ire_type == IRE_CACHE) &&
17830 	    ((ill_t *)ire->ire_stq->q_ptr == ucill))
17831 		ire_delete(ire);
17832 }
17833 
17834 /*
17835  * ire_walk routine to delete every IRE dependent on the interface
17836  * address that is going down.	(Always called as writer.)
17837  * Works for both v4 and v6.
17838  * In addition for checking for ire_ipif matches it also checks for
17839  * IRE_CACHE entries which have the same source address as the
17840  * disappearing ipif since ipif_select_source might have picked
17841  * that source. Note that ipif_down/ipif_update_other_ipifs takes
17842  * care of any IRE_INTERFACE with the disappearing source address.
17843  */
17844 static void
17845 ipif_down_delete_ire(ire_t *ire, char *ipif_arg)
17846 {
17847 	ipif_t	*ipif = (ipif_t *)ipif_arg;
17848 	ill_t *ire_ill;
17849 	ill_t *ipif_ill;
17850 
17851 	ASSERT(IAM_WRITER_IPIF(ipif));
17852 	if (ire->ire_ipif == NULL)
17853 		return;
17854 
17855 	/*
17856 	 * For IPv4, we derive source addresses for an IRE from ipif's
17857 	 * belonging to the same IPMP group as the IRE's outgoing
17858 	 * interface.  If an IRE's outgoing interface isn't in the
17859 	 * same IPMP group as a particular ipif, then that ipif
17860 	 * couldn't have been used as a source address for this IRE.
17861 	 *
17862 	 * For IPv6, source addresses are only restricted to the IPMP group
17863 	 * if the IRE is for a link-local address or a multicast address.
17864 	 * Otherwise, source addresses for an IRE can be chosen from
17865 	 * interfaces other than the the outgoing interface for that IRE.
17866 	 *
17867 	 * For source address selection details, see ipif_select_source()
17868 	 * and ipif_select_source_v6().
17869 	 */
17870 	if (ire->ire_ipversion == IPV4_VERSION ||
17871 	    IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) ||
17872 	    IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) {
17873 		ire_ill = ire->ire_ipif->ipif_ill;
17874 		ipif_ill = ipif->ipif_ill;
17875 
17876 		if (ire_ill->ill_group != ipif_ill->ill_group) {
17877 			return;
17878 		}
17879 	}
17880 
17881 
17882 	if (ire->ire_ipif != ipif) {
17883 		/*
17884 		 * Look for a matching source address.
17885 		 */
17886 		if (ire->ire_type != IRE_CACHE)
17887 			return;
17888 		if (ipif->ipif_flags & IPIF_NOLOCAL)
17889 			return;
17890 
17891 		if (ire->ire_ipversion == IPV4_VERSION) {
17892 			if (ire->ire_src_addr != ipif->ipif_src_addr)
17893 				return;
17894 		} else {
17895 			if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6,
17896 			    &ipif->ipif_v6lcl_addr))
17897 				return;
17898 		}
17899 		ire_delete(ire);
17900 		return;
17901 	}
17902 	/*
17903 	 * ire_delete() will do an ire_flush_cache which will delete
17904 	 * all ire_ipif matches
17905 	 */
17906 	ire_delete(ire);
17907 }
17908 
17909 /*
17910  * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when
17911  * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or
17912  * 2) when an interface is brought up or down (on that ill).
17913  * This ensures that the IRE_CACHE entries don't retain stale source
17914  * address selection results.
17915  */
17916 void
17917 ill_ipif_cache_delete(ire_t *ire, char *ill_arg)
17918 {
17919 	ill_t	*ill = (ill_t *)ill_arg;
17920 	ill_t	*ipif_ill;
17921 
17922 	ASSERT(IAM_WRITER_ILL(ill));
17923 	/*
17924 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17925 	 * Hence this should be IRE_CACHE.
17926 	 */
17927 	ASSERT(ire->ire_type == IRE_CACHE);
17928 
17929 	/*
17930 	 * We are called for IRE_CACHES whose ire_ipif matches ill.
17931 	 * We are only interested in IRE_CACHES that has borrowed
17932 	 * the source address from ill_arg e.g. ipif_up_done[_v6]
17933 	 * for which we need to look at ire_ipif->ipif_ill match
17934 	 * with ill.
17935 	 */
17936 	ASSERT(ire->ire_ipif != NULL);
17937 	ipif_ill = ire->ire_ipif->ipif_ill;
17938 	if (ipif_ill == ill || (ill->ill_group != NULL &&
17939 	    ipif_ill->ill_group == ill->ill_group)) {
17940 		ire_delete(ire);
17941 	}
17942 }
17943 
17944 /*
17945  * Delete all the ire whose stq references ill_arg.
17946  */
17947 static void
17948 ill_stq_cache_delete(ire_t *ire, char *ill_arg)
17949 {
17950 	ill_t	*ill = (ill_t *)ill_arg;
17951 	ill_t	*ire_ill;
17952 
17953 	ASSERT(IAM_WRITER_ILL(ill));
17954 	/*
17955 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17956 	 * Hence this should be IRE_CACHE.
17957 	 */
17958 	ASSERT(ire->ire_type == IRE_CACHE);
17959 
17960 	/*
17961 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17962 	 * matches ill. We are only interested in IRE_CACHES that
17963 	 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the
17964 	 * filtering here.
17965 	 */
17966 	ire_ill = (ill_t *)ire->ire_stq->q_ptr;
17967 
17968 	if (ire_ill == ill)
17969 		ire_delete(ire);
17970 }
17971 
17972 /*
17973  * This is called when an ill leaves the group. We want to delete
17974  * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is
17975  * pointing at ill.
17976  */
17977 static void
17978 illgrp_cache_delete(ire_t *ire, char *ill_arg)
17979 {
17980 	ill_t	*ill = (ill_t *)ill_arg;
17981 
17982 	ASSERT(IAM_WRITER_ILL(ill));
17983 	ASSERT(ill->ill_group == NULL);
17984 	/*
17985 	 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4.
17986 	 * Hence this should be IRE_CACHE.
17987 	 */
17988 	ASSERT(ire->ire_type == IRE_CACHE);
17989 	/*
17990 	 * We are called for IRE_CACHES whose ire_stq and ire_ipif
17991 	 * matches ill. We are interested in both.
17992 	 */
17993 	ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) ||
17994 	    (ire->ire_ipif->ipif_ill == ill));
17995 
17996 	ire_delete(ire);
17997 }
17998 
17999 /*
18000  * Initiate deallocate of an IPIF. Always called as writer. Called by
18001  * ill_delete or ip_sioctl_removeif.
18002  */
18003 static void
18004 ipif_free(ipif_t *ipif)
18005 {
18006 	ASSERT(IAM_WRITER_IPIF(ipif));
18007 
18008 	/* Remove conn references */
18009 	reset_conn_ipif(ipif);
18010 
18011 	/*
18012 	 * Make sure we have valid net and subnet broadcast ire's for the
18013 	 * other ipif's which share them with this ipif.
18014 	 */
18015 	if (!ipif->ipif_isv6)
18016 		ipif_check_bcast_ires(ipif);
18017 
18018 	/*
18019 	 * Take down the interface. We can be called either from ill_delete
18020 	 * or from ip_sioctl_removeif.
18021 	 */
18022 	(void) ipif_down(ipif, NULL, NULL);
18023 
18024 	rw_enter(&ill_g_lock, RW_WRITER);
18025 	/* Remove pointers to this ill in the multicast routing tables */
18026 	reset_mrt_vif_ipif(ipif);
18027 	rw_exit(&ill_g_lock);
18028 }
18029 
18030 static void
18031 ipif_free_tail(ipif_t *ipif)
18032 {
18033 	mblk_t	*mp;
18034 	ipif_t	**ipifp;
18035 
18036 	/*
18037 	 * Free state for addition IRE_IF_[NO]RESOLVER ire's.
18038 	 */
18039 	mutex_enter(&ipif->ipif_saved_ire_lock);
18040 	mp = ipif->ipif_saved_ire_mp;
18041 	ipif->ipif_saved_ire_mp = NULL;
18042 	mutex_exit(&ipif->ipif_saved_ire_lock);
18043 	freemsg(mp);
18044 
18045 	/*
18046 	 * Need to hold both ill_g_lock and ill_lock while
18047 	 * inserting or removing an ipif from the linked list
18048 	 * of ipifs hanging off the ill.
18049 	 */
18050 	rw_enter(&ill_g_lock, RW_WRITER);
18051 	/*
18052 	 * Remove all multicast memberships on the interface now.
18053 	 * This removes IPv4 multicast memberships joined within
18054 	 * the kernel as ipif_down does not do ipif_multicast_down
18055 	 * for IPv4. IPv6 is not handled here as the multicast memberships
18056 	 * are based on ill and not on ipif.
18057 	 */
18058 	ilm_free(ipif);
18059 
18060 	/*
18061 	 * Since we held the ill_g_lock while doing the ilm_free above,
18062 	 * we can assert the ilms were really deleted and not just marked
18063 	 * ILM_DELETED.
18064 	 */
18065 	ASSERT(ilm_walk_ipif(ipif) == 0);
18066 
18067 
18068 	IPIF_TRACE_CLEANUP(ipif);
18069 
18070 	/* Ask SCTP to take it out of it list */
18071 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
18072 
18073 	mutex_enter(&ipif->ipif_ill->ill_lock);
18074 	/* Get it out of the ILL interface list. */
18075 	ipifp = &ipif->ipif_ill->ill_ipif;
18076 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
18077 		if (*ipifp == ipif) {
18078 			*ipifp = ipif->ipif_next;
18079 			break;
18080 		}
18081 	}
18082 
18083 	mutex_exit(&ipif->ipif_ill->ill_lock);
18084 	rw_exit(&ill_g_lock);
18085 
18086 	mutex_destroy(&ipif->ipif_saved_ire_lock);
18087 	/* Free the memory. */
18088 	mi_free((char *)ipif);
18089 }
18090 
18091 /*
18092  * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero,
18093  * "ill_name" otherwise.
18094  */
18095 char *
18096 ipif_get_name(const ipif_t *ipif, char *buf, int len)
18097 {
18098 	char	lbuf[32];
18099 	char	*name;
18100 	size_t	name_len;
18101 
18102 	buf[0] = '\0';
18103 	if (!ipif)
18104 		return (buf);
18105 	name = ipif->ipif_ill->ill_name;
18106 	name_len = ipif->ipif_ill->ill_name_length;
18107 	if (ipif->ipif_id != 0) {
18108 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
18109 		    ipif->ipif_id);
18110 		name = lbuf;
18111 		name_len = mi_strlen(name) + 1;
18112 	}
18113 	len -= 1;
18114 	buf[len] = '\0';
18115 	len = MIN(len, name_len);
18116 	bcopy(name, buf, len);
18117 	return (buf);
18118 }
18119 
18120 /*
18121  * Find an IPIF based on the name passed in.  Names can be of the
18122  * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1),
18123  * The <phys> string can have forms like <dev><#> (e.g., le0),
18124  * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3).
18125  * When there is no colon, the implied unit id is zero. <phys> must
18126  * correspond to the name of an ILL.  (May be called as writer.)
18127  */
18128 static ipif_t *
18129 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
18130     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q,
18131     mblk_t *mp, ipsq_func_t func, int *error)
18132 {
18133 	char	*cp;
18134 	char	*endp;
18135 	long	id;
18136 	ill_t	*ill;
18137 	ipif_t	*ipif;
18138 	uint_t	ire_type;
18139 	boolean_t did_alloc = B_FALSE;
18140 	ipsq_t	*ipsq;
18141 
18142 	if (error != NULL)
18143 		*error = 0;
18144 
18145 	/*
18146 	 * If the caller wants to us to create the ipif, make sure we have a
18147 	 * valid zoneid
18148 	 */
18149 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
18150 
18151 	if (namelen == 0) {
18152 		if (error != NULL)
18153 			*error = ENXIO;
18154 		return (NULL);
18155 	}
18156 
18157 	*exists = B_FALSE;
18158 	/* Look for a colon in the name. */
18159 	endp = &name[namelen];
18160 	for (cp = endp; --cp > name; ) {
18161 		if (*cp == IPIF_SEPARATOR_CHAR)
18162 			break;
18163 	}
18164 
18165 	if (*cp == IPIF_SEPARATOR_CHAR) {
18166 		/*
18167 		 * Reject any non-decimal aliases for logical
18168 		 * interfaces. Aliases with leading zeroes
18169 		 * are also rejected as they introduce ambiguity
18170 		 * in the naming of the interfaces.
18171 		 * In order to confirm with existing semantics,
18172 		 * and to not break any programs/script relying
18173 		 * on that behaviour, if<0>:0 is considered to be
18174 		 * a valid interface.
18175 		 *
18176 		 * If alias has two or more digits and the first
18177 		 * is zero, fail.
18178 		 */
18179 		if (&cp[2] < endp && cp[1] == '0')
18180 			return (NULL);
18181 	}
18182 
18183 	if (cp <= name) {
18184 		cp = endp;
18185 	} else {
18186 		*cp = '\0';
18187 	}
18188 
18189 	/*
18190 	 * Look up the ILL, based on the portion of the name
18191 	 * before the slash. ill_lookup_on_name returns a held ill.
18192 	 * Temporary to check whether ill exists already. If so
18193 	 * ill_lookup_on_name will clear it.
18194 	 */
18195 	ill = ill_lookup_on_name(name, do_alloc, isv6,
18196 	    q, mp, func, error, &did_alloc);
18197 	if (cp != endp)
18198 		*cp = IPIF_SEPARATOR_CHAR;
18199 	if (ill == NULL)
18200 		return (NULL);
18201 
18202 	/* Establish the unit number in the name. */
18203 	id = 0;
18204 	if (cp < endp && *endp == '\0') {
18205 		/* If there was a colon, the unit number follows. */
18206 		cp++;
18207 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
18208 			ill_refrele(ill);
18209 			if (error != NULL)
18210 				*error = ENXIO;
18211 			return (NULL);
18212 		}
18213 	}
18214 
18215 	GRAB_CONN_LOCK(q);
18216 	mutex_enter(&ill->ill_lock);
18217 	/* Now see if there is an IPIF with this unit number. */
18218 	for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
18219 		if (ipif->ipif_id == id) {
18220 			if (zoneid != ALL_ZONES &&
18221 			    zoneid != ipif->ipif_zoneid &&
18222 			    ipif->ipif_zoneid != ALL_ZONES) {
18223 				mutex_exit(&ill->ill_lock);
18224 				RELEASE_CONN_LOCK(q);
18225 				ill_refrele(ill);
18226 				if (error != NULL)
18227 					*error = ENXIO;
18228 				return (NULL);
18229 			}
18230 			/*
18231 			 * The block comment at the start of ipif_down
18232 			 * explains the use of the macros used below
18233 			 */
18234 			if (IPIF_CAN_LOOKUP(ipif)) {
18235 				ipif_refhold_locked(ipif);
18236 				mutex_exit(&ill->ill_lock);
18237 				if (!did_alloc)
18238 					*exists = B_TRUE;
18239 				/*
18240 				 * Drop locks before calling ill_refrele
18241 				 * since it can potentially call into
18242 				 * ipif_ill_refrele_tail which can end up
18243 				 * in trying to acquire any lock.
18244 				 */
18245 				RELEASE_CONN_LOCK(q);
18246 				ill_refrele(ill);
18247 				return (ipif);
18248 			} else if (IPIF_CAN_WAIT(ipif, q)) {
18249 				ipsq = ill->ill_phyint->phyint_ipsq;
18250 				mutex_enter(&ipsq->ipsq_lock);
18251 				mutex_exit(&ill->ill_lock);
18252 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
18253 				mutex_exit(&ipsq->ipsq_lock);
18254 				RELEASE_CONN_LOCK(q);
18255 				ill_refrele(ill);
18256 				*error = EINPROGRESS;
18257 				return (NULL);
18258 			}
18259 		}
18260 	}
18261 	RELEASE_CONN_LOCK(q);
18262 
18263 	if (!do_alloc) {
18264 		mutex_exit(&ill->ill_lock);
18265 		ill_refrele(ill);
18266 		if (error != NULL)
18267 			*error = ENXIO;
18268 		return (NULL);
18269 	}
18270 
18271 	/*
18272 	 * If none found, atomically allocate and return a new one.
18273 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
18274 	 * to support "receive only" use of lo0:1 etc. as is still done
18275 	 * below as an initial guess.
18276 	 * However, this is now likely to be overriden later in ipif_up_done()
18277 	 * when we know for sure what address has been configured on the
18278 	 * interface, since we might have more than one loopback interface
18279 	 * with a loopback address, e.g. in the case of zones, and all the
18280 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
18281 	 */
18282 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
18283 		ire_type = IRE_LOOPBACK;
18284 	else
18285 		ire_type = IRE_LOCAL;
18286 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE);
18287 	if (ipif != NULL)
18288 		ipif_refhold_locked(ipif);
18289 	else if (error != NULL)
18290 		*error = ENOMEM;
18291 	mutex_exit(&ill->ill_lock);
18292 	ill_refrele(ill);
18293 	return (ipif);
18294 }
18295 
18296 /*
18297  * This routine is called whenever a new address comes up on an ipif.  If
18298  * we are configured to respond to address mask requests, then we are supposed
18299  * to broadcast an address mask reply at this time.  This routine is also
18300  * called if we are already up, but a netmask change is made.  This is legal
18301  * but might not make the system manager very popular.	(May be called
18302  * as writer.)
18303  */
18304 static void
18305 ipif_mask_reply(ipif_t *ipif)
18306 {
18307 	icmph_t	*icmph;
18308 	ipha_t	*ipha;
18309 	mblk_t	*mp;
18310 
18311 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
18312 
18313 	if (!ip_respond_to_address_mask_broadcast)
18314 		return;
18315 
18316 	/* ICMP mask reply is IPv4 only */
18317 	ASSERT(!ipif->ipif_isv6);
18318 	/* ICMP mask reply is not for a loopback interface */
18319 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
18320 
18321 	mp = allocb(REPLY_LEN, BPRI_HI);
18322 	if (mp == NULL)
18323 		return;
18324 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
18325 
18326 	ipha = (ipha_t *)mp->b_rptr;
18327 	bzero(ipha, REPLY_LEN);
18328 	*ipha = icmp_ipha;
18329 	ipha->ipha_ttl = ip_broadcast_ttl;
18330 	ipha->ipha_src = ipif->ipif_src_addr;
18331 	ipha->ipha_dst = ipif->ipif_brd_addr;
18332 	ipha->ipha_length = htons(REPLY_LEN);
18333 	ipha->ipha_ident = 0;
18334 
18335 	icmph = (icmph_t *)&ipha[1];
18336 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
18337 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
18338 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
18339 	if (icmph->icmph_checksum == 0)
18340 		icmph->icmph_checksum = 0xffff;
18341 
18342 	put(ipif->ipif_wq, mp);
18343 
18344 #undef	REPLY_LEN
18345 }
18346 
18347 /*
18348  * When the mtu in the ipif changes, we call this routine through ire_walk
18349  * to update all the relevant IREs.
18350  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18351  */
18352 static void
18353 ipif_mtu_change(ire_t *ire, char *ipif_arg)
18354 {
18355 	ipif_t *ipif = (ipif_t *)ipif_arg;
18356 
18357 	if (ire->ire_stq == NULL || ire->ire_ipif != ipif)
18358 		return;
18359 	ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET);
18360 }
18361 
18362 /*
18363  * When the mtu in the ill changes, we call this routine through ire_walk
18364  * to update all the relevant IREs.
18365  * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq.
18366  */
18367 void
18368 ill_mtu_change(ire_t *ire, char *ill_arg)
18369 {
18370 	ill_t	*ill = (ill_t *)ill_arg;
18371 
18372 	if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill)
18373 		return;
18374 	ire->ire_max_frag = ire->ire_ipif->ipif_mtu;
18375 }
18376 
18377 /*
18378  * Join the ipif specific multicast groups.
18379  * Must be called after a mapping has been set up in the resolver.  (Always
18380  * called as writer.)
18381  */
18382 void
18383 ipif_multicast_up(ipif_t *ipif)
18384 {
18385 	int err, index;
18386 	ill_t *ill;
18387 
18388 	ASSERT(IAM_WRITER_IPIF(ipif));
18389 
18390 	ill = ipif->ipif_ill;
18391 	index = ill->ill_phyint->phyint_ifindex;
18392 
18393 	ip1dbg(("ipif_multicast_up\n"));
18394 	if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up)
18395 		return;
18396 
18397 	if (ipif->ipif_isv6) {
18398 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
18399 			return;
18400 
18401 		/* Join the all hosts multicast address */
18402 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18403 		/*
18404 		 * Passing B_TRUE means we have to join the multicast
18405 		 * membership on this interface even though this is
18406 		 * FAILED. If we join on a different one in the group,
18407 		 * we will not be able to delete the membership later
18408 		 * as we currently don't track where we join when we
18409 		 * join within the kernel unlike applications where
18410 		 * we have ilg/ilg_orig_index. See ip_addmulti_v6
18411 		 * for more on this.
18412 		 */
18413 		err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index,
18414 		    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18415 		if (err != 0) {
18416 			ip0dbg(("ipif_multicast_up: "
18417 			    "all_hosts_mcast failed %d\n",
18418 			    err));
18419 			return;
18420 		}
18421 		/*
18422 		 * Enable multicast for the solicited node multicast address
18423 		 */
18424 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18425 			in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18426 
18427 			ipv6_multi.s6_addr32[3] |=
18428 			    ipif->ipif_v6lcl_addr.s6_addr32[3];
18429 
18430 			err = ip_addmulti_v6(&ipv6_multi, ill, index,
18431 			    ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE,
18432 			    NULL);
18433 			if (err != 0) {
18434 				ip0dbg(("ipif_multicast_up: solicited MC"
18435 				    " failed %d\n", err));
18436 				(void) ip_delmulti_v6(&ipv6_all_hosts_mcast,
18437 				    ill, ill->ill_phyint->phyint_ifindex,
18438 				    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18439 				return;
18440 			}
18441 		}
18442 	} else {
18443 		if (ipif->ipif_lcl_addr == INADDR_ANY)
18444 			return;
18445 
18446 		/* Join the all hosts multicast address */
18447 		ip1dbg(("ipif_multicast_up - addmulti\n"));
18448 		err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif,
18449 		    ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL);
18450 		if (err) {
18451 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
18452 			return;
18453 		}
18454 	}
18455 	ipif->ipif_multicast_up = 1;
18456 }
18457 
18458 /*
18459  * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up();
18460  * any explicit memberships are blown away in ill_leave_multicast() when the
18461  * ill is brought down.
18462  */
18463 static void
18464 ipif_multicast_down(ipif_t *ipif)
18465 {
18466 	int err;
18467 
18468 	ASSERT(IAM_WRITER_IPIF(ipif));
18469 
18470 	ip1dbg(("ipif_multicast_down\n"));
18471 	if (!ipif->ipif_multicast_up)
18472 		return;
18473 
18474 	ASSERT(ipif->ipif_isv6);
18475 
18476 	ip1dbg(("ipif_multicast_down - delmulti\n"));
18477 
18478 	/*
18479 	 * Leave the all hosts multicast address. Similar to ip_addmulti_v6,
18480 	 * we should look for ilms on this ill rather than the ones that have
18481 	 * been failed over here.  They are here temporarily. As
18482 	 * ipif_multicast_up has joined on this ill, we should delete only
18483 	 * from this ill.
18484 	 */
18485 	err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill,
18486 	    ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid,
18487 	    B_TRUE, B_TRUE);
18488 	if (err != 0) {
18489 		ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n",
18490 		    err));
18491 	}
18492 	/*
18493 	 * Disable multicast for the solicited node multicast address
18494 	 */
18495 	if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
18496 		in6_addr_t ipv6_multi = ipv6_solicited_node_mcast;
18497 
18498 		ipv6_multi.s6_addr32[3] |=
18499 		    ipif->ipif_v6lcl_addr.s6_addr32[3];
18500 
18501 		err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill,
18502 		    ipif->ipif_ill->ill_phyint->phyint_ifindex,
18503 		    ipif->ipif_zoneid, B_TRUE, B_TRUE);
18504 
18505 		if (err != 0) {
18506 			ip0dbg(("ipif_multicast_down: sol MC failed %d\n",
18507 			    err));
18508 		}
18509 	}
18510 
18511 	ipif->ipif_multicast_up = 0;
18512 }
18513 
18514 /*
18515  * Used when an interface comes up to recreate any extra routes on this
18516  * interface.
18517  */
18518 static ire_t **
18519 ipif_recover_ire(ipif_t *ipif)
18520 {
18521 	mblk_t	*mp;
18522 	ire_t	**ipif_saved_irep;
18523 	ire_t	**irep;
18524 
18525 	ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name,
18526 	    ipif->ipif_id));
18527 
18528 	mutex_enter(&ipif->ipif_saved_ire_lock);
18529 	ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) *
18530 	    ipif->ipif_saved_ire_cnt, KM_NOSLEEP);
18531 	if (ipif_saved_irep == NULL) {
18532 		mutex_exit(&ipif->ipif_saved_ire_lock);
18533 		return (NULL);
18534 	}
18535 
18536 	irep = ipif_saved_irep;
18537 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
18538 		ire_t		*ire;
18539 		queue_t		*rfq;
18540 		queue_t		*stq;
18541 		ifrt_t		*ifrt;
18542 		uchar_t		*src_addr;
18543 		uchar_t		*gateway_addr;
18544 		mblk_t		*resolver_mp;
18545 		ushort_t	type;
18546 
18547 		/*
18548 		 * When the ire was initially created and then added in
18549 		 * ip_rt_add(), it was created either using ipif->ipif_net_type
18550 		 * in the case of a traditional interface route, or as one of
18551 		 * the IRE_OFFSUBNET types (with the exception of
18552 		 * IRE_HOST_REDIRECT which is created by icmp_redirect() and
18553 		 * which we don't need to save or recover).  In the case where
18554 		 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update
18555 		 * the ire_type to IRE_IF_NORESOLVER before calling ire_add()
18556 		 * to satisfy software like GateD and Sun Cluster which creates
18557 		 * routes using the the loopback interface's address as a
18558 		 * gateway.
18559 		 *
18560 		 * As ifrt->ifrt_type reflects the already updated ire_type and
18561 		 * since ire_create() expects that IRE_IF_NORESOLVER will have
18562 		 * a valid ire_dlureq_mp field (which doesn't make sense for a
18563 		 * IRE_LOOPBACK), ire_create() will be called in the same way
18564 		 * here as in ip_rt_add(), namely using ipif->ipif_net_type when
18565 		 * the route looks like a traditional interface route (where
18566 		 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using
18567 		 * the saved ifrt->ifrt_type.  This means that in the case where
18568 		 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by
18569 		 * ire_create() will be an IRE_LOOPBACK, it will then be turned
18570 		 * into an IRE_IF_NORESOLVER and then added by ire_add().
18571 		 */
18572 		ifrt = (ifrt_t *)mp->b_rptr;
18573 		if (ifrt->ifrt_type & IRE_INTERFACE) {
18574 			rfq = NULL;
18575 			stq = (ipif->ipif_net_type == IRE_IF_RESOLVER)
18576 			    ? ipif->ipif_rq : ipif->ipif_wq;
18577 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18578 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18579 			    : (uint8_t *)&ipif->ipif_src_addr;
18580 			gateway_addr = NULL;
18581 			resolver_mp = ipif->ipif_resolver_mp;
18582 			type = ipif->ipif_net_type;
18583 		} else if (ifrt->ifrt_type & IRE_BROADCAST) {
18584 			/* Recover multiroute broadcast IRE. */
18585 			rfq = ipif->ipif_rq;
18586 			stq = ipif->ipif_wq;
18587 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18588 			    ? (uint8_t *)&ifrt->ifrt_src_addr
18589 			    : (uint8_t *)&ipif->ipif_src_addr;
18590 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18591 			resolver_mp = ipif->ipif_bcast_mp;
18592 			type = ifrt->ifrt_type;
18593 		} else {
18594 			rfq = NULL;
18595 			stq = NULL;
18596 			src_addr = (ifrt->ifrt_flags & RTF_SETSRC)
18597 			    ? (uint8_t *)&ifrt->ifrt_src_addr : NULL;
18598 			gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr;
18599 			resolver_mp = NULL;
18600 			type = ifrt->ifrt_type;
18601 		}
18602 
18603 		/*
18604 		 * Create a copy of the IRE with the saved address and netmask.
18605 		 */
18606 		ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for "
18607 		    "0x%x/0x%x\n",
18608 		    ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type,
18609 		    ntohl(ifrt->ifrt_addr),
18610 		    ntohl(ifrt->ifrt_mask)));
18611 		ire = ire_create(
18612 		    (uint8_t *)&ifrt->ifrt_addr,
18613 		    (uint8_t *)&ifrt->ifrt_mask,
18614 		    src_addr,
18615 		    gateway_addr,
18616 		    NULL,
18617 		    &ifrt->ifrt_max_frag,
18618 		    NULL,
18619 		    rfq,
18620 		    stq,
18621 		    type,
18622 		    resolver_mp,
18623 		    ipif,
18624 		    NULL,
18625 		    0,
18626 		    0,
18627 		    0,
18628 		    ifrt->ifrt_flags,
18629 		    &ifrt->ifrt_iulp_info,
18630 		    NULL,
18631 		    NULL);
18632 
18633 		if (ire == NULL) {
18634 			mutex_exit(&ipif->ipif_saved_ire_lock);
18635 			kmem_free(ipif_saved_irep,
18636 			    ipif->ipif_saved_ire_cnt * sizeof (ire_t *));
18637 			return (NULL);
18638 		}
18639 
18640 		/*
18641 		 * Some software (for example, GateD and Sun Cluster) attempts
18642 		 * to create (what amount to) IRE_PREFIX routes with the
18643 		 * loopback address as the gateway.  This is primarily done to
18644 		 * set up prefixes with the RTF_REJECT flag set (for example,
18645 		 * when generating aggregate routes.)
18646 		 *
18647 		 * If the IRE type (as defined by ipif->ipif_net_type) is
18648 		 * IRE_LOOPBACK, then we map the request into a
18649 		 * IRE_IF_NORESOLVER.
18650 		 */
18651 		if (ipif->ipif_net_type == IRE_LOOPBACK)
18652 			ire->ire_type = IRE_IF_NORESOLVER;
18653 		/*
18654 		 * ire held by ire_add, will be refreled' towards the
18655 		 * the end of ipif_up_done
18656 		 */
18657 		(void) ire_add(&ire, NULL, NULL, NULL);
18658 		*irep = ire;
18659 		irep++;
18660 		ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire));
18661 	}
18662 	mutex_exit(&ipif->ipif_saved_ire_lock);
18663 	return (ipif_saved_irep);
18664 }
18665 
18666 /*
18667  * Used to set the netmask and broadcast address to default values when the
18668  * interface is brought up.  (Always called as writer.)
18669  */
18670 static void
18671 ipif_set_default(ipif_t *ipif)
18672 {
18673 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
18674 
18675 	if (!ipif->ipif_isv6) {
18676 		/*
18677 		 * Interface holds an IPv4 address. Default
18678 		 * mask is the natural netmask.
18679 		 */
18680 		if (!ipif->ipif_net_mask) {
18681 			ipaddr_t	v4mask;
18682 
18683 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
18684 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
18685 		}
18686 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18687 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18688 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18689 		} else {
18690 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18691 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18692 		}
18693 		/*
18694 		 * NOTE: SunOS 4.X does this even if the broadcast address
18695 		 * has been already set thus we do the same here.
18696 		 */
18697 		if (ipif->ipif_flags & IPIF_BROADCAST) {
18698 			ipaddr_t	v4addr;
18699 
18700 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
18701 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
18702 		}
18703 	} else {
18704 		/*
18705 		 * Interface holds an IPv6-only address.  Default
18706 		 * mask is all-ones.
18707 		 */
18708 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
18709 			ipif->ipif_v6net_mask = ipv6_all_ones;
18710 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
18711 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
18712 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
18713 		} else {
18714 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
18715 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
18716 		}
18717 	}
18718 }
18719 
18720 /*
18721  * Return 0 if this address can be used as local address without causing
18722  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
18723  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
18724  * Special checks are needed to allow the same IPv6 link-local address
18725  * on different ills.
18726  * TODO: allowing the same site-local address on different ill's.
18727  */
18728 int
18729 ip_addr_availability_check(ipif_t *new_ipif)
18730 {
18731 	in6_addr_t our_v6addr;
18732 	ill_t *ill;
18733 	ipif_t *ipif;
18734 	ill_walk_context_t ctx;
18735 
18736 	ASSERT(IAM_WRITER_IPIF(new_ipif));
18737 	ASSERT(MUTEX_HELD(&ip_addr_avail_lock));
18738 	ASSERT(RW_READ_HELD(&ill_g_lock));
18739 
18740 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
18741 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
18742 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
18743 		return (0);
18744 
18745 	our_v6addr = new_ipif->ipif_v6lcl_addr;
18746 
18747 	if (new_ipif->ipif_isv6)
18748 		ill = ILL_START_WALK_V6(&ctx);
18749 	else
18750 		ill = ILL_START_WALK_V4(&ctx);
18751 
18752 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
18753 		for (ipif = ill->ill_ipif; ipif != NULL;
18754 		    ipif = ipif->ipif_next) {
18755 			if ((ipif == new_ipif) ||
18756 			    !(ipif->ipif_flags & IPIF_UP) ||
18757 			    (ipif->ipif_flags & IPIF_UNNUMBERED))
18758 				continue;
18759 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
18760 			    &our_v6addr)) {
18761 				if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
18762 				    new_ipif->ipif_flags |= IPIF_UNNUMBERED;
18763 				else if (ipif->ipif_flags & IPIF_POINTOPOINT)
18764 				    ipif->ipif_flags |= IPIF_UNNUMBERED;
18765 				else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) &&
18766 				    new_ipif->ipif_ill != ill)
18767 					continue;
18768 				else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) &&
18769 				    new_ipif->ipif_ill != ill)
18770 					continue;
18771 				else if (new_ipif->ipif_zoneid !=
18772 				    ipif->ipif_zoneid &&
18773 				    ipif->ipif_zoneid != ALL_ZONES &&
18774 				    (ill->ill_phyint->phyint_flags &
18775 				    PHYI_LOOPBACK))
18776 					continue;
18777 				else if (new_ipif->ipif_ill == ill)
18778 					return (EADDRINUSE);
18779 				else
18780 					return (EADDRNOTAVAIL);
18781 			}
18782 		}
18783 	}
18784 
18785 	return (0);
18786 }
18787 
18788 /*
18789  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
18790  * IREs for the ipif.
18791  * When the routine returns EINPROGRESS then mp has been consumed and
18792  * the ioctl will be acked from ip_rput_dlpi.
18793  */
18794 static int
18795 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
18796 {
18797 	ill_t	*ill = ipif->ipif_ill;
18798 	boolean_t isv6 = ipif->ipif_isv6;
18799 	int	err = 0;
18800 	boolean_t success;
18801 
18802 	ASSERT(IAM_WRITER_IPIF(ipif));
18803 
18804 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
18805 
18806 	/* Shouldn't get here if it is already up. */
18807 	if (ipif->ipif_flags & IPIF_UP)
18808 		return (EALREADY);
18809 
18810 	/* Skip arp/ndp for any loopback interface. */
18811 	if (ill->ill_wq != NULL) {
18812 		conn_t *connp = Q_TO_CONN(q);
18813 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
18814 
18815 		if (!ill->ill_dl_up) {
18816 			/*
18817 			 * ill_dl_up is not yet set. i.e. we are yet to
18818 			 * DL_BIND with the driver and this is the first
18819 			 * logical interface on the ill to become "up".
18820 			 * Tell the driver to get going (via DL_BIND_REQ).
18821 			 * Note that changing "significant" IFF_ flags
18822 			 * address/netmask etc cause a down/up dance, but
18823 			 * does not cause an unbind (DL_UNBIND) with the driver
18824 			 */
18825 			return (ill_dl_up(ill, ipif, mp, q));
18826 		}
18827 
18828 		/*
18829 		 * ipif_resolver_up may end up sending an
18830 		 * AR_INTERFACE_UP message to ARP, which would, in
18831 		 * turn send a DLPI message to the driver. ioctls are
18832 		 * serialized and so we cannot send more than one
18833 		 * interface up message at a time. If ipif_resolver_up
18834 		 * does send an interface up message to ARP, we get
18835 		 * EINPROGRESS and we will complete in ip_arp_done.
18836 		 */
18837 
18838 		ASSERT(connp != NULL);
18839 		ASSERT(ipsq->ipsq_pending_mp == NULL);
18840 		mutex_enter(&connp->conn_lock);
18841 		mutex_enter(&ill->ill_lock);
18842 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18843 		mutex_exit(&ill->ill_lock);
18844 		mutex_exit(&connp->conn_lock);
18845 		if (!success)
18846 			return (EINTR);
18847 
18848 		/*
18849 		 * Crank up IPv6 neighbor discovery
18850 		 * Unlike ARP, this should complete when
18851 		 * ipif_ndp_up returns. However, for
18852 		 * ILLF_XRESOLV interfaces we also send a
18853 		 * AR_INTERFACE_UP to the external resolver.
18854 		 * That ioctl will complete in ip_rput.
18855 		 */
18856 		if (isv6) {
18857 			err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr,
18858 			    B_FALSE);
18859 			if (err != 0) {
18860 				mp = ipsq_pending_mp_get(ipsq, &connp);
18861 				return (err);
18862 			}
18863 		}
18864 		/* Now, ARP */
18865 		if ((err = ipif_resolver_up(ipif, B_FALSE)) ==
18866 		    EINPROGRESS) {
18867 			/* We will complete it in ip_arp_done */
18868 			return (err);
18869 		}
18870 		mp = ipsq_pending_mp_get(ipsq, &connp);
18871 		ASSERT(mp != NULL);
18872 		if (err != 0)
18873 			return (err);
18874 	}
18875 	return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
18876 }
18877 
18878 /*
18879  * Perform a bind for the physical device.
18880  * When the routine returns EINPROGRESS then mp has been consumed and
18881  * the ioctl will be acked from ip_rput_dlpi.
18882  * Allocate an unbind message and save it until ipif_down.
18883  */
18884 static int
18885 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
18886 {
18887 	mblk_t	*areq_mp = NULL;
18888 	mblk_t	*bind_mp = NULL;
18889 	mblk_t	*unbind_mp = NULL;
18890 	conn_t	*connp;
18891 	boolean_t success;
18892 
18893 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
18894 	ASSERT(IAM_WRITER_ILL(ill));
18895 
18896 	ASSERT(mp != NULL);
18897 
18898 	/* Create a resolver cookie for ARP */
18899 	if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) {
18900 		areq_t		*areq;
18901 		uint16_t	sap_addr;
18902 
18903 		areq_mp = ill_arp_alloc(ill,
18904 			(uchar_t *)&ip_areq_template, 0);
18905 		if (areq_mp == NULL) {
18906 			return (ENOMEM);
18907 		}
18908 		freemsg(ill->ill_resolver_mp);
18909 		ill->ill_resolver_mp = areq_mp;
18910 		areq = (areq_t *)areq_mp->b_rptr;
18911 		sap_addr = ill->ill_sap;
18912 		bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr));
18913 		/*
18914 		 * Wait till we call ill_pending_mp_add to determine
18915 		 * the success before we free the ill_resolver_mp and
18916 		 * attach areq_mp in it's place.
18917 		 */
18918 	}
18919 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
18920 	    DL_BIND_REQ);
18921 	if (bind_mp == NULL)
18922 		goto bad;
18923 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
18924 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
18925 
18926 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
18927 	if (unbind_mp == NULL)
18928 		goto bad;
18929 
18930 	/*
18931 	 * Record state needed to complete this operation when the
18932 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
18933 	 */
18934 	if (WR(q)->q_next == NULL) {
18935 		connp = Q_TO_CONN(q);
18936 		mutex_enter(&connp->conn_lock);
18937 	} else {
18938 		connp = NULL;
18939 	}
18940 	mutex_enter(&ipif->ipif_ill->ill_lock);
18941 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
18942 	mutex_exit(&ipif->ipif_ill->ill_lock);
18943 	if (connp != NULL)
18944 		mutex_exit(&connp->conn_lock);
18945 	if (!success)
18946 		goto bad;
18947 
18948 	/*
18949 	 * Save the unbind message for ill_dl_down(); it will be consumed when
18950 	 * the interface goes down.
18951 	 */
18952 	ASSERT(ill->ill_unbind_mp == NULL);
18953 	ill->ill_unbind_mp = unbind_mp;
18954 
18955 	ill_dlpi_send(ill, bind_mp);
18956 	/* Send down link-layer capabilities probe if not already done. */
18957 	ill_capability_probe(ill);
18958 
18959 	/*
18960 	 * Sysid used to rely on the fact that netboots set domainname
18961 	 * and the like. Now that miniroot boots aren't strictly netboots
18962 	 * and miniroot network configuration is driven from userland
18963 	 * these things still need to be set. This situation can be detected
18964 	 * by comparing the interface being configured here to the one
18965 	 * dhcack was set to reference by the boot loader. Once sysid is
18966 	 * converted to use dhcp_ipc_getinfo() this call can go away.
18967 	 */
18968 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) &&
18969 	    (strcmp(ill->ill_name, dhcack) == 0) &&
18970 	    (strlen(srpc_domain) == 0)) {
18971 		if (dhcpinit() != 0)
18972 			cmn_err(CE_WARN, "no cached dhcp response");
18973 	}
18974 
18975 	/*
18976 	 * This operation will complete in ip_rput_dlpi with either
18977 	 * a DL_BIND_ACK or DL_ERROR_ACK.
18978 	 */
18979 	return (EINPROGRESS);
18980 bad:
18981 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
18982 	/*
18983 	 * We don't have to check for possible removal from illgrp
18984 	 * as we have not yet inserted in illgrp. For groups
18985 	 * without names, this ipif is still not UP and hence
18986 	 * this could not have possibly had any influence in forming
18987 	 * groups.
18988 	 */
18989 
18990 	if (bind_mp != NULL)
18991 		freemsg(bind_mp);
18992 	if (unbind_mp != NULL)
18993 		freemsg(unbind_mp);
18994 	return (ENOMEM);
18995 }
18996 
18997 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
18998 
18999 /*
19000  * DLPI and ARP is up.
19001  * Create all the IREs associated with an interface bring up multicast.
19002  * Set the interface flag and finish other initialization
19003  * that potentially had to be differed to after DL_BIND_ACK.
19004  */
19005 int
19006 ipif_up_done(ipif_t *ipif)
19007 {
19008 	ire_t	*ire_array[20];
19009 	ire_t	**irep = ire_array;
19010 	ire_t	**irep1;
19011 	ipaddr_t net_mask = 0;
19012 	ipaddr_t subnet_mask, route_mask;
19013 	ill_t	*ill = ipif->ipif_ill;
19014 	queue_t	*stq;
19015 	ipif_t	 *src_ipif;
19016 	ipif_t   *tmp_ipif;
19017 	boolean_t	flush_ire_cache = B_TRUE;
19018 	int	err = 0;
19019 	phyint_t *phyi;
19020 	ire_t	**ipif_saved_irep = NULL;
19021 	int ipif_saved_ire_cnt;
19022 	int	cnt;
19023 	boolean_t	src_ipif_held = B_FALSE;
19024 	boolean_t	ire_added = B_FALSE;
19025 	boolean_t	loopback = B_FALSE;
19026 
19027 	ip1dbg(("ipif_up_done(%s:%u)\n",
19028 		ipif->ipif_ill->ill_name, ipif->ipif_id));
19029 	/* Check if this is a loopback interface */
19030 	if (ipif->ipif_ill->ill_wq == NULL)
19031 		loopback = B_TRUE;
19032 
19033 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19034 	/*
19035 	 * If all other interfaces for this ill are down or DEPRECATED,
19036 	 * or otherwise unsuitable for source address selection, remove
19037 	 * any IRE_CACHE entries for this ill to make sure source
19038 	 * address selection gets to take this new ipif into account.
19039 	 * No need to hold ill_lock while traversing the ipif list since
19040 	 * we are writer
19041 	 */
19042 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
19043 		tmp_ipif = tmp_ipif->ipif_next) {
19044 		if (((tmp_ipif->ipif_flags &
19045 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
19046 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
19047 		    (tmp_ipif == ipif))
19048 			continue;
19049 		/* first useable pre-existing interface */
19050 		flush_ire_cache = B_FALSE;
19051 		break;
19052 	}
19053 	if (flush_ire_cache)
19054 		ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE,
19055 		    IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill);
19056 
19057 	/*
19058 	 * Figure out which way the send-to queue should go.  Only
19059 	 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK
19060 	 * should show up here.
19061 	 */
19062 	switch (ill->ill_net_type) {
19063 	case IRE_IF_RESOLVER:
19064 		stq = ill->ill_rq;
19065 		break;
19066 	case IRE_IF_NORESOLVER:
19067 	case IRE_LOOPBACK:
19068 		stq = ill->ill_wq;
19069 		break;
19070 	default:
19071 		return (EINVAL);
19072 	}
19073 
19074 	if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) {
19075 		/*
19076 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
19077 		 * ipif_lookup_on_name(), but in the case of zones we can have
19078 		 * several loopback addresses on lo0. So all the interfaces with
19079 		 * loopback addresses need to be marked IRE_LOOPBACK.
19080 		 */
19081 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
19082 		    htonl(INADDR_LOOPBACK))
19083 			ipif->ipif_ire_type = IRE_LOOPBACK;
19084 		else
19085 			ipif->ipif_ire_type = IRE_LOCAL;
19086 	}
19087 
19088 	if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) {
19089 		/*
19090 		 * Can't use our source address. Select a different
19091 		 * source address for the IRE_INTERFACE and IRE_LOCAL
19092 		 */
19093 		src_ipif = ipif_select_source(ipif->ipif_ill,
19094 		    ipif->ipif_subnet, ipif->ipif_zoneid);
19095 		if (src_ipif == NULL)
19096 			src_ipif = ipif;	/* Last resort */
19097 		else
19098 			src_ipif_held = B_TRUE;
19099 	} else {
19100 		src_ipif = ipif;
19101 	}
19102 
19103 	/* Create all the IREs associated with this interface */
19104 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19105 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19106 
19107 		/*
19108 		 * If we're on a labeled system then make sure that zone-
19109 		 * private addresses have proper remote host database entries.
19110 		 */
19111 		if (is_system_labeled() &&
19112 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
19113 		    !tsol_check_interface_address(ipif))
19114 			return (EINVAL);
19115 
19116 		/* Register the source address for __sin6_src_id */
19117 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
19118 		    ipif->ipif_zoneid);
19119 		if (err != 0) {
19120 			ip0dbg(("ipif_up_done: srcid_insert %d\n", err));
19121 			return (err);
19122 		}
19123 
19124 		/* If the interface address is set, create the local IRE. */
19125 		ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n",
19126 			(void *)ipif,
19127 			ipif->ipif_ire_type,
19128 			ntohl(ipif->ipif_lcl_addr)));
19129 		*irep++ = ire_create(
19130 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
19131 		    (uchar_t *)&ip_g_all_ones,		/* mask */
19132 		    (uchar_t *)&src_ipif->ipif_src_addr, /* source address */
19133 		    NULL,				/* no gateway */
19134 		    NULL,
19135 		    &ip_loopback_mtuplus,		/* max frag size */
19136 		    NULL,
19137 		    ipif->ipif_rq,			/* recv-from queue */
19138 		    NULL,				/* no send-to queue */
19139 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
19140 		    NULL,
19141 		    ipif,
19142 		    NULL,
19143 		    0,
19144 		    0,
19145 		    0,
19146 		    (ipif->ipif_flags & IPIF_PRIVATE) ?
19147 		    RTF_PRIVATE : 0,
19148 		    &ire_uinfo_null,
19149 		    NULL,
19150 		    NULL);
19151 	} else {
19152 		ip1dbg((
19153 		    "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n",
19154 		    ipif->ipif_ire_type,
19155 		    ntohl(ipif->ipif_lcl_addr),
19156 		    (uint_t)ipif->ipif_flags));
19157 	}
19158 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
19159 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
19160 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
19161 	} else {
19162 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
19163 	}
19164 
19165 	subnet_mask = ipif->ipif_net_mask;
19166 
19167 	/*
19168 	 * If mask was not specified, use natural netmask of
19169 	 * interface address. Also, store this mask back into the
19170 	 * ipif struct.
19171 	 */
19172 	if (subnet_mask == 0) {
19173 		subnet_mask = net_mask;
19174 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
19175 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
19176 		    ipif->ipif_v6subnet);
19177 	}
19178 
19179 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
19180 	if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) &&
19181 	    ipif->ipif_subnet != INADDR_ANY) {
19182 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
19183 
19184 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
19185 			route_mask = IP_HOST_MASK;
19186 		} else {
19187 			route_mask = subnet_mask;
19188 		}
19189 
19190 		ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p "
19191 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
19192 			(void *)ipif, (void *)ill,
19193 			ill->ill_net_type,
19194 			ntohl(ipif->ipif_subnet)));
19195 		*irep++ = ire_create(
19196 		    (uchar_t *)&ipif->ipif_subnet,	/* dest address */
19197 		    (uchar_t *)&route_mask,		/* mask */
19198 		    (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
19199 		    NULL,				/* no gateway */
19200 		    NULL,
19201 		    &ipif->ipif_mtu,			/* max frag */
19202 		    NULL,
19203 		    NULL,				/* no recv queue */
19204 		    stq,				/* send-to queue */
19205 		    ill->ill_net_type,			/* IF_[NO]RESOLVER */
19206 		    ill->ill_resolver_mp,		/* xmit header */
19207 		    ipif,
19208 		    NULL,
19209 		    0,
19210 		    0,
19211 		    0,
19212 		    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0,
19213 		    &ire_uinfo_null,
19214 		    NULL,
19215 		    NULL);
19216 	}
19217 
19218 	/*
19219 	 * If the interface address is set, create the broadcast IREs.
19220 	 *
19221 	 * ire_create_bcast checks if the proposed new IRE matches
19222 	 * any existing IRE's with the same physical interface (ILL).
19223 	 * This should get rid of duplicates.
19224 	 * ire_create_bcast also check IPIF_NOXMIT and does not create
19225 	 * any broadcast ires.
19226 	 */
19227 	if ((ipif->ipif_subnet != INADDR_ANY) &&
19228 	    (ipif->ipif_flags & IPIF_BROADCAST)) {
19229 		ipaddr_t addr;
19230 
19231 		ip1dbg(("ipif_up_done: creating broadcast IRE\n"));
19232 		irep = ire_check_and_create_bcast(ipif, 0, irep,
19233 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19234 		irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep,
19235 		    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19236 
19237 		/*
19238 		 * For backward compatibility, we need to create net
19239 		 * broadcast ire's based on the old "IP address class
19240 		 * system."  The reason is that some old machines only
19241 		 * respond to these class derived net broadcast.
19242 		 *
19243 		 * But we should not create these net broadcast ire's if
19244 		 * the subnet_mask is shorter than the IP address class based
19245 		 * derived netmask.  Otherwise, we may create a net
19246 		 * broadcast address which is the same as an IP address
19247 		 * on the subnet.  Then TCP will refuse to talk to that
19248 		 * address.
19249 		 *
19250 		 * Nor do we need IRE_BROADCAST ire's for the interface
19251 		 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that
19252 		 * interface is already created.  Creating these broadcast
19253 		 * ire's will only create confusion as the "addr" is going
19254 		 * to be same as that of the IP address of the interface.
19255 		 */
19256 		if (net_mask < subnet_mask) {
19257 			addr = net_mask & ipif->ipif_subnet;
19258 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19259 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19260 			irep = ire_check_and_create_bcast(ipif,
19261 			    ~net_mask | addr, irep,
19262 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19263 		}
19264 
19265 		if (subnet_mask != 0xFFFFFFFF) {
19266 			addr = ipif->ipif_subnet;
19267 			irep = ire_check_and_create_bcast(ipif, addr, irep,
19268 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19269 			irep = ire_check_and_create_bcast(ipif,
19270 			    ~subnet_mask|addr, irep,
19271 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19272 		}
19273 	}
19274 
19275 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
19276 
19277 	/* If an earlier ire_create failed, get out now */
19278 	for (irep1 = irep; irep1 > ire_array; ) {
19279 		irep1--;
19280 		if (*irep1 == NULL) {
19281 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
19282 			err = ENOMEM;
19283 			goto bad;
19284 		}
19285 	}
19286 
19287 	/*
19288 	 * Need to atomically check for ip_addr_availablity_check
19289 	 * under ip_addr_avail_lock, and if it fails got bad, and remove
19290 	 * from group also.The ill_g_lock is grabbed as reader
19291 	 * just to make sure no new ills or new ipifs are being added
19292 	 * to the system while we are checking the uniqueness of addresses.
19293 	 */
19294 	rw_enter(&ill_g_lock, RW_READER);
19295 	mutex_enter(&ip_addr_avail_lock);
19296 	/* Mark it up, and increment counters. */
19297 	ill->ill_ipif_up_count++;
19298 	ipif->ipif_flags |= IPIF_UP;
19299 	err = ip_addr_availability_check(ipif);
19300 	mutex_exit(&ip_addr_avail_lock);
19301 	rw_exit(&ill_g_lock);
19302 
19303 	if (err != 0) {
19304 		/*
19305 		 * Our address may already be up on the same ill. In this case,
19306 		 * the ARP entry for our ipif replaced the one for the other
19307 		 * ipif. So we don't want to delete it (otherwise the other ipif
19308 		 * would be unable to send packets).
19309 		 * ip_addr_availability_check() identifies this case for us and
19310 		 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL
19311 		 * which is the expected error code.
19312 		 */
19313 		if (err == EADDRINUSE) {
19314 			freemsg(ipif->ipif_arp_del_mp);
19315 			ipif->ipif_arp_del_mp = NULL;
19316 			err = EADDRNOTAVAIL;
19317 		}
19318 		ill->ill_ipif_up_count--;
19319 		ipif->ipif_flags &= ~IPIF_UP;
19320 		goto bad;
19321 	}
19322 
19323 	/*
19324 	 * Add in all newly created IREs.  ire_create_bcast() has
19325 	 * already checked for duplicates of the IRE_BROADCAST type.
19326 	 * We want to add before we call ifgrp_insert which wants
19327 	 * to know whether IRE_IF_RESOLVER exists or not.
19328 	 *
19329 	 * NOTE : We refrele the ire though we may branch to "bad"
19330 	 *	  later on where we do ire_delete. This is okay
19331 	 *	  because nobody can delete it as we are running
19332 	 *	  exclusively.
19333 	 */
19334 	for (irep1 = irep; irep1 > ire_array; ) {
19335 		irep1--;
19336 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock)));
19337 		/*
19338 		 * refheld by ire_add. refele towards the end of the func
19339 		 */
19340 		(void) ire_add(irep1, NULL, NULL, NULL);
19341 	}
19342 	ire_added = B_TRUE;
19343 	/*
19344 	 * Form groups if possible.
19345 	 *
19346 	 * If we are supposed to be in a ill_group with a name, insert it
19347 	 * now as we know that at least one ipif is UP. Otherwise form
19348 	 * nameless groups.
19349 	 *
19350 	 * If ip_enable_group_ifs is set and ipif address is not 0, insert
19351 	 * this ipif into the appropriate interface group, or create a
19352 	 * new one. If this is already in a nameless group, we try to form
19353 	 * a bigger group looking at other ills potentially sharing this
19354 	 * ipif's prefix.
19355 	 */
19356 	phyi = ill->ill_phyint;
19357 	if (phyi->phyint_groupname_len != 0) {
19358 		ASSERT(phyi->phyint_groupname != NULL);
19359 		if (ill->ill_ipif_up_count == 1) {
19360 			ASSERT(ill->ill_group == NULL);
19361 			err = illgrp_insert(&illgrp_head_v4, ill,
19362 			    phyi->phyint_groupname, NULL, B_TRUE);
19363 			if (err != 0) {
19364 				ip1dbg(("ipif_up_done: illgrp allocation "
19365 				    "failed, error %d\n", err));
19366 				goto bad;
19367 			}
19368 		}
19369 		ASSERT(ill->ill_group != NULL);
19370 	}
19371 
19372 	/*
19373 	 * When this is part of group, we need to make sure that
19374 	 * any broadcast ires created because of this ipif coming
19375 	 * UP gets marked/cleared with IRE_MARK_NORECV appropriately
19376 	 * so that we don't receive duplicate broadcast packets.
19377 	 */
19378 	if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0)
19379 		ipif_renominate_bcast(ipif);
19380 
19381 	/* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */
19382 	ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt;
19383 	ipif_saved_irep = ipif_recover_ire(ipif);
19384 
19385 	if (!loopback) {
19386 		/*
19387 		 * If the broadcast address has been set, make sure it makes
19388 		 * sense based on the interface address.
19389 		 * Only match on ill since we are sharing broadcast addresses.
19390 		 */
19391 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
19392 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
19393 			ire_t	*ire;
19394 
19395 			ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0,
19396 			    IRE_BROADCAST, ipif, ALL_ZONES,
19397 			    NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL));
19398 
19399 			if (ire == NULL) {
19400 				/*
19401 				 * If there isn't a matching broadcast IRE,
19402 				 * revert to the default for this netmask.
19403 				 */
19404 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
19405 				mutex_enter(&ipif->ipif_ill->ill_lock);
19406 				ipif_set_default(ipif);
19407 				mutex_exit(&ipif->ipif_ill->ill_lock);
19408 			} else {
19409 				ire_refrele(ire);
19410 			}
19411 		}
19412 
19413 	}
19414 
19415 
19416 	/* This is the first interface on this ill */
19417 	if (ipif->ipif_ipif_up_count == 1 && !loopback) {
19418 		/*
19419 		 * Need to recover all multicast memberships in the driver.
19420 		 * This had to be deferred until we had attached.
19421 		 */
19422 		ill_recover_multicast(ill);
19423 	}
19424 	/* Join the allhosts multicast address */
19425 	ipif_multicast_up(ipif);
19426 
19427 	if (!loopback) {
19428 		/*
19429 		 * See whether anybody else would benefit from the
19430 		 * new ipif that we added. We call this always rather
19431 		 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST
19432 		 * ipif is for the benefit of illgrp_insert (done above)
19433 		 * which does not do source address selection as it does
19434 		 * not want to re-create interface routes that we are
19435 		 * having reference to it here.
19436 		 */
19437 		ill_update_source_selection(ill);
19438 	}
19439 
19440 	for (irep1 = irep; irep1 > ire_array; ) {
19441 		irep1--;
19442 		if (*irep1 != NULL) {
19443 			/* was held in ire_add */
19444 			ire_refrele(*irep1);
19445 		}
19446 	}
19447 
19448 	cnt = ipif_saved_ire_cnt;
19449 	for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) {
19450 		if (*irep1 != NULL) {
19451 			/* was held in ire_add */
19452 			ire_refrele(*irep1);
19453 		}
19454 	}
19455 
19456 	/*
19457 	 * This had to be deferred until we had bound.
19458 	 * tell routing sockets that this interface is up
19459 	 */
19460 	ip_rts_ifmsg(ipif);
19461 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
19462 
19463 	if (!loopback) {
19464 		/* Broadcast an address mask reply. */
19465 		ipif_mask_reply(ipif);
19466 	}
19467 	if (ipif_saved_irep != NULL) {
19468 		kmem_free(ipif_saved_irep,
19469 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19470 	}
19471 	if (src_ipif_held)
19472 		ipif_refrele(src_ipif);
19473 	/* Let SCTP update the status for this ipif */
19474 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
19475 	return (0);
19476 
19477 bad:
19478 	ip1dbg(("ipif_up_done: FAILED \n"));
19479 	/*
19480 	 * We don't have to bother removing from ill groups because
19481 	 *
19482 	 * 1) For groups with names, we insert only when the first ipif
19483 	 *    comes up. In that case if it fails, it will not be in any
19484 	 *    group. So, we need not try to remove for that case.
19485 	 *
19486 	 * 2) For groups without names, either we tried to insert ipif_ill
19487 	 *    in a group as singleton or found some other group to become
19488 	 *    a bigger group. For the former, if it fails we don't have
19489 	 *    anything to do as ipif_ill is not in the group and for the
19490 	 *    latter, there are no failures in illgrp_insert/illgrp_delete
19491 	 *    (ENOMEM can't occur for this. Check ifgrp_insert).
19492 	 */
19493 	while (irep > ire_array) {
19494 		irep--;
19495 		if (*irep != NULL) {
19496 			ire_delete(*irep);
19497 			if (ire_added)
19498 				ire_refrele(*irep);
19499 		}
19500 	}
19501 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid);
19502 
19503 	if (ipif_saved_irep != NULL) {
19504 		kmem_free(ipif_saved_irep,
19505 		    ipif_saved_ire_cnt * sizeof (ire_t *));
19506 	}
19507 	if (src_ipif_held)
19508 		ipif_refrele(src_ipif);
19509 
19510 	ipif_arp_down(ipif);
19511 	return (err);
19512 }
19513 
19514 /*
19515  * Turn off the ARP with the ILLF_NOARP flag.
19516  */
19517 static int
19518 ill_arp_off(ill_t *ill)
19519 {
19520 	mblk_t	*arp_off_mp = NULL;
19521 	mblk_t	*arp_on_mp = NULL;
19522 
19523 	ip1dbg(("ill_arp_off(%s)\n", ill->ill_name));
19524 
19525 	ASSERT(IAM_WRITER_ILL(ill));
19526 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19527 
19528 	/*
19529 	 * If the on message is still around we've already done
19530 	 * an arp_off without doing an arp_on thus there is no
19531 	 * work needed.
19532 	 */
19533 	if (ill->ill_arp_on_mp != NULL)
19534 		return (0);
19535 
19536 	/*
19537 	 * Allocate an ARP on message (to be saved) and an ARP off message
19538 	 */
19539 	arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0);
19540 	if (!arp_off_mp)
19541 		return (ENOMEM);
19542 
19543 	arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0);
19544 	if (!arp_on_mp)
19545 		goto failed;
19546 
19547 	ASSERT(ill->ill_arp_on_mp == NULL);
19548 	ill->ill_arp_on_mp = arp_on_mp;
19549 
19550 	/* Send an AR_INTERFACE_OFF request */
19551 	putnext(ill->ill_rq, arp_off_mp);
19552 	return (0);
19553 failed:
19554 
19555 	if (arp_off_mp)
19556 		freemsg(arp_off_mp);
19557 	return (ENOMEM);
19558 }
19559 
19560 /*
19561  * Turn on ARP by turning off the ILLF_NOARP flag.
19562  */
19563 static int
19564 ill_arp_on(ill_t *ill)
19565 {
19566 	mblk_t	*mp;
19567 
19568 	ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name));
19569 
19570 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
19571 
19572 	ASSERT(IAM_WRITER_ILL(ill));
19573 	/*
19574 	 * Send an AR_INTERFACE_ON request if we have already done
19575 	 * an arp_off (which allocated the message).
19576 	 */
19577 	if (ill->ill_arp_on_mp != NULL) {
19578 		mp = ill->ill_arp_on_mp;
19579 		ill->ill_arp_on_mp = NULL;
19580 		putnext(ill->ill_rq, mp);
19581 	}
19582 	return (0);
19583 }
19584 
19585 /*
19586  * Called after either deleting ill from the group or when setting
19587  * FAILED or STANDBY on the interface.
19588  */
19589 static void
19590 illgrp_reset_schednext(ill_t *ill)
19591 {
19592 	ill_group_t *illgrp;
19593 	ill_t *save_ill;
19594 
19595 	ASSERT(IAM_WRITER_ILL(ill));
19596 	/*
19597 	 * When called from illgrp_delete, ill_group will be non-NULL.
19598 	 * But when called from ip_sioctl_flags, it could be NULL if
19599 	 * somebody is setting FAILED/INACTIVE on some interface which
19600 	 * is not part of a group.
19601 	 */
19602 	illgrp = ill->ill_group;
19603 	if (illgrp == NULL)
19604 		return;
19605 	if (illgrp->illgrp_ill_schednext != ill)
19606 		return;
19607 
19608 	illgrp->illgrp_ill_schednext = NULL;
19609 	save_ill = ill;
19610 	/*
19611 	 * Choose a good ill to be the next one for
19612 	 * outbound traffic. As the flags FAILED/STANDBY is
19613 	 * not yet marked when called from ip_sioctl_flags,
19614 	 * we check for ill separately.
19615 	 */
19616 	for (ill = illgrp->illgrp_ill; ill != NULL;
19617 	    ill = ill->ill_group_next) {
19618 		if ((ill != save_ill) &&
19619 		    !(ill->ill_phyint->phyint_flags &
19620 		    (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) {
19621 			illgrp->illgrp_ill_schednext = ill;
19622 			return;
19623 		}
19624 	}
19625 }
19626 
19627 /*
19628  * Given an ill, find the next ill in the group to be scheduled.
19629  * (This should be called by ip_newroute() before ire_create().)
19630  * The passed in ill may be pulled out of the group, after we have picked
19631  * up a different outgoing ill from the same group. However ire add will
19632  * atomically check this.
19633  */
19634 ill_t *
19635 illgrp_scheduler(ill_t *ill)
19636 {
19637 	ill_t *retill;
19638 	ill_group_t *illgrp;
19639 	int illcnt;
19640 	int i;
19641 	uint64_t flags;
19642 
19643 	/*
19644 	 * We don't use a lock to check for the ill_group. If this ill
19645 	 * is currently being inserted we may end up just returning this
19646 	 * ill itself. That is ok.
19647 	 */
19648 	if (ill->ill_group == NULL) {
19649 		ill_refhold(ill);
19650 		return (ill);
19651 	}
19652 
19653 	/*
19654 	 * Grab the ill_g_lock as reader to make sure we are dealing with
19655 	 * a set of stable ills. No ill can be added or deleted or change
19656 	 * group while we hold the reader lock.
19657 	 */
19658 	rw_enter(&ill_g_lock, RW_READER);
19659 	if ((illgrp = ill->ill_group) == NULL) {
19660 		rw_exit(&ill_g_lock);
19661 		ill_refhold(ill);
19662 		return (ill);
19663 	}
19664 
19665 	illcnt = illgrp->illgrp_ill_count;
19666 	mutex_enter(&illgrp->illgrp_lock);
19667 	retill = illgrp->illgrp_ill_schednext;
19668 
19669 	if (retill == NULL)
19670 		retill = illgrp->illgrp_ill;
19671 
19672 	/*
19673 	 * We do a circular search beginning at illgrp_ill_schednext
19674 	 * or illgrp_ill. We don't check the flags against the ill lock
19675 	 * since it can change anytime. The ire creation will be atomic
19676 	 * and will fail if the ill is FAILED or OFFLINE.
19677 	 */
19678 	for (i = 0; i < illcnt; i++) {
19679 		flags = retill->ill_phyint->phyint_flags;
19680 
19681 		if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) &&
19682 		    ILL_CAN_LOOKUP(retill)) {
19683 			illgrp->illgrp_ill_schednext = retill->ill_group_next;
19684 			ill_refhold(retill);
19685 			break;
19686 		}
19687 		retill = retill->ill_group_next;
19688 		if (retill == NULL)
19689 			retill = illgrp->illgrp_ill;
19690 	}
19691 	mutex_exit(&illgrp->illgrp_lock);
19692 	rw_exit(&ill_g_lock);
19693 
19694 	return (i == illcnt ? NULL : retill);
19695 }
19696 
19697 /*
19698  * Checks for availbility of a usable source address (if there is one) when the
19699  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
19700  * this selection is done regardless of the destination.
19701  */
19702 boolean_t
19703 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid)
19704 {
19705 	uint_t	ifindex;
19706 	ipif_t	*ipif = NULL;
19707 	ill_t	*uill;
19708 	boolean_t isv6;
19709 
19710 	ASSERT(ill != NULL);
19711 
19712 	isv6 = ill->ill_isv6;
19713 	ifindex = ill->ill_usesrc_ifindex;
19714 	if (ifindex != 0) {
19715 		uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL,
19716 		    NULL);
19717 		if (uill == NULL)
19718 			return (NULL);
19719 		mutex_enter(&uill->ill_lock);
19720 		for (ipif = uill->ill_ipif; ipif != NULL;
19721 		    ipif = ipif->ipif_next) {
19722 			if (!IPIF_CAN_LOOKUP(ipif))
19723 				continue;
19724 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19725 				continue;
19726 			if (!(ipif->ipif_flags & IPIF_UP))
19727 				continue;
19728 			if (ipif->ipif_zoneid != zoneid)
19729 				continue;
19730 			if ((isv6 &&
19731 			    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) ||
19732 			    (ipif->ipif_lcl_addr == INADDR_ANY))
19733 				continue;
19734 			mutex_exit(&uill->ill_lock);
19735 			ill_refrele(uill);
19736 			return (B_TRUE);
19737 		}
19738 		mutex_exit(&uill->ill_lock);
19739 		ill_refrele(uill);
19740 	}
19741 	return (B_FALSE);
19742 }
19743 
19744 /*
19745  * Determine the best source address given a destination address and an ill.
19746  * Prefers non-deprecated over deprecated but will return a deprecated
19747  * address if there is no other choice. If there is a usable source address
19748  * on the interface pointed to by ill_usesrc_ifindex then that is given
19749  * first preference.
19750  *
19751  * Returns NULL if there is no suitable source address for the ill.
19752  * This only occurs when there is no valid source address for the ill.
19753  */
19754 ipif_t *
19755 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid)
19756 {
19757 	ipif_t *ipif;
19758 	ipif_t *ipif_dep = NULL;	/* Fallback to deprecated */
19759 	ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE];
19760 	int index = 0;
19761 	boolean_t wrapped = B_FALSE;
19762 	boolean_t same_subnet_only = B_FALSE;
19763 	boolean_t ipif_same_found, ipif_other_found;
19764 	boolean_t specific_found;
19765 	ill_t	*till, *usill = NULL;
19766 	tsol_tpc_t *src_rhtp, *dst_rhtp;
19767 
19768 	if (ill->ill_usesrc_ifindex != 0) {
19769 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, B_FALSE,
19770 		    NULL, NULL, NULL, NULL);
19771 		if (usill != NULL)
19772 			ill = usill;	/* Select source from usesrc ILL */
19773 		else
19774 			return (NULL);
19775 	}
19776 
19777 	/*
19778 	 * If we're dealing with an unlabeled destination on a labeled system,
19779 	 * make sure that we ignore source addresses that are incompatible with
19780 	 * the destination's default label.  That destination's default label
19781 	 * must dominate the minimum label on the source address.
19782 	 */
19783 	dst_rhtp = NULL;
19784 	if (is_system_labeled()) {
19785 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
19786 		if (dst_rhtp == NULL)
19787 			return (NULL);
19788 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
19789 			TPC_RELE(dst_rhtp);
19790 			dst_rhtp = NULL;
19791 		}
19792 	}
19793 
19794 	/*
19795 	 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill
19796 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
19797 	 * After selecting the right ipif, under ill_lock make sure ipif is
19798 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
19799 	 * we retry. Inside the loop we still need to check for CONDEMNED,
19800 	 * but not under a lock.
19801 	 */
19802 	rw_enter(&ill_g_lock, RW_READER);
19803 
19804 retry:
19805 	till = ill;
19806 	ipif_arr[0] = NULL;
19807 
19808 	if (till->ill_group != NULL)
19809 		till = till->ill_group->illgrp_ill;
19810 
19811 	/*
19812 	 * Choose one good source address from each ill across the group.
19813 	 * If possible choose a source address in the same subnet as
19814 	 * the destination address.
19815 	 *
19816 	 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE
19817 	 * This is okay because of the following.
19818 	 *
19819 	 *    If PHYI_FAILED is set and we still have non-deprecated
19820 	 *    addresses, it means the addresses have not yet been
19821 	 *    failed over to a different interface. We potentially
19822 	 *    select them to create IRE_CACHES, which will be later
19823 	 *    flushed when the addresses move over.
19824 	 *
19825 	 *    If PHYI_INACTIVE is set and we still have non-deprecated
19826 	 *    addresses, it means either the user has configured them
19827 	 *    or PHYI_INACTIVE has not been cleared after the addresses
19828 	 *    been moved over. For the former, in.mpathd does a failover
19829 	 *    when the interface becomes INACTIVE and hence we should
19830 	 *    not find them. Once INACTIVE is set, we don't allow them
19831 	 *    to create logical interfaces anymore. For the latter, a
19832 	 *    flush will happen when INACTIVE is cleared which will
19833 	 *    flush the IRE_CACHES.
19834 	 *
19835 	 *    If PHYI_OFFLINE is set, all the addresses will be failed
19836 	 *    over soon. We potentially select them to create IRE_CACHEs,
19837 	 *    which will be later flushed when the addresses move over.
19838 	 *
19839 	 * NOTE : As ipif_select_source is called to borrow source address
19840 	 * for an ipif that is part of a group, source address selection
19841 	 * will be re-done whenever the group changes i.e either an
19842 	 * insertion/deletion in the group.
19843 	 *
19844 	 * Fill ipif_arr[] with source addresses, using these rules:
19845 	 *
19846 	 *	1. At most one source address from a given ill ends up
19847 	 *	   in ipif_arr[] -- that is, at most one of the ipif's
19848 	 *	   associated with a given ill ends up in ipif_arr[].
19849 	 *
19850 	 *	2. If there is at least one non-deprecated ipif in the
19851 	 *	   IPMP group with a source address on the same subnet as
19852 	 *	   our destination, then fill ipif_arr[] only with
19853 	 *	   source addresses on the same subnet as our destination.
19854 	 *	   Note that because of (1), only the first
19855 	 *	   non-deprecated ipif found with a source address
19856 	 *	   matching the destination ends up in ipif_arr[].
19857 	 *
19858 	 *	3. Otherwise, fill ipif_arr[] with non-deprecated source
19859 	 *	   addresses not in the same subnet as our destination.
19860 	 *	   Again, because of (1), only the first off-subnet source
19861 	 *	   address will be chosen.
19862 	 *
19863 	 *	4. If there are no non-deprecated ipifs, then just use
19864 	 *	   the source address associated with the last deprecated
19865 	 *	   one we find that happens to be on the same subnet,
19866 	 *	   otherwise the first one not in the same subnet.
19867 	 */
19868 	specific_found = B_FALSE;
19869 	for (; till != NULL; till = till->ill_group_next) {
19870 		ipif_same_found = B_FALSE;
19871 		ipif_other_found = B_FALSE;
19872 		for (ipif = till->ill_ipif; ipif != NULL;
19873 		    ipif = ipif->ipif_next) {
19874 			if (!IPIF_CAN_LOOKUP(ipif))
19875 				continue;
19876 			/* Always skip NOLOCAL and ANYCAST interfaces */
19877 			if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
19878 				continue;
19879 			if (!(ipif->ipif_flags & IPIF_UP))
19880 				continue;
19881 			if (ipif->ipif_zoneid != zoneid &&
19882 			    ipif->ipif_zoneid != ALL_ZONES)
19883 				continue;
19884 			/*
19885 			 * Interfaces with 0.0.0.0 address are allowed to be UP,
19886 			 * but are not valid as source addresses.
19887 			 */
19888 			if (ipif->ipif_lcl_addr == INADDR_ANY)
19889 				continue;
19890 
19891 			/*
19892 			 * Check compatibility of local address for
19893 			 * destination's default label if we're on a labeled
19894 			 * system.  Incompatible addresses can't be used at
19895 			 * all.
19896 			 */
19897 			if (dst_rhtp != NULL) {
19898 				boolean_t incompat;
19899 
19900 				src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
19901 				    IPV4_VERSION, B_FALSE);
19902 				if (src_rhtp == NULL)
19903 					continue;
19904 				incompat =
19905 				    src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
19906 				    src_rhtp->tpc_tp.tp_doi !=
19907 				    dst_rhtp->tpc_tp.tp_doi ||
19908 				    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
19909 				    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
19910 				    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
19911 				    src_rhtp->tpc_tp.tp_sl_set_cipso));
19912 				TPC_RELE(src_rhtp);
19913 				if (incompat)
19914 					continue;
19915 			}
19916 
19917 			/*
19918 			 * We prefer not to use all all-zones addresses, if we
19919 			 * can avoid it, as they pose problems with unlabeled
19920 			 * destinations.
19921 			 */
19922 			if (ipif->ipif_zoneid != ALL_ZONES) {
19923 				if (!specific_found &&
19924 				    (!same_subnet_only ||
19925 				    (ipif->ipif_net_mask & dst) ==
19926 				    ipif->ipif_subnet)) {
19927 					index = 0;
19928 					specific_found = B_TRUE;
19929 					ipif_other_found = B_FALSE;
19930 				}
19931 			} else {
19932 				if (specific_found)
19933 					continue;
19934 			}
19935 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
19936 				if (ipif_dep == NULL ||
19937 				    (ipif->ipif_net_mask & dst) ==
19938 				    ipif->ipif_subnet)
19939 					ipif_dep = ipif;
19940 				continue;
19941 			}
19942 			if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) {
19943 				/* found a source address in the same subnet */
19944 				if (!same_subnet_only) {
19945 					same_subnet_only = B_TRUE;
19946 					index = 0;
19947 				}
19948 				ipif_same_found = B_TRUE;
19949 			} else {
19950 				if (same_subnet_only || ipif_other_found)
19951 					continue;
19952 				ipif_other_found = B_TRUE;
19953 			}
19954 			ipif_arr[index++] = ipif;
19955 			if (index == MAX_IPIF_SELECT_SOURCE) {
19956 				wrapped = B_TRUE;
19957 				index = 0;
19958 			}
19959 			if (ipif_same_found)
19960 				break;
19961 		}
19962 	}
19963 
19964 	if (ipif_arr[0] == NULL) {
19965 		ipif = ipif_dep;
19966 	} else {
19967 		if (wrapped)
19968 			index = MAX_IPIF_SELECT_SOURCE;
19969 		ipif = ipif_arr[ipif_rand() % index];
19970 		ASSERT(ipif != NULL);
19971 	}
19972 
19973 	if (ipif != NULL) {
19974 		mutex_enter(&ipif->ipif_ill->ill_lock);
19975 		if (!IPIF_CAN_LOOKUP(ipif)) {
19976 			mutex_exit(&ipif->ipif_ill->ill_lock);
19977 			goto retry;
19978 		}
19979 		ipif_refhold_locked(ipif);
19980 		mutex_exit(&ipif->ipif_ill->ill_lock);
19981 	}
19982 
19983 	rw_exit(&ill_g_lock);
19984 	if (usill != NULL)
19985 		ill_refrele(usill);
19986 	if (dst_rhtp != NULL)
19987 		TPC_RELE(dst_rhtp);
19988 
19989 #ifdef DEBUG
19990 	if (ipif == NULL) {
19991 		char buf1[INET6_ADDRSTRLEN];
19992 
19993 		ip1dbg(("ipif_select_source(%s, %s) -> NULL\n",
19994 		    ill->ill_name,
19995 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
19996 	} else {
19997 		char buf1[INET6_ADDRSTRLEN];
19998 		char buf2[INET6_ADDRSTRLEN];
19999 
20000 		ip1dbg(("ipif_select_source(%s, %s) -> %s\n",
20001 		    ipif->ipif_ill->ill_name,
20002 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
20003 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
20004 		    buf2, sizeof (buf2))));
20005 	}
20006 #endif /* DEBUG */
20007 	return (ipif);
20008 }
20009 
20010 
20011 /*
20012  * If old_ipif is not NULL, see if ipif was derived from old
20013  * ipif and if so, recreate the interface route by re-doing
20014  * source address selection. This happens when ipif_down ->
20015  * ipif_update_other_ipifs calls us.
20016  *
20017  * If old_ipif is NULL, just redo the source address selection
20018  * if needed. This happens when illgrp_insert or ipif_up_done
20019  * calls us.
20020  */
20021 static void
20022 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif)
20023 {
20024 	ire_t *ire;
20025 	ire_t *ipif_ire;
20026 	queue_t *stq;
20027 	ipif_t *nipif;
20028 	ill_t *ill;
20029 	boolean_t need_rele = B_FALSE;
20030 
20031 	ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif));
20032 	ASSERT(IAM_WRITER_IPIF(ipif));
20033 
20034 	ill = ipif->ipif_ill;
20035 	if (!(ipif->ipif_flags &
20036 	    (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) {
20037 		/*
20038 		 * Can't possibly have borrowed the source
20039 		 * from old_ipif.
20040 		 */
20041 		return;
20042 	}
20043 
20044 	/*
20045 	 * Is there any work to be done? No work if the address
20046 	 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST (
20047 	 * ipif_select_source() does not borrow addresses from
20048 	 * NOLOCAL and ANYCAST interfaces).
20049 	 */
20050 	if ((old_ipif != NULL) &&
20051 	    ((old_ipif->ipif_lcl_addr == INADDR_ANY) ||
20052 	    (old_ipif->ipif_ill->ill_wq == NULL) ||
20053 	    (old_ipif->ipif_flags &
20054 	    (IPIF_NOLOCAL|IPIF_ANYCAST)))) {
20055 		return;
20056 	}
20057 
20058 	/*
20059 	 * Perform the same checks as when creating the
20060 	 * IRE_INTERFACE in ipif_up_done.
20061 	 */
20062 	if (!(ipif->ipif_flags & IPIF_UP))
20063 		return;
20064 
20065 	if ((ipif->ipif_flags & IPIF_NOXMIT) ||
20066 	    (ipif->ipif_subnet == INADDR_ANY))
20067 		return;
20068 
20069 	ipif_ire = ipif_to_ire(ipif);
20070 	if (ipif_ire == NULL)
20071 		return;
20072 
20073 	/*
20074 	 * We know that ipif uses some other source for its
20075 	 * IRE_INTERFACE. Is it using the source of this
20076 	 * old_ipif?
20077 	 */
20078 	if (old_ipif != NULL &&
20079 	    old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) {
20080 		ire_refrele(ipif_ire);
20081 		return;
20082 	}
20083 	if (ip_debug > 2) {
20084 		/* ip1dbg */
20085 		pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for"
20086 		    " src %s\n", AF_INET, &ipif_ire->ire_src_addr);
20087 	}
20088 
20089 	stq = ipif_ire->ire_stq;
20090 
20091 	/*
20092 	 * Can't use our source address. Select a different
20093 	 * source address for the IRE_INTERFACE.
20094 	 */
20095 	nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid);
20096 	if (nipif == NULL) {
20097 		/* Last resort - all ipif's have IPIF_NOLOCAL */
20098 		nipif = ipif;
20099 	} else {
20100 		need_rele = B_TRUE;
20101 	}
20102 
20103 	ire = ire_create(
20104 	    (uchar_t *)&ipif->ipif_subnet,	/* dest pref */
20105 	    (uchar_t *)&ipif->ipif_net_mask,	/* mask */
20106 	    (uchar_t *)&nipif->ipif_src_addr,	/* src addr */
20107 	    NULL,				/* no gateway */
20108 	    NULL,
20109 	    &ipif->ipif_mtu,			/* max frag */
20110 	    NULL,				/* fast path header */
20111 	    NULL,				/* no recv from queue */
20112 	    stq,				/* send-to queue */
20113 	    ill->ill_net_type,			/* IF_[NO]RESOLVER */
20114 	    ill->ill_resolver_mp,		/* xmit header */
20115 	    ipif,
20116 	    NULL,
20117 	    0,
20118 	    0,
20119 	    0,
20120 	    0,
20121 	    &ire_uinfo_null,
20122 	    NULL,
20123 	    NULL);
20124 
20125 	if (ire != NULL) {
20126 		ire_t *ret_ire;
20127 		int error;
20128 
20129 		/*
20130 		 * We don't need ipif_ire anymore. We need to delete
20131 		 * before we add so that ire_add does not detect
20132 		 * duplicates.
20133 		 */
20134 		ire_delete(ipif_ire);
20135 		ret_ire = ire;
20136 		error = ire_add(&ret_ire, NULL, NULL, NULL);
20137 		ASSERT(error == 0);
20138 		ASSERT(ire == ret_ire);
20139 		/* Held in ire_add */
20140 		ire_refrele(ret_ire);
20141 	}
20142 	/*
20143 	 * Either we are falling through from above or could not
20144 	 * allocate a replacement.
20145 	 */
20146 	ire_refrele(ipif_ire);
20147 	if (need_rele)
20148 		ipif_refrele(nipif);
20149 }
20150 
20151 /*
20152  * This old_ipif is going away.
20153  *
20154  * Determine if any other ipif's is using our address as
20155  * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or
20156  * IPIF_DEPRECATED).
20157  * Find the IRE_INTERFACE for such ipifs and recreate them
20158  * to use an different source address following the rules in
20159  * ipif_up_done.
20160  *
20161  * This function takes an illgrp as an argument so that illgrp_delete
20162  * can call this to update source address even after deleting the
20163  * old_ipif->ipif_ill from the ill group.
20164  */
20165 static void
20166 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp)
20167 {
20168 	ipif_t *ipif;
20169 	ill_t *ill;
20170 	char	buf[INET6_ADDRSTRLEN];
20171 
20172 	ASSERT(IAM_WRITER_IPIF(old_ipif));
20173 	ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif));
20174 
20175 	ill = old_ipif->ipif_ill;
20176 
20177 	ip1dbg(("ipif_update_other_ipifs(%s, %s)\n",
20178 	    ill->ill_name,
20179 	    inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr,
20180 	    buf, sizeof (buf))));
20181 	/*
20182 	 * If this part of a group, look at all ills as ipif_select_source
20183 	 * borrows source address across all the ills in the group.
20184 	 */
20185 	if (illgrp != NULL)
20186 		ill = illgrp->illgrp_ill;
20187 
20188 	for (; ill != NULL; ill = ill->ill_group_next) {
20189 		for (ipif = ill->ill_ipif; ipif != NULL;
20190 		    ipif = ipif->ipif_next) {
20191 
20192 			if (ipif == old_ipif)
20193 				continue;
20194 
20195 			ipif_recreate_interface_routes(old_ipif, ipif);
20196 		}
20197 	}
20198 }
20199 
20200 /* ARGSUSED */
20201 int
20202 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20203 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20204 {
20205 	/*
20206 	 * ill_phyint_reinit merged the v4 and v6 into a single
20207 	 * ipsq. Could also have become part of a ipmp group in the
20208 	 * process, and we might not have been able to complete the
20209 	 * operation in ipif_set_values, if we could not become
20210 	 * exclusive.  If so restart it here.
20211 	 */
20212 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20213 }
20214 
20215 
20216 /* ARGSUSED */
20217 int
20218 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20219     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20220 {
20221 	queue_t		*q1 = q;
20222 	char 		*cp;
20223 	char		interf_name[LIFNAMSIZ];
20224 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
20225 
20226 	if (!q->q_next) {
20227 		ip1dbg((
20228 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
20229 		return (EINVAL);
20230 	}
20231 
20232 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
20233 		return (EALREADY);
20234 
20235 	do {
20236 		q1 = q1->q_next;
20237 	} while (q1->q_next);
20238 	cp = q1->q_qinfo->qi_minfo->mi_idname;
20239 	(void) sprintf(interf_name, "%s%d", cp, ppa);
20240 
20241 	/*
20242 	 * Here we are not going to delay the ioack until after
20243 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
20244 	 * original ioctl message before sending the requests.
20245 	 */
20246 	return (ipif_set_values(q, mp, interf_name, &ppa));
20247 }
20248 
20249 /* ARGSUSED */
20250 int
20251 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
20252     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
20253 {
20254 	return (ENXIO);
20255 }
20256 
20257 /*
20258  * Net and subnet broadcast ire's are now specific to the particular
20259  * physical interface (ill) and not to any one locigal interface (ipif).
20260  * However, if a particular logical interface is being taken down, it's
20261  * associated ire's will be taken down as well.  Hence, when we go to
20262  * take down or change the local address, broadcast address or netmask
20263  * of a specific logical interface, we must check to make sure that we
20264  * have valid net and subnet broadcast ire's for the other logical
20265  * interfaces which may have been shared with the logical interface
20266  * being brought down or changed.
20267  *
20268  * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it
20269  * is tied to the first interface coming UP. If that ipif is going down,
20270  * we need to recreate them on the next valid ipif.
20271  *
20272  * Note: assume that the ipif passed in is still up so that it's IRE
20273  * entries are still valid.
20274  */
20275 static void
20276 ipif_check_bcast_ires(ipif_t *test_ipif)
20277 {
20278 	ipif_t	*ipif;
20279 	ire_t	*test_subnet_ire, *test_net_ire;
20280 	ire_t	*test_allzero_ire, *test_allone_ire;
20281 	ire_t	*ire_array[12];
20282 	ire_t	**irep = &ire_array[0];
20283 	ire_t	**irep1;
20284 
20285 	ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask;
20286 	ipaddr_t test_net_addr, test_subnet_addr;
20287 	ipaddr_t test_net_mask, test_subnet_mask;
20288 	boolean_t need_net_bcast_ire = B_FALSE;
20289 	boolean_t need_subnet_bcast_ire = B_FALSE;
20290 	boolean_t allzero_bcast_ire_created = B_FALSE;
20291 	boolean_t allone_bcast_ire_created = B_FALSE;
20292 	boolean_t net_bcast_ire_created = B_FALSE;
20293 	boolean_t subnet_bcast_ire_created = B_FALSE;
20294 
20295 	ipif_t  *backup_ipif_net = (ipif_t *)NULL;
20296 	ipif_t  *backup_ipif_subnet = (ipif_t *)NULL;
20297 	ipif_t  *backup_ipif_allzeros = (ipif_t *)NULL;
20298 	ipif_t  *backup_ipif_allones = (ipif_t *)NULL;
20299 	uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST;
20300 
20301 	ASSERT(!test_ipif->ipif_isv6);
20302 	ASSERT(IAM_WRITER_IPIF(test_ipif));
20303 
20304 	/*
20305 	 * No broadcast IREs for the LOOPBACK interface
20306 	 * or others such as point to point and IPIF_NOXMIT.
20307 	 */
20308 	if (!(test_ipif->ipif_flags & IPIF_BROADCAST) ||
20309 	    (test_ipif->ipif_flags & IPIF_NOXMIT))
20310 		return;
20311 
20312 	test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST,
20313 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20314 
20315 	test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST,
20316 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20317 
20318 	test_net_mask = ip_net_mask(test_ipif->ipif_subnet);
20319 	test_subnet_mask = test_ipif->ipif_net_mask;
20320 
20321 	/*
20322 	 * If no net mask set, assume the default based on net class.
20323 	 */
20324 	if (test_subnet_mask == 0)
20325 		test_subnet_mask = test_net_mask;
20326 
20327 	/*
20328 	 * Check if there is a network broadcast ire associated with this ipif
20329 	 */
20330 	test_net_addr = test_net_mask  & test_ipif->ipif_subnet;
20331 	test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST,
20332 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20333 
20334 	/*
20335 	 * Check if there is a subnet broadcast IRE associated with this ipif
20336 	 */
20337 	test_subnet_addr = test_subnet_mask  & test_ipif->ipif_subnet;
20338 	test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST,
20339 	    test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF));
20340 
20341 	/*
20342 	 * No broadcast ire's associated with this ipif.
20343 	 */
20344 	if ((test_subnet_ire == NULL) && (test_net_ire == NULL) &&
20345 	    (test_allzero_ire == NULL) && (test_allone_ire == NULL)) {
20346 		return;
20347 	}
20348 
20349 	/*
20350 	 * We have established which bcast ires have to be replaced.
20351 	 * Next we try to locate ipifs that match there ires.
20352 	 * The rules are simple: If we find an ipif that matches on the subnet
20353 	 * address it will also match on the net address, the allzeros and
20354 	 * allones address. Any ipif that matches only on the net address will
20355 	 * also match the allzeros and allones addresses.
20356 	 * The other criterion is the ipif_flags. We look for non-deprecated
20357 	 * (and non-anycast and non-nolocal) ipifs as the best choice.
20358 	 * ipifs with check_flags matching (deprecated, etc) are used only
20359 	 * if good ipifs are not available. While looping, we save existing
20360 	 * deprecated ipifs as backup_ipif.
20361 	 * We loop through all the ipifs for this ill looking for ipifs
20362 	 * whose broadcast addr match the ipif passed in, but do not have
20363 	 * their own broadcast ires. For creating 0.0.0.0 and
20364 	 * 255.255.255.255 we just need an ipif on this ill to create.
20365 	 */
20366 	for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL;
20367 	    ipif = ipif->ipif_next) {
20368 
20369 		ASSERT(!ipif->ipif_isv6);
20370 		/*
20371 		 * Already checked the ipif passed in.
20372 		 */
20373 		if (ipif == test_ipif) {
20374 			continue;
20375 		}
20376 
20377 		/*
20378 		 * We only need to recreate broadcast ires if another ipif in
20379 		 * the same zone uses them. The new ires must be created in the
20380 		 * same zone.
20381 		 */
20382 		if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) {
20383 			continue;
20384 		}
20385 
20386 		/*
20387 		 * Only interested in logical interfaces with valid local
20388 		 * addresses or with the ability to broadcast.
20389 		 */
20390 		if ((ipif->ipif_subnet == 0) ||
20391 		    !(ipif->ipif_flags & IPIF_BROADCAST) ||
20392 		    (ipif->ipif_flags & IPIF_NOXMIT) ||
20393 		    !(ipif->ipif_flags & IPIF_UP)) {
20394 			continue;
20395 		}
20396 		/*
20397 		 * Check if there is a net broadcast ire for this
20398 		 * net address.  If it turns out that the ipif we are
20399 		 * about to take down owns this ire, we must make a
20400 		 * new one because it is potentially going away.
20401 		 */
20402 		if (test_net_ire && (!net_bcast_ire_created)) {
20403 			net_mask = ip_net_mask(ipif->ipif_subnet);
20404 			net_addr = net_mask & ipif->ipif_subnet;
20405 			if (net_addr == test_net_addr) {
20406 				need_net_bcast_ire = B_TRUE;
20407 				/*
20408 				 * Use DEPRECATED ipif only if no good
20409 				 * ires are available. subnet_addr is
20410 				 * a better match than net_addr.
20411 				 */
20412 				if ((ipif->ipif_flags & check_flags) &&
20413 				    (backup_ipif_net == NULL)) {
20414 					backup_ipif_net = ipif;
20415 				}
20416 			}
20417 		}
20418 		/*
20419 		 * Check if there is a subnet broadcast ire for this
20420 		 * net address.  If it turns out that the ipif we are
20421 		 * about to take down owns this ire, we must make a
20422 		 * new one because it is potentially going away.
20423 		 */
20424 		if (test_subnet_ire && (!subnet_bcast_ire_created)) {
20425 			subnet_mask = ipif->ipif_net_mask;
20426 			subnet_addr = ipif->ipif_subnet;
20427 			if (subnet_addr == test_subnet_addr) {
20428 				need_subnet_bcast_ire = B_TRUE;
20429 				if ((ipif->ipif_flags & check_flags) &&
20430 				    (backup_ipif_subnet == NULL)) {
20431 					backup_ipif_subnet = ipif;
20432 				}
20433 			}
20434 		}
20435 
20436 
20437 		/* Short circuit here if this ipif is deprecated */
20438 		if (ipif->ipif_flags & check_flags) {
20439 			if ((test_allzero_ire != NULL) &&
20440 			    (!allzero_bcast_ire_created) &&
20441 			    (backup_ipif_allzeros == NULL)) {
20442 				backup_ipif_allzeros = ipif;
20443 			}
20444 			if ((test_allone_ire != NULL) &&
20445 			    (!allone_bcast_ire_created) &&
20446 			    (backup_ipif_allones == NULL)) {
20447 				backup_ipif_allones = ipif;
20448 			}
20449 			continue;
20450 		}
20451 
20452 		/*
20453 		 * Found an ipif which has the same broadcast ire as the
20454 		 * ipif passed in and the ipif passed in "owns" the ire.
20455 		 * Create new broadcast ire's for this broadcast addr.
20456 		 */
20457 		if (need_net_bcast_ire && !net_bcast_ire_created) {
20458 			irep = ire_create_bcast(ipif, net_addr, irep);
20459 			irep = ire_create_bcast(ipif,
20460 			    ~net_mask | net_addr, irep);
20461 			net_bcast_ire_created = B_TRUE;
20462 		}
20463 		if (need_subnet_bcast_ire && !subnet_bcast_ire_created) {
20464 			irep = ire_create_bcast(ipif, subnet_addr, irep);
20465 			irep = ire_create_bcast(ipif,
20466 			    ~subnet_mask | subnet_addr, irep);
20467 			subnet_bcast_ire_created = B_TRUE;
20468 		}
20469 		if (test_allzero_ire != NULL && !allzero_bcast_ire_created) {
20470 			irep = ire_create_bcast(ipif, 0, irep);
20471 			allzero_bcast_ire_created = B_TRUE;
20472 		}
20473 		if (test_allone_ire != NULL && !allone_bcast_ire_created) {
20474 			irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep);
20475 			allone_bcast_ire_created = B_TRUE;
20476 		}
20477 		/*
20478 		 * Once we have created all the appropriate ires, we
20479 		 * just break out of this loop to add what we have created.
20480 		 * This has been indented similar to ire_match_args for
20481 		 * readability.
20482 		 */
20483 		if (((test_net_ire == NULL) ||
20484 			(net_bcast_ire_created)) &&
20485 		    ((test_subnet_ire == NULL) ||
20486 			(subnet_bcast_ire_created)) &&
20487 		    ((test_allzero_ire == NULL) ||
20488 			(allzero_bcast_ire_created)) &&
20489 		    ((test_allone_ire == NULL) ||
20490 			(allone_bcast_ire_created))) {
20491 			break;
20492 		}
20493 	}
20494 
20495 	/*
20496 	 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs
20497 	 * exist. 6 pairs of bcast ires are needed.
20498 	 * Note - the old ires are deleted in ipif_down.
20499 	 */
20500 	if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) {
20501 		ipif = backup_ipif_net;
20502 		irep = ire_create_bcast(ipif, net_addr, irep);
20503 		irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep);
20504 		net_bcast_ire_created = B_TRUE;
20505 	}
20506 	if (need_subnet_bcast_ire && !subnet_bcast_ire_created &&
20507 	    backup_ipif_subnet) {
20508 		ipif = backup_ipif_subnet;
20509 		irep = ire_create_bcast(ipif, subnet_addr, irep);
20510 		irep = ire_create_bcast(ipif,
20511 		    ~subnet_mask | subnet_addr, irep);
20512 		subnet_bcast_ire_created = B_TRUE;
20513 	}
20514 	if (test_allzero_ire != NULL && !allzero_bcast_ire_created &&
20515 	    backup_ipif_allzeros) {
20516 		irep = ire_create_bcast(backup_ipif_allzeros, 0, irep);
20517 		allzero_bcast_ire_created = B_TRUE;
20518 	}
20519 	if (test_allone_ire != NULL && !allone_bcast_ire_created &&
20520 	    backup_ipif_allones) {
20521 		irep = ire_create_bcast(backup_ipif_allones,
20522 		    INADDR_BROADCAST, irep);
20523 		allone_bcast_ire_created = B_TRUE;
20524 	}
20525 
20526 	/*
20527 	 * If we can't create all of them, don't add any of them.
20528 	 * Code in ip_wput_ire and ire_to_ill assumes that we
20529 	 * always have a non-loopback copy and loopback copy
20530 	 * for a given address.
20531 	 */
20532 	for (irep1 = irep; irep1 > ire_array; ) {
20533 		irep1--;
20534 		if (*irep1 == NULL) {
20535 			ip0dbg(("ipif_check_bcast_ires: can't create "
20536 			    "IRE_BROADCAST, memory allocation failure\n"));
20537 			while (irep > ire_array) {
20538 				irep--;
20539 				if (*irep != NULL)
20540 					ire_delete(*irep);
20541 			}
20542 			goto bad;
20543 		}
20544 	}
20545 	for (irep1 = irep; irep1 > ire_array; ) {
20546 		int error;
20547 
20548 		irep1--;
20549 		error = ire_add(irep1, NULL, NULL, NULL);
20550 		if (error == 0) {
20551 			ire_refrele(*irep1);		/* Held in ire_add */
20552 		}
20553 	}
20554 bad:
20555 	if (test_allzero_ire != NULL)
20556 		ire_refrele(test_allzero_ire);
20557 	if (test_allone_ire != NULL)
20558 		ire_refrele(test_allone_ire);
20559 	if (test_net_ire != NULL)
20560 		ire_refrele(test_net_ire);
20561 	if (test_subnet_ire != NULL)
20562 		ire_refrele(test_subnet_ire);
20563 }
20564 
20565 /*
20566  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
20567  * from lifr_flags and the name from lifr_name.
20568  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
20569  * since ipif_lookup_on_name uses the _isv6 flags when matching.
20570  * Returns EINPROGRESS when mp has been consumed by queueing it on
20571  * ill_pending_mp and the ioctl will complete in ip_rput.
20572  */
20573 /* ARGSUSED */
20574 int
20575 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20576     ip_ioctl_cmd_t *ipip, void *if_req)
20577 {
20578 	int	err;
20579 	ill_t	*ill;
20580 	struct lifreq *lifr = (struct lifreq *)if_req;
20581 
20582 	ASSERT(ipif != NULL);
20583 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
20584 	ASSERT(q->q_next != NULL);
20585 
20586 	ill = (ill_t *)q->q_ptr;
20587 	/*
20588 	 * If we are not writer on 'q' then this interface exists already
20589 	 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif.
20590 	 * So return EALREADY
20591 	 */
20592 	if (ill != ipif->ipif_ill)
20593 		return (EALREADY);
20594 
20595 	if (ill->ill_name[0] != '\0')
20596 		return (EALREADY);
20597 
20598 	/*
20599 	 * Set all the flags. Allows all kinds of override. Provide some
20600 	 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST
20601 	 * unless there is either multicast/broadcast support in the driver
20602 	 * or it is a pt-pt link.
20603 	 */
20604 	if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) {
20605 		/* Meaningless to IP thus don't allow them to be set. */
20606 		ip1dbg(("ip_setname: EINVAL 1\n"));
20607 		return (EINVAL);
20608 	}
20609 	/*
20610 	 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the
20611 	 * ill_bcast_addr_length info.
20612 	 */
20613 	if (!ill->ill_needs_attach &&
20614 	    ((lifr->lifr_flags & IFF_MULTICAST) &&
20615 	    !(lifr->lifr_flags & IFF_POINTOPOINT) &&
20616 	    ill->ill_bcast_addr_length == 0)) {
20617 		/* Link not broadcast/pt-pt capable i.e. no multicast */
20618 		ip1dbg(("ip_setname: EINVAL 2\n"));
20619 		return (EINVAL);
20620 	}
20621 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
20622 	    ((lifr->lifr_flags & IFF_IPV6) ||
20623 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
20624 		/* Link not broadcast capable or IPv6 i.e. no broadcast */
20625 		ip1dbg(("ip_setname: EINVAL 3\n"));
20626 		return (EINVAL);
20627 	}
20628 	if (lifr->lifr_flags & IFF_UP) {
20629 		/* Can only be set with SIOCSLIFFLAGS */
20630 		ip1dbg(("ip_setname: EINVAL 4\n"));
20631 		return (EINVAL);
20632 	}
20633 	if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 &&
20634 	    (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) {
20635 		ip1dbg(("ip_setname: EINVAL 5\n"));
20636 		return (EINVAL);
20637 	}
20638 	/*
20639 	 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces.
20640 	 */
20641 	if ((lifr->lifr_flags & IFF_XRESOLV) &&
20642 	    !(lifr->lifr_flags & IFF_IPV6) &&
20643 	    !(ipif->ipif_isv6)) {
20644 		ip1dbg(("ip_setname: EINVAL 6\n"));
20645 		return (EINVAL);
20646 	}
20647 
20648 	/*
20649 	 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence
20650 	 * we have all the flags here. So, we assign rather than we OR.
20651 	 * We can't OR the flags here because we don't want to set
20652 	 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in
20653 	 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending
20654 	 * on lifr_flags value here.
20655 	 */
20656 	/*
20657 	 * This ill has not been inserted into the global list.
20658 	 * So we are still single threaded and don't need any lock
20659 	 */
20660 	ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS;
20661 	ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS;
20662 	ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS;
20663 
20664 	/* We started off as V4. */
20665 	if (ill->ill_flags & ILLF_IPV6) {
20666 		ill->ill_phyint->phyint_illv6 = ill;
20667 		ill->ill_phyint->phyint_illv4 = NULL;
20668 	}
20669 	err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa);
20670 	return (err);
20671 }
20672 
20673 /* ARGSUSED */
20674 int
20675 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20676     ip_ioctl_cmd_t *ipip, void *if_req)
20677 {
20678 	/*
20679 	 * ill_phyint_reinit merged the v4 and v6 into a single
20680 	 * ipsq. Could also have become part of a ipmp group in the
20681 	 * process, and we might not have been able to complete the
20682 	 * slifname in ipif_set_values, if we could not become
20683 	 * exclusive.  If so restart it here
20684 	 */
20685 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
20686 }
20687 
20688 /*
20689  * Return a pointer to the ipif which matches the index, IP version type and
20690  * zoneid.
20691  */
20692 ipif_t *
20693 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
20694     queue_t *q, mblk_t *mp, ipsq_func_t func, int *err)
20695 {
20696 	ill_t	*ill;
20697 	ipsq_t  *ipsq;
20698 	phyint_t *phyi;
20699 	ipif_t	*ipif;
20700 
20701 	ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) ||
20702 	    (q != NULL && mp != NULL && func != NULL && err != NULL));
20703 
20704 	if (err != NULL)
20705 		*err = 0;
20706 
20707 	/*
20708 	 * Indexes are stored in the phyint - a common structure
20709 	 * to both IPv4 and IPv6.
20710 	 */
20711 
20712 	rw_enter(&ill_g_lock, RW_READER);
20713 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_index,
20714 	    (void *) &index, NULL);
20715 	if (phyi != NULL) {
20716 		ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4;
20717 		if (ill == NULL) {
20718 			rw_exit(&ill_g_lock);
20719 			if (err != NULL)
20720 				*err = ENXIO;
20721 			return (NULL);
20722 		}
20723 		GRAB_CONN_LOCK(q);
20724 		mutex_enter(&ill->ill_lock);
20725 		if (ILL_CAN_LOOKUP(ill)) {
20726 			for (ipif = ill->ill_ipif; ipif != NULL;
20727 			    ipif = ipif->ipif_next) {
20728 				if (IPIF_CAN_LOOKUP(ipif) &&
20729 				    (zoneid == ALL_ZONES ||
20730 				    zoneid == ipif->ipif_zoneid ||
20731 				    ipif->ipif_zoneid == ALL_ZONES)) {
20732 					ipif_refhold_locked(ipif);
20733 					mutex_exit(&ill->ill_lock);
20734 					RELEASE_CONN_LOCK(q);
20735 					rw_exit(&ill_g_lock);
20736 					return (ipif);
20737 				}
20738 			}
20739 		} else if (ILL_CAN_WAIT(ill, q)) {
20740 			ipsq = ill->ill_phyint->phyint_ipsq;
20741 			mutex_enter(&ipsq->ipsq_lock);
20742 			rw_exit(&ill_g_lock);
20743 			mutex_exit(&ill->ill_lock);
20744 			ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
20745 			mutex_exit(&ipsq->ipsq_lock);
20746 			RELEASE_CONN_LOCK(q);
20747 			*err = EINPROGRESS;
20748 			return (NULL);
20749 		}
20750 		mutex_exit(&ill->ill_lock);
20751 		RELEASE_CONN_LOCK(q);
20752 	}
20753 	rw_exit(&ill_g_lock);
20754 	if (err != NULL)
20755 		*err = ENXIO;
20756 	return (NULL);
20757 }
20758 
20759 typedef struct conn_change_s {
20760 	uint_t cc_old_ifindex;
20761 	uint_t cc_new_ifindex;
20762 } conn_change_t;
20763 
20764 /*
20765  * ipcl_walk function for changing interface index.
20766  */
20767 static void
20768 conn_change_ifindex(conn_t *connp, caddr_t arg)
20769 {
20770 	conn_change_t *connc;
20771 	uint_t old_ifindex;
20772 	uint_t new_ifindex;
20773 	int i;
20774 	ilg_t *ilg;
20775 
20776 	connc = (conn_change_t *)arg;
20777 	old_ifindex = connc->cc_old_ifindex;
20778 	new_ifindex = connc->cc_new_ifindex;
20779 
20780 	if (connp->conn_orig_bound_ifindex == old_ifindex)
20781 		connp->conn_orig_bound_ifindex = new_ifindex;
20782 
20783 	if (connp->conn_orig_multicast_ifindex == old_ifindex)
20784 		connp->conn_orig_multicast_ifindex = new_ifindex;
20785 
20786 	if (connp->conn_orig_xmit_ifindex == old_ifindex)
20787 		connp->conn_orig_xmit_ifindex = new_ifindex;
20788 
20789 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
20790 		ilg = &connp->conn_ilg[i];
20791 		if (ilg->ilg_orig_ifindex == old_ifindex)
20792 			ilg->ilg_orig_ifindex = new_ifindex;
20793 	}
20794 }
20795 
20796 /*
20797  * Walk all the ipifs and ilms on this ill and change the orig_ifindex
20798  * to new_index if it matches the old_index.
20799  *
20800  * Failovers typically happen within a group of ills. But somebody
20801  * can remove an ill from the group after a failover happened. If
20802  * we are setting the ifindex after this, we potentially need to
20803  * look at all the ills rather than just the ones in the group.
20804  * We cut down the work by looking at matching ill_net_types
20805  * and ill_types as we could not possibly grouped them together.
20806  */
20807 static void
20808 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc)
20809 {
20810 	ill_t *ill;
20811 	ipif_t *ipif;
20812 	uint_t old_ifindex;
20813 	uint_t new_ifindex;
20814 	ilm_t *ilm;
20815 	ill_walk_context_t ctx;
20816 
20817 	old_ifindex = connc->cc_old_ifindex;
20818 	new_ifindex = connc->cc_new_ifindex;
20819 
20820 	rw_enter(&ill_g_lock, RW_READER);
20821 	ill = ILL_START_WALK_ALL(&ctx);
20822 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
20823 		if ((ill_orig->ill_net_type != ill->ill_net_type) ||
20824 			(ill_orig->ill_type != ill->ill_type)) {
20825 			continue;
20826 		}
20827 		for (ipif = ill->ill_ipif; ipif != NULL;
20828 				ipif = ipif->ipif_next) {
20829 			if (ipif->ipif_orig_ifindex == old_ifindex)
20830 				ipif->ipif_orig_ifindex = new_ifindex;
20831 		}
20832 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
20833 			if (ilm->ilm_orig_ifindex == old_ifindex)
20834 				ilm->ilm_orig_ifindex = new_ifindex;
20835 		}
20836 	}
20837 	rw_exit(&ill_g_lock);
20838 }
20839 
20840 /*
20841  * We first need to ensure that the new index is unique, and
20842  * then carry the change across both v4 and v6 ill representation
20843  * of the physical interface.
20844  */
20845 /* ARGSUSED */
20846 int
20847 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20848     ip_ioctl_cmd_t *ipip, void *ifreq)
20849 {
20850 	ill_t		*ill;
20851 	ill_t		*ill_other;
20852 	phyint_t	*phyi;
20853 	int		old_index;
20854 	conn_change_t	connc;
20855 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20856 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20857 	uint_t	index;
20858 	ill_t	*ill_v4;
20859 	ill_t	*ill_v6;
20860 
20861 	if (ipip->ipi_cmd_type == IF_CMD)
20862 		index = ifr->ifr_index;
20863 	else
20864 		index = lifr->lifr_index;
20865 
20866 	/*
20867 	 * Only allow on physical interface. Also, index zero is illegal.
20868 	 *
20869 	 * Need to check for PHYI_FAILED and PHYI_INACTIVE
20870 	 *
20871 	 * 1) If PHYI_FAILED is set, a failover could have happened which
20872 	 *    implies a possible failback might have to happen. As failback
20873 	 *    depends on the old index, we should fail setting the index.
20874 	 *
20875 	 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that
20876 	 *    any addresses or multicast memberships are failed over to
20877 	 *    a non-STANDBY interface. As failback depends on the old
20878 	 *    index, we should fail setting the index for this case also.
20879 	 *
20880 	 * 3) If PHYI_OFFLINE is set, a possible failover has happened.
20881 	 *    Be consistent with PHYI_FAILED and fail the ioctl.
20882 	 */
20883 	ill = ipif->ipif_ill;
20884 	phyi = ill->ill_phyint;
20885 	if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) ||
20886 	    ipif->ipif_id != 0 || index == 0) {
20887 		return (EINVAL);
20888 	}
20889 	old_index = phyi->phyint_ifindex;
20890 
20891 	/* If the index is not changing, no work to do */
20892 	if (old_index == index)
20893 		return (0);
20894 
20895 	/*
20896 	 * Use ill_lookup_on_ifindex to determine if the
20897 	 * new index is unused and if so allow the change.
20898 	 */
20899 	ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL);
20900 	ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL);
20901 	if (ill_v6 != NULL || ill_v4 != NULL) {
20902 		if (ill_v4 != NULL)
20903 			ill_refrele(ill_v4);
20904 		if (ill_v6 != NULL)
20905 			ill_refrele(ill_v6);
20906 		return (EBUSY);
20907 	}
20908 
20909 	/*
20910 	 * The new index is unused. Set it in the phyint.
20911 	 * Locate the other ill so that we can send a routing
20912 	 * sockets message.
20913 	 */
20914 	if (ill->ill_isv6) {
20915 		ill_other = phyi->phyint_illv4;
20916 	} else {
20917 		ill_other = phyi->phyint_illv6;
20918 	}
20919 
20920 	phyi->phyint_ifindex = index;
20921 
20922 	connc.cc_old_ifindex = old_index;
20923 	connc.cc_new_ifindex = index;
20924 	ip_change_ifindex(ill, &connc);
20925 	ipcl_walk(conn_change_ifindex, (caddr_t)&connc);
20926 
20927 	/* Send the routing sockets message */
20928 	ip_rts_ifmsg(ipif);
20929 	if (ill_other != NULL)
20930 		ip_rts_ifmsg(ill_other->ill_ipif);
20931 
20932 	return (0);
20933 }
20934 
20935 /* ARGSUSED */
20936 int
20937 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20938     ip_ioctl_cmd_t *ipip, void *ifreq)
20939 {
20940 	struct ifreq	*ifr = (struct ifreq *)ifreq;
20941 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20942 
20943 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
20944 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20945 	/* Get the interface index */
20946 	if (ipip->ipi_cmd_type == IF_CMD) {
20947 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20948 	} else {
20949 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
20950 	}
20951 	return (0);
20952 }
20953 
20954 /* ARGSUSED */
20955 int
20956 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20957     ip_ioctl_cmd_t *ipip, void *ifreq)
20958 {
20959 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20960 
20961 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
20962 		ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
20963 	/* Get the interface zone */
20964 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20965 	lifr->lifr_zoneid = ipif->ipif_zoneid;
20966 	return (0);
20967 }
20968 
20969 /*
20970  * Set the zoneid of an interface.
20971  */
20972 /* ARGSUSED */
20973 int
20974 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
20975     ip_ioctl_cmd_t *ipip, void *ifreq)
20976 {
20977 	struct lifreq	*lifr = (struct lifreq *)ifreq;
20978 	int err = 0;
20979 	boolean_t need_up = B_FALSE;
20980 	zone_t *zptr;
20981 	zone_status_t status;
20982 	zoneid_t zoneid;
20983 
20984 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
20985 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
20986 		if (!is_system_labeled())
20987 			return (ENOTSUP);
20988 		zoneid = GLOBAL_ZONEID;
20989 	}
20990 
20991 	/* cannot assign instance zero to a non-global zone */
20992 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
20993 		return (ENOTSUP);
20994 
20995 	/*
20996 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
20997 	 * the event of a race with the zone shutdown processing, since IP
20998 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
20999 	 * interface will be cleaned up even if the zone is shut down
21000 	 * immediately after the status check. If the interface can't be brought
21001 	 * down right away, and the zone is shut down before the restart
21002 	 * function is called, we resolve the possible races by rechecking the
21003 	 * zone status in the restart function.
21004 	 */
21005 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
21006 		return (EINVAL);
21007 	status = zone_status_get(zptr);
21008 	zone_rele(zptr);
21009 
21010 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
21011 		return (EINVAL);
21012 
21013 	if (ipif->ipif_flags & IPIF_UP) {
21014 		/*
21015 		 * If the interface is already marked up,
21016 		 * we call ipif_down which will take care
21017 		 * of ditching any IREs that have been set
21018 		 * up based on the old interface address.
21019 		 */
21020 		err = ipif_logical_down(ipif, q, mp);
21021 		if (err == EINPROGRESS)
21022 			return (err);
21023 		ipif_down_tail(ipif);
21024 		need_up = B_TRUE;
21025 	}
21026 
21027 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
21028 	return (err);
21029 }
21030 
21031 static int
21032 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
21033     queue_t *q, mblk_t *mp, boolean_t need_up)
21034 {
21035 	int	err = 0;
21036 
21037 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
21038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21039 
21040 	/* Set the new zone id. */
21041 	ipif->ipif_zoneid = zoneid;
21042 
21043 	/* Update sctp list */
21044 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
21045 
21046 	if (need_up) {
21047 		/*
21048 		 * Now bring the interface back up.  If this
21049 		 * is the only IPIF for the ILL, ipif_up
21050 		 * will have to re-bind to the device, so
21051 		 * we may get back EINPROGRESS, in which
21052 		 * case, this IOCTL will get completed in
21053 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
21054 		 */
21055 		err = ipif_up(ipif, q, mp);
21056 	}
21057 	return (err);
21058 }
21059 
21060 /* ARGSUSED */
21061 int
21062 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21063     ip_ioctl_cmd_t *ipip, void *if_req)
21064 {
21065 	struct lifreq *lifr = (struct lifreq *)if_req;
21066 	zoneid_t zoneid;
21067 	zone_t *zptr;
21068 	zone_status_t status;
21069 
21070 	ASSERT(ipif->ipif_id != 0);
21071 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
21072 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
21073 		zoneid = GLOBAL_ZONEID;
21074 
21075 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
21076 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21077 
21078 	/*
21079 	 * We recheck the zone status to resolve the following race condition:
21080 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
21081 	 * 2) hme0:1 is up and can't be brought down right away;
21082 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
21083 	 * 3) zone "myzone" is halted; the zone status switches to
21084 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
21085 	 * the interfaces to remove - hme0:1 is not returned because it's not
21086 	 * yet in "myzone", so it won't be removed;
21087 	 * 4) the restart function for SIOCSLIFZONE is called; without the
21088 	 * status check here, we would have hme0:1 in "myzone" after it's been
21089 	 * destroyed.
21090 	 * Note that if the status check fails, we need to bring the interface
21091 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
21092 	 * ipif_up_done[_v6]().
21093 	 */
21094 	status = ZONE_IS_UNINITIALIZED;
21095 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
21096 		status = zone_status_get(zptr);
21097 		zone_rele(zptr);
21098 	}
21099 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
21100 		if (ipif->ipif_isv6) {
21101 			(void) ipif_up_done_v6(ipif);
21102 		} else {
21103 			(void) ipif_up_done(ipif);
21104 		}
21105 		return (EINVAL);
21106 	}
21107 
21108 	ipif_down_tail(ipif);
21109 
21110 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
21111 	    B_TRUE));
21112 }
21113 
21114 /* ARGSUSED */
21115 int
21116 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21117 	ip_ioctl_cmd_t *ipip, void *ifreq)
21118 {
21119 	struct lifreq	*lifr = ifreq;
21120 
21121 	ASSERT(q->q_next == NULL);
21122 	ASSERT(CONN_Q(q));
21123 
21124 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
21125 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
21126 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
21127 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
21128 
21129 	return (0);
21130 }
21131 
21132 
21133 /* Find the previous ILL in this usesrc group */
21134 static ill_t *
21135 ill_prev_usesrc(ill_t *uill)
21136 {
21137 	ill_t *ill;
21138 
21139 	for (ill = uill->ill_usesrc_grp_next;
21140 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
21141 	    ill = ill->ill_usesrc_grp_next)
21142 		/* do nothing */;
21143 	return (ill);
21144 }
21145 
21146 /*
21147  * Release all members of the usesrc group. This routine is called
21148  * from ill_delete when the interface being unplumbed is the
21149  * group head.
21150  */
21151 static void
21152 ill_disband_usesrc_group(ill_t *uill)
21153 {
21154 	ill_t *next_ill, *tmp_ill;
21155 	ASSERT(RW_WRITE_HELD(&ill_g_usesrc_lock));
21156 	next_ill = uill->ill_usesrc_grp_next;
21157 
21158 	do {
21159 		ASSERT(next_ill != NULL);
21160 		tmp_ill = next_ill->ill_usesrc_grp_next;
21161 		ASSERT(tmp_ill != NULL);
21162 		next_ill->ill_usesrc_grp_next = NULL;
21163 		next_ill->ill_usesrc_ifindex = 0;
21164 		next_ill = tmp_ill;
21165 	} while (next_ill->ill_usesrc_ifindex != 0);
21166 	uill->ill_usesrc_grp_next = NULL;
21167 }
21168 
21169 /*
21170  * Remove the client usesrc ILL from the list and relink to a new list
21171  */
21172 int
21173 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
21174 {
21175 	ill_t *ill, *tmp_ill;
21176 
21177 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
21178 	    (uill != NULL) && RW_WRITE_HELD(&ill_g_usesrc_lock));
21179 
21180 	/*
21181 	 * Check if the usesrc client ILL passed in is not already
21182 	 * in use as a usesrc ILL i.e one whose source address is
21183 	 * in use OR a usesrc ILL is not already in use as a usesrc
21184 	 * client ILL
21185 	 */
21186 	if ((ucill->ill_usesrc_ifindex == 0) ||
21187 	    (uill->ill_usesrc_ifindex != 0)) {
21188 		return (-1);
21189 	}
21190 
21191 	ill = ill_prev_usesrc(ucill);
21192 	ASSERT(ill->ill_usesrc_grp_next != NULL);
21193 
21194 	/* Remove from the current list */
21195 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
21196 		/* Only two elements in the list */
21197 		ASSERT(ill->ill_usesrc_ifindex == 0);
21198 		ill->ill_usesrc_grp_next = NULL;
21199 	} else {
21200 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
21201 	}
21202 
21203 	if (ifindex == 0) {
21204 		ucill->ill_usesrc_ifindex = 0;
21205 		ucill->ill_usesrc_grp_next = NULL;
21206 		return (0);
21207 	}
21208 
21209 	ucill->ill_usesrc_ifindex = ifindex;
21210 	tmp_ill = uill->ill_usesrc_grp_next;
21211 	uill->ill_usesrc_grp_next = ucill;
21212 	ucill->ill_usesrc_grp_next =
21213 	    (tmp_ill != NULL) ? tmp_ill : uill;
21214 	return (0);
21215 }
21216 
21217 /*
21218  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
21219  * ip.c for locking details.
21220  */
21221 /* ARGSUSED */
21222 int
21223 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
21224     ip_ioctl_cmd_t *ipip, void *ifreq)
21225 {
21226 	struct lifreq *lifr = (struct lifreq *)ifreq;
21227 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE,
21228 	    ill_flag_changed = B_FALSE;
21229 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
21230 	int err = 0, ret;
21231 	uint_t ifindex;
21232 	phyint_t *us_phyint, *us_cli_phyint;
21233 	ipsq_t *ipsq = NULL;
21234 
21235 	ASSERT(IAM_WRITER_IPIF(ipif));
21236 	ASSERT(q->q_next == NULL);
21237 	ASSERT(CONN_Q(q));
21238 
21239 	isv6 = (Q_TO_CONN(q))->conn_af_isv6;
21240 	us_cli_phyint = usesrc_cli_ill->ill_phyint;
21241 
21242 	ASSERT(us_cli_phyint != NULL);
21243 
21244 	/*
21245 	 * If the client ILL is being used for IPMP, abort.
21246 	 * Note, this can be done before ipsq_try_enter since we are already
21247 	 * exclusive on this ILL
21248 	 */
21249 	if ((us_cli_phyint->phyint_groupname != NULL) ||
21250 	    (us_cli_phyint->phyint_flags & PHYI_STANDBY)) {
21251 		return (EINVAL);
21252 	}
21253 
21254 	ifindex = lifr->lifr_index;
21255 	if (ifindex == 0) {
21256 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
21257 			/* non usesrc group interface, nothing to reset */
21258 			return (0);
21259 		}
21260 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
21261 		/* valid reset request */
21262 		reset_flg = B_TRUE;
21263 	}
21264 
21265 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp,
21266 	    ip_process_ioctl, &err);
21267 
21268 	if (usesrc_ill == NULL) {
21269 		return (err);
21270 	}
21271 
21272 	/*
21273 	 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP
21274 	 * group nor can either of the interfaces be used for standy. So
21275 	 * to guarantee mutual exclusion with ip_sioctl_flags (which sets
21276 	 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname)
21277 	 * we need to be exclusive on the ipsq belonging to the usesrc_ill.
21278 	 * We are already exlusive on this ipsq i.e ipsq corresponding to
21279 	 * the usesrc_cli_ill
21280 	 */
21281 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
21282 	    NEW_OP, B_TRUE);
21283 	if (ipsq == NULL) {
21284 		err = EINPROGRESS;
21285 		/* Operation enqueued on the ipsq of the usesrc ILL */
21286 		goto done;
21287 	}
21288 
21289 	/* Check if the usesrc_ill is used for IPMP */
21290 	us_phyint = usesrc_ill->ill_phyint;
21291 	if ((us_phyint->phyint_groupname != NULL) ||
21292 	    (us_phyint->phyint_flags & PHYI_STANDBY)) {
21293 		err = EINVAL;
21294 		goto done;
21295 	}
21296 
21297 	/*
21298 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
21299 	 * already a client then return EINVAL
21300 	 */
21301 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
21302 		err = EINVAL;
21303 		goto done;
21304 	}
21305 
21306 	/*
21307 	 * If the ill_usesrc_ifindex field is already set to what it needs to
21308 	 * be then this is a duplicate operation.
21309 	 */
21310 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
21311 		err = 0;
21312 		goto done;
21313 	}
21314 
21315 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
21316 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
21317 	    usesrc_ill->ill_isv6));
21318 
21319 	/*
21320 	 * The next step ensures that no new ires will be created referencing
21321 	 * the client ill, until the ILL_CHANGING flag is cleared. Then
21322 	 * we go through an ire walk deleting all ire caches that reference
21323 	 * the client ill. New ires referencing the client ill that are added
21324 	 * to the ire table before the ILL_CHANGING flag is set, will be
21325 	 * cleaned up by the ire walk below. Attempt to add new ires referencing
21326 	 * the client ill while the ILL_CHANGING flag is set will be failed
21327 	 * during the ire_add in ire_atomic_start. ire_atomic_start atomically
21328 	 * checks (under the ill_g_usesrc_lock) that the ire being added
21329 	 * is not stale, i.e the ire_stq and ire_ipif are consistent and
21330 	 * belong to the same usesrc group.
21331 	 */
21332 	mutex_enter(&usesrc_cli_ill->ill_lock);
21333 	usesrc_cli_ill->ill_state_flags |= ILL_CHANGING;
21334 	mutex_exit(&usesrc_cli_ill->ill_lock);
21335 	ill_flag_changed = B_TRUE;
21336 
21337 	if (ipif->ipif_isv6)
21338 		ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21339 		    ALL_ZONES);
21340 	else
21341 		ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill,
21342 		    ALL_ZONES);
21343 
21344 	/*
21345 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
21346 	 * and the ill_usesrc_ifindex fields
21347 	 */
21348 	rw_enter(&ill_g_usesrc_lock, RW_WRITER);
21349 
21350 	if (reset_flg) {
21351 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
21352 		if (ret != 0) {
21353 			err = EINVAL;
21354 		}
21355 		rw_exit(&ill_g_usesrc_lock);
21356 		goto done;
21357 	}
21358 
21359 	/*
21360 	 * Four possibilities to consider:
21361 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
21362 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
21363 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
21364 	 * 4. Both are part of their respective usesrc groups
21365 	 */
21366 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
21367 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21368 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
21369 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21370 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21371 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
21372 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
21373 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
21374 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
21375 		/* Insert at head of list */
21376 		usesrc_cli_ill->ill_usesrc_grp_next =
21377 		    usesrc_ill->ill_usesrc_grp_next;
21378 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
21379 	} else {
21380 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
21381 		    ifindex);
21382 		if (ret != 0)
21383 			err = EINVAL;
21384 	}
21385 	rw_exit(&ill_g_usesrc_lock);
21386 
21387 done:
21388 	if (ill_flag_changed) {
21389 		mutex_enter(&usesrc_cli_ill->ill_lock);
21390 		usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING;
21391 		mutex_exit(&usesrc_cli_ill->ill_lock);
21392 	}
21393 	if (ipsq != NULL)
21394 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
21395 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
21396 	ill_refrele(usesrc_ill);
21397 	return (err);
21398 }
21399 
21400 /*
21401  * comparison function used by avl.
21402  */
21403 static int
21404 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
21405 {
21406 
21407 	uint_t index;
21408 
21409 	ASSERT(phyip != NULL && index_ptr != NULL);
21410 
21411 	index = *((uint_t *)index_ptr);
21412 	/*
21413 	 * let the phyint with the lowest index be on top.
21414 	 */
21415 	if (((phyint_t *)phyip)->phyint_ifindex < index)
21416 		return (1);
21417 	if (((phyint_t *)phyip)->phyint_ifindex > index)
21418 		return (-1);
21419 	return (0);
21420 }
21421 
21422 /*
21423  * comparison function used by avl.
21424  */
21425 static int
21426 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
21427 {
21428 	ill_t *ill;
21429 	int res = 0;
21430 
21431 	ASSERT(phyip != NULL && name_ptr != NULL);
21432 
21433 	if (((phyint_t *)phyip)->phyint_illv4)
21434 		ill = ((phyint_t *)phyip)->phyint_illv4;
21435 	else
21436 		ill = ((phyint_t *)phyip)->phyint_illv6;
21437 	ASSERT(ill != NULL);
21438 
21439 	res = strcmp(ill->ill_name, (char *)name_ptr);
21440 	if (res > 0)
21441 		return (1);
21442 	else if (res < 0)
21443 		return (-1);
21444 	return (0);
21445 }
21446 /*
21447  * This function is called from ill_delete when the ill is being
21448  * unplumbed. We remove the reference from the phyint and we also
21449  * free the phyint when there are no more references to it.
21450  */
21451 static void
21452 ill_phyint_free(ill_t *ill)
21453 {
21454 	phyint_t *phyi;
21455 	phyint_t *next_phyint;
21456 	ipsq_t *cur_ipsq;
21457 
21458 	ASSERT(ill->ill_phyint != NULL);
21459 
21460 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21461 	phyi = ill->ill_phyint;
21462 	ill->ill_phyint = NULL;
21463 	/*
21464 	 * ill_init allocates a phyint always to store the copy
21465 	 * of flags relevant to phyint. At that point in time, we could
21466 	 * not assign the name and hence phyint_illv4/v6 could not be
21467 	 * initialized. Later in ipif_set_values, we assign the name to
21468 	 * the ill, at which point in time we assign phyint_illv4/v6.
21469 	 * Thus we don't rely on phyint_illv6 to be initialized always.
21470 	 */
21471 	if (ill->ill_flags & ILLF_IPV6) {
21472 		phyi->phyint_illv6 = NULL;
21473 	} else {
21474 		phyi->phyint_illv4 = NULL;
21475 	}
21476 	/*
21477 	 * ipif_down removes it from the group when the last ipif goes
21478 	 * down.
21479 	 */
21480 	ASSERT(ill->ill_group == NULL);
21481 
21482 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL)
21483 		return;
21484 
21485 	/*
21486 	 * Make sure this phyint was put in the list.
21487 	 */
21488 	if (phyi->phyint_ifindex > 0) {
21489 		avl_remove(&phyint_g_list.phyint_list_avl_by_index,
21490 		    phyi);
21491 		avl_remove(&phyint_g_list.phyint_list_avl_by_name,
21492 		    phyi);
21493 	}
21494 	/*
21495 	 * remove phyint from the ipsq list.
21496 	 */
21497 	cur_ipsq = phyi->phyint_ipsq;
21498 	if (phyi == cur_ipsq->ipsq_phyint_list) {
21499 		cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next;
21500 	} else {
21501 		next_phyint = cur_ipsq->ipsq_phyint_list;
21502 		while (next_phyint != NULL) {
21503 			if (next_phyint->phyint_ipsq_next == phyi) {
21504 				next_phyint->phyint_ipsq_next =
21505 					phyi->phyint_ipsq_next;
21506 				break;
21507 			}
21508 			next_phyint = next_phyint->phyint_ipsq_next;
21509 		}
21510 		ASSERT(next_phyint != NULL);
21511 	}
21512 	IPSQ_DEC_REF(cur_ipsq);
21513 
21514 	if (phyi->phyint_groupname_len != 0) {
21515 		ASSERT(phyi->phyint_groupname != NULL);
21516 		mi_free(phyi->phyint_groupname);
21517 	}
21518 	mi_free(phyi);
21519 }
21520 
21521 /*
21522  * Attach the ill to the phyint structure which can be shared by both
21523  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
21524  * function is called from ipif_set_values and ill_lookup_on_name (for
21525  * loopback) where we know the name of the ill. We lookup the ill and if
21526  * there is one present already with the name use that phyint. Otherwise
21527  * reuse the one allocated by ill_init.
21528  */
21529 static void
21530 ill_phyint_reinit(ill_t *ill)
21531 {
21532 	boolean_t isv6 = ill->ill_isv6;
21533 	phyint_t *phyi_old;
21534 	phyint_t *phyi;
21535 	avl_index_t where = 0;
21536 	ill_t	*ill_other = NULL;
21537 	ipsq_t	*ipsq;
21538 
21539 	ASSERT(RW_WRITE_HELD(&ill_g_lock));
21540 
21541 	phyi_old = ill->ill_phyint;
21542 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
21543 	    phyi_old->phyint_illv6 == NULL));
21544 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
21545 	    phyi_old->phyint_illv4 == NULL));
21546 	ASSERT(phyi_old->phyint_ifindex == 0);
21547 
21548 	phyi = avl_find(&phyint_g_list.phyint_list_avl_by_name,
21549 	    ill->ill_name, &where);
21550 
21551 	/*
21552 	 * 1. We grabbed the ill_g_lock before inserting this ill into
21553 	 *    the global list of ills. So no other thread could have located
21554 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
21555 	 * 2. Now locate the other protocol instance of this ill.
21556 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
21557 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
21558 	 *    of neither ill can change.
21559 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
21560 	 *    other ill.
21561 	 * 5. Release all locks.
21562 	 */
21563 
21564 	/*
21565 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
21566 	 * we are initializing IPv4.
21567 	 */
21568 	if (phyi != NULL) {
21569 		ill_other = (isv6) ? phyi->phyint_illv4 :
21570 		    phyi->phyint_illv6;
21571 		ASSERT(ill_other->ill_phyint != NULL);
21572 		ASSERT((isv6 && !ill_other->ill_isv6) ||
21573 		    (!isv6 && ill_other->ill_isv6));
21574 		GRAB_ILL_LOCKS(ill, ill_other);
21575 		/*
21576 		 * We are potentially throwing away phyint_flags which
21577 		 * could be different from the one that we obtain from
21578 		 * ill_other->ill_phyint. But it is okay as we are assuming
21579 		 * that the state maintained within IP is correct.
21580 		 */
21581 		mutex_enter(&phyi->phyint_lock);
21582 		if (isv6) {
21583 			ASSERT(phyi->phyint_illv6 == NULL);
21584 			phyi->phyint_illv6 = ill;
21585 		} else {
21586 			ASSERT(phyi->phyint_illv4 == NULL);
21587 			phyi->phyint_illv4 = ill;
21588 		}
21589 		/*
21590 		 * This is a new ill, currently undergoing SLIFNAME
21591 		 * So we could not have joined an IPMP group until now.
21592 		 */
21593 		ASSERT(phyi_old->phyint_ipsq_next == NULL &&
21594 		    phyi_old->phyint_groupname == NULL);
21595 
21596 		/*
21597 		 * This phyi_old is going away. Decref ipsq_refs and
21598 		 * assert it is zero. The ipsq itself will be freed in
21599 		 * ipsq_exit
21600 		 */
21601 		ipsq = phyi_old->phyint_ipsq;
21602 		IPSQ_DEC_REF(ipsq);
21603 		ASSERT(ipsq->ipsq_refs == 0);
21604 		/* Get the singleton phyint out of the ipsq list */
21605 		ASSERT(phyi_old->phyint_ipsq_next == NULL);
21606 		ipsq->ipsq_phyint_list = NULL;
21607 		phyi_old->phyint_illv4 = NULL;
21608 		phyi_old->phyint_illv6 = NULL;
21609 		mi_free(phyi_old);
21610 	} else {
21611 		mutex_enter(&ill->ill_lock);
21612 		/*
21613 		 * We don't need to acquire any lock, since
21614 		 * the ill is not yet visible globally  and we
21615 		 * have not yet released the ill_g_lock.
21616 		 */
21617 		phyi = phyi_old;
21618 		mutex_enter(&phyi->phyint_lock);
21619 		/* XXX We need a recovery strategy here. */
21620 		if (!phyint_assign_ifindex(phyi))
21621 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
21622 
21623 		avl_insert(&phyint_g_list.phyint_list_avl_by_name,
21624 		    (void *)phyi, where);
21625 
21626 		(void) avl_find(&phyint_g_list.phyint_list_avl_by_index,
21627 		    &phyi->phyint_ifindex, &where);
21628 		avl_insert(&phyint_g_list.phyint_list_avl_by_index,
21629 		    (void *)phyi, where);
21630 	}
21631 
21632 	/*
21633 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
21634 	 * pending mp is not affected because that is per ill basis.
21635 	 */
21636 	ill->ill_phyint = phyi;
21637 
21638 	/*
21639 	 * Keep the index on ipif_orig_index to be used by FAILOVER.
21640 	 * We do this here as when the first ipif was allocated,
21641 	 * ipif_allocate does not know the right interface index.
21642 	 */
21643 
21644 	ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex;
21645 	/*
21646 	 * Now that the phyint's ifindex has been assigned, complete the
21647 	 * remaining
21648 	 */
21649 	if (ill->ill_isv6) {
21650 		ill->ill_ip6_mib->ipv6IfIndex =
21651 		    ill->ill_phyint->phyint_ifindex;
21652 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
21653 		    ill->ill_phyint->phyint_ifindex;
21654 	}
21655 
21656 	RELEASE_ILL_LOCKS(ill, ill_other);
21657 	mutex_exit(&phyi->phyint_lock);
21658 }
21659 
21660 /*
21661  * Notify any downstream modules of the name of this interface.
21662  * An M_IOCTL is used even though we don't expect a successful reply.
21663  * Any reply message from the driver (presumably an M_IOCNAK) will
21664  * eventually get discarded somewhere upstream.  The message format is
21665  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
21666  * to IP.
21667  */
21668 static void
21669 ip_ifname_notify(ill_t *ill, queue_t *q)
21670 {
21671 	mblk_t *mp1, *mp2;
21672 	struct iocblk *iocp;
21673 	struct lifreq *lifr;
21674 
21675 	mp1 = mkiocb(SIOCSLIFNAME);
21676 	if (mp1 == NULL)
21677 		return;
21678 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
21679 	if (mp2 == NULL) {
21680 		freeb(mp1);
21681 		return;
21682 	}
21683 
21684 	mp1->b_cont = mp2;
21685 	iocp = (struct iocblk *)mp1->b_rptr;
21686 	iocp->ioc_count = sizeof (struct lifreq);
21687 
21688 	lifr = (struct lifreq *)mp2->b_rptr;
21689 	mp2->b_wptr += sizeof (struct lifreq);
21690 	bzero(lifr, sizeof (struct lifreq));
21691 
21692 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
21693 	lifr->lifr_ppa = ill->ill_ppa;
21694 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
21695 
21696 	putnext(q, mp1);
21697 }
21698 
21699 static boolean_t ip_trash_timer_started = B_FALSE;
21700 
21701 static int
21702 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
21703 {
21704 	int err;
21705 
21706 	/* Set the obsolete NDD per-interface forwarding name. */
21707 	err = ill_set_ndd_name(ill);
21708 	if (err != 0) {
21709 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
21710 		    err);
21711 	}
21712 
21713 	/* Tell downstream modules where they are. */
21714 	ip_ifname_notify(ill, q);
21715 
21716 	/*
21717 	 * ill_dl_phys returns EINPROGRESS in the usual case.
21718 	 * Error cases are ENOMEM ...
21719 	 */
21720 	err = ill_dl_phys(ill, ipif, mp, q);
21721 
21722 	/*
21723 	 * If there is no IRE expiration timer running, get one started.
21724 	 * igmp and mld timers will be triggered by the first multicast
21725 	 */
21726 	if (!ip_trash_timer_started) {
21727 		/*
21728 		 * acquire the lock and check again.
21729 		 */
21730 		mutex_enter(&ip_trash_timer_lock);
21731 		if (!ip_trash_timer_started) {
21732 			ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL,
21733 			    MSEC_TO_TICK(ip_timer_interval));
21734 			ip_trash_timer_started = B_TRUE;
21735 		}
21736 		mutex_exit(&ip_trash_timer_lock);
21737 	}
21738 
21739 	if (ill->ill_isv6) {
21740 		mutex_enter(&mld_slowtimeout_lock);
21741 		if (mld_slowtimeout_id == 0) {
21742 			mld_slowtimeout_id = timeout(mld_slowtimo, NULL,
21743 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21744 		}
21745 		mutex_exit(&mld_slowtimeout_lock);
21746 	} else {
21747 		mutex_enter(&igmp_slowtimeout_lock);
21748 		if (igmp_slowtimeout_id == 0) {
21749 			igmp_slowtimeout_id = timeout(igmp_slowtimo, NULL,
21750 				MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
21751 		}
21752 		mutex_exit(&igmp_slowtimeout_lock);
21753 	}
21754 
21755 	return (err);
21756 }
21757 
21758 /*
21759  * Common routine for ppa and ifname setting. Should be called exclusive.
21760  *
21761  * Returns EINPROGRESS when mp has been consumed by queueing it on
21762  * ill_pending_mp and the ioctl will complete in ip_rput.
21763  *
21764  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
21765  * the new name and new ppa in lifr_name and lifr_ppa respectively.
21766  * For SLIFNAME, we pass these values back to the userland.
21767  */
21768 static int
21769 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
21770 {
21771 	ill_t	*ill;
21772 	ipif_t	*ipif;
21773 	ipsq_t	*ipsq;
21774 	char	*ppa_ptr;
21775 	char	*old_ptr;
21776 	char	old_char;
21777 	int	error;
21778 
21779 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
21780 	ASSERT(q->q_next != NULL);
21781 	ASSERT(interf_name != NULL);
21782 
21783 	ill = (ill_t *)q->q_ptr;
21784 
21785 	ASSERT(ill->ill_name[0] == '\0');
21786 	ASSERT(IAM_WRITER_ILL(ill));
21787 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
21788 	ASSERT(ill->ill_ppa == UINT_MAX);
21789 
21790 	/* The ppa is sent down by ifconfig or is chosen */
21791 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
21792 		return (EINVAL);
21793 	}
21794 
21795 	/*
21796 	 * make sure ppa passed in is same as ppa in the name.
21797 	 * This check is not made when ppa == UINT_MAX in that case ppa
21798 	 * in the name could be anything. System will choose a ppa and
21799 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
21800 	 */
21801 	if (*new_ppa_ptr != UINT_MAX) {
21802 		/* stoi changes the pointer */
21803 		old_ptr = ppa_ptr;
21804 		/*
21805 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
21806 		 * (they don't have an externally visible ppa).  We assign one
21807 		 * here so that we can manage the interface.  Note that in
21808 		 * the past this value was always 0 for DLPI 1 drivers.
21809 		 */
21810 		if (*new_ppa_ptr == 0)
21811 			*new_ppa_ptr = stoi(&old_ptr);
21812 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
21813 			return (EINVAL);
21814 	}
21815 	/*
21816 	 * terminate string before ppa
21817 	 * save char at that location.
21818 	 */
21819 	old_char = ppa_ptr[0];
21820 	ppa_ptr[0] = '\0';
21821 
21822 	ill->ill_ppa = *new_ppa_ptr;
21823 	/*
21824 	 * Finish as much work now as possible before calling ill_glist_insert
21825 	 * which makes the ill globally visible and also merges it with the
21826 	 * other protocol instance of this phyint. The remaining work is
21827 	 * done after entering the ipsq which may happen sometime later.
21828 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
21829 	 */
21830 	ipif = ill->ill_ipif;
21831 
21832 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
21833 	ipif_assign_seqid(ipif);
21834 
21835 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
21836 		ill->ill_flags |= ILLF_IPV4;
21837 
21838 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
21839 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
21840 
21841 	if (ill->ill_flags & ILLF_IPV6) {
21842 
21843 		ill->ill_isv6 = B_TRUE;
21844 		if (ill->ill_rq != NULL) {
21845 			ill->ill_rq->q_qinfo = &rinit_ipv6;
21846 			ill->ill_wq->q_qinfo = &winit_ipv6;
21847 		}
21848 
21849 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
21850 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
21851 		ipif->ipif_v6src_addr = ipv6_all_zeros;
21852 		ipif->ipif_v6subnet = ipv6_all_zeros;
21853 		ipif->ipif_v6net_mask = ipv6_all_zeros;
21854 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
21855 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
21856 		/*
21857 		 * point-to-point or Non-mulicast capable
21858 		 * interfaces won't do NUD unless explicitly
21859 		 * configured to do so.
21860 		 */
21861 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
21862 		    !(ill->ill_flags & ILLF_MULTICAST)) {
21863 			ill->ill_flags |= ILLF_NONUD;
21864 		}
21865 		/* Make sure IPv4 specific flag is not set on IPv6 if */
21866 		if (ill->ill_flags & ILLF_NOARP) {
21867 			/*
21868 			 * Note: xresolv interfaces will eventually need
21869 			 * NOARP set here as well, but that will require
21870 			 * those external resolvers to have some
21871 			 * knowledge of that flag and act appropriately.
21872 			 * Not to be changed at present.
21873 			 */
21874 			ill->ill_flags &= ~ILLF_NOARP;
21875 		}
21876 		/*
21877 		 * Set the ILLF_ROUTER flag according to the global
21878 		 * IPv6 forwarding policy.
21879 		 */
21880 		if (ipv6_forward != 0)
21881 			ill->ill_flags |= ILLF_ROUTER;
21882 	} else if (ill->ill_flags & ILLF_IPV4) {
21883 		ill->ill_isv6 = B_FALSE;
21884 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
21885 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr);
21886 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
21887 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
21888 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
21889 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
21890 		/*
21891 		 * Set the ILLF_ROUTER flag according to the global
21892 		 * IPv4 forwarding policy.
21893 		 */
21894 		if (ip_g_forward != 0)
21895 			ill->ill_flags |= ILLF_ROUTER;
21896 	}
21897 
21898 	ASSERT(ill->ill_phyint != NULL);
21899 
21900 	/*
21901 	 * The ipv6Ifindex and ipv6IfIcmpIfIndex assignments will
21902 	 * be completed in ill_glist_insert -> ill_phyint_reinit
21903 	 */
21904 	if (ill->ill_isv6) {
21905 		/* allocate v6 mib */
21906 		if (!ill_allocate_mibs(ill))
21907 			return (ENOMEM);
21908 	}
21909 
21910 	/*
21911 	 * Pick a default sap until we get the DL_INFO_ACK back from
21912 	 * the driver.
21913 	 */
21914 	if (ill->ill_sap == 0) {
21915 		if (ill->ill_isv6)
21916 			ill->ill_sap  = IP6_DL_SAP;
21917 		else
21918 			ill->ill_sap  = IP_DL_SAP;
21919 	}
21920 
21921 	ill->ill_ifname_pending = 1;
21922 	ill->ill_ifname_pending_err = 0;
21923 
21924 	ill_refhold(ill);
21925 	rw_enter(&ill_g_lock, RW_WRITER);
21926 	if ((error = ill_glist_insert(ill, interf_name,
21927 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
21928 		ill->ill_ppa = UINT_MAX;
21929 		ill->ill_name[0] = '\0';
21930 		/*
21931 		 * undo null termination done above.
21932 		 */
21933 		ppa_ptr[0] = old_char;
21934 		rw_exit(&ill_g_lock);
21935 		ill_refrele(ill);
21936 		return (error);
21937 	}
21938 
21939 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
21940 
21941 	/*
21942 	 * When we return the buffer pointed to by interf_name should contain
21943 	 * the same name as in ill_name.
21944 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
21945 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
21946 	 * so copy full name and update the ppa ptr.
21947 	 * When ppa passed in != UINT_MAX all values are correct just undo
21948 	 * null termination, this saves a bcopy.
21949 	 */
21950 	if (*new_ppa_ptr == UINT_MAX) {
21951 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
21952 		*new_ppa_ptr = ill->ill_ppa;
21953 	} else {
21954 		/*
21955 		 * undo null termination done above.
21956 		 */
21957 		ppa_ptr[0] = old_char;
21958 	}
21959 
21960 	/* Let SCTP know about this ILL */
21961 	sctp_update_ill(ill, SCTP_ILL_INSERT);
21962 
21963 	/* and also about the first ipif */
21964 	sctp_update_ipif(ipif, SCTP_IPIF_INSERT);
21965 
21966 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP,
21967 	    B_TRUE);
21968 
21969 	rw_exit(&ill_g_lock);
21970 	ill_refrele(ill);
21971 	if (ipsq == NULL)
21972 		return (EINPROGRESS);
21973 
21974 	/*
21975 	 * Need to set the ipsq_current_ipif now, if we have changed ipsq
21976 	 * due to the phyint merge in ill_phyint_reinit.
21977 	 */
21978 	ASSERT(ipsq->ipsq_current_ipif == NULL ||
21979 		ipsq->ipsq_current_ipif == ipif);
21980 	ipsq->ipsq_current_ipif = ipif;
21981 	ipsq->ipsq_last_cmd = SIOCSLIFNAME;
21982 	error = ipif_set_values_tail(ill, ipif, mp, q);
21983 	ipsq_exit(ipsq, B_TRUE, B_TRUE);
21984 	if (error != 0 && error != EINPROGRESS) {
21985 		/*
21986 		 * restore previous values
21987 		 */
21988 		ill->ill_isv6 = B_FALSE;
21989 	}
21990 	return (error);
21991 }
21992 
21993 
21994 extern void (*ip_cleanup_func)(void);
21995 
21996 void
21997 ipif_init(void)
21998 {
21999 	hrtime_t hrt;
22000 	int i;
22001 
22002 	/*
22003 	 * Can't call drv_getparm here as it is too early in the boot.
22004 	 * As we use ipif_src_random just for picking a different
22005 	 * source address everytime, this need not be really random.
22006 	 */
22007 	hrt = gethrtime();
22008 	ipif_src_random = ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff);
22009 
22010 	for (i = 0; i < MAX_G_HEADS; i++) {
22011 		ill_g_heads[i].ill_g_list_head = (ill_if_t *)&ill_g_heads[i];
22012 		ill_g_heads[i].ill_g_list_tail = (ill_if_t *)&ill_g_heads[i];
22013 	}
22014 
22015 	avl_create(&phyint_g_list.phyint_list_avl_by_index,
22016 	    ill_phyint_compare_index,
22017 	    sizeof (phyint_t),
22018 	    offsetof(struct phyint, phyint_avl_by_index));
22019 	avl_create(&phyint_g_list.phyint_list_avl_by_name,
22020 	    ill_phyint_compare_name,
22021 	    sizeof (phyint_t),
22022 	    offsetof(struct phyint, phyint_avl_by_name));
22023 
22024 	ip_cleanup_func = ip_thread_exit;
22025 }
22026 
22027 /*
22028  * This is called by ip_rt_add when src_addr value is other than zero.
22029  * src_addr signifies the source address of the incoming packet. For
22030  * reverse tunnel route we need to create a source addr based routing
22031  * table. This routine creates ip_mrtun_table if it's empty and then
22032  * it adds the route entry hashed by source address. It verifies that
22033  * the outgoing interface is always a non-resolver interface (tunnel).
22034  */
22035 int
22036 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg,
22037     ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func)
22038 {
22039 	ire_t   *ire;
22040 	ire_t	*save_ire;
22041 	ipif_t  *ipif;
22042 	ill_t   *in_ill = NULL;
22043 	ill_t	*out_ill;
22044 	queue_t	*stq;
22045 	mblk_t	*dlureq_mp;
22046 	int	error;
22047 
22048 	if (ire_arg != NULL)
22049 		*ire_arg = NULL;
22050 	ASSERT(in_src_addr != INADDR_ANY);
22051 
22052 	ipif = ipif_arg;
22053 	if (ipif != NULL) {
22054 		out_ill = ipif->ipif_ill;
22055 	} else {
22056 		ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n"));
22057 		return (EINVAL);
22058 	}
22059 
22060 	if (src_ipif == NULL) {
22061 		ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n"));
22062 		return (EINVAL);
22063 	}
22064 	in_ill = src_ipif->ipif_ill;
22065 
22066 	/*
22067 	 * Check for duplicates. We don't need to
22068 	 * match out_ill, because the uniqueness of
22069 	 * a route is only dependent on src_addr and
22070 	 * in_ill.
22071 	 */
22072 	ire = ire_mrtun_lookup(in_src_addr, in_ill);
22073 	if (ire != NULL) {
22074 		ire_refrele(ire);
22075 		return (EEXIST);
22076 	}
22077 	if (ipif->ipif_net_type != IRE_IF_NORESOLVER) {
22078 		ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n",
22079 		    ipif->ipif_net_type));
22080 		return (EINVAL);
22081 	}
22082 
22083 	stq = ipif->ipif_wq;
22084 	ASSERT(stq != NULL);
22085 
22086 	/*
22087 	 * The outgoing interface must be non-resolver
22088 	 * interface.
22089 	 */
22090 	dlureq_mp = ill_dlur_gen(NULL,
22091 	    out_ill->ill_phys_addr_length, out_ill->ill_sap,
22092 	    out_ill->ill_sap_length);
22093 
22094 	if (dlureq_mp == NULL) {
22095 		ip1dbg(("ip_newroute: dlureq_mp NULL\n"));
22096 		return (ENOMEM);
22097 	}
22098 
22099 	/* Create the IRE. */
22100 
22101 	ire = ire_create(
22102 	    NULL,				/* Zero dst addr */
22103 	    NULL,				/* Zero mask */
22104 	    NULL,				/* Zero gateway addr */
22105 	    NULL,				/* Zero ipif_src addr */
22106 	    (uint8_t *)&in_src_addr,		/* in_src-addr */
22107 	    &ipif->ipif_mtu,
22108 	    NULL,
22109 	    NULL,				/* rfq */
22110 	    stq,
22111 	    IRE_MIPRTUN,
22112 	    dlureq_mp,
22113 	    ipif,
22114 	    in_ill,
22115 	    0,
22116 	    0,
22117 	    0,
22118 	    flags,
22119 	    &ire_uinfo_null,
22120 	    NULL,
22121 	    NULL);
22122 
22123 	if (ire == NULL)
22124 		return (ENOMEM);
22125 	ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n",
22126 	    ire->ire_type));
22127 	save_ire = ire;
22128 	ASSERT(save_ire != NULL);
22129 	error = ire_add_mrtun(&ire, q, mp, func);
22130 	/*
22131 	 * If ire_add_mrtun() failed, the ire passed in was freed
22132 	 * so there is no need to do so here.
22133 	 */
22134 	if (error != 0) {
22135 		return (error);
22136 	}
22137 
22138 	/* Duplicate check */
22139 	if (ire != save_ire) {
22140 		/* route already exists by now */
22141 		ire_refrele(ire);
22142 		return (EEXIST);
22143 	}
22144 
22145 	if (ire_arg != NULL) {
22146 		/*
22147 		 * Store the ire that was just added. the caller
22148 		 * ip_rts_request responsible for doing ire_refrele()
22149 		 * on it.
22150 		 */
22151 		*ire_arg = ire;
22152 	} else {
22153 		ire_refrele(ire);	/* held in ire_add_mrtun */
22154 	}
22155 
22156 	return (0);
22157 }
22158 
22159 /*
22160  * It is called by ip_rt_delete() only when mipagent requests to delete
22161  * a reverse tunnel route that was added by ip_mrtun_rt_add() before.
22162  */
22163 
22164 int
22165 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif)
22166 {
22167 	ire_t   *ire = NULL;
22168 
22169 	if (in_src_addr == INADDR_ANY)
22170 		return (EINVAL);
22171 	if (src_ipif == NULL)
22172 		return (EINVAL);
22173 
22174 	/* search if this route exists in the ip_mrtun_table */
22175 	ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill);
22176 	if (ire == NULL) {
22177 		ip2dbg(("ip_mrtun_rt_delete: ire not found\n"));
22178 		return (ESRCH);
22179 	}
22180 	ire_delete(ire);
22181 	ire_refrele(ire);
22182 	return (0);
22183 }
22184 
22185 /*
22186  * Lookup the ipif corresponding to the onlink destination address. For
22187  * point-to-point interfaces, it matches with remote endpoint destination
22188  * address. For point-to-multipoint interfaces it only tries to match the
22189  * destination with the interface's subnet address. The longest, most specific
22190  * match is found to take care of such rare network configurations like -
22191  * le0: 129.146.1.1/16
22192  * le1: 129.146.2.2/24
22193  * It is used only by SO_DONTROUTE at the moment.
22194  */
22195 ipif_t *
22196 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid)
22197 {
22198 	ipif_t	*ipif, *best_ipif;
22199 	ill_t	*ill;
22200 	ill_walk_context_t ctx;
22201 
22202 	ASSERT(zoneid != ALL_ZONES);
22203 	best_ipif = NULL;
22204 
22205 	rw_enter(&ill_g_lock, RW_READER);
22206 	ill = ILL_START_WALK_V4(&ctx);
22207 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
22208 		mutex_enter(&ill->ill_lock);
22209 		for (ipif = ill->ill_ipif; ipif != NULL;
22210 		    ipif = ipif->ipif_next) {
22211 			if (!IPIF_CAN_LOOKUP(ipif))
22212 				continue;
22213 			if (ipif->ipif_zoneid != zoneid &&
22214 			    ipif->ipif_zoneid != ALL_ZONES)
22215 				continue;
22216 			/*
22217 			 * Point-to-point case. Look for exact match with
22218 			 * destination address.
22219 			 */
22220 			if (ipif->ipif_flags & IPIF_POINTOPOINT) {
22221 				if (ipif->ipif_pp_dst_addr == addr) {
22222 					ipif_refhold_locked(ipif);
22223 					mutex_exit(&ill->ill_lock);
22224 					rw_exit(&ill_g_lock);
22225 					if (best_ipif != NULL)
22226 						ipif_refrele(best_ipif);
22227 					return (ipif);
22228 				}
22229 			} else if (ipif->ipif_subnet == (addr &
22230 			    ipif->ipif_net_mask)) {
22231 				/*
22232 				 * Point-to-multipoint case. Looping through to
22233 				 * find the most specific match. If there are
22234 				 * multiple best match ipif's then prefer ipif's
22235 				 * that are UP. If there is only one best match
22236 				 * ipif and it is DOWN we must still return it.
22237 				 */
22238 				if ((best_ipif == NULL) ||
22239 				    (ipif->ipif_net_mask >
22240 				    best_ipif->ipif_net_mask) ||
22241 				    ((ipif->ipif_net_mask ==
22242 				    best_ipif->ipif_net_mask) &&
22243 				    ((ipif->ipif_flags & IPIF_UP) &&
22244 				    (!(best_ipif->ipif_flags & IPIF_UP))))) {
22245 					ipif_refhold_locked(ipif);
22246 					mutex_exit(&ill->ill_lock);
22247 					rw_exit(&ill_g_lock);
22248 					if (best_ipif != NULL)
22249 						ipif_refrele(best_ipif);
22250 					best_ipif = ipif;
22251 					rw_enter(&ill_g_lock, RW_READER);
22252 					mutex_enter(&ill->ill_lock);
22253 				}
22254 			}
22255 		}
22256 		mutex_exit(&ill->ill_lock);
22257 	}
22258 	rw_exit(&ill_g_lock);
22259 	return (best_ipif);
22260 }
22261 
22262 
22263 /*
22264  * Save enough information so that we can recreate the IRE if
22265  * the interface goes down and then up.
22266  */
22267 static void
22268 ipif_save_ire(ipif_t *ipif, ire_t *ire)
22269 {
22270 	mblk_t	*save_mp;
22271 
22272 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
22273 	if (save_mp != NULL) {
22274 		ifrt_t	*ifrt;
22275 
22276 		save_mp->b_wptr += sizeof (ifrt_t);
22277 		ifrt = (ifrt_t *)save_mp->b_rptr;
22278 		bzero(ifrt, sizeof (ifrt_t));
22279 		ifrt->ifrt_type = ire->ire_type;
22280 		ifrt->ifrt_addr = ire->ire_addr;
22281 		ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
22282 		ifrt->ifrt_src_addr = ire->ire_src_addr;
22283 		ifrt->ifrt_mask = ire->ire_mask;
22284 		ifrt->ifrt_flags = ire->ire_flags;
22285 		ifrt->ifrt_max_frag = ire->ire_max_frag;
22286 		mutex_enter(&ipif->ipif_saved_ire_lock);
22287 		save_mp->b_cont = ipif->ipif_saved_ire_mp;
22288 		ipif->ipif_saved_ire_mp = save_mp;
22289 		ipif->ipif_saved_ire_cnt++;
22290 		mutex_exit(&ipif->ipif_saved_ire_lock);
22291 	}
22292 }
22293 
22294 
22295 static void
22296 ipif_remove_ire(ipif_t *ipif, ire_t *ire)
22297 {
22298 	mblk_t	**mpp;
22299 	mblk_t	*mp;
22300 	ifrt_t	*ifrt;
22301 
22302 	/* Remove from ipif_saved_ire_mp list if it is there */
22303 	mutex_enter(&ipif->ipif_saved_ire_lock);
22304 	for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL;
22305 	    mpp = &(*mpp)->b_cont) {
22306 		/*
22307 		 * On a given ipif, the triple of address, gateway and
22308 		 * mask is unique for each saved IRE (in the case of
22309 		 * ordinary interface routes, the gateway address is
22310 		 * all-zeroes).
22311 		 */
22312 		mp = *mpp;
22313 		ifrt = (ifrt_t *)mp->b_rptr;
22314 		if (ifrt->ifrt_addr == ire->ire_addr &&
22315 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
22316 		    ifrt->ifrt_mask == ire->ire_mask) {
22317 			*mpp = mp->b_cont;
22318 			ipif->ipif_saved_ire_cnt--;
22319 			freeb(mp);
22320 			break;
22321 		}
22322 	}
22323 	mutex_exit(&ipif->ipif_saved_ire_lock);
22324 }
22325 
22326 
22327 /*
22328  * IP multirouting broadcast routes handling
22329  * Append CGTP broadcast IREs to regular ones created
22330  * at ifconfig time.
22331  */
22332 static void
22333 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst)
22334 {
22335 	ire_t *ire_prim;
22336 
22337 	ASSERT(ire != NULL);
22338 	ASSERT(ire_dst != NULL);
22339 
22340 	ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22341 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22342 	if (ire_prim != NULL) {
22343 		/*
22344 		 * We are in the special case of broadcasts for
22345 		 * CGTP. We add an IRE_BROADCAST that holds
22346 		 * the RTF_MULTIRT flag, the destination
22347 		 * address of ire_dst and the low level
22348 		 * info of ire_prim. In other words, CGTP
22349 		 * broadcast is added to the redundant ipif.
22350 		 */
22351 		ipif_t *ipif_prim;
22352 		ire_t  *bcast_ire;
22353 
22354 		ipif_prim = ire_prim->ire_ipif;
22355 
22356 		ip2dbg(("ip_cgtp_filter_bcast_add: "
22357 		    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22358 		    (void *)ire_dst, (void *)ire_prim,
22359 		    (void *)ipif_prim));
22360 
22361 		bcast_ire = ire_create(
22362 		    (uchar_t *)&ire->ire_addr,
22363 		    (uchar_t *)&ip_g_all_ones,
22364 		    (uchar_t *)&ire_dst->ire_src_addr,
22365 		    (uchar_t *)&ire->ire_gateway_addr,
22366 		    NULL,
22367 		    &ipif_prim->ipif_mtu,
22368 		    NULL,
22369 		    ipif_prim->ipif_rq,
22370 		    ipif_prim->ipif_wq,
22371 		    IRE_BROADCAST,
22372 		    ipif_prim->ipif_bcast_mp,
22373 		    ipif_prim,
22374 		    NULL,
22375 		    0,
22376 		    0,
22377 		    0,
22378 		    ire->ire_flags,
22379 		    &ire_uinfo_null,
22380 		    NULL,
22381 		    NULL);
22382 
22383 		if (bcast_ire != NULL) {
22384 
22385 			if (ire_add(&bcast_ire, NULL, NULL, NULL) == 0) {
22386 				ip2dbg(("ip_cgtp_filter_bcast_add: "
22387 				    "added bcast_ire %p\n",
22388 				    (void *)bcast_ire));
22389 
22390 				ipif_save_ire(bcast_ire->ire_ipif,
22391 				    bcast_ire);
22392 				ire_refrele(bcast_ire);
22393 			}
22394 		}
22395 		ire_refrele(ire_prim);
22396 	}
22397 }
22398 
22399 
22400 /*
22401  * IP multirouting broadcast routes handling
22402  * Remove the broadcast ire
22403  */
22404 static void
22405 ip_cgtp_bcast_delete(ire_t *ire)
22406 {
22407 	ire_t *ire_dst;
22408 
22409 	ASSERT(ire != NULL);
22410 	ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST,
22411 	    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22412 	if (ire_dst != NULL) {
22413 		ire_t *ire_prim;
22414 
22415 		ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0,
22416 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE);
22417 		if (ire_prim != NULL) {
22418 			ipif_t *ipif_prim;
22419 			ire_t  *bcast_ire;
22420 
22421 			ipif_prim = ire_prim->ire_ipif;
22422 
22423 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
22424 			    "ire_dst %p, ire_prim %p, ipif_prim %p\n",
22425 			    (void *)ire_dst, (void *)ire_prim,
22426 			    (void *)ipif_prim));
22427 
22428 			bcast_ire = ire_ctable_lookup(ire->ire_addr,
22429 			    ire->ire_gateway_addr,
22430 			    IRE_BROADCAST,
22431 			    ipif_prim, ALL_ZONES,
22432 			    NULL,
22433 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF |
22434 			    MATCH_IRE_MASK);
22435 
22436 			if (bcast_ire != NULL) {
22437 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
22438 				    "looked up bcast_ire %p\n",
22439 				    (void *)bcast_ire));
22440 				ipif_remove_ire(bcast_ire->ire_ipif,
22441 					bcast_ire);
22442 				ire_delete(bcast_ire);
22443 			}
22444 			ire_refrele(ire_prim);
22445 		}
22446 		ire_refrele(ire_dst);
22447 	}
22448 }
22449 
22450 /*
22451  * IPsec hardware acceleration capabilities related functions.
22452  */
22453 
22454 /*
22455  * Free a per-ill IPsec capabilities structure.
22456  */
22457 static void
22458 ill_ipsec_capab_free(ill_ipsec_capab_t *capab)
22459 {
22460 	if (capab->auth_hw_algs != NULL)
22461 		kmem_free(capab->auth_hw_algs, capab->algs_size);
22462 	if (capab->encr_hw_algs != NULL)
22463 		kmem_free(capab->encr_hw_algs, capab->algs_size);
22464 	if (capab->encr_algparm != NULL)
22465 		kmem_free(capab->encr_algparm, capab->encr_algparm_size);
22466 	kmem_free(capab, sizeof (ill_ipsec_capab_t));
22467 }
22468 
22469 /*
22470  * Allocate a new per-ill IPsec capabilities structure. This structure
22471  * is specific to an IPsec protocol (AH or ESP). It is implemented as
22472  * an array which specifies, for each algorithm, whether this algorithm
22473  * is supported by the ill or not.
22474  */
22475 static ill_ipsec_capab_t *
22476 ill_ipsec_capab_alloc(void)
22477 {
22478 	ill_ipsec_capab_t *capab;
22479 	uint_t nelems;
22480 
22481 	capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP);
22482 	if (capab == NULL)
22483 		return (NULL);
22484 
22485 	/* we need one bit per algorithm */
22486 	nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t);
22487 	capab->algs_size = nelems * sizeof (ipsec_capab_elem_t);
22488 
22489 	/* allocate memory to store algorithm flags */
22490 	capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22491 	if (capab->encr_hw_algs == NULL)
22492 		goto nomem;
22493 	capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP);
22494 	if (capab->auth_hw_algs == NULL)
22495 		goto nomem;
22496 	/*
22497 	 * Leave encr_algparm NULL for now since we won't need it half
22498 	 * the time
22499 	 */
22500 	return (capab);
22501 
22502 nomem:
22503 	ill_ipsec_capab_free(capab);
22504 	return (NULL);
22505 }
22506 
22507 /*
22508  * Resize capability array.  Since we're exclusive, this is OK.
22509  */
22510 static boolean_t
22511 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid)
22512 {
22513 	ipsec_capab_algparm_t *nalp, *oalp;
22514 	uint32_t olen, nlen;
22515 
22516 	oalp = capab->encr_algparm;
22517 	olen = capab->encr_algparm_size;
22518 
22519 	if (oalp != NULL) {
22520 		if (algid < capab->encr_algparm_end)
22521 			return (B_TRUE);
22522 	}
22523 
22524 	nlen = (algid + 1) * sizeof (*nalp);
22525 	nalp = kmem_zalloc(nlen, KM_NOSLEEP);
22526 	if (nalp == NULL)
22527 		return (B_FALSE);
22528 
22529 	if (oalp != NULL) {
22530 		bcopy(oalp, nalp, olen);
22531 		kmem_free(oalp, olen);
22532 	}
22533 	capab->encr_algparm = nalp;
22534 	capab->encr_algparm_size = nlen;
22535 	capab->encr_algparm_end = algid + 1;
22536 
22537 	return (B_TRUE);
22538 }
22539 
22540 /*
22541  * Compare the capabilities of the specified ill with the protocol
22542  * and algorithms specified by the SA passed as argument.
22543  * If they match, returns B_TRUE, B_FALSE if they do not match.
22544  *
22545  * The ill can be passed as a pointer to it, or by specifying its index
22546  * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments).
22547  *
22548  * Called by ipsec_out_is_accelerated() do decide whether an outbound
22549  * packet is eligible for hardware acceleration, and by
22550  * ill_ipsec_capab_send_all() to decide whether a SA must be sent down
22551  * to a particular ill.
22552  */
22553 boolean_t
22554 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6,
22555     ipsa_t *sa)
22556 {
22557 	boolean_t sa_isv6;
22558 	uint_t algid;
22559 	struct ill_ipsec_capab_s *cpp;
22560 	boolean_t need_refrele = B_FALSE;
22561 
22562 	if (ill == NULL) {
22563 		ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL,
22564 		    NULL, NULL, NULL);
22565 		if (ill == NULL) {
22566 			ip0dbg(("ipsec_capab_match: ill doesn't exist\n"));
22567 			return (B_FALSE);
22568 		}
22569 		need_refrele = B_TRUE;
22570 	}
22571 
22572 	/*
22573 	 * Use the address length specified by the SA to determine
22574 	 * if it corresponds to a IPv6 address, and fail the matching
22575 	 * if the isv6 flag passed as argument does not match.
22576 	 * Note: this check is used for SADB capability checking before
22577 	 * sending SA information to an ill.
22578 	 */
22579 	sa_isv6 = (sa->ipsa_addrfam == AF_INET6);
22580 	if (sa_isv6 != ill_isv6)
22581 		/* protocol mismatch */
22582 		goto done;
22583 
22584 	/*
22585 	 * Check if the ill supports the protocol, algorithm(s) and
22586 	 * key size(s) specified by the SA, and get the pointers to
22587 	 * the algorithms supported by the ill.
22588 	 */
22589 	switch (sa->ipsa_type) {
22590 
22591 	case SADB_SATYPE_ESP:
22592 		if (!(ill->ill_capabilities & ILL_CAPAB_ESP))
22593 			/* ill does not support ESP acceleration */
22594 			goto done;
22595 		cpp = ill->ill_ipsec_capab_esp;
22596 		algid = sa->ipsa_auth_alg;
22597 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs))
22598 			goto done;
22599 		algid = sa->ipsa_encr_alg;
22600 		if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs))
22601 			goto done;
22602 		if (algid < cpp->encr_algparm_end) {
22603 			ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid];
22604 			if (sa->ipsa_encrkeybits < alp->minkeylen)
22605 				goto done;
22606 			if (sa->ipsa_encrkeybits > alp->maxkeylen)
22607 				goto done;
22608 		}
22609 		break;
22610 
22611 	case SADB_SATYPE_AH:
22612 		if (!(ill->ill_capabilities & ILL_CAPAB_AH))
22613 			/* ill does not support AH acceleration */
22614 			goto done;
22615 		if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg,
22616 		    ill->ill_ipsec_capab_ah->auth_hw_algs))
22617 			goto done;
22618 		break;
22619 	}
22620 
22621 	if (need_refrele)
22622 		ill_refrele(ill);
22623 	return (B_TRUE);
22624 done:
22625 	if (need_refrele)
22626 		ill_refrele(ill);
22627 	return (B_FALSE);
22628 }
22629 
22630 
22631 /*
22632  * Add a new ill to the list of IPsec capable ills.
22633  * Called from ill_capability_ipsec_ack() when an ACK was received
22634  * indicating that IPsec hardware processing was enabled for an ill.
22635  *
22636  * ill must point to the ill for which acceleration was enabled.
22637  * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP.
22638  */
22639 static void
22640 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync)
22641 {
22642 	ipsec_capab_ill_t **ills, *cur_ill, *new_ill;
22643 	uint_t sa_type;
22644 	uint_t ipproto;
22645 
22646 	ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) ||
22647 	    (dl_cap == DL_CAPAB_IPSEC_ESP));
22648 
22649 	switch (dl_cap) {
22650 	case DL_CAPAB_IPSEC_AH:
22651 		sa_type = SADB_SATYPE_AH;
22652 		ills = &ipsec_capab_ills_ah;
22653 		ipproto = IPPROTO_AH;
22654 		break;
22655 	case DL_CAPAB_IPSEC_ESP:
22656 		sa_type = SADB_SATYPE_ESP;
22657 		ills = &ipsec_capab_ills_esp;
22658 		ipproto = IPPROTO_ESP;
22659 		break;
22660 	}
22661 
22662 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22663 
22664 	/*
22665 	 * Add ill index to list of hardware accelerators. If
22666 	 * already in list, do nothing.
22667 	 */
22668 	for (cur_ill = *ills; cur_ill != NULL &&
22669 	    (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex ||
22670 	    cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next)
22671 		;
22672 
22673 	if (cur_ill == NULL) {
22674 		/* if this is a new entry for this ill */
22675 		new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP);
22676 		if (new_ill == NULL) {
22677 			rw_exit(&ipsec_capab_ills_lock);
22678 			return;
22679 		}
22680 
22681 		new_ill->ill_index = ill->ill_phyint->phyint_ifindex;
22682 		new_ill->ill_isv6 = ill->ill_isv6;
22683 		new_ill->next = *ills;
22684 		*ills = new_ill;
22685 	} else if (!sadb_resync) {
22686 		/* not resync'ing SADB and an entry exists for this ill */
22687 		rw_exit(&ipsec_capab_ills_lock);
22688 		return;
22689 	}
22690 
22691 	rw_exit(&ipsec_capab_ills_lock);
22692 
22693 	if (ipcl_proto_fanout_v6[ipproto].connf_head != NULL)
22694 		/*
22695 		 * IPsec module for protocol loaded, initiate dump
22696 		 * of the SADB to this ill.
22697 		 */
22698 		sadb_ill_download(ill, sa_type);
22699 }
22700 
22701 /*
22702  * Remove an ill from the list of IPsec capable ills.
22703  */
22704 static void
22705 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap)
22706 {
22707 	ipsec_capab_ill_t **ills, *cur_ill, *prev_ill;
22708 
22709 	ASSERT(dl_cap == DL_CAPAB_IPSEC_AH ||
22710 	    dl_cap == DL_CAPAB_IPSEC_ESP);
22711 
22712 	ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipsec_capab_ills_ah :
22713 	    &ipsec_capab_ills_esp;
22714 
22715 	rw_enter(&ipsec_capab_ills_lock, RW_WRITER);
22716 
22717 	prev_ill = NULL;
22718 	for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index !=
22719 	    ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 !=
22720 	    ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next)
22721 		;
22722 	if (cur_ill == NULL) {
22723 		/* entry not found */
22724 		rw_exit(&ipsec_capab_ills_lock);
22725 		return;
22726 	}
22727 	if (prev_ill == NULL) {
22728 		/* entry at front of list */
22729 		*ills = NULL;
22730 	} else {
22731 		prev_ill->next = cur_ill->next;
22732 	}
22733 	kmem_free(cur_ill, sizeof (ipsec_capab_ill_t));
22734 	rw_exit(&ipsec_capab_ills_lock);
22735 }
22736 
22737 
22738 /*
22739  * Handling of DL_CONTROL_REQ messages that must be sent down to
22740  * an ill while having exclusive access.
22741  */
22742 /* ARGSUSED */
22743 static void
22744 ill_ipsec_capab_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
22745 {
22746 	ill_t *ill = (ill_t *)q->q_ptr;
22747 
22748 	ill_dlpi_send(ill, mp);
22749 }
22750 
22751 
22752 /*
22753  * Called by SADB to send a DL_CONTROL_REQ message to every ill
22754  * supporting the specified IPsec protocol acceleration.
22755  * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP.
22756  * We free the mblk and, if sa is non-null, release the held referece.
22757  */
22758 void
22759 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa)
22760 {
22761 	ipsec_capab_ill_t *ici, *cur_ici;
22762 	ill_t *ill;
22763 	mblk_t *nmp, *mp_ship_list = NULL, *next_mp;
22764 
22765 	ici = (sa_type == SADB_SATYPE_AH) ? ipsec_capab_ills_ah :
22766 	    ipsec_capab_ills_esp;
22767 
22768 	rw_enter(&ipsec_capab_ills_lock, RW_READER);
22769 
22770 	for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) {
22771 		ill = ill_lookup_on_ifindex(cur_ici->ill_index,
22772 		    cur_ici->ill_isv6, NULL, NULL, NULL, NULL);
22773 
22774 		/*
22775 		 * Handle the case where the ill goes away while the SADB is
22776 		 * attempting to send messages.  If it's going away, it's
22777 		 * nuking its shadow SADB, so we don't care..
22778 		 */
22779 
22780 		if (ill == NULL)
22781 			continue;
22782 
22783 		if (sa != NULL) {
22784 			/*
22785 			 * Make sure capabilities match before
22786 			 * sending SA to ill.
22787 			 */
22788 			if (!ipsec_capab_match(ill, cur_ici->ill_index,
22789 			    cur_ici->ill_isv6, sa)) {
22790 				ill_refrele(ill);
22791 				continue;
22792 			}
22793 
22794 			mutex_enter(&sa->ipsa_lock);
22795 			sa->ipsa_flags |= IPSA_F_HW;
22796 			mutex_exit(&sa->ipsa_lock);
22797 		}
22798 
22799 		/*
22800 		 * Copy template message, and add it to the front
22801 		 * of the mblk ship list. We want to avoid holding
22802 		 * the ipsec_capab_ills_lock while sending the
22803 		 * message to the ills.
22804 		 *
22805 		 * The b_next and b_prev are temporarily used
22806 		 * to build a list of mblks to be sent down, and to
22807 		 * save the ill to which they must be sent.
22808 		 */
22809 		nmp = copymsg(mp);
22810 		if (nmp == NULL) {
22811 			ill_refrele(ill);
22812 			continue;
22813 		}
22814 		ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL);
22815 		nmp->b_next = mp_ship_list;
22816 		mp_ship_list = nmp;
22817 		nmp->b_prev = (mblk_t *)ill;
22818 	}
22819 
22820 	rw_exit(&ipsec_capab_ills_lock);
22821 
22822 	nmp = mp_ship_list;
22823 	while (nmp != NULL) {
22824 		/* restore the mblk to a sane state */
22825 		next_mp = nmp->b_next;
22826 		nmp->b_next = NULL;
22827 		ill = (ill_t *)nmp->b_prev;
22828 		nmp->b_prev = NULL;
22829 
22830 		/*
22831 		 * Ship the mblk to the ill, must be exclusive. Keep the
22832 		 * reference to the ill as qwriter_ip() does a ill_referele().
22833 		 */
22834 		(void) qwriter_ip(NULL, ill, ill->ill_wq, nmp,
22835 		    ill_ipsec_capab_send_writer, NEW_OP, B_TRUE);
22836 
22837 		nmp = next_mp;
22838 	}
22839 
22840 	if (sa != NULL)
22841 		IPSA_REFRELE(sa);
22842 	freemsg(mp);
22843 }
22844 
22845 
22846 /*
22847  * Derive an interface id from the link layer address.
22848  * Knows about IEEE 802 and IEEE EUI-64 mappings.
22849  */
22850 static boolean_t
22851 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22852 {
22853 	char		*addr;
22854 
22855 	if (phys_length != ETHERADDRL)
22856 		return (B_FALSE);
22857 
22858 	/* Form EUI-64 like address */
22859 	addr = (char *)&v6addr->s6_addr32[2];
22860 	bcopy((char *)phys_addr, addr, 3);
22861 	addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
22862 	addr[3] = (char)0xff;
22863 	addr[4] = (char)0xfe;
22864 	bcopy((char *)phys_addr + 3, addr + 5, 3);
22865 	return (B_TRUE);
22866 }
22867 
22868 /* ARGSUSED */
22869 static boolean_t
22870 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22871 {
22872 	return (B_FALSE);
22873 }
22874 
22875 /* ARGSUSED */
22876 static boolean_t
22877 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22878     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22879 {
22880 	/*
22881 	 * Multicast address mappings used over Ethernet/802.X.
22882 	 * This address is used as a base for mappings.
22883 	 */
22884 	static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00,
22885 	    0x00, 0x00, 0x00};
22886 
22887 	/*
22888 	 * Extract low order 32 bits from IPv6 multicast address.
22889 	 * Or that into the link layer address, starting from the
22890 	 * second byte.
22891 	 */
22892 	*hw_start = 2;
22893 	v6_extract_mask->s6_addr32[0] = 0;
22894 	v6_extract_mask->s6_addr32[1] = 0;
22895 	v6_extract_mask->s6_addr32[2] = 0;
22896 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22897 	bcopy(ipv6_g_phys_multi_addr, maddr, lla_length);
22898 	return (B_TRUE);
22899 }
22900 
22901 /*
22902  * Indicate by return value whether multicast is supported. If not,
22903  * this code should not touch/change any parameters.
22904  */
22905 /* ARGSUSED */
22906 static boolean_t
22907 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
22908     uint32_t *hw_start, ipaddr_t *extract_mask)
22909 {
22910 	/*
22911 	 * Multicast address mappings used over Ethernet/802.X.
22912 	 * This address is used as a base for mappings.
22913 	 */
22914 	static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e,
22915 	    0x00, 0x00, 0x00 };
22916 
22917 	if (phys_length != ETHERADDRL)
22918 		return (B_FALSE);
22919 
22920 	*extract_mask = htonl(0x007fffff);
22921 	*hw_start = 2;
22922 	bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL);
22923 	return (B_TRUE);
22924 }
22925 
22926 /*
22927  * Derive IPoIB interface id from the link layer address.
22928  */
22929 static boolean_t
22930 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr)
22931 {
22932 	char		*addr;
22933 
22934 	if (phys_length != 20)
22935 		return (B_FALSE);
22936 	addr = (char *)&v6addr->s6_addr32[2];
22937 	bcopy(phys_addr + 12, addr, 8);
22938 	/*
22939 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
22940 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
22941 	 * rules. In these cases, the IBA considers these GUIDs to be in
22942 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
22943 	 * required; vendors are required not to assign global EUI-64's
22944 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
22945 	 * of the interface identifier. Whether the GUID is in modified
22946 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
22947 	 * bit set to 1.
22948 	 */
22949 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
22950 	return (B_TRUE);
22951 }
22952 
22953 /*
22954  * Note on mapping from multicast IP addresses to IPoIB multicast link
22955  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
22956  * The format of an IPoIB multicast address is:
22957  *
22958  *  4 byte QPN      Scope Sign.  Pkey
22959  * +--------------------------------------------+
22960  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
22961  * +--------------------------------------------+
22962  *
22963  * The Scope and Pkey components are properties of the IBA port and
22964  * network interface. They can be ascertained from the broadcast address.
22965  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
22966  */
22967 
22968 static boolean_t
22969 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr,
22970     uint32_t *hw_start, in6_addr_t *v6_extract_mask)
22971 {
22972 	/*
22973 	 * Base IPoIB IPv6 multicast address used for mappings.
22974 	 * Does not contain the IBA scope/Pkey values.
22975 	 */
22976 	static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
22977 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
22978 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22979 
22980 	/*
22981 	 * Extract low order 80 bits from IPv6 multicast address.
22982 	 * Or that into the link layer address, starting from the
22983 	 * sixth byte.
22984 	 */
22985 	*hw_start = 6;
22986 	bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length);
22987 
22988 	/*
22989 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
22990 	 */
22991 	*(maddr + 5) = *(bphys_addr + 5);
22992 	*(maddr + 8) = *(bphys_addr + 8);
22993 	*(maddr + 9) = *(bphys_addr + 9);
22994 
22995 	v6_extract_mask->s6_addr32[0] = 0;
22996 	v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff);
22997 	v6_extract_mask->s6_addr32[2] = 0xffffffffU;
22998 	v6_extract_mask->s6_addr32[3] = 0xffffffffU;
22999 	return (B_TRUE);
23000 }
23001 
23002 static boolean_t
23003 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr,
23004     uint32_t *hw_start, ipaddr_t *extract_mask)
23005 {
23006 	/*
23007 	 * Base IPoIB IPv4 multicast address used for mappings.
23008 	 * Does not contain the IBA scope/Pkey values.
23009 	 */
23010 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
23011 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
23012 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
23013 
23014 	if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr))
23015 		return (B_FALSE);
23016 
23017 	/*
23018 	 * Extract low order 28 bits from IPv4 multicast address.
23019 	 * Or that into the link layer address, starting from the
23020 	 * sixteenth byte.
23021 	 */
23022 	*extract_mask = htonl(0x0fffffff);
23023 	*hw_start = 16;
23024 	bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length);
23025 
23026 	/*
23027 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
23028 	 */
23029 	*(maddr + 5) = *(bphys_addr + 5);
23030 	*(maddr + 8) = *(bphys_addr + 8);
23031 	*(maddr + 9) = *(bphys_addr + 9);
23032 	return (B_TRUE);
23033 }
23034 
23035 /*
23036  * Returns B_TRUE if an ipif is present in the given zone, matching some flags
23037  * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there.
23038  * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with
23039  * the link-local address is preferred.
23040  */
23041 boolean_t
23042 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23043 {
23044 	ipif_t	*ipif;
23045 	ipif_t	*maybe_ipif = NULL;
23046 
23047 	mutex_enter(&ill->ill_lock);
23048 	if (ill->ill_state_flags & ILL_CONDEMNED) {
23049 		mutex_exit(&ill->ill_lock);
23050 		if (ipifp != NULL)
23051 			*ipifp = NULL;
23052 		return (B_FALSE);
23053 	}
23054 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
23055 		if (!IPIF_CAN_LOOKUP(ipif))
23056 			continue;
23057 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
23058 		    ipif->ipif_zoneid != ALL_ZONES)
23059 			continue;
23060 		if ((ipif->ipif_flags & flags) != flags)
23061 			continue;
23062 
23063 		if (ipifp == NULL) {
23064 			mutex_exit(&ill->ill_lock);
23065 			ASSERT(maybe_ipif == NULL);
23066 			return (B_TRUE);
23067 		}
23068 		if (!ill->ill_isv6 ||
23069 		    IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) {
23070 			ipif_refhold_locked(ipif);
23071 			mutex_exit(&ill->ill_lock);
23072 			*ipifp = ipif;
23073 			return (B_TRUE);
23074 		}
23075 		if (maybe_ipif == NULL)
23076 			maybe_ipif = ipif;
23077 	}
23078 	if (ipifp != NULL) {
23079 		if (maybe_ipif != NULL)
23080 			ipif_refhold_locked(maybe_ipif);
23081 		*ipifp = maybe_ipif;
23082 	}
23083 	mutex_exit(&ill->ill_lock);
23084 	return (maybe_ipif != NULL);
23085 }
23086 
23087 /*
23088  * Same as ipif_lookup_zoneid() but looks at all the ills in the same group.
23089  */
23090 boolean_t
23091 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp)
23092 {
23093 	ill_t *illg;
23094 
23095 	/*
23096 	 * We look at the passed-in ill first without grabbing ill_g_lock.
23097 	 */
23098 	if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) {
23099 		return (B_TRUE);
23100 	}
23101 	rw_enter(&ill_g_lock, RW_READER);
23102 	if (ill->ill_group == NULL) {
23103 		/* ill not in a group */
23104 		rw_exit(&ill_g_lock);
23105 		return (B_FALSE);
23106 	}
23107 
23108 	/*
23109 	 * There's no ipif in the zone on ill, however ill is part of an IPMP
23110 	 * group. We need to look for an ipif in the zone on all the ills in the
23111 	 * group.
23112 	 */
23113 	illg = ill->ill_group->illgrp_ill;
23114 	do {
23115 		/*
23116 		 * We don't call ipif_lookup_zoneid() on ill as we already know
23117 		 * that it's not there.
23118 		 */
23119 		if (illg != ill &&
23120 		    ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) {
23121 			break;
23122 		}
23123 	} while ((illg = illg->ill_group_next) != NULL);
23124 	rw_exit(&ill_g_lock);
23125 	return (illg != NULL);
23126 }
23127 
23128 /*
23129  * Check if this ill is only being used to send ICMP probes for IPMP
23130  */
23131 boolean_t
23132 ill_is_probeonly(ill_t *ill)
23133 {
23134 	/*
23135 	 * Check if the interface is FAILED, or INACTIVE
23136 	 */
23137 	if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))
23138 		return (B_TRUE);
23139 
23140 	return (B_FALSE);
23141 }
23142